Implement urgency flag matcher

Currently it supports the following options:
"oldest": match the first window that triggered an urgent event
"latest": match the last window that triggered an urgent event
This commit is contained in:
Jeremy O'Brien 2012-01-24 18:00:27 -05:00 committed by Michael Stapelberg
parent 23abfcf7f2
commit 53541817ef
8 changed files with 84 additions and 0 deletions

View File

@ -1092,6 +1092,10 @@ id::
Compares the X11 window ID, which you can get via +xwininfo+ for example.
title::
Compares the X11 window title (_NET_WM_NAME or WM_NAME as fallback).
urgent::
Compares the urgent state of the window. Can be "latest" or "oldest".
Matches the latest or oldest urgent window, respectively.
(The following aliases are also available: newest, last, recent, first)
con_mark::
Compares the mark set for this container, see <<vim_like_marks>>.
con_id::

View File

@ -304,6 +304,9 @@ struct Window {
/** Whether the application needs to receive WM_TAKE_FOCUS */
bool needs_take_focus;
/** When this window was marked urgent. 0 means not urgent */
time_t urgent;
/** Whether this window accepts focus. We store this inverted so that the
* default will be 'accepts focus'. */
bool doesnt_accept_focus;
@ -335,6 +338,11 @@ struct Match {
struct regex *instance;
struct regex *mark;
struct regex *role;
enum {
U_DONTCHECK = -1,
U_LATEST = 0,
U_OLDEST = 1
} urgent;
enum {
M_DONTCHECK = -1,
M_NODOCK = 0,

View File

@ -43,6 +43,7 @@ state CRITERIA:
ctype = 'id' -> CRITERION
ctype = 'con_mark' -> CRITERION
ctype = 'title' -> CRITERION
ctype = 'urgent' -> CRITERION
']' -> call cmd_criteria_match_windows(); INITIAL
state CRITERION:

View File

@ -250,6 +250,7 @@ id { yy_push_state(WANT_QSTRING); return TOK_ID; }
con_id { yy_push_state(WANT_QSTRING); return TOK_CON_ID; }
con_mark { yy_push_state(WANT_QSTRING); return TOK_MARK; }
title { yy_push_state(WANT_QSTRING); return TOK_TITLE; }
urgent { yy_push_state(WANT_QSTRING); return TOK_URGENT; }
<*>{EOL} {
FREE(context->line_copy);

View File

@ -740,6 +740,7 @@ void parse_file(const char *f) {
%token TOK_ID "id"
%token TOK_CON_ID "con_id"
%token TOK_TITLE "title"
%token TOK_URGENT "urgent"
%type <binding> binding
%type <binding> bindcode
@ -953,6 +954,20 @@ criterion:
current_match.title = regex_new($3);
free($3);
}
| TOK_URGENT '=' STR
{
printf("criteria: urgent = %s\n", $3);
if (strcasecmp($3, "latest") == 0 ||
strcasecmp($3, "newest") == 0 ||
strcasecmp($3, "recent") == 0 ||
strcasecmp($3, "last") == 0) {
current_match.urgent = U_LATEST;
} else if (strcasecmp($3, "oldest") == 0 ||
strcasecmp($3, "first") == 0) {
current_match.urgent = U_OLDEST;
}
free($3);
}
;
qstring_or_number:

View File

@ -307,6 +307,19 @@ char *cmd_criteria_add(Match *current_match, char *ctype, char *cvalue) {
return NULL;
}
if (strcmp(ctype, "urgent") == 0) {
if (strcasecmp(cvalue, "latest") == 0 ||
strcasecmp(cvalue, "newest") == 0 ||
strcasecmp(cvalue, "recent") == 0 ||
strcasecmp(cvalue, "last") == 0) {
current_match->urgent = U_LATEST;
} else if (strcasecmp(cvalue, "oldest") == 0 ||
strcasecmp(cvalue, "first") == 0) {
current_match->urgent = U_OLDEST;
}
return NULL;
}
ELOG("Unknown criterion: %s\n", ctype);
/* This command is internal and does not generate a JSON reply. */

View File

@ -841,12 +841,20 @@ static bool handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_
if (!con->urgent && focused == con) {
DLOG("Ignoring urgency flag for current client\n");
con->window->urgent = 0;
goto end;
}
/* Update the flag on the client directly */
con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
//CLIENT_LOG(con);
if (con->window) {
if (con->urgent) {
con->window->urgent = time(NULL);
} else {
con->window->urgent = 0;
}
}
LOG("Urgency flag changed to %d\n", con->urgent);
Con *ws;

View File

@ -22,6 +22,7 @@
void match_init(Match *match) {
memset(match, 0, sizeof(Match));
match->dock = -1;
match->urgent = U_DONTCHECK;
}
/*
@ -39,6 +40,7 @@ bool match_is_empty(Match *match) {
match->class == NULL &&
match->instance == NULL &&
match->role == NULL &&
match->urgent == U_DONTCHECK &&
match->id == XCB_NONE &&
match->con_id == NULL &&
match->dock == -1 &&
@ -120,6 +122,38 @@ bool match_matches_window(Match *match, i3Window *window) {
}
}
Con *con = NULL;
if (match->urgent == U_LATEST) {
/* if the window isn't urgent, no sense in searching */
if (window->urgent == 0) {
return false;
}
/* if we find a window that is newer than this one, bail */
TAILQ_FOREACH(con, &all_cons, all_cons) {
if ((con->window != NULL) &&
(con->window->urgent > window->urgent)) {
return false;
}
}
LOG("urgent matches latest\n");
}
if (match->urgent == U_OLDEST) {
/* if the window isn't urgent, no sense in searching */
if (window->urgent == 0) {
return false;
}
/* if we find a window that is older than this one (and not 0), bail */
TAILQ_FOREACH(con, &all_cons, all_cons) {
if ((con->window != NULL) &&
(con->window->urgent != 0) &&
(con->window->urgent < window->urgent)) {
return false;
}
}
LOG("urgent matches oldest\n");
}
if (match->dock != -1) {
if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
(window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||