1#!/bin/sh
2
3# === Verify `compton --dbus` status ===
4
5if [ -z "`dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep compton`" ]; then
6  echo "compton DBus interface unavailable"
7  if [ -n "`pgrep compton`" ]; then
8    echo "compton running without dbus interface"
9    #killall compton & # Causes all windows to flicker away and come back ugly.
10    #compton --dbus & # Causes all windows to flicker away and come back beautiful
11  else
12    echo "compton not running"
13  fi
14  exit 1;
15fi
16
17# === Setup sed ===
18
19if [ -z "$SED" ]; then
20  SED="sed"
21  command -v gsed > /dev/null && SED="gsed"
22fi
23
24# === Get connection parameters ===
25
26dpy=$(echo -n "$DISPLAY" | tr -c '[:alnum:]' _)
27
28if [ -z "$dpy" ]; then
29  echo "Cannot find display."
30  exit 1;
31fi
32
33service="com.github.chjj.compton.${dpy}"
34interface="com.github.chjj.compton"
35compton_dbus="dbus-send --print-reply --dest="${service}" / "${interface}"."
36type_win='uint32'
37type_enum='uint16'
38
39# === Color Inversion ===
40
41# Get window ID of window to invert
42if [ -z "$1" -o "$1" = "selected" ]; then
43  window=$(xwininfo -frame | sed -n 's/^xwininfo: Window id: \(0x[[:xdigit:]][[:xdigit:]]*\).*/\1/p') # Select window by mouse
44elif [ "$1" = "focused" ]; then
45  # Ensure we are tracking focus
46  ${compton_dbus}opts_set string:track_focus boolean:true &
47  window=$(${compton_dbus}find_win string:focused | $SED -n 's/^[[:space:]]*'${type_win}'[[:space:]]*\([[:digit:]]*\).*/\1/p') # Query compton for the active window
48elif echo "$1" | grep -Eiq '^([[:digit:]][[:digit:]]*|0x[[:xdigit:]][[:xdigit:]]*)$'; then
49  window="$1" # Accept user-specified window-id if the format is correct
50else
51  echo "$0" "[ selected | focused | window-id ]"
52fi
53
54# Color invert the selected or focused window
55if [ -n "$window" ]; then
56  invert_status="$(${compton_dbus}win_get "${type_win}:${window}" string:invert_color | $SED -n 's/^[[:space:]]*boolean[[:space:]]*\([[:alpha:]]*\).*/\1/p')"
57  if [ "$invert_status" = true ]; then
58    invert=0 # Set the window to have normal color
59  else
60    invert=1 # Set the window to have inverted color
61  fi
62  ${compton_dbus}win_set "${type_win}:${window}" string:invert_color_force "${type_enum}:${invert}" &
63else
64  echo "Cannot find $1 window."
65  exit 1;
66fi
67exit 0;
68