Skip to content

Commit

Permalink
feat: implement makoctl count with variants
Browse files Browse the repository at this point in the history
  • Loading branch information
fidelicura committed Jul 23, 2024
1 parent 4d48aca commit 534b006
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
39 changes: 39 additions & 0 deletions dbus/mako.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@
static const char *service_path = "/fr/emersion/Mako";
static const char *service_interface = "fr.emersion.Mako";

static int handle_count_notifications(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) {
struct mako_state *state = data;

const char *notif_type;
int ret = sd_bus_message_read(msg, "s", &notif_type);

sd_bus_message *reply = NULL;
ret = sd_bus_message_new_method_return(msg, &reply);
if (ret < 0) {
return ret;
}

uint32_t len;
if (strncmp(notif_type, "waiting", 7) == 0) {
len = wl_list_length(&state->notifications);
} else if (strncmp(notif_type, "history", 7) == 0) {
len = wl_list_length(&state->history);
} else {
// UNREACHABLE: we filter out invalid
// values on the `makoctl` script side.
return -1;
}

ret = sd_bus_message_append_basic(reply, 'u', &len);
if (ret < 0) {
return ret;
}

ret = sd_bus_send(NULL, reply, NULL);
if (ret < 0) {
return ret;
}

sd_bus_message_unref(reply);
return 0;
}

static int handle_dismiss_all_notifications(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) {
struct mako_state *state = data;
Expand Down Expand Up @@ -435,6 +473,7 @@ static int handle_set_modes(sd_bus_message *msg, void *data,

static const sd_bus_vtable service_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("CountNotifications", "s", "u", handle_count_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("DismissAllNotifications", "", "", handle_dismiss_all_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("DismissGroupNotifications", "", "", handle_dismiss_group_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("DismissLastNotification", "", "", handle_dismiss_last_notification, SD_BUS_VTABLE_UNPRIVILEGED),
Expand Down
15 changes: 15 additions & 0 deletions makoctl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ usage() {
echo "Usage: makoctl <command> [options...]"
echo ""
echo "Commands:"
echo " count [waiting|history] Show the number of notifications"
echo " dismiss [-n id] Dismiss the notification with the"
echo " given id, or the last notification"
echo " if none is given"
Expand Down Expand Up @@ -56,6 +57,20 @@ if [ $# -eq 0 ]; then
fi

case "$1" in
"count")
if [ $# -gt 1 ]; then
case "$2" in
waiting|history)
call CountNotifications "s" "$2"
;;
*)
echo >&2 "$0: Expected 'waiting' or 'history' after 'count'"
;;
esac
else
echo >&2 "$0: Expected 'waiting' or 'history' after 'count'"
fi
;;
"dismiss")
all=0
group=0
Expand Down

0 comments on commit 534b006

Please sign in to comment.