-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhost.py
167 lines (136 loc) · 4.34 KB
/
host.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio # noqa
from gi.repository import GLib # noqa
MENU_PATH = os.path.join(os.path.dirname(__file__), 'menu.py')
NODE_INFO = Gio.DBusNodeInfo.new_for_xml("""
<?xml version="1.0" encoding="UTF-8"?>
<node>
<interface name="org.kde.StatusNotifierWatcher">
<method name="RegisterStatusNotifierItem">
<arg type="s" direction="in"/>
</method>
<property name="RegisteredStatusNotifierItems" type="as" access="read">
</property>
<property name="IsStatusNotifierHostRegistered" type="b" access="read">
</property>
</interface>
</node>""")
items = {}
def render():
# customize this function to your needs
# see https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem/
# for available fields
labels = []
for key, item in reversed(items.items()):
name, path = key.split('/', 1)
if item['Status'] == 'Passive':
continue
label = f'[{item["IconName"]}]'
cmd = (
f'busctl --user call \\{name} /{path} '
'org.kde.StatusNotifierItem Activate ii 0 0'
)
menu_cmd = f'python3 {MENU_PATH} \\{name} {item["Menu"]}'
label = f'%{{A1:{cmd}:}}{label}%{{A}}'
label = f'%{{A3:{menu_cmd}:}}{label}%{{A}}'
labels.append(label)
print(' '.join(labels), flush=True)
def get_item_data(conn, sender, path):
def callback(conn, red, user_data=None):
args = conn.call_finish(red)
items[sender + path] = args[0]
render()
conn.call(
sender,
path,
'org.freedesktop.DBus.Properties',
'GetAll',
GLib.Variant('(s)', ['org.kde.StatusNotifierItem']),
GLib.VariantType('(a{sv})'),
Gio.DBusCallFlags.NONE,
-1,
None,
callback,
None,
)
def on_call(
conn, sender, path, interface, method, params, invocation, user_data=None
):
props = {
'RegisteredStatusNotifierItems': GLib.Variant('as', items.keys()),
'IsStatusNotifierHostRegistered': GLib.Variant('b', True),
}
if method == 'Get' and params[1] in props:
invocation.return_value(GLib.Variant('(v)', [props[params[1]]]))
conn.flush()
if method == 'GetAll':
invocation.return_value(GLib.Variant('(a{sv})', [props]))
conn.flush()
elif method == 'RegisterStatusNotifierItem':
if params[0].startswith('/'):
path = params[0]
else:
path = '/StatusNotifierItem'
get_item_data(conn, sender, path)
invocation.return_value(None)
conn.flush()
def on_signal(
conn, sender, path, interface, signal, params, invocation, user_data=None
):
if signal == 'NameOwnerChanged':
if params[2] != '':
return
keys = [key for key in items if key.startswith(params[0] + '/')]
if not keys:
return
for key in keys:
del items[key]
render()
elif sender + path in items:
get_item_data(conn, sender, path)
def on_bus_acquired(conn, name, user_data=None):
for interface in NODE_INFO.interfaces:
if interface.name == name:
conn.register_object('/StatusNotifierWatcher', interface, on_call)
def signal_subscribe(interface, signal):
conn.signal_subscribe(
None, # sender
interface,
signal,
None, # path
None,
Gio.DBusSignalFlags.NONE,
on_signal,
None, # user_data
)
signal_subscribe('org.freedesktop.DBus', 'NameOwnerChanged')
for signal in [
'NewAttentionIcon',
'NewIcon',
'NewIconThemePath',
'NewStatus',
'NewTitle',
]:
signal_subscribe('org.kde.StatusNotifierItem', signal)
def on_name_lost(conn, name, user_data=None):
sys.exit(
f'Could not aquire name {name}. '
f'Is some other service blocking it?'
)
if __name__ == '__main__':
owner_id = Gio.bus_own_name(
Gio.BusType.SESSION,
NODE_INFO.interfaces[0].name,
Gio.BusNameOwnerFlags.NONE,
on_bus_acquired,
None,
on_name_lost,
)
try:
loop = GLib.MainLoop()
loop.run()
finally:
Gio.bus_unown_name(owner_id)