+#!/bin/bash
+
+kernel_pkg_ver() {
+ pacman -Qi linux | sed -rn 's/^Version\s*:\s*(.*)$/\1/p'
+}
+
+kernel_running_ver() {
+ uname -r | sed -r 's/-/./'
+}
+
+try_finding_stylus() {
+ local found=0
+ if [[ "$(kernel_pkg_ver)" != "$(kernel_running_ver)" ]]; then
+ echo "Warning: Kernel was updated: You need a reboot!" >&2
+ fi
+ echo "Tip with the stylus on monitor ${1}"
+ for (( i = 0; i < 16; i++ )); do
+ sleep 1
+ if grep -Fq "${2}" < <(xinput list --name-only); then
+ found=1
+ break
+ fi
+ done
+ if (( !found )); then
+ echo "Error: Stylus was not found: ${2}" >&2
+ return 1
+ fi
+
+}
+
+setup_styluses() {
+ local -A styluses=(
+ [eDP-1]="ELAN9008:00 04F3:425B Stylus Pen (0)"
+ [eDP-2]="ELAN9009:00 04F3:425A Stylus Pen (0)"
+ )
+ local mon stylus
+ for mon in "${!styluses[@]}"; do
+ stylus="${styluses[${mon}]}"
+ xinput disable "${stylus}"
+ if grep -q "${mon}" < <(xrandr --listactivemonitors); then
+ if ! grep -Fq "${stylus}" < <(xinput list --name-only); then
+ try_finding_stylus "${mon}" "${stylus}" || return 1
+ fi
+ xinput enable "${stylus}"
+ xinput --map-to-output "${stylus}" "${mon}"
+ echo "$(date) mapped stylus pen ${stylus%% *} to ${mon}" >&2
+ else
+ echo "$(date) disabled stylus pen ${stylus%% *}" >&2
+ fi
+ done
+}
+
+setup_touchpads() {
+ local touchpads=(
+ "ELAN9008:00 04F3:425B"
+ "ELAN9008:00 04F3:425B Touchpad"
+ "ELAN9009:00 04F3:425A"
+ "ELAN9009:00 04F3:425A Touchpad"
+ )
+ local touchpad
+ for touchpad in "${touchpads[@]}"; do
+ xinput disable "${touchpad}"
+ done
+}
+
+check_config () {
+ local config
+ for config in "${configs[@]}"; do
+ if [[ "${1}" == "${config}" ]]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+exit_script() {
+ local ret="${?}"
+ if (( wait_for_keypress )); then
+ echo "Press a key!"
+ read -n1
+ fi
+ echo exit "${1:-${ret}}"
+}
+
+help() {
+ echo "TODO: help text"
+ exit 0
+}
+
+wait_for_keypress=0
+load_saved=
+configs=()
+
+if ! output="$(lxqt-config-monitor --list-saved 2>/dev/null)"; then
+ lxqt-config-monitor --list-saved
+ exit_script
+elif [[ "${output}" ]]; then
+ mapfile -d $'\n' -t configs < <(printf '%s\n' "${output}")
+fi
+unset output
+list_saved_failed="${?}"
+while (( ${#} )); do
+ case "${1}" in
+ --wait-for-keypress)
+ wait_for_keypress=1
+ ;;
+ --load-saved)
+ load_saved="${2}"
+ shift
+ ;;
+ --help)
+ help
+ ;;
+ *)
+ echo "Error: unknown argument: ${1}" >&2
+ exit_script 1
+ ;;
+ esac
+ shift
+done
+
+if [[ "${load_saved}" ]]; then
+ (( list_saved_failed )) && exit "${list_saved_failed}"
+ if check_config "${load_saved}"; then
+ lxqt-config-monitor --load-saved "${load_saved}" || exit 1
+ sleep .4
+ else
+ echo "Error: no such saved config: ${load_saved}" >&2
+ exit_script 1
+ fi
+fi
+
+setup_touchpads
+setup_styluses
+
+exit_script