forked from emersion/mako
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.c
343 lines (302 loc) · 8.1 KB
/
notification.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
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#ifdef __linux__
#include <linux/input-event-codes.h>
#elif __FreeBSD__
#include <dev/evdev/input-event-codes.h>
#endif
#include "config.h"
#include "dbus.h"
#include "event-loop.h"
#include "mako.h"
#include "notification.h"
bool hotspot_at(struct mako_hotspot *hotspot, int32_t x, int32_t y) {
return x >= hotspot->x &&
y >= hotspot->y &&
x < hotspot->x + hotspot->width &&
y < hotspot->y + hotspot->height;
}
struct mako_notification *create_notification(struct mako_state *state) {
struct mako_notification *notif =
calloc(1, sizeof(struct mako_notification));
if (notif == NULL) {
fprintf(stderr, "allocation failed\n");
return NULL;
}
notif->state = state;
++state->last_id;
notif->id = state->last_id;
wl_list_init(¬if->actions);
notif->urgency = MAKO_NOTIFICATION_URGENCY_UNKNOWN;
return notif;
}
void destroy_notification(struct mako_notification *notif) {
wl_list_remove(¬if->link);
struct mako_action *action, *tmp;
wl_list_for_each_safe(action, tmp, ¬if->actions, link) {
wl_list_remove(&action->link);
free(action->key);
free(action->title);
free(action);
}
destroy_timer(notif->timer);
finish_style(¬if->style);
free(notif->app_name);
free(notif->app_icon);
free(notif->summary);
free(notif->body);
free(notif->category);
free(notif->desktop_entry);
free(notif);
}
void close_notification(struct mako_notification *notif,
enum mako_notification_close_reason reason) {
notify_notification_closed(notif, reason);
destroy_notification(notif);
}
struct mako_notification *get_notification(struct mako_state *state,
uint32_t id) {
struct mako_notification *notif;
wl_list_for_each(notif, &state->notifications, link) {
if (notif->id == id) {
return notif;
}
}
return NULL;
}
void close_all_notifications(struct mako_state *state,
enum mako_notification_close_reason reason) {
struct mako_notification *notif, *tmp;
wl_list_for_each_safe(notif, tmp, &state->notifications, link) {
close_notification(notif, reason);
}
}
static size_t trim_space(char *dst, const char *src) {
size_t src_len = strlen(src);
const char *start = src;
const char *end = src + src_len;
while (start != end && isspace(start[0])) {
++start;
}
while (end != start && isspace(end[-1])) {
--end;
}
size_t trimmed_len = end - start;
memmove(dst, start, trimmed_len);
dst[trimmed_len] = '\0';
return trimmed_len;
}
static const char *escape_markup_char(char c) {
switch (c) {
case '&': return "&";
case '<': return "<";
case '>': return ">";
case '\'': return "'";
case '"': return """;
}
return NULL;
}
static size_t escape_markup(const char *s, char *buf) {
size_t len = 0;
while (s[0] != '\0') {
const char *replacement = escape_markup_char(s[0]);
if (replacement != NULL) {
size_t replacement_len = strlen(replacement);
if (buf != NULL) {
memcpy(buf + len, replacement, replacement_len);
}
len += replacement_len;
} else {
if (buf != NULL) {
buf[len] = s[0];
}
++len;
}
++s;
}
if (buf != NULL) {
buf[len] = '\0';
}
return len;
}
static char *mako_asprintf(const char *fmt, ...) {
char *text;
va_list args;
va_start(args, fmt);
int size = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (size < 0) {
return NULL;
}
text = malloc(size + 1);
if (text == NULL) {
return NULL;
}
va_start(args, fmt);
vsnprintf(text, size + 1, fmt, args);
va_end(args);
return text;
}
char *format_state_text(char variable, bool *markup, void *data) {
struct mako_state *state = data;
switch (variable) {
case 'h':;
int hidden = wl_list_length(&state->notifications) - state->config.max_visible;
return mako_asprintf("%d", hidden);
case 't':;
int count = wl_list_length(&state->notifications);
return mako_asprintf("%d", count);
}
return NULL;
}
char *format_notif_text(char variable, bool *markup, void *data) {
struct mako_notification *notif = data;
switch (variable) {
case 'a':
return strdup(notif->app_name);
case 's':
return strdup(notif->summary);
case 'b':
*markup = true;
return strdup(notif->body);
}
return NULL;
}
size_t format_text(const char *format, char *buf, mako_format_func_t format_func, void *data) {
size_t len = 0;
const char *last = format;
while (1) {
char *current = strchr(last, '%');
if (current == NULL || current[1] == '\0') {
size_t tail_len = strlen(last);
if (buf != NULL) {
memcpy(buf + len, last, tail_len + 1);
}
len += tail_len;
break;
}
size_t chunk_len = current - last;
if (buf != NULL) {
memcpy(buf + len, last, chunk_len);
}
len += chunk_len;
char *value = NULL;
bool markup = false;
if (current[1] == '%') {
value = strdup("%");
} else {
value = format_func(current[1], &markup, data);
}
if (value == NULL) {
value = strdup("");
}
size_t value_len;
if (!markup) {
char *escaped = NULL;
if (buf != NULL) {
escaped = buf + len;
}
value_len = escape_markup(value, escaped);
} else {
value_len = strlen(value);
if (buf != NULL) {
memcpy(buf + len, value, value_len);
}
}
free(value);
len += value_len;
last = current + 2;
}
if (buf != NULL) {
trim_space(buf, buf);
}
return len;
}
static enum mako_button_binding get_button_binding(struct mako_config *config,
uint32_t button) {
switch (button) {
case BTN_LEFT:
return config->button_bindings.left;
case BTN_RIGHT:
return config->button_bindings.right;
case BTN_MIDDLE:
return config->button_bindings.middle;
}
return MAKO_BUTTON_BINDING_NONE;
}
void notification_handle_button(struct mako_notification *notif, uint32_t button,
enum wl_pointer_button_state state) {
if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
return;
}
switch (get_button_binding(¬if->state->config, button)) {
case MAKO_BUTTON_BINDING_NONE:
break;
case MAKO_BUTTON_BINDING_DISMISS:
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED);
break;
case MAKO_BUTTON_BINDING_DISMISS_ALL:
close_all_notifications(notif->state, MAKO_NOTIFICATION_CLOSE_DISMISSED);
break;
case MAKO_BUTTON_BINDING_INVOKE_DEFAULT_ACTION:;
struct mako_action *action;
wl_list_for_each(action, ¬if->actions, link) {
if (strcmp(action->key, DEFAULT_ACTION_KEY) == 0) {
notify_action_invoked(action);
break;
}
}
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED);
break;
}
}
/*
* Searches through the notifications list and returns the next position at
* which to insert. If no results for the specified urgency are found,
* it will return the closest link searching in the direction specifed.
* (-1 for lower, 1 or upper).
*/
static struct wl_list *get_last_notif_by_urgency(struct wl_list *notifications,
enum mako_notification_urgency urgency, int direction) {
enum mako_notification_urgency current = urgency;
if (wl_list_empty(notifications)) {
return notifications;
}
while (current <= MAKO_NOTIFICATION_URGENCY_HIGH &&
current >= MAKO_NOTIFICATION_URGENCY_UNKNOWN) {
struct mako_notification *notif;
wl_list_for_each_reverse(notif, notifications, link) {
if (notif->urgency == current) {
return ¬if->link;
}
}
current += direction;
}
return notifications;
}
void insert_notification(struct mako_state *state, struct mako_notification *notif) {
struct mako_config *config = &state->config;
struct wl_list *insert_node;
if (config->sort_criteria == MAKO_SORT_CRITERIA_TIME &&
!(config->sort_asc & MAKO_SORT_CRITERIA_TIME)) {
insert_node = &state->notifications;
} else if (config->sort_criteria == MAKO_SORT_CRITERIA_TIME &&
(config->sort_asc & MAKO_SORT_CRITERIA_TIME)) {
insert_node = state->notifications.prev;
} else if (config->sort_criteria & MAKO_SORT_CRITERIA_URGENCY) {
int direction = (config->sort_asc & MAKO_SORT_CRITERIA_URGENCY) ? -1 : 1;
int offset = 0;
if (!(config->sort_asc & MAKO_SORT_CRITERIA_TIME)) {
offset = direction;
}
insert_node = get_last_notif_by_urgency(&state->notifications,
notif->urgency + offset, direction);
} else {
insert_node = &state->notifications;
}
wl_list_insert(insert_node, ¬if->link);
}