annotate main.c @ 13:eb97cafe34e5

Try to prevent messages from being chopped up into multiple notifications Read data into a buffer and only send a notification when 250ms have passed since the first read instead of sending a notification immediately after each read. This is an attempt to prevent messages from being chopped up into multiple notifications. A (rather negligible) downside of this approach is that multiple messages rapidly following each other are coalesced into a single notification.
author Guido Berhoerster <guido+xwrited@berhoerster.name>
date Mon, 16 Mar 2015 23:54:20 +0100
parents 6440ba6e3466
children 4a5330979433
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
1 /*
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
2 * Copyright (C) 2015 Guido Berhoerster <guido+xwrited@berhoerster.name>
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
3 *
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
11 *
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
14 *
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
22 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
23
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
24 #define _XOPEN_SOURCE 600
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
25
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
26 #include <stdio.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
27 #include <stdlib.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
28 #include <string.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
29 #include <unistd.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
30 #include <fcntl.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
31 #include <sys/stat.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
32 #include <signal.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
33 #include <errno.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
34 #include <locale.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
35 #include <libintl.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
36 #include <glib.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
37 #include <glib/gi18n.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
38 #include <libnotify/notify.h>
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
39 #include "xwrited-debug.h"
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
40 #include "xwrited-unique.h"
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
41 #include "xwrited-utmp.h"
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
42
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
43 #define BUFFER_TIMEOUT (250)
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
44
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
45 enum {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
46 PIPE_R_FD = 0,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
47 PIPE_W_FD
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
48 };
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
49
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
50 static int signal_pipe_fd[2] = { -1, -1 };
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
51 static guint notify_timeout_id;
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
52 static GMainLoop *loop;
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
53 static GString *buffer;
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
54
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
55 static void
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
56 on_signal(int signo)
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
57 {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
58 int old_errno = errno;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
59 ssize_t n;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
60 sigset_t sigset;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
61
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
62 /* try to read unread signals from the pipe and add the new one to it */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
63 n = read(signal_pipe_fd[PIPE_R_FD], &sigset, sizeof (sigset));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
64 if ((n == -1) || ((size_t)n < sizeof (sigset))) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
65 sigemptyset(&sigset);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
66 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
67 sigaddset(&sigset, signo);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
68 write(signal_pipe_fd[PIPE_W_FD], &sigset, sizeof (sigset));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
69
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
70 errno = old_errno;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
71 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
72
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
73 static gboolean
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
74 signal_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data)
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
75 {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
76 sigset_t sigset;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
77 sigset_t old_sigset;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
78 GIOStatus status;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
79 gsize n;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
80 GError *error = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
81
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
82 /*
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
83 * deal with pending signals previously received in the signal handler,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
84 * try to read a sigset from the pipe, avoid partial reads by blocking
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
85 * all signals during the read operation
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
86 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
87 sigfillset(&sigset);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
88 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
89 status = g_io_channel_read_chars(source, (gchar *)&sigset,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
90 sizeof (sigset), &n, &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
91 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
92 if (status != G_IO_STATUS_NORMAL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
93 if (status != G_IO_STATUS_AGAIN) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
94 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
95 g_critical("failed to read from signal pipe: "
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
96 "%s", error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
97 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
98 g_main_loop_quit(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
99 } else {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
100 g_critical("failed to read from signal pipe");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
101 g_main_loop_quit(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
102 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
103 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
104 } else if (n != sizeof (sigset)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
105 g_critical("short read from signal pipe");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
106 g_main_loop_quit(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
107 } else {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
108 if ((sigismember(&sigset, SIGINT) == 1) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
109 (sigismember(&sigset, SIGTERM) == 1) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
110 (sigismember(&sigset, SIGQUIT) == 1) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
111 (sigismember(&sigset, SIGHUP) == 1)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
112 g_debug("received signal, exiting");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
113 g_main_loop_quit(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
114 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
115 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
116
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
117 return (TRUE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
118 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
119
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
120 static gboolean
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
121 send_notification(void)
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
122 {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
123 gboolean retval = FALSE;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
124 GString *utf8_str = NULL;
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
125 gchar *startp = buffer->str;
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
126 gchar *endp;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
127 GRegex *regex = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
128 GError *error = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
129 gchar *body = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
130 GList *capabilities = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
131 gchar *tmp;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
132 NotifyNotification *notification = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
133
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
134 utf8_str = g_string_sized_new(buffer->len);
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
135 while (!g_utf8_validate(startp, buffer->str + buffer->len -
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
136 startp, (const gchar **)&endp)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
137 g_string_append_len(utf8_str, startp, endp - startp);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
138 /*
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
139 * replace each byte that does not belong to a UTF-8-encoded
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
140 * character with the Unicode REPLACEMENT CHARACTER (U+FFFD)
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
141 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
142 g_string_append(utf8_str, "\357\277\275");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
143
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
144 startp = endp + ((endp < buffer->str + buffer->len) ? 1 : 0);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
145 }
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
146 g_string_append_len(utf8_str, startp, buffer->str + buffer->len -
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
147 startp);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
148
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
149 /* remove any CR, BEL and trailing space and tabs */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
150 regex = g_regex_new("([\r\a]+|[ \t\r\a]+$)", G_REGEX_MULTILINE, 0,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
151 &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
152 if (error != NULL) {
11
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
153 g_critical("failed to create regex object: %s",
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
154 error->message);
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
155 g_error_free(error);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
156 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
157 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
158 body = g_regex_replace_literal(regex, utf8_str->str, -1, 0, "", 0,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
159 &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
160 if (error != NULL) {
11
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
161 g_critical("failed to replace control and space characters: "
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
162 "%s", error->message);
fd2e275436a4 Display error messages and free errors
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 1
diff changeset
163 g_error_free(error);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
164 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
165 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
166
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
167 /*
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
168 * skip empty messages or messages only consisting of whitespace and
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
169 * control characters
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
170 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
171 if ((strlen(body) == 0) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
172 !g_regex_match_simple("[^[:space:][:cntrl:]]", body, 0, 0)) {
12
6440ba6e3466 Do not print a warning if an empty notification is skipped
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 11
diff changeset
173 retval = TRUE;
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
174 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
175 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
176
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
177 /*
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
178 * if the notification daemon supports markup the message needs to be
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
179 * escaped
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
180 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
181 capabilities = notify_get_server_caps();
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
182 if (g_list_find_custom(capabilities, "body-markup",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
183 (GCompareFunc)strcmp) != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
184 tmp = g_markup_escape_text(body, -1);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
185 g_free(body);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
186 body = tmp;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
187 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
188
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
189 /* show notification */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
190 notification = notify_notification_new(_("Message received"),
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
191 body, "utilities-terminal"
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
192 #if !defined(NOTIFY_VERSION_MINOR) || \
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
193 (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
194 , NULL
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
195 #endif
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
196 );
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
197 if (notification == NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
198 g_critical("failed to create a notification object");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
199 g_main_loop_quit(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
200 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
201 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
202 notify_notification_set_timeout(notification, NOTIFY_EXPIRES_NEVER);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
203 retval = notify_notification_show(notification, NULL);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
204
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
205 out:
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
206 if (notification != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
207 g_object_unref(G_OBJECT(notification));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
208 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
209 if (capabilities != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
210 g_list_free_full(capabilities, g_free);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
211 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
212 g_free(body);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
213 if (regex != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
214 g_regex_unref(regex);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
215 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
216 if (utf8_str != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
217 g_string_free(utf8_str, TRUE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
218 }
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
219 /* prevent a permanently large buffer */
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
220 g_string_free(buffer, FALSE);
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
221 buffer = g_string_sized_new(BUFSIZ);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
222
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
223 return (retval);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
224 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
225
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
226 static gboolean
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
227 notify_timeout_cb(gpointer user_data)
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
228 {
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
229 if (!send_notification()) {
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
230 g_warning("failed to send notification");
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
231 }
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
232
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
233 notify_timeout_id = 0;
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
234
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
235 return (FALSE);
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
236 }
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
237
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
238 static gboolean
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
239 master_pty_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data)
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
240 {
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
241 gchar buf[BUFSIZ];
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
242 GIOStatus status;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
243 gsize buf_len;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
244 GError *error = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
245
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
246 if ((cond & G_IO_IN) || (cond & G_IO_PRI)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
247 /* read message from master pty */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
248 while ((status = g_io_channel_read_chars(source, buf, BUFSIZ,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
249 &buf_len, &error)) == G_IO_STATUS_NORMAL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
250 if (buf_len > 0) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
251 g_debug("read %" G_GSSIZE_FORMAT " bytes from "
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
252 "master pty", buf_len);
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
253 g_string_append_len(buffer, buf,
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
254 (gssize)buf_len);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
255 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
256 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
257 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
258 g_critical("failed to read from master pty: %s",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
259 error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
260 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
261 g_main_loop_quit(loop);
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
262 return (FALSE);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
263 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
264
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
265 /*
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
266 * schedule a timeout for sending a notification with the
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
267 * buffered message
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
268 */
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
269 if (notify_timeout_id <= 0) {
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
270 notify_timeout_id = g_timeout_add(BUFFER_TIMEOUT,
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
271 notify_timeout_cb, NULL);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
272 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
273 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
274
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
275 if ((cond & G_IO_ERR) || (cond & G_IO_HUP)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
276 g_critical("connection to master pty broken");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
277 g_main_loop_quit(loop);
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
278 return (FALSE);
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
279 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
280
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
281 return (TRUE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
282 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
283
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
284 int
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
285 main(int argc, char *argv[])
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
286 {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
287 int status = EXIT_FAILURE;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
288 GError *error = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
289 XWritedUnique *app = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
290 GOptionContext *context = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
291 struct sigaction sigact;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
292 GIOChannel *signal_channel = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
293 GIOChannel *master_pty_channel = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
294 int masterfd = -1;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
295 int slavefd = -1;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
296 char *slave_name = NULL;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
297 gboolean vflag = FALSE;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
298 gboolean dflag = FALSE;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
299 const GOptionEntry options[] = {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
300 { "debug", 'd', 0, G_OPTION_ARG_NONE, &dflag,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
301 N_("Show extra debugging information"), NULL },
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
302 { "version", 'V', 0, G_OPTION_ARG_NONE, &vflag,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
303 N_("Print the current version and exit"), NULL },
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
304 { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, 0 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
305 };
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
306
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
307 setlocale(LC_ALL, "");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
308 bindtextdomain(PACKAGE, LOCALEDIR);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
309 bind_textdomain_codeset(PACKAGE, "UTF-8");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
310 textdomain(PACKAGE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
311
1
0907cc7064d4 Add support for newer glib versions
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 0
diff changeset
312 #if !GLIB_CHECK_VERSION(2, 35, 0)
0907cc7064d4 Add support for newer glib versions
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 0
diff changeset
313 /* deprecated in glib >= 2.35 */
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
314 g_type_init();
1
0907cc7064d4 Add support for newer glib versions
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 0
diff changeset
315 #endif
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
316
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
317 context = g_option_context_new("- display write and wall messages as "
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
318 "desktop notifications");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
319 g_option_context_add_main_entries(context, options, PACKAGE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
320 g_option_context_set_translation_domain(context, PACKAGE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
321 g_option_context_parse(context, &argc, &argv, &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
322 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
323 g_printerr("%s.\n", error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
324 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
325 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
326 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
327
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
328 xwrited_debug_init(dflag);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
329
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
330 if (vflag) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
331 g_print("%s %s\n", PACKAGE, VERSION);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
332 status = EXIT_SUCCESS;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
333 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
334 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
335
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
336 app = xwrited_unique_new("org.guido-berhoerster.code.xwrited");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
337 if (app == NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
338 g_critical("failed to initialize application");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
339 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
340 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
341 if (!xwrited_unique_is_unique(app)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
342 g_printerr(_("xwrited is already running in this session.\n"));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
343 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
344 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
345
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
346 if (!notify_init(APP_NAME)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
347 g_critical("failed to initialize libnotify");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
348 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
349 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
350
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
351 loop = g_main_loop_new(NULL, FALSE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
352 if (loop == NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
353 g_critical("failed to create main loop");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
354 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
355 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
356
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
357 buffer = g_string_sized_new(BUFSIZ);
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
358
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
359 /* open master pty */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
360 masterfd = posix_openpt(O_RDWR | O_NOCTTY);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
361 if (masterfd == -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
362 g_critical("failed to open master pty: %s", g_strerror(errno));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
363 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
364 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
365
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
366 /* create slave pty */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
367 if ((grantpt(masterfd) == -1) || (unlockpt(masterfd) == -1)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
368 g_critical("failed to create slave pty: %s", g_strerror(errno));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
369 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
370 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
371 slave_name = ptsname(masterfd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
372 if (slave_name == NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
373 g_critical("failed to obtain name of slave pty");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
374 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
375 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
376
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
377 /*
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
378 * keep an open fd around order to prevent closing the master fd when
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
379 * receiving an EOF
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
380 */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
381 slavefd = open(slave_name, O_RDWR);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
382 if (slavefd == -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
383 g_critical("failed to open slave pty: %s", g_strerror(errno));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
384 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
385 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
386
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
387 /* create a GIOChannel for monitoring the master pty for messages */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
388 master_pty_channel = g_io_channel_unix_new(masterfd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
389 g_io_channel_set_flags(master_pty_channel,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
390 g_io_channel_get_flags(master_pty_channel) | G_IO_FLAG_NONBLOCK,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
391 &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
392 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
393 g_critical("failed set flags on the master pty channel: %s",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
394 error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
395 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
396 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
397 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
398 if (!g_io_add_watch(master_pty_channel, G_IO_IN | G_IO_PRI | G_IO_HUP |
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
399 G_IO_ERR, master_pty_read_cb, NULL)) {
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
400 g_critical("failed to add watch on signal channel");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
401 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
402 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
403
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
404 /* create pipe for delivering signals to a listener in the main loop */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
405 if (pipe(signal_pipe_fd) == -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
406 g_critical("failed to create signal pipe: %s",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
407 g_strerror(errno));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
408 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
409 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
410 if (fcntl(signal_pipe_fd[PIPE_W_FD], F_SETFL, O_NONBLOCK) == -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
411 g_critical("failed to set flags on signal pipe: %s",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
412 g_strerror(errno));
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
413 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
414 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
415
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
416 /* create GIO channel for reading from the signal_pipe */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
417 signal_channel = g_io_channel_unix_new(signal_pipe_fd[PIPE_R_FD]);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
418 g_io_channel_set_encoding(signal_channel, NULL, &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
419 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
420 g_critical("failed to set binary encoding for signal channel: "
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
421 "%s", error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
422 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
423 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
424 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
425 g_io_channel_set_buffered(signal_channel, FALSE);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
426 g_io_channel_set_flags(signal_channel,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
427 g_io_channel_get_flags(signal_channel) | G_IO_FLAG_NONBLOCK,
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
428 &error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
429 if (error != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
430 g_critical("failed set flags on signal channel: %s",
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
431 error->message);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
432 g_error_free(error);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
433 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
434 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
435 if (g_io_add_watch(signal_channel, G_IO_IN | G_IO_PRI | G_IO_HUP |
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
436 G_IO_ERR, signal_read_cb, NULL) == 0) {
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
437 g_critical("failed to add watch on the signal channel");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
438 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
439 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
440
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
441 /* set up signal handler */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
442 sigact.sa_handler = on_signal;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
443 sigact.sa_flags = SA_RESTART;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
444 sigemptyset(&sigact.sa_mask);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
445 if ((sigaction(SIGINT, &sigact, NULL) < 0) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
446 (sigaction(SIGTERM, &sigact, NULL) < 0) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
447 (sigaction(SIGQUIT, &sigact, NULL) < 0) ||
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
448 (sigaction(SIGHUP, &sigact, NULL) < 0)) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
449 g_critical("failed to set up signal handler");
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
450 goto out;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
451 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
452
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
453 xwrited_utmp_add_entry(masterfd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
454
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
455 /* main loop */
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
456 g_main_loop_run(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
457
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
458 xwrited_utmp_remove_entry(masterfd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
459
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
460 status = EXIT_SUCCESS;
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
461
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
462 out:
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
463 if (context != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
464 g_option_context_free(context);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
465 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
466
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
467 if (signal_channel != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
468 g_io_channel_shutdown(signal_channel, FALSE, NULL);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
469 g_io_channel_unref(signal_channel);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
470 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
471
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
472 if (signal_pipe_fd[PIPE_R_FD] != -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
473 close(signal_pipe_fd[PIPE_R_FD]);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
474 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
475 if (signal_pipe_fd[PIPE_W_FD] != -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
476 close(signal_pipe_fd[PIPE_W_FD]);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
477 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
478
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
479 if (master_pty_channel != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
480 g_io_channel_shutdown(master_pty_channel, FALSE, NULL);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
481 g_io_channel_unref(master_pty_channel);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
482 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
483
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
484 if (slavefd != -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
485 close(slavefd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
486 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
487
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
488 if (masterfd != -1) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
489 close(masterfd);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
490 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
491
13
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
492 if (buffer != NULL) {
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
493 g_string_free(buffer, FALSE);
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
494 }
eb97cafe34e5 Try to prevent messages from being chopped up into multiple notifications
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 12
diff changeset
495
0
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
496 if (app != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
497 g_object_unref(app);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
498 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
499
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
500 if (loop != NULL) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
501 g_main_loop_unref(loop);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
502 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
503
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
504 if (notify_is_initted()) {
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
505 notify_uninit();
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
506 }
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
507
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
508 exit(status);
52694b49dcc4 Initial revision
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
509 }