Skip to content

Commit

Permalink
Introduce modes
Browse files Browse the repository at this point in the history
Closes: #335
  • Loading branch information
emersion committed Jun 15, 2021
1 parent 445bf2d commit 36fa699
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
9 changes: 9 additions & 0 deletions criteria.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void destroy_criteria(struct mako_criteria *criteria) {
regfree(&criteria->body_pattern);
free(criteria->raw_string);
free(criteria->output);
free(criteria->mode);
free(criteria);
}

Expand Down Expand Up @@ -151,6 +152,10 @@ bool match_criteria(struct mako_criteria *criteria,
return false;
}

if (spec.mode && strcmp(criteria->mode, notif->state->current_mode) != 0) {
return false;
}

return true;
}

Expand Down Expand Up @@ -351,6 +356,10 @@ bool apply_criteria_field(struct mako_criteria *criteria, char *token) {
criteria->output = strdup(value);
criteria->spec.output = true;
return true;
} else if (strcmp(key, "mode") == 0) {
criteria->mode = strdup(value);
criteria->spec.mode = true;
return true;
} else {
// Anything left must be one of the boolean fields, defined using
// standard syntax. Continue on.
Expand Down
19 changes: 19 additions & 0 deletions dbus/mako.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,24 @@ static void reapply_config(struct mako_state *state) {
}
}

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

const char *mode;
int ret = sd_bus_message_read(msg, "s", &mode);
if (ret < 0) {
return ret;
}

free(state->current_mode);
state->current_mode = strdup(mode);

reapply_config(state);

return sd_bus_reply_method_return(msg, "");
}

static int handle_reload(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) {
struct mako_state *state = data;
Expand All @@ -328,6 +346,7 @@ static const sd_bus_vtable service_vtable[] = {
SD_BUS_METHOD("RestoreNotification", "", "", handle_restore_action, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ListNotifications", "", "aa{sv}", handle_list_notifications, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("Reload", "", "", handle_reload, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetMode", "s", "", handle_set_mode, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END
};

Expand Down
2 changes: 2 additions & 0 deletions include/criteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct mako_criteria {
char *body;
regex_t body_pattern;

char *mode;

// Second-pass matches:
int group_index;
bool grouped; // Whether group_index is non-zero
Expand Down
1 change: 1 addition & 0 deletions include/mako.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct mako_state {
uint32_t last_id;
struct wl_list notifications; // mako_notification::link
struct wl_list history; // mako_notification::link
char *current_mode;

int argc;
char **argv;
Expand Down
2 changes: 2 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct mako_criteria_spec {
bool body;
bool body_pattern;

bool mode;

bool none; // Special criteria that never matches, used for grouping

// Fields that can only be matched after grouping, and thus can't be
Expand Down
3 changes: 3 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ static bool init(struct mako_state *state) {
}
wl_list_init(&state->notifications);
wl_list_init(&state->history);
state->current_mode = strdup("default");
return true;
}

static void finish(struct mako_state *state) {
free(state->current_mode);

struct mako_notification *notif, *tmp;
wl_list_for_each_safe(notif, tmp, &state->notifications, link) {
destroy_notification(notif);
Expand Down
23 changes: 23 additions & 0 deletions mako.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ The following fields are available in criteria:
- _desktop-entry_ (string)
- _actionable_ (boolean)
- _expiring_ (boolean)
- _mode_ (string)
- Only apply style options in this section if the provided mode is
currently enabled. For more information about modes, see the _MODES_
section.

The following fields are also available to match on a second pass based on
where previous style options decided to place each notification:
Expand Down Expand Up @@ -388,6 +392,25 @@ specifiers.

*%t* Total number of notifications

# MODES

mako supports applying style options conditionally via modes. A configuration
section with a _mode_ criteria will only be applied if the current mode
matches. **makoctl**(1) can be used to change the current mode.

The default mode is named "default".

For example, to hide all notifications if the mode "dnd" (Do Not Disturb) is
enabled:

```
[mode=dnd]
invisible=1
```

_makoctl set-mode dnd_ will hide all notifications, _makoctl set-mode default_
will show them again.

# AUTHORS

Maintained by Simon Ser <[email protected]>, who is assisted by other
Expand Down
4 changes: 4 additions & 0 deletions makoctl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ usage() {
echo " notification if none is given"
echo " list List notifications"
echo " reload Reload the configuration file"
echo " set-mode <name> Switch the current mode"
echo " help Show this help"
}

Expand Down Expand Up @@ -129,6 +130,9 @@ case "$1" in
"reload")
call Reload
;;
"set-mode")
call SetMode "s" "$2"
;;
"help"|"--help"|"-h")
usage
;;
Expand Down
5 changes: 5 additions & 0 deletions makoctl.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Sends IPC commands to the running mako daemon via dbus.
*reload*
Reloads the configuration file.

*set-mode* <name>
Switches the current mode to _name_. This replaces the previous mode.

See the _MODES_ section in **mako**(5) for more information about modes.

*help, -h, --help*
Show help message and quit.

Expand Down

0 comments on commit 36fa699

Please sign in to comment.