Files
scripts/setup-keyboard-remap-poller.sh
T
2026-06-11 13:47:48 -04:00

114 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# setup-keyboard-remap-poller.sh
# =============================
# Installs a systemd user service that polls for keyboard plug-in / KVM switch events
# and runs ~/sh/keymaps.sh (your xmodmap stuff) when it detects a change.
#
# Why polling? Udev is too early / env broken for X11 tools in most cases.
set -euo pipefail
SERVICE_NAME="keyboard-remap-poller"
SERVICE_FILE="$HOME/.config/systemd/user/$SERVICE_NAME.service"
POLLER_SCRIPT="$HOME/.local/bin/keyboard-remap-poller.sh"
KEYMAPS_SCRIPT="$HOME/sh/keymaps.sh"
if [[ ! -x "$KEYMAPS_SCRIPT" ]]; then
echo "Error: $KEYMAPS_SCRIPT not found or not executable."
echo "Make sure it exists and chmod +x it."
exit 1
fi
echo "Creating poller script: $POLLER_SCRIPT"
mkdir -p "$(dirname "$POLLER_SCRIPT")"
cat > "$POLLER_SCRIPT" << 'EOP'
#!/usr/bin/env bash
# keyboard-remap-poller.sh - checks for keyboard/input changes and runs keymaps.sh
KEYMAPS_SCRIPT="$HOME/sh/keymaps.sh"
LOG_FILE="$HOME/.local/share/keyboard-remap.log"
LAST_COUNT_FILE="$HOME/.cache/keyboard-event-count"
# Initial count of input devices (rough way to detect plug/unplug/KVM)
get_input_count() {
ls /dev/input/event* 2>/dev/null | wc -l
}
last_count=$(cat "$LAST_COUNT_FILE" 2>/dev/null || echo "0")
while true; do
current_count=$(get_input_count)
if [[ "$current_count" != "$last_count" ]]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Input device count changed ($last_count → $current_count). Running keymaps.sh" >> "$LOG_FILE"
# Run your script in X session context
if [[ -n "${DISPLAY:-}" ]]; then
"$KEYMAPS_SCRIPT" >> "$LOG_FILE" 2>&1
else
# Fallback: try common display :0 or :0.0
DISPLAY=:0 XAUTHORITY="$HOME/.Xauthority" "$KEYMAPS_SCRIPT" >> "$LOG_FILE" 2>&1 || \
DISPLAY=:0.0 XAUTHORITY="$HOME/.Xauthority" "$KEYMAPS_SCRIPT" >> "$LOG_FILE" 2>&1
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - keymaps.sh finished (exit code $?)" >> "$LOG_FILE"
echo "$current_count" > "$LAST_COUNT_FILE"
fi
last_count="$current_count"
sleep 8 # Check every 8 seconds - low overhead
done
EOP
chmod +x "$POLLER_SCRIPT"
echo "Poller script created."
echo "Creating systemd user service: $SERVICE_FILE"
mkdir -p "$(dirname "$SERVICE_FILE")"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Poll for keyboard plug-in / KVM events and run keymaps.sh
After=graphical-session.target
[Service]
Type=simple
ExecStart=$POLLER_SCRIPT
Restart=always
RestartSec=5
[Install]
WantedBy=graphical-session.target
EOF
echo "Service file created."
echo "Enabling and starting the service (user level)..."
systemctl --user daemon-reload
systemctl --user enable --now "$SERVICE_NAME.service"
echo ""
echo "=== Setup Done ==="
echo "The service is now running as your user."
echo ""
echo "Test it:"
echo " 1. Switch KVM or plug/unplug a keyboard"
echo " 2. Wait 1020 seconds"
echo " 3. Check if your xmodmap changes applied (e.g. press your remapped keys)"
echo " 4. Look at log:"
echo " tail -f $HOME/.local/share/keyboard-remap.log"
echo ""
echo "If it doesn't trigger reliably:"
echo " - Increase sleep 8 → sleep 5 in $POLLER_SCRIPT"
echo " - Or if DISPLAY is wrong, edit the DISPLAY/XAUTHORITY lines"
echo ""
echo "To stop/disable:"
echo " systemctl --user stop $SERVICE_NAME.service"
echo " systemctl --user disable $SERVICE_NAME.service"
echo ""
echo "Enjoy your remaps without F10 every time!"