From 53541817effb71f65798a2dafecdf06d1633fd33 Mon Sep 17 00:00:00 2001 From: Jeremy O'Brien Date: Tue, 24 Jan 2012 18:00:27 -0500 Subject: [PATCH] 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 --- docs/userguide | 4 ++++ include/data.h | 8 ++++++++ parser-specs/commands.spec | 1 + src/cfgparse.l | 1 + src/cfgparse.y | 15 +++++++++++++++ src/commands.c | 13 +++++++++++++ src/handlers.c | 8 ++++++++ src/match.c | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 84 insertions(+) diff --git a/docs/userguide b/docs/userguide index d4fefafd..c7534552 100644 --- a/docs/userguide +++ b/docs/userguide @@ -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 <>. con_id:: diff --git a/include/data.h b/include/data.h index fe648d3d..d7ba8af4 100644 --- a/include/data.h +++ b/include/data.h @@ -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, diff --git a/parser-specs/commands.spec b/parser-specs/commands.spec index 014da222..bbd7ab28 100644 --- a/parser-specs/commands.spec +++ b/parser-specs/commands.spec @@ -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: diff --git a/src/cfgparse.l b/src/cfgparse.l index aa090447..d829b336 100644 --- a/src/cfgparse.l +++ b/src/cfgparse.l @@ -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); diff --git a/src/cfgparse.y b/src/cfgparse.y index 86d92829..e357fb70 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -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 %type 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: diff --git a/src/commands.c b/src/commands.c index 272ba5ca..80be16cf 100644 --- a/src/commands.c +++ b/src/commands.c @@ -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. */ diff --git a/src/handlers.c b/src/handlers.c index a1f90ba6..ac57e0a4 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -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; diff --git a/src/match.c b/src/match.c index c2773acc..c460d422 100644 --- a/src/match.c +++ b/src/match.c @@ -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) || -- 2.39.5