GajimDBus: gajim2beryl.py

File gajim2beryl.py, 2.5 KB (added by jan@…, 3 years ago)

Python dbus script to glue gajim and beryl together

Line 
1#!/usr/bin/python
2
3# notification about new messages in
4# gajim using the beryl water ripple effect
5# Jan Vornberger (jav), xmpp:jav@jabber.de
6
7import os
8import gobject
9import string
10import re
11import dbus
12
13if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
14    import dbus.glib
15
16GAJIM_SERVICE = 'org.gajim.dbus'
17GAJIM_OBJ_PATH = '/org/gajim/dbus/RemoteObject'
18GAJIM_INTERFACE = 'org.gajim.dbus.RemoteInterface'
19
20BERYL_SERVICE = 'org.freedesktop.beryl'
21BERYL_OBJ_PATH = '/org/freedesktop/beryl/water/allscreens/point'
22BERYL_INTERFACE = 'org.freedesktop.beryl.activate'
23
24# function to find root window id;
25# we will need to pass that to beryl
26def get_root_window_id():
27        rootid = 0
28        idline = os.popen('xwininfo -root | grep "id:"').readlines()[0]
29        idmatch = re.search('id: ([^ ]*)', idline)
30        if (idmatch):
31                rootid = int(idmatch.group(1), 16)
32                return rootid
33        else:
34                raise 'Can not find window id of root window'
35
36# function to find the position of gajim systray icon;
37# we want to ping that position
38def get_systray_icon_position():
39        # find systray icon
40        cmd = 'xwininfo  -root -tree | grep gajim'
41        posregex = re.compile('(\d+)x(\d+)\+\d+\+\d+\s*\+(\d+)\+(\d+)') # match window position info
42
43        for line in os.popen(cmd).readlines():
44                posmatch = posregex.search(line)
45                if (posmatch):
46                        width = int(posmatch.group(1))
47                        height = int(posmatch.group(2))
48                        x = int(posmatch.group(3))
49                        y = int(posmatch.group(4))
50                        if (width >= 15 and width <= 30 and height >= 15 and height < 30):
51                                # I guess this is the systray icon
52                                return x + (width / 2), y
53
54        raise 'Can not find gajim systray icon'
55
56# callback function to be informed about new messages in gajim
57def new_message(details):
58        # new message -> ripple notification
59        beryl_dbus_iface.activate('root', rootid,
60                                        'amplitude', 1,
61                                        'x', systray_x,
62                                        'y', systray_y,
63                                        reply_handler=(lambda *args: None),
64                                        error_handler=(lambda *args: None))
65
66# get root id and systray position for later use
67rootid = get_root_window_id()
68systray_x, systray_y = get_systray_icon_position()
69
70# magic starts here
71bus = dbus.SessionBus()
72
73# connect to NewMessage signal from gajim
74gajim_proxy_obj = bus.get_object(GAJIM_SERVICE, GAJIM_OBJ_PATH)
75gajim_dbus_iface = dbus.Interface(gajim_proxy_obj, GAJIM_INTERFACE)
76gajim_dbus_iface.connect_to_signal('NewMessage', new_message)
77
78# prepare beryl dbus connection
79beryl_proxy_obj = bus.get_object(BERYL_SERVICE, BERYL_OBJ_PATH)
80beryl_dbus_iface = dbus.Interface(beryl_proxy_obj, BERYL_INTERFACE)
81
82# get things going
83mainloop = gobject.MainLoop()
84mainloop.run()