From 5da123c63217f9e940a24b5a54f1e6a8712776a6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 3 Feb 2019 15:54:32 +0530 Subject: [PATCH] Respond to clicks on user notifications via DBus --- kitty/boss.py | 4 ++-- kitty/glfw.c | 10 ++++++++++ kitty/notify.py | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index ec9dc8d9e..27e24c5d1 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1012,8 +1012,8 @@ class Boss: notification_activated() def dbus_notification_callback(self, activated, *args): - from .notify import dbus_notification_created + from .notify import dbus_notification_created, dbus_notification_activated if activated: - pass + dbus_notification_activated(*args) else: dbus_notification_created(*args) diff --git a/kitty/glfw.c b/kitty/glfw.c index 2fefd5468..a7dca5387 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -669,6 +669,12 @@ error_callback(int error, const char* description) { } +static void +dbus_user_notification_activated(uint32_t notification_id, const char* action) { + unsigned long nid = notification_id; + call_boss(dbus_notification_callback, "Oks", Py_True, nid, action); +} + PyObject* glfw_init(PyObject UNUSED *self, PyObject *args) { const char* path; @@ -685,6 +691,10 @@ glfw_init(PyObject UNUSED *self, PyObject *args) { #ifdef __APPLE__ glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, 0); glfwInitHint(GLFW_COCOA_MENUBAR, 0); +#else + if (glfwDBusSetUserNotificationHandler) { + glfwDBusSetUserNotificationHandler(dbus_user_notification_activated); + } #endif PyObject *ans = glfwInit() ? Py_True: Py_False; if (ans == Py_True) { diff --git a/kitty/notify.py b/kitty/notify.py index 49cebd35b..a138f31ef 100644 --- a/kitty/notify.py +++ b/kitty/notify.py @@ -23,15 +23,22 @@ if is_macos: else: from .fast_data_types import dbus_send_notification + from .constants import get_boss alloc_map = {} identifier_map = {} def dbus_notification_created(alloc_id, notification_id): - identifier = alloc_map.get(alloc_id) + identifier = alloc_map.pop(alloc_id, None) if identifier is not None: identifier_map[identifier] = notification_id + def dbus_notification_activated(notification_id, action): + rmap = {v: k for k, v in identifier_map.items()} + identifier = rmap.get(notification_id) + if identifier is not None: + get_boss().notification_activated(identifier) + def notify( title, body,