From 3598f3f7515dca0e8193e58c0f3a35793d43d1f0 Mon Sep 17 00:00:00 2001 From: denis smpio-ubuntu Date: Sun, 8 Nov 2026 14:49:06 +0100 Subject: [PATCH] 1.0.0-alpha.2: global hotkeys (grabAccelerator, ungrabAccelerator, accelerator event) Mutter accelerator grabs owned by the current client of com.giglabo.SMBAct: released when it goes away or changes and on disable; NORMAL | OVERVIEW action modes (never the lock screen). A freshly enabled extension asks the bus for the client's owner when its watcher has not reported yet (GNOME re-enables extensions after every unlock and SMBAct grabs again right away). scripts/call.py --grab holds a hotkey and prints its presses. Same API 1; the new ops are listed in hello. Co-Authored-By: Claude Opus 5.5 --- PROTOCOL.md | 20 ++++- gnome-shell/smbact@giglabo.com/extension.js | 88 ++++++++++++++++++++ gnome-shell/smbact@giglabo.com/metadata.json | 6 +- scripts/call.py | 31 +++++++ 4 files changed, 140 insertions(+), 5 deletions(-) diff --git a/PROTOCOL.md b/PROTOCOL.md index 5917ff5..fe3f2e9 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -32,8 +32,8 @@ is not running; GNOME's own allow-lists have the same limit. { "ok": false, "error": { "code": "NO_WINDOW", "message": "no window 2653220665" } } ``` -Error codes: `BAD_REQUEST` (not JSON, missing or wrong arguments), `UNKNOWN_OP`, `NO_WINDOW`, `FAILED` (the shell -threw; `message` has its text). +Error codes: `BAD_REQUEST` (not JSON, missing or wrong arguments), `UNKNOWN_OP`, `NO_WINDOW`, `GRAB_FAILED` +(`grabAccelerator`: the shell refused the accelerator), `FAILED` (the shell threw; `message` has its text). Unknown fields in a request are ignored; new optional fields and new operations do not change the API number. A change that breaks an existing operation does, and `hello` reports which API the plugin speaks. @@ -104,6 +104,8 @@ XWayland. Rectangles are `{ "x", "y", "width", "height" }`. | `displays` | | `{ monitors: [{ index, geometry, workArea, scale, primary }] }` | | `iconPng` | `path` (an `icon` path), `size?` (px, default 48) | `{ png, width, height }` (`png`: base64 PNG bytes) | | `pointer` | | `{ x, y }` | +| `grabAccelerator` | `accelerator` (GTK notation, `x`) | `{ accelerator, action }` | +| `ungrabAccelerator` | `accelerator` (as grabbed) | `{ released }` | - `setState` states: `minimized`, `maximized`, `maximizedHorizontally`, `maximizedVertically`, `fullscreen`, `above`. @@ -116,6 +118,16 @@ XWayland. Rectangles are `{ "x", "y", "width", "height" }`. with `getWindow` (or wait for `windowsChanged`) when the new one matters. - `activate` uses the server's current time unless `time` is given (an input event's timestamp). - `close` asks the window to close; `force` kills its client. +- `grabAccelerator` makes the accelerator a global hotkey, whatever window has the focus, the way GNOME grabs its + custom shortcuts: it fires on the desktop and in the overview, never on the lock or login screen. Each press sends + an `accelerator` event. It fails with `GRAB_FAILED` when another shortcut (GNOME's own, a custom one, another + extension's) has the combination, or when Mutter cannot parse it. Grabbing the same string again is a no-op. +- Grabs belong to the client: the plugin releases all of them when the owner of `com.giglabo.SMBAct` goes away or + changes, and when it is disabled. A client that starts again grabs again. +- GNOME disables every user extension while the screen is locked and enables them again after the unlock (seen on + GNOME 50), so the plugin's bus name goes away and comes back without any grabs. Clients watch + `com.giglabo.SMBAct.Shell` (`NameOwnerChanged`) and grab again when it reappears. The hotkeys do not fire on the + lock screen anyway. - `showDesktop` with `show: true` minimizes the visible windows of the active workspace and remembers them; `false` restores those. @@ -125,7 +137,11 @@ XWayland. Rectangles are `{ "x", "y", "width", "height" }`. - `windowsChanged`: a window appeared, went away, moved, resized or changed state (coalesced, at most every 100 ms). - `focusChanged` with `id` (or `null`). +- `accelerator` with `accelerator` (the string given to `grabAccelerator`) and `time` (the key press's timestamp, + usable as `activate`'s `time`). ## History - API 1 (2026-11): window list, geometry, activation, states, close, displays, pointer, events. +- API 1, plugin 1.0.0-alpha.2 (2026-11): `grabAccelerator`, `ungrabAccelerator`, the `accelerator` event (new + operations, same API; clients look for them in `hello`'s `ops`). diff --git a/gnome-shell/smbact@giglabo.com/extension.js b/gnome-shell/smbact@giglabo.com/extension.js index 0b09119..c502073 100644 --- a/gnome-shell/smbact@giglabo.com/extension.js +++ b/gnome-shell/smbact@giglabo.com/extension.js @@ -4,6 +4,7 @@ // the app. Rules it follows (all seen on GNOME 50): // - windows come from display.list_all_windows(), not global.get_window_actors() (other extensions patch that one); // - activation uses the server's current time (time 0 may not get the focus); +// - global hotkeys are Mutter accelerator grabs owned by the current client: released when it goes away; // - no synchronous work beyond one frame, every request in try/catch, everything undone in disable(): a hang // freezes the desktop and a crash ends the session. import GdkPixbuf from 'gi://GdkPixbuf'; @@ -23,6 +24,11 @@ const OBJECT_PATH = '/com/giglabo/SMBAct/Shell1'; /** Only the current owner of this name may call (the SMBAct app requests it). */ const CLIENT_NAME = 'com.giglabo.SMBAct'; const EVENT_COALESCE_MS = 100; +/** + * Where SMBAct's hotkeys fire: the desktop and the overview, like GNOME's custom shortcuts; never on the lock or + * login screen or behind a system dialog. + */ +const HOTKEY_ACTION_MODE = Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW; const IFACE_XML = ` @@ -117,6 +123,9 @@ class Service { this._windowSignals = new Map(); this._displaySignals = []; this._eventTimer = 0; + /** accelerator (as the client sent it) -> Mutter action id; action id -> accelerator */ + this._grabs = new Map(); + this._grabActions = new Map(); this._ops = { hello: () => this._hello(), listWindows: req => this._listWindows(req), @@ -138,6 +147,8 @@ class Service { const [x, y] = global.get_pointer(); return {x, y}; }, + grabAccelerator: req => this._grabAccelerator(req), + ungrabAccelerator: req => this._ungrabAccelerator(req), }; } @@ -147,9 +158,13 @@ class Service { this._nameId = Gio.bus_own_name_on_connection(Gio.DBus.session, BUS_NAME, Gio.BusNameOwnerFlags.NONE, null, null); this._watchId = Gio.bus_watch_name_on_connection(Gio.DBus.session, CLIENT_NAME, Gio.BusNameWatcherFlags.NONE, (_c, _name, owner) => { + // grabs belong to the client that made them: a new owner starts without any + if (this._clientOwner !== owner) + this._releaseGrabs(); this._clientOwner = owner; }, () => { + this._releaseGrabs(); this._clientOwner = null; }); const d = global.display; @@ -160,12 +175,14 @@ class Service { }), d.connect('restacked', () => this._windowsChanged()), d.connect('notify::focus-window', () => this._emit({type: 'focusChanged', id: d.focus_window?.get_id() ?? null})), + d.connect('accelerator-activated', (_d, action, ...rest) => this._acceleratorActivated(action, rest[rest.length - 1])), ]; for (const w of allWindows()) this._watchWindow(w); } stop() { + this._releaseGrabs(); for (const id of this._displaySignals) global.display.disconnect(id); this._displaySignals = []; @@ -195,6 +212,27 @@ class Service { // ---- D-Bus CallAsync([request], invocation) { + if (this._clientOwner) { + this._answer(request, invocation); + return; + } + // Just enabled (GNOME re-enables extensions after every unlock): the name watcher may not have reported the + // client yet, and SMBAct grabs its hotkeys again right away. Ask the bus instead of refusing. + Gio.DBus.session.call('org.freedesktop.DBus', '/org/freedesktop/DBus', 'org.freedesktop.DBus', 'GetNameOwner', + new GLib.Variant('(s)', [CLIENT_NAME]), new GLib.VariantType('(s)'), Gio.DBusCallFlags.NONE, 1000, null, + (connection, result) => { + let owner = null; + try { + [owner] = connection.call_finish(result).deepUnpack(); + } catch { + // no owner + } + this._clientOwner ??= owner; + this._answer(request, invocation); + }); + } + + _answer(request, invocation) { if (!this._clientOwner || invocation.get_sender() !== this._clientOwner) { invocation.return_dbus_error('org.freedesktop.DBus.Error.AccessDenied', 'Call is not allowed'); return; @@ -252,6 +290,56 @@ class Service { this._windowSignals.set(w, ids); } + // ---- hotkeys (Mutter accelerator grabs, the way GNOME grabs custom shortcuts for gnome-settings-daemon) + + _acceleratorActivated(action, time) { + const accelerator = this._grabActions.get(action); + if (accelerator === undefined) + return; // another grab (gnome-settings-daemon's) + this._emit({type: 'accelerator', accelerator, time: typeof time === 'number' ? time : 0}); + } + + _grabAccelerator(req) { + const accelerator = req.accelerator; + if (typeof accelerator !== 'string' || !accelerator) + throw badRequest('"accelerator" must be a GTK accelerator such as "x"'); + const existing = this._grabs.get(accelerator); + if (existing !== undefined) + return {accelerator, action: existing}; + const action = global.display.grab_accelerator(accelerator, Meta.KeyBindingFlags.NONE); + if (action === Meta.KeyBindingAction.NONE) { + throw new ShellError('GRAB_FAILED', + `GNOME Shell did not grab "${accelerator}": another shortcut has it, or it is not a valid accelerator`); + } + Main.wm.allowKeybinding(Meta.external_binding_name_for_action(action), HOTKEY_ACTION_MODE); + this._grabs.set(accelerator, action); + this._grabActions.set(action, accelerator); + return {accelerator, action}; + } + + _ungrabAccelerator(req) { + const accelerator = req.accelerator; + if (typeof accelerator !== 'string' || !accelerator) + throw badRequest('"accelerator" must be a string'); + const action = this._grabs.get(accelerator); + if (action === undefined) + return {released: false}; + this._ungrab(accelerator, action); + return {released: true}; + } + + _ungrab(accelerator, action) { + this._grabs.delete(accelerator); + this._grabActions.delete(action); + tryOr(() => Main.wm.allowKeybinding(Meta.external_binding_name_for_action(action), Shell.ActionMode.NONE)); + tryOr(() => global.display.ungrab_accelerator(action)); + } + + _releaseGrabs() { + for (const [accelerator, action] of [...this._grabs]) + this._ungrab(accelerator, action); + } + // ---- operations _hello() { diff --git a/gnome-shell/smbact@giglabo.com/metadata.json b/gnome-shell/smbact@giglabo.com/metadata.json index bea5e2c..126b1c6 100644 --- a/gnome-shell/smbact@giglabo.com/metadata.json +++ b/gnome-shell/smbact@giglabo.com/metadata.json @@ -1,9 +1,9 @@ { "uuid": "smbact@giglabo.com", "name": "SMBAct", - "description": "Lets the SMBAct app see and arrange every window on Wayland: the window list, moving and resizing, activation and window states, over D-Bus. It does nothing on its own; SMBAct installs and uses it.", + "description": "Lets the SMBAct app see and arrange every window on Wayland: the window list, moving and resizing, activation, window states and global hotkeys, over D-Bus. It does nothing on its own; SMBAct installs and uses it.", "shell-version": ["46", "47", "48", "49", "50", "51"], "url": "https://github.com/GigLaboCom/smbact-shell", - "version": 1, - "version-name": "1.0.0-alpha.1" + "version": 2, + "version-name": "1.0.0-alpha.2" } diff --git a/scripts/call.py b/scripts/call.py index fe8c1d7..9a23754 100755 --- a/scripts/call.py +++ b/scripts/call.py @@ -8,6 +8,7 @@ scripts/call.py listWindows '{"icons": true}' scripts/call.py moveResize '{"id": 123, "x": 100, "y": 100, "width": 800, "height": 600}' scripts/call.py --events 10 # print Event signals for 10 seconds + scripts/call.py --grab 'j' 30 # hold a hotkey for 30 seconds, print its presses scripts/call.py --as-stranger hello # without the name: expect AccessDenied """ import json @@ -25,6 +26,8 @@ def main(argv): if not argv: print(__doc__) return 2 + if argv[0] == "--grab": + return grab(argv[1], int(argv[2]) if len(argv) > 2 else 30) if argv[0] == "--events": from gi.repository import GLib dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) @@ -51,5 +54,33 @@ def main(argv): return 0 +def grab(accelerator, seconds): + """Grabs belong to the owner of com.giglabo.SMBAct: keep the name (and the grab) while listening.""" + from gi.repository import GLib + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + bus = dbus.SessionBus() + got = bus.request_name(CLIENT, dbus.bus.NAME_FLAG_DO_NOT_QUEUE) + if got not in (dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER, dbus.bus.REQUEST_NAME_REPLY_ALREADY_OWNER): + print(f"cannot own {CLIENT} (is SMBAct running?)", file=sys.stderr) + return 1 + call = bus.get_object(DEST, PATH).get_dbus_method("Call", IFACE) + reply = json.loads(call(json.dumps({"op": "grabAccelerator", "accelerator": accelerator}))) + print(json.dumps(reply), flush=True) + if not reply.get("ok"): + return 1 + + def on_event(e): + event = json.loads(e) + if event.get("type") == "accelerator": + print(json.dumps(event), flush=True) + + bus.add_signal_receiver(on_event, "Event", IFACE, None, PATH) + loop = GLib.MainLoop() + GLib.timeout_add_seconds(seconds, loop.quit) + loop.run() + print(call(json.dumps({"op": "ungrabAccelerator", "accelerator": accelerator})), flush=True) + return 0 + + if __name__ == "__main__": sys.exit(main(sys.argv[1:]))