|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +# If gi.override.Gdk has been imported, the GDK |
| 6 | +# backend has already been set and it is too late |
| 7 | +# to override it. |
| 8 | +assert ( |
| 9 | + "gi.override.Gdk" not in sys.modules |
| 10 | +), "must import this module before loading GDK" |
| 11 | + |
| 12 | +# Modifying the environment while multiple threads |
| 13 | +# are running leads to use-after-free in glibc, so |
| 14 | +# ensure that only one thread is running. |
| 15 | +assert ( |
| 16 | + len(os.listdir("/proc/self/task")) == 1 |
| 17 | +), "multiple threads already running" |
| 18 | + |
| 19 | +# Only the X11 backend is supported |
| 20 | +os.environ["GDK_BACKEND"] = "x11" |
| 21 | + |
| 22 | +import gi |
| 23 | + |
| 24 | +gi.require_version("Gdk", "3.0") |
| 25 | +gi.require_version("Gtk", "3.0") |
| 26 | +from gi.repository import Gtk, Gdk |
| 27 | + |
| 28 | + |
| 29 | +is_xwayland = "WAYLAND_DISPLAY" in os.environ |
| 30 | + |
| 31 | + |
| 32 | +class X11FullscreenWindowHack: |
| 33 | + """ |
| 34 | + No-op implementation of the hack, for use on stock X11. |
| 35 | + """ |
| 36 | + |
| 37 | + def clear_widget(self) -> None: |
| 38 | + pass |
| 39 | + |
| 40 | + def show_for_widget(self, _widget: Gtk.Widget, /) -> None: |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +class X11FullscreenWindowHackXWayland(X11FullscreenWindowHack): |
| 45 | + """ |
| 46 | + GTK3 menus have a bug under Xwayland: if the user clicks on a native |
| 47 | + Wayland surface, the menu is not dismissed. This class works around |
| 48 | + the problem by using a fullscreen transparent override-redirect |
| 49 | + window. This is a horrible hack because if the application freezes, |
| 50 | + the user won't be able to click on any other applications. That's |
| 51 | + no worse than under native X11, though. |
| 52 | + """ |
| 53 | + |
| 54 | + _window: Gtk.Window |
| 55 | + _widget: Optional[Gtk.Widget] |
| 56 | + _unmap_signal_id: int |
| 57 | + _map_signal_id: int |
| 58 | + |
| 59 | + def __init__(self) -> None: |
| 60 | + self._widget = None |
| 61 | + # Get the default GDK screen. |
| 62 | + screen = Gdk.Screen.get_default() |
| 63 | + # This is deprecated, but it gets the total width and height |
| 64 | + # of all screens, which is what we want. It will go away in |
| 65 | + # GTK4, but this code will never be ported to GTK4. |
| 66 | + width = screen.get_width() |
| 67 | + height = screen.get_height() |
| 68 | + # Create a window that will fill the screen. |
| 69 | + window = self._window = Gtk.Window() |
| 70 | + # Move that window to the top left. |
| 71 | + # pylint: disable=no-member |
| 72 | + window.move(0, 0) |
| 73 | + # Make the window fill the whole screen. |
| 74 | + # pylint: disable=no-member |
| 75 | + window.resize(width, height) |
| 76 | + # Request that the window not be decorated by the window manager. |
| 77 | + window.set_decorated(False) |
| 78 | + # Connect a signal so that the window and menu can be |
| 79 | + # unmapped (no longer shown on screen) once clicked. |
| 80 | + window.connect("button-press-event", self.on_button_press) |
| 81 | + # When the window is created, mark it as override-redirect |
| 82 | + # (invisible to the window manager) and transparent. |
| 83 | + window.connect("realize", self._on_realize) |
| 84 | + # The signal IDs of the map and unmap signals, so that this class |
| 85 | + # can stop listening to signals from the old menu when it is |
| 86 | + # replaced or unregistered. |
| 87 | + self._unmap_signal_id = self._map_signal_id = 0 |
| 88 | + |
| 89 | + def clear_widget(self) -> None: |
| 90 | + """ |
| 91 | + Clears the connected widget. Automatically called by |
| 92 | + show_for_widget(). |
| 93 | + """ |
| 94 | + widget = self._widget |
| 95 | + map_signal_id = self._map_signal_id |
| 96 | + unmap_signal_id = self._unmap_signal_id |
| 97 | + |
| 98 | + # Double-disconnect is C-level undefined behavior, so ensure |
| 99 | + # it cannot happen. It is better to leak memory if an exception |
| 100 | + # is thrown here. GObject.disconnect_by_func() is buggy |
| 101 | + # (https://gitlab.gnome.org/GNOME/pygobject/-/issues/106), |
| 102 | + # so avoid it. |
| 103 | + if widget is not None: |
| 104 | + if map_signal_id != 0: |
| 105 | + # Clear the signal ID to avoid double-disconnect |
| 106 | + # if this method is interrupted and then called again. |
| 107 | + self._map_signal_id = 0 |
| 108 | + widget.disconnect(map_signal_id) |
| 109 | + if unmap_signal_id != 0: |
| 110 | + # Clear the signal ID to avoid double-disconnect |
| 111 | + # if this method is interrupted and then called again. |
| 112 | + self._unmap_signal_id = 0 |
| 113 | + widget.disconnect(unmap_signal_id) |
| 114 | + self._widget = None |
| 115 | + |
| 116 | + def show_for_widget(self, widget: Gtk.Widget, /) -> None: |
| 117 | + # Clear any existing connections. |
| 118 | + self.clear_widget() |
| 119 | + # Store the new widget. |
| 120 | + self._widget = widget |
| 121 | + # Connect map and unmap signals. |
| 122 | + self._unmap_signal_id = widget.connect("unmap", self._hide) |
| 123 | + self._map_signal_id = widget.connect("map", self._show) |
| 124 | + |
| 125 | + @staticmethod |
| 126 | + def _on_realize(window: Gtk.Window, /) -> None: |
| 127 | + window.set_opacity(0) |
| 128 | + gdk_window = window.get_window() |
| 129 | + gdk_window.set_override_redirect(True) |
| 130 | + window.get_root_window().set_cursor( |
| 131 | + Gdk.Cursor.new_for_display( |
| 132 | + display=gdk_window.get_display(), |
| 133 | + cursor_type=Gdk.CursorType.ARROW, |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + def _show(self, widget: Gtk.Widget, /) -> None: |
| 138 | + assert widget is self._widget, "signal not properly disconnected" |
| 139 | + # pylint: disable=no-member |
| 140 | + self._window.show_all() |
| 141 | + |
| 142 | + def _hide(self, widget: Gtk.Widget, /) -> None: |
| 143 | + assert widget is self._widget, "signal not properly disconnected" |
| 144 | + self._window.hide() |
| 145 | + |
| 146 | + # pylint: disable=line-too-long |
| 147 | + def on_button_press( |
| 148 | + self, window: Gtk.Window, _event: Gdk.EventButton, / |
| 149 | + ) -> None: |
| 150 | + # Hide the window and the widget. |
| 151 | + window.hide() |
| 152 | + self._widget.hide() |
| 153 | + |
| 154 | + |
| 155 | +def get_fullscreen_window_hack() -> X11FullscreenWindowHack: |
| 156 | + if is_xwayland: |
| 157 | + return X11FullscreenWindowHackXWayland() |
| 158 | + return X11FullscreenWindowHack() |
0 commit comments