So, This was one of the major problem that we have to work on is to receive XFCE API signals in Plug developed using Python, signals were emitting out in our socket part that is written in C programming language, so we have to figure some logic so that our plug can response to emitted signals.
So, To solve this problem we have used the concept of d-bus. What we are trying to do is create server with python plug and then client side in C programmed Gtk Socket.
Want to have a look over how does client and server process work with python?
So lets start with code snippet in Python programming language.
Here is an example how we will setup server side for dbus in python.
Server side for dbus in Python
#!/usr/bin/env python3 import dbus import dbus.service import dbus.mainloop.glib from gi.repository import GLib class Service(dbus.service.Object): def __init__(self, message): self._message = message def run(self): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) # define service bus name bus_name = dbus.service.BusName("com.example.service", dbus.SessionBus()) dbus.service.Object.__init__(self, bus_name, "/com/example/service") self._loop = GLib.MainLoop() print("Service running...") self._loop.run() print("Service stopped") @dbus.service.method("com.example.service.Message", in_signature='', out_signature='s') def get_message(self): """ Function without input string but string as output Typecast of output value is defined as s as value of out_signature """ print(" sending message") return self._message @dbus.service.method("com.example.service.Quit", in_signature='', out_signature='') def quit(self): """ function to quit mainloop """ print(" shutting down") self._loop.quit() @dbus.service.method("com.example.service.Print", in_signature='s', out_signature='s') def print(self, input_string): """ Function with input parameter Typecast of input value is defined as s as value of in_signature Typecast of output value is defined as s as value of out_signature """ print(" print method", input_string) return input_string Service("This is the service").run()
in_signature and out_signature are used to define typecast for input and output value. For example, In the above example you can see for string as input, so we have defined ‘s’ as value of in_signature.
so now we are moving toward our client side of dbus in python.
Client Side for dbus in Python
#!/usr/bin/env python3 import dbus class Client(): def __init__(self): # Get the Session bus bus = dbus.SessionBus() # Get the object service = bus.get_object('com.example.service', "/com/example/service") # The service is an object that provides dbus.service.Object self._message = service.get_dbus_method('get_message', 'com.example.service.Message') self._quit = service.get_dbus_method('quit', 'com.example.service.Quit') self.print = service.get_dbus_method('print', 'com.example.service.Print') def run(self): print("Message from service:", self._message()) print("print method running ", self.print("Hello World")) self._quit() Client().run()
So from this example you can see that we are accessing dbus.service objects inside the client side. It proof that we can use different objects through dbus.
For testing out, these programs you have to open down two different terminal, In one terminal you have to start server side and then in other terminal you have to start client, Then finally you will see successful results.
Here is some glimpse of output
Server side output
Client side output
In the next blog we will have client part code snippet in C with some XFCE API example.
Suggestions and improvements are welcomed in the comment section of the blog.
Pingback:Final blog during GSOC 2021 with XFCE – FreshlyBuilt.com