Skip to content

Instantly share code, notes, and snippets.

@jigpu
Last active September 29, 2015 23:06
Show Gist options
  • Save jigpu/eccc75b1096a9f6ac898 to your computer and use it in GitHub Desktop.
Save jigpu/eccc75b1096a9f6ac898 to your computer and use it in GitHub Desktop.
Wacom configuration daemon for Linux
#!/bin/bash
# This script can be run in the background, where it will attempt to
# handle configuration of all Wacom devices that are currently plugged
# in, as well as any Wacom devices that may be plugged in later. It
# does this by monitoring the system for hotplug events and then using
# xsetwacom to apply the appropriate configuration.
#
# This should be useful for users of alternative desktop environments
# (e.g. XFCE, MATE, Enlightenment, etc.) which don't provide their own
# Wacom control panels or daemons. Its potentially also useful for even
# GNOME users as a way of working around its buggy handling of ExpressKeys
# with low-end Bamboo/Intuos tablets.
#
# To use, simply adjust the configuration below. Each named tablet may have
# several lines of configuration, the text of which is simply the part of
# the 'xsetwacom' command which you would normally type out after the
# device name. For instance, P["foo"]="bar\nbaz" is interpreted by this
# script as a request to run `xsetwacom set "foo" bar` as well as
# `xsetwacom set "foo" baz`.
declare -A P
export P
########################################################################
# BEGIN EDITS HERE
########################################################################
if [ x"${XDG_CURRENT_DESKTOP}" == x"GNOME" ]; then
# Some settings are specific to GNOME, such as the requirement to
# fix the button mapping on the low-end Bamboo/Intuos tablets (see
# http://sourceforge.net/p/linuxwacom/bugs/244/)
P["Wacom Graphire4 4x5 Pad pad"]="
Button 8 button 1
Button 9 button 2
"
P["Wacom Bamboo 16FG 6x8 Pad pad"]="
Button 1 button 1
Button 3 button 2
Button 8 button 3
Button 9 button 4
"
P["Wacom Intuos PT M Pad pad"]="
Button 1 button 1
Button 3 button 2
Button 8 button 3
Button 9 button 4
"
else
# Some settings (e.g. display mapping) are already handled by
# GNOME and only need to be set if we're running a different
# desktop environment.
P["Wacom Cintiq 24HD touch Finger touch"]="
MapToOutput DVI-0
"
P["Wacom Cintiq 24HD touch Pen stylus"]="
MapToOutput DVI-0
"
fi
########################################################################
# DO NOT EDIT BELOW THIS POINT
########################################################################
################
# Reads lines of input from stdin and filters out anything which is not
# a hotplug event for an input device. This expects to be fed with input
# from 'udevadm monitor'.
#####
filter_hotplugged_input_devices() {
while read -r LINE; do
echo "${LINE}" | grep -q "add"
if [ $? -ne 0 ]; then
>&2 echo "Device change noticed, but ignored (not an \"add\")"
continue
fi
EVENT=$(echo "${LINE}" | grep -Eo "event[0-9]+")
if [ x"$EVENT" == "x" ]; then
continue
fi
>&2 echo "New input device hotplugged: $EVENT"
echo "/dev/input/$EVENT"
done
}
################
# Reads lines of input that contain device files and attempts to locate
# the xinput device IDs that correspond to them. If a provided device
# file is not used for any X devices, nothing will be output. If a
# device is used by multiple X devices, multiple lines will be printed,
# one for each X device.
#####
devicenames_to_xinput_ids() {
while read DEVICE; do
>&2 echo "Determining what Xinput devices use $DEVICE"
for ID in $(xinput list --id-only); do
if $(xinput list-props $ID | grep -q \""$DEVICE\""); then
NAME=$(xinput --list --name-only $ID)
>&2 echo "Xinput device $ID ($NAME) uses $DEVICE"
echo $ID;
fi
done
done
}
################
# Reads lines of input that contain X device IDs and filters out any
# that are not under the control of the 'wacom' driver.
#####
filter_wacom_xinput_ids() {
while read ID; do
xinput list-props $ID | grep -q "Wacom Debug Levels"
if [ $? -ne 0 ]; then
>&2 echo "Ignoring Xinput device $ID since it is not handled by the wacom driver"
continue
fi
>&2 echo "Xinput device $ID appears to be handled by wacom driver"
echo $ID
done
}
################
# Reads lines of input that contain X device IDs of wacom devices and
# uses xsetwacom to apply configurations that may exist for a device
# which shares the same name.
#####
configure_wacom_devices() {
while read ID; do
NAME=$(xinput --list --name-only $ID)
if [ x"${P[$NAME]}" == x ]; then
>&2 echo "No configuration found for $NAME"
continue
fi
while read -r PREFERENCE; do
if [ x"$PREFERENCE" == "x" ]; then
continue
fi;
CMD="xsetwacom set \"$NAME\" $PREFERENCE"
echo "Running \`$CMD\`"
eval $CMD
done < <(echo "${P[$NAME]}")
done
}
cat <(ls /dev/input/event*) \
<(stdbuf -oL udevadm monitor -u -s input | filter_hotplugged_input_devices) | \
devicenames_to_xinput_ids | \
filter_wacom_xinput_ids | \
configure_wacom_devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment