forge
Explore
Log in

Don't you dare go Hollow

Soft plug/unplug menu, keyboard replug, waybar drawer

Browse files at this commit

4fb15de5eca4b8c6aaa0b6b80577e7b42033fd79Ahmet Kaan GümüşParent: a11d404
  • feat
    • soft plug/unplug menu, keyboard replug, waybar drawer
Show full message
feat(usb): soft plug/unplug menu, keyboard replug, waybar drawer

usb_menu.sh deauthorizes/reauthorizes USB devices in sysfs (real eject via
udisks for storage), so a wedged keyboard can be replugged without reaching
behind the PC. Keyboard identified by usb_devices.sh (autodetect cannot tell a
Razer combo mouse from a keyboard). udev rule + installer drop the pkexec
prompt by giving wheel 0660 on the authorized attrs.

Reachable by mouse: waybar tray drawer now nests a group/system drawer -
hover the hamburger for the dots, hover the dots for USB + power. Power menu
is the same script as Mod+Escape. Keybind Mod+Shift+K in both compositors.

cpu tooltip: waybar renders \n, not \r - the earlier \r version showed nothing.
.config/hypr/hyprland.confmodified+1 -0
@@ -60,6 +60,7 @@ bind = $mainMod, Escape, exec, $power_menu
60 bind = $mainMod, W, exec, ~/.config/scripts/hyprland/wallpaper_picker.sh60 bind = $mainMod, W, exec, ~/.config/scripts/hyprland/wallpaper_picker.sh
61 bind = $mainMod, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh61 bind = $mainMod, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh
62 bindl = $mainMod SHIFT, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh on62 bindl = $mainMod SHIFT, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh on
63+bind = $mainMod SHIFT, K, exec, bash ~/.config/scripts/system/usb_menu.sh
63 64
64 $discord = flatpak run dev.vencord.Vesktop65 $discord = flatpak run dev.vencord.Vesktop
65 $youtube = mullvad-exclude flatpak run io.freetubeapp.FreeTube66 $youtube = mullvad-exclude flatpak run io.freetubeapp.FreeTube
.config/niri/config.kdlmodified+1 -0
@@ -193,6 +193,7 @@ binds {
193 Mod+H hotkey-overlay-title="Toggle waybar" { spawn-sh "pkill -SIGUSR1 waybar"; }193 Mod+H hotkey-overlay-title="Toggle waybar" { spawn-sh "pkill -SIGUSR1 waybar"; }
194 Mod+B hotkey-overlay-title="Wallpaper picker" { spawn-sh "~/.config/scripts/niri/wallpaper_picker.sh"; }194 Mod+B hotkey-overlay-title="Wallpaper picker" { spawn-sh "~/.config/scripts/niri/wallpaper_picker.sh"; }
195 Mod+Shift+M hotkey-overlay-title="Monitors menu" { spawn-sh "~/.config/scripts/niri/toggle_monitors.sh"; }195 Mod+Shift+M hotkey-overlay-title="Monitors menu" { spawn-sh "~/.config/scripts/niri/toggle_monitors.sh"; }
196+ Mod+Shift+K hotkey-overlay-title="USB plug/unplug menu" { spawn-sh "bash ~/.config/scripts/system/usb_menu.sh"; }
196 Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; }197 Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; }
197 198
198 Alt+4 repeat=false { close-window; }199 Alt+4 repeat=false { close-window; }
.config/scripts/system/install-usb-nopasswd.shadded+17 -0
@@ -0,0 +1,17 @@
1+#!/bin/bash
2+# Installs the udev rule that lets usb_menu.sh work without a password prompt.
3+# Run once as root: sudo bash ~/.config/scripts/system/install-usb-nopasswd.sh
4+set -euo pipefail
5+[[ $EUID -eq 0 ]] || { echo "run as root: sudo bash $0" >&2; exit 1; }
6+
7+src="$(dirname "$(readlink -f "$0")")/usb-nopasswd.rules"
8+install -m 0644 "$src" /etc/udev/rules.d/90-usb-authorized.rules
9+udevadm control --reload
10+
11+# Apply to devices already plugged in (udev add events already fired for them).
12+for d in /sys/bus/usb/devices/*/authorized; do
13+ chgrp wheel "$d" && chmod 0660 "$d"
14+done
15+
16+echo "installed: /etc/udev/rules.d/90-usb-authorized.rules"
17+ls -l /sys/bus/usb/devices/1-*/authorized 2>/dev/null | head -3
.config/scripts/system/usb-nopasswd.rulesadded+8 -0
@@ -0,0 +1,8 @@
1+# Let wheel members soft-unplug/replug USB devices without a root prompt.
2+# usb_menu.sh writes 0/1 to /sys/bus/usb/devices/<dev>/authorized; by default that
3+# file is root-owned 0644, which forced a pkexec dialog on every action.
4+# Runs on every USB add, so re-plugged/new devices get the same permissions.
5+ACTION=="add", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", \
6+ RUN+="/bin/sh -c 'chgrp wheel /sys/$devpath/authorized; chmod 0660 /sys/$devpath/authorized'"
7+
8+# Root hubs / already-present devices are handled by the installer's one-shot pass.
.config/scripts/system/usb_devices.shadded+6 -0
@@ -0,0 +1,6 @@
1+# Device map for usb_menu.sh — sysfs paths under /sys/bus/usb/devices.
2+# `usb_menu.sh list` prints the current paths in parentheses.
3+# Paths change only if you move the device to another physical port/hub.
4+
5+# Keyboard (ASUS ROG OMNI RECEIVER — also carries other ROG wireless gear).
6+KEYBOARD_USB="1-5.4"
.config/scripts/system/usb_menu.shadded+248 -0
@@ -0,0 +1,248 @@
1+#!/bin/bash
2+# USB soft plug/unplug menu (rofi), compositor-agnostic.
3+# "Unplug" = deauthorize the device in sysfs: kernel unbinds its drivers and it
4+# behaves as if pulled out, but the sysfs node stays so it can be plugged back.
5+# Storage devices are unmounted/powered off through udisks instead (no root).
6+
7+set -u
8+
9+SYSUSB=/sys/bus/usb/devices
10+
11+# Optional device map (KEYBOARD_USB=...). Missing file is fine: autodetect kicks in.
12+# shellcheck source=/dev/null
13+[[ -r ${0%/*}/usb_devices.sh ]] && source "${0%/*}/usb_devices.sh"
14+
15+notify() {
16+ command -v notify-send >/dev/null && notify-send -a "USB" "$1" "${2:-}"
17+}
18+
19+# --- device helpers ---------------------------------------------------------
20+
21+# echo every real device node (skip interfaces "1-5:1.0" and root hubs "usb1")
22+usb_paths() {
23+ local d
24+ for d in "$SYSUSB"/*; do
25+ local n=${d##*/}
26+ [[ $n == usb* || $n == *:* ]] && continue
27+ [[ -f $d/idVendor ]] && echo "$n"
28+ done
29+}
30+
31+dev_name() {
32+ local p=$SYSUSB/$1 vendor product
33+ product=$(cat "$p/product" 2>/dev/null)
34+ vendor=$(cat "$p/manufacturer" 2>/dev/null)
35+ if [[ -n $product ]]; then
36+ [[ -n $vendor && $product != "$vendor"* ]] && echo "$vendor $product" || echo "$product"
37+ else
38+ echo "$(cat "$p/idVendor" 2>/dev/null):$(cat "$p/idProduct" 2>/dev/null)"
39+ fi
40+}
41+
42+dev_authorized() { cat "$SYSUSB/$1/authorized" 2>/dev/null; }
43+
44+# block devices belonging to this usb device, e.g. "sdb"
45+dev_blocks() {
46+ local b
47+ for b in "$SYSUSB/$1"/*/host*/target*/*/block/*; do
48+ [[ -e $b ]] && echo "${b##*/}"
49+ done
50+}
51+
52+is_hub() { [[ $(cat "$SYSUSB/$1"/*:*/bInterfaceClass 2>/dev/null | head -1) == "09" ]]; }
53+
54+# sysfs path of the keyboard: configured value wins, else the USB device whose
55+# input children expose a kbd handler and are not a pointer.
56+keyboard_path() {
57+ if [[ -n ${KEYBOARD_USB:-} && -d $SYSUSB/$KEYBOARD_USB ]]; then
58+ echo "$KEYBOARD_USB"
59+ return
60+ fi
61+ # Fallback only. Combo receivers (Razer/ROG) register fake keyboard inputs on
62+ # mice too, so this can pick the wrong one — set KEYBOARD_USB to be sure.
63+ local i dev p
64+ for i in /sys/class/input/input*; do
65+ [[ -e $i/capabilities/key ]] || continue
66+ [[ $(cat "$i/name" 2>/dev/null) == *[Kk]eyboard* ]] || continue
67+ # skip pointers (they own a mouse/js handler)
68+ compgen -G "$i/mouse*" >/dev/null && continue
69+ dev=$(readlink -f "$i/device" 2>/dev/null) || continue
70+ # walk up to the usb_device node
71+ while [[ $dev == /sys/* && $dev != / ]]; do
72+ if [[ -f $dev/idVendor ]]; then
73+ p=${dev##*/}
74+ [[ -d $SYSUSB/$p ]] && { echo "$p"; return; }
75+ break
76+ fi
77+ dev=${dev%/*}
78+ done
79+ done
80+}
81+
82+# --- actions ----------------------------------------------------------------
83+
84+set_auth() {
85+ local path=$1 val=$2
86+ if [[ -w $SYSUSB/$path/authorized ]]; then
87+ echo "$val" > "$SYSUSB/$path/authorized"
88+ else
89+ pkexec /bin/sh -c "echo $val > $SYSUSB/$path/authorized"
90+ fi
91+}
92+
93+unplug() {
94+ local path=$1 name blocks
95+ name=$(dev_name "$path")
96+ blocks=$(dev_blocks "$path")
97+ if [[ -n $blocks ]]; then
98+ local b part
99+ for b in $blocks; do
100+ for part in /sys/block/"$b"/"$b"*; do
101+ [[ -e $part ]] && udisksctl unmount -b "/dev/${part##*/}" --no-user-interaction 2>/dev/null
102+ done
103+ udisksctl unmount -b "/dev/$b" --no-user-interaction 2>/dev/null
104+ done
105+ for b in $blocks; do
106+ if udisksctl power-off -b "/dev/$b" --no-user-interaction; then
107+ notify "Ejected" "$name ($b)"
108+ return 0
109+ fi
110+ done
111+ fi
112+ set_auth "$path" 0 && notify "Unplugged" "$name [$path]"
113+}
114+
115+plug() {
116+ local path=$1
117+ set_auth "$path" 1 && notify "Plugged" "$(dev_name "$path") [$path]"
118+}
119+
120+replug() {
121+ local path=$1
122+ set_auth "$path" 0 || return 1
123+ sleep 1
124+ set_auth "$path" 1 && notify "Replugged" "$(dev_name "$path") [$path]"
125+}
126+
127+rereplug_keyboard() {
128+ local p
129+ p=$(keyboard_path)
130+ [[ -n $p ]] || { notify "No keyboard found" "set KEYBOARD_USB in usb_devices.sh"; return 1; }
131+ replug "$p"
132+}
133+
134+plug_all() {
135+ local p
136+ for p in $(usb_paths); do
137+ [[ $(dev_authorized "$p") == 1 ]] || continue
138+ set_auth "$p" 0 || continue
139+ sleep 1
140+ set_auth "$p" 1
141+ done
142+ notify "All USB devices replugged"
143+}
144+
145+replug_keyboard() {
146+ local p
147+ p=$(keyboard_path)
148+ [[ -n $p ]] || { notify "No keyboard found" "set KEYBOARD_USB in usb_devices.sh"; return 1; }
149+ replug "$p"
150+}
151+
152+plug_all() {
153+ local p
154+ for p in $(usb_paths); do
155+ [[ $(dev_authorized "$p") == 0 ]] && set_auth "$p" 1
156+ done
157+ notify "All USB devices plugged"
158+}
159+
160+# JSON for a waybar custom module.
161+waybar_json() {
162+ local total=0 off=0 p kb kbname state
163+ for p in $(usb_paths); do
164+ total=$((total + 1))
165+ [[ $(dev_authorized "$p") == 0 ]] && off=$((off + 1))
166+ done
167+ kb=$(keyboard_path)
168+ if [[ -n $kb ]]; then
169+ kbname="$(dev_name "$kb") [$kb]"
170+ [[ $(dev_authorized "$kb") == 1 ]] && state="plugged" || state="UNPLUGGED"
171+ kbname="keyboard: $kbname ($state)"
172+ else
173+ kbname="keyboard: not identified"
174+ fi
175+ local tip="left click: USB menu\nright click: replug keyboard\nmiddle click: replug all\n\n$kbname\n$off of $total devices unplugged"
176+ printf '{"text":"\uf287","tooltip":"%s","class":"%s"}\n' "$tip" "$([[ $off -gt 0 ]] && echo unplugged || echo ok)"
177+}
178+
179+# --- menus ------------------------------------------------------------------
180+
181+list_devices() {
182+ local p state icon blocks extra kb line
183+ kb=$(keyboard_path)
184+ for p in $(usb_paths); do
185+ state=$(dev_authorized "$p")
186+ [[ $state == 1 ]] && icon="🔌" || icon="⛔"
187+ blocks=$(dev_blocks "$p" | tr '\n' ' ')
188+ extra=""
189+ [[ -n $blocks ]] && extra=" [${blocks% }]"
190+ is_hub "$p" && extra="$extra [hub]"
191+ [[ $p == "$kb" ]] && extra="$extra [⌨️ keyboard]"
192+ line=$(printf '%s %s%s (%s)' "$icon" "$(dev_name "$p")" "$extra" "$p")
193+ # keyboard first — it is the one you need when you cannot type
194+ [[ $p == "$kb" ]] && printf '0\t%s\n' "$line" || printf '1\t%s\n' "$line"
195+ done | sort -s -k1,1 | cut -f2-
196+}
197+
198+menu() {
199+ local choice path
200+ choice=$(printf '%s\n🔄 Replug All\n💡 Plug All\n❌ Cancel\n' "$(list_devices)" | rofi -dmenu -p "USB" -i)
201+ [[ -z $choice ]] && exit 0
202+
203+ case "$choice" in
204+ "🔄 Replug All") replug_all; return ;;
205+ "💡 Plug All") plug_all; return ;;
206+ "❌ Cancel") return ;;
207+ esac
208+
209+ path=$(sed -n 's/.*(\(.*\))$/\1/p' <<<"$choice")
210+ [[ -d $SYSUSB/$path ]] || { notify "Unknown device" "$choice"; return; }
211+
212+ local name state actions act
213+ name=$(dev_name "$path")
214+ state=$(dev_authorized "$path")
215+ if [[ $state == 1 ]]; then
216+ if [[ $path == "$(keyboard_path)" ]]; then
217+ actions="🔄 Replug\n⛔ Unplug"
218+ else
219+ actions="⛔ Unplug\n🔄 Replug"
220+ fi
221+ [[ -n $(dev_blocks "$path") ]] && actions="⏏️ Eject (unmount + power off)\n$actions"
222+ else
223+ actions="🔌 Plug\n🔄 Replug"
224+ fi
225+ act=$(printf '%b\n❌ Cancel\n' "$actions" | rofi -dmenu -p "$name" -i)
226+
227+ case "$act" in
228+ "⏏️ Eject (unmount + power off)") unplug "$path" ;;
229+ "⛔ Unplug") unplug "$path" ;;
230+ "🔌 Plug") plug "$path" ;;
231+ "🔄 Replug") replug "$path" ;;
232+ esac
233+}
234+
235+# --- entry ------------------------------------------------------------------
236+
237+case "${1:-menu}" in
238+ menu) menu ;;
239+ list) list_devices ;;
240+ off) unplug "${2:?usage: $0 off <sysfs-path e.g. 1-5.2>}" ;;
241+ on) plug "${2:?usage: $0 on <sysfs-path>}" ;;
242+ replug) replug "${2:?usage: $0 replug <sysfs-path>}" ;;
243+ all-on) plug_all ;;
244+ all-replug) replug_all ;;
245+ replug-keyboard|kb) replug_keyboard ;;
246+ waybar) waybar_json ;;
247+ *) echo "usage: ${0##*/} [menu|list|off <path>|on <path>|replug <path>|all-on|all-replug|replug-keyboard|waybar]" >&2; exit 1 ;;
248+esac
.config/waybar/configmodified+42 -1
@@ -86,6 +86,24 @@
86 "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"86 "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"
87 },87 },
88 88
89+ "custom/usb": {
90+ "format": "{}",
91+ "exec": "bash ~/.config/scripts/system/usb_menu.sh waybar",
92+ "interval": 10,
93+ "return-type": "json",
94+ "tooltip": true,
95+ "on-click": "bash ~/.config/scripts/system/usb_menu.sh menu",
96+ "on-click-right": "bash ~/.config/scripts/system/usb_menu.sh replug-keyboard",
97+ "on-click-middle": "bash ~/.config/scripts/system/usb_menu.sh all-replug"
98+ },
99+
100+ "custom/power": {
101+ "format": "\uf011",
102+ "tooltip": true,
103+ "tooltip-format": "Power menu (Mod+Escape)",
104+ "on-click": "bash ~/.config/scripts/system/power_menu.sh"
105+ },
106+
89 "custom/camera": {107 "custom/camera": {
90 "format": "{text}",108 "format": "{text}",
91 "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar",109 "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar",
@@ -130,7 +148,9 @@
130 },148 },
131 "cpu": {149 "cpu": {
132 "interval": 1,150 "interval": 1,
133- "format": "{usage}%"151+ "format": "{usage}%",
152+ "tooltip": true,
153+ "tooltip-format": "CPU {usage}%\nload {load}\nfreq {avg_frequency} GHz (max {max_frequency})"
134 },154 },
135 "memory": {155 "memory": {
136 "interval": 1,156 "interval": 1,
@@ -159,6 +179,26 @@
159 "on-click-right": "pavucontrol",179 "on-click-right": "pavucontrol",
160 "format-muted": "\uf00d {volume}%"180 "format-muted": "\uf00d {volume}%"
161 },181 },
182+ // Nested drawer: hovering the ☰ reveals the ⋯ (plus the tray icons); hovering
183+ // the ⋯ expands the USB and power menus. Nothing extra is claimed on the bar.
184+ "group/system": {
185+ "orientation": "horizontal",
186+ "drawer": {
187+ "transition-duration": 300,
188+ "transition-left-to-right": false
189+ },
190+ "modules": [
191+ "custom/system-toggle",
192+ "custom/usb",
193+ "custom/power"
194+ ]
195+ },
196+
197+ "custom/system-toggle": {
198+ "format": "\u22ef",
199+ "tooltip-format": "USB + power"
200+ },
201+
162 "group/tray": {202 "group/tray": {
163 "orientation": "horizontal",203 "orientation": "horizontal",
164 "drawer": {204 "drawer": {
@@ -167,6 +207,7 @@
167 },207 },
168 "modules": [208 "modules": [
169 "custom/tray-toggle",209 "custom/tray-toggle",
210+ "group/system",
170 "tray"211 "tray"
171 ]212 ]
172 },213 },
.config/waybar/config-nirimodified+42 -1
@@ -86,6 +86,24 @@
86 "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"86 "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"
87 },87 },
88 88
89+ "custom/usb": {
90+ "format": "{}",
91+ "exec": "bash ~/.config/scripts/system/usb_menu.sh waybar",
92+ "interval": 10,
93+ "return-type": "json",
94+ "tooltip": true,
95+ "on-click": "bash ~/.config/scripts/system/usb_menu.sh menu",
96+ "on-click-right": "bash ~/.config/scripts/system/usb_menu.sh replug-keyboard",
97+ "on-click-middle": "bash ~/.config/scripts/system/usb_menu.sh all-replug"
98+ },
99+
100+ "custom/power": {
101+ "format": "\uf011",
102+ "tooltip": true,
103+ "tooltip-format": "Power menu (Mod+Escape)",
104+ "on-click": "bash ~/.config/scripts/system/power_menu.sh"
105+ },
106+
89 "custom/camera": {107 "custom/camera": {
90 "format": "{text}",108 "format": "{text}",
91 "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar",109 "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar",
@@ -133,7 +151,9 @@
133 },151 },
134 "cpu": {152 "cpu": {
135 "interval": 1,153 "interval": 1,
136- "format": "{usage}%"154+ "format": "{usage}%",
155+ "tooltip": true,
156+ "tooltip-format": "CPU {usage}%\nload {load}\nfreq {avg_frequency} GHz (max {max_frequency})"
137 },157 },
138 "memory": {158 "memory": {
139 "interval": 1,159 "interval": 1,
@@ -162,6 +182,26 @@
162 "on-click-right": "pavucontrol",182 "on-click-right": "pavucontrol",
163 "format-muted": "\uf00d {volume}%"183 "format-muted": "\uf00d {volume}%"
164 },184 },
185+ // Nested drawer: hovering the ☰ reveals the ⋯ (plus the tray icons); hovering
186+ // the ⋯ expands the USB and power menus. Nothing extra is claimed on the bar.
187+ "group/system": {
188+ "orientation": "horizontal",
189+ "drawer": {
190+ "transition-duration": 300,
191+ "transition-left-to-right": false
192+ },
193+ "modules": [
194+ "custom/system-toggle",
195+ "custom/usb",
196+ "custom/power"
197+ ]
198+ },
199+
200+ "custom/system-toggle": {
201+ "format": "\u22ef",
202+ "tooltip-format": "USB + power"
203+ },
204+
165 "group/tray": {205 "group/tray": {
166 "orientation": "horizontal",206 "orientation": "horizontal",
167 "drawer": {207 "drawer": {
@@ -170,6 +210,7 @@
170 },210 },
171 "modules": [211 "modules": [
172 "custom/tray-toggle",212 "custom/tray-toggle",
213+ "group/system",
173 "tray"214 "tray"
174 ]215 ]
175 },216 },
.config/waybar/style.cssmodified+13 -0
@@ -45,3 +45,16 @@
45 box-shadow: none;45 box-shadow: none;
46 font-size: 16px;46 font-size: 16px;
47 }47 }
48+
49+/* USB + power live behind the ⋯, which itself lives inside the ☰ tray drawer,
50+ so none of the three claims bar width until the hamburger is hovered. */
51+#custom-system-toggle,
52+#custom-usb,
53+#custom-power {
54+ padding: 0 6px;
55+ font-size: 15px;
56+}
57+
58+#custom-usb.unplugged {
59+ color: #ff6b6b;
60+}