|
|
@ -11,435 +11,501 @@
|
|
|
|
#include "list.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum criteria_type { // *must* keep in sync with criteria_strings[]
|
|
|
|
bool criteria_is_empty(struct criteria *criteria) {
|
|
|
|
CRIT_APP_ID,
|
|
|
|
return !criteria->title
|
|
|
|
CRIT_CLASS,
|
|
|
|
&& !criteria->app_id
|
|
|
|
CRIT_CON_ID,
|
|
|
|
&& !criteria->class
|
|
|
|
CRIT_CON_MARK,
|
|
|
|
&& !criteria->instance
|
|
|
|
CRIT_FLOATING,
|
|
|
|
&& !criteria->con_mark
|
|
|
|
CRIT_ID,
|
|
|
|
&& !criteria->con_id
|
|
|
|
CRIT_INSTANCE,
|
|
|
|
&& !criteria->id
|
|
|
|
CRIT_TILING,
|
|
|
|
&& !criteria->window_role
|
|
|
|
CRIT_TITLE,
|
|
|
|
&& !criteria->window_type
|
|
|
|
CRIT_URGENT,
|
|
|
|
&& !criteria->floating
|
|
|
|
CRIT_WINDOW_ROLE,
|
|
|
|
&& !criteria->tiling
|
|
|
|
CRIT_WINDOW_TYPE,
|
|
|
|
&& !criteria->urgent
|
|
|
|
CRIT_WORKSPACE,
|
|
|
|
&& !criteria->workspace;
|
|
|
|
CRIT_LAST
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
void criteria_destroy(struct criteria *criteria) {
|
|
|
|
* A single criteria token (ie. value/regex pair),
|
|
|
|
pcre_free(criteria->title);
|
|
|
|
* e.g. 'class="some class regex"'.
|
|
|
|
pcre_free(criteria->app_id);
|
|
|
|
*/
|
|
|
|
pcre_free(criteria->class);
|
|
|
|
struct crit_token {
|
|
|
|
pcre_free(criteria->instance);
|
|
|
|
enum criteria_type type;
|
|
|
|
pcre_free(criteria->con_mark);
|
|
|
|
pcre *regex;
|
|
|
|
pcre_free(criteria->window_role);
|
|
|
|
char *raw;
|
|
|
|
free(criteria->workspace);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
free(criteria->raw);
|
|
|
|
|
|
|
|
free(criteria);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void free_crit_token(struct crit_token *crit) {
|
|
|
|
static int regex_cmp(const char *item, const pcre *regex) {
|
|
|
|
pcre_free(crit->regex);
|
|
|
|
return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
|
|
|
|
free(crit->raw);
|
|
|
|
|
|
|
|
free(crit);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void free_crit_tokens(list_t *crit_tokens) {
|
|
|
|
static bool criteria_matches_view(struct criteria *criteria,
|
|
|
|
for (int i = 0; i < crit_tokens->length; i++) {
|
|
|
|
struct sway_view *view) {
|
|
|
|
free_crit_token(crit_tokens->items[i]);
|
|
|
|
if (criteria->title) {
|
|
|
|
|
|
|
|
const char *title = view_get_title(view);
|
|
|
|
|
|
|
|
if (!title || regex_cmp(title, criteria->title) != 0) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_free(crit_tokens);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Extracts criteria string from its brackets. Returns new (duplicate)
|
|
|
|
if (criteria->app_id) {
|
|
|
|
// substring.
|
|
|
|
const char *app_id = view_get_app_id(view);
|
|
|
|
static char *criteria_from(const char *arg) {
|
|
|
|
if (!app_id || regex_cmp(app_id, criteria->app_id) != 0) {
|
|
|
|
char *criteria = NULL;
|
|
|
|
return false;
|
|
|
|
if (*arg == '[') {
|
|
|
|
}
|
|
|
|
criteria = strdup(arg + 1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
criteria = strdup(arg);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int last = strlen(criteria) - 1;
|
|
|
|
if (criteria->class) {
|
|
|
|
if (criteria[last] == ']') {
|
|
|
|
const char *class = view_get_class(view);
|
|
|
|
criteria[last] = '\0';
|
|
|
|
if (!class || regex_cmp(class, criteria->class) != 0) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return criteria;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return instances of c found in str.
|
|
|
|
if (criteria->instance) {
|
|
|
|
static int countchr(char *str, char c) {
|
|
|
|
const char *instance = view_get_instance(view);
|
|
|
|
int found = 0;
|
|
|
|
if (!instance || regex_cmp(instance, criteria->instance) != 0) {
|
|
|
|
for (int i = 0; str[i]; i++) {
|
|
|
|
return false;
|
|
|
|
if (str[i] == c) {
|
|
|
|
|
|
|
|
++found;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// criteria_str is e.g. '[class="some class regex" instance="instance name"]'.
|
|
|
|
if (criteria->con_mark) {
|
|
|
|
//
|
|
|
|
// TODO
|
|
|
|
// Will create array of pointers in buf, where first is duplicate of given
|
|
|
|
return false;
|
|
|
|
// 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.
|
|
|
|
if (criteria->con_id) { // Internal ID
|
|
|
|
//
|
|
|
|
if (!view->swayc || view->swayc->id != criteria->con_id) {
|
|
|
|
// Returns error string or NULL if successful.
|
|
|
|
return false;
|
|
|
|
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);
|
|
|
|
if (criteria->id) { // X11 window ID
|
|
|
|
char *head = base;
|
|
|
|
uint32_t x11_window_id = view_get_x11_window_id(view);
|
|
|
|
char *namep = head; // start of criteria name
|
|
|
|
if (!x11_window_id || x11_window_id != criteria->id) {
|
|
|
|
char *valp = NULL; // start of value
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
// 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 (criteria->window_role) {
|
|
|
|
if (!valp) {
|
|
|
|
// TODO
|
|
|
|
quoted = false;
|
|
|
|
|
|
|
|
valp = head; // value starts here
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (valp && !quoted && *head == ' ') {
|
|
|
|
|
|
|
|
// value ends here
|
|
|
|
if (criteria->window_type) {
|
|
|
|
argv[*argc] = valp;
|
|
|
|
uint32_t type = view_get_window_type(view);
|
|
|
|
*argc += 1;
|
|
|
|
if (!type || type != criteria->window_type) {
|
|
|
|
*head = '\0';
|
|
|
|
return false;
|
|
|
|
valp = NULL;
|
|
|
|
|
|
|
|
namep = head + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
head++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// catch last unquoted value if needed
|
|
|
|
if (criteria->floating) {
|
|
|
|
if (valp && !quoted && !*head) {
|
|
|
|
// TODO
|
|
|
|
argv[*argc] = valp;
|
|
|
|
return false;
|
|
|
|
*argc += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
if (criteria->tiling) {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (criteria->urgent) {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (criteria->workspace) {
|
|
|
|
|
|
|
|
if (!view->swayc) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE);
|
|
|
|
|
|
|
|
if (!ws || strcmp(ws->name, criteria->workspace) != 0) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns error string on failure or NULL otherwise.
|
|
|
|
list_t *criteria_for_view(struct sway_view *view, enum criteria_type types) {
|
|
|
|
static char *parse_criteria_name(enum criteria_type *type, char *name) {
|
|
|
|
list_t *criterias = config->criteria;
|
|
|
|
*type = CRIT_LAST;
|
|
|
|
list_t *matches = create_list();
|
|
|
|
for (int i = 0; i < CRIT_LAST; i++) {
|
|
|
|
for (int i = 0; i < criterias->length; ++i) {
|
|
|
|
if (strcmp(criteria_strings[i], name) == 0) {
|
|
|
|
struct criteria *criteria = criterias->items[i];
|
|
|
|
*type = (enum criteria_type) i;
|
|
|
|
if ((criteria->type & types) && criteria_matches_view(criteria, view)) {
|
|
|
|
break;
|
|
|
|
list_add(matches, criteria);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*type == CRIT_LAST) {
|
|
|
|
return matches;
|
|
|
|
const char *fmt = "Criteria type '%s' is invalid or unsupported.";
|
|
|
|
}
|
|
|
|
int len = strlen(name) + strlen(fmt) - 1;
|
|
|
|
|
|
|
|
char *error = malloc(len);
|
|
|
|
struct match_data {
|
|
|
|
snprintf(error, len, fmt, name);
|
|
|
|
struct criteria *criteria;
|
|
|
|
return error;
|
|
|
|
list_t *matches;
|
|
|
|
} else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE ||
|
|
|
|
};
|
|
|
|
*type == CRIT_WINDOW_TYPE) {
|
|
|
|
|
|
|
|
// (we're just being helpful here)
|
|
|
|
static void criteria_get_views_iterator(struct sway_container *container,
|
|
|
|
const char *fmt = "\"%s\" criteria currently unsupported, "
|
|
|
|
void *data) {
|
|
|
|
"no window will match this";
|
|
|
|
struct match_data *match_data = data;
|
|
|
|
int len = strlen(fmt) + strlen(name) - 1;
|
|
|
|
if (container->type == C_VIEW) {
|
|
|
|
char *error = malloc(len);
|
|
|
|
if (criteria_matches_view(match_data->criteria, container->sway_view)) {
|
|
|
|
snprintf(error, len, fmt, name);
|
|
|
|
list_add(match_data->matches, container->sway_view);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list_t *criteria_get_views(struct criteria *criteria) {
|
|
|
|
|
|
|
|
list_t *matches = create_list();
|
|
|
|
|
|
|
|
struct match_data data = {
|
|
|
|
|
|
|
|
.criteria = criteria,
|
|
|
|
|
|
|
|
.matches = matches,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
container_for_each_descendant_dfs(&root_container,
|
|
|
|
|
|
|
|
criteria_get_views_iterator, &data);
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The error pointer is used for parsing functions, and saves having to pass it
|
|
|
|
|
|
|
|
// as an argument in several places.
|
|
|
|
|
|
|
|
char *error = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// Returns error string on failure or NULL otherwise.
|
|
|
|
// Returns error string on failure or NULL otherwise.
|
|
|
|
static char *generate_regex(pcre **regex, char *value) {
|
|
|
|
static bool generate_regex(pcre **regex, char *value) {
|
|
|
|
const char *reg_err;
|
|
|
|
const char *reg_err;
|
|
|
|
int offset;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
|
|
|
|
*regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL);
|
|
|
|
*regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
if (!*regex) {
|
|
|
|
if (!*regex) {
|
|
|
|
const char *fmt = "Regex compilation (for '%s') failed: %s";
|
|
|
|
const char *fmt = "Regex compilation for '%s' failed: %s";
|
|
|
|
int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
|
|
|
|
int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
|
|
|
|
char *error = malloc(len);
|
|
|
|
error = malloc(len);
|
|
|
|
snprintf(error, len, fmt, value, reg_err);
|
|
|
|
snprintf(error, len, fmt, value, reg_err);
|
|
|
|
return error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test whether the criterion corresponds to the currently focused window
|
|
|
|
return true;
|
|
|
|
static bool crit_is_focused(const char *value) {
|
|
|
|
|
|
|
|
return !strcmp(value, "focused") || !strcmp(value, "__focused__");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Populate list with crit_tokens extracted from criteria string, returns error
|
|
|
|
enum criteria_token {
|
|
|
|
// string or NULL if successful.
|
|
|
|
T_APP_ID,
|
|
|
|
char *extract_crit_tokens(list_t *tokens, const char * const criteria) {
|
|
|
|
T_CLASS,
|
|
|
|
int argc;
|
|
|
|
T_CON_ID,
|
|
|
|
char **argv = NULL, *error = NULL;
|
|
|
|
T_CON_MARK,
|
|
|
|
if ((error = crit_tokens(&argc, &argv, criteria))) {
|
|
|
|
T_FLOATING,
|
|
|
|
goto ect_cleanup;
|
|
|
|
T_ID,
|
|
|
|
}
|
|
|
|
T_INSTANCE,
|
|
|
|
for (int i = 1; i + 1 < argc; i += 2) {
|
|
|
|
T_TILING,
|
|
|
|
char* name = argv[i], *value = argv[i + 1];
|
|
|
|
T_TITLE,
|
|
|
|
struct crit_token *token = calloc(1, sizeof(struct crit_token));
|
|
|
|
T_URGENT,
|
|
|
|
token->raw = strdup(value);
|
|
|
|
T_WINDOW_ROLE,
|
|
|
|
|
|
|
|
T_WINDOW_TYPE,
|
|
|
|
if ((error = parse_criteria_name(&token->type, name))) {
|
|
|
|
T_WORKSPACE,
|
|
|
|
free_crit_token(token);
|
|
|
|
|
|
|
|
goto ect_cleanup;
|
|
|
|
T_INVALID,
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
wlr_log(L_DEBUG, "%s -> /%s/", name, value);
|
|
|
|
|
|
|
|
list_add(tokens, token);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ect_cleanup:
|
|
|
|
|
|
|
|
free(argv[0]); // base string
|
|
|
|
|
|
|
|
free(argv);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int regex_cmp(const char *item, const pcre *regex) {
|
|
|
|
static enum criteria_token token_from_name(char *name) {
|
|
|
|
return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
|
|
|
|
if (strcmp(name, "app_id") == 0) {
|
|
|
|
|
|
|
|
return T_APP_ID;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "class") == 0) {
|
|
|
|
|
|
|
|
return T_CLASS;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "con_id") == 0) {
|
|
|
|
|
|
|
|
return T_CON_ID;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "con_mark") == 0) {
|
|
|
|
|
|
|
|
return T_CON_MARK;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "id") == 0) {
|
|
|
|
|
|
|
|
return T_ID;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "instance") == 0) {
|
|
|
|
|
|
|
|
return T_INSTANCE;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "title") == 0) {
|
|
|
|
|
|
|
|
return T_TITLE;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "urgent") == 0) {
|
|
|
|
|
|
|
|
return T_URGENT;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "window_role") == 0) {
|
|
|
|
|
|
|
|
return T_WINDOW_ROLE;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "window_type") == 0) {
|
|
|
|
|
|
|
|
return T_WINDOW_TYPE;
|
|
|
|
|
|
|
|
} else if (strcmp(name, "workspace") == 0) {
|
|
|
|
|
|
|
|
return T_WORKSPACE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return T_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
* Get a property of the focused view.
|
|
|
|
if (cont->type != C_CONTAINER && cont->type != C_VIEW) {
|
|
|
|
*
|
|
|
|
return false;
|
|
|
|
* Note that we are taking the focused view at the time of criteria parsing, not
|
|
|
|
}
|
|
|
|
* at the time of execution. This is because __focused__ only makes sense when
|
|
|
|
int matches = 0;
|
|
|
|
* using criteria via IPC. Using __focused__ in config is not useful because
|
|
|
|
for (int i = 0; i < tokens->length; i++) {
|
|
|
|
* criteria is only executed once per view.
|
|
|
|
struct crit_token *crit = tokens->items[i];
|
|
|
|
*/
|
|
|
|
switch (crit->type) {
|
|
|
|
static char *get_focused_prop(enum criteria_token token) {
|
|
|
|
case CRIT_CLASS:
|
|
|
|
struct sway_seat *seat = input_manager_current_seat(input_manager);
|
|
|
|
{
|
|
|
|
struct sway_container *focus = seat_get_focus(seat);
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (*endptr == 0 && cont->id == crit_id) {
|
|
|
|
if (!focus || focus->type != C_VIEW) {
|
|
|
|
++matches;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sway_view *view = focus->sway_view;
|
|
|
|
|
|
|
|
const char *value = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (token) {
|
|
|
|
|
|
|
|
case T_APP_ID:
|
|
|
|
|
|
|
|
value = view_get_app_id(view);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_CLASS:
|
|
|
|
case CRIT_CON_MARK:
|
|
|
|
value = view_get_class(view);
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_FLOATING:
|
|
|
|
case T_INSTANCE:
|
|
|
|
// TODO
|
|
|
|
value = view_get_instance(view);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_ID:
|
|
|
|
case T_TITLE:
|
|
|
|
// TODO
|
|
|
|
value = view_get_class(view);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_APP_ID:
|
|
|
|
case T_WINDOW_ROLE:
|
|
|
|
{
|
|
|
|
value = view_get_class(view);
|
|
|
|
const char *app_id = view_get_app_id(cont->sway_view);
|
|
|
|
|
|
|
|
if (!app_id) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_WORKSPACE:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct sway_container *ws = container_parent(focus, C_WORKSPACE);
|
|
|
|
|
|
|
|
if (ws) {
|
|
|
|
|
|
|
|
value = ws->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (crit->regex && regex_cmp(app_id, crit->regex) == 0) {
|
|
|
|
|
|
|
|
matches++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_CON_ID: // These do not support __focused__
|
|
|
|
case CRIT_INSTANCE:
|
|
|
|
case T_CON_MARK:
|
|
|
|
{
|
|
|
|
case T_FLOATING:
|
|
|
|
const char *instance = view_get_instance(cont->sway_view);
|
|
|
|
case T_ID:
|
|
|
|
if (!instance) {
|
|
|
|
case T_TILING:
|
|
|
|
|
|
|
|
case T_URGENT:
|
|
|
|
|
|
|
|
case T_WINDOW_TYPE:
|
|
|
|
|
|
|
|
case T_INVALID:
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
|
|
|
|
return strdup(value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (crit->regex && regex_cmp(instance, crit->regex) == 0) {
|
|
|
|
static bool parse_token(struct criteria *criteria, char *name, char *value) {
|
|
|
|
matches++;
|
|
|
|
enum criteria_token token = token_from_name(name);
|
|
|
|
|
|
|
|
if (token == T_INVALID) {
|
|
|
|
|
|
|
|
const char *fmt = "Token '%s' is not recognized";
|
|
|
|
|
|
|
|
int len = strlen(fmt) + strlen(name) - 1;
|
|
|
|
|
|
|
|
error = malloc(len);
|
|
|
|
|
|
|
|
snprintf(error, len, fmt, name);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
char *effective_value = NULL;
|
|
|
|
|
|
|
|
if (value && strcmp(value, "__focused__") == 0) {
|
|
|
|
|
|
|
|
effective_value = get_focused_prop(token);
|
|
|
|
|
|
|
|
} else if (value) {
|
|
|
|
|
|
|
|
effective_value = strdup(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case CRIT_TILING:
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// Require value, unless token is floating or tiled
|
|
|
|
break;
|
|
|
|
if (!effective_value && token != T_FLOATING && token != T_TILING) {
|
|
|
|
case CRIT_TITLE:
|
|
|
|
const char *fmt = "Token '%s' requires a value";
|
|
|
|
{
|
|
|
|
int len = strlen(fmt) + strlen(name) - 1;
|
|
|
|
const char *title = view_get_title(cont->sway_view);
|
|
|
|
error = malloc(len);
|
|
|
|
if (!title) {
|
|
|
|
snprintf(error, len, fmt, name);
|
|
|
|
break;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (crit->regex && regex_cmp(title, crit->regex) == 0) {
|
|
|
|
char *endptr = NULL;
|
|
|
|
matches++;
|
|
|
|
switch (token) {
|
|
|
|
|
|
|
|
case T_TITLE:
|
|
|
|
|
|
|
|
generate_regex(&criteria->title, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_APP_ID:
|
|
|
|
|
|
|
|
generate_regex(&criteria->app_id, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_CLASS:
|
|
|
|
|
|
|
|
generate_regex(&criteria->class, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_INSTANCE:
|
|
|
|
|
|
|
|
generate_regex(&criteria->instance, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_CON_ID:
|
|
|
|
|
|
|
|
criteria->con_id = strtoul(effective_value, &endptr, 10);
|
|
|
|
|
|
|
|
if (*endptr != 0) {
|
|
|
|
|
|
|
|
error = strdup("The value for 'con_id' should be numeric");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_CON_MARK:
|
|
|
|
|
|
|
|
generate_regex(&criteria->con_mark, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_WINDOW_ROLE:
|
|
|
|
|
|
|
|
generate_regex(&criteria->window_role, effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_WINDOW_TYPE:
|
|
|
|
|
|
|
|
// TODO: This is a string but will be stored as an enum or integer
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ID:
|
|
|
|
|
|
|
|
criteria->id = strtoul(effective_value, &endptr, 10);
|
|
|
|
|
|
|
|
if (*endptr != 0) {
|
|
|
|
|
|
|
|
error = strdup("The value for 'id' should be numeric");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case CRIT_URGENT:
|
|
|
|
|
|
|
|
// TODO "latest" or "oldest"
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_WINDOW_ROLE:
|
|
|
|
case T_FLOATING:
|
|
|
|
// TODO
|
|
|
|
criteria->floating = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_WINDOW_TYPE:
|
|
|
|
case T_TILING:
|
|
|
|
// TODO
|
|
|
|
criteria->tiling = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CRIT_WORKSPACE:
|
|
|
|
case T_URGENT:
|
|
|
|
// TODO
|
|
|
|
if (strcmp(effective_value, "latest") == 0) {
|
|
|
|
|
|
|
|
criteria->urgent = 'l';
|
|
|
|
|
|
|
|
} else if (strcmp(effective_value, "oldest") == 0) {
|
|
|
|
|
|
|
|
criteria->urgent = 'o';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
error =
|
|
|
|
|
|
|
|
strdup("The value for 'urgent' must be 'latest' or 'oldest'");
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case T_WORKSPACE:
|
|
|
|
sway_abort("Invalid criteria type (%i)", crit->type);
|
|
|
|
criteria->workspace = strdup(effective_value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_INVALID:
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(effective_value);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches == tokens->length;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int criteria_cmp(const void *a, const void *b) {
|
|
|
|
static void skip_spaces(char **head) {
|
|
|
|
if (a == b) {
|
|
|
|
while (**head == ' ') {
|
|
|
|
return 0;
|
|
|
|
++*head;
|
|
|
|
} 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) {
|
|
|
|
// Remove escaping slashes from value
|
|
|
|
if (crit->tokens) {
|
|
|
|
static void unescape(char *value) {
|
|
|
|
free_crit_tokens(crit->tokens);
|
|
|
|
if (!strchr(value, '\\')) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char *copy = calloc(strlen(value) + 1, 1);
|
|
|
|
|
|
|
|
char *readhead = value;
|
|
|
|
|
|
|
|
char *writehead = copy;
|
|
|
|
|
|
|
|
while (*readhead) {
|
|
|
|
|
|
|
|
if (*readhead == '\\' && *(readhead + 1) == '"') {
|
|
|
|
|
|
|
|
// skip the slash
|
|
|
|
|
|
|
|
++readhead;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*writehead = *readhead;
|
|
|
|
|
|
|
|
++writehead;
|
|
|
|
|
|
|
|
++readhead;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(value, copy);
|
|
|
|
|
|
|
|
free(copy);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Parse a raw criteria string such as [class="foo" instance="bar"] into a
|
|
|
|
|
|
|
|
* criteria struct.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* If errors are found, NULL will be returned and the error argument will be
|
|
|
|
|
|
|
|
* populated with an error string.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct criteria *criteria_parse(char *raw, char **error_arg) {
|
|
|
|
|
|
|
|
free(error);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *head = raw;
|
|
|
|
|
|
|
|
skip_spaces(&head);
|
|
|
|
|
|
|
|
if (*head != '[') {
|
|
|
|
|
|
|
|
*error_arg = strdup("No criteria");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (crit->cmdlist) {
|
|
|
|
++head;
|
|
|
|
free(crit->cmdlist);
|
|
|
|
|
|
|
|
|
|
|
|
struct criteria *criteria = calloc(sizeof(struct criteria), 1);
|
|
|
|
|
|
|
|
char *name = NULL, *value = NULL;
|
|
|
|
|
|
|
|
bool in_quotes = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (*head && *head != ']') {
|
|
|
|
|
|
|
|
skip_spaces(&head);
|
|
|
|
|
|
|
|
// Parse token name
|
|
|
|
|
|
|
|
char *namestart = head;
|
|
|
|
|
|
|
|
while ((*head >= 'a' && *head <= 'z') || *head == '_') {
|
|
|
|
|
|
|
|
++head;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
name = calloc(head - namestart + 1, 1);
|
|
|
|
|
|
|
|
strncpy(name, namestart, head - namestart);
|
|
|
|
|
|
|
|
// Parse token value
|
|
|
|
|
|
|
|
skip_spaces(&head);
|
|
|
|
|
|
|
|
value = NULL;
|
|
|
|
|
|
|
|
if (*head == '=') {
|
|
|
|
|
|
|
|
++head;
|
|
|
|
|
|
|
|
skip_spaces(&head);
|
|
|
|
|
|
|
|
if (*head == '"') {
|
|
|
|
|
|
|
|
in_quotes = true;
|
|
|
|
|
|
|
|
++head;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char *valuestart = head;
|
|
|
|
|
|
|
|
if (in_quotes) {
|
|
|
|
|
|
|
|
while (*head && (*head != '"' || *(head - 1) == '\\')) {
|
|
|
|
|
|
|
|
++head;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*head) {
|
|
|
|
|
|
|
|
*error_arg = strdup("Quote mismatch in criteria");
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (crit->crit_raw) {
|
|
|
|
} else {
|
|
|
|
free(crit->crit_raw);
|
|
|
|
while (*head && *head != ' ' && *head != ']') {
|
|
|
|
|
|
|
|
++head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = calloc(head - valuestart + 1, 1);
|
|
|
|
|
|
|
|
strncpy(value, valuestart, head - valuestart);
|
|
|
|
|
|
|
|
if (in_quotes) {
|
|
|
|
|
|
|
|
++head;
|
|
|
|
|
|
|
|
in_quotes = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
unescape(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_log(L_DEBUG, "Found pair: %s=%s", name, value);
|
|
|
|
|
|
|
|
if (!parse_token(criteria, name, value)) {
|
|
|
|
|
|
|
|
*error_arg = error;
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
skip_spaces(&head);
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
free(value);
|
|
|
|
struct list_tokens {
|
|
|
|
name = NULL;
|
|
|
|
list_t *list;
|
|
|
|
value = NULL;
|
|
|
|
list_t *tokens;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (*head != ']') {
|
|
|
|
|
|
|
|
*error_arg = strdup("No closing brace found in criteria");
|
|
|
|
static void container_match_add(struct sway_container *container,
|
|
|
|
goto cleanup;
|
|
|
|
struct list_tokens *list_tokens) {
|
|
|
|
|
|
|
|
if (criteria_test(container, list_tokens->tokens)) {
|
|
|
|
|
|
|
|
list_add(list_tokens->list, container);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list_t *container_for_crit_tokens(list_t *tokens) {
|
|
|
|
|
|
|
|
struct list_tokens list_tokens =
|
|
|
|
|
|
|
|
(struct list_tokens){create_list(), tokens};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
container_for_each_descendant_dfs(&root_container,
|
|
|
|
if (criteria_is_empty(criteria)) {
|
|
|
|
(void (*)(struct sway_container *, void *))container_match_add,
|
|
|
|
*error_arg = strdup("Criteria is empty");
|
|
|
|
&list_tokens);
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO look in the scratchpad
|
|
|
|
++head;
|
|
|
|
|
|
|
|
int len = head - raw;
|
|
|
|
|
|
|
|
criteria->raw = calloc(len + 1, 1);
|
|
|
|
|
|
|
|
strncpy(criteria->raw, raw, len);
|
|
|
|
|
|
|
|
return criteria;
|
|
|
|
|
|
|
|
|
|
|
|
return list_tokens.list;
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
free(value);
|
|
|
|
|
|
|
|
criteria_destroy(criteria);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|