| 1 | #!/bin/shscip-shell filesystem . . `/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="$(basenamescip-shell system . . basename. "$EXE_NAME")" |
| 45 | |
| 46 | |
| 47 | print_usagescip-shell shell shell-project 0.0.0 print_usage().() |
| 48 | { #{{ |
| 49 | echoscip-shell system . . echo. "Usage: $EXE_NAME [options] [+|-]opacity" |
| 50 | echoscip-shell system . . echo. "" |
| 51 | echoscip-shell system . . echo. "Options:" |
| 52 | echoscip-shell system . . echo. " -h, --help Print this help message." |
| 53 | echoscip-shell system . . echo. " -o, --opacity OPACITY Specify the new opacity value in range 0-100 for the window. If" |
| 54 | echoscip-shell system . . echo. " prefixed with + or -, increment or decrement from the current" |
| 55 | echoscip-shell system . . echo. " opacity of the target window." |
| 56 | echoscip-shell system . . echo. "" |
| 57 | echoscip-shell system . . echo. "Actions:" |
| 58 | echoscip-shell system . . echo. " -g, --get Print current opacity of the target window." |
| 59 | echoscip-shell system . . echo. " -d, --delete Delete opacity of the target window." |
| 60 | echoscip-shell system . . echo. " -t, --toggle Toggle the target window's opacity, i.e. set if not already set" |
| 61 | echoscip-shell system . . echo. " and delete else." |
| 62 | echoscip-shell system . . echo. " -r, --reset Reset opacity for all windows." |
| 63 | echoscip-shell system . . echo. "" |
| 64 | echoscip-shell system . . echo. "Window Selection:" |
| 65 | echoscip-shell system . . echo. " -s, --select Select target window with mouse cursor. (DEFAULT)" |
| 66 | echoscip-shell system . . echo. " -c, --current Select the currently active window as target." |
| 67 | echoscip-shell system . . echo. " -n, --name WINDOW_NAME Specify and try to match a window name." |
| 68 | echoscip-shell system . . echo. " -w, --window WINDOW_ID Specify the window id of the target window." |
| 69 | } #}} |
| 70 | |
| 71 | |
| 72 | parse_argsscip-shell shell shell-project 0.0.0 parse_args().() |
| 73 | { #{{ |
| 74 | i=1 # because we start from "$1", not from "$0" |
| 75 | while [scip-shell system . . `[`. $i -le $# ] |
| 76 | do |
| 77 | #### [START] Convert GNU longopts to POSIX equivalents #### |
| 78 | if [scip-shell system . . `[`. "$1" = "--${1##--}" ] # check if $1 is a longopt |
| 79 | then |
| 80 | # Catch invalid options |
| 81 | case "$1" in |
| 82 | (--opacity=|--name=|--window=) |
| 83 | echoscip-shell system . . echo. "$EXE_NAME: option ${1%=} needs a value" >&2 |
| 84 | exit 1;; |
| 85 | (--opacity|--name|--window) |
| 86 | testscip-shell system . . test. $i -eq $# \ |
| 87 | && echoscip-shell system . . 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="$(echoscip-shell system . . echo. "$1" | sedscip-shell system . . 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="$(echoscip-shell system . . echo. "$ARG" | cutscip-shell system . . 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 | echoscip-shell system . . 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 echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -qE '^[+-]?[[:digit:]]+%?$' && \ |
| 144 | ! eval "printf '%s' \"\${$#}\"" | grepscip-shell system . . 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_usagescip-shell shell shell-project 0.0.0 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_idscip-shell shell shell-project 0.0.0 get_target_window_id().() |
| 190 | { #{{ |
| 191 | |
| 192 | # Get the output of xwininfo |
| 193 | if testscip-shell system . . test. -z "$winidtype" |
| 194 | then xwininfo_output="$(xwininfo -children -frame)" |
| 195 | elif testscip-shell system . . test. "$winidtype" = "-name" |
| 196 | then xwininfo_output="$(xwininfo -children -name "$winid")" |
| 197 | elif testscip-shell system . . test. "$winidtype" = "-id" |
| 198 | then |
| 199 | # First, check if supplied window id is valid |
| 200 | if ! echoscip-shell system . . echo. "$winid" | grepscip-shell system . . grep. -Eiq '^[[:space:]]*(0x[[:xdigit:]]+|[[:digit:]]+)[[:space:]]*$' |
| 201 | then |
| 202 | echoscip-shell system . . 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="$(echoscip-shell system . . echo. "$xwininfo_output" | sedscip-shell system . . sed. -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')" |
| 210 | if testscip-shell system . . test. -z "$winid" |
| 211 | then |
| 212 | echoscip-shell system . . echo. "Failed to find window" >&2 |
| 213 | exit 1 |
| 214 | fi |
| 215 | |
| 216 | # Make sure it's not root window |
| 217 | if echoscip-shell system . . echo. "$xwininfo_output" | grepscip-shell system . . grep. -Fq "Parent window id: 0x0" |
| 218 | then |
| 219 | echoscip-shell system . . 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 ! echoscip-shell system . . echo. "$xwininfo_output" | grepscip-shell system . . grep. -q 'Parent window id: 0x[[:xdigit:]]* (the root window)' |
| 225 | then |
| 226 | window_tree="$(xwininfo -root -tree)" |
| 227 | if testscip-shell system . . test. -z "$window_tree" |
| 228 | then |
| 229 | echoscip-shell system . . 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="$(echoscip-shell system . . echo. "$window_tree" \ |
| 235 | | sed -n "/^\s*$winid/q;s/^ \(0x[[:xdigit:]]*\).*/\1/p" \ |
| 236 | | tail -n 1)" |
| 237 | if testscip-shell system . . test. -z "$winid" |
| 238 | then |
| 239 | echoscip-shell system . . echo. "Failed to find window in window tree" >&2 |
| 240 | exit 1 |
| 241 | fi |
| 242 | fi |
| 243 | if testscip-shell system . . test. -z "$winid" |
| 244 | then |
| 245 | echoscip-shell system . . echo. "Failed to find the highest parent window below root of the selected window" >&2 |
| 246 | exit 1 |
| 247 | fi |
| 248 | |
| 249 | echoscip-shell system . . echo. "$winid" |
| 250 | } #}} |
| 251 | |
| 252 | get_focused_window_idscip-shell shell shell-project 0.0.0 get_focused_window_id().() |
| 253 | { #{{ |
| 254 | id="$(xprop -root -notype -f _NET_ACTIVE_WINDOW 32x '$0' _NET_ACTIVE_WINDOW)" |
| 255 | echoscip-shell system . . echo. "${id#_NET_ACTIVE_WINDOW}" |
| 256 | } #}} |
| 257 | |
| 258 | get_current_opacityscip-shell shell shell-project 0.0.0 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 | testscip-shell system . . test. -n "$cur" && |
| 266 | cur=$(( cur * 100 / 0xffffffff )) |
| 267 | echoscip-shell system . . echo. "$cur" |
| 268 | } #}} |
| 269 | |
| 270 | |
| 271 | get_opacityscip-shell shell shell-project 0.0.0 get_opacity().() |
| 272 | { #{{ |
| 273 | cur="$(get_current_opacityscip-shell shell shell-project 0.0.0 get_current_opacity().)" |
| 274 | testscip-shell system . . test. -z "$cur" && cur=100 # Unset opacity means fully opaque |
| 275 | echoscip-shell system . . echo. "$cur" |
| 276 | exit 0 |
| 277 | } #}} |
| 278 | |
| 279 | delete_opacityscip-shell shell shell-project 0.0.0 delete_opacity().() |
| 280 | { #{{ |
| 281 | xprop -id "$winid" -remove _NET_WM_WINDOW_OPACITY |
| 282 | exit 0 |
| 283 | } #}} |
| 284 | |
| 285 | reset_opacityscip-shell shell shell-project 0.0.0 reset_opacity().() # Reset opacity of all windows |
| 286 | { #{{ |
| 287 | for winid in $(xwininfo -root -tree | sedscip-shell system . . sed. -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p') |
| 288 | do xprop -id "$winid" -remove _NET_WM_WINDOW_OPACITY 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 289 | done |
| 290 | exit 0 |
| 291 | } #}} |
| 292 | |
| 293 | set_opacityscip-shell shell shell-project 0.0.0 set_opacity().() |
| 294 | { #{{ |
| 295 | if ! echoscip-shell system . . echo. "$target_opacity" | grepscip-shell system . . grep. -qE '^[+-]?[[:digit:]]+%?$' |
| 296 | then |
| 297 | if testscip-shell system . . test. -z "$target_opacity" |
| 298 | then echoscip-shell system . . echo. "No opacity specified" >&2 |
| 299 | else echoscip-shell system . . 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 echoscip-shell system . . echo. "$target_opacity" | grepscip-shell system . . grep. -q '^[+-]' |
| 308 | then |
| 309 | current_opacity="$(get_current_opacityscip-shell shell shell-project 0.0.0 get_current_opacity().)" |
| 310 | testscip-shell system . . test. -z "$current_opacity" && current_opacity=100 |
| 311 | target_opacity=$(( current_opacity + target_opacity )) |
| 312 | fi |
| 313 | |
| 314 | testscip-shell system . . test. $target_opacity -lt 0 && target_opacity=0 |
| 315 | testscip-shell system . . 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_opacityscip-shell shell shell-project 0.0.0 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 testscip-shell system . . test. -z "$(get_current_opacityscip-shell shell shell-project 0.0.0 get_current_opacity().)" |
| 330 | then |
| 331 | testscip-shell system . . test. -n "$target_opacity" || target_opacity=100 |
| 332 | set_opacityscip-shell shell shell-project 0.0.0 set_opacity(). |
| 333 | else |
| 334 | delete_opacityscip-shell shell shell-project 0.0.0 delete_opacity(). |
| 335 | fi |
| 336 | } #}} |
| 337 | |
| 338 | |
| 339 | # Warn about rename of compton to picom |
| 340 | case "$0" in |
| 341 | *compton-trans*) echoscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`. || ! command -v xwininfo >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 347 | then |
| 348 | echoscip-shell system . . 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 testscip-shell system . . test. $# -eq 0 |
| 355 | then |
| 356 | print_usagescip-shell shell shell-project 0.0.0 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 testscip-shell system . . test. $# -eq 1 && echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -qE '^[+-]?[[:digit:]]+%?$' |
| 371 | then |
| 372 | target_opacity=$1 |
| 373 | shift |
| 374 | else |
| 375 | parse_argsscip-shell shell shell-project 0.0.0 parse_args(). "$@" |
| 376 | fi |
| 377 | |
| 378 | |
| 379 | # reset_opacity doesn't need $winid |
| 380 | case $action in |
| 381 | (reset) reset_opacityscip-shell shell shell-project 0.0.0 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_idscip-shell shell shell-project 0.0.0 get_target_window_id().) || exit $? |
| 393 | winidtype=-id |
| 394 | case $action in |
| 395 | (set) set_opacityscip-shell shell shell-project 0.0.0 set_opacity().;; |
| 396 | (get) get_opacityscip-shell shell shell-project 0.0.0 get_opacity().;; |
| 397 | (delete) delete_opacityscip-shell shell shell-project 0.0.0 delete_opacity().;; |
| 398 | (toggle) toggle_opacityscip-shell shell shell-project 0.0.0 toggle_opacity().;; |
| 399 | esac |
| 400 | |
| 401 | |
| 402 | # We should never reach this part of the file |
| 403 | echoscip-shell system . . 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 |