From a396dd9af9c023315ce97605198b95c649176055 Mon Sep 17 00:00:00 2001 From: Ben Blain Date: Tue, 2 Mar 2021 19:47:28 +0000 Subject: [PATCH] notify-send: Add option to wait until notification has been closed If an expiration timeout is set, the notification is not waited longer than such time. --- docs/notify-send.xml | 6 ++++++ tools/notify-send.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/docs/notify-send.xml b/docs/notify-send.xml index e2c4b1d..185dbd6 100644 --- a/docs/notify-send.xml +++ b/docs/notify-send.xml @@ -104,6 +104,12 @@ The ID of the notification to replace. + + , + + Wait for the notification to be closed before exiting. If the is set, it will be used as the maximum waiting time. + + diff --git a/tools/notify-send.c b/tools/notify-send.c index 937fb41..e8dc272 100644 --- a/tools/notify-send.c +++ b/tools/notify-send.c @@ -32,6 +32,7 @@ #define GETTEXT_PACKAGE NULL static NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL; +static GMainLoop *loop = NULL; static gboolean g_option_arg_urgency_cb (const char *option_name, @@ -120,6 +121,22 @@ notify_notification_set_hint_variant (NotifyNotification *notification, return TRUE; } +static void +handle_closed (NotifyNotification *notify, + gpointer user_data) +{ + g_main_loop_quit (loop); +} + +static gboolean +on_wait_timeout (gpointer data) +{ + fprintf (stderr, "Wait timeout expired\n"); + g_main_loop_quit (loop); + + return FALSE; +} + int main (int argc, char *argv[]) { @@ -134,6 +151,7 @@ main (int argc, char *argv[]) static gint notification_id = 0; static gboolean do_version = FALSE; static gboolean hint_error = FALSE, show_error = FALSE; + static gboolean wait = FALSE; static glong expire_timeout = NOTIFY_EXPIRES_DEFAULT; GOptionContext *opt_ctx; NotifyNotification *notify; @@ -165,6 +183,9 @@ main (int argc, char *argv[]) N_ ("Print the notification ID."), NULL}, {"replace-id", 'r', 0, G_OPTION_ARG_INT, ¬ification_id, N_ ("The ID of the notification to replace."), N_("REPLACE_ID")}, + {"wait", 'w', 0, G_OPTION_ARG_NONE, &wait, + N_("Wait for the notification to be closed before exiting."), + NULL}, {"version", 'v', 0, G_OPTION_ARG_NONE, &do_version, N_("Version of the package."), NULL}, @@ -273,6 +294,17 @@ main (int argc, char *argv[]) } } + if (wait) { + g_signal_connect (G_OBJECT (notify), + "closed", + G_CALLBACK (handle_closed), + NULL); + + if (expire_timeout > 0) { + g_timeout_add (expire_timeout, on_wait_timeout, NULL); + } + } + if (!hint_error) { retval = notify_notification_show (notify, &error); @@ -288,6 +320,13 @@ main (int argc, char *argv[]) g_printf ("%d\n", notification_id); } + if (wait) { + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + g_main_loop_unref (loop); + loop = NULL; + } + g_object_unref (G_OBJECT (notify)); notify_uninit ();