Skip to content

Instantly share code, notes, and snippets.

@Sporif
Last active April 1, 2021 20:33
Show Gist options
  • Save Sporif/8f472bc603dac7564fa12ee0a1091498 to your computer and use it in GitHub Desktop.
Save Sporif/8f472bc603dac7564fa12ee0a1091498 to your computer and use it in GitHub Desktop.
Suspend KWin X11 compositing when certain windows are in focus and fullscreen. Looks in ~/.config/suspend-kwin/app_list for programs to suspend.
#!/usr/bin/env bash
# Suspend KWin X11 compositing when certain windows are in focus and fullscreen
# Requirements:
# qdbus: suspend compositing, check status of compositing
# xprop: find window in focus and if fullscreen
# The "Suspend Compositing" global shortcut to be present and enabled
# Credits to: https://askubuntu.com/a/1017567, https://bbs.archlinux.org/viewtopic.php?pid=994356#p994356
# Specifiy apps to suspend via their window class name
# Run this command to list the class names of windows as they are focused:
# xprop -spy -root _NET_ACTIVE_WINDOW | grep --line-buffered -o '0[xX][a-zA-Z0-9]\{7\}' | while read -r id; do xprop -id $id WM_CLASS; done
# $class_name is passed to grep
# match a pattern of class names in a file and ignore case
class_name="-i -f $HOME/.config/suspend-kwin/app_list"
#class_name="$1"
#class_name="overwatch"
# Only suspend when app is fullscreen as well
fullscreen_only=true
toggleCompositing() {
qdbus org.kde.kglobalaccel /component/kwin invokeShortcut "Suspend Compositing"
}
isCompositingActive() {
qdbus org.kde.KWin /Compositor active
}
turnEffectsOn() {
if [ "$(isCompositingActive)" == "false" ]; then
echo -e " Compositing OFF - turning ON\n"
toggleCompositing
fi
}
turnEffectsOff() {
if [ "$(isCompositingActive)" == "true" ]; then
echo -e " Compositing ON - turning OFF\n"
toggleCompositing
fi
}
checkKwin() {
if ! pgrep kwin_x11 &>/dev/null; then
echo "ERROR: kwin_x11 not running"
exit 1
fi
}
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
echo -e "\nclass_name: $class_name"
echo -e "fullscreen_only: $fullscreen_only\n"
xprop -spy -root _NET_ACTIVE_WINDOW | grep --line-buffered -o '0[xX][a-zA-Z0-9]\{7\}' |
while read -r id; do
checkKwin
class_old="$class"
class="$(xprop -id "$id" WM_CLASS)"
[ "$class" == "$class_old" ] && continue
class_wanted="$(echo "$class" | grep $class_name)"
if [ -n "$class_wanted" ]; then
if [ "$fullscreen_only" == "true" ]; then
is_fullscreen="$(xprop -id "$id" | grep "_NET_WM_STATE_FULLSCREEN")"
[ -n "$is_fullscreen" ] && \
echo -e "${red}$class_wanted${reset} - in focus and fullscreen - should be OFF" && \
turnEffectsOff
[ -z "$is_fullscreen" ] && \
echo -e "${green}$class_wanted${reset} - in focus but not fullscreen - should be ON" && \
turnEffectsOn
else
echo -e "${red}$class_wanted${reset} - in focus - should be OFF"
turnEffectsOff
fi
else
echo "${green}$class${reset} - in focus - should be ON"
turnEffectsOn
fi
done
@Sporif
Copy link
Author

Sporif commented Oct 22, 2018

Example .config/suspend-kwin/app_list:

overwatch
witcher3
bf1
bf4
gta5
batmanak
csgo_linux64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment