From 4fb15de5eca4b8c6aaa0b6b80577e7b42033fd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 22:37:27 +0300 Subject: [PATCH] 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.conf | 1 + .config/niri/config.kdl | 1 + .../scripts/system/install-usb-nopasswd.sh | 17 ++ .config/scripts/system/usb-nopasswd.rules | 8 + .config/scripts/system/usb_devices.sh | 6 + .config/scripts/system/usb_menu.sh | 248 ++++++++++++++++++ .config/waybar/config | 43 ++- .config/waybar/config-niri | 43 ++- .config/waybar/style.css | 13 + 9 files changed, 378 insertions(+), 2 deletions(-) create mode 100755 .config/scripts/system/install-usb-nopasswd.sh create mode 100644 .config/scripts/system/usb-nopasswd.rules create mode 100644 .config/scripts/system/usb_devices.sh create mode 100755 .config/scripts/system/usb_menu.sh diff --git a/.config/hypr/hyprland.conf b/.config/hypr/hyprland.conf index 8411419..25ed246 100644 --- a/.config/hypr/hyprland.conf +++ b/.config/hypr/hyprland.conf @@ -60,6 +60,7 @@ bind = $mainMod, Escape, exec, $power_menu bind = $mainMod, W, exec, ~/.config/scripts/hyprland/wallpaper_picker.sh bind = $mainMod, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh bindl = $mainMod SHIFT, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh on +bind = $mainMod SHIFT, K, exec, bash ~/.config/scripts/system/usb_menu.sh $discord = flatpak run dev.vencord.Vesktop $youtube = mullvad-exclude flatpak run io.freetubeapp.FreeTube diff --git a/.config/niri/config.kdl b/.config/niri/config.kdl index 31cde2d..51d6b0b 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -193,6 +193,7 @@ binds { Mod+H hotkey-overlay-title="Toggle waybar" { spawn-sh "pkill -SIGUSR1 waybar"; } Mod+B hotkey-overlay-title="Wallpaper picker" { spawn-sh "~/.config/scripts/niri/wallpaper_picker.sh"; } Mod+Shift+M hotkey-overlay-title="Monitors menu" { spawn-sh "~/.config/scripts/niri/toggle_monitors.sh"; } + Mod+Shift+K hotkey-overlay-title="USB plug/unplug menu" { spawn-sh "bash ~/.config/scripts/system/usb_menu.sh"; } Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; } Alt+4 repeat=false { close-window; } diff --git a/.config/scripts/system/install-usb-nopasswd.sh b/.config/scripts/system/install-usb-nopasswd.sh new file mode 100755 index 0000000..4ce523f --- /dev/null +++ b/.config/scripts/system/install-usb-nopasswd.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Installs the udev rule that lets usb_menu.sh work without a password prompt. +# Run once as root: sudo bash ~/.config/scripts/system/install-usb-nopasswd.sh +set -euo pipefail +[[ $EUID -eq 0 ]] || { echo "run as root: sudo bash $0" >&2; exit 1; } + +src="$(dirname "$(readlink -f "$0")")/usb-nopasswd.rules" +install -m 0644 "$src" /etc/udev/rules.d/90-usb-authorized.rules +udevadm control --reload + +# Apply to devices already plugged in (udev add events already fired for them). +for d in /sys/bus/usb/devices/*/authorized; do + chgrp wheel "$d" && chmod 0660 "$d" +done + +echo "installed: /etc/udev/rules.d/90-usb-authorized.rules" +ls -l /sys/bus/usb/devices/1-*/authorized 2>/dev/null | head -3 diff --git a/.config/scripts/system/usb-nopasswd.rules b/.config/scripts/system/usb-nopasswd.rules new file mode 100644 index 0000000..2a10608 --- /dev/null +++ b/.config/scripts/system/usb-nopasswd.rules @@ -0,0 +1,8 @@ +# Let wheel members soft-unplug/replug USB devices without a root prompt. +# usb_menu.sh writes 0/1 to /sys/bus/usb/devices//authorized; by default that +# file is root-owned 0644, which forced a pkexec dialog on every action. +# Runs on every USB add, so re-plugged/new devices get the same permissions. +ACTION=="add", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", \ + RUN+="/bin/sh -c 'chgrp wheel /sys/$devpath/authorized; chmod 0660 /sys/$devpath/authorized'" + +# Root hubs / already-present devices are handled by the installer's one-shot pass. diff --git a/.config/scripts/system/usb_devices.sh b/.config/scripts/system/usb_devices.sh new file mode 100644 index 0000000..677d184 --- /dev/null +++ b/.config/scripts/system/usb_devices.sh @@ -0,0 +1,6 @@ +# Device map for usb_menu.sh — sysfs paths under /sys/bus/usb/devices. +# `usb_menu.sh list` prints the current paths in parentheses. +# Paths change only if you move the device to another physical port/hub. + +# Keyboard (ASUS ROG OMNI RECEIVER — also carries other ROG wireless gear). +KEYBOARD_USB="1-5.4" diff --git a/.config/scripts/system/usb_menu.sh b/.config/scripts/system/usb_menu.sh new file mode 100755 index 0000000..3471f14 --- /dev/null +++ b/.config/scripts/system/usb_menu.sh @@ -0,0 +1,248 @@ +#!/bin/bash +# USB soft plug/unplug menu (rofi), compositor-agnostic. +# "Unplug" = deauthorize the device in sysfs: kernel unbinds its drivers and it +# behaves as if pulled out, but the sysfs node stays so it can be plugged back. +# Storage devices are unmounted/powered off through udisks instead (no root). + +set -u + +SYSUSB=/sys/bus/usb/devices + +# Optional device map (KEYBOARD_USB=...). Missing file is fine: autodetect kicks in. +# shellcheck source=/dev/null +[[ -r ${0%/*}/usb_devices.sh ]] && source "${0%/*}/usb_devices.sh" + +notify() { + command -v notify-send >/dev/null && notify-send -a "USB" "$1" "${2:-}" +} + +# --- device helpers --------------------------------------------------------- + +# echo every real device node (skip interfaces "1-5:1.0" and root hubs "usb1") +usb_paths() { + local d + for d in "$SYSUSB"/*; do + local n=${d##*/} + [[ $n == usb* || $n == *:* ]] && continue + [[ -f $d/idVendor ]] && echo "$n" + done +} + +dev_name() { + local p=$SYSUSB/$1 vendor product + product=$(cat "$p/product" 2>/dev/null) + vendor=$(cat "$p/manufacturer" 2>/dev/null) + if [[ -n $product ]]; then + [[ -n $vendor && $product != "$vendor"* ]] && echo "$vendor $product" || echo "$product" + else + echo "$(cat "$p/idVendor" 2>/dev/null):$(cat "$p/idProduct" 2>/dev/null)" + fi +} + +dev_authorized() { cat "$SYSUSB/$1/authorized" 2>/dev/null; } + +# block devices belonging to this usb device, e.g. "sdb" +dev_blocks() { + local b + for b in "$SYSUSB/$1"/*/host*/target*/*/block/*; do + [[ -e $b ]] && echo "${b##*/}" + done +} + +is_hub() { [[ $(cat "$SYSUSB/$1"/*:*/bInterfaceClass 2>/dev/null | head -1) == "09" ]]; } + +# sysfs path of the keyboard: configured value wins, else the USB device whose +# input children expose a kbd handler and are not a pointer. +keyboard_path() { + if [[ -n ${KEYBOARD_USB:-} && -d $SYSUSB/$KEYBOARD_USB ]]; then + echo "$KEYBOARD_USB" + return + fi + # Fallback only. Combo receivers (Razer/ROG) register fake keyboard inputs on + # mice too, so this can pick the wrong one — set KEYBOARD_USB to be sure. + local i dev p + for i in /sys/class/input/input*; do + [[ -e $i/capabilities/key ]] || continue + [[ $(cat "$i/name" 2>/dev/null) == *[Kk]eyboard* ]] || continue + # skip pointers (they own a mouse/js handler) + compgen -G "$i/mouse*" >/dev/null && continue + dev=$(readlink -f "$i/device" 2>/dev/null) || continue + # walk up to the usb_device node + while [[ $dev == /sys/* && $dev != / ]]; do + if [[ -f $dev/idVendor ]]; then + p=${dev##*/} + [[ -d $SYSUSB/$p ]] && { echo "$p"; return; } + break + fi + dev=${dev%/*} + done + done +} + +# --- actions ---------------------------------------------------------------- + +set_auth() { + local path=$1 val=$2 + if [[ -w $SYSUSB/$path/authorized ]]; then + echo "$val" > "$SYSUSB/$path/authorized" + else + pkexec /bin/sh -c "echo $val > $SYSUSB/$path/authorized" + fi +} + +unplug() { + local path=$1 name blocks + name=$(dev_name "$path") + blocks=$(dev_blocks "$path") + if [[ -n $blocks ]]; then + local b part + for b in $blocks; do + for part in /sys/block/"$b"/"$b"*; do + [[ -e $part ]] && udisksctl unmount -b "/dev/${part##*/}" --no-user-interaction 2>/dev/null + done + udisksctl unmount -b "/dev/$b" --no-user-interaction 2>/dev/null + done + for b in $blocks; do + if udisksctl power-off -b "/dev/$b" --no-user-interaction; then + notify "Ejected" "$name ($b)" + return 0 + fi + done + fi + set_auth "$path" 0 && notify "Unplugged" "$name [$path]" +} + +plug() { + local path=$1 + set_auth "$path" 1 && notify "Plugged" "$(dev_name "$path") [$path]" +} + +replug() { + local path=$1 + set_auth "$path" 0 || return 1 + sleep 1 + set_auth "$path" 1 && notify "Replugged" "$(dev_name "$path") [$path]" +} + +rereplug_keyboard() { + local p + p=$(keyboard_path) + [[ -n $p ]] || { notify "No keyboard found" "set KEYBOARD_USB in usb_devices.sh"; return 1; } + replug "$p" +} + +plug_all() { + local p + for p in $(usb_paths); do + [[ $(dev_authorized "$p") == 1 ]] || continue + set_auth "$p" 0 || continue + sleep 1 + set_auth "$p" 1 + done + notify "All USB devices replugged" +} + +replug_keyboard() { + local p + p=$(keyboard_path) + [[ -n $p ]] || { notify "No keyboard found" "set KEYBOARD_USB in usb_devices.sh"; return 1; } + replug "$p" +} + +plug_all() { + local p + for p in $(usb_paths); do + [[ $(dev_authorized "$p") == 0 ]] && set_auth "$p" 1 + done + notify "All USB devices plugged" +} + +# JSON for a waybar custom module. +waybar_json() { + local total=0 off=0 p kb kbname state + for p in $(usb_paths); do + total=$((total + 1)) + [[ $(dev_authorized "$p") == 0 ]] && off=$((off + 1)) + done + kb=$(keyboard_path) + if [[ -n $kb ]]; then + kbname="$(dev_name "$kb") [$kb]" + [[ $(dev_authorized "$kb") == 1 ]] && state="plugged" || state="UNPLUGGED" + kbname="keyboard: $kbname ($state)" + else + kbname="keyboard: not identified" + fi + local tip="left click: USB menu\nright click: replug keyboard\nmiddle click: replug all\n\n$kbname\n$off of $total devices unplugged" + printf '{"text":"\uf287","tooltip":"%s","class":"%s"}\n' "$tip" "$([[ $off -gt 0 ]] && echo unplugged || echo ok)" +} + +# --- menus ------------------------------------------------------------------ + +list_devices() { + local p state icon blocks extra kb line + kb=$(keyboard_path) + for p in $(usb_paths); do + state=$(dev_authorized "$p") + [[ $state == 1 ]] && icon="šŸ”Œ" || icon="ā›”" + blocks=$(dev_blocks "$p" | tr '\n' ' ') + extra="" + [[ -n $blocks ]] && extra=" [${blocks% }]" + is_hub "$p" && extra="$extra [hub]" + [[ $p == "$kb" ]] && extra="$extra [āŒØļø keyboard]" + line=$(printf '%s %s%s (%s)' "$icon" "$(dev_name "$p")" "$extra" "$p") + # keyboard first — it is the one you need when you cannot type + [[ $p == "$kb" ]] && printf '0\t%s\n' "$line" || printf '1\t%s\n' "$line" + done | sort -s -k1,1 | cut -f2- +} + +menu() { + local choice path + choice=$(printf '%s\nšŸ”„ Replug All\nšŸ’” Plug All\nāŒ Cancel\n' "$(list_devices)" | rofi -dmenu -p "USB" -i) + [[ -z $choice ]] && exit 0 + + case "$choice" in + "šŸ”„ Replug All") replug_all; return ;; + "šŸ’” Plug All") plug_all; return ;; + "āŒ Cancel") return ;; + esac + + path=$(sed -n 's/.*(\(.*\))$/\1/p' <<<"$choice") + [[ -d $SYSUSB/$path ]] || { notify "Unknown device" "$choice"; return; } + + local name state actions act + name=$(dev_name "$path") + state=$(dev_authorized "$path") + if [[ $state == 1 ]]; then + if [[ $path == "$(keyboard_path)" ]]; then + actions="šŸ”„ Replug\nā›” Unplug" + else + actions="ā›” Unplug\nšŸ”„ Replug" + fi + [[ -n $(dev_blocks "$path") ]] && actions="āļø Eject (unmount + power off)\n$actions" + else + actions="šŸ”Œ Plug\nšŸ”„ Replug" + fi + act=$(printf '%b\nāŒ Cancel\n' "$actions" | rofi -dmenu -p "$name" -i) + + case "$act" in + "āļø Eject (unmount + power off)") unplug "$path" ;; + "ā›” Unplug") unplug "$path" ;; + "šŸ”Œ Plug") plug "$path" ;; + "šŸ”„ Replug") replug "$path" ;; + esac +} + +# --- entry ------------------------------------------------------------------ + +case "${1:-menu}" in + menu) menu ;; + list) list_devices ;; + off) unplug "${2:?usage: $0 off }" ;; + on) plug "${2:?usage: $0 on }" ;; + replug) replug "${2:?usage: $0 replug }" ;; + all-on) plug_all ;; + all-replug) replug_all ;; + replug-keyboard|kb) replug_keyboard ;; + waybar) waybar_json ;; + *) echo "usage: ${0##*/} [menu|list|off |on |replug |all-on|all-replug|replug-keyboard|waybar]" >&2; exit 1 ;; +esac diff --git a/.config/waybar/config b/.config/waybar/config index 317b7ba..da4d5d3 100644 --- a/.config/waybar/config +++ b/.config/waybar/config @@ -86,6 +86,24 @@ "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" }, + "custom/usb": { + "format": "{}", + "exec": "bash ~/.config/scripts/system/usb_menu.sh waybar", + "interval": 10, + "return-type": "json", + "tooltip": true, + "on-click": "bash ~/.config/scripts/system/usb_menu.sh menu", + "on-click-right": "bash ~/.config/scripts/system/usb_menu.sh replug-keyboard", + "on-click-middle": "bash ~/.config/scripts/system/usb_menu.sh all-replug" + }, + + "custom/power": { + "format": "\uf011", + "tooltip": true, + "tooltip-format": "Power menu (Mod+Escape)", + "on-click": "bash ~/.config/scripts/system/power_menu.sh" + }, + "custom/camera": { "format": "{text}", "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar", @@ -130,7 +148,9 @@ }, "cpu": { "interval": 1, - "format": "{usage}%" + "format": "{usage}%", + "tooltip": true, + "tooltip-format": "CPU {usage}%\nload {load}\nfreq {avg_frequency} GHz (max {max_frequency})" }, "memory": { "interval": 1, @@ -159,6 +179,26 @@ "on-click-right": "pavucontrol", "format-muted": "\uf00d {volume}%" }, + // Nested drawer: hovering the ☰ reveals the ⋯ (plus the tray icons); hovering + // the ⋯ expands the USB and power menus. Nothing extra is claimed on the bar. + "group/system": { + "orientation": "horizontal", + "drawer": { + "transition-duration": 300, + "transition-left-to-right": false + }, + "modules": [ + "custom/system-toggle", + "custom/usb", + "custom/power" + ] + }, + + "custom/system-toggle": { + "format": "\u22ef", + "tooltip-format": "USB + power" + }, + "group/tray": { "orientation": "horizontal", "drawer": { @@ -167,6 +207,7 @@ }, "modules": [ "custom/tray-toggle", + "group/system", "tray" ] }, diff --git a/.config/waybar/config-niri b/.config/waybar/config-niri index d6e126b..d5597bd 100644 --- a/.config/waybar/config-niri +++ b/.config/waybar/config-niri @@ -86,6 +86,24 @@ "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" }, + "custom/usb": { + "format": "{}", + "exec": "bash ~/.config/scripts/system/usb_menu.sh waybar", + "interval": 10, + "return-type": "json", + "tooltip": true, + "on-click": "bash ~/.config/scripts/system/usb_menu.sh menu", + "on-click-right": "bash ~/.config/scripts/system/usb_menu.sh replug-keyboard", + "on-click-middle": "bash ~/.config/scripts/system/usb_menu.sh all-replug" + }, + + "custom/power": { + "format": "\uf011", + "tooltip": true, + "tooltip-format": "Power menu (Mod+Escape)", + "on-click": "bash ~/.config/scripts/system/power_menu.sh" + }, + "custom/camera": { "format": "{text}", "exec": "bash ~/.config/scripts/waybar/camera_monitor.sh bar", @@ -133,7 +151,9 @@ }, "cpu": { "interval": 1, - "format": "{usage}%" + "format": "{usage}%", + "tooltip": true, + "tooltip-format": "CPU {usage}%\nload {load}\nfreq {avg_frequency} GHz (max {max_frequency})" }, "memory": { "interval": 1, @@ -162,6 +182,26 @@ "on-click-right": "pavucontrol", "format-muted": "\uf00d {volume}%" }, + // Nested drawer: hovering the ☰ reveals the ⋯ (plus the tray icons); hovering + // the ⋯ expands the USB and power menus. Nothing extra is claimed on the bar. + "group/system": { + "orientation": "horizontal", + "drawer": { + "transition-duration": 300, + "transition-left-to-right": false + }, + "modules": [ + "custom/system-toggle", + "custom/usb", + "custom/power" + ] + }, + + "custom/system-toggle": { + "format": "\u22ef", + "tooltip-format": "USB + power" + }, + "group/tray": { "orientation": "horizontal", "drawer": { @@ -170,6 +210,7 @@ }, "modules": [ "custom/tray-toggle", + "group/system", "tray" ] }, diff --git a/.config/waybar/style.css b/.config/waybar/style.css index 8d4e86d..d8e24fc 100644 --- a/.config/waybar/style.css +++ b/.config/waybar/style.css @@ -45,3 +45,16 @@ box-shadow: none; font-size: 16px; } + +/* USB + power live behind the ⋯, which itself lives inside the ☰ tray drawer, + so none of the three claims bar width until the hamburger is hovered. */ +#custom-system-toggle, +#custom-usb, +#custom-power { + padding: 0 6px; + font-size: 15px; +} + +#custom-usb.unplugged { + color: #ff6b6b; +}