├── LICENSE ├── README.md ├── extension.js └── metadata.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 flexagoon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Focused window D-Bus 2 | 3 | This GNOME Shell extension allows you to get the currently focused window using 4 | a D-Bus call. This allows you to get the focused window on Wayland, where there 5 | is no other way to do this. 6 | 7 | # Installation 8 | 9 | You can install this extension from Gnome Extensions 10 | 11 | https://extensions.gnome.org/extension/5592/focused-window-d-bus 12 | 13 | # Usage 14 | 15 | ```sh 16 | gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/shell/extensions/FocusedWindow --method org.gnome.shell.extensions.FocusedWindow.Get 17 | ``` 18 | 19 | Return format is similar to the `Details` call of the 20 | [Window Calls](https://github.com/ickyicky/window-calls) extensions, because the 21 | code is ~~stolen~~ taken from there, except there's no `frame_bounds` variable 22 | since it caused GNOME Shell to crash for some reason. There's also a `title` 23 | variable. 24 | 25 | Example: 26 | 27 | ```json 28 | { 29 | "title": "Calculator", 30 | "wm_class": "org.gnome.Calculator", 31 | "wm_class_instance": "org.gnome.Calculator", 32 | "pid": 56643, 33 | "id": 2274918779, 34 | "width": 421, 35 | "height": 560, 36 | "x": -22, 37 | "y": -3, 38 | "focus": true, 39 | "in_current_workspace": true, 40 | "moveable": true, 41 | "resizeable": true, 42 | "canclose": true, 43 | "canmaximize": true, 44 | "maximized": 0, 45 | "canminimize": true, 46 | "canshade": false, 47 | "display": {}, 48 | "frame_type": 0, 49 | "window_type": 0, 50 | "layer": 2, 51 | "monitor": 0, 52 | "role": null, 53 | "area": {}, 54 | "area_all": {}, 55 | "area_cust": {} 56 | } 57 | ``` 58 | -------------------------------------------------------------------------------- /extension.js: -------------------------------------------------------------------------------- 1 | import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js'; 2 | import Gio from 'gi://Gio'; 3 | 4 | const DBUS_SCHEMA = ` 5 | 6 | 7 | 8 | 9 | 10 | 11 | `; 12 | 13 | export default class FocusedWindowDbus extends Extension { 14 | Get() { 15 | let window_list = global.get_window_actors(); 16 | let focusedWindow = window_list.find((window) => 17 | window.meta_window.has_focus() 18 | ); 19 | 20 | // Stolen from the "Window Calls" extension 21 | // https://github.com/ickyicky/window-calls/blob/b5ac7abe7e7acfcced75c5139824bd434d7c8f89/extension.js#L129 22 | let workspaceManager = global.workspace_manager; 23 | let currentmonitor = global.display.get_current_monitor(); 24 | if (focusedWindow) { 25 | return JSON.stringify({ 26 | title: focusedWindow.meta_window.get_title(), 27 | wm_class: focusedWindow.meta_window.get_wm_class(), 28 | wm_class_instance: focusedWindow.meta_window.get_wm_class_instance(), 29 | pid: focusedWindow.meta_window.get_pid(), 30 | id: focusedWindow.meta_window.get_id(), 31 | width: focusedWindow.get_width(), 32 | height: focusedWindow.get_height(), 33 | x: focusedWindow.get_x(), 34 | y: focusedWindow.get_y(), 35 | focus: focusedWindow.meta_window.has_focus(), 36 | in_current_workspace: focusedWindow.meta_window.located_on_workspace( 37 | workspaceManager.get_active_workspace() 38 | ), 39 | moveable: focusedWindow.meta_window.allows_move(), 40 | resizeable: focusedWindow.meta_window.allows_resize(), 41 | canclose: focusedWindow.meta_window.can_close(), 42 | canmaximize: focusedWindow.meta_window.can_maximize(), 43 | maximized: focusedWindow.meta_window.get_maximized(), 44 | canminimize: focusedWindow.meta_window.can_minimize(), 45 | display: focusedWindow.meta_window.get_display(), 46 | frame_type: focusedWindow.meta_window.get_frame_type(), 47 | window_type: focusedWindow.meta_window.get_window_type(), 48 | layer: focusedWindow.meta_window.get_layer(), 49 | monitor: focusedWindow.meta_window.get_monitor(), 50 | role: focusedWindow.meta_window.get_role(), 51 | area: focusedWindow.meta_window.get_work_area_current_monitor(), 52 | area_all: focusedWindow.meta_window.get_work_area_all_monitors(), 53 | area_cust: 54 | focusedWindow.meta_window.get_work_area_for_monitor(currentmonitor), 55 | }); 56 | } else { 57 | return "{}"; 58 | } 59 | } 60 | 61 | enable() { 62 | this._dbus = Gio.DBusExportedObject.wrapJSObject(DBUS_SCHEMA, this); 63 | this._dbus.export( 64 | Gio.DBus.session, 65 | "/org/gnome/shell/extensions/FocusedWindow" 66 | ); 67 | } 68 | 69 | disable() { 70 | this._dbus.flush(); 71 | this._dbus.unexport(); 72 | delete this._dbus; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Focused Window D-Bus", 3 | "description": "Exposes a D-Bus method to get active window title and class", 4 | "uuid": "focused-window-dbus@flexagoon.com", 5 | "version": 8, 6 | "url": "https://github.com/flexagoon/focused-window-dbus", 7 | "shell-version": ["45", "46", "47", "48"] 8 | } 9 | --------------------------------------------------------------------------------