summaryrefslogtreecommitdiff
path: root/x11/printscreen/files/printscreen.in
blob: 29feaa18da034e8062283813b894725688128c37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh 

xwdcommon='-nobdrs'
xwdroot='-root'

INFMT=pnm
OUTFMT=png
STOREDIR="${HOME}/.screenshots"
SHOTDATE="$(date +%Y.%m.%d-%H.%M.%S)"

have()
{
	type $1 >/dev/null 2>&1 && return 0
}

parse_options()
{
	local OPT OPTARG OPTIND fmt fmtlist prefix

	while getopts d:f:bhlqs OPT; do
		# escape meta
		OPTARG=${OPTARG%%[;\\\$]*}

		case ${OPT} in
		b)	xwdcommon="${xwdcommon} -frame" ;;
		d)	delay="${OPTARG}" ;;
		f)
			OUTFMT="${OPTARG}"
			unset INFMT

			for fmt in pnm pbm pgm ppm; do
				have "${fmt}to${OUTFMT}" && { INFMT=${fmt}; break; }
			done

			[ -z "${INFMT}" ] && usage
			;;
		l)
			prefix=$(type ppmtoppm 2>/dev/null | \
				sed 's:ppmtoppm is \(.*\)ppmtoppm:\1:')
			if [ -z "${prefix}" ]; then
				echo 'Cannot find image converters. Make sure that netpbm are installed'
				exit 1
			fi

			fmtlist=$(echo ${prefix}/p[nbgp]mto* | tr ' ' '\n' | \
				sed -E 's:^.*/(pnm|pbm|pgm|ppm)to::g')
			echo Supported output formats:
			echo ${fmtlist}
			exit 0
			;;
		q)	xwdcommon="${xwdcommon} -silent";;
		s)	unset xwdroot ;;
		*)	usage ;;
		esac
	done

	OPTC=$((${OPTIND} - 1))
}

usage()
{
	echo "usage: ${0##*/} [-bhlqs] [-d sec] [-f outfmt] [shotname]"
	echo '	-b when selecting a window, grab wm border too'
	echo '	-d wait sec seconds before taking a shot'
	echo '	-f set output format'
	echo '	-h display this help and exit'
	echo '	-l list output formats'
	echo '	-q be silent'
	echo '	-s interactively choose a window'
	echo
	echo "By default screenshots stored in ${STOREDIR}"
	echo
	exit 1
}

parse_options ${1+"$@"}
shift ${OPTC}

if [ -z ${@+1} ]; then
	SHOTNAME="${STOREDIR}/screenshot-${SHOTDATE}.${OUTFMT}"
else
	SHOTNAME=$1
	shift

	[ -z ${@+1} ] || usage
fi

[ -d "${STOREDIR}" ] || mkdir "${STOREDIR}"

[ -n "${delay}" ] && sleep "${delay}"

xwd ${xwdcommon} ${xwdroot} | xwdtopnm 2>/dev/null | \
	"${INFMT}to${OUTFMT}" 2>/dev/null > "${SHOTNAME}"