-
Notifications
You must be signed in to change notification settings - Fork 343
/
menu.c
392 lines (318 loc) · 10.7 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#include "menu.h"
#include <errno.h>
#include <glib.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "dbus.h"
#include "dunst.h"
#include "log.h"
#include "notification.h"
#include "queues.h"
#include "settings.h"
#include "utils.h"
static bool is_initialized = false;
static regex_t url_regex;
static gpointer context_menu_thread(gpointer data);
struct {
GList *locked_notifications;
} menu_ctx;
/**
* Initializes regexes needed for matching.
*
* @return true if initialization succeeded
*/
static bool regex_init(void)
{
if (is_initialized)
return true;
char *regex =
"\\<(https?://|ftps?://|news://|mailto:|file://|www\\.)"
"[-[:alnum:]_\\@;/?:&=%$.+!*\x27,~#]*"
"(\\([-[:alnum:]_\\@;/?:&=%$.+!*\x27,~#]*\\)|[-[:alnum:]_\\@;/?:&=%$+*~])+";
int code = regcomp(&url_regex, regex, REG_EXTENDED | REG_ICASE);
if (code != 0) {
char error_buf[120];
regerror(code, &url_regex, error_buf, sizeof(error_buf));
LOG_W("Failed to compile URL-matching regex: %s", error_buf);
return false;
} else {
is_initialized = true;
return true;
}
}
void regex_teardown(void)
{
if (is_initialized) {
regfree(&url_regex);
is_initialized = false;
}
}
/* see menu.h */
char *extract_urls(const char *to_match)
{
if (!to_match)
return NULL;
if (!regex_init())
return NULL;
char *urls = NULL;
const char *p = to_match;
regmatch_t m;
while (1) {
int nomatch = regexec(&url_regex, p, 1, &m, 0);
if (nomatch || m.rm_so == -1)
break;
int start = m.rm_so + (p - to_match);
int finish = m.rm_eo + (p - to_match);
char *match = g_strndup(to_match + start, finish - start);
urls = string_append(urls, match, "\n");
g_free(match);
p += m.rm_eo;
}
return urls;
}
/*
* Open url in browser.
*
*/
void open_browser(const char *in)
{
if (!settings.browser_cmd) {
LOG_C("Unable to open browser: No browser command set.");
return;
}
char *url, *end;
// If any, remove leading [ linktext ] from URL
if (*in == '[' && (end = strstr(in, "] ")))
url = g_strdup(end + 2);
else
url = g_strdup(in);
int argc = 2+g_strv_length(settings.browser_cmd);
char **argv = g_malloc_n(argc, sizeof(char*));
memcpy(argv, settings.browser_cmd, argc * sizeof(char*));
argv[argc-2] = url;
argv[argc-1] = NULL;
GError *err = NULL;
g_spawn_async(NULL,
argv,
NULL,
G_SPAWN_DEFAULT
| G_SPAWN_SEARCH_PATH
| G_SPAWN_STDOUT_TO_DEV_NULL
| G_SPAWN_STDERR_TO_DEV_NULL,
NULL,
NULL,
NULL,
&err);
if (err) {
LOG_C("Cannot spawn browser: %s", err->message);
g_error_free(err);
}
g_free(argv);
g_free(url);
}
char *notification_dmenu_string(struct notification *n)
{
char *dmenu_str = NULL;
gpointer p_key;
gpointer p_value;
GHashTableIter iter;
g_hash_table_iter_init(&iter, n->actions);
while (g_hash_table_iter_next(&iter, &p_key, &p_value)) {
char *key = (char*) p_key;
char *value = (char*) p_value;
char *act_str = g_strdup_printf("#%s (%s) [%d,%s]", value, n->summary, n->id, key);
dmenu_str = string_append(dmenu_str, act_str, "\n");
g_free(act_str);
}
return dmenu_str;
}
/*
* Notify the corresponding client
* that an action has been invoked
*/
void invoke_action(const char *action)
{
struct notification *invoked = NULL;
uint id;
char *data_start, *data_comma, *data_end;
/* format: #<human readable> (<summary>)[<id>,<action>] */
data_start = strrchr(action, '[');
if (!data_start) {
LOG_W("Invalid action: '%s'", action);
return;
}
id = strtol(++data_start, &data_comma, 10);
if (*data_comma != ',') {
LOG_W("Invalid action: '%s'", action);
return;
}
data_end = strchr(data_comma+1, ']');
if (!data_end) {
LOG_W("Invalid action: '%s'", action);
return;
}
char *action_key = g_strndup(data_comma+1, data_end-data_comma-1);
for (const GList *iter = queues_get_displayed();
iter;
iter = iter->next) {
struct notification *n = iter->data;
if (n->id != id)
continue;
if (g_hash_table_contains(n->actions, action_key)) {
invoked = n;
break;
}
}
if (invoked && action_key) {
signal_action_invoked(invoked, action_key);
}
g_free(action_key);
}
/**
* Dispatch whatever has been returned by dmenu.
* If the given result of dmenu is empty or NULL, nothing will be done.
*
* @param input The result from dmenu.
*/
void dispatch_menu_result(const char *input)
{
ASSERT_OR_RET(input,);
char *in = g_strdup(input);
g_strstrip(in);
if (in[0] == '#')
invoke_action(in + 1);
else if (in[0] != '\0')
open_browser(in);
g_free(in);
}
/** Call dmenu with the specified input. Blocks until dmenu is finished.
*
* @param dmenu_input The input string to feed into dmenu
* @returns the selected string from dmenu
*/
char *invoke_dmenu(const char *dmenu_input)
{
if (!settings.dmenu_cmd) {
LOG_C("Unable to open dmenu: No dmenu command set.");
return NULL;
}
ASSERT_OR_RET(STR_FULL(dmenu_input), NULL);
gint dunst_to_dmenu;
gint dmenu_to_dunst;
GError *err = NULL;
char buf[1024];
char *ret = NULL;
g_spawn_async_with_pipes(NULL,
settings.dmenu_cmd,
NULL,
G_SPAWN_DEFAULT
| G_SPAWN_SEARCH_PATH,
NULL,
NULL,
NULL,
&dunst_to_dmenu,
&dmenu_to_dunst,
NULL,
&err);
if (err) {
LOG_C("Cannot spawn dmenu: %s", err->message);
g_error_free(err);
} else {
size_t wlen = strlen(dmenu_input);
if (write(dunst_to_dmenu, dmenu_input, wlen) != wlen) {
LOG_W("Cannot feed dmenu with input: %s", strerror(errno));
}
close(dunst_to_dmenu);
ssize_t rlen = read(dmenu_to_dunst, buf, sizeof(buf));
close(dmenu_to_dunst);
if (rlen > 0)
ret = g_strndup(buf, rlen);
else
LOG_W("Didn't receive input from dmenu.");
}
return ret;
}
/**
* Lock and get all notifications with an action or URL.
**/
static GList *get_actionable_notifications(void)
{
GList *locked_notifications = NULL;
for (const GList *iter = queues_get_displayed(); iter;
iter = iter->next) {
struct notification *n = iter->data;
if (n->urls || g_hash_table_size(n->actions)) {
notification_lock(n);
locked_notifications = g_list_prepend(locked_notifications, n);
}
}
return locked_notifications;
}
/* see menu.h */
void context_menu(void)
{
GList *notifications = get_actionable_notifications();
context_menu_for(notifications);
}
/* see menu.h */
void context_menu_for(GList *notifications)
{
if (menu_ctx.locked_notifications) {
LOG_W("Context menu already running, refusing to rerun");
return;
}
menu_ctx.locked_notifications = notifications;
GError *err = NULL;
g_thread_unref(g_thread_try_new("dmenu",
context_menu_thread,
NULL,
&err));
if (err) {
LOG_C("Cannot start thread to call dmenu: %s", err->message);
g_error_free(err);
}
}
static gboolean context_menu_result_dispatch(gpointer user_data)
{
char *dmenu_output = (char*)user_data;
dispatch_menu_result(dmenu_output);
for (GList *iter = menu_ctx.locked_notifications; iter; iter = iter->next) {
struct notification *n = iter->data;
notification_unlock(n);
if (n->marked_for_closure) {
// Don't close notification if context was aborted
if (dmenu_output != NULL)
queues_notification_close(n, n->marked_for_closure);
n->marked_for_closure = 0;
}
}
menu_ctx.locked_notifications = NULL;
g_list_free(menu_ctx.locked_notifications);
g_free(dmenu_output);
wake_up();
return G_SOURCE_REMOVE;
}
static gpointer context_menu_thread(gpointer data)
{
char *dmenu_input = NULL;
char *dmenu_output;
for (GList *iter = menu_ctx.locked_notifications; iter; iter = iter->next) {
struct notification *n = iter->data;
char *dmenu_str = notification_dmenu_string(n);
dmenu_input = string_append(dmenu_input, dmenu_str, "\n");
g_free(dmenu_str);
if (n->urls)
dmenu_input = string_append(dmenu_input, n->urls, "\n");
}
dmenu_output = invoke_dmenu(dmenu_input);
g_timeout_add(50, context_menu_result_dispatch, dmenu_output);
g_free(dmenu_input);
return NULL;
}
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */