Debian Codegraph @

bin/picom-trans

raw symbols SCIP
1 #!/bin/sh
2 # SPDX-License-Identifier: MIT
3
4 #
5 # picom-trans
6 # Copyright (c) 2021, Subhaditya Nath
7 # Based on previous works of Christopher Jeffrey
8 #
9
10 #
11 # Conforming to POSIX-1.2007
12 # https://pubs.opengroup.org/onlinepubs/9699919799
13 #
14
15 # Usage:
16 # $ picom-trans [options] [+|-]opacity
17 # By window id
18 # $ picom-trans -w "$WINDOWID" 75
19 # By name
20 # $ picom-trans -n "urxvt" 75
21 # By current window
22 # $ picom-trans -c 75
23 # By selection
24 # $ picom-trans 75
25 # $ picom-trans -s 75
26 # Increment current window 5%
27 # $ picom-trans -c +5
28 # Delete current window's opacity
29 # $ picom-trans -c --delete
30 # Toggle current window's opacity between 90 and unset
31 # $ picom-trans -c --toggle 90
32 # Reset all windows
33 # $ picom-trans --reset
34
35
36 # Save $0 now to print correct value while printing from functions.
37 # Printing errormsgs from functions using "$0" prints the function name
38 # instead of the executable name.
39 EXE_NAME="$0"
40
41
42 # Instead of printing the full path to this file (e.g. /usr/bin/picom-trans)
43 # only print the base name (i.e. picom-trans)
44 EXE_NAME="$(basename "$EXE_NAME")"
45
46
47 print_usage()
48 { #{{
49 echo "Usage: $EXE_NAME [options] [+|-]opacity"
50 echo ""
51 echo "Options:"
52 echo " -h, --help Print this help message."
53 echo " -o, --opacity OPACITY Specify the new opacity value in range 0-100 for the window. If"
54 echo " prefixed with + or -, increment or decrement from the current"
55 echo " opacity of the target window."
56 echo ""
57 echo "Actions:"
58 echo " -g, --get Print current opacity of the target window."
59 echo " -d, --delete Delete opacity of the target window."
60 echo " -t, --toggle Toggle the target window's opacity, i.e. set if not already set"
61 echo " and delete else."
62 echo " -r, --reset Reset opacity for all windows."
63 echo ""
64 echo "Window Selection:"
65 echo " -s, --select Select target window with mouse cursor. (DEFAULT)"
66 echo " -c, --current Select the currently active window as target."
67 echo " -n, --name WINDOW_NAME Specify and try to match a window name."
68 echo " -w, --window WINDOW_ID Specify the window id of the target window."
69 } #}}
70
71
72 parse_args()
73 { #{{
74 i=1 # because we start from "$1", not from "$0"
75 while [ $i -le $# ]
76 do
77 #### [START] Convert GNU longopts to POSIX equivalents ####
78 if [ "$1" = "--${1##--}" ] # check if $1 is a longopt
79 then
80 # Catch invalid options
81 case "$1" in
82 (--opacity=|--name=|--window=)
83 echo "$EXE_NAME: option ${1%=} needs a value" >&2
84 exit 1;;
85 (--opacity|--name|--window)
86 test $i -eq $# \
87 && echo "$EXE_NAME: option $1 needs a value" >&2 \
88 && exit 1;;
89 esac
90
91 # Separate "--ARG=VAL" into "--ARG" "VAL"
92 case "$1" in
93 (--opacity=*|--name=*|--window=*)
94 ARG="$(echo "$1" | sed -E 's/(--[^=]+)=.*$/\1/')"
95 VAL="${1##${ARG}=}"
96 shift && set -- "$ARG" "$VAL" "$@"
97 esac
98
99 # Turn into short form
100 case "$1" in
101 (--help|--opacity|--get|--delete|--toggle|--reset|--select|--current|--name|--window)
102 ARG=${1#-} # remove one '-' from prefix
103 ARG="$(echo "$ARG" | cut -c -2)" # get first two characters
104 shift && set -- "$ARG" "$@"
105 esac
106
107 # If the argument still starts with --, it is an invalid argument
108 case "$1" in
109 (--*)
110 echo "$EXE_NAME: illegal option $1" >&2
111 exit 1
112 esac
113 fi
114 #### [END] Convert GNU longopts to POSIX equivalents ####
115
116
117 #### [START] Prepend '-o' to standalone opacity values ####
118 # Iterate over every argument and check if it is an opacity without the -o
119 # option in the previous argument. If so, then prepend the -o option.
120 # e.g. Turn this -
121 # picom-trans -c +10 -s
122 # into this -
123 # picom-trans -c -o +10 -s
124 #
125 # NOTE: Don't touch arguments that are preceded by -o, -w, or -n (i.e. the
126 # options that take a value.)
127 # e.g. This -
128 # picom-trans -w 75 -o 90
129 # should NOT be turned into this -
130 # picom-trans -w -o 75 -o 90
131 # We ensure this by checking the "$#"th (i.e. the last) argument. If
132 # argument is an option that needs a value, we don't do anything to $1.
133 #
134 # NOTE: we are using printf because most echo implementations aren't
135 # POSIX-compliant. For example, according to POSIX.1-2017, echo doesn't
136 # support any options, so,
137 # $ echo "-n"
138 # should output -
139 # -n
140 # But it doesn't. It instead interprets the "-n" as the option -n, which,
141 # in most implementations, means that the trailing newline should not be
142 # printed.
143 if echo "$1" | grep -qE '^[+-]?[[:digit:]]+%?$' && \
144 ! eval "printf '%s' \"\${$#}\"" | grep -q '^-[hdtrgsc]*[own]$'
145 # NOTE: eval "printf '%s' \"\${$#}\"" means 'print the last argument'
146 # NOTE: The letters inside the first square brackets (ie. hdtrgsc) are
147 # the same as those in the getopts argument, minus those that are
148 # followed by a ':'
149 # NOTE: The letters inside the second square brackets (ie. own) are
150 # the same as those in the getopts argument, minus those that are
151 # NOT followed by a ':'
152 then
153 set -- "$@" "-o"
154 i=$(( i + 1 ))
155 fi
156 #### [END] Prepend '-o' to standalone opacity values ####
157
158
159 # Prepare for next iteration
160 ARG="$1"
161 shift && set -- "$@" "$ARG"
162 i=$(( i + 1 ))
163 done
164
165
166 # NOTE: DO NOT ATTEMPT TO USE "$OPTIND" INSIDE THE getopts LOOP
167 # - https://github.com/yshui/picom/pull/634#discussion_r654571535
168 # - https://www.mail-archive.com/austin-group-l%40opengroup.org/msg04112.html
169 OPTIND=1
170 while getopts 'ho:dtrgsn:w:c' OPTION
171 do
172 case "$OPTION" in
173 (h) print_usage; exit 0;;
174 (o) target_opacity="$OPTARG";;
175 (d) action=delete;;
176 (t) action=toggle;;
177 (r) action=reset;;
178 (g) action=get;;
179 (s) winidtype=; winid=;;
180 (n) winidtype=-name; winid="$OPTARG";;
181 (w) winidtype=-id; winid="$OPTARG";;
182 (c) winidtype=-id; winid="$(get_focused_window_id)";;
183 (\?) exit 1
184 esac
185 done
186 } #}}
187
188
189 get_target_window_id()
190 { #{{
191
192 # Get the output of xwininfo
193 if test -z "$winidtype"
194 then xwininfo_output="$(xwininfo -children -frame)"
195 elif test "$winidtype" = "-name"
196 then xwininfo_output="$(xwininfo -children -name "$winid")"
197 elif test "$winidtype" = "-id"
198 then
199 # First, check if supplied window id is valid
200 if ! echo "$winid" | grep -Eiq '^[[:space:]]*(0x[[:xdigit:]]+|[[:digit:]]+)[[:space:]]*$'
201 then
202 echo "Bad window ID" >&2
203 exit 1
204 fi
205 xwininfo_output="$(xwininfo -children -id "$winid")"
206 fi
207
208 # Extract window id from xwininfo output
209 winid="$(echo "$xwininfo_output" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')"
210 if test -z "$winid"
211 then
212 echo "Failed to find window" >&2
213 exit 1
214 fi
215
216 # Make sure it's not root window
217 if echo "$xwininfo_output" | grep -Fq "Parent window id: 0x0"
218 then
219 echo "Cannot set opacity on root window" >&2
220 exit 1
221 fi
222
223 # If it's not the topmost window, get the topmost window
224 if ! echo "$xwininfo_output" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'
225 then
226 window_tree="$(xwininfo -root -tree)"
227 if test -z "$window_tree"
228 then
229 echo "Failed to get root window tree" >&2
230 exit 1
231 fi
232
233 # Find the highest ancestor of the target window
234 winid="$(echo "$window_tree" \
235 | sed -n "/^\s*$winid/q;s/^ \(0x[[:xdigit:]]*\).*/\1/p" \
236 | tail -n 1)"
237 if test -z "$winid"
238 then
239 echo "Failed to find window in window tree" >&2
240 exit 1
241 fi
242 fi
243 if test -z "$winid"
244 then
245 echo "Failed to find the highest parent window below root of the selected window" >&2
246 exit 1
247 fi
248
249 echo "$winid"
250 } #}}
251
252 get_focused_window_id()
253 { #{{
254 id="$(xprop -root -notype -f _NET_ACTIVE_WINDOW 32x '$0' _NET_ACTIVE_WINDOW)"
255 echo "${id#_NET_ACTIVE_WINDOW}"
256 } #}}
257
258 get_current_opacity()
259 { #{{
260 # Gets current opacity in the range 0-100
261 # Doesn't output anything if opacity isn't set
262 cur="$(xprop -id "$winid" -notype -f _NET_WM_WINDOW_OPACITY 32c '$0' _NET_WM_WINDOW_OPACITY)"
263 cur="${cur#_NET_WM_WINDOW_OPACITY}"
264 cur="${cur%:*}"
265 test -n "$cur" &&
266 cur=$(( cur * 100 / 0xffffffff ))
267 echo "$cur"
268 } #}}
269
270
271 get_opacity()
272 { #{{
273 cur="$(get_current_opacity)"
274 test -z "$cur" && cur=100 # Unset opacity means fully opaque
275 echo "$cur"
276 exit 0
277 } #}}
278
279 delete_opacity()
280 { #{{
281 xprop -id "$winid" -remove _NET_WM_WINDOW_OPACITY
282 exit 0
283 } #}}
284
285 reset_opacity() # Reset opacity of all windows
286 { #{{
287 for winid in $(xwininfo -root -tree | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p')
288 do xprop -id "$winid" -remove _NET_WM_WINDOW_OPACITY 2>/dev/null
289 done
290 exit 0
291 } #}}
292
293 set_opacity()
294 { #{{
295 if ! echo "$target_opacity" | grep -qE '^[+-]?[[:digit:]]+%?$'
296 then
297 if test -z "$target_opacity"
298 then echo "No opacity specified" >&2
299 else echo "Invalid opacity specified: $target_opacity" >&2
300 fi
301 exit 1
302 fi
303
304 # strip trailing '%' sign, if any
305 target_opacity="${target_opacity%%%}"
306
307 if echo "$target_opacity" | grep -q '^[+-]'
308 then
309 current_opacity="$(get_current_opacity)"
310 test -z "$current_opacity" && current_opacity=100
311 target_opacity=$(( current_opacity + target_opacity ))
312 fi
313
314 test $target_opacity -lt 0 && target_opacity=0
315 test $target_opacity -gt 100 && target_opacity=100
316
317 target_opacity=$(( target_opacity * 0xffffffff / 100 ))
318 xprop -id "$winid" -f _NET_WM_WINDOW_OPACITY 32c \
319 -set _NET_WM_WINDOW_OPACITY "$target_opacity"
320
321 exit $?
322 } #}}
323
324 toggle_opacity()
325 { #{{
326 # If opacity is currently set, unset it.
327 # If opacity is currently unset, set opacity to the supplied value. If no
328 # value is supplied, we default to 100%.
329 if test -z "$(get_current_opacity)"
330 then
331 test -n "$target_opacity" || target_opacity=100
332 set_opacity
333 else
334 delete_opacity
335 fi
336 } #}}
337
338
339 # Warn about rename of compton to picom
340 case "$0" in
341 *compton-trans*) echo "Warning: compton has been renamed, please use picom-trans instead" >&2;;
342 esac
343
344
345 # Check if both xwininfo and xprop are available
346 if ! command -v xprop >/dev/null || ! command -v xwininfo >/dev/null
347 then
348 echo "The command xwininfo or xprop is not available. They might reside in a package named xwininfo, xprop, x11-utils, xorg-xprop, or xorg-xwininfo" >&2
349 exit 1
350 fi
351
352
353 # No arguments given. Show help.
354 if test $# -eq 0
355 then
356 print_usage >&2
357 exit 1
358 fi
359
360
361 # Variables
362 # action is set to 'set' by default
363 action=set
364 winid=
365 winidtype=
366 target_opacity=
367
368 # If there's only one argument, and it's a valid opacity
369 # then take it as target_opacity. Else, parse all arguments.
370 if test $# -eq 1 && echo "$1" | grep -qE '^[+-]?[[:digit:]]+%?$'
371 then
372 target_opacity=$1
373 shift
374 else
375 parse_args "$@"
376 fi
377
378
379 # reset_opacity doesn't need $winid
380 case $action in
381 (reset) reset_opacity;;
382 esac
383
384 # Any other action needs $winid
385 #
386 # NOTE: Do NOT change the order of winid= and winidtype= below
387 # the output of get_target_window_id depends on $winidtype
388 #
389 # NOTE: If get_target_window_id returns with a non-zero $?
390 # that must mean that some error occured. So, exit with that same $?
391 #
392 winid=$(get_target_window_id) || exit $?
393 winidtype=-id
394 case $action in
395 (set) set_opacity;;
396 (get) get_opacity;;
397 (delete) delete_opacity;;
398 (toggle) toggle_opacity;;
399 esac
400
401
402 # We should never reach this part of the file
403 echo "This sentence shouldn't have been printed. Please file a bug report." >&2
404 exit 128
405
406
407 # vim:ft=sh:ts=4:sts=4:sw=2:et:fdm=marker:fmr=#{{,#}}:nowrap