Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, `<Control><Alt>x`) | `{ accelerator, action }` |
| `ungrabAccelerator` | `accelerator` (as grabbed) | `{ released }` |

- `setState` states: `minimized`, `maximized`, `maximizedHorizontally`, `maximizedVertically`, `fullscreen`,
`above`.
Expand All @@ -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.

Expand All @@ -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`).
88 changes: 88 additions & 0 deletions gnome-shell/smbact@giglabo.com/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = `
<node>
Expand Down Expand Up @@ -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),
Expand All @@ -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),
};
}

Expand All @@ -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;
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 "<Control><Alt>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() {
Expand Down
6 changes: 3 additions & 3 deletions gnome-shell/smbact@giglabo.com/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
31 changes: 31 additions & 0 deletions scripts/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<Control><Alt>j' 30 # hold a hotkey for 30 seconds, print its presses
scripts/call.py --as-stranger hello # without the name: expect AccessDenied
"""
import json
Expand All @@ -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)
Expand All @@ -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:]))
Loading