__init__.py
Python script, ASCII text executable
1import dataclasses 2import os 3import sys 4from pathlib import Path 5from pywayland.client import Display 6from pywayland.protocol.wayland import WlRegistry, WlSeat, WlSurface, WlCompositor 7from pywayland.protocol.wlr_foreign_toplevel_management_unstable_v1 import ( 8ZwlrForeignToplevelManagerV1, 9ZwlrForeignToplevelHandleV1 10) 11import panorama_panel 12 13import gi 14 15gi.require_version("Gtk", "4.0") 16gi.require_version("GdkWayland", "4.0") 17 18from gi.repository import Gtk, GLib, Gtk4LayerShell, Gio, Gdk 19 20 21import ctypes 22from cffi import FFI 23ffi = FFI() 24ffi.cdef(""" 25void * gdk_wayland_display_get_wl_display (void * display); 26void * gdk_wayland_surface_get_wl_surface (void * surface); 27""") 28gtk = ffi.dlopen("libgtk-4.so.1") 29 30 31module_directory = Path(__file__).resolve().parent 32 33 34def split_bytes_into_ints(array: bytes, size: int = 4) -> list[int]: 35if len(array) % size: 36raise ValueError(f"The byte string's length must be a multiple of {size}") 37 38values: list[int] = [] 39for i in range(0, len(array), size): 40values.append(int.from_bytes(array[i : i+size], byteorder=sys.byteorder)) 41 42return values 43 44 45def get_widget_rect(widget: Gtk.Widget) -> tuple[int, int, int, int]: 46width = int(widget.get_width()) 47height = int(widget.get_height()) 48 49toplevel = widget.get_root() 50if not toplevel: 51return None, None, width, height 52 53x, y = widget.translate_coordinates(toplevel, 0, 0) 54x = int(x) 55y = int(y) 56 57return x, y, width, height 58 59 60@dataclasses.dataclass 61class WindowState: 62minimised: bool 63maximised: bool 64fullscreen: bool 65focused: bool 66 67@classmethod 68def from_state_array(cls, array: bytes): 69values = split_bytes_into_ints(array) 70instance = cls(False, False, False, False) 71for value in values: 72match value: 73case 0: 74instance.maximised = True 75case 1: 76instance.minimised = True 77case 2: 78instance.focused = True 79case 3: 80instance.fullscreen = True 81 82return instance 83 84 85class WindowButton(Gtk.ToggleButton): 86def __init__(self, window_id, window_title, **kwargs): 87super().__init__(**kwargs) 88 89self.window_id: ZwlrForeignToplevelHandleV1 = window_id 90self.set_has_frame(False) 91self.label = Gtk.Label() 92self.icon = Gtk.Image.new_from_icon_name("application-x-executable") 93box = Gtk.Box() 94box.append(self.icon) 95box.append(self.label) 96self.set_child(box) 97 98self.window_title = window_title 99self.window_state = WindowState(False, False, False, False) 100 101@property 102def window_title(self): 103return self.label.get_text() 104 105@window_title.setter 106def window_title(self, value): 107self.label.set_text(value) 108 109def set_icon_from_app_id(self, app_id): 110# Try getting an icon from the correct theme 111app_ids = app_id.split() 112icon_theme = Gtk.IconTheme.get_for_display(self.get_display()) 113 114for app_id in app_ids: 115if icon_theme.has_icon(app_id): 116self.icon.set_from_icon_name(app_id) 117return 118 119# If that doesn't work, try getting one from .desktop files 120for app_id in app_ids: 121try: 122desktop_file = Gio.DesktopAppInfo.new(app_id + ".desktop") 123if desktop_file: 124self.icon.set_from_gicon(desktop_file.get_icon()) 125return 126except TypeError: 127# Due to a bug, the constructor may sometimes return C NULL 128pass 129 130 131class WFWindowList(panorama_panel.Applet): 132name = "Wayfire window list" 133description = "Traditional window list (for Wayfire)" 134 135def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 136super().__init__(orientation=orientation, config=config) 137if config is None: 138config = {} 139 140self.toplevel_buttons: dict[ZwlrForeignToplevelHandleV1, WindowButton] = {} 141# This button doesn't belong to any window but is used for the button group and to be 142# selected when no window is focused 143self.initial_button = Gtk.ToggleButton() 144 145self.display = None 146self.wl_surface_ptr = None 147self.registry = None 148self.compositor = None 149self.seat = None 150 151self.context_menu = self.make_context_menu() 152panorama_panel.track_popover(self.context_menu) 153 154right_click_controller = Gtk.GestureClick() 155right_click_controller.set_button(3) 156right_click_controller.connect("pressed", self.show_context_menu) 157 158self.add_controller(right_click_controller) 159 160action_group = Gio.SimpleActionGroup() 161options_action = Gio.SimpleAction.new("options", None) 162options_action.connect("activate", self.show_options) 163action_group.add_action(options_action) 164self.insert_action_group("applet", action_group) 165self.connect("realize", lambda *args: self.get_wl_resources()) 166 167self.options_window = None 168 169def get_wl_resources(self): 170ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p 171ctypes.pythonapi.PyCapsule_GetPointer.argtypes = (ctypes.py_object,) 172 173self.display = Display() 174wl_display_ptr = gtk.gdk_wayland_display_get_wl_display( 175ffi.cast("void *", ctypes.pythonapi.PyCapsule_GetPointer(self.get_root().get_native().get_display().__gpointer__, None))) 176self.display._ptr = wl_display_ptr 177 178#self.display.connect() 179 180self.registry = self.display.get_registry() 181self.registry.dispatcher["global"] = self.on_global 182self.display.roundtrip() 183fd = self.display.get_fd() 184GLib.io_add_watch(fd, GLib.IO_IN, self.on_display_event) 185 186def on_display_event(self, source, condition): 187if condition == GLib.IO_IN: 188self.display.dispatch(block=True) 189return True 190 191def on_global(self, registry, name, interface, version): 192if interface == "zwlr_foreign_toplevel_manager_v1": 193self.print_log("Interface registered") 194self.manager = registry.bind(name, ZwlrForeignToplevelManagerV1, version) 195self.manager.dispatcher["toplevel"] = self.on_new_toplevel 196self.manager.dispatcher["finished"] = lambda *a: print("Toplevel manager finished") 197self.display.roundtrip() 198self.display.flush() 199elif interface == "wl_seat": 200self.print_log("Seat found") 201self.seat = registry.bind(name, WlSeat, version) 202elif interface == "wl_compositor": 203self.compositor = registry.bind(name, WlCompositor, version) 204self.wl_surface_ptr = gtk.gdk_wayland_surface_get_wl_surface( 205ffi.cast("void *", ctypes.pythonapi.PyCapsule_GetPointer( 206self.get_root().get_native().get_surface().__gpointer__, None))) 207 208def on_new_toplevel(self, manager: ZwlrForeignToplevelManagerV1, 209handle: ZwlrForeignToplevelHandleV1): 210handle.dispatcher["title"] = lambda h, title: self.on_title_changed(h, title) 211handle.dispatcher["app_id"] = lambda h, app_id: self.on_app_id_changed(h, app_id) 212handle.dispatcher["state"] = lambda h, states: self.on_state_changed(h, states) 213handle.dispatcher["closed"] = lambda h: self.on_closed(h) 214 215def on_title_changed(self, handle, title): 216if handle not in self.toplevel_buttons: 217button = WindowButton(handle, title) 218button.set_group(self.initial_button) 219button.connect("clicked", self.on_button_click) 220self.toplevel_buttons[handle] = button 221self.append(button) 222else: 223button = self.toplevel_buttons[handle] 224button.window_title = title 225 226def on_button_click(self, button: WindowButton): 227if button.window_state.focused: 228# Already pressed in, so minimise the focused window 229# Set a rectangle for animation 230surface = WlSurface() 231surface._ptr = self.wl_surface_ptr 232button.window_id.set_rectangle(surface, *get_widget_rect(button)) 233button.window_id.set_minimized() 234else: 235button.window_id.unset_minimized() 236button.window_id.activate(self.seat) 237 238self.display.flush() 239 240def on_state_changed(self, handle, states): 241if handle in self.toplevel_buttons: 242state_info = WindowState.from_state_array(states) 243button = self.toplevel_buttons[handle] 244button.window_state = state_info 245if state_info.focused: 246button.set_active(True) 247else: 248self.initial_button.set_active(True) 249 250def on_app_id_changed(self, handle, app_id): 251if handle in self.toplevel_buttons: 252button = self.toplevel_buttons[handle] 253button.set_icon_from_app_id(app_id) 254 255def on_closed(self, handle): 256if handle in self.toplevel_buttons: 257self.remove(self.toplevel_buttons[handle]) 258self.toplevel_buttons.pop(handle) 259 260def make_context_menu(self): 261menu = Gio.Menu() 262menu.append("Window list _options", "applet.options") 263context_menu = Gtk.PopoverMenu.new_from_model(menu) 264context_menu.set_has_arrow(False) 265context_menu.set_parent(self) 266context_menu.set_halign(Gtk.Align.START) 267context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED) 268return context_menu 269 270def show_context_menu(self, gesture, n_presses, x, y): 271rect = Gdk.Rectangle() 272rect.x = int(x) 273rect.y = int(y) 274rect.width = 1 275rect.height = 1 276 277self.context_menu.set_pointing_to(rect) 278self.context_menu.popup() 279 280def show_options(self, _0=None, _1=None): 281pass 282 283def get_config(self): 284return {}