Skip to content

Commit 91a1fc4

Browse files
qiu-xemersion
authored andcommitted
dbus: unify Dismiss* methods
Unify DismissAllNotifications, DismissGroupNotifications, DismissLastNotification, DismissNotification under one Dismiss method that takes a Dict as input.
1 parent 5321a4c commit 91a1fc4

File tree

5 files changed

+121
-70
lines changed

5 files changed

+121
-70
lines changed

dbus/mako.c

+49-56
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,72 @@
1414
static const char *service_path = "/fr/emersion/Mako";
1515
static const char *service_interface = "fr.emersion.Mako";
1616

17-
static int handle_dismiss_all_notifications(sd_bus_message *msg, void *data,
17+
static int handle_dismiss(sd_bus_message *msg, void *data,
1818
sd_bus_error *ret_error) {
1919
struct mako_state *state = data;
2020

21-
close_all_notifications(state, MAKO_NOTIFICATION_CLOSE_DISMISSED);
22-
struct mako_surface *surface;
23-
24-
wl_list_for_each(surface, &state->surfaces, link) {
25-
set_dirty(surface);
26-
}
27-
28-
return sd_bus_reply_method_return(msg, "");
29-
}
30-
31-
static int handle_dismiss_group_notifications(sd_bus_message *msg, void *data,
32-
sd_bus_error *ret_error) {
33-
struct mako_state *state = data;
21+
uint32_t id = 0;
22+
int group = 0;
23+
int all = 0;
24+
int history = 1; // Keep history be default
3425

35-
if (wl_list_empty(&state->notifications)) {
36-
goto done;
26+
int ret = sd_bus_message_enter_container(msg, 'a', "{sv}");
27+
if (ret < 0) {
28+
return ret;
3729
}
3830

39-
struct mako_notification *notif =
40-
wl_container_of(state->notifications.next, notif, link);
41-
42-
struct mako_surface *surface = notif->surface;
43-
close_group_notifications(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED);
44-
set_dirty(surface);
31+
while (true) {
32+
ret = sd_bus_message_enter_container(msg, 'e', "sv");
33+
if (ret < 0) {
34+
return ret;
35+
} else if (ret == 0) {
36+
break;
37+
}
4538

46-
done:
47-
return sd_bus_reply_method_return(msg, "");
48-
}
39+
const char *key = NULL;
40+
ret = sd_bus_message_read(msg, "s", &key);
41+
if (ret < 0) {
42+
return ret;
43+
}
4944

50-
static int handle_dismiss_last_notification(sd_bus_message *msg, void *data,
51-
sd_bus_error *ret_error) {
52-
struct mako_state *state = data;
45+
if (strcmp(key, "id") == 0) {
46+
ret = sd_bus_message_read(msg, "v", "u", &id);
47+
} else if (strcmp(key, "group") == 0) {
48+
ret = sd_bus_message_read(msg, "v", "b", &group);
49+
} else if (strcmp(key, "history") == 0) {
50+
ret = sd_bus_message_read(msg, "v", "b", &history);
51+
} else if (strcmp(key, "all") == 0) {
52+
ret = sd_bus_message_read(msg, "v", "b", &all);
53+
} else {
54+
ret = sd_bus_message_skip(msg, "v");
55+
}
56+
if (ret < 0) {
57+
return ret;
58+
}
5359

54-
if (wl_list_empty(&state->notifications)) {
55-
goto done;
60+
ret = sd_bus_message_exit_container(msg);
61+
if (ret < 0) {
62+
return ret;
63+
}
5664
}
5765

58-
struct mako_notification *notif =
59-
wl_container_of(state->notifications.next, notif, link);
60-
struct mako_surface *surface = notif->surface;
61-
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, true);
62-
set_dirty(surface);
63-
64-
done:
65-
return sd_bus_reply_method_return(msg, "");
66-
}
67-
68-
static int handle_dismiss_notification(sd_bus_message *msg, void *data,
69-
sd_bus_error *ret_error) {
70-
struct mako_state *state = data;
71-
72-
uint32_t id = 0;
73-
int dismiss_group = 0;
74-
int ret = sd_bus_message_read(msg, "ub", &id, &dismiss_group);
75-
if (ret < 0) {
76-
return ret;
66+
// These don't make sense together
67+
if (all && group) {
68+
return -EINVAL;
69+
} else if ((all || group) && id != 0) {
70+
return -EINVAL;
7771
}
7872

7973
struct mako_notification *notif;
8074
wl_list_for_each(notif, &state->notifications, link) {
8175
if (notif->id == id || id == 0) {
8276
struct mako_surface *surface = notif->surface;
83-
if (dismiss_group) {
84-
close_group_notifications(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED);
77+
if (group) {
78+
close_group_notifications(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, history);
79+
} else if (all) {
80+
close_all_notifications(state, MAKO_NOTIFICATION_CLOSE_DISMISSED, history);
8581
} else {
86-
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, true);
82+
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, history);
8783
}
8884
set_dirty(surface);
8985
break;
@@ -468,10 +464,7 @@ void emit_modes_changed(struct mako_state *state) {
468464

469465
static const sd_bus_vtable service_vtable[] = {
470466
SD_BUS_VTABLE_START(0),
471-
SD_BUS_METHOD("DismissAllNotifications", "", "", handle_dismiss_all_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
472-
SD_BUS_METHOD("DismissGroupNotifications", "", "", handle_dismiss_group_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
473-
SD_BUS_METHOD("DismissLastNotification", "", "", handle_dismiss_last_notification, SD_BUS_VTABLE_UNPRIVILEGED),
474-
SD_BUS_METHOD("DismissNotification", "ub", "", handle_dismiss_notification, SD_BUS_VTABLE_UNPRIVILEGED),
467+
SD_BUS_METHOD("DismissNotifications", "a{sv}", "", handle_dismiss, SD_BUS_VTABLE_UNPRIVILEGED),
475468
SD_BUS_METHOD("InvokeAction", "us", "", handle_invoke_action, SD_BUS_VTABLE_UNPRIVILEGED),
476469
SD_BUS_METHOD("RestoreNotification", "", "", handle_restore_action, SD_BUS_VTABLE_UNPRIVILEGED),
477470
SD_BUS_METHOD("ListNotifications", "", "aa{sv}", handle_list_notifications, SD_BUS_VTABLE_UNPRIVILEGED),

doc/makoctl.1.scd

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Sends IPC commands to the running mako daemon via dbus.
2525
*-g, --group*
2626
Dismiss the first notification group.
2727

28+
*-h, --no-history*
29+
Dismiss the current notification without adding it to history.
30+
2831
*-n* <id>
2932
Dismiss the notification with the given id. Defaults to the first
3033
notification.

include/notification.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ void close_notification(struct mako_notification *notif,
8888
enum mako_notification_close_reason reason,
8989
bool add_to_history);
9090
void close_group_notifications(struct mako_notification *notif,
91-
enum mako_notification_close_reason reason);
91+
enum mako_notification_close_reason reason, bool add_to_history);
9292
void close_all_notifications(struct mako_state *state,
93-
enum mako_notification_close_reason reason);
93+
enum mako_notification_close_reason reason, bool add_to_history);
9494
char *format_hidden_text(char variable, bool *markup, void *data);
9595
char *format_notif_text(char variable, bool *markup, void *data);
9696
size_t format_text(const char *format, char *buf, mako_format_func_t func, void *data);

makoctl.c

+58-5
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ static int run_dismiss(sd_bus *bus, int argc, char *argv[]) {
8484
uint32_t id = 0;
8585
bool group = false;
8686
bool all = false;
87+
bool no_history = false;
8788
while (true) {
8889
const struct option options[] = {
8990
{ "all", no_argument, 0, 'a' },
9091
{ "group", no_argument, 0, 'g' },
92+
{ "no-history", no_argument, 0, 'h' },
9193
{0},
9294
};
93-
int opt = getopt_long(argc, argv, "agn:", options, NULL);
95+
int opt = getopt_long(argc, argv, "aghn:", options, NULL);
9496
if (opt == -1) {
9597
break;
9698
}
@@ -109,16 +111,66 @@ static int run_dismiss(sd_bus *bus, int argc, char *argv[]) {
109111
return 1;
110112
}
111113
break;
114+
case 'h':;
115+
no_history = true;
116+
break;
112117
default:
113118
return -EINVAL;
114119
}
115120
}
116121

117-
if (all) {
118-
return call_method(bus, "DismissAllNotifications", NULL, "");
119-
} else {
120-
return call_method(bus, "DismissNotification", NULL, "ub", id, (int)group);
122+
if (all && group) {
123+
fprintf(stderr, "-a and -g cannot be used together\n");
124+
return -EINVAL;
125+
} else if ((all || group) && id != 0) {
126+
fprintf(stderr, "-n cannot be used with -a or -g\n");
127+
return -EINVAL;
128+
}
129+
130+
char types[6] = "a{sv}";
131+
132+
sd_bus_message *msg = NULL;
133+
int ret = new_method_call(bus, &msg, "DismissNotifications");
134+
if (ret < 0) {
135+
return ret;
136+
}
137+
138+
ret = sd_bus_message_open_container(msg, 'a', "{sv}");
139+
if (ret < 0) {
140+
return ret;
141+
}
142+
143+
ret = sd_bus_message_append(msg, "{sv}", "id", "u", id);
144+
if (ret < 0) {
145+
return ret;
146+
}
147+
148+
ret = sd_bus_message_append(msg, "{sv}", "group", "b", (int)group);
149+
if (ret < 0) {
150+
return ret;
151+
}
152+
153+
int history = !no_history;
154+
ret = sd_bus_message_append(msg, "{sv}", "history", "b", (int)history);
155+
if (ret < 0) {
156+
return ret;
157+
}
158+
159+
ret = sd_bus_message_append(msg, "{sv}", "all", "b", (int)all);
160+
if (ret < 0) {
161+
return ret;
121162
}
163+
164+
ret = sd_bus_message_close_container(msg);
165+
if (ret < 0) {
166+
return ret;
167+
}
168+
169+
ret = call(bus, msg, NULL);
170+
sd_bus_message_unref(msg);
171+
return ret;
172+
173+
return call_method(bus, "DismissNotifications", NULL, types, &msg);
122174
}
123175

124176
static int run_invoke(sd_bus *bus, int argc, char *argv[]) {
@@ -740,6 +792,7 @@ static const char usage[] =
740792
" if none is given\n"
741793
" [-a|--all] Dismiss all notifications\n"
742794
" [-g|--group] Dismiss all the notifications\n"
795+
" [-h|--no-history] Dismiss w/o adding to history\n"
743796
" in the last notification's group\n"
744797
" restore Restore the most recently expired\n"
745798
" notification from the history buffer\n"

notification.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ struct mako_notification *get_tagged_notification(struct mako_state *state,
167167
}
168168

169169
void close_group_notifications(struct mako_notification *top_notif,
170-
enum mako_notification_close_reason reason) {
170+
enum mako_notification_close_reason reason,
171+
bool add_to_history) {
171172
struct mako_state *state = top_notif->state;
172173

173174
if (top_notif->style.group_criteria_spec.none) {
174175
// No grouping, just close the notification
175-
close_notification(top_notif, reason, true);
176+
close_notification(top_notif, reason, add_to_history);
176177
return;
177178
}
178179

@@ -182,18 +183,19 @@ void close_group_notifications(struct mako_notification *top_notif,
182183
struct mako_notification *notif, *tmp;
183184
wl_list_for_each_safe(notif, tmp, &state->notifications, link) {
184185
if (match_criteria(notif_criteria, notif)) {
185-
close_notification(notif, reason, true);
186+
close_notification(notif, reason, add_to_history);
186187
}
187188
}
188189

189190
destroy_criteria(notif_criteria);
190191
}
191192

192193
void close_all_notifications(struct mako_state *state,
193-
enum mako_notification_close_reason reason) {
194+
enum mako_notification_close_reason reason,
195+
bool add_to_history) {
194196
struct mako_notification *notif, *tmp;
195197
wl_list_for_each_safe(notif, tmp, &state->notifications, link) {
196-
close_notification(notif, reason, true);
198+
close_notification(notif, reason, add_to_history);
197199
}
198200
}
199201

@@ -385,10 +387,10 @@ void notification_execute_binding(struct mako_notification *notif,
385387
close_notification(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, false);
386388
break;
387389
case MAKO_BINDING_DISMISS_GROUP:
388-
close_group_notifications(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED);
390+
close_group_notifications(notif, MAKO_NOTIFICATION_CLOSE_DISMISSED, true);
389391
break;
390392
case MAKO_BINDING_DISMISS_ALL:
391-
close_all_notifications(notif->state, MAKO_NOTIFICATION_CLOSE_DISMISSED);
393+
close_all_notifications(notif->state, MAKO_NOTIFICATION_CLOSE_DISMISSED, true);
392394
break;
393395
case MAKO_BINDING_INVOKE_ACTION:
394396
assert(binding->action_name != NULL);

0 commit comments

Comments
 (0)