Repository navigation
Expand file tree
/
Copy pathcodexmicro-doctor
More file actions
executable file
·98 lines (90 loc) · 3.07 KB
/
Copy pathcodexmicro-doctor
File metadata and controls
executable file
·98 lines (90 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
#**************************************************************
#* File:: codexmicro-doctor
#*
#* Description:: Detects a connected QMK/HID macropad and installs the udev rule
#* for it, so the user never has to look up the device's idVendor/idProduct by
#* hand. It reads each hidraw device's identity from sysfs, lists them, and
#* writes a rule that grants the local user access and names the device
#* /dev/codexmicro-pad. Run with root to write into /etc/udev. The sysfs root
#* and rule destination can be overridden (SYS_HIDRAW, RULE_DST) for testing.
#*
#**************************************************************
set -e
SYS_HIDRAW="${SYS_HIDRAW:-/sys/class/hidraw}"
RULE_DST="${RULE_DST:-/etc/udev/rules.d/99-codexmicro.rules}"
# Read one hidraw device's identity from sysfs and print "vendor product name".
# The kernel exposes HID_ID as "bus:vendor:product" in padded hex, so the last
# four digits of each field are the values udev matches on.
parse_dev() {
local ue="$SYS_HIDRAW/$1/device/uevent"
local hidid name ven prod _bus
[ -r "$ue" ] || return 1
hidid=$(sed -n 's/^HID_ID=//p' "$ue" | head -1)
name=$(sed -n 's/^HID_NAME=//p' "$ue" | head -1)
[ -n "$hidid" ] || return 1
IFS=: read -r _bus ven prod <<< "$hidid"
ven=${ven: -4}
prod=${prod: -4}
printf '%s %s %s\n' \
"$(printf '%s' "$ven" | tr 'A-Z' 'a-z')" \
"$(printf '%s' "$prod" | tr 'A-Z' 'a-z')" \
"$name"
}
# List every readable hidraw device as "hidrawN vendor product name".
list_devices() {
local d n info
for d in "$SYS_HIDRAW"/hidraw*; do
[ -e "$d" ] || continue
n=$(basename "$d")
info=$(parse_dev "$n") || continue
printf '%-9s %s\n' "$n" "$info"
done
}
# Write the udev rule for a chosen vendor/product and reload udev.
emit_rule() {
local vendor="$1" product="$2"
{
echo "# Generated by codexmicro-doctor for a QMK macropad."
echo "SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"$vendor\", ATTRS{idProduct}==\"$product\", TAG+=\"uaccess\", SYMLINK+=\"codexmicro-pad\""
} > "$RULE_DST"
echo "Wrote $RULE_DST (idVendor=$vendor idProduct=$product)"
# Skippable in tests; otherwise reload so the rule takes effect on replug.
if [ -z "$CODEXMICRO_NO_UDEV" ]; then
if udevadm control --reload 2>/dev/null && udevadm trigger 2>/dev/null; then
echo "Reloaded udev. Replug the pad; it will appear as /dev/codexmicro-pad."
else
echo "Could not reload udev (run as root): sudo udevadm control --reload && sudo udevadm trigger"
fi
fi
}
# Resolve a hidraw name to its vendor/product and write the rule.
use_device() {
local info
info=$(parse_dev "$1") || { echo "cannot read $1"; exit 1; }
set -- $info
emit_rule "$1" "$2"
}
case "${1:-}" in
--list)
list_devices
;;
--device)
[ -n "$2" ] || { echo "usage: $0 --device hidrawN"; exit 1; }
use_device "$2"
;;
"")
echo "Connected HID devices:"
echo
list_devices | nl -w2 -s') '
echo
read -rp "Pick the number of your macropad: " choice
dev=$(list_devices | sed -n "${choice}p" | awk '{print $1}')
[ -n "$dev" ] || { echo "no such choice"; exit 1; }
use_device "$dev"
;;
*)
echo "usage: $0 [--list | --device hidrawN]"
exit 1
;;
esac