dbus code now works

This commit is contained in:
Fredrick W. Warren 2025-02-26 05:48:30 -07:00
parent 0716c1d849
commit 1ba03bfe6b

View File

@ -8,6 +8,11 @@ Licensed under the GNU General Public License.
import logging
from wmdocklib import wmoo as wmoo
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
import threading
from icecream import ic
line_height = 9
@ -30,6 +35,74 @@ class Application(wmoo.Application):
[" TANDA", 0],
[" OTHER", 0],
]
# Initialize D-Bus and connect to Pidgin's ReceivedIMMsg signal
self.register_dbus()
def register_dbus(self):
try:
# Set up the D-Bus main loop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
# Connect to the session bus
bus = dbus.SessionBus()
# Obtain the Pidgin D-Bus service object
purple_service = bus.get_object(
"im.pidgin.purple.PurpleService",
"/im/pidgin/purple/PurpleObject"
)
# Get the interface to interact with
purple_interface = dbus.Interface(
purple_service,
"im.pidgin.purple.PurpleInterface"
)
# Connect the ReceivedIMMsg signal to the handler
purple_interface.connect_to_signal(
"ReceivedImMsg",
self.handle_received_im_msg
)
# Connect the SentIMMsg signal to the handler
purple_interface.connect_to_signal(
"SentImMsg",
self.handle_sent_im_msg
)
ic("Connected to Pidgin's ReceivedIMMsg signal successfully.")
except dbus.DBusException as e:
print("Failed to connect to Pidgin's D-Bus interface:", e)
def handle_received_im_msg(self, account, sender, message, conversation, flags):
"""
Callback function that handles the ReceivedIMMsg signal.
Prints the sender and message.
Parameters:
account (str): The account from which the message was received.
sender (str): The sender's identifier.
message (str): The message content.
conversation (str): The conversation identifier.
flags (int): Message flags.
"""
ic("")
ic(f"sender: {sender}")
ic(f"message: {message}")
self._flasher = 7
def handle_sent_im_msg(self, account, recepient, message):
"""
Callback function that handles the SentImMsg signal.
Prints the sender and message.
Parameters:
recepient (str): The recepien's identifier.
message (str): The message content.
"""
ic("")
ic(f"recepient: {recepient}")
ic(f"message: {message}")
def draw_string(self, xstart, ystart, text):
for char in text:
@ -279,18 +352,16 @@ patterns = [
]
# Example signal handler (replace with your actual handler)
def handle_received_im_msg(sender, message):
"""Handles the ReceivedIMMsg signal from the PurpleService."""
print(f"Received message from {sender}: {message}")
# Process the message here
def handle_sending_im_msg(sender, message):
"""Handles the ReceivedIMMsg signal from the PurpleService."""
print(f"Received message from {sender}: {message}")
# Process the message here
def run_glib_mainloop():
"""
Runs the GLib main loop. This should be executed in a separate thread.
"""
loop = GLib.MainLoop()
ic("Start Loop")
try:
loop.run()
except KeyboardInterrupt:
loop.quit()
def main():
"""
@ -309,35 +380,13 @@ def main():
app.draw_all_text()
app.addCallback(app.toggle_backlight, 'buttonrelease', area=(2,2,62,62))
"""this contains the eventLoop. events are examined and if a
callback has been registered, it is called, passing it the event as
argument.
"""
# Initialize DBus mainloop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus() # Or dbus.SystemBus() if it's a system service
try:
purple_service = bus.get_object('im.pidgin.purple.PurpleService', '/im/pidgin/purple/PurpleObject')
purple_service.connect_to_signal("ReceivedIMMsg", handle_received_im_msg, dbus_interface="im.pidgin.purple.PurpleInterface")
purple_service.connect_to_signal("SendingIMMsg", handle_sending_im_msg, dbus_interface="im.pidgin.purple.PurpleInterface")
except dbus.exceptions.DBusException as e:
print(f"Error connecting to PurpleService: {e}")
purple_service = None # Handle the case where the service isn't available
def check_dbus():
"""Check for and process DBus messages."""
# This is a placeholder. DBus messages are handled automatically by the mainloop.
# You don't need to explicitly fetch them. The signal handlers you connect
# (like the commented-out example above) will be called when signals arrive.
return True # Keep the GLib.timeout_add running
# Add a GLib timeout to periodically check for DBus messages (though it's mostly for keeping the loop alive)
GLib.timeout_add(100, check_dbus) # Check every 100 milliseconds
# Start the GLib main loop in a separate thread
glib_thread = threading.Thread(target=run_glib_mainloop, daemon=True)
glib_thread.start()
# Run the application's main loop
app.run()
if __name__ == '__main__':
main()
main()