Replace rofi monitor and USB menus with launcher plugins
Browse files at this commit363097c33170b46c3f57445ced0ea52fee752b7eParent: e5da62d- feat
- replace rofi monitor and USB menus with launcher plugins
7 changed files +378 -95
- .config/niri/config.kdl +2 -2
- .config/noctalia/plugins/monitors/monitors.luau +77 -0
- .config/noctalia/plugins/monitors/plugin.toml +17 -0
- .config/noctalia/plugins/usb/plugin.toml +17 -0
- .config/noctalia/plugins/usb/usb.luau +176 -0
- .config/scripts/niri/toggle_monitors.sh +59 -26
- .config/scripts/system/usb_menu.sh +30 -67
.config/niri/config.kdlmodified+2 -2
| @@ -207,8 +207,8 @@ binds { | |||
| 207 | Mod+X hotkey-overlay-title="Show desktop" { spawn-sh "~/.config/scripts/niri/show_desktop.sh"; } | 207 | Mod+X hotkey-overlay-title="Show desktop" { spawn-sh "~/.config/scripts/niri/show_desktop.sh"; } |
| 208 | Mod+H hotkey-overlay-title="Toggle bar" { spawn-sh "noctalia msg bar-toggle"; } | 208 | Mod+H hotkey-overlay-title="Toggle bar" { spawn-sh "noctalia msg bar-toggle"; } |
| 209 | Mod+B hotkey-overlay-title="Noctalia: wallpaper picker" { spawn-sh "noctalia msg panel-toggle wallpaper"; } | 209 | Mod+B hotkey-overlay-title="Noctalia: wallpaper picker" { spawn-sh "noctalia msg panel-toggle wallpaper"; } |
| 210 | - Mod+Shift+M hotkey-overlay-title="Monitors menu" { spawn-sh "~/.config/scripts/niri/toggle_monitors.sh"; } | 210 | + Mod+Shift+M hotkey-overlay-title="Noctalia: monitors menu" { spawn-sh "noctalia msg panel-toggle launcher /monitors"; } |
| 211 | - Mod+Shift+K hotkey-overlay-title="USB plug/unplug menu" { spawn-sh "bash ~/.config/scripts/system/usb_menu.sh"; } | 211 | + Mod+Shift+K hotkey-overlay-title="Noctalia: USB plug/unplug menu" { spawn-sh "noctalia msg panel-toggle launcher /usb"; } |
| 212 | Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; } | 212 | Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; } |
| 213 | 213 | ||
| 214 | Alt+4 repeat=false { close-window; } | 214 | Alt+4 repeat=false { close-window; } |
.config/noctalia/plugins/monitors/monitors.luauadded+77 -0
| @@ -0,0 +1,77 @@ | |||
| 1 | +--!nonstrict | ||
| 2 | +-- Monitors launcher provider. | ||
| 3 | +-- | ||
| 4 | +-- Type `/monitors` in the launcher to list monitor actions built from the live | ||
| 5 | +-- niri outputs; Enter runs the action. Listing and actions live in | ||
| 6 | +-- ~/.config/scripts/niri/toggle_monitors.sh (`list` and `run <action>` modes). | ||
| 7 | + | ||
| 8 | +local SCRIPT = "$HOME/.config/scripts/niri/toggle_monitors.sh" | ||
| 9 | + | ||
| 10 | +-- nil until the first list arrives; rescanned whenever the menu opens (empty query) | ||
| 11 | +-- so outputs that appeared or changed show up, but not on every keystroke. | ||
| 12 | +local monitors = nil | ||
| 13 | +local lastQuery = "" | ||
| 14 | + | ||
| 15 | +local function parse(stdout) | ||
| 16 | + local list = {} | ||
| 17 | + for line in stdout:gmatch("[^\n]+") do | ||
| 18 | + local action, title, glyph = line:match("^([^\t]*)\t([^\t]*)\t(.+)$") | ||
| 19 | + if action and action ~= "" then | ||
| 20 | + table.insert(list, { action = action, title = title, glyph = glyph }) | ||
| 21 | + end | ||
| 22 | + end | ||
| 23 | + return list | ||
| 24 | +end | ||
| 25 | + | ||
| 26 | +local function publish(query) | ||
| 27 | + if not monitors then | ||
| 28 | + launcher.setResults(query, { | ||
| 29 | + { id = "", title = "Loading monitors…", glyph = "loader" }, | ||
| 30 | + }) | ||
| 31 | + return | ||
| 32 | + end | ||
| 33 | + | ||
| 34 | + local text = noctalia.string.trim(query) | ||
| 35 | + local rows = {} | ||
| 36 | + for _, m in monitors do | ||
| 37 | + local score = 0 | ||
| 38 | + if text ~= "" then | ||
| 39 | + score = noctalia.fuzzyScore(text, m.title) | ||
| 40 | + end | ||
| 41 | + if score then | ||
| 42 | + table.insert(rows, { | ||
| 43 | + id = m.action, | ||
| 44 | + title = m.title, | ||
| 45 | + glyph = m.glyph, | ||
| 46 | + score = score, | ||
| 47 | + }) | ||
| 48 | + end | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | + if #rows == 0 then | ||
| 52 | + rows[1] = { id = "", title = "No matches", glyph = "search" } | ||
| 53 | + end | ||
| 54 | + launcher.setResults(query, rows) | ||
| 55 | +end | ||
| 56 | + | ||
| 57 | +function onQuery(query) | ||
| 58 | + lastQuery = query | ||
| 59 | + publish(query) | ||
| 60 | + if monitors and noctalia.string.trim(query) ~= "" then | ||
| 61 | + return | ||
| 62 | + end | ||
| 63 | + noctalia.runAsync(SCRIPT .. " list", function(result) | ||
| 64 | + if result.exitCode == 0 then | ||
| 65 | + monitors = parse(result.stdout) | ||
| 66 | + publish(lastQuery) | ||
| 67 | + end | ||
| 68 | + end) | ||
| 69 | +end | ||
| 70 | + | ||
| 71 | +function onActivate(id) | ||
| 72 | + -- ids are our own actions (all-off, all-on, off:DP-1, on:HDMI-A-1); refuse anything else | ||
| 73 | + if not id:match("^[%w:%-_.]+$") then | ||
| 74 | + return | ||
| 75 | + end | ||
| 76 | + noctalia.runAsync(SCRIPT .. " run '" .. id .. "'") | ||
| 77 | +end | ||
.config/noctalia/plugins/monitors/plugin.tomladded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +id = "tahinli/monitors" | ||
| 2 | +name = "Monitors" | ||
| 3 | +version = "1.0.0" | ||
| 4 | +plugin_api = 3 | ||
| 5 | +author = "tahinli" | ||
| 6 | +license = "MIT" | ||
| 7 | +dependencies = [] | ||
| 8 | +tags = ["launcher", "niri", "monitors"] | ||
| 9 | +icon = "device-desktop" | ||
| 10 | +description = "Turn niri monitors on or off from the launcher (replaces the rofi monitors menu)." | ||
| 11 | + | ||
| 12 | +[[launcher_provider]] | ||
| 13 | +id = "monitors" | ||
| 14 | +entry = "monitors.luau" | ||
| 15 | +prefix = "monitors" | ||
| 16 | +glyph = "device-desktop" | ||
| 17 | +include_in_global_search = false | ||
.config/noctalia/plugins/usb/plugin.tomladded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +id = "tahinli/usb" | ||
| 2 | +name = "USB" | ||
| 3 | +version = "1.0.0" | ||
| 4 | +plugin_api = 3 | ||
| 5 | +author = "tahinli" | ||
| 6 | +license = "MIT" | ||
| 7 | +dependencies = [] | ||
| 8 | +tags = ["launcher", "usb"] | ||
| 9 | +icon = "usb" | ||
| 10 | +description = "Plug, unplug, eject and replug USB devices (usb_menu.sh)." | ||
| 11 | + | ||
| 12 | +[[launcher_provider]] | ||
| 13 | +id = "usb" | ||
| 14 | +entry = "usb.luau" | ||
| 15 | +prefix = "usb" | ||
| 16 | +glyph = "usb" | ||
| 17 | +include_in_global_search = false | ||
.config/noctalia/plugins/usb/usb.luauadded+176 -0
| @@ -0,0 +1,176 @@ | |||
| 1 | +--!nonstrict | ||
| 2 | +-- USB launcher provider (replaces the rofi USB menu). | ||
| 3 | +-- | ||
| 4 | +-- Type `/usb` in the launcher to list USB devices. Enter on a device opens its | ||
| 5 | +-- actions (Plug / Unplug / Replug, Eject for storage); "Replug all" and "Plug all" | ||
| 6 | +-- run on every device. Discovery and every action live in | ||
| 7 | +-- ~/.config/scripts/system/usb_menu.sh. | ||
| 8 | + | ||
| 9 | +local SCRIPT = "$HOME/.config/scripts/system/usb_menu.sh" | ||
| 10 | + | ||
| 11 | +-- device list from `usb_menu.sh tsv`; refreshed on an empty query and when a | ||
| 12 | +-- device level is entered. byPath indexes the same entries by sysfs path. | ||
| 13 | +local devices = {} | ||
| 14 | +local byPath = {} | ||
| 15 | +local loaded = false | ||
| 16 | +local lastQuery = "" | ||
| 17 | + | ||
| 18 | +local function trim(s) | ||
| 19 | + return noctalia.string.trim(s) | ||
| 20 | +end | ||
| 21 | + | ||
| 22 | +-- lines: path \t authorized \t name \t extra (extra may be empty) | ||
| 23 | +local function parse(stdout) | ||
| 24 | + local list = {} | ||
| 25 | + local index = {} | ||
| 26 | + for line in stdout:gmatch("[^\n]+") do | ||
| 27 | + local path, auth, rest = line:match("^([^\t]*)\t([^\t]*)\t(.*)$") | ||
| 28 | + if path and path ~= "" then | ||
| 29 | + local name, extra = rest:match("^([^\t]*)\t?(.*)$") | ||
| 30 | + local dev = { | ||
| 31 | + path = path, | ||
| 32 | + plugged = auth == "1", | ||
| 33 | + name = name, | ||
| 34 | + extra = extra, | ||
| 35 | + keyboard = extra:find("keyboard", 1, true) ~= nil, | ||
| 36 | + block = extra:find("[sd", 1, true) ~= nil or extra:find("[nvme", 1, true) ~= nil, | ||
| 37 | + } | ||
| 38 | + table.insert(list, dev) | ||
| 39 | + index[path] = dev | ||
| 40 | + end | ||
| 41 | + end | ||
| 42 | + return list, index | ||
| 43 | +end | ||
| 44 | + | ||
| 45 | +local function refresh(done) | ||
| 46 | + local started = noctalia.runAsync(SCRIPT .. " tsv", function(result) | ||
| 47 | + if result.exitCode == 0 then | ||
| 48 | + devices, byPath = parse(result.stdout) | ||
| 49 | + loaded = true | ||
| 50 | + end | ||
| 51 | + done() | ||
| 52 | + end) | ||
| 53 | + if not started then | ||
| 54 | + done() | ||
| 55 | + end | ||
| 56 | +end | ||
| 57 | + | ||
| 58 | +local function publishRoot(query) | ||
| 59 | + if not loaded then | ||
| 60 | + launcher.setResults(query, { | ||
| 61 | + { id = "", title = "Loading devices…", glyph = "loader" }, | ||
| 62 | + }) | ||
| 63 | + return | ||
| 64 | + end | ||
| 65 | + | ||
| 66 | + local text = trim(query) | ||
| 67 | + local rows = {} | ||
| 68 | + for _, dev in devices do | ||
| 69 | + local score = 0 | ||
| 70 | + if text ~= "" then | ||
| 71 | + score = noctalia.fuzzyScore(text, dev.name .. " " .. dev.path) | ||
| 72 | + end | ||
| 73 | + if score then | ||
| 74 | + local sub = dev.path | ||
| 75 | + if dev.extra ~= "" then | ||
| 76 | + sub = dev.path .. " " .. dev.extra | ||
| 77 | + end | ||
| 78 | + table.insert(rows, { | ||
| 79 | + id = dev.path, | ||
| 80 | + title = dev.name, | ||
| 81 | + subtitle = sub, | ||
| 82 | + glyph = if dev.keyboard then "keyboard" elseif dev.plugged then "plug" else "plug-off", | ||
| 83 | + score = score, | ||
| 84 | + query = dev.path, -- opens this device's action level | ||
| 85 | + }) | ||
| 86 | + end | ||
| 87 | + end | ||
| 88 | + | ||
| 89 | + local globals = { | ||
| 90 | + { id = "all-replug", title = "Replug all", glyph = "refresh" }, | ||
| 91 | + { id = "all-on", title = "Plug all", glyph = "plug" }, | ||
| 92 | + } | ||
| 93 | + for _, g in globals do | ||
| 94 | + local score = 0 | ||
| 95 | + if text ~= "" then | ||
| 96 | + score = noctalia.fuzzyScore(text, g.title) | ||
| 97 | + end | ||
| 98 | + if score then | ||
| 99 | + table.insert(rows, { id = g.id, title = g.title, glyph = g.glyph, score = score }) | ||
| 100 | + end | ||
| 101 | + end | ||
| 102 | + | ||
| 103 | + if #rows == 0 then | ||
| 104 | + rows[1] = { id = "", title = "No matches", glyph = "search" } | ||
| 105 | + end | ||
| 106 | + launcher.setResults(query, rows) | ||
| 107 | +end | ||
| 108 | + | ||
| 109 | +local function publishDevice(query, dev) | ||
| 110 | + local p = dev.path | ||
| 111 | + local rows = {} | ||
| 112 | + local function add(id, title, glyph) | ||
| 113 | + table.insert(rows, { id = id, title = title, subtitle = dev.name, glyph = glyph }) | ||
| 114 | + end | ||
| 115 | + | ||
| 116 | + if dev.plugged then | ||
| 117 | + if dev.keyboard then | ||
| 118 | + add("replug:" .. p, "Replug", "refresh") | ||
| 119 | + end | ||
| 120 | + if dev.block then | ||
| 121 | + add("off:" .. p, "Eject (unmount + power off)", "eject") | ||
| 122 | + end | ||
| 123 | + add("off:" .. p, "Unplug", "plug-off") | ||
| 124 | + if not dev.keyboard then | ||
| 125 | + add("replug:" .. p, "Replug", "refresh") | ||
| 126 | + end | ||
| 127 | + else | ||
| 128 | + add("on:" .. p, "Plug", "plug") | ||
| 129 | + add("replug:" .. p, "Replug", "refresh") | ||
| 130 | + end | ||
| 131 | + | ||
| 132 | + table.insert(rows, { id = "back", title = "Back", glyph = "arrow-left", query = "" }) | ||
| 133 | + launcher.setResults(query, rows) | ||
| 134 | +end | ||
| 135 | + | ||
| 136 | +-- pick the level from the current query: exact known device path -> actions | ||
| 137 | +local function render(query) | ||
| 138 | + local dev = byPath[trim(query)] | ||
| 139 | + if dev then | ||
| 140 | + publishDevice(query, dev) | ||
| 141 | + else | ||
| 142 | + publishRoot(query) | ||
| 143 | + end | ||
| 144 | +end | ||
| 145 | + | ||
| 146 | +function onQuery(query) | ||
| 147 | + lastQuery = query | ||
| 148 | + local text = trim(query) | ||
| 149 | + render(query) | ||
| 150 | + if text == "" or byPath[text] then | ||
| 151 | + refresh(function() | ||
| 152 | + render(lastQuery) | ||
| 153 | + end) | ||
| 154 | + end | ||
| 155 | +end | ||
| 156 | + | ||
| 157 | +function onActivate(id) | ||
| 158 | + -- ids are built by this plugin; refuse anything outside the plain token alphabet | ||
| 159 | + if not id:match("^[%w%-:.]+$") then | ||
| 160 | + return | ||
| 161 | + end | ||
| 162 | + | ||
| 163 | + local cmd = nil | ||
| 164 | + if id == "all-replug" or id == "all-on" then | ||
| 165 | + cmd = id | ||
| 166 | + else | ||
| 167 | + local action, path = id:match("^(%a+):([%w%-%.]+)$") | ||
| 168 | + if action == "off" or action == "on" or action == "replug" then | ||
| 169 | + cmd = action .. " " .. path | ||
| 170 | + end | ||
| 171 | + end | ||
| 172 | + | ||
| 173 | + if cmd then | ||
| 174 | + noctalia.runAsync(SCRIPT .. " " .. cmd) | ||
| 175 | + end | ||
| 176 | +end | ||
.config/scripts/niri/toggle_monitors.shmodified+59 -26
| @@ -2,36 +2,69 @@ | |||
| 2 | 2 | ||
| 3 | source "$(dirname "$0")/monitors.sh" | 3 | source "$(dirname "$0")/monitors.sh" |
| 4 | 4 | ||
| 5 | -case "${1:-menu}" in | 5 | +usage() { |
| 6 | - menu) | 6 | + echo "usage: $(basename "$0") list | run <action> | on" |
| 7 | - menu=$(echo -e "🖥️ All Monitors Off\n🖥️ $LEFT_MONITOR Off\n🖥️ $RIGHT_MONITOR Off\n💡 All Monitors On\n💡 $LEFT_MONITOR On\n💡 $RIGHT_MONITOR On\n❌ Cancel" | rofi -dmenu -p "Monitors" -i) | 7 | + echo " list TSV rows (action, title, glyph) for the noctalia monitors plugin" |
| 8 | + echo " run <action> all-off | all-on | off:NAME | on:NAME" | ||
| 9 | + echo " on turn LEFT_MONITOR and RIGHT_MONITOR on" | ||
| 10 | +} | ||
| 8 | 11 | ||
| 9 | - case "$menu" in | 12 | +list_rows() { |
| 10 | - "🖥️ All Monitors Off") | 13 | + local json |
| 11 | - niri msg action power-off-monitors | 14 | + json=$(niri msg -j outputs) || return 1 |
| 12 | - ;; | 15 | + printf 'all-off\tAll monitors off\tdevice-desktop-off\n' |
| 13 | - "🖥️ $LEFT_MONITOR Off") | 16 | + printf 'all-on\tAll monitors on\tbulb\n' |
| 14 | - niri msg output "$LEFT_MONITOR" off | 17 | + jq -r ' |
| 15 | - ;; | 18 | + .[] | . as $o |
| 16 | - "🖥️ $RIGHT_MONITOR Off") | 19 | + | if $o.current_mode != null then |
| 17 | - niri msg output "$RIGHT_MONITOR" off | 20 | + "off:\($o.name)\t\($o.name) off (\($o.make) \($o.model))\tdevice-desktop-off" |
| 18 | - ;; | 21 | + else |
| 19 | - "💡 All Monitors On") | 22 | + "on:\($o.name)\t\($o.name) on (\($o.make) \($o.model))\tdevice-desktop" |
| 20 | - niri msg output "$LEFT_MONITOR" on | 23 | + end' <<< "$json" |
| 21 | - niri msg output "$RIGHT_MONITOR" on | 24 | +} |
| 22 | - ;; | 25 | + |
| 23 | - "💡 $LEFT_MONITOR On") | 26 | +run_action() { |
| 24 | - niri msg output "$LEFT_MONITOR" on | 27 | + local action="$1" json name |
| 25 | - ;; | 28 | + local -a names |
| 26 | - "💡 $RIGHT_MONITOR On") | 29 | + json=$(niri msg -j outputs) || return 1 |
| 27 | - niri msg output "$RIGHT_MONITOR" on | 30 | + case "$action" in |
| 28 | - ;; | 31 | + all-off) |
| 29 | - "❌ Cancel") | 32 | + niri msg action power-off-monitors |
| 30 | - ;; | 33 | + ;; |
| 31 | - esac | 34 | + all-on) |
| 35 | + mapfile -t names < <(jq -r 'keys[]' <<< "$json") | ||
| 36 | + for name in "${names[@]}"; do | ||
| 37 | + niri msg output "$name" on | ||
| 38 | + done | ||
| 39 | + ;; | ||
| 40 | + off:*|on:*) | ||
| 41 | + name="${action#*:}" | ||
| 42 | + if ! jq -e --arg n "$name" 'has($n)' <<< "$json" > /dev/null; then | ||
| 43 | + echo "unknown output: $name" >&2 | ||
| 44 | + return 1 | ||
| 45 | + fi | ||
| 46 | + niri msg output "$name" "${action%%:*}" | ||
| 47 | + ;; | ||
| 48 | + *) | ||
| 49 | + echo "unknown action: $action" >&2 | ||
| 50 | + return 1 | ||
| 51 | + ;; | ||
| 52 | + esac | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +case "${1:-}" in | ||
| 56 | + list) | ||
| 57 | + list_rows | ||
| 58 | + ;; | ||
| 59 | + run) | ||
| 60 | + run_action "${2:-}" | ||
| 32 | ;; | 61 | ;; |
| 33 | on) | 62 | on) |
| 34 | niri msg output "$LEFT_MONITOR" on | 63 | niri msg output "$LEFT_MONITOR" on |
| 35 | niri msg output "$RIGHT_MONITOR" on | 64 | niri msg output "$RIGHT_MONITOR" on |
| 36 | ;; | 65 | ;; |
| 66 | + *) | ||
| 67 | + usage | ||
| 68 | + exit 1 | ||
| 69 | + ;; | ||
| 37 | esac | 70 | esac |
.config/scripts/system/usb_menu.shmodified+30 -67
| @@ -1,8 +1,9 @@ | |||
| 1 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | -# USB soft plug/unplug menu (rofi), compositor-agnostic. | 2 | +# USB soft plug/unplug helper, compositor-agnostic. |
| 3 | # "Unplug" = deauthorize the device in sysfs: kernel unbinds its drivers and it | 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. | 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). | 5 | # Storage devices are unmounted/powered off through udisks instead (no root). |
| 6 | +# Front end: the noctalia "usb" launcher plugin (tsv mode) and direct CLI calls. | ||
| 6 | 7 | ||
| 7 | set -u | 8 | set -u |
| 8 | 9 | ||
| @@ -51,6 +52,17 @@ dev_blocks() { | |||
| 51 | 52 | ||
| 52 | is_hub() { [[ $(cat "$SYSUSB/$1"/*:*/bInterfaceClass 2>/dev/null | head -1) == "09" ]]; } | 53 | is_hub() { [[ $(cat "$SYSUSB/$1"/*:*/bInterfaceClass 2>/dev/null | head -1) == "09" ]]; } |
| 53 | 54 | ||
| 55 | +# info tags for one device, leading space included: " [sdb] [hub] [⌨️ keyboard]" | ||
| 56 | +# args: path keyboard_path | ||
| 57 | +dev_extra() { | ||
| 58 | + local p=$1 kb=$2 blocks extra="" | ||
| 59 | + blocks=$(dev_blocks "$p" | tr '\n' ' ') | ||
| 60 | + [[ -n $blocks ]] && extra=" [${blocks% }]" | ||
| 61 | + is_hub "$p" && extra="$extra [hub]" | ||
| 62 | + [[ $p == "$kb" ]] && extra="$extra [⌨️ keyboard]" | ||
| 63 | + echo "$extra" | ||
| 64 | +} | ||
| 65 | + | ||
| 54 | # sysfs path of the keyboard: configured value wins, else the USB device whose | 66 | # 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. | 67 | # input children expose a kbd handler and are not a pointer. |
| 56 | keyboard_path() { | 68 | keyboard_path() { |
| @@ -150,92 +162,43 @@ plug_all() { | |||
| 150 | notify "All USB devices plugged" | 162 | notify "All USB devices plugged" |
| 151 | } | 163 | } |
| 152 | 164 | ||
| 153 | -# JSON for a waybar custom module. | 165 | +# --- listings --------------------------------------------------------------- |
| 154 | -waybar_json() { | ||
| 155 | - local total=0 off=0 p kb kbname state | ||
| 156 | - for p in $(usb_paths); do | ||
| 157 | - total=$((total + 1)) | ||
| 158 | - [[ $(dev_authorized "$p") == 0 ]] && off=$((off + 1)) | ||
| 159 | - done | ||
| 160 | - kb=$(keyboard_path) | ||
| 161 | - if [[ -n $kb ]]; then | ||
| 162 | - kbname="$(dev_name "$kb") [$kb]" | ||
| 163 | - [[ $(dev_authorized "$kb") == 1 ]] && state="plugged" || state="UNPLUGGED" | ||
| 164 | - kbname="keyboard: $kbname ($state)" | ||
| 165 | - else | ||
| 166 | - kbname="keyboard: not identified" | ||
| 167 | - fi | ||
| 168 | - local tip="left click: USB menu\nright click: replug keyboard\nmiddle click: replug all\n\n$kbname\n$off of $total devices unplugged" | ||
| 169 | - printf '{"text":"\uf287","tooltip":"%s","class":"%s"}\n' "$tip" "$([[ $off -gt 0 ]] && echo unplugged || echo ok)" | ||
| 170 | -} | ||
| 171 | - | ||
| 172 | -# --- menus ------------------------------------------------------------------ | ||
| 173 | 166 | ||
| 174 | list_devices() { | 167 | list_devices() { |
| 175 | - local p state icon blocks extra kb line | 168 | + local p state icon kb line |
| 176 | kb=$(keyboard_path) | 169 | kb=$(keyboard_path) |
| 177 | for p in $(usb_paths); do | 170 | for p in $(usb_paths); do |
| 178 | state=$(dev_authorized "$p") | 171 | state=$(dev_authorized "$p") |
| 179 | [[ $state == 1 ]] && icon="🔌" || icon="⛔" | 172 | [[ $state == 1 ]] && icon="🔌" || icon="⛔" |
| 180 | - blocks=$(dev_blocks "$p" | tr '\n' ' ') | 173 | + line=$(printf '%s %s%s (%s)' "$icon" "$(dev_name "$p")" "$(dev_extra "$p" "$kb")" "$p") |
| 181 | - extra="" | ||
| 182 | - [[ -n $blocks ]] && extra=" [${blocks% }]" | ||
| 183 | - is_hub "$p" && extra="$extra [hub]" | ||
| 184 | - [[ $p == "$kb" ]] && extra="$extra [⌨️ keyboard]" | ||
| 185 | - line=$(printf '%s %s%s (%s)' "$icon" "$(dev_name "$p")" "$extra" "$p") | ||
| 186 | # keyboard first — it is the one you need when you cannot type | 174 | # keyboard first — it is the one you need when you cannot type |
| 187 | [[ $p == "$kb" ]] && printf '0\t%s\n' "$line" || printf '1\t%s\n' "$line" | 175 | [[ $p == "$kb" ]] && printf '0\t%s\n' "$line" || printf '1\t%s\n' "$line" |
| 188 | done | sort -s -k1,1 | cut -f2- | 176 | done | sort -s -k1,1 | cut -f2- |
| 189 | } | 177 | } |
| 190 | 178 | ||
| 191 | -menu() { | 179 | +# one line per device: path<TAB>authorized(0/1)<TAB>name<TAB>extra |
| 192 | - local choice path | 180 | +# keyboard first; extra is dev_extra without its leading space (may be empty) |
| 193 | - choice=$(printf '%s\n🔄 Replug All\n💡 Plug All\n❌ Cancel\n' "$(list_devices)" | rofi -dmenu -p "USB" -i) | 181 | +list_tsv() { |
| 194 | - [[ -z $choice ]] && exit 0 | 182 | + local p state kb |
| 195 | - | 183 | + kb=$(keyboard_path) |
| 196 | - case "$choice" in | 184 | + for p in $(usb_paths); do |
| 197 | - "🔄 Replug All") replug_all; return ;; | 185 | + state=$(dev_authorized "$p") |
| 198 | - "💡 Plug All") plug_all; return ;; | 186 | + [[ $state == 1 ]] || state=0 |
| 199 | - "❌ Cancel") return ;; | 187 | + [[ $p == "$kb" ]] && printf '0\t' || printf '1\t' |
| 200 | - esac | 188 | + printf '%s\t%s\t%s\t%s\n' "$p" "$state" "$(dev_name "$p")" "$(dev_extra "$p" "$kb" | sed 's/^ //')" |
| 201 | - | 189 | + done | sort -s -k1,1 | cut -f2- |
| 202 | - path=$(sed -n 's/.*(\(.*\))$/\1/p' <<<"$choice") | ||
| 203 | - [[ -d $SYSUSB/$path ]] || { notify "Unknown device" "$choice"; return; } | ||
| 204 | - | ||
| 205 | - local name state actions act | ||
| 206 | - name=$(dev_name "$path") | ||
| 207 | - state=$(dev_authorized "$path") | ||
| 208 | - if [[ $state == 1 ]]; then | ||
| 209 | - if [[ $path == "$(keyboard_path)" ]]; then | ||
| 210 | - actions="🔄 Replug\n⛔ Unplug" | ||
| 211 | - else | ||
| 212 | - actions="⛔ Unplug\n🔄 Replug" | ||
| 213 | - fi | ||
| 214 | - [[ -n $(dev_blocks "$path") ]] && actions="⏏️ Eject (unmount + power off)\n$actions" | ||
| 215 | - else | ||
| 216 | - actions="🔌 Plug\n🔄 Replug" | ||
| 217 | - fi | ||
| 218 | - act=$(printf '%b\n❌ Cancel\n' "$actions" | rofi -dmenu -p "$name" -i) | ||
| 219 | - | ||
| 220 | - case "$act" in | ||
| 221 | - "⏏️ Eject (unmount + power off)") unplug "$path" ;; | ||
| 222 | - "⛔ Unplug") unplug "$path" ;; | ||
| 223 | - "🔌 Plug") plug "$path" ;; | ||
| 224 | - "🔄 Replug") replug "$path" ;; | ||
| 225 | - esac | ||
| 226 | } | 190 | } |
| 227 | 191 | ||
| 228 | # --- entry ------------------------------------------------------------------ | 192 | # --- entry ------------------------------------------------------------------ |
| 229 | 193 | ||
| 230 | -case "${1:-menu}" in | 194 | +case "${1:-}" in |
| 231 | - menu) menu ;; | ||
| 232 | list) list_devices ;; | 195 | list) list_devices ;; |
| 196 | + tsv) list_tsv ;; | ||
| 233 | off) unplug "${2:?usage: $0 off <sysfs-path e.g. 1-5.2>}" ;; | 197 | off) unplug "${2:?usage: $0 off <sysfs-path e.g. 1-5.2>}" ;; |
| 234 | on) plug "${2:?usage: $0 on <sysfs-path>}" ;; | 198 | on) plug "${2:?usage: $0 on <sysfs-path>}" ;; |
| 235 | replug) replug "${2:?usage: $0 replug <sysfs-path>}" ;; | 199 | replug) replug "${2:?usage: $0 replug <sysfs-path>}" ;; |
| 236 | all-on) plug_all ;; | 200 | all-on) plug_all ;; |
| 237 | all-replug) replug_all ;; | 201 | all-replug) replug_all ;; |
| 238 | replug-keyboard|kb) replug_keyboard ;; | 202 | replug-keyboard|kb) replug_keyboard ;; |
| 239 | - waybar) waybar_json ;; | 203 | + *) echo "usage: ${0##*/} [list|tsv|off <path>|on <path>|replug <path>|all-on|all-replug|replug-keyboard]" >&2; exit 1 ;; |
| 240 | - *) echo "usage: ${0##*/} [menu|list|off <path>|on <path>|replug <path>|all-on|all-replug|replug-keyboard|waybar]" >&2; exit 1 ;; | ||
| 241 | esac | 204 | esac |