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