|
3 | 3 | License: GNU GPLv3 |
4 | 4 | Copyright (c) 2019 RedFantom |
5 | 5 | """ |
| 6 | +import gi |
| 7 | +gi.require_version("GdkPixbuf", "2.0") |
| 8 | +from gi.repository import GLib as glib |
| 9 | +from gi.repository import GdkPixbuf as pixbuf |
6 | 10 | import mk_notifications |
7 | 11 | import time |
8 | | -from gi.repository import GLib as glib |
9 | 12 | import dbus |
10 | 13 | from dbus.mainloop.glib import DBusGMainLoop |
| 14 | +import traceback |
| 15 | +import numpy |
| 16 | +from PIL import Image |
11 | 17 |
|
12 | 18 |
|
13 | 19 | def notifications(bus, message): |
14 | | - print([arg for arg in message.get_arg_list()]) |
| 20 | + try: |
| 21 | + handle_notification(message) |
| 22 | + except Exception: |
| 23 | + print("Exception in notification handler!") |
| 24 | + print(traceback.format_exc()) |
| 25 | + |
| 26 | + |
| 27 | +def handle_notification(message): |
| 28 | + for arg in message.get_args_list(): |
| 29 | + if type(arg) == dbus.Dictionary: |
| 30 | + arg = {str(k): v for k, v in arg.items()} |
| 31 | + print(list(arg.keys())) |
| 32 | + if "image-data" not in arg or len(arg["image-data"]) != 7: |
| 33 | + print(len(arg), list(arg.keys())) |
| 34 | + print("Invalid message:", message) |
| 35 | + continue |
| 36 | + w, h, r, a, b, d, data = arg["image-data"] |
| 37 | + data = bytes([int(b) for b in data]) |
| 38 | + mode = "RGB" if not a else "RGBA" |
| 39 | + image = Image.frombytes( |
| 40 | + mode, (w, h), data, "raw", mode, r) |
| 41 | + if mode == "RGBA": |
| 42 | + image = image.convert("RGB") |
| 43 | + array = numpy.array(image) |
| 44 | + data = [[tuple(int(e) for e in column) for column in row] for row in array] |
| 45 | + color = mk_notifications.calculate_dominant_color( |
| 46 | + data, 1, len(data), len(data[0]), 25, 700, 60, 1) |
| 47 | + mk_notifications.flash_keyboard(*color) |
15 | 48 |
|
16 | 49 |
|
17 | 50 | if __name__ == '__main__': |
18 | | - r = mk_notifications.init(2, 25, 700, 60, 1, 20.0, 2, 1.0) |
| 51 | + r = mk_notifications.init(2, 25, 700, 60, 1, 20.0, 2, .5) |
19 | 52 | if r is None: |
20 | 53 | raise RuntimeError() |
21 | 54 | mk_notifications.start() |
22 | 55 |
|
23 | 56 | DBusGMainLoop(set_as_default=True) |
24 | | - bus = dbus.SystemBus() |
| 57 | + bus = dbus.SessionBus() |
25 | 58 | bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'") |
26 | 59 | bus.add_message_filter(notifications) |
27 | 60 |
|
|
0 commit comments