From 62e45197bb4d440e002b731715343e565cf50ff3 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 20 May 2026 21:55:13 -0400 Subject: feat(hyprland): add touchpad state indicator to waybar The $mod+F9 toggle and the toggle-touchpad / touchpad-auto scripts already worked, but the scripts lived only in ~/.local/bin and were never committed, and there was no way to see the touchpad's state at a glance. I committed both scripts into the repo so stow installs them, and added a waybar indicator: a waybar-touchpad status script and a custom/touchpad module that shows a mouse glyph when the touchpad is on and a mouse-off glyph (in the dupre orange) when it's off. The scripts signal the module with pkill -RTMIN+9 waybar after each state change, so the icon updates the moment the touchpad toggles. touchpad-auto now runs at login via exec-once. The waybar-touchpad script has unit tests under tests/waybar-touchpad/ covering the enabled, disabled, and missing-state-file cases. --- dotfiles/hyprland/.config/hypr/hyprland.conf | 1 + dotfiles/hyprland/.config/waybar/config | 8 ++ dotfiles/hyprland/.config/waybar/style.css | 6 ++ dotfiles/hyprland/.local/bin/toggle-touchpad | 25 +++++++ dotfiles/hyprland/.local/bin/touchpad-auto | 53 +++++++++++++ dotfiles/hyprland/.local/bin/waybar-touchpad | 15 ++++ tests/waybar-touchpad/test_waybar_touchpad.py | 104 ++++++++++++++++++++++++++ 7 files changed, 212 insertions(+) create mode 100755 dotfiles/hyprland/.local/bin/toggle-touchpad create mode 100755 dotfiles/hyprland/.local/bin/touchpad-auto create mode 100755 dotfiles/hyprland/.local/bin/waybar-touchpad create mode 100644 tests/waybar-touchpad/test_waybar_touchpad.py diff --git a/dotfiles/hyprland/.config/hypr/hyprland.conf b/dotfiles/hyprland/.config/hypr/hyprland.conf index 2873c4d..2e41b30 100644 --- a/dotfiles/hyprland/.config/hypr/hyprland.conf +++ b/dotfiles/hyprland/.config/hypr/hyprland.conf @@ -26,6 +26,7 @@ exec-once = dunst > ~/.local/var/log/dunst-$(date +%Y-%m-%d-%H%M%S).log 2>&1 exec-once = awww-daemon && sleep 1 && awww img ~/pictures/wallpaper/trondheim-norway.jpg # Background services +exec-once = touchpad-auto exec-once = hypridle > ~/.local/var/log/hypridle-$(date +%Y-%m-%d-%H%M%S).log 2>&1 exec-once = /usr/lib/geoclue-2.0/demos/agent exec-once = gammastep > ~/.local/var/log/gammastep-$(date +%Y-%m-%d-%H%M%S).log 2>&1 diff --git a/dotfiles/hyprland/.config/waybar/config b/dotfiles/hyprland/.config/waybar/config index 55ca359..2ae43fe 100644 --- a/dotfiles/hyprland/.config/waybar/config +++ b/dotfiles/hyprland/.config/waybar/config @@ -17,6 +17,7 @@ "group/sysmonitor", "custom/netspeed", "pulseaudio", + "custom/touchpad", "idle_inhibitor", "custom/pocketbook", "tray", @@ -141,6 +142,13 @@ "on-scroll-down": "pactl set-sink-volume @DEFAULT_SINK@ -5%" }, + "custom/touchpad": { + "exec": "waybar-touchpad", + "return-type": "json", + "signal": 9, + "on-click": "toggle-touchpad" + }, + "idle_inhibitor": { "format": "{icon}", "format-icons": { diff --git a/dotfiles/hyprland/.config/waybar/style.css b/dotfiles/hyprland/.config/waybar/style.css index 3f7814b..3a849c5 100644 --- a/dotfiles/hyprland/.config/waybar/style.css +++ b/dotfiles/hyprland/.config/waybar/style.css @@ -76,6 +76,7 @@ window#waybar { #custom-date, #custom-worldclock, #custom-layout, +#custom-touchpad, #window { padding: 0.45rem; margin: 0.3rem; @@ -96,6 +97,7 @@ window#waybar { #custom-netspeed:hover, #pulseaudio:hover, #sysmonitor:hover, +#custom-touchpad:hover, #custom-layout:hover { background-color: #474544; border-radius: 1rem; @@ -106,6 +108,10 @@ window#waybar { color: #d47c59; } +#custom-touchpad.disabled { + color: #d47c59; +} + #temperature.warning { color: #d7af5f; } diff --git a/dotfiles/hyprland/.local/bin/toggle-touchpad b/dotfiles/hyprland/.local/bin/toggle-touchpad new file mode 100755 index 0000000..cab605b --- /dev/null +++ b/dotfiles/hyprland/.local/bin/toggle-touchpad @@ -0,0 +1,25 @@ +#!/bin/bash +# Toggle the laptop touchpad on/off + +TOUCHPAD="pixa3854:00-093a:0274-touchpad" +STATE_FILE="${XDG_RUNTIME_DIR:-/tmp}/touchpad-state" + +# Default to enabled if no state file +if [ ! -f "$STATE_FILE" ]; then + echo "enabled" > "$STATE_FILE" +fi + +state=$(cat "$STATE_FILE") + +if [ "$state" = "enabled" ]; then + hyprctl keyword "device[$TOUCHPAD]:enabled" false >/dev/null + echo "disabled" > "$STATE_FILE" + notify info "Touchpad" "Disabled" +else + hyprctl keyword "device[$TOUCHPAD]:enabled" true >/dev/null + echo "enabled" > "$STATE_FILE" + notify info "Touchpad" "Enabled" +fi + +# Refresh the waybar indicator immediately (custom/touchpad listens on signal 9). +pkill -RTMIN+9 waybar 2>/dev/null diff --git a/dotfiles/hyprland/.local/bin/touchpad-auto b/dotfiles/hyprland/.local/bin/touchpad-auto new file mode 100755 index 0000000..830a8f2 --- /dev/null +++ b/dotfiles/hyprland/.local/bin/touchpad-auto @@ -0,0 +1,53 @@ +#!/bin/bash +# Auto-disable touchpad when an external mouse is connected, re-enable when removed. +# Watches Hyprland socket for device add/remove events. + +TOUCHPAD="pixa3854:00-093a:0274-touchpad" +STATE_FILE="${XDG_RUNTIME_DIR:-/tmp}/touchpad-state" + +has_external_mouse() { + hyprctl devices -j | jq -e '[.mice[] | select(.name != "'"$TOUCHPAD"'" and .name != "pixa3854:00-093a:0274-mouse" and (.name | test("frmw") | not))] | length > 0' >/dev/null 2>&1 +} + +set_touchpad() { + hyprctl keyword "device[$TOUCHPAD]:enabled" "$1" >/dev/null + if [ "$1" = "true" ]; then + echo "enabled" > "$STATE_FILE" + else + echo "disabled" > "$STATE_FILE" + fi + # Refresh the waybar indicator (custom/touchpad listens on signal 9). + pkill -RTMIN+9 waybar 2>/dev/null +} + +# Set initial state +if has_external_mouse; then + set_touchpad false +else + set_touchpad true +fi + +# BT mice may auto-reconnect after boot — recheck after delay +(sleep 10 && if has_external_mouse; then set_touchpad false; fi) & + +# Watch for device events +socat -u "UNIX-CONNECT:$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" - | while read -r line; do + case "$line" in + mouseadded\>\>*|mouseremoved\>\>*) + sleep 0.5 + if has_external_mouse; then + set_touchpad false + else + set_touchpad true + fi + ;; + configreloaded\>\>*) + sleep 0.5 + if has_external_mouse; then + set_touchpad false + else + set_touchpad true + fi + ;; + esac +done diff --git a/dotfiles/hyprland/.local/bin/waybar-touchpad b/dotfiles/hyprland/.local/bin/waybar-touchpad new file mode 100755 index 0000000..d3adddd --- /dev/null +++ b/dotfiles/hyprland/.local/bin/waybar-touchpad @@ -0,0 +1,15 @@ +#!/bin/sh +# Touchpad on/off indicator for waybar. +# Reads the state file that toggle-touchpad and touchpad-auto maintain; emits +# one JSON line (text + tooltip + class) for the custom/touchpad module. +# Anything other than "disabled" reads as enabled, so a missing or garbled +# state file fails safe (pointer shown rather than hidden). + +STATE_FILE="${XDG_RUNTIME_DIR:-/tmp}/touchpad-state" +state=$(cat "$STATE_FILE" 2>/dev/null || echo enabled) + +if [ "$state" = "disabled" ]; then + echo "{\"text\": \"󰍾\", \"tooltip\": \"Touchpad disabled\", \"class\": \"disabled\"}" +else + echo "{\"text\": \"󰍽\", \"tooltip\": \"Touchpad enabled\", \"class\": \"enabled\"}" +fi diff --git a/tests/waybar-touchpad/test_waybar_touchpad.py b/tests/waybar-touchpad/test_waybar_touchpad.py new file mode 100644 index 0000000..3133bb4 --- /dev/null +++ b/tests/waybar-touchpad/test_waybar_touchpad.py @@ -0,0 +1,104 @@ +"""Tests for dotfiles/hyprland/.local/bin/waybar-touchpad. + +The script emits one JSON line for waybar's custom/touchpad module, derived +from the touchpad state file that toggle-touchpad / touchpad-auto maintain +at $XDG_RUNTIME_DIR/touchpad-state. Tests point XDG_RUNTIME_DIR at a temp +dir and assert on the emitted JSON for each state — enabled, disabled, and +the missing-file default. No mocking: the real script runs against a real +state file. + +Run from repo root: + python3 -m unittest tests.waybar-touchpad.test_waybar_touchpad +""" + +import json +import os +import shutil +import subprocess +import tempfile +import unittest + + +REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) +SCRIPT = os.path.join(REPO_ROOT, "dotfiles/hyprland/.local/bin/waybar-touchpad") + + +class WaybarTouchpadHarness(unittest.TestCase): + + def setUp(self): + self.tmp = tempfile.mkdtemp(prefix="waybar-touchpad-test-") + self.state_file = os.path.join(self.tmp, "touchpad-state") + + def tearDown(self): + shutil.rmtree(self.tmp, ignore_errors=True) + + def set_state(self, value): + with open(self.state_file, "w") as f: + f.write(value) + + def run_script(self): + env = os.environ.copy() + env["XDG_RUNTIME_DIR"] = self.tmp + return subprocess.run( + [SCRIPT], env=env, capture_output=True, text=True, timeout=10, + ) + + def emitted_json(self): + result = self.run_script() + self.assertEqual(result.returncode, 0, msg=result.stderr) + return json.loads(result.stdout) + + +# ----------------------------------------------------------------------------- +# Normal cases +# ----------------------------------------------------------------------------- + +class TestWaybarTouchpadNormal(WaybarTouchpadHarness): + + def test_enabled_state_emits_enabled_class_and_mouse_icon(self): + self.set_state("enabled") + data = self.emitted_json() + self.assertEqual(data["class"], "enabled") + self.assertIn("\U000f037d", data["text"]) # 󰍽 mouse + self.assertIn("enabled", data["tooltip"].lower()) + + def test_disabled_state_emits_disabled_class_and_mouse_off_icon(self): + self.set_state("disabled") + data = self.emitted_json() + self.assertEqual(data["class"], "disabled") + self.assertIn("\U000f037e", data["text"]) # 󰍾 mouse-off + self.assertIn("disabled", data["tooltip"].lower()) + + +# ----------------------------------------------------------------------------- +# Boundary cases +# ----------------------------------------------------------------------------- + +class TestWaybarTouchpadBoundary(WaybarTouchpadHarness): + + def test_missing_state_file_defaults_to_enabled(self): + # No state file written → script should treat the touchpad as enabled. + data = self.emitted_json() + self.assertEqual(data["class"], "enabled") + + def test_state_with_trailing_newline_is_handled(self): + self.set_state("disabled\n") + data = self.emitted_json() + self.assertEqual(data["class"], "disabled") + + def test_unknown_state_value_treated_as_enabled(self): + # Anything that isn't "disabled" reads as enabled (fail-safe: the + # pointer is usable rather than hidden). + self.set_state("garbage") + data = self.emitted_json() + self.assertEqual(data["class"], "enabled") + + def test_output_is_a_single_json_object(self): + self.set_state("enabled") + result = self.run_script() + lines = [ln for ln in result.stdout.splitlines() if ln.strip()] + self.assertEqual(len(lines), 1, msg=f"expected one line, got {lines!r}") + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3