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