Last active
March 11, 2022 13:39
-
-
Save carlocastoldi/b52a3434c36b8bf185afe4a545ff90d8 to your computer and use it in GitHub Desktop.
Inhibit portal test in GTK4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Desktop Entry] | |
Type=Application | |
Encoding=UTF-8 | |
Name=Portal Inhibitor Test | |
Comment=A sample application | |
Exec=./xdg-portal-inhibitor-test-gtk4.py | |
Icon=settings | |
Terminal=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import dbus | |
import gi | |
gi.require_version("Gtk", "4.0") | |
gi.require_version('GdkWayland', '4.0') | |
from gi.repository import Gtk, GLib, GdkWayland, GdkX11 | |
APP_ID="com.myapp.InhibitorTest" | |
APP_NAME="XDG Portal Inhibitor Test" | |
APP_PRGNAME="xdg-portal-inhibitor-test" | |
FLAGS = 1|2|4|8 # Logout|User Switch|Suspend|Idle | |
GLib.set_application_name(APP_NAME) | |
GLib.set_prgname(APP_PRGNAME) | |
class XdgPortalInhibitor(): | |
def __init__(self): | |
self.bus = dbus.SessionBus() | |
self._inhibited = False | |
def inhibit(self, window_id): | |
self.__proxy = self.bus.get_object( | |
"org.freedesktop.portal.Desktop", | |
"/org/freedesktop/portal/desktop" | |
) | |
self.__proxy = dbus.Interface( | |
self.__proxy, dbus_interface="org.freedesktop.portal.Inhibit" | |
) | |
request_path = self.__proxy.Inhibit(window_id, FLAGS, {"reason": "Testing xdg-portal inhibition!"}) | |
self.handle = self.bus.get_object('org.freedesktop.portal.Desktop', request_path) | |
self.handle = dbus.Interface(self.handle, dbus_interface="org.freedesktop.portal.Request") | |
self._inhibited = True | |
print("Wake up!") | |
def uninhibit(self): | |
if self._inhibited: | |
self.handle.Close() | |
self._inhibited = False | |
print("You can sleep now.") | |
@property | |
def applicable(self): | |
return "org.freedesktop.portal.Desktop" in self.bus.list_names() | |
class TestApp(Gtk.Application): | |
def __init__(self): | |
self.xdg_inhibitor= XdgPortalInhibitor() | |
self.win = None | |
super().__init__(application_id=APP_ID) | |
self.connect('activate', lambda app: app.on_activate()) | |
def toggle_inhibition(self): | |
top_level= self.win.get_surface() | |
if self.xdg_inhibitor._inhibited: | |
self.xdg_inhibitor.uninhibit() | |
top_level.unexport_handle() | |
else: | |
if 'GdkX11Toplevel' in str(type(top_level)): | |
compositor_window_id = f"x11:{top_level.get_xid()}" | |
self.xdg_inhibitor.inhibit(compositor_window_id) | |
else: | |
top_level.export_handle(lambda w,x,y,z: self.wayland_handle_obtained(w,x,y,z), self.win, "\"destroy_func\"") | |
def on_activate(self): | |
self.win = Gtk.Window(application=self, title="A window") | |
toggle_btn = Gtk.Button(label="Toggle Inhibition") | |
toggle_btn.connect('clicked', lambda x: self.toggle_inhibition()) | |
self.win.set_child(toggle_btn) | |
self.win.present() | |
def wayland_handle_obtained(self, top_level, handle, user_data, destroy_func): | |
print("TOP_LEVEL:", top_level) | |
print("HANDLE:", f"wayland:{handle}") | |
print("USER_DATA:", user_data) | |
print("DESTROY_FUNC:", destroy_func) | |
compositor_window_id = f"wayland:{handle}" | |
self.xdg_inhibitor.inhibit(compositor_window_id) | |
if __name__ == "__main__": | |
app = TestApp() | |
app.run(None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment