1
1
#!/usr/bin/env python
2
2
"""
3
- D-Bus service to interact with the current panel
3
+ D-Bus service to interact with the panel
4
4
"""
5
-
6
5
import sys
7
6
import dbus
8
7
import dbus .service
9
8
from dbus .mainloop .glib import DBusGMainLoop
10
9
from gi .repository import GLib
11
10
12
11
DBusGMainLoop (set_as_default = True )
13
- bus = dbus .SessionBus ()
14
12
15
13
CONTAINMENT_ID = sys .argv [1 ]
16
14
PANEL_ID = sys .argv [2 ]
17
15
SERVICE_NAME = "luisbocanegra.panel.colorizer.c" + CONTAINMENT_ID + ".w" + PANEL_ID
16
+ SHARED_INTERFACE = "luisbocanegra.panel.colorizer.all"
18
17
PATH = "/preset"
19
18
20
19
@@ -25,15 +24,21 @@ class Service(dbus.service.Object):
25
24
dbus (dbus.service.Object): D-Bus object
26
25
"""
27
26
28
- def __init__ (self ):
27
+ def __init__ (self , bus : dbus . Bus ):
29
28
self ._loop = GLib .MainLoop ()
30
29
self ._last_preset = ""
31
30
self ._pending_witch = False
31
+ self ._bus = bus
32
32
super ().__init__ ()
33
33
34
34
def run (self ):
35
35
"""run"""
36
36
DBusGMainLoop (set_as_default = True )
37
+ self ._bus .add_signal_receiver (
38
+ self .on_shared_preset_signal ,
39
+ dbus_interface = SHARED_INTERFACE ,
40
+ signal_name = "preset" ,
41
+ )
37
42
bus_name = dbus .service .BusName (SERVICE_NAME , dbus .SessionBus ())
38
43
dbus .service .Object .__init__ (self , bus_name , PATH )
39
44
@@ -53,12 +58,19 @@ def preset(self, m="") -> str:
53
58
"""
54
59
if m :
55
60
if m != self ._last_preset :
56
- print (f"last_last_preset: '{ m } '" )
57
61
self ._last_preset = m
58
62
self ._pending_witch = True
59
63
return "saved"
60
64
return self ._last_preset
61
65
66
+ def on_shared_preset_signal (self , preset_name : str ):
67
+ """Handle the shared signal to set the preset
68
+
69
+ Args:
70
+ preset_name (str): The preset name from the signal
71
+ """
72
+ self .preset (preset_name )
73
+
62
74
@dbus .service .method (SERVICE_NAME , in_signature = "" , out_signature = "b" )
63
75
def pending_switch (self ) -> bool :
64
76
"""Wether there is a pending preset switch
@@ -82,8 +94,9 @@ def quit(self):
82
94
83
95
if __name__ == "__main__" :
84
96
# Keep a single instance of the service
97
+ session_bus = dbus .SessionBus ()
85
98
try :
86
- bus .get_object (SERVICE_NAME , PATH )
99
+ session_bus .get_object (SERVICE_NAME , PATH )
87
100
print ("Service is already running" )
88
101
except dbus .exceptions .DBusException :
89
- Service ().run ()
102
+ Service (session_bus ).run ()
0 commit comments