swayfx/sway/criteria.c

446 lines
11 KiB
C
Raw Normal View History

2018-01-21 08:21:45 +11:00
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <pcre.h>
#include "sway/criteria.h"
#include "sway/tree/container.h"
2018-01-21 08:21:45 +11:00
#include "sway/config.h"
#include "sway/tree/view.h"
2018-01-21 08:21:45 +11:00
#include "stringop.h"
#include "list.h"
#include "log.h"
enum criteria_type { // *must* keep in sync with criteria_strings[]
CRIT_APP_ID,
CRIT_CLASS,
CRIT_CON_ID,
CRIT_CON_MARK,
CRIT_FLOATING,
CRIT_ID,
CRIT_INSTANCE,
CRIT_TILING,
CRIT_TITLE,
CRIT_URGENT,
CRIT_WINDOW_ROLE,
CRIT_WINDOW_TYPE,
CRIT_WORKSPACE,
CRIT_LAST
};
2018-01-21 08:21:45 +11:00
static const char * const criteria_strings[CRIT_LAST] = {
[CRIT_APP_ID] = "app_id",
[CRIT_CLASS] = "class",
[CRIT_CON_ID] = "con_id",
[CRIT_CON_MARK] = "con_mark",
[CRIT_FLOATING] = "floating",
[CRIT_ID] = "id",
[CRIT_INSTANCE] = "instance",
[CRIT_TILING] = "tiling",
[CRIT_TITLE] = "title",
[CRIT_URGENT] = "urgent", // either "latest" or "oldest" ...
[CRIT_WINDOW_ROLE] = "window_role",
[CRIT_WINDOW_TYPE] = "window_type",
[CRIT_WORKSPACE] = "workspace"
};
2018-01-21 08:21:45 +11:00
/**
* A single criteria token (ie. value/regex pair),
* e.g. 'class="some class regex"'.
*/
struct crit_token {
enum criteria_type type;
pcre *regex;
char *raw;
};
2018-01-21 08:21:45 +11:00
static void free_crit_token(struct crit_token *crit) {
pcre_free(crit->regex);
free(crit->raw);
free(crit);
}
2018-01-21 08:21:45 +11:00
static void free_crit_tokens(list_t *crit_tokens) {
for (int i = 0; i < crit_tokens->length; i++) {
free_crit_token(crit_tokens->items[i]);
2018-01-21 08:21:45 +11:00
}
list_free(crit_tokens);
}
2018-01-21 08:21:45 +11:00
// Extracts criteria string from its brackets. Returns new (duplicate)
// substring.
static char *criteria_from(const char *arg) {
char *criteria = NULL;
if (*arg == '[') {
criteria = strdup(arg + 1);
} else {
criteria = strdup(arg);
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
int last = strlen(criteria) - 1;
if (criteria[last] == ']') {
criteria[last] = '\0';
2018-01-21 08:21:45 +11:00
}
return criteria;
}
2018-01-21 08:21:45 +11:00
// Return instances of c found in str.
static int countchr(char *str, char c) {
int found = 0;
for (int i = 0; str[i]; i++) {
if (str[i] == c) {
++found;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
2018-01-21 08:21:45 +11:00
}
return found;
}
2018-01-21 08:21:45 +11:00
// criteria_str is e.g. '[class="some class regex" instance="instance name"]'.
//
// Will create array of pointers in buf, where first is duplicate of given
// string (must be freed) and the rest are pointers to names and values in the
// base string (every other, naturally). argc will be populated with the length
// of buf.
//
// Returns error string or NULL if successful.
static char *crit_tokens(int *argc, char ***buf,
const char * const criteria_str) {
wlr_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str);
char *base = criteria_from(criteria_str);
char *head = base;
char *namep = head; // start of criteria name
char *valp = NULL; // start of value
// We're going to place EOS markers where we need to and fill up an array
// of pointers to the start of each token (either name or value).
int pairs = countchr(base, '=');
int max_tokens = pairs * 2 + 1; // this gives us at least enough slots
char **argv = *buf = calloc(max_tokens, sizeof(char*));
argv[0] = base; // this needs to be freed by caller
bool quoted = true;
*argc = 1; // uneven = name, even = value
while (*head && *argc < max_tokens) {
if (namep != head && *(head - 1) == '\\') {
// escaped character: don't try to parse this
} else if (*head == '=' && namep != head) {
if (*argc % 2 != 1) {
// we're not expecting a name
return strdup("Unable to parse criteria: "
"Found out of place equal sign");
} else {
// name ends here
char *end = head; // don't want to rewind the head
while (*(end - 1) == ' ') {
--end;
}
*end = '\0';
if (*(namep) == ' ') {
namep = strrchr(namep, ' ') + 1;
}
argv[*argc] = namep;
*argc += 1;
}
} else if (*head == '"') {
if (*argc % 2 != 0) {
// we're not expecting a value
return strdup("Unable to parse criteria: "
"Found quoted value where it was not expected");
} else if (!valp) { // value starts here
valp = head + 1;
quoted = true;
} else {
// value ends here
argv[*argc] = valp;
*argc += 1;
*head = '\0';
valp = NULL;
namep = head + 1;
}
} else if (*argc % 2 == 0 && *head != ' ') {
// parse unquoted values
if (!valp) {
quoted = false;
valp = head; // value starts here
}
} else if (valp && !quoted && *head == ' ') {
// value ends here
argv[*argc] = valp;
*argc += 1;
*head = '\0';
valp = NULL;
namep = head + 1;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
head++;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
// catch last unquoted value if needed
if (valp && !quoted && !*head) {
argv[*argc] = valp;
*argc += 1;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
return NULL;
2018-01-21 08:21:45 +11:00
}
// Returns error string on failure or NULL otherwise.
static char *parse_criteria_name(enum criteria_type *type, char *name) {
*type = CRIT_LAST;
for (int i = 0; i < CRIT_LAST; i++) {
if (strcmp(criteria_strings[i], name) == 0) {
*type = (enum criteria_type) i;
break;
2018-01-21 08:21:45 +11:00
}
}
if (*type == CRIT_LAST) {
const char *fmt = "Criteria type '%s' is invalid or unsupported.";
int len = strlen(name) + strlen(fmt) - 1;
char *error = malloc(len);
snprintf(error, len, fmt, name);
return error;
} else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE ||
*type == CRIT_WINDOW_TYPE) {
// (we're just being helpful here)
const char *fmt = "\"%s\" criteria currently unsupported, "
"no window will match this";
int len = strlen(fmt) + strlen(name) - 1;
char *error = malloc(len);
snprintf(error, len, fmt, name);
return error;
2018-01-21 08:21:45 +11:00
}
return NULL;
2018-01-21 08:21:45 +11:00
}
// Returns error string on failure or NULL otherwise.
static char *generate_regex(pcre **regex, char *value) {
2018-01-21 08:21:45 +11:00
const char *reg_err;
int offset;
*regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, &reg_err, &offset, NULL);
if (!*regex) {
const char *fmt = "Regex compilation (for '%s') failed: %s";
2018-01-21 08:21:45 +11:00
int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
char *error = malloc(len);
2018-01-21 08:21:45 +11:00
snprintf(error, len, fmt, value, reg_err);
return error;
2018-01-21 08:21:45 +11:00
}
return NULL;
}
2018-01-21 08:21:45 +11:00
// Test whether the criterion corresponds to the currently focused window
static bool crit_is_focused(const char *value) {
return !strcmp(value, "focused") || !strcmp(value, "__focused__");
2018-01-21 08:21:45 +11:00
}
// Populate list with crit_tokens extracted from criteria string, returns error
// string or NULL if successful.
char *extract_crit_tokens(list_t *tokens, const char * const criteria) {
int argc;
char **argv = NULL, *error = NULL;
if ((error = crit_tokens(&argc, &argv, criteria))) {
goto ect_cleanup;
2018-01-21 08:21:45 +11:00
}
for (int i = 1; i + 1 < argc; i += 2) {
char* name = argv[i], *value = argv[i + 1];
struct crit_token *token = calloc(1, sizeof(struct crit_token));
token->raw = strdup(value);
if ((error = parse_criteria_name(&token->type, name))) {
free_crit_token(token);
goto ect_cleanup;
} else if (token->type == CRIT_URGENT || crit_is_focused(value)) {
wlr_log(L_DEBUG, "%s -> \"%s\"", name, value);
list_add(tokens, token);
} else if((error = generate_regex(&token->regex, value))) {
free_crit_token(token);
goto ect_cleanup;
2018-01-21 08:21:45 +11:00
} else {
wlr_log(L_DEBUG, "%s -> /%s/", name, value);
list_add(tokens, token);
2018-01-21 08:21:45 +11:00
}
}
ect_cleanup:
free(argv[0]); // base string
free(argv);
return error;
2018-01-21 08:21:45 +11:00
}
static int regex_cmp(const char *item, const pcre *regex) {
return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
2018-01-21 08:21:45 +11:00
}
// test a single view if it matches list of criteria tokens (all of them).
static bool criteria_test(struct sway_container *cont, list_t *tokens) {
if (cont->type != C_CONTAINER && cont->type != C_VIEW) {
return false;
2018-01-21 08:21:45 +11:00
}
int matches = 0;
for (int i = 0; i < tokens->length; i++) {
struct crit_token *crit = tokens->items[i];
switch (crit->type) {
case CRIT_CLASS:
{
const char *class = view_get_class(cont->sway_view);
if (!class) {
break;
}
if (crit->regex && regex_cmp(class, crit->regex) == 0) {
matches++;
}
break;
}
case CRIT_CON_ID:
{
char *endptr;
size_t crit_id = strtoul(crit->raw, &endptr, 10);
2018-01-21 08:21:45 +11:00
if (*endptr == 0 && cont->id == crit_id) {
++matches;
}
break;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
case CRIT_CON_MARK:
// TODO
break;
case CRIT_FLOATING:
// TODO
break;
case CRIT_ID:
// TODO
break;
case CRIT_APP_ID:
{
const char *app_id = view_get_app_id(cont->sway_view);
if (!app_id) {
break;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
if (crit->regex && regex_cmp(app_id, crit->regex) == 0) {
matches++;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
break;
}
case CRIT_INSTANCE:
{
const char *instance = view_get_instance(cont->sway_view);
if (!instance) {
break;
}
if (crit->regex && regex_cmp(instance, crit->regex) == 0) {
matches++;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
break;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
case CRIT_TILING:
// TODO
break;
case CRIT_TITLE:
{
const char *title = view_get_title(cont->sway_view);
if (!title) {
break;
}
if (crit->regex && regex_cmp(title, crit->regex) == 0) {
matches++;
}
break;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
case CRIT_URGENT:
// TODO "latest" or "oldest"
break;
case CRIT_WINDOW_ROLE:
// TODO
break;
case CRIT_WINDOW_TYPE:
// TODO
break;
case CRIT_WORKSPACE:
// TODO
break;
default:
sway_abort("Invalid criteria type (%i)", crit->type);
break;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
}
return matches == tokens->length;
}
int criteria_cmp(const void *a, const void *b) {
if (a == b) {
return 0;
} else if (!a) {
return -1;
} else if (!b) {
return 1;
}
const struct criteria *crit_a = a, *crit_b = b;
int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist);
if (cmp != 0) {
return cmp;
}
return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw);
}
void free_criteria(struct criteria *crit) {
if (crit->tokens) {
free_crit_tokens(crit->tokens);
}
if (crit->cmdlist) {
free(crit->cmdlist);
}
if (crit->crit_raw) {
free(crit->crit_raw);
}
free(crit);
}
bool criteria_any(struct sway_container *cont, list_t *criteria) {
for (int i = 0; i < criteria->length; i++) {
struct criteria *bc = criteria->items[i];
if (criteria_test(cont, bc->tokens)) {
return true;
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
2018-01-21 08:21:45 +11:00
}
return false;
}
list_t *criteria_for(struct sway_container *cont) {
list_t *criteria = config->criteria, *matches = create_list();
for (int i = 0; i < criteria->length; i++) {
struct criteria *bc = criteria->items[i];
if (criteria_test(cont, bc->tokens)) {
list_add(matches, bc);
}
2018-01-21 08:21:45 +11:00
}
return matches;
}
2018-01-21 08:21:45 +11:00
struct list_tokens {
list_t *list;
list_t *tokens;
};
static void container_match_add(struct sway_container *container,
struct list_tokens *list_tokens) {
if (criteria_test(container, list_tokens->tokens)) {
list_add(list_tokens->list, container);
Overhaul criteria implementation The criteria struct now uses properties for each token type rather than the list_t list of tokens. The reason for this is that different token types have different data types: pcre, string and number to name a few. This solution should be more flexible moving forward. A bonus of this is that criteria is now easier to understand when looking at the struct definition. The criteria parser has been rewritten because the previous one didn't support valueless pairs (eg. [class="foo" floating]). Criteria now has types. Types at the moment are CT_COMMAND, CT_ASSIGN_WORKSPACE and CT_ASSIGN_OUTPUT. i3 uses types as well. Previously the assign command was creating a criteria with 'move to workspace <name>' as its command, but this caused the window to appear briefly on the focused workspace before being moved to the assigned workspace. It now creates the view directly in the assigned workspace. Each view will only execute a given criteria once. This is achieved by storing a list of executed criteria in the view. This is the same strategy used by i3. Escaping now works properly. Previously you could do things like [class="Fire\"fox"] and the stored value would be 'Fire\"fox', but it should be (and now is) 'Fire"fox'. The public functions in criteria.c are now all prefixed with criteria_. Xwayland views now listen to the set_title, set_class and set_window_type events and criteria will be run when these happen. XDG shell has none of these events so it continues to update the title in handle_commit. Each view type's get_prop function has been split into get_string_prop and get_int_prop because some properties like the X11 window ID and window type are numeric. The following new criteria tokens are now supported: * id (X11 window ID) * instance * tiling * workspace
2018-05-09 14:23:20 +10:00
}
}
2018-01-21 08:21:45 +11:00
list_t *container_for_crit_tokens(list_t *tokens) {
struct list_tokens list_tokens =
(struct list_tokens){create_list(), tokens};
2018-01-21 08:21:45 +11:00
container_for_each_descendant_dfs(&root_container,
(void (*)(struct sway_container *, void *))container_match_add,
&list_tokens);
// TODO look in the scratchpad
return list_tokens.list;
2018-01-21 08:21:45 +11:00
}