Skip to content

Instantly share code, notes, and snippets.

@jigpu
Last active January 31, 2022 16:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jigpu/8e9f435fd69580440d54932872019215 to your computer and use it in GitHub Desktop.
Save jigpu/8e9f435fd69580440d54932872019215 to your computer and use it in GitHub Desktop.
Apply application-specific profiles with xsetwacom on Linux
#!/bin/bash
# wacom_app_config.sh
#
# BASH script to automatically run change Wacom driver config
# based on the focused application.
#
# Limited support for use under GNOME and its forks due to the
# way it does its own device/took configuration, but should work
# fine elsewhere. It may be possible to use the power of dconf
# to list/read/write keys under '/org/gnome/desktop/peripherals'
# to get a bit more GNOME compatibility.
#
# The sky's the limit
function apply_config {
CONFIG=$1
reset_config
case "$CONFIG" in
blender)
# Insert any command to run when switching to blender here...
;;
gimp)
# Insert any command to run when switching to gimp here...
# Touchring zoom for "all" pads
wac_config *PAD "AbsWheelDown key +SHIFT +"
wac_config *PAD "AbsWheelUp key -"
;;
krita)
# Insert any command to run when switching to krita here...
# Touchring zoom for "all" pads
wac_config *PAD "AbsWheelDown key +CTRL +SHIFT +"
wac_config *PAD "AbsWheelUp key +CTRL -"
;;
default)
# Insert any command to run when switching to any unrecognized app here...
# Pan through documents and webpages with lower side-switch with "all" styli
wac_config *STYLUS "button 2 pan"
;;
esac
}
function apply_config_for_binary {
APP_BINARY=$1
case "$APP_BINARY" in
# Add new cases here for each binary that you want to handle.
#
# To find the name of the binary for any window, simply click
# on it while this script is running. Some programs require the
# the name of the binary (e.g. Blender is `blender`) but others
# seem to include the full path (e.g. Firefox is `/usr/lib/firefox/firefox`).
blender)
apply_config "blender";;
krita)
apply_config "krita";;
gimp-2.10)
apply_config "gimp";;
*)
apply_config "default";;
esac
}
#######################################################################
### Here be dragons.
#######################################################################
# Set up generally-sane defaults
function reset_config {
for BUTTON in 1 2 3 $(seq 8 20); do
wac_config *PAD "button $BUTTON $BUTTON"
done
for BUTTON in 1 2 3; do
wac_config *STYLUS "button $BUTTON $BUTTON"
wac_config *ERASER "button $BUTTON $BUTTON"
done
wac_config *PAD "AbsWheelUp button 4"
wac_config *PAD "AbsWheelDown button 5"
wac_config *PAD "RelWheelUp button 4"
wac_config *PAD "RelWheelDown button 5"
}
# Call xsetwacom for the named device
function wac_config {
DEVICE=$1
SUFFIX=$2
if [[ "${DEVICE:0:1}" == "*" ]]; then
DEVICE="type: ${DEVICE#?}"
fi
DEVICE_IDS=$(wac_devices "$DEVICE")
if [[ -z "$DEVICE_IDS" ]]; then
echo "Unable to find any matches for $DEVICE"
break
fi
for ID in $DEVICE_IDS; do
xsetwacom set $ID $SUFFIX 2>/dev/null
done
}
# Find device IDs based on a filter
function wac_devices {
xsetwacom list | grep "$1" | cut -f2 | cut -d: -f2
}
# Keep an eye on the active window and change settings as needed
function window_spy {
xprop -root -spy 32x '\t$0\n' _NET_ACTIVE_WINDOW | while read LINE; do
WID=$(echo "$LINE" | cut -f2)
APP_TITLE=$(xprop -id $WID '\t$0+\n' _NET_WM_NAME | cut -f2)
APP_STATE=$(xprop -id $WID '\t$0+\n' _NET_WM_STATE | cut -f2)
APP_BINARY=$(ps -p $(xprop -id $WID '\t$0+\n' _NET_WM_PID | cut -f2) -o cmd h)
echo "Now focused: $APP_BINARY ($APP_STATE) -- $APP_TITLE"
if [[ "$APP_STATE" == *"_NET_WM_STATE_SKIP_TASKBAR"* ]]; then
echo "Ignoring focus change"
continue
fi
apply_config_for_binary "$APP_BINARY"
done
}
window_spy &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment