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