-
-
Save Aviana/a3a0368172e326d6ccc57cb254c1e569 to your computer and use it in GitHub Desktop.
Test xdg-desktop-portal Global Shortcuts
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 | |
from gi.repository import GLib | |
import threading | |
import re | |
import dbus | |
from dbus.mainloop.glib import DBusGMainLoop | |
bus_name = 'org.freedesktop.portal.Desktop' | |
request_interface = 'org.freedesktop.portal.Request' | |
shortcut_interface = 'org.freedesktop.portal.GlobalShortcuts' | |
object_path = '/org/freedesktop/portal/desktop' | |
shortcuts = [ | |
( | |
"binding1", | |
{ | |
"description": "Binding #1", | |
# "preferred-trigger": "CTRL+a", | |
}, | |
), | |
( | |
"binding2", | |
{ | |
"description": "Binding #2", | |
# "preferred-trigger": "CTRL+b", | |
}, | |
), | |
] | |
class PortalGlobalShortcuts: | |
def __init__(self): | |
DBusGMainLoop(set_as_default=True) | |
self.bus = dbus.SessionBus() | |
self.portal = self.bus.get_object(bus_name, object_path) | |
self.CreateSession() | |
def BindShortcuts(self): | |
self.portal.BindShortcuts(self.session, shortcuts, '', {}, dbus_interface=shortcut_interface) | |
print("Bind Shortcuts") | |
def ListShortcuts(self): | |
self.portal.ListShortcuts(self.session, {}, dbus_interface=shortcut_interface) | |
print("Listing Shortcuts") | |
def ShortcutsChanged(self, shortcuts): | |
print(shortcuts) | |
def Callback(self, response, results): | |
print("Callback Event") | |
self.ShortcutsChanged(results['shortcuts']) | |
def CreateSessionCallback(self, response, results): | |
print("CreateSession called back") | |
self.session = results['session_handle'] | |
reply = self.portal.ListShortcuts(self.session, {}, dbus_interface=shortcut_interface) | |
self.bus.add_signal_receiver(self.Callback, | |
'Response', | |
request_interface, | |
bus_name, | |
reply) | |
self.BindShortcuts() | |
def KeyActivatedCallback(self, session_handle, shortcut_id, timestamp, options): | |
print(f"{shortcut_id} activated") | |
def KeyDeactivatedCallback(self, session_handle, shortcut_id, timestamp, options): | |
print(f"{shortcut_id} deactivated") | |
def ShortcutsChangedCallback(self, session_handle, shortcuts): | |
print("Shortcuts Changed") | |
if session_handle == self.session: | |
self.ShortcutsChanged(shortcuts) | |
def CreateSession(self): | |
options = { 'handle_token': 'mytest', | |
'session_handle_token': 'Mytest', | |
} | |
reply = self.portal.CreateSession(options, dbus_interface=shortcut_interface) | |
self.bus.add_signal_receiver(self.CreateSessionCallback, | |
'Response', | |
request_interface, | |
bus_name, | |
reply) | |
self.bus.add_signal_receiver(self.KeyActivatedCallback, | |
'Activated', | |
shortcut_interface, | |
bus_name, | |
object_path) | |
self.bus.add_signal_receiver(self.KeyDeactivatedCallback, | |
'Deactivated', | |
shortcut_interface, | |
bus_name, | |
object_path) | |
self.bus.add_signal_receiver(self.ShortcutsChangedCallback, | |
'ShortcutsChanged', | |
shortcut_interface, | |
bus_name, | |
object_path) | |
print("Created Session") | |
def loop_th(): | |
loop.run() | |
loop = GLib.MainLoop() | |
portal = PortalGlobalShortcuts() | |
thread = threading.Thread(target=loop_th) | |
thread.daemon = True | |
thread.start() | |
x = input() #hold off quitting |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment