From 363097c33170b46c3f57445ced0ea52fee752b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 27 Oct 2026 18:39:40 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(noctalia):=20replace=20rofi=20?= =?UTF-8?q?monitor=20and=20USB=20menus=20with=20launcher=20plugins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/niri/config.kdl | 4 +- .../noctalia/plugins/monitors/monitors.luau | 77 ++++++++ .config/noctalia/plugins/monitors/plugin.toml | 17 ++ .config/noctalia/plugins/usb/plugin.toml | 17 ++ .config/noctalia/plugins/usb/usb.luau | 176 ++++++++++++++++++ .config/scripts/niri/toggle_monitors.sh | 85 ++++++--- .config/scripts/system/usb_menu.sh | 97 +++------- 7 files changed, 378 insertions(+), 95 deletions(-) create mode 100644 .config/noctalia/plugins/monitors/monitors.luau create mode 100644 .config/noctalia/plugins/monitors/plugin.toml create mode 100644 .config/noctalia/plugins/usb/plugin.toml create mode 100644 .config/noctalia/plugins/usb/usb.luau diff --git a/.config/niri/config.kdl b/.config/niri/config.kdl index 3a136ac..681647a 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -207,8 +207,8 @@ binds { Mod+X hotkey-overlay-title="Show desktop" { spawn-sh "~/.config/scripts/niri/show_desktop.sh"; } Mod+H hotkey-overlay-title="Toggle bar" { spawn-sh "noctalia msg bar-toggle"; } Mod+B hotkey-overlay-title="Noctalia: wallpaper picker" { spawn-sh "noctalia msg panel-toggle wallpaper"; } - 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+Shift+M hotkey-overlay-title="Noctalia: monitors menu" { spawn-sh "noctalia msg panel-toggle launcher /monitors"; } + Mod+Shift+K hotkey-overlay-title="Noctalia: USB plug/unplug menu" { spawn-sh "noctalia msg panel-toggle launcher /usb"; } Mod+Ctrl+M hotkey-overlay-title="Wake monitors" { power-on-monitors; } Alt+4 repeat=false { close-window; } diff --git a/.config/noctalia/plugins/monitors/monitors.luau b/.config/noctalia/plugins/monitors/monitors.luau new file mode 100644 index 0000000..19e5f15 --- /dev/null +++ b/.config/noctalia/plugins/monitors/monitors.luau @@ -0,0 +1,77 @@ +--!nonstrict +-- Monitors launcher provider. +-- +-- Type `/monitors` in the launcher to list monitor actions built from the live +-- niri outputs; Enter runs the action. Listing and actions live in +-- ~/.config/scripts/niri/toggle_monitors.sh (`list` and `run ` modes). + +local SCRIPT = "$HOME/.config/scripts/niri/toggle_monitors.sh" + +-- nil until the first list arrives; rescanned whenever the menu opens (empty query) +-- so outputs that appeared or changed show up, but not on every keystroke. +local monitors = nil +local lastQuery = "" + +local function parse(stdout) + local list = {} + for line in stdout:gmatch("[^\n]+") do + local action, title, glyph = line:match("^([^\t]*)\t([^\t]*)\t(.+)$") + if action and action ~= "" then + table.insert(list, { action = action, title = title, glyph = glyph }) + end + end + return list +end + +local function publish(query) + if not monitors then + launcher.setResults(query, { + { id = "", title = "Loading monitors…", glyph = "loader" }, + }) + return + end + + local text = noctalia.string.trim(query) + local rows = {} + for _, m in monitors do + local score = 0 + if text ~= "" then + score = noctalia.fuzzyScore(text, m.title) + end + if score then + table.insert(rows, { + id = m.action, + title = m.title, + glyph = m.glyph, + score = score, + }) + end + end + + if #rows == 0 then + rows[1] = { id = "", title = "No matches", glyph = "search" } + end + launcher.setResults(query, rows) +end + +function onQuery(query) + lastQuery = query + publish(query) + if monitors and noctalia.string.trim(query) ~= "" then + return + end + noctalia.runAsync(SCRIPT .. " list", function(result) + if result.exitCode == 0 then + monitors = parse(result.stdout) + publish(lastQuery) + end + end) +end + +function onActivate(id) + -- ids are our own actions (all-off, all-on, off:DP-1, on:HDMI-A-1); refuse anything else + if not id:match("^[%w:%-_.]+$") then + return + end + noctalia.runAsync(SCRIPT .. " run '" .. id .. "'") +end diff --git a/.config/noctalia/plugins/monitors/plugin.toml b/.config/noctalia/plugins/monitors/plugin.toml new file mode 100644 index 0000000..a157b37 --- /dev/null +++ b/.config/noctalia/plugins/monitors/plugin.toml @@ -0,0 +1,17 @@ +id = "tahinli/monitors" +name = "Monitors" +version = "1.0.0" +plugin_api = 3 +author = "tahinli" +license = "MIT" +dependencies = [] +tags = ["launcher", "niri", "monitors"] +icon = "device-desktop" +description = "Turn niri monitors on or off from the launcher (replaces the rofi monitors menu)." + +[[launcher_provider]] +id = "monitors" +entry = "monitors.luau" +prefix = "monitors" +glyph = "device-desktop" +include_in_global_search = false diff --git a/.config/noctalia/plugins/usb/plugin.toml b/.config/noctalia/plugins/usb/plugin.toml new file mode 100644 index 0000000..cc0e73d --- /dev/null +++ b/.config/noctalia/plugins/usb/plugin.toml @@ -0,0 +1,17 @@ +id = "tahinli/usb" +name = "USB" +version = "1.0.0" +plugin_api = 3 +author = "tahinli" +license = "MIT" +dependencies = [] +tags = ["launcher", "usb"] +icon = "usb" +description = "Plug, unplug, eject and replug USB devices (usb_menu.sh)." + +[[launcher_provider]] +id = "usb" +entry = "usb.luau" +prefix = "usb" +glyph = "usb" +include_in_global_search = false diff --git a/.config/noctalia/plugins/usb/usb.luau b/.config/noctalia/plugins/usb/usb.luau new file mode 100644 index 0000000..bf360f5 --- /dev/null +++ b/.config/noctalia/plugins/usb/usb.luau @@ -0,0 +1,176 @@ +--!nonstrict +-- USB launcher provider (replaces the rofi USB menu). +-- +-- Type `/usb` in the launcher to list USB devices. Enter on a device opens its +-- actions (Plug / Unplug / Replug, Eject for storage); "Replug all" and "Plug all" +-- run on every device. Discovery and every action live in +-- ~/.config/scripts/system/usb_menu.sh. + +local SCRIPT = "$HOME/.config/scripts/system/usb_menu.sh" + +-- device list from `usb_menu.sh tsv`; refreshed on an empty query and when a +-- device level is entered. byPath indexes the same entries by sysfs path. +local devices = {} +local byPath = {} +local loaded = false +local lastQuery = "" + +local function trim(s) + return noctalia.string.trim(s) +end + +-- lines: path \t authorized \t name \t extra (extra may be empty) +local function parse(stdout) + local list = {} + local index = {} + for line in stdout:gmatch("[^\n]+") do + local path, auth, rest = line:match("^([^\t]*)\t([^\t]*)\t(.*)$") + if path and path ~= "" then + local name, extra = rest:match("^([^\t]*)\t?(.*)$") + local dev = { + path = path, + plugged = auth == "1", + name = name, + extra = extra, + keyboard = extra:find("keyboard", 1, true) ~= nil, + block = extra:find("[sd", 1, true) ~= nil or extra:find("[nvme", 1, true) ~= nil, + } + table.insert(list, dev) + index[path] = dev + end + end + return list, index +end + +local function refresh(done) + local started = noctalia.runAsync(SCRIPT .. " tsv", function(result) + if result.exitCode == 0 then + devices, byPath = parse(result.stdout) + loaded = true + end + done() + end) + if not started then + done() + end +end + +local function publishRoot(query) + if not loaded then + launcher.setResults(query, { + { id = "", title = "Loading devices…", glyph = "loader" }, + }) + return + end + + local text = trim(query) + local rows = {} + for _, dev in devices do + local score = 0 + if text ~= "" then + score = noctalia.fuzzyScore(text, dev.name .. " " .. dev.path) + end + if score then + local sub = dev.path + if dev.extra ~= "" then + sub = dev.path .. " " .. dev.extra + end + table.insert(rows, { + id = dev.path, + title = dev.name, + subtitle = sub, + glyph = if dev.keyboard then "keyboard" elseif dev.plugged then "plug" else "plug-off", + score = score, + query = dev.path, -- opens this device's action level + }) + end + end + + local globals = { + { id = "all-replug", title = "Replug all", glyph = "refresh" }, + { id = "all-on", title = "Plug all", glyph = "plug" }, + } + for _, g in globals do + local score = 0 + if text ~= "" then + score = noctalia.fuzzyScore(text, g.title) + end + if score then + table.insert(rows, { id = g.id, title = g.title, glyph = g.glyph, score = score }) + end + end + + if #rows == 0 then + rows[1] = { id = "", title = "No matches", glyph = "search" } + end + launcher.setResults(query, rows) +end + +local function publishDevice(query, dev) + local p = dev.path + local rows = {} + local function add(id, title, glyph) + table.insert(rows, { id = id, title = title, subtitle = dev.name, glyph = glyph }) + end + + if dev.plugged then + if dev.keyboard then + add("replug:" .. p, "Replug", "refresh") + end + if dev.block then + add("off:" .. p, "Eject (unmount + power off)", "eject") + end + add("off:" .. p, "Unplug", "plug-off") + if not dev.keyboard then + add("replug:" .. p, "Replug", "refresh") + end + else + add("on:" .. p, "Plug", "plug") + add("replug:" .. p, "Replug", "refresh") + end + + table.insert(rows, { id = "back", title = "Back", glyph = "arrow-left", query = "" }) + launcher.setResults(query, rows) +end + +-- pick the level from the current query: exact known device path -> actions +local function render(query) + local dev = byPath[trim(query)] + if dev then + publishDevice(query, dev) + else + publishRoot(query) + end +end + +function onQuery(query) + lastQuery = query + local text = trim(query) + render(query) + if text == "" or byPath[text] then + refresh(function() + render(lastQuery) + end) + end +end + +function onActivate(id) + -- ids are built by this plugin; refuse anything outside the plain token alphabet + if not id:match("^[%w%-:.]+$") then + return + end + + local cmd = nil + if id == "all-replug" or id == "all-on" then + cmd = id + else + local action, path = id:match("^(%a+):([%w%-%.]+)$") + if action == "off" or action == "on" or action == "replug" then + cmd = action .. " " .. path + end + end + + if cmd then + noctalia.runAsync(SCRIPT .. " " .. cmd) + end +end diff --git a/.config/scripts/niri/toggle_monitors.sh b/.config/scripts/niri/toggle_monitors.sh index 3cffaf0..e5e9ca6 100755 --- a/.config/scripts/niri/toggle_monitors.sh +++ b/.config/scripts/niri/toggle_monitors.sh @@ -2,36 +2,69 @@ source "$(dirname "$0")/monitors.sh" -case "${1:-menu}" in - menu) - 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) +usage() { + echo "usage: $(basename "$0") list | run | on" + echo " list TSV rows (action, title, glyph) for the noctalia monitors plugin" + echo " run all-off | all-on | off:NAME | on:NAME" + echo " on turn LEFT_MONITOR and RIGHT_MONITOR on" +} - case "$menu" in - "πŸ–₯️ All Monitors Off") - niri msg action power-off-monitors - ;; - "πŸ–₯️ $LEFT_MONITOR Off") - niri msg output "$LEFT_MONITOR" off - ;; - "πŸ–₯️ $RIGHT_MONITOR Off") - niri msg output "$RIGHT_MONITOR" off - ;; - "πŸ’‘ All Monitors On") - niri msg output "$LEFT_MONITOR" on - niri msg output "$RIGHT_MONITOR" on - ;; - "πŸ’‘ $LEFT_MONITOR On") - niri msg output "$LEFT_MONITOR" on - ;; - "πŸ’‘ $RIGHT_MONITOR On") - niri msg output "$RIGHT_MONITOR" on - ;; - "❌ Cancel") - ;; - esac +list_rows() { + local json + json=$(niri msg -j outputs) || return 1 + printf 'all-off\tAll monitors off\tdevice-desktop-off\n' + printf 'all-on\tAll monitors on\tbulb\n' + jq -r ' + .[] | . as $o + | if $o.current_mode != null then + "off:\($o.name)\t\($o.name) off (\($o.make) \($o.model))\tdevice-desktop-off" + else + "on:\($o.name)\t\($o.name) on (\($o.make) \($o.model))\tdevice-desktop" + end' <<< "$json" +} + +run_action() { + local action="$1" json name + local -a names + json=$(niri msg -j outputs) || return 1 + case "$action" in + all-off) + niri msg action power-off-monitors + ;; + all-on) + mapfile -t names < <(jq -r 'keys[]' <<< "$json") + for name in "${names[@]}"; do + niri msg output "$name" on + done + ;; + off:*|on:*) + name="${action#*:}" + if ! jq -e --arg n "$name" 'has($n)' <<< "$json" > /dev/null; then + echo "unknown output: $name" >&2 + return 1 + fi + niri msg output "$name" "${action%%:*}" + ;; + *) + echo "unknown action: $action" >&2 + return 1 + ;; + esac +} + +case "${1:-}" in + list) + list_rows + ;; + run) + run_action "${2:-}" ;; on) niri msg output "$LEFT_MONITOR" on niri msg output "$RIGHT_MONITOR" on ;; + *) + usage + exit 1 + ;; esac diff --git a/.config/scripts/system/usb_menu.sh b/.config/scripts/system/usb_menu.sh index a90d301..742918d 100755 --- a/.config/scripts/system/usb_menu.sh +++ b/.config/scripts/system/usb_menu.sh @@ -1,8 +1,9 @@ #!/bin/bash -# USB soft plug/unplug menu (rofi), compositor-agnostic. +# USB soft plug/unplug helper, 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). +# Front end: the noctalia "usb" launcher plugin (tsv mode) and direct CLI calls. set -u @@ -51,6 +52,17 @@ dev_blocks() { is_hub() { [[ $(cat "$SYSUSB/$1"/*:*/bInterfaceClass 2>/dev/null | head -1) == "09" ]]; } +# info tags for one device, leading space included: " [sdb] [hub] [⌨️ keyboard]" +# args: path keyboard_path +dev_extra() { + local p=$1 kb=$2 blocks extra="" + blocks=$(dev_blocks "$p" | tr '\n' ' ') + [[ -n $blocks ]] && extra=" [${blocks% }]" + is_hub "$p" && extra="$extra [hub]" + [[ $p == "$kb" ]] && extra="$extra [⌨️ keyboard]" + echo "$extra" +} + # 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() { @@ -150,92 +162,43 @@ plug_all() { 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 ------------------------------------------------------------------ +# --- listings --------------------------------------------------------------- list_devices() { - local p state icon blocks extra kb line + local p state icon 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") + line=$(printf '%s %s%s (%s)' "$icon" "$(dev_name "$p")" "$(dev_extra "$p" "$kb")" "$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 +# one line per device: pathauthorized(0/1)nameextra +# keyboard first; extra is dev_extra without its leading space (may be empty) +list_tsv() { + local p state kb + kb=$(keyboard_path) + for p in $(usb_paths); do + state=$(dev_authorized "$p") + [[ $state == 1 ]] || state=0 + [[ $p == "$kb" ]] && printf '0\t' || printf '1\t' + printf '%s\t%s\t%s\t%s\n' "$p" "$state" "$(dev_name "$p")" "$(dev_extra "$p" "$kb" | sed 's/^ //')" + done | sort -s -k1,1 | cut -f2- } # --- entry ------------------------------------------------------------------ -case "${1:-menu}" in - menu) menu ;; +case "${1:-}" in list) list_devices ;; + tsv) list_tsv ;; 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 ;; + *) echo "usage: ${0##*/} [list|tsv|off |on |replug |all-on|all-replug|replug-keyboard]" >&2; exit 1 ;; esac