Add for_window --always

This allows a for_window command to trigger multiple times on the same window;
the default is to only run the command once (when it first matches).

This can be useful to apply rules to applications like VLC that hide and show a
window without re-creating it (when using the taskbar icon to hide/show).
This commit is contained in:
Daniel De Graaf 2020-09-02 22:01:49 -04:00
parent 9c30140b0a
commit 7bf8c3ead0
3 changed files with 18 additions and 2 deletions

View file

@ -45,6 +45,7 @@ struct criteria {
#endif #endif
bool floating; bool floating;
bool tiling; bool tiling;
bool always;
char urgent; // 'l' for latest or 'o' for oldest char urgent; // 'l' for latest or 'o' for oldest
struct pattern *workspace; struct pattern *workspace;
pid_t pid; pid_t pid;

View file

@ -6,6 +6,19 @@
#include "stringop.h" #include "stringop.h"
struct cmd_results *cmd_for_window(int argc, char **argv) { struct cmd_results *cmd_for_window(int argc, char **argv) {
bool always = false;
while (argc > 0) {
if (strcmp("--always", argv[0]) == 0) {
always = true;
} else if (strcmp("--once", argv[0]) == 0) {
always = false;
} else {
break;
}
argv++;
argc--;
}
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) { if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) {
return error; return error;
@ -20,6 +33,7 @@ struct cmd_results *cmd_for_window(int argc, char **argv) {
} }
criteria->type = CT_COMMAND; criteria->type = CT_COMMAND;
criteria->always = always;
criteria->cmdlist = join_args(argv + 1, argc - 1); criteria->cmdlist = join_args(argv + 1, argc - 1);
// Check if it already exists // Check if it already exists

View file

@ -488,12 +488,13 @@ void view_execute_criteria(struct sway_view *view, const char* trigger) {
for (int i = 0; i < criterias->length; i++) { for (int i = 0; i < criterias->length; i++) {
struct criteria *criteria = criterias->items[i]; struct criteria *criteria = criterias->items[i];
sway_log(SWAY_DEBUG, "Checking criteria %s", criteria->raw); sway_log(SWAY_DEBUG, "Checking criteria %s", criteria->raw);
if (view_has_executed_criteria(view, criteria)) { if (!criteria->always && view_has_executed_criteria(view, criteria)) {
sway_log(SWAY_DEBUG, "Criteria already executed"); sway_log(SWAY_DEBUG, "Criteria already executed");
continue; continue;
} }
sway_log(SWAY_DEBUG, "for_window '%s' matches view %p, cmd: '%s'", sway_log(SWAY_DEBUG, "for_window '%s' matches view %p, cmd: '%s'",
criteria->raw, view, criteria->cmdlist); criteria->raw, view, criteria->cmdlist);
if (!criteria->always)
list_add(view->executed_criteria, criteria); list_add(view->executed_criteria, criteria);
list_t *res_list = execute_command( list_t *res_list = execute_command(
criteria->cmdlist, NULL, view->container); criteria->cmdlist, NULL, view->container);