rofi 1.7.7
window.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#define G_LOG_DOMAIN "Modes.Window"
30
31#include "config.h"
32
33#ifdef WINDOW_MODE
34
35#include <errno.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <strings.h>
41#include <unistd.h>
42#include <xcb/xcb.h>
43#include <xcb/xcb_atom.h>
44#include <xcb/xcb_ewmh.h>
45#include <xcb/xcb_icccm.h>
46
47#include <glib.h>
48
49#include "xcb-internal.h"
50#include "xcb.h"
51
52#include "helper.h"
53#include "modes/window.h"
54#include "rofi.h"
55#include "settings.h"
56#include "widgets/textbox.h"
57
58#include "timings.h"
59
60#include "mode-private.h"
61#include "rofi-icon-fetcher.h"
62
63#define WINLIST 32
64
65#define CLIENTSTATE 10
66#define CLIENTWINDOWTYPE 10
67
68// Fields to match in window mode
69typedef struct {
70 char *field_name;
71 gboolean enabled;
72} WinModeField;
73
74typedef enum {
75 WIN_MATCH_FIELD_TITLE,
76 WIN_MATCH_FIELD_CLASS,
77 WIN_MATCH_FIELD_ROLE,
78 WIN_MATCH_FIELD_NAME,
79 WIN_MATCH_FIELD_DESKTOP,
80 WIN_MATCH_NUM_FIELDS,
81} WinModeMatchingFields;
82
83static WinModeField matching_window_fields[WIN_MATCH_NUM_FIELDS] = {
84 {
85 .field_name = "title",
86 .enabled = TRUE,
87 },
88 {
89 .field_name = "class",
90 .enabled = TRUE,
91 },
92 {
93 .field_name = "role",
94 .enabled = TRUE,
95 },
96 {
97 .field_name = "name",
98 .enabled = TRUE,
99 },
100 {
101 .field_name = "desktop",
102 .enabled = TRUE,
103 }};
104
105static gboolean window_matching_fields_parsed = FALSE;
106
107// a manageable window
108typedef struct {
109 xcb_window_t window;
110 xcb_get_window_attributes_reply_t xattr;
111 char *title;
112 char *class;
113 char *name;
114 char *role;
115 int states;
116 xcb_atom_t state[CLIENTSTATE];
117 int window_types;
118 xcb_atom_t window_type[CLIENTWINDOWTYPE];
119 int active;
120 int demands;
121 long hint_flags;
122 uint32_t wmdesktop;
123 char *wmdesktopstr;
124 unsigned int wmdesktopstr_len;
125 cairo_surface_t *icon;
126 gboolean icon_checked;
127 uint32_t icon_fetch_uid;
128 uint32_t icon_fetch_size;
129 gboolean thumbnail_checked;
130 gboolean icon_theme_checked;
131} client;
132
133// window lists
134typedef struct {
135 xcb_window_t *array;
136 client **data;
137 int len;
138} winlist;
139
140typedef struct {
141 unsigned int id;
142 winlist *ids;
143 // Current window.
144 unsigned int index;
145 char *cache;
146 unsigned int wmdn_len;
147 unsigned int clf_len;
148 unsigned int name_len;
149 unsigned int title_len;
150 unsigned int role_len;
151 GRegex *window_regex;
152 // Hide current active window
153 gboolean hide_active_window;
154 gboolean prefer_icon_theme;
155} WindowModePrivateData;
156
157winlist *cache_client = NULL;
158
164static winlist *winlist_new(void) {
165 winlist *l = g_malloc(sizeof(winlist));
166 l->len = 0;
167 l->array = g_malloc_n(WINLIST + 1, sizeof(xcb_window_t));
168 l->data = g_malloc_n(WINLIST + 1, sizeof(client *));
169 return l;
170}
171
181static int winlist_append(winlist *l, xcb_window_t w, client *d) {
182 if (l->len > 0 && !(l->len % WINLIST)) {
183 l->array =
184 g_realloc(l->array, sizeof(xcb_window_t) * (l->len + WINLIST + 1));
185 l->data = g_realloc(l->data, sizeof(client *) * (l->len + WINLIST + 1));
186 }
187 // Make clang-check happy.
188 // TODO: make clang-check clear this should never be 0.
189 if (l->data == NULL || l->array == NULL) {
190 return -1;
191 }
192
193 l->data[l->len] = d;
194 l->array[l->len++] = w;
195 return l->len - 1;
196}
197
198static void client_free(client *c) {
199 if (c == NULL) {
200 return;
201 }
202 if (c->icon) {
203 cairo_surface_destroy(c->icon);
204 }
205 g_free(c->title);
206 g_free(c->class);
207 g_free(c->name);
208 g_free(c->role);
209 g_free(c->wmdesktopstr);
210}
211static void winlist_empty(winlist *l) {
212 while (l->len > 0) {
213 client *c = l->data[--l->len];
214 if (c != NULL) {
215 client_free(c);
216 g_free(c);
217 }
218 }
219}
220
226static void winlist_free(winlist *l) {
227 if (l != NULL) {
228 winlist_empty(l);
229 g_free(l->array);
230 g_free(l->data);
231 g_free(l);
232 }
233}
234
243static int winlist_find(winlist *l, xcb_window_t w) {
244 if (l == NULL) {
245 return -1;
246 }
247 // iterate backwards. Theory is: windows most often accessed will be
248 // nearer the end. Testing with kcachegrind seems to support this...
249 int i;
250
251 for (i = (l->len - 1); i >= 0; i--) {
252 if (l->array[i] == w) {
253 return i;
254 }
255 }
256
257 return -1;
258}
262static void x11_cache_create(void) {
263 if (cache_client == NULL) {
264 cache_client = winlist_new();
265 }
266}
267
271static void x11_cache_free(void) {
272 winlist_free(cache_client);
273 cache_client = NULL;
274}
275
285static xcb_get_window_attributes_reply_t *
286window_get_attributes(xcb_window_t w) {
287 xcb_get_window_attributes_cookie_t c =
288 xcb_get_window_attributes(xcb->connection, w);
289 xcb_get_window_attributes_reply_t *r =
290 xcb_get_window_attributes_reply(xcb->connection, c, NULL);
291 if (r) {
292 return r;
293 }
294 return NULL;
295}
296// _NET_WM_STATE_*
297static int client_has_state(client *c, xcb_atom_t state) {
298 for (int i = 0; i < c->states; i++) {
299 if (c->state[i] == state) {
300 return 1;
301 }
302 }
303
304 return 0;
305}
306static int client_has_window_type(client *c, xcb_atom_t type) {
307 for (int i = 0; i < c->window_types; i++) {
308 if (c->window_type[i] == type) {
309 return 1;
310 }
311 }
312
313 return 0;
314}
315
316static client *window_client(WindowModePrivateData *pd, xcb_window_t win) {
317 if (win == XCB_WINDOW_NONE) {
318 return NULL;
319 }
320
321 int idx = winlist_find(cache_client, win);
322
323 if (idx >= 0) {
324 return cache_client->data[idx];
325 }
326
327 // if this fails, we're up that creek
328 xcb_get_window_attributes_reply_t *attr = window_get_attributes(win);
329
330 if (!attr) {
331 return NULL;
332 }
333 client *c = g_malloc0(sizeof(client));
334 c->window = win;
335
336 // copy xattr so we don't have to care when stuff is freed
337 memmove(&c->xattr, attr, sizeof(xcb_get_window_attributes_reply_t));
338
339 xcb_get_property_cookie_t cky = xcb_ewmh_get_wm_state(&xcb->ewmh, win);
340 xcb_ewmh_get_atoms_reply_t states;
341 if (xcb_ewmh_get_wm_state_reply(&xcb->ewmh, cky, &states, NULL)) {
342 c->states = MIN(CLIENTSTATE, states.atoms_len);
343 memcpy(c->state, states.atoms,
344 MIN(CLIENTSTATE, states.atoms_len) * sizeof(xcb_atom_t));
345 xcb_ewmh_get_atoms_reply_wipe(&states);
346 }
347 cky = xcb_ewmh_get_wm_window_type(&xcb->ewmh, win);
348 if (xcb_ewmh_get_wm_window_type_reply(&xcb->ewmh, cky, &states, NULL)) {
349 c->window_types = MIN(CLIENTWINDOWTYPE, states.atoms_len);
350 memcpy(c->window_type, states.atoms,
351 MIN(CLIENTWINDOWTYPE, states.atoms_len) * sizeof(xcb_atom_t));
352 xcb_ewmh_get_atoms_reply_wipe(&states);
353 }
354
355 char *tmp_title = window_get_text_prop(c->window, xcb->ewmh._NET_WM_NAME);
356 if (tmp_title == NULL) {
357 tmp_title = window_get_text_prop(c->window, XCB_ATOM_WM_NAME);
358 }
359 if (tmp_title != NULL) {
360 c->title = g_markup_escape_text(tmp_title, -1);
361 } else {
362 c->title = g_strdup("<i>no title set</i>");
363 }
364 pd->title_len =
365 MAX(c->title ? g_utf8_strlen(c->title, -1) : 0, pd->title_len);
366 g_free(tmp_title);
367
368 char *tmp_role = window_get_text_prop(c->window, netatoms[WM_WINDOW_ROLE]);
369 c->role = g_markup_escape_text(tmp_role ? tmp_role : "", -1);
370 pd->role_len = MAX(c->role ? g_utf8_strlen(c->role, -1) : 0, pd->role_len);
371 g_free(tmp_role);
372
373 cky = xcb_icccm_get_wm_class(xcb->connection, c->window);
374 xcb_icccm_get_wm_class_reply_t wcr;
375 if (xcb_icccm_get_wm_class_reply(xcb->connection, cky, &wcr, NULL)) {
376 c->class = g_markup_escape_text(wcr.class_name, -1);
377 c->name = g_markup_escape_text(wcr.instance_name, -1);
378 pd->name_len = MAX(c->name ? g_utf8_strlen(c->name, -1) : 0, pd->name_len);
379 xcb_icccm_get_wm_class_reply_wipe(&wcr);
380 }
381
382 xcb_get_property_cookie_t cc =
383 xcb_icccm_get_wm_hints(xcb->connection, c->window);
384 xcb_icccm_wm_hints_t r;
385 if (xcb_icccm_get_wm_hints_reply(xcb->connection, cc, &r, NULL)) {
386 c->hint_flags = r.flags;
387 }
388
389 idx = winlist_append(cache_client, c->window, c);
390 // Should never happen.
391 if (idx < 0) {
392 client_free(c);
393 g_free(c);
394 c = NULL;
395 }
396 g_free(attr);
397 return c;
398}
399
400guint window_reload_timeout = 0;
401static gboolean window_client_reload(G_GNUC_UNUSED void *data) {
402 window_reload_timeout = 0;
403 if (window_mode.private_data) {
404 window_mode._destroy(&window_mode);
405 window_mode._init(&window_mode);
406 }
407 if (window_mode_cd.private_data) {
408 window_mode_cd._destroy(&window_mode_cd);
409 window_mode_cd._init(&window_mode_cd);
410 }
411 if (window_mode.private_data || window_mode_cd.private_data) {
413 }
414 return G_SOURCE_REMOVE;
415}
416void window_client_handle_signal(G_GNUC_UNUSED xcb_window_t win,
417 G_GNUC_UNUSED gboolean create) {
418 // g_idle_add_full(G_PRIORITY_HIGH_IDLE, window_client_reload, NULL, NULL);
419 if (window_reload_timeout > 0) {
420 g_source_remove(window_reload_timeout);
421 window_reload_timeout = 0;
422 }
423 window_reload_timeout = g_timeout_add(100, window_client_reload, NULL);
424}
425static int window_match(const Mode *sw, rofi_int_matcher **tokens,
426 unsigned int index) {
427 WindowModePrivateData *rmpd =
428 (WindowModePrivateData *)mode_get_private_data(sw);
429 int match = 1;
430 const winlist *ids = (winlist *)rmpd->ids;
431 // Want to pull directly out of cache, X calls are not thread safe.
432 int idx = winlist_find(cache_client, ids->array[index]);
433 g_assert(idx >= 0);
434 client *c = cache_client->data[idx];
435
436 if (tokens) {
437 for (int j = 0; match && tokens[j] != NULL; j++) {
438 int test = 0;
439 // Dirty hack. Normally helper_token_match does _all_ the matching,
440 // Now we want it to match only one item at the time.
441 // If hack not in place it would not match queries spanning multiple
442 // fields. e.g. when searching 'title element' and 'class element'
443 rofi_int_matcher *ftokens[2] = {tokens[j], NULL};
444 if (c->title != NULL && c->title[0] != '\0' &&
445 matching_window_fields[WIN_MATCH_FIELD_TITLE].enabled) {
446 test = helper_token_match(ftokens, c->title);
447 }
448
449 if (test == tokens[j]->invert && c->class != NULL &&
450 c->class[0] != '\0' &&
451 matching_window_fields[WIN_MATCH_FIELD_CLASS].enabled) {
452 test = helper_token_match(ftokens, c->class);
453 }
454
455 if (test == tokens[j]->invert && c->role != NULL && c->role[0] != '\0' &&
456 matching_window_fields[WIN_MATCH_FIELD_ROLE].enabled) {
457 test = helper_token_match(ftokens, c->role);
458 }
459
460 if (test == tokens[j]->invert && c->name != NULL && c->name[0] != '\0' &&
461 matching_window_fields[WIN_MATCH_FIELD_NAME].enabled) {
462 test = helper_token_match(ftokens, c->name);
463 }
464 if (test == tokens[j]->invert && c->wmdesktopstr != NULL &&
465 c->wmdesktopstr[0] != '\0' &&
466 matching_window_fields[WIN_MATCH_FIELD_DESKTOP].enabled) {
467 test = helper_token_match(ftokens, c->wmdesktopstr);
468 }
469
470 if (test == 0) {
471 match = 0;
472 }
473 }
474 }
475
476 return match;
477}
478
479static void window_mode_parse_fields(void) {
480 window_matching_fields_parsed = TRUE;
481 char *savept = NULL;
482 // Make a copy, as strtok will modify it.
483 char *switcher_str = g_strdup(config.window_match_fields);
484 const char *const sep = ",#";
485 // Split token on ','. This modifies switcher_str.
486 for (unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
487 matching_window_fields[i].enabled = FALSE;
488 }
489 for (char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
490 token = strtok_r(NULL, sep, &savept)) {
491 if (strcmp(token, "all") == 0) {
492 for (unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
493 matching_window_fields[i].enabled = TRUE;
494 }
495 break;
496 }
497 gboolean matched = FALSE;
498 for (unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
499 const char *field_name = matching_window_fields[i].field_name;
500 if (strcmp(token, field_name) == 0) {
501 matching_window_fields[i].enabled = TRUE;
502 matched = TRUE;
503 }
504 }
505 if (!matched) {
506 g_warning("Invalid window field name :%s", token);
507 }
508 }
509 // Free string that was modified by strtok_r
510 g_free(switcher_str);
511}
512
513static unsigned int window_mode_get_num_entries(const Mode *sw) {
514 const WindowModePrivateData *pd =
515 (const WindowModePrivateData *)mode_get_private_data(sw);
516
517 return pd->ids ? pd->ids->len : 0;
518}
523const char *invalid_desktop_name = "n/a";
524static const char *_window_name_list_entry(const char *str, uint32_t length,
525 int entry) {
526 uint32_t offset = 0;
527 int index = 0;
528 while (index < entry && offset < length) {
529 if (str[offset] == 0) {
530 index++;
531 }
532 offset++;
533 }
534 if (offset >= length) {
535 return invalid_desktop_name;
536 }
537 return &str[offset];
538}
539static void _window_mode_load_data(Mode *sw, unsigned int cd) {
540 WindowModePrivateData *pd =
541 (WindowModePrivateData *)mode_get_private_data(sw);
542 // find window list
543 xcb_window_t curr_win_id;
544 int found = 0;
545
546 // Create cache
547
548 x11_cache_create();
549 xcb_get_property_cookie_t c =
550 xcb_ewmh_get_active_window(&(xcb->ewmh), xcb->screen_nbr);
551 if (!xcb_ewmh_get_active_window_reply(&xcb->ewmh, c, &curr_win_id, NULL)) {
552 curr_win_id = 0;
553 }
554
555 // Get the current desktop.
556 unsigned int current_desktop = 0;
557 c = xcb_ewmh_get_current_desktop(&xcb->ewmh, xcb->screen_nbr);
558 if (!xcb_ewmh_get_current_desktop_reply(&xcb->ewmh, c, &current_desktop,
559 NULL)) {
560 current_desktop = 0;
561 }
562
563 g_debug("Get list from: %d", xcb->screen_nbr);
564 c = xcb_ewmh_get_client_list_stacking(&xcb->ewmh, xcb->screen_nbr);
565 xcb_ewmh_get_windows_reply_t clients = {
566 0,
567 };
568 if (xcb_ewmh_get_client_list_stacking_reply(&xcb->ewmh, c, &clients, NULL)) {
569 found = 1;
570 } else {
571 c = xcb_ewmh_get_client_list(&xcb->ewmh, xcb->screen_nbr);
572 if (xcb_ewmh_get_client_list_reply(&xcb->ewmh, c, &clients, NULL)) {
573 found = 1;
574 }
575 }
576 if (!found) {
577 return;
578 }
579
580 if (clients.windows_len > 0) {
581 int i;
582 // windows we actually display. May be slightly different to
583 // _NET_CLIENT_LIST_STACKING if we happen to have a window destroyed while
584 // we're working...
585 pd->ids = winlist_new();
586
587 int has_names = FALSE;
588 ssize_t ws_names_length = 0;
589 char *ws_names = NULL;
590 xcb_get_property_cookie_t prop_cookie =
591 xcb_ewmh_get_desktop_names(&xcb->ewmh, xcb->screen_nbr);
592 xcb_ewmh_get_utf8_strings_reply_t names;
593 if (xcb_ewmh_get_desktop_names_reply(&xcb->ewmh, prop_cookie, &names,
594 NULL)) {
595 ws_names_length = names.strings_len;
596 ws_names = g_malloc0_n(names.strings_len + 1, sizeof(char));
597 memcpy(ws_names, names.strings, names.strings_len);
598 has_names = TRUE;
599 xcb_ewmh_get_utf8_strings_reply_wipe(&names);
600 }
601 // calc widths of fields
602 for (i = clients.windows_len - 1; i > -1; i--) {
603 client *winclient = window_client(pd, clients.windows[i]);
604 if ((winclient != NULL) && !winclient->xattr.override_redirect &&
605 !client_has_window_type(winclient,
606 xcb->ewmh._NET_WM_WINDOW_TYPE_DOCK) &&
607 !client_has_window_type(winclient,
608 xcb->ewmh._NET_WM_WINDOW_TYPE_DESKTOP) &&
609 !client_has_state(winclient, xcb->ewmh._NET_WM_STATE_SKIP_PAGER) &&
610 !client_has_state(winclient, xcb->ewmh._NET_WM_STATE_SKIP_TASKBAR)) {
611 pd->clf_len =
612 MAX(pd->clf_len, (winclient->class != NULL)
613 ? (g_utf8_strlen(winclient->class, -1))
614 : 0);
615
616 if (client_has_state(winclient,
617 xcb->ewmh._NET_WM_STATE_DEMANDS_ATTENTION)) {
618 winclient->demands = TRUE;
619 }
620 if ((winclient->hint_flags & XCB_ICCCM_WM_HINT_X_URGENCY) != 0) {
621 winclient->demands = TRUE;
622 }
623
624 if (winclient->window == curr_win_id) {
625 winclient->active = TRUE;
626 }
627 // find client's desktop.
628 xcb_get_property_cookie_t cookie;
629 xcb_get_property_reply_t *r;
630
631 winclient->wmdesktop = 0xFFFFFFFF;
632 cookie = xcb_get_property(xcb->connection, 0, winclient->window,
633 xcb->ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL,
634 0, 1);
635 r = xcb_get_property_reply(xcb->connection, cookie, NULL);
636 if (r) {
637 if (r->type == XCB_ATOM_CARDINAL) {
638 winclient->wmdesktop = *((uint32_t *)xcb_get_property_value(r));
639 }
640 free(r);
641 }
642 if (winclient->wmdesktop != 0xFFFFFFFF) {
643 if (has_names) {
646 char *output = NULL;
647 if (pango_parse_markup(
648 _window_name_list_entry(ws_names, ws_names_length,
649 winclient->wmdesktop),
650 -1, 0, NULL, &output, NULL, NULL)) {
651 winclient->wmdesktopstr = g_strdup(_window_name_list_entry(
652 ws_names, ws_names_length, winclient->wmdesktop));
653 winclient->wmdesktopstr_len = g_utf8_strlen(output, -1);
654 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
655 g_free(output);
656 } else {
657 winclient->wmdesktopstr = g_strdup("Invalid name");
658 winclient->wmdesktopstr_len =
659 g_utf8_strlen(winclient->wmdesktopstr, -1);
660 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
661 }
662 } else {
663 winclient->wmdesktopstr = g_markup_escape_text(
664 _window_name_list_entry(ws_names, ws_names_length,
665 winclient->wmdesktop),
666 -1);
667 winclient->wmdesktopstr_len =
668 g_utf8_strlen(winclient->wmdesktopstr, -1);
669 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
670 }
671 } else {
672 winclient->wmdesktopstr =
673 g_strdup_printf("%u", (uint32_t)winclient->wmdesktop);
674 winclient->wmdesktopstr_len =
675 g_utf8_strlen(winclient->wmdesktopstr, -1);
676 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
677 }
678 } else {
679 winclient->wmdesktopstr = g_strdup("");
680 winclient->wmdesktopstr_len =
681 g_utf8_strlen(winclient->wmdesktopstr, -1);
682 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
683 }
684 if (cd && winclient->wmdesktop != current_desktop) {
685 continue;
686 }
687 if (!pd->hide_active_window || winclient->window != curr_win_id) {
688 winlist_append(pd->ids, winclient->window, NULL);
689 }
690 }
691 }
692
693 if (has_names) {
694 g_free(ws_names);
695 }
696 }
697 xcb_ewmh_get_windows_reply_wipe(&clients);
698}
699static int window_mode_init(Mode *sw) {
700 if (mode_get_private_data(sw) == NULL) {
701
702 WindowModePrivateData *pd = g_malloc0(sizeof(*pd));
703 ThemeWidget *wid = rofi_config_find_widget(sw->name, NULL, TRUE);
704 Property *p =
705 rofi_theme_find_property(wid, P_BOOLEAN, "hide-active-window", FALSE);
706 if (p && p->type == P_BOOLEAN && p->value.b == TRUE) {
707 pd->hide_active_window = TRUE;
708 }
709 // prefer icon theme selection
710 p = rofi_theme_find_property(wid, P_BOOLEAN, "prefer-icon-theme", FALSE);
711 if (p && p->type == P_BOOLEAN && p->value.b == TRUE) {
712 pd->prefer_icon_theme = TRUE;
713 }
714 pd->window_regex = g_regex_new("{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL);
715 mode_set_private_data(sw, (void *)pd);
716 _window_mode_load_data(sw, FALSE);
717 if (!window_matching_fields_parsed) {
718 window_mode_parse_fields();
719 }
720 }
721 return TRUE;
722}
723static int window_mode_init_cd(Mode *sw) {
724 if (mode_get_private_data(sw) == NULL) {
725 WindowModePrivateData *pd = g_malloc0(sizeof(*pd));
726
727 ThemeWidget *wid = rofi_config_find_widget(sw->name, NULL, TRUE);
728 Property *p =
729 rofi_theme_find_property(wid, P_BOOLEAN, "hide-active-window", FALSE);
730 if (p && p->type == P_BOOLEAN && p->value.b == TRUE) {
731 pd->hide_active_window = TRUE;
732 }
733 pd->window_regex = g_regex_new("{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL);
734 mode_set_private_data(sw, (void *)pd);
735 _window_mode_load_data(sw, TRUE);
736 if (!window_matching_fields_parsed) {
737 window_mode_parse_fields();
738 }
739 }
740 return TRUE;
741}
742
743static inline int act_on_window(xcb_window_t window) {
744 int retv = TRUE;
745 char **args = NULL;
746 int argc = 0;
747 char window_regex[100]; /* We are probably safe here */
748
749 g_snprintf(window_regex, sizeof window_regex, "%d", window);
750
751 helper_parse_setup(config.window_command, &args, &argc, "{window}",
752 window_regex, (char *)0);
753
754 GError *error = NULL;
755 g_spawn_async(NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL,
756 &error);
757 if (error != NULL) {
758 char *msg = g_strdup_printf(
759 "Failed to execute action for window: '%s'\nError: '%s'", window_regex,
760 error->message);
761 rofi_view_error_dialog(msg, FALSE);
762 g_free(msg);
763 // print error.
764 g_error_free(error);
765 retv = FALSE;
766 }
767
768 // Free the args list.
769 g_strfreev(args);
770 return retv;
771}
772
773static ModeMode window_mode_result(Mode *sw, int mretv,
774 G_GNUC_UNUSED char **input,
775 unsigned int selected_line) {
776 WindowModePrivateData *rmpd =
777 (WindowModePrivateData *)mode_get_private_data(sw);
778 ModeMode retv = MODE_EXIT;
779 if ((mretv & (MENU_OK))) {
780 if (mretv & MENU_CUSTOM_ACTION) {
781 act_on_window(rmpd->ids->array[selected_line]);
782 } else {
783 // Disable reverting input focus to previous window.
784 xcb->focus_revert = 0;
787 // Get the desktop of the client to switch to
788 uint32_t wmdesktop = 0;
789 xcb_get_property_cookie_t cookie;
790 xcb_get_property_reply_t *r;
791 // Get the current desktop.
792 unsigned int current_desktop = 0;
793 xcb_get_property_cookie_t c =
794 xcb_ewmh_get_current_desktop(&xcb->ewmh, xcb->screen_nbr);
795 if (!xcb_ewmh_get_current_desktop_reply(&xcb->ewmh, c, &current_desktop,
796 NULL)) {
797 current_desktop = 0;
798 }
799
800 cookie = xcb_get_property(
801 xcb->connection, 0, rmpd->ids->array[selected_line],
802 xcb->ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 0, 1);
803 r = xcb_get_property_reply(xcb->connection, cookie, NULL);
804 if (r && r->type == XCB_ATOM_CARDINAL) {
805 wmdesktop = *((uint32_t *)xcb_get_property_value(r));
806 }
807 if (r && r->type != XCB_ATOM_CARDINAL) {
808 // Assume the client is on all desktops.
809 wmdesktop = current_desktop;
810 }
811 free(r);
812
813 // If we have to switch the desktop, do
814 if (wmdesktop != current_desktop) {
815 xcb_ewmh_request_change_current_desktop(&xcb->ewmh, xcb->screen_nbr,
816 wmdesktop, XCB_CURRENT_TIME);
817 }
818 }
819 // Activate the window
820 xcb_ewmh_request_change_active_window(
821 &xcb->ewmh, xcb->screen_nbr, rmpd->ids->array[selected_line],
822 XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER, XCB_CURRENT_TIME,
824 xcb_flush(xcb->connection);
825 }
826 } else if ((mretv & (MENU_ENTRY_DELETE)) == MENU_ENTRY_DELETE) {
827 xcb_ewmh_request_close_window(
828 &(xcb->ewmh), xcb->screen_nbr, rmpd->ids->array[selected_line],
829 XCB_CURRENT_TIME, XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER);
830 xcb_flush(xcb->connection);
831 ThemeWidget *wid = rofi_config_find_widget(sw->name, NULL, TRUE);
832 Property *p =
833 rofi_theme_find_property(wid, P_BOOLEAN, "close-on-delete", TRUE);
834 if (p && p->type == P_BOOLEAN && p->value.b == FALSE) {
835
836 return RELOAD_DIALOG;
837 }
838 } else if ((mretv & MENU_CUSTOM_INPUT) && *input != NULL &&
839 *input[0] != '\0') {
840 GError *error = NULL;
841 gboolean run_in_term = ((mretv & MENU_CUSTOM_ACTION) == MENU_CUSTOM_ACTION);
842 gsize lf_cmd_size = 0;
843 gchar *lf_cmd = g_locale_from_utf8(*input, -1, NULL, &lf_cmd_size, &error);
844 if (error != NULL) {
845 g_warning("Failed to convert command to locale encoding: %s",
846 error->message);
847 g_error_free(error);
848 return RELOAD_DIALOG;
849 }
850
851 RofiHelperExecuteContext context = {.name = NULL};
852 if (!helper_execute_command(NULL, lf_cmd, run_in_term,
853 run_in_term ? &context : NULL)) {
854 retv = RELOAD_DIALOG;
855 }
856 g_free(lf_cmd);
857 } else if (mretv & MENU_CUSTOM_COMMAND) {
858 retv = (mretv & MENU_LOWER_MASK);
859 }
860 return retv;
861}
862
863static void window_mode_destroy(Mode *sw) {
864 WindowModePrivateData *rmpd =
865 (WindowModePrivateData *)mode_get_private_data(sw);
866 if (rmpd != NULL) {
867 winlist_free(rmpd->ids);
868 x11_cache_free();
869 g_free(rmpd->cache);
870 g_regex_unref(rmpd->window_regex);
871 g_free(rmpd);
872 mode_set_private_data(sw, NULL);
873 }
874}
875struct arg {
876 const WindowModePrivateData *pd;
877 const client *c;
878};
879
880static void helper_eval_add_str(GString *str, const char *input, int l,
881 int max_len, int nc) {
882 // g_utf8 does not work with NULL string.
883 const char *input_nn = input ? input : "";
884 // Both l and max_len are in characters, not bytes.
885 int spaces = 0;
886 if (l > 0) {
887 if (nc > l) {
888 int bl = g_utf8_offset_to_pointer(input_nn, l) - input_nn;
889 char *tmp = g_markup_escape_text(input_nn, bl);
890 g_string_append(str, tmp);
891 g_free(tmp);
892 } else {
893 spaces = l - nc;
894 char *tmp = g_markup_escape_text(input_nn, -1);
895 g_string_append(str, tmp);
896 g_free(tmp);
897 }
898 } else {
899 g_string_append(str, input_nn);
900 if (l == 0) {
901 spaces = MAX(0, max_len - nc);
902 }
903 }
904 while (spaces--) {
905 g_string_append_c(str, ' ');
906 }
907}
908static gboolean helper_eval_cb(const GMatchInfo *info, GString *str,
909 gpointer data) {
910 struct arg *d = (struct arg *)data;
911 gchar *match;
912 // Get the match
913 match = g_match_info_fetch(info, 0);
914 if (match != NULL) {
915 int l = 0;
916 if (match[2] == ':') {
917 l = (int)g_ascii_strtoll(&match[3], NULL, 10);
918 }
919 if (match[1] == 'w') {
920 helper_eval_add_str(str, d->c->wmdesktopstr, l, d->pd->wmdn_len,
921 d->c->wmdesktopstr_len);
922 } else if (match[1] == 'c') {
923 helper_eval_add_str(str, d->c->class, l, d->pd->clf_len,
924 g_utf8_strlen(d->c->class, -1));
925 } else if (match[1] == 't') {
926 helper_eval_add_str(str, d->c->title, l, d->pd->title_len,
927 g_utf8_strlen(d->c->title, -1));
928 } else if (match[1] == 'n') {
929 helper_eval_add_str(str, d->c->name, l, d->pd->name_len,
930 g_utf8_strlen(d->c->name, -1));
931 } else if (match[1] == 'r') {
932 helper_eval_add_str(str, d->c->role, l, d->pd->role_len,
933 g_utf8_strlen(d->c->role, -1));
934 }
935
936 g_free(match);
937 }
938 return FALSE;
939}
940static char *_generate_display_string(const WindowModePrivateData *pd,
941 const client *c) {
942 struct arg d = {pd, c};
943 char *res = g_regex_replace_eval(pd->window_regex, config.window_format, -1,
944 0, 0, helper_eval_cb, &d, NULL);
945 return g_strchomp(res);
946}
947
948static char *_get_display_value(const Mode *sw, unsigned int selected_line,
949 int *state, G_GNUC_UNUSED GList **list,
950 int get_entry) {
951 WindowModePrivateData *rmpd = mode_get_private_data(sw);
952 const client *c = window_client(rmpd, rmpd->ids->array[selected_line]);
953 if (c == NULL) {
954 return get_entry ? g_strdup("Window has vanished") : NULL;
955 }
956 if (c->demands) {
957 *state |= URGENT;
958 }
959 if (c->active) {
960 *state |= ACTIVE;
961 }
962 *state |= MARKUP;
963 return get_entry ? _generate_display_string(rmpd, c) : NULL;
964}
965
969static cairo_user_data_key_t data_key;
970
977static cairo_surface_t *draw_surface_from_data(uint32_t width, uint32_t height,
978 uint32_t const *const data) {
979 // limit surface size.
980 if (width >= 65536 || height >= 65536) {
981 return NULL;
982 }
983 uint32_t len = width * height;
984 uint32_t i;
985 uint32_t *buffer = g_new0(uint32_t, len);
986 cairo_surface_t *surface;
987
988 /* Cairo wants premultiplied alpha, meh :( */
989 for (i = 0; i < len; i++) {
990 uint8_t a = (data[i] >> 24) & 0xff;
991 double alpha = a / 255.0;
992 uint8_t r = ((data[i] >> 16) & 0xff) * alpha;
993 uint8_t g = ((data[i] >> 8) & 0xff) * alpha;
994 uint8_t b = ((data[i] >> 0) & 0xff) * alpha;
995 buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
996 }
997
998 surface = cairo_image_surface_create_for_data(
999 (unsigned char *)buffer, CAIRO_FORMAT_ARGB32, width, height, width * 4);
1000 /* This makes sure that buffer will be freed */
1001 cairo_surface_set_user_data(surface, &data_key, buffer, g_free);
1002
1003 return surface;
1004}
1005static cairo_surface_t *ewmh_window_icon_from_reply(xcb_get_property_reply_t *r,
1006 uint32_t preferred_size) {
1007 uint32_t *data, *end, *found_data = 0;
1008 uint32_t found_size = 0;
1009
1010 if (!r || r->type != XCB_ATOM_CARDINAL || r->format != 32 || r->length < 2) {
1011 return 0;
1012 }
1013
1014 data = (uint32_t *)xcb_get_property_value(r);
1015 if (!data) {
1016 return 0;
1017 }
1018
1019 end = data + r->length;
1020
1021 /* Goes over the icon data and picks the icon that best matches the size
1022 * preference. In case the size match is not exact, picks the closest bigger
1023 * size if present, closest smaller size otherwise.
1024 */
1025 while (data + 1 < end) {
1026 /* check whether the data size specified by width and height fits into the
1027 * array we got */
1028 uint64_t data_size = (uint64_t)data[0] * data[1];
1029 if (data_size > (uint64_t)(end - data - 2)) {
1030 break;
1031 }
1032
1033 /* use the greater of the two dimensions to match against the preferred
1034 * size
1035 */
1036 uint32_t size = MAX(data[0], data[1]);
1037
1038 /* pick the icon if it's a better match than the one we already have */
1039 gboolean found_icon_too_small = found_size < preferred_size;
1040 gboolean found_icon_too_large = found_size > preferred_size;
1041 gboolean icon_empty = data[0] == 0 || data[1] == 0;
1042 gboolean better_because_bigger = found_icon_too_small && size > found_size;
1043 gboolean better_because_smaller =
1044 found_icon_too_large && size >= preferred_size && size < found_size;
1045 if (!icon_empty &&
1046 (better_because_bigger || better_because_smaller || found_size == 0)) {
1047 found_data = data;
1048 found_size = size;
1049 }
1050
1051 data += data_size + 2;
1052 }
1053
1054 if (!found_data) {
1055 return 0;
1056 }
1057
1058 return draw_surface_from_data(found_data[0], found_data[1], found_data + 2);
1059}
1061static cairo_surface_t *get_net_wm_icon(xcb_window_t xid,
1062 uint32_t preferred_size) {
1063 xcb_get_property_cookie_t cookie = xcb_get_property_unchecked(
1064 xcb->connection, FALSE, xid, xcb->ewmh._NET_WM_ICON, XCB_ATOM_CARDINAL, 0,
1065 UINT32_MAX);
1066 xcb_get_property_reply_t *r =
1067 xcb_get_property_reply(xcb->connection, cookie, NULL);
1068 cairo_surface_t *surface = ewmh_window_icon_from_reply(r, preferred_size);
1069 free(r);
1070 return surface;
1071}
1072static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
1073 unsigned int size) {
1074 WindowModePrivateData *rmpd = mode_get_private_data(sw);
1075 client *c = window_client(rmpd, rmpd->ids->array[selected_line]);
1076 if (c == NULL) {
1077 return NULL;
1078 }
1079 if (c->icon_fetch_size != size) {
1080 if (c->icon) {
1081 cairo_surface_destroy(c->icon);
1082 c->icon = NULL;
1083 }
1084 c->thumbnail_checked = FALSE;
1085 c->icon_checked = FALSE;
1086 c->icon_theme_checked = FALSE;
1087 }
1088 if (config.window_thumbnail && c->thumbnail_checked == FALSE) {
1089 c->icon = x11_helper_get_screenshot_surface_window(c->window, size);
1090 c->thumbnail_checked = TRUE;
1091 }
1092 if (rmpd->prefer_icon_theme == FALSE) {
1093 if (c->icon == NULL && c->icon_checked == FALSE) {
1094 c->icon = get_net_wm_icon(rmpd->ids->array[selected_line], size);
1095 c->icon_checked = TRUE;
1096 }
1097 if (c->icon == NULL && c->class && c->icon_theme_checked == FALSE) {
1098 if (c->icon_fetch_uid == 0) {
1099 char *class_lower = g_utf8_strdown(c->class, -1);
1100 c->icon_fetch_uid = rofi_icon_fetcher_query(class_lower, size);
1101 g_free(class_lower);
1102 c->icon_fetch_size = size;
1103 }
1104 c->icon_theme_checked =
1105 rofi_icon_fetcher_get_ex(c->icon_fetch_uid, &(c->icon));
1106 if (c->icon) {
1107 cairo_surface_reference(c->icon);
1108 }
1109 }
1110 } else {
1111 if (c->icon == NULL && c->class && c->icon_theme_checked == FALSE) {
1112 if (c->icon_fetch_uid == 0) {
1113 char *class_lower = g_utf8_strdown(c->class, -1);
1114 c->icon_fetch_uid = rofi_icon_fetcher_query(class_lower, size);
1115 g_free(class_lower);
1116 c->icon_fetch_size = size;
1117 }
1118 c->icon_theme_checked =
1119 rofi_icon_fetcher_get_ex(c->icon_fetch_uid, &(c->icon));
1120 if (c->icon) {
1121 cairo_surface_reference(c->icon);
1122 }
1123 }
1124 if (c->icon_theme_checked == TRUE && c->icon == NULL &&
1125 c->icon_checked == FALSE) {
1126 c->icon = get_net_wm_icon(rmpd->ids->array[selected_line], size);
1127 c->icon_checked = TRUE;
1128 }
1129 }
1130 c->icon_fetch_size = size;
1131 return c->icon;
1132}
1133
1134#include "mode-private.h"
1135Mode window_mode = {.name = "window",
1136 .cfg_name_key = "display-window",
1137 ._init = window_mode_init,
1138 ._get_num_entries = window_mode_get_num_entries,
1139 ._result = window_mode_result,
1140 ._destroy = window_mode_destroy,
1141 ._token_match = window_match,
1142 ._get_display_value = _get_display_value,
1143 ._get_icon = _get_icon,
1144 ._get_completion = NULL,
1145 ._preprocess_input = NULL,
1146 .private_data = NULL,
1147 .free = NULL,
1148 .type = MODE_TYPE_SWITCHER};
1149Mode window_mode_cd = {.name = "windowcd",
1150 .cfg_name_key = "display-windowcd",
1151 ._init = window_mode_init_cd,
1152 ._get_num_entries = window_mode_get_num_entries,
1153 ._result = window_mode_result,
1154 ._destroy = window_mode_destroy,
1155 ._token_match = window_match,
1156 ._get_display_value = _get_display_value,
1157 ._get_icon = _get_icon,
1158 ._get_completion = NULL,
1159 ._preprocess_input = NULL,
1160 .private_data = NULL,
1161 .free = NULL,
1162 .type = MODE_TYPE_SWITCHER};
1163
1164#endif // WINDOW_MODE
static cairo_surface_t * _get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
static char * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **attr_list, int get_entry)
Property * rofi_theme_find_property(ThemeWidget *wid, PropertyType type, const char *property, gboolean exact)
Definition theme.c:743
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
Definition helper.c:1028
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
Definition theme.c:780
int helper_parse_setup(char *string, char ***output, int *length,...)
Definition helper.c:76
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition helper.c:515
gboolean rofi_icon_fetcher_get_ex(const uint32_t uid, cairo_surface_t **surface)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
struct rofi_mode Mode
Definition mode.h:44
void * mode_get_private_data(const Mode *mode)
Definition mode.c:171
void mode_set_private_data(Mode *mode, void *pd)
Definition mode.c:176
ModeMode
Definition mode.h:49
@ MENU_CUSTOM_COMMAND
Definition mode.h:79
@ MENU_LOWER_MASK
Definition mode.h:87
@ MENU_ENTRY_DELETE
Definition mode.h:75
@ MENU_CUSTOM_ACTION
Definition mode.h:85
@ MENU_OK
Definition mode.h:67
@ MENU_CUSTOM_INPUT
Definition mode.h:73
@ MODE_EXIT
Definition mode.h:51
@ RELOAD_DIALOG
Definition mode.h:55
@ URGENT
Definition textbox.h:106
@ ACTIVE
Definition textbox.h:108
@ MARKUP
Definition textbox.h:112
void rofi_view_hide(void)
Definition view.c:2616
void rofi_view_reload(void)
Definition view.c:586
xcb_window_t rofi_view_get_window(void)
Definition view.c:2793
int rofi_view_error_dialog(const char *msg, int markup)
Definition view.c:2574
struct _icon icon
Definition icon.h:44
@ MODE_TYPE_SWITCHER
@ P_BOOLEAN
Definition rofi-types.h:18
struct rofi_int_matcher_t rofi_int_matcher
Settings config
PropertyValue value
Definition rofi-types.h:293
PropertyType type
Definition rofi-types.h:291
char * name
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
Definition xcb.c:387
xcb_stuff * xcb
Definition xcb.c:101
WindowManagerQuirk current_window_manager
Definition xcb.c:85
xcb_atom_t netatoms[NUM_NETATOMS]
Definition xcb.c:113
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
Definition xcb.c:286
@ WM_PANGO_WORKSPACE_NAMES
Definition xcb.h:211
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP
Definition xcb.h:209