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.
This commit is contained in:
Ben Blain 2021-03-02 19:47:28 +00:00 committed by Marco Trevisan (Treviño)
parent 00a7e74774
commit a396dd9af9
2 changed files with 45 additions and 0 deletions

View File

@ -104,6 +104,12 @@
<para>The ID of the notification to replace.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-w</option>, <option>--wait</option></term>
<listitem>
<para>Wait for the notification to be closed before exiting. If the <option>expire-time</option> is set, it will be used as the maximum waiting time.</para>
</listitem>
</varlistentry>
</variablelist>
</refsection>
<refsection>

View File

@ -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, &notification_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 ();