summarylogtreecommitdiffstats
path: root/picom-inverter
blob: 618d4130bd1e6cc42ea41a5a44675ff94e0b7cf1 (plain)
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
#!/bin/sh

# === Verify `picom --dbus` status ===

if [ -z "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep compton)" ]; then
  echo "compton DBus interface unavailable"
  if [ -n "$(pgrep picom)" ]; then
    echo "picom running without dbus interface"
    #killall picom & # Causes all windows to flicker away and come back ugly.
    #picom --dbus & # Causes all windows to flicker away and come back beautiful
  else
    echo "picom not running"
  fi
  exit 1;
fi

# === Setup sed ===

if [ -z "$SED" ]; then
  SED="sed"
  command -v gsed > /dev/null && SED="gsed"
fi

# === Get connection parameters ===

dpy=$(echo -n "$DISPLAY" | tr -c '[:alnum:]' _)

if [ -z "$dpy" ]; then
  echo "Cannot find display."
  exit 1;
fi

service="com.github.chjj.compton.${dpy}"
interface="com.github.chjj.compton"
picom_dbus="dbus-send --print-reply --dest="${service}" / "${interface}"."
type_win='uint32'
type_enum='uint16'

# === Color Inversion ===

# Get window ID of window to invert
if [ -z "$1" -o "$1" = "selected" ]; then
  window=$(xwininfo -frame | sed -n 's/^xwininfo: Window id: \(0x[[:xdigit:]][[:xdigit:]]*\).*/\1/p') # Select window by mouse
elif [ "$1" = "focused" ]; then
  # Ensure we are tracking focus
  ${picom_dbus}opts_set string:track_focus boolean:true &
  window=$(${picom_dbus}find_win string:focused | $SED -n 's/^[[:space:]]*'${type_win}'[[:space:]]*\([[:digit:]]*\).*/\1/p') # Query picom for the active window
elif echo "$1" | grep -Eiq '^([[:digit:]][[:digit:]]*|0x[[:xdigit:]][[:xdigit:]]*)$'; then
  window="$1" # Accept user-specified window-id if the format is correct
else
  echo "$0" "[ selected | focused | window-id ]"
fi

# Color invert the selected or focused window
if [ -n "$window" ]; then
  invert_status="$(${picom_dbus}win_get "${type_win}:${window}" string:invert_color | $SED -n 's/^[[:space:]]*boolean[[:space:]]*\([[:alpha:]]*\).*/\1/p')"
  if [ "$invert_status" = true ]; then
    invert=0 # Set the window to have normal color
  else
    invert=1 # Set the window to have inverted color
  fi
  ${picom_dbus}win_set "${type_win}:${window}" string:invert_color_force "${type_enum}:${invert}" &
else
  echo "Cannot find $1 window."
  exit 1;
fi
exit 0;