[pulseaudio-discuss] [PATCH 1/4] desktop-notifications: Refactored the code to make notifications independent of the backend. Added a notification manager to handle notifications.

Ștefan Săftescu stefan.saftescu at gmail.com
Thu Jul 5 14:48:58 PDT 2012


---
 src/modules/notifications/notification-backend.h |   36 ++++++
 src/modules/notifications/notification-manager.c |  148 ++++++++++++++++++++++
 src/modules/notifications/notification-manager.h |   45 +++++++
 src/modules/notifications/notification.c         |   50 ++++++++
 src/modules/notifications/notification.h         |   49 +++++++
 5 files changed, 328 insertions(+)
 create mode 100644 src/modules/notifications/notification-backend.h
 create mode 100644 src/modules/notifications/notification-manager.c
 create mode 100644 src/modules/notifications/notification-manager.h
 create mode 100644 src/modules/notifications/notification.c
 create mode 100644 src/modules/notifications/notification.h

diff --git a/src/modules/notifications/notification-backend.h b/src/modules/notifications/notification-backend.h
new file mode 100644
index 0000000..79d998f
--- /dev/null
+++ b/src/modules/notifications/notification-backend.h
@@ -0,0 +1,36 @@
+#ifndef foonotificationbackendfoo
+#define foonotificationbackendfoo
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2012 Ștefan Săftescu
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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 Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+#include "notification.h"
+
+typedef struct pa_ui_notification_backend pa_ui_notification_backend;
+
+typedef void(*pa_ui_notification_backend_cb_t)(pa_ui_notification_backend *b, pa_ui_notification *n);
+
+struct pa_ui_notification_backend {
+    pa_ui_notification_backend_cb_t send_notification;
+    pa_ui_notification_backend_cb_t cancel_notification;
+
+    void* userdata;
+};
+
+#endif
diff --git a/src/modules/notifications/notification-manager.c b/src/modules/notifications/notification-manager.c
new file mode 100644
index 0000000..b4dafb8
--- /dev/null
+++ b/src/modules/notifications/notification-manager.c
@@ -0,0 +1,148 @@
+
+
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2012 Ștefan Săftescu
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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 Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/refcnt.h>
+#include <pulsecore/shared.h>
+
+#include "notification.h"
+#include "notification-backend.h"
+#include "notification-manager.h"
+
+struct pa_ui_notification_manager {
+    PA_REFCNT_DECLARE;
+
+    pa_core *core;
+    pa_ui_notification_backend *backend;
+};
+
+static pa_ui_notification_manager* pa_ui_notification_manager_new(pa_core *c) {
+    pa_ui_notification_manager *m;
+
+    pa_assert(c);
+
+    m = pa_xnew(pa_ui_notification_manager, 1);
+    PA_REFCNT_INIT(m);
+
+    m->core =  c;
+    m->backend = NULL;
+
+    pa_assert_se(pa_shared_set(c, "ui-notification-manager", m) >= 0);
+
+    return m;
+}
+
+static void pa_ui_notification_manager_free(pa_ui_notification_manager *m) {
+    pa_assert(m);
+    pa_assert(m->backend); /* TODO: handle backends. */
+
+    pa_assert_se(pa_shared_remove(m->core, "ui-notification-manager"));
+
+    pa_xfree(m);
+}
+
+pa_ui_notification_manager* pa_ui_notification_manager_get(pa_core *c) {
+    pa_ui_notification_manager *m;
+
+    if((m = pa_shared_get(c, "ui-notification-manager")))
+        return pa_ui_notification_manager_ref(m);
+
+    return pa_ui_notification_manager_new(c);
+}
+
+pa_ui_notification_manager* pa_ui_notification_manager_ref(pa_ui_notification_manager *m) {
+    pa_assert(m);
+    pa_assert(PA_REFCNT_VALUE(m) >= 1);
+
+    PA_REFCNT_INC(m);
+
+    return m;
+}
+
+void pa_ui_notification_manager_unref(pa_ui_notification_manager *m) {
+    pa_assert(m);
+    pa_assert(PA_REFCNT_VALUE(m) >= 1);
+
+    if(PA_REFCNT_DEC(m) <= 0)
+        pa_ui_notification_manager_free(m);
+}
+
+int pa_ui_notification_manager_register_backend(pa_ui_notification_manager *m, pa_ui_notification_backend *b) {
+    pa_assert(m);
+    pa_assert(b);
+
+    if(m->backend != NULL) {
+        pa_log_error("A UI notification backend is already registered.");
+        return -1;
+    }
+
+    m->backend = b;
+
+    return 0;
+}
+
+void pa_ui_notification_manager_unregister_backend(pa_ui_notification_manager *m) {
+    pa_assert(m);
+
+    m->backend = NULL;
+}
+
+pa_ui_notification_backend* pa_ui_notification_manager_get_backend(pa_ui_notification_manager *m) {
+    pa_assert(m);
+
+    return m->backend;
+}
+
+int pa_ui_notification_manager_send(pa_ui_notification_manager *m, pa_ui_notification *n) {
+    pa_assert(m);
+    pa_assert(n);
+
+    if(m->backend == NULL) {
+        pa_log_error("No UI notification backend is registered.");
+        return -1;
+    }
+
+    m->backend->send_notification(m->backend, n);
+
+    return 0;
+}
+
+int pa_ui_notification_manager_cancel(pa_ui_notification_manager *m, pa_ui_notification *n) {
+    pa_assert(m);
+    pa_assert(n);
+
+    if(m->backend == NULL) {
+        pa_log_error("No UI notification backend is registered.");
+        return -1;
+    }
+
+    m->backend->cancel_notification(m->backend, n);
+
+    return 0;
+}
diff --git a/src/modules/notifications/notification-manager.h b/src/modules/notifications/notification-manager.h
new file mode 100644
index 0000000..0d2f036
--- /dev/null
+++ b/src/modules/notifications/notification-manager.h
@@ -0,0 +1,45 @@
+#ifndef foonotificationmanagerfoo
+#define foonotificationmanagerfoo
+
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2012 Ștefan Săftescu
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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 Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <pulsecore/core.h>
+
+#include "notification.h"
+#include "notification-backend.h"
+
+typedef struct pa_ui_notification_manager pa_ui_notification_manager;
+
+pa_ui_notification_manager* pa_ui_notification_manager_get(pa_core *c);
+
+pa_ui_notification_manager* pa_ui_notification_manager_ref(pa_ui_notification_manager *m);
+void pa_ui_notification_manager_unref(pa_ui_notification_manager *m);
+
+int pa_ui_notification_manager_register_backend(pa_ui_notification_manager *m, pa_ui_notification_backend *b);
+void pa_ui_notification_manager_unregister_backend(pa_ui_notification_manager *m);
+
+pa_ui_notification_backend* pa_ui_notification_manager_get_backend(pa_ui_notification_manager *m);
+
+int pa_ui_notification_manager_send(pa_ui_notification_manager *m, pa_ui_notification *n);
+int pa_ui_notification_manager_cancel(pa_ui_notification_manager *m, pa_ui_notification *n);
+
+#endif
diff --git a/src/modules/notifications/notification.c b/src/modules/notifications/notification.c
new file mode 100644
index 0000000..cb6bca1
--- /dev/null
+++ b/src/modules/notifications/notification.c
@@ -0,0 +1,50 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2012 Ștefan Săftescu
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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 Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include "notification.h"
+#include "notification-backend.h"
+
+pa_ui_notification* pa_ui_notification_new(pa_ui_notification_reply_cb_t reply_cb, void *userdata) {
+    pa_ui_notification *n = pa_xnew(pa_ui_notification, 1);
+
+    n->replaces_id = 0;
+    n->summary = "";
+    n->body = "";
+    n->actions = NULL;
+    n->num_actions = 0; /* TODO: actions */
+    n->expire_timeout = -1;
+
+    n->handle_reply = reply_cb;
+    n->userdata = userdata;
+
+
+    return n;
+}
+
+void pa_ui_notification_free(pa_ui_notification *n) {
+    pa_xfree(n);
+}
diff --git a/src/modules/notifications/notification.h b/src/modules/notifications/notification.h
new file mode 100644
index 0000000..03623a6
--- /dev/null
+++ b/src/modules/notifications/notification.h
@@ -0,0 +1,49 @@
+#ifndef foonotificationfoo
+#define foonotificationfoo
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2012 Ștefan Săftescu
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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 Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+#include <stdint.h>
+
+typedef struct pa_ui_notification pa_ui_notification;
+typedef struct pa_ui_notification_reply pa_ui_notification_reply;
+
+typedef void(*pa_ui_notification_reply_cb_t)(pa_ui_notification_reply *reply);
+
+struct pa_ui_notification {
+    uint32_t replaces_id;
+    char *summary;
+    char *body;
+    char **actions;
+    size_t num_actions;
+    int32_t expire_timeout;
+
+    pa_ui_notification_reply_cb_t handle_reply;
+    void* userdata;
+};
+
+struct pa_ui_notification_reply {
+    pa_ui_notification *source;
+};
+
+pa_ui_notification* pa_ui_notification_new(pa_ui_notification_reply_cb_t reply_cb, void *userdata);
+void pa_ui_notification_free(pa_ui_notification *notification);
+
+#endif
-- 
1.7.10.4



More information about the pulseaudio-discuss mailing list