Allow multiple marks on windows.
This patch allows multiple marks to be set on a single window. The restriction that a mark may only be on one window at a time is still upheld as this is necessary for commands like "move window to mark" to make sense. relates to #2014
This commit is contained in:
@ -290,10 +290,16 @@ void cmd_criteria_match_windows(I3_CMD) {
|
||||
DLOG("doesnt match\n");
|
||||
free(current);
|
||||
}
|
||||
} else if (current_match->mark != NULL && current->con->mark != NULL &&
|
||||
regex_matches(current_match->mark, current->con->mark)) {
|
||||
DLOG("match by mark\n");
|
||||
TAILQ_INSERT_TAIL(&owindows, current, owindows);
|
||||
} else if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
|
||||
mark_t *mark;
|
||||
TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
|
||||
if (!regex_matches(current_match->mark, mark->name))
|
||||
continue;
|
||||
|
||||
DLOG("match by mark\n");
|
||||
TAILQ_INSERT_TAIL(&owindows, current, owindows);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (current->con->window && match_matches_window(current_match, current->con->window)) {
|
||||
DLOG("matches window!\n");
|
||||
|
63
src/con.c
63
src/con.c
@ -55,6 +55,7 @@ Con *con_new_skeleton(Con *parent, i3Window *window) {
|
||||
TAILQ_INIT(&(new->nodes_head));
|
||||
TAILQ_INIT(&(new->focus_head));
|
||||
TAILQ_INIT(&(new->swallow_head));
|
||||
TAILQ_INIT(&(new->marks_head));
|
||||
|
||||
if (parent != NULL)
|
||||
con_attach(new, parent, false);
|
||||
@ -512,13 +513,27 @@ Con *con_by_frame_id(xcb_window_t frame) {
|
||||
Con *con_by_mark(const char *mark) {
|
||||
Con *con;
|
||||
TAILQ_FOREACH(con, &all_cons, all_cons) {
|
||||
if (con->mark != NULL && strcmp(con->mark, mark) == 0)
|
||||
if (con_has_mark(con, mark))
|
||||
return con;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if and only if the given containers holds the mark.
|
||||
*
|
||||
*/
|
||||
bool con_has_mark(Con *con, const char *mark) {
|
||||
mark_t *current;
|
||||
TAILQ_FOREACH(current, &(con->marks_head), marks) {
|
||||
if (strcmp(current->name, mark) == 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Toggles the mark on a container.
|
||||
* If the container already has this mark, the mark is removed.
|
||||
@ -529,7 +544,7 @@ void con_mark_toggle(Con *con, const char *mark) {
|
||||
assert(con != NULL);
|
||||
DLOG("Toggling mark \"%s\" on con = %p.\n", mark, con);
|
||||
|
||||
if (con->mark != NULL && strcmp(con->mark, mark) == 0) {
|
||||
if (con_has_mark(con, mark)) {
|
||||
con_unmark(mark);
|
||||
} else {
|
||||
con_mark(con, mark);
|
||||
@ -544,22 +559,13 @@ void con_mark(Con *con, const char *mark) {
|
||||
assert(con != NULL);
|
||||
DLOG("Setting mark \"%s\" on con = %p.\n", mark, con);
|
||||
|
||||
FREE(con->mark);
|
||||
con->mark = sstrdup(mark);
|
||||
con_unmark(mark);
|
||||
|
||||
mark_t *new = scalloc(1, sizeof(mark_t));
|
||||
new->name = sstrdup(mark);
|
||||
TAILQ_INSERT_TAIL(&(con->marks_head), new, marks);
|
||||
|
||||
con->mark_changed = true;
|
||||
|
||||
DLOG("Clearing the mark from all other windows.\n");
|
||||
Con *other;
|
||||
TAILQ_FOREACH(other, &all_cons, all_cons) {
|
||||
/* Skip the window we actually handled since we took care of it already. */
|
||||
if (con == other)
|
||||
continue;
|
||||
|
||||
if (other->mark != NULL && strcmp(other->mark, mark) == 0) {
|
||||
FREE(other->mark);
|
||||
other->mark_changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -572,10 +578,17 @@ void con_unmark(const char *mark) {
|
||||
if (mark == NULL) {
|
||||
DLOG("Unmarking all containers.\n");
|
||||
TAILQ_FOREACH(con, &all_cons, all_cons) {
|
||||
if (con->mark == NULL)
|
||||
if (TAILQ_EMPTY(&(con->marks_head)))
|
||||
continue;
|
||||
|
||||
FREE(con->mark);
|
||||
mark_t *current;
|
||||
while (!TAILQ_EMPTY(&(con->marks_head))) {
|
||||
current = TAILQ_FIRST(&(con->marks_head));
|
||||
FREE(current->name);
|
||||
TAILQ_REMOVE(&(con->marks_head), current, marks);
|
||||
FREE(current);
|
||||
}
|
||||
|
||||
con->mark_changed = true;
|
||||
}
|
||||
} else {
|
||||
@ -587,8 +600,18 @@ void con_unmark(const char *mark) {
|
||||
}
|
||||
|
||||
DLOG("Found mark on con = %p. Removing it now.\n", con);
|
||||
FREE(con->mark);
|
||||
con->mark_changed = true;
|
||||
|
||||
mark_t *current;
|
||||
TAILQ_FOREACH(current, &(con->marks_head), marks) {
|
||||
if (strcmp(current->name, mark) != 0)
|
||||
continue;
|
||||
|
||||
FREE(current->name);
|
||||
TAILQ_REMOVE(&(con->marks_head), current, marks);
|
||||
FREE(current);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
22
src/ipc.c
22
src/ipc.c
@ -275,9 +275,16 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
|
||||
ystr("urgent");
|
||||
y(bool, con->urgent);
|
||||
|
||||
if (con->mark != NULL) {
|
||||
ystr("mark");
|
||||
ystr(con->mark);
|
||||
if (!TAILQ_EMPTY(&(con->marks_head))) {
|
||||
ystr("marks");
|
||||
y(array_open);
|
||||
|
||||
mark_t *mark;
|
||||
TAILQ_FOREACH(mark, &(con->marks_head), marks) {
|
||||
ystr(mark->name);
|
||||
}
|
||||
|
||||
y(array_close);
|
||||
}
|
||||
|
||||
ystr("focused");
|
||||
@ -819,9 +826,12 @@ IPC_HANDLER(get_marks) {
|
||||
y(array_open);
|
||||
|
||||
Con *con;
|
||||
TAILQ_FOREACH(con, &all_cons, all_cons)
|
||||
if (con->mark != NULL)
|
||||
ystr(con->mark);
|
||||
TAILQ_FOREACH(con, &all_cons, all_cons) {
|
||||
mark_t *mark;
|
||||
TAILQ_FOREACH(mark, &(con->marks_head), marks) {
|
||||
ystr(mark->name);
|
||||
}
|
||||
}
|
||||
|
||||
y(array_close);
|
||||
|
||||
|
@ -28,6 +28,7 @@ static bool parsing_deco_rect;
|
||||
static bool parsing_window_rect;
|
||||
static bool parsing_geometry;
|
||||
static bool parsing_focus;
|
||||
static bool parsing_marks;
|
||||
struct Match *current_swallow;
|
||||
|
||||
/* This list is used for reordering the focus stack after parsing the 'focus'
|
||||
@ -159,12 +160,16 @@ static int json_end_map(void *ctx) {
|
||||
|
||||
static int json_end_array(void *ctx) {
|
||||
LOG("end of array\n");
|
||||
if (!parsing_swallows && !parsing_focus) {
|
||||
if (!parsing_swallows && !parsing_focus && !parsing_marks) {
|
||||
con_fix_percent(json_node);
|
||||
}
|
||||
if (parsing_swallows) {
|
||||
parsing_swallows = false;
|
||||
}
|
||||
if (parsing_marks) {
|
||||
parsing_marks = false;
|
||||
}
|
||||
|
||||
if (parsing_focus) {
|
||||
/* Clear the list of focus mappings */
|
||||
struct focus_mapping *mapping;
|
||||
@ -214,6 +219,9 @@ static int json_key(void *ctx, const unsigned char *val, size_t len) {
|
||||
if (strcasecmp(last_key, "focus") == 0)
|
||||
parsing_focus = true;
|
||||
|
||||
if (strcasecmp(last_key, "marks") == 0)
|
||||
parsing_marks = true;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -234,6 +242,11 @@ static int json_string(void *ctx, const unsigned char *val, size_t len) {
|
||||
ELOG("swallow key %s unknown\n", last_key);
|
||||
}
|
||||
free(sval);
|
||||
} else if (parsing_marks) {
|
||||
char *mark;
|
||||
sasprintf(&mark, "%.*s", (int)len, val);
|
||||
|
||||
con_mark(json_node, mark);
|
||||
} else {
|
||||
if (strcasecmp(last_key, "name") == 0) {
|
||||
json_node->name = scalloc(len + 1, 1);
|
||||
@ -336,13 +349,12 @@ static int json_string(void *ctx, const unsigned char *val, size_t len) {
|
||||
LOG("Unhandled \"last_splitlayout\": %s\n", buf);
|
||||
free(buf);
|
||||
} else if (strcasecmp(last_key, "mark") == 0) {
|
||||
DLOG("Found deprecated key \"mark\".\n");
|
||||
|
||||
char *buf = NULL;
|
||||
sasprintf(&buf, "%.*s", (int)len, val);
|
||||
|
||||
/* We unmark any containers using this mark to avoid duplicates. */
|
||||
con_unmark(buf);
|
||||
|
||||
json_node->mark = buf;
|
||||
con_mark(json_node, buf);
|
||||
} else if (strcasecmp(last_key, "floating") == 0) {
|
||||
char *buf = NULL;
|
||||
sasprintf(&buf, "%.*s", (int)len, val);
|
||||
@ -589,6 +601,7 @@ void tree_append_json(Con *con, const char *filename, char **errormsg) {
|
||||
parsing_window_rect = false;
|
||||
parsing_geometry = false;
|
||||
parsing_focus = false;
|
||||
parsing_marks = false;
|
||||
setlocale(LC_NUMERIC, "C");
|
||||
stat = yajl_parse(hand, (const unsigned char *)buf, n);
|
||||
if (stat != yajl_status_ok) {
|
||||
|
38
src/x.c
38
src/x.c
@ -545,18 +545,34 @@ void x_draw_decoration(Con *con) {
|
||||
int indent_px = (indent_level * 5) * indent_mult;
|
||||
|
||||
int mark_width = 0;
|
||||
if (config.show_marks && con->mark != NULL && (con->mark)[0] != '_') {
|
||||
char *formatted_mark;
|
||||
sasprintf(&formatted_mark, "[%s]", con->mark);
|
||||
i3String *mark = i3string_from_utf8(formatted_mark);
|
||||
if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
|
||||
char *formatted_mark = sstrdup("");
|
||||
bool had_visible_mark = false;
|
||||
|
||||
mark_t *mark;
|
||||
TAILQ_FOREACH(mark, &(con->marks_head), marks) {
|
||||
if (mark->name[0] == '_')
|
||||
continue;
|
||||
had_visible_mark = true;
|
||||
|
||||
char *buf;
|
||||
sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
|
||||
free(formatted_mark);
|
||||
formatted_mark = buf;
|
||||
}
|
||||
|
||||
if (had_visible_mark) {
|
||||
i3String *mark = i3string_from_utf8(formatted_mark);
|
||||
mark_width = predict_text_width(mark);
|
||||
|
||||
draw_text(mark, parent->pixmap, parent->pm_gc, NULL,
|
||||
con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
|
||||
con->deco_rect.y + text_offset_y, mark_width);
|
||||
|
||||
I3STRING_FREE(mark);
|
||||
}
|
||||
|
||||
FREE(formatted_mark);
|
||||
mark_width = predict_text_width(mark);
|
||||
|
||||
draw_text(mark, parent->pixmap, parent->pm_gc, NULL,
|
||||
con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
|
||||
con->deco_rect.y + text_offset_y, mark_width);
|
||||
|
||||
I3STRING_FREE(mark);
|
||||
}
|
||||
|
||||
i3String *title = win->title_format == NULL ? win->name : window_parse_title_format(win);
|
||||
|
Reference in New Issue
Block a user