Merge pull request #1149 from 4e554c4c/criteria

Improve criteria handling
master
Drew DeVault 8 years ago committed by GitHub
commit 3f40b61321

@ -5,6 +5,9 @@
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include "config.h" #include "config.h"
// Container that a called command should act upon. Only valid in command functions.
extern swayc_t *current_container;
/** /**
* Indicates the result of a command's execution. * Indicates the result of a command's execution.
*/ */

@ -33,4 +33,7 @@ char *extract_crit_tokens(list_t *tokens, const char *criteria);
// been set with `for_window` commands and have an associated cmdlist. // been set with `for_window` commands and have an associated cmdlist.
list_t *criteria_for(swayc_t *cont); list_t *criteria_for(swayc_t *cont);
// Returns a list of all containers that match the given list of tokens.
list_t *container_for(list_t *tokens);
#endif #endif

@ -43,6 +43,8 @@ struct cmd_handler {
int sp_index = 0; int sp_index = 0;
swayc_t *current_container = NULL;
// Returns error object, or NULL if check succeeds. // Returns error object, or NULL if check succeeds.
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
@ -371,42 +373,37 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
char *head = exec; char *head = exec;
char *cmdlist; char *cmdlist;
char *cmd; char *cmd;
char *criteria __attribute__((unused)); list_t *containers = NULL;
head = exec; head = exec;
do { do {
// Extract criteria (valid for this command list only). // Extract criteria (valid for this command list only).
criteria = NULL;
if (*head == '[') { if (*head == '[') {
++head; ++head;
criteria = argsep(&head, "]"); char *criteria_string = argsep(&head, "]");
if (head) { if (head) {
++head; ++head;
// TODO handle criteria list_t *tokens = create_list();
char *error;
if ((error = extract_crit_tokens(tokens, criteria_string))) {
results = cmd_results_new(CMD_INVALID, criteria_string,
"Can't parse criteria string: %s", error);
free(error);
free(tokens);
goto cleanup;
}
containers = container_for(tokens);
free(tokens);
} else { } else {
if (!results) { if (!results) {
results = cmd_results_new(CMD_INVALID, criteria, "Unmatched ["); results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched [");
} }
goto cleanup; goto cleanup;
} }
// Skip leading whitespace // Skip leading whitespace
head += strspn(head, whitespace); head += strspn(head, whitespace);
// TODO: it will yield unexpected results to execute commands
// (on any view) that where meant for certain views only.
if (!results) {
int len = strlen(criteria) + strlen(head) + 4;
char *tmp = malloc(len);
if (tmp) {
snprintf(tmp, len, "[%s] %s", criteria, head);
} else {
sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result");
}
results = cmd_results_new(CMD_INVALID, tmp,
"Can't handle criteria string: Refusing to execute command");
free(tmp);
}
goto cleanup;
} }
// Split command list // Split command list
cmdlist = argsep(&head, ";"); cmdlist = argsep(&head, ";");
@ -450,21 +447,43 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
free_argv(argc, argv); free_argv(argc, argv);
goto cleanup; goto cleanup;
} }
struct cmd_results *res = handler->handle(argc-1, argv+1); int i = 0;
if (res->status != CMD_SUCCESS) { do {
free_argv(argc, argv); if (!containers) {
if (results) { current_container = get_focused_container(&root_container);
free_cmd_results(results); } else if (containers->length == 0) {
break;
} else {
current_container = (swayc_t *)containers->items[i];
} }
results = res; sway_log(L_INFO, "Running on container '%s'", current_container->name);
goto cleanup;
} struct cmd_results *res = handler->handle(argc-1, argv+1);
if (res->status != CMD_SUCCESS) {
free_argv(argc, argv);
if (results) {
free_cmd_results(results);
}
results = res;
goto cleanup;
}
free_cmd_results(res);
++i;
} while(containers && i < containers->length);
free_argv(argc, argv); free_argv(argc, argv);
free_cmd_results(res);
} while(cmdlist); } while(cmdlist);
if (containers) {
list_free(containers);
containers = NULL;
}
} while(head); } while(head);
cleanup: cleanup:
free(exec); free(exec);
if (containers) {
free(containers);
}
if (!results) { if (!results) {
results = cmd_results_new(CMD_SUCCESS, NULL, NULL); results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

@ -20,7 +20,7 @@ struct cmd_results *cmd_border(int argc, char **argv) {
"Expected 'border <normal|pixel|none|toggle> [<n>]"); "Expected 'border <normal|pixel|none|toggle> [<n>]");
} }
swayc_t *view = get_focused_view(&root_container); swayc_t *view = current_container;
enum swayc_border_types border = view->border_type; enum swayc_border_types border = view->border_type;
int thickness = view->border_thickness; int thickness = view->border_thickness;

@ -13,7 +13,7 @@ struct cmd_results *cmd_floating(int argc, char **argv) {
if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = current_container;
bool wants_floating; bool wants_floating;
if (strcasecmp(argv[0], "enable") == 0) { if (strcasecmp(argv[0], "enable") == 0) {
wants_floating = true; wants_floating = true;

@ -30,6 +30,9 @@ struct cmd_results *cmd_focus(int argc, char **argv) {
} }
} }
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} else if (argc == 0) {
set_focused_container(current_container);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) { } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }

@ -14,7 +14,7 @@ struct cmd_results *cmd_fullscreen(int argc, char **argv) {
if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) { if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) {
return error; return error;
} }
swayc_t *container = get_focused_view(&root_container); swayc_t *container = current_container;
if(container->type != C_VIEW){ if(container->type != C_VIEW){
return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen"); return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen");
} }

@ -6,7 +6,7 @@ struct cmd_results *cmd_kill(int argc, char **argv) {
if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file."); if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file.");
if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running."); if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running.");
swayc_t *container = get_focused_container(&root_container); swayc_t *container = current_container;
close_views(container); close_views(container);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

@ -16,7 +16,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) {
if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
return error; return error;
} }
swayc_t *parent = get_focused_container(&root_container); swayc_t *parent = current_container;
if (parent->is_floating) { if (parent->is_floating) {
return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows");
} }

@ -8,11 +8,11 @@
struct cmd_results *cmd_mark(int argc, char **argv) { struct cmd_results *cmd_mark(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file.");
if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = current_container;
bool add = false; bool add = false;
bool toggle = false; bool toggle = false;

@ -20,7 +20,7 @@ struct cmd_results *cmd_move(int argc, char **argv) {
"'move <container|window> to workspace <name>' or " "'move <container|window> to workspace <name>' or "
"'move <container|window|workspace> to output <name|direction>' or " "'move <container|window|workspace> to output <name|direction>' or "
"'move position mouse'"; "'move position mouse'";
swayc_t *view = get_focused_container(&root_container); swayc_t *view = current_container;
if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0 )) { if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0 )) {
char *inv; char *inv;
@ -125,7 +125,7 @@ struct cmd_results *cmd_move(int argc, char **argv) {
if (view->type != C_CONTAINER && view->type != C_VIEW) { if (view->type != C_CONTAINER && view->type != C_VIEW) {
return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views."); return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = current_container;
int i; int i;
for (i = 0; i < scratchpad->length; i++) { for (i = 0; i < scratchpad->length; i++) {
if (scratchpad->items[i] == view) { if (scratchpad->items[i] == view) {

@ -19,7 +19,7 @@ enum resize_dim_types {
}; };
static bool set_size_floating(int new_dimension, bool use_width) { static bool set_size_floating(int new_dimension, bool use_width) {
swayc_t *view = get_focused_float(swayc_active_workspace()); swayc_t *view = current_container;
if (view) { if (view) {
if (use_width) { if (use_width) {
int current_width = view->width; int current_width = view->width;
@ -50,7 +50,7 @@ static bool set_size_floating(int new_dimension, bool use_width) {
} }
static bool resize_floating(int amount, bool use_width) { static bool resize_floating(int amount, bool use_width) {
swayc_t *view = get_focused_float(swayc_active_workspace()); swayc_t *view = current_container;
if (view) { if (view) {
if (use_width) { if (use_width) {
@ -64,7 +64,7 @@ static bool resize_floating(int amount, bool use_width) {
} }
static bool resize_tiled(int amount, bool use_width) { static bool resize_tiled(int amount, bool use_width) {
swayc_t *container = get_focused_view(swayc_active_workspace()); swayc_t *container = current_container;
swayc_t *parent = container->parent; swayc_t *parent = container->parent;
int idx_focused = 0; int idx_focused = 0;
bool use_major = false; bool use_major = false;
@ -199,7 +199,7 @@ static bool resize_tiled(int amount, bool use_width) {
static bool set_size_tiled(int amount, bool use_width) { static bool set_size_tiled(int amount, bool use_width) {
int desired; int desired;
swayc_t *focused = get_focused_view(swayc_active_workspace()); swayc_t *focused = current_container;
if (use_width) { if (use_width) {
desired = amount - focused->width; desired = amount - focused->width;
@ -211,7 +211,7 @@ static bool set_size_tiled(int amount, bool use_width) {
} }
static bool set_size(int dimension, bool use_width) { static bool set_size(int dimension, bool use_width) {
swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace()); swayc_t *focused = current_container;
if (focused) { if (focused) {
if (focused->is_floating) { if (focused->is_floating) {
@ -225,7 +225,7 @@ static bool set_size(int dimension, bool use_width) {
} }
static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) { static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) {
swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace()); swayc_t *focused = current_container;
// translate "10 ppt" (10%) to appropriate # of pixels in case we need it // translate "10 ppt" (10%) to appropriate # of pixels in case we need it
float ppt_dim = (float)dimension / 100; float ppt_dim = (float)dimension / 100;

@ -17,7 +17,7 @@ static struct cmd_results *_do_split(int argc, char **argv, int layout) {
if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) {
return error; return error;
} }
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = current_container;
// Case of floating window, don't split // Case of floating window, don't split
if (focused->is_floating) { if (focused->is_floating) {
@ -66,7 +66,7 @@ struct cmd_results *cmd_split(int argc, char **argv) {
} else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) { } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) {
_do_split(argc - 1, argv + 1, L_HORIZ); _do_split(argc - 1, argv + 1, L_HORIZ);
} else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) { } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) {
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = current_container;
if (focused->parent->layout == L_VERT) { if (focused->parent->layout == L_VERT) {
_do_split(argc - 1, argv + 1, L_HORIZ); _do_split(argc - 1, argv + 1, L_HORIZ);
} else { } else {
@ -89,7 +89,7 @@ struct cmd_results *cmd_splith(int argc, char **argv) {
} }
struct cmd_results *cmd_splitt(int argc, char **argv) { struct cmd_results *cmd_splitt(int argc, char **argv) {
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = current_container;
if (focused->parent->layout == L_VERT) { if (focused->parent->layout == L_VERT) {
return _do_split(argc, argv, L_HORIZ); return _do_split(argc, argv, L_HORIZ);
} else { } else {

@ -5,7 +5,7 @@
#include "stringop.h" #include "stringop.h"
struct cmd_results *cmd_unmark(int argc, char **argv) { struct cmd_results *cmd_unmark(int argc, char **argv) {
swayc_t *view = get_focused_container(&root_container); swayc_t *view = current_container;
if (view->marks) { if (view->marks) {
if (argc) { if (argc) {

@ -245,7 +245,7 @@ ect_cleanup:
return error; return error;
} }
int regex_cmp(const char *item, const regex_t *regex) { static int regex_cmp(const char *item, const regex_t *regex) {
return regexec(regex, item, 0, NULL, 0); return regexec(regex, item, 0, NULL, 0);
} }
@ -272,7 +272,10 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
break; break;
case CRIT_CON_MARK: case CRIT_CON_MARK:
if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) {
++matches; // Make sure it isn't matching the NUL string
if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) {
++matches;
}
} }
break; break;
case CRIT_ID: case CRIT_ID:
@ -285,7 +288,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
case CRIT_INSTANCE: case CRIT_INSTANCE:
if (!cont->instance) { if (!cont->instance) {
// ignore // ignore
} else if (strcmp(crit->raw, "focused") == 0) { } else if (crit_is_focused(crit->raw)) {
swayc_t *focused = get_focused_view(&root_container); swayc_t *focused = get_focused_view(&root_container);
if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { if (focused->instance && strcmp(cont->instance, focused->instance) == 0) {
matches++; matches++;
@ -373,3 +376,21 @@ list_t *criteria_for(swayc_t *cont) {
} }
return matches; return matches;
} }
struct list_tokens {
list_t *list;
list_t *tokens;
};
static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) {
if (criteria_test(container, list_tokens->tokens)) {
list_add(list_tokens->list, container);
}
}
list_t *container_for(list_t *tokens) {
struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens};
container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens);
return list_tokens.list;
}

@ -316,7 +316,7 @@ The default colors are:
If smart_gaps are _on_ then gaps will only be enabled if a workspace has more If smart_gaps are _on_ then gaps will only be enabled if a workspace has more
than one child container. than one child container.
**mark** <--add|--replace> <--toggle> <identifier>:: **mark** \<--add|--replace> \<--toggle> <identifier>::
Marks are arbitrary labels that can be used to identify certain windows and Marks are arbitrary labels that can be used to identify certain windows and
then jump to them at a later time. By default, the **mark** command sets then jump to them at a later time. By default, the **mark** command sets
_identifier_ as the only mark on a window. By specifying _--add_, mark will _identifier_ as the only mark on a window. By specifying _--add_, mark will
@ -426,6 +426,20 @@ The string contains one or more (space separated) attribute/value pairs and they
are used by some commands filter which views to execute actions on. All attributes are used by some commands filter which views to execute actions on. All attributes
must match for the criteria string to match. must match for the criteria string to match.
Criteria may be used with either the **for_window** or **assign** commands to
specify operations to perform on new views. A criteria may also be used to
perform specific commands (ones that normally act upon one window) on all views
that match that criteria. For example:
Focus on a window with the mark "IRC":
[con_mark="IRC"] focus
Kill all windows with the title "Emacs":
[class="Emacs"] kill
Mark all Firefox windows with "Browser":
[class="Firefox"] mark Browser
Currently supported attributes: Currently supported attributes:
**class**:: **class**::

Loading…
Cancel
Save