By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

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