Skip to content

Commit 78c0377

Browse files
committed
Complete notification example
1 parent 8612f53 commit 78c0377

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

examples/notifications/mk_notifications.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,11 @@ static PyObject* stop(PyObject* self, PyObject* args) {
183183
static PyObject* py_calculate_dominant_color(PyObject* self, PyObject* args) {
184184
/** Python interface to fast dominant color calculation function */
185185
PyObject* list;
186-
int w, h, divider, lower, upper, sat_bias;
187-
bool brightness_norm;
188-
if (!PyArg_ParseTuple(args, "O!iiiiiip",
186+
int w, h, divider, lower, upper, sat_bias, brightness_norm;
187+
if (!PyArg_ParseTuple(args, "O!iiiiiii",
189188
&PyList_Type, &list, &divider, &w, &h, &lower, &upper,
190189
&sat_bias, &brightness_norm))
191190
return NULL;
192-
193191
unsigned char data[w][h][3];
194192
PyObject* column, *row, *e;
195193
for (int x=0; x<w; x++) {
@@ -215,19 +213,17 @@ static PyObject* py_calculate_dominant_color(PyObject* self, PyObject* args) {
215213
}
216214
}
217215
}
218-
219216
unsigned char result[3];
220217
PyObject* tuple = PyTuple_New(3);
221218
calc_dominant_color(
222-
data, w, h, result, divider, sat_bias, lower, upper, brightness_norm);
219+
data, w, h, result, divider, sat_bias, lower, upper, brightness_norm==1);
223220
for (int i=0; i<3; i++)
224221
PyTuple_SetItem(tuple, i, PyInt_FromLong(result[i]));
225222
return tuple;
226223
}
227224

228225

229226
void __flash_keyboard(unsigned char* color) {
230-
printf("Flashing %d, %d, %d\n", color[0], color[1], color[2]);
231227
pthread_mutex_lock(&target_lock);
232228
target_override = true;
233229
pthread_mutex_unlock(&target_lock);

examples/notifications/notifications.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,58 @@
33
License: GNU GPLv3
44
Copyright (c) 2019 RedFantom
55
"""
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
610
import mk_notifications
711
import time
8-
from gi.repository import GLib as glib
912
import dbus
1013
from dbus.mainloop.glib import DBusGMainLoop
14+
import traceback
15+
import numpy
16+
from PIL import Image
1117

1218

1319
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)
1548

1649

1750
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)
1952
if r is None:
2053
raise RuntimeError()
2154
mk_notifications.start()
2255

2356
DBusGMainLoop(set_as_default=True)
24-
bus = dbus.SystemBus()
57+
bus = dbus.SessionBus()
2558
bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'")
2659
bus.add_message_filter(notifications)
2760

0 commit comments

Comments
 (0)