-
Notifications
You must be signed in to change notification settings - Fork 393
/
tray.py
481 lines (382 loc) · 16.4 KB
/
tray.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
## Copyright (C) 2012-2013 Daniel Pavel
## Copyright (C) 2014-2024 Solaar Contributors https://pwr-solaar.github.io/Solaar/
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import logging
import os
from time import time as _timestamp
import gi
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository.Gdk import ScrollDirection
import solaar.gtk as gtk
from solaar import NAME
from solaar.i18n import _
from . import icons as _icons
from .about import show_window as _show_about_window
from .action import make_image_menu_item
from .window import popup as _window_popup
from .window import toggle as _window_toggle
logger = logging.getLogger(__name__)
_TRAY_ICON_SIZE = 48
_MENU_ICON_SIZE = Gtk.IconSize.LARGE_TOOLBAR
def _create_menu(quit_handler):
# per-device menu entries will be generated as-needed
menu = Gtk.Menu()
no_receiver = Gtk.MenuItem.new_with_label(_("No supported device found"))
no_receiver.set_sensitive(False)
menu.append(no_receiver)
menu.append(Gtk.SeparatorMenuItem.new())
menu.append(make_image_menu_item(_("About %s") % NAME, "help-about", _show_about_window))
menu.append(make_image_menu_item(_("Quit %s") % NAME, "application-exit", quit_handler))
menu.show_all()
return menu
_last_scroll = 0
def _scroll(tray_icon, event, direction=None):
if direction is None:
direction = event.direction
now = event.time / 1000.0
else:
now = None
if direction != ScrollDirection.UP and direction != ScrollDirection.DOWN:
# ignore all other directions
return
if sum(map(lambda i: i[1] is not None, _devices_info)) < 2: # don't bother even trying to scroll if less than two devices
return
# scroll events come way too fast (at least 5-6 at once) so take a little break between them
global _last_scroll
now = now or _timestamp()
if now - _last_scroll < 0.33: # seconds
return
_last_scroll = now
global _picked_device
candidate = None
if _picked_device is None:
for info in _devices_info:
# pick first peripheral found
if info[1] is not None:
candidate = info
break
else:
found = False
for info in _devices_info:
if not info[1]:
# only conside peripherals
continue
# compare peripherals
if info[0:2] == _picked_device[0:2]:
if direction == ScrollDirection.UP and candidate:
# select previous device
break
found = True
else:
if found:
candidate = info
if direction == ScrollDirection.DOWN:
break
# if direction is up, but no candidate found before _picked,
# let it run through all candidates, will get stuck with the last one
else:
if direction == ScrollDirection.DOWN:
# only use the first one, in case no candidates are after _picked
if candidate is None:
candidate = info
else:
candidate = info
# if the last _picked_device is gone, clear it
# the candidate will be either the first or last one remaining,
# depending on the scroll direction
if not found:
_picked_device = None
_picked_device = candidate or _picked_device
if logger.isEnabledFor(logging.DEBUG):
logger.debug("scroll: picked %s", _picked_device)
_update_tray_icon()
try:
try:
gi.require_version("AyatanaAppIndicator3", "0.1")
from gi.repository import AyatanaAppIndicator3 as AppIndicator3
ayatana_appindicator_found = True
except ValueError:
try:
gi.require_version("AppIndicator3", "0.1")
from gi.repository import AppIndicator3
ayatana_appindicator_found = False
except ValueError as exc:
# treat unavailable versions the same as unavailable packages
raise ImportError from exc
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"using {'Ayatana ' if ayatana_appindicator_found else ''}AppIndicator3")
# Defense against AppIndicator3 bug that treats files in current directory as icon files
# https://bugs.launchpad.net/ubuntu/+source/libappindicator/+bug/1363277
# Defense against bug that shows up in XFCE 4.16 where icons are not upscaled
def _icon_file(icon_name):
if gtk.tray_icon_size is None and not os.path.isfile(icon_name):
return icon_name
icon_info = Gtk.IconTheme.get_default().lookup_icon(
icon_name, gtk.tray_icon_size or _TRAY_ICON_SIZE, Gtk.IconLookupFlags.FORCE_SVG
)
return icon_info.get_filename() if icon_info else icon_name
def _create(menu):
_icons._init_icon_paths()
ind = AppIndicator3.Indicator.new(
"indicator-solaar", _icon_file(_icons.TRAY_INIT), AppIndicator3.IndicatorCategory.HARDWARE
)
ind.set_title(NAME)
ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
# ind.set_attention_icon_full(_icon_file(_icons.TRAY_ATTENTION), '') # works poorly for XFCE 16
# ind.set_label(NAME.lower(), NAME.lower())
ind.set_menu(menu)
ind.connect("scroll-event", _scroll)
return ind
def _hide(indicator):
indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
def _show(indicator):
indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
def _update_tray_icon():
if _picked_device and gtk.battery_icons_style != "solaar":
_ignore, _ignore, name, device = _picked_device
battery_level = device.battery_info.level if device.battery_info is not None else None
battery_charging = device.battery_info.charging() if device.battery_info is not None else None
tray_icon_name = _icons.battery(battery_level, battery_charging)
description = f"{name}: {device.status_string()}"
else:
# there may be a receiver, but no peripherals
tray_icon_name = _icons.TRAY_OKAY if _devices_info else _icons.TRAY_INIT
description_lines = _generate_description_lines()
description = "\n".join(description_lines).rstrip("\n")
# icon_file = _icons.icon_file(icon_name, _TRAY_ICON_SIZE)
_icon.set_icon_full(_icon_file(tray_icon_name), description)
def attention(reason=None):
if _icon.get_status() != AppIndicator3.IndicatorStatus.ATTENTION:
# _icon.set_attention_icon_full(_icon_file(_icons.TRAY_ATTENTION), reason or '') # works poorly for XFCe 16
_icon.set_status(AppIndicator3.IndicatorStatus.ATTENTION)
GLib.timeout_add(10 * 1000, _icon.set_status, AppIndicator3.IndicatorStatus.ACTIVE)
except ImportError:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("using StatusIcon")
def _create(menu):
icon = Gtk.StatusIcon.new_from_icon_name(_icons.TRAY_INIT)
icon.set_name(NAME.lower())
icon.set_title(NAME)
icon.set_tooltip_text(NAME)
icon.connect("activate", _window_toggle)
icon.connect("scroll-event", _scroll)
icon.connect("popup-menu", lambda icon, button, time: menu.popup(None, None, icon.position_menu, icon, button, time))
return icon
def _hide(icon):
icon.set_visible(False)
def _show(icon):
icon.set_visible(True)
def _update_tray_icon():
tooltip_lines = _generate_tooltip_lines()
tooltip = "\n".join(tooltip_lines).rstrip("\n")
_icon.set_tooltip_markup(tooltip)
if _picked_device and gtk.battery_icons_style != "solaar":
_ignore, _ignore, name, device = _picked_device
battery_level = device.battery_info.level if device.battery_info is not None else None
battery_charging = device.battery_info.charging() if device.battery_info is not None else None
tray_icon_name = _icons.battery(battery_level, battery_charging)
else:
# there may be a receiver, but no peripherals
tray_icon_name = _icons.TRAY_OKAY if _devices_info else _icons.TRAY_ATTENTION
_icon.set_from_icon_name(tray_icon_name)
_icon_before_attention = None
def _blink(count):
global _icon_before_attention
if count % 2:
_icon.set_from_icon_name(_icons.TRAY_ATTENTION)
else:
_icon.set_from_icon_name(_icon_before_attention)
if count > 0:
GLib.timeout_add(1000, _blink, count - 1)
else:
_icon_before_attention = None
def attention(reason=None):
global _icon_before_attention
if _icon_before_attention is None:
_icon_before_attention = _icon.get_icon_name()
GLib.idle_add(_blink, 9)
def _generate_tooltip_lines():
if not _devices_info:
yield f"<b>{NAME}</b>: " + _("no receiver")
return
yield from _generate_description_lines()
def _generate_description_lines():
if not _devices_info:
yield _("no receiver")
return
for _ignore, number, name, device in _devices_info:
if number is None: # receiver
continue
p = device.status_string()
if p: # does it have any properties to print?
yield f"<b>{name}</b>"
if device.online:
yield f"\t{p}"
else:
yield f"\t{p} <small>(" + _("offline") + ")</small>"
else:
if device.online:
yield f"<b>{name}</b> <small>(" + _("no status") + ")</small>"
else:
yield f"<b>{name}</b> <small>(" + _("offline") + ")</small>"
def _pick_device_with_lowest_battery():
if not _devices_info:
return None
picked = None
picked_level = 1000
for info in _devices_info:
if info[1] is None: # is receiver
continue
level = info[-1].battery_info.level if info[-1].battery_info is not None else None
if level is not None and picked_level > level:
picked = info
picked_level = level or 0
if logger.isEnabledFor(logging.DEBUG):
logger.debug("picked device with lowest battery: %s", picked)
return picked
def _add_device(device):
assert device
index = 0
receiver_path = device.receiver.path if device.receiver is not None else device.path
if device.receiver is not None: # if receiver insert into devices for the receiver in device number order
for idx, (path, _ignore, _ignore, _ignore) in enumerate(_devices_info):
if path and path == receiver_path:
index = idx + 1 # the first entry matching the receiver serial should be for the receiver itself
break
while index < len(_devices_info):
path, number, _ignore, _ignore = _devices_info[index]
if not path == receiver_path:
break
assert number != device.number
if number > device.number:
break
index = index + 1
new_device_info = (receiver_path, device.number, device.name, device)
_devices_info.insert(index, new_device_info)
label = (" " if device.number else "") + device.name
new_menu_item = make_image_menu_item(label, None, _window_popup, receiver_path, device.number)
_menu.insert(new_menu_item, index)
return index
def _remove_device(index):
assert index is not None
menu_items = _menu.get_children()
_menu.remove(menu_items[index])
removed_device = _devices_info.pop(index)
global _picked_device
if _picked_device and _picked_device[0:2] == removed_device[0:2]:
# the current pick was unpaired
_picked_device = None
def _add_receiver(receiver):
index = len(_devices_info)
new_receiver_info = (receiver.path, None, receiver.name, None)
_devices_info.insert(index, new_receiver_info)
icon_name = _icons.device_icon_name(receiver.name, receiver.kind)
new_menu_item = make_image_menu_item(receiver.name, icon_name, _window_popup, receiver.path)
_menu.insert(new_menu_item, index)
return 0
def _remove_receiver(receiver):
index = 0
# remove all entries in devices_info that match this receiver
while index < len(_devices_info):
path, _ignore, _ignore, _ignore = _devices_info[index]
if path == receiver.path:
_remove_device(index)
else:
index += 1
def _update_menu_item(index, device):
if device is None:
logger.warning("updating an inactive device %s, assuming disconnected", device)
return None
menu_items = _menu.get_children()
menu_item = menu_items[index]
level = device.battery_info.level if device.battery_info is not None else None
charging = device.battery_info.charging() if device.battery_info is not None else None
icon_name = _icons.battery(level, charging)
menu_item.label.set_label((" " if 0 < device.number <= 6 else "") + device.name + ": " + device.status_string())
image_widget = menu_item.icon
image_widget.set_sensitive(bool(device.online))
image_widget.set_from_icon_name(icon_name, _MENU_ICON_SIZE)
# for which device to show the battery info in systray, if more than one
# it's actually an entry in _devices_info
_picked_device = None
# cached list of devices and some of their properties
# contains tuples of (receiver path, device number, name, device)
_devices_info = []
_menu = None
_icon = None
def init(_quit_handler):
global _menu, _icon
assert _menu is None
_menu = _create_menu(_quit_handler)
assert _icon is None
_icon = _create(_menu)
update()
def destroy():
global _icon, _menu, _devices_info
if _icon is not None:
i, _icon = _icon, None
_hide(i)
i = None
_icon = None
_menu = None
_devices_info = None
def update(device=None):
if _icon is None:
return
if device is not None:
if device.kind is None:
# receiver
is_alive = bool(device)
receiver_path = device.path
if is_alive:
index = None
for idx, (path, _ignore, _ignore, _ignore) in enumerate(_devices_info):
if path == receiver_path:
index = idx
break
if index is None:
_add_receiver(device)
else:
_remove_receiver(device)
else:
# peripheral
is_paired = bool(device)
receiver_path = device.receiver.path if device.receiver is not None else device.path
index = None
for idx, (path, number, _ignore, _ignore) in enumerate(_devices_info):
if path == receiver_path and number == device.number:
index = idx
if is_paired:
if index is None:
index = _add_device(device)
_update_menu_item(index, device)
else: # was just unpaired or unplugged
if index is not None:
_remove_device(index)
menu_items = _menu.get_children()
no_receivers_index = len(_devices_info)
menu_items[no_receivers_index].set_visible(not _devices_info)
global _picked_device
if (not _picked_device or _last_scroll == 0) and device is not None and device.kind is not None:
# if it's just a receiver update, it's unlikely the picked device would change
_picked_device = _pick_device_with_lowest_battery()
_update_tray_icon()
if _icon:
if not _devices_info:
_hide(_icon)
else:
_show(_icon)