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