Skip to content

Commit be3aba4

Browse files
committed
feat: D-Bus signal to apply preset to all Panel Colorizer instances
refs: #126
1 parent 3f70386 commit be3aba4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

package/contents/ui/tools/service.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#!/usr/bin/env python
22
"""
3-
D-Bus service to interact with the current panel
3+
D-Bus service to interact with the panel
44
"""
5-
65
import sys
76
import dbus
87
import dbus.service
98
from dbus.mainloop.glib import DBusGMainLoop
109
from gi.repository import GLib
1110

1211
DBusGMainLoop(set_as_default=True)
13-
bus = dbus.SessionBus()
1412

1513
CONTAINMENT_ID = sys.argv[1]
1614
PANEL_ID = sys.argv[2]
1715
SERVICE_NAME = "luisbocanegra.panel.colorizer.c" + CONTAINMENT_ID + ".w" + PANEL_ID
16+
SHARED_INTERFACE = "luisbocanegra.panel.colorizer.all"
1817
PATH = "/preset"
1918

2019

@@ -25,15 +24,21 @@ class Service(dbus.service.Object):
2524
dbus (dbus.service.Object): D-Bus object
2625
"""
2726

28-
def __init__(self):
27+
def __init__(self, bus: dbus.Bus):
2928
self._loop = GLib.MainLoop()
3029
self._last_preset = ""
3130
self._pending_witch = False
31+
self._bus = bus
3232
super().__init__()
3333

3434
def run(self):
3535
"""run"""
3636
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+
)
3742
bus_name = dbus.service.BusName(SERVICE_NAME, dbus.SessionBus())
3843
dbus.service.Object.__init__(self, bus_name, PATH)
3944

@@ -53,12 +58,19 @@ def preset(self, m="") -> str:
5358
"""
5459
if m:
5560
if m != self._last_preset:
56-
print(f"last_last_preset: '{m}'")
5761
self._last_preset = m
5862
self._pending_witch = True
5963
return "saved"
6064
return self._last_preset
6165

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+
6274
@dbus.service.method(SERVICE_NAME, in_signature="", out_signature="b")
6375
def pending_switch(self) -> bool:
6476
"""Wether there is a pending preset switch
@@ -82,8 +94,9 @@ def quit(self):
8294

8395
if __name__ == "__main__":
8496
# Keep a single instance of the service
97+
session_bus = dbus.SessionBus()
8598
try:
86-
bus.get_object(SERVICE_NAME, PATH)
99+
session_bus.get_object(SERVICE_NAME, PATH)
87100
print("Service is already running")
88101
except dbus.exceptions.DBusException:
89-
Service().run()
102+
Service(session_bus).run()

0 commit comments

Comments
 (0)