rofi  1.6.1
window.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2020 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 <stdlib.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <strings.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <xcb/xcb.h>
43 #include <xcb/xcb_ewmh.h>
44 #include <xcb/xcb_icccm.h>
45 #include <xcb/xcb_atom.h>
46 
47 #include <glib.h>
48 
49 #include "xcb-internal.h"
50 #include "xcb.h"
51 
52 #include "rofi.h"
53 #include "settings.h"
54 #include "helper.h"
55 #include "widgets/textbox.h"
56 #include "dialogs/window.h"
57 
58 #include "timings.h"
59 
60 #include "rofi-icon-fetcher.h"
61 
62 #define WINLIST 32
63 
64 #define CLIENTSTATE 10
65 #define CLIENTWINDOWTYPE 10
66 
67 // Fields to match in window mode
68 typedef struct
69 {
70  char *field_name;
71  gboolean enabled;
72 } WinModeField;
73 
74 typedef enum
75 {
76  WIN_MATCH_FIELD_TITLE,
77  WIN_MATCH_FIELD_CLASS,
78  WIN_MATCH_FIELD_ROLE,
79  WIN_MATCH_FIELD_NAME,
80  WIN_MATCH_FIELD_DESKTOP,
81  WIN_MATCH_NUM_FIELDS,
82 } WinModeMatchingFields;
83 
84 static WinModeField matching_window_fields[WIN_MATCH_NUM_FIELDS] = {
85  { .field_name = "title", .enabled = TRUE, },
86  { .field_name = "class", .enabled = TRUE, },
87  { .field_name = "role", .enabled = TRUE, },
88  { .field_name = "name", .enabled = TRUE, },
89  { .field_name = "desktop", .enabled = TRUE, }
90 };
91 
92 static gboolean window_matching_fields_parsed = FALSE;
93 
94 // a manageable window
95 typedef struct
96 {
97  xcb_window_t window;
98  xcb_get_window_attributes_reply_t xattr;
99  char *title;
100  char *class;
101  char *name;
102  char *role;
103  int states;
104  xcb_atom_t state[CLIENTSTATE];
105  int window_types;
106  xcb_atom_t window_type[CLIENTWINDOWTYPE];
107  int active;
108  int demands;
109  long hint_flags;
110  uint32_t wmdesktop;
111  char *wmdesktopstr;
112  cairo_surface_t *icon;
113  gboolean icon_checked;
114  uint32_t icon_fetch_uid;
115  gboolean thumbnail_checked;
116 } client;
117 
118 // window lists
119 typedef struct
120 {
121  xcb_window_t *array;
122  client **data;
123  int len;
124 } winlist;
125 
126 typedef struct
127 {
128  unsigned int id;
129  winlist *ids;
130  // Current window.
131  unsigned int index;
132  char *cache;
133  unsigned int wmdn_len;
134  unsigned int clf_len;
135  unsigned int name_len;
136  unsigned int title_len;
137  unsigned int role_len;
138  GRegex *window_regex;
139 } ModeModePrivateData;
140 
141 winlist *cache_client = NULL;
142 
148 static winlist* winlist_new ()
149 {
150  winlist *l = g_malloc ( sizeof ( winlist ) );
151  l->len = 0;
152  l->array = g_malloc_n ( WINLIST + 1, sizeof ( xcb_window_t ) );
153  l->data = g_malloc_n ( WINLIST + 1, sizeof ( client* ) );
154  return l;
155 }
156 
166 static int winlist_append ( winlist *l, xcb_window_t w, client *d )
167 {
168  if ( l->len > 0 && !( l->len % WINLIST ) ) {
169  l->array = g_realloc ( l->array, sizeof ( xcb_window_t ) * ( l->len + WINLIST + 1 ) );
170  l->data = g_realloc ( l->data, sizeof ( client* ) * ( l->len + WINLIST + 1 ) );
171  }
172  // Make clang-check happy.
173  // TODO: make clang-check clear this should never be 0.
174  if ( l->data == NULL || l->array == NULL ) {
175  return 0;
176  }
177 
178  l->data[l->len] = d;
179  l->array[l->len++] = w;
180  return l->len - 1;
181 }
182 
183 static void winlist_empty ( winlist *l )
184 {
185  while ( l->len > 0 ) {
186  client *c = l->data[--l->len];
187  if ( c != NULL ) {
188  if ( c->icon ) {
189  cairo_surface_destroy ( c->icon );
190  }
191  g_free ( c->title );
192  g_free ( c->class );
193  g_free ( c->name );
194  g_free ( c->role );
195  g_free ( c->wmdesktopstr );
196  g_free ( c );
197  }
198  }
199 }
200 
206 static void winlist_free ( winlist *l )
207 {
208  if ( l != NULL ) {
209  winlist_empty ( l );
210  g_free ( l->array );
211  g_free ( l->data );
212  g_free ( l );
213  }
214 }
215 
224 static int winlist_find ( winlist *l, xcb_window_t w )
225 {
226 // iterate backwards. Theory is: windows most often accessed will be
227 // nearer the end. Testing with kcachegrind seems to support this...
228  int i;
229 
230  for ( i = ( l->len - 1 ); i >= 0; i-- ) {
231  if ( l->array[i] == w ) {
232  return i;
233  }
234  }
235 
236  return -1;
237 }
241 static void x11_cache_create ( void )
242 {
243  if ( cache_client == NULL ) {
244  cache_client = winlist_new ();
245  }
246 }
247 
251 static void x11_cache_free ( void )
252 {
253  winlist_free ( cache_client );
254  cache_client = NULL;
255 }
256 
266 static xcb_get_window_attributes_reply_t * window_get_attributes ( xcb_window_t w )
267 {
268  xcb_get_window_attributes_cookie_t c = xcb_get_window_attributes ( xcb->connection, w );
269  xcb_get_window_attributes_reply_t *r = xcb_get_window_attributes_reply ( xcb->connection, c, NULL );
270  if ( r ) {
271  return r;
272  }
273  return NULL;
274 }
275 // _NET_WM_STATE_*
276 static int client_has_state ( client *c, xcb_atom_t state )
277 {
278  for ( int i = 0; i < c->states; i++ ) {
279  if ( c->state[i] == state ) {
280  return 1;
281  }
282  }
283 
284  return 0;
285 }
286 static int client_has_window_type ( client *c, xcb_atom_t type )
287 {
288  for ( int i = 0; i < c->window_types; i++ ) {
289  if ( c->window_type[i] == type ) {
290  return 1;
291  }
292  }
293 
294  return 0;
295 }
296 
297 static client* window_client ( ModeModePrivateData *pd, xcb_window_t win )
298 {
299  if ( win == XCB_WINDOW_NONE ) {
300  return NULL;
301  }
302 
303  int idx = winlist_find ( cache_client, win );
304 
305  if ( idx >= 0 ) {
306  return cache_client->data[idx];
307  }
308 
309  // if this fails, we're up that creek
310  xcb_get_window_attributes_reply_t *attr = window_get_attributes ( win );
311 
312  if ( !attr ) {
313  return NULL;
314  }
315  client *c = g_malloc0 ( sizeof ( client ) );
316  c->window = win;
317 
318  // copy xattr so we don't have to care when stuff is freed
319  memmove ( &c->xattr, attr, sizeof ( xcb_get_window_attributes_reply_t ) );
320 
321  xcb_get_property_cookie_t cky = xcb_ewmh_get_wm_state ( &xcb->ewmh, win );
322  xcb_ewmh_get_atoms_reply_t states;
323  if ( xcb_ewmh_get_wm_state_reply ( &xcb->ewmh, cky, &states, NULL ) ) {
324  c->states = MIN ( CLIENTSTATE, states.atoms_len );
325  memcpy ( c->state, states.atoms, MIN ( CLIENTSTATE, states.atoms_len ) * sizeof ( xcb_atom_t ) );
326  xcb_ewmh_get_atoms_reply_wipe ( &states );
327  }
328  cky = xcb_ewmh_get_wm_window_type ( &xcb->ewmh, win );
329  if ( xcb_ewmh_get_wm_window_type_reply ( &xcb->ewmh, cky, &states, NULL ) ) {
330  c->window_types = MIN ( CLIENTWINDOWTYPE, states.atoms_len );
331  memcpy ( c->window_type, states.atoms, MIN ( CLIENTWINDOWTYPE, states.atoms_len ) * sizeof ( xcb_atom_t ) );
332  xcb_ewmh_get_atoms_reply_wipe ( &states );
333  }
334 
335  c->title = window_get_text_prop ( c->window, xcb->ewmh._NET_WM_NAME );
336  if ( c->title == NULL ) {
337  c->title = window_get_text_prop ( c->window, XCB_ATOM_WM_NAME );
338  }
339  pd->title_len = MAX ( c->title ? g_utf8_strlen ( c->title, -1 ) : 0, pd->title_len );
340 
341  c->role = window_get_text_prop ( c->window, netatoms[WM_WINDOW_ROLE] );
342  pd->role_len = MAX ( c->role ? g_utf8_strlen ( c->role, -1 ) : 0, pd->role_len );
343 
344  cky = xcb_icccm_get_wm_class ( xcb->connection, c->window );
345  xcb_icccm_get_wm_class_reply_t wcr;
346  if ( xcb_icccm_get_wm_class_reply ( xcb->connection, cky, &wcr, NULL ) ) {
347  c->class = rofi_latin_to_utf8_strdup ( wcr.class_name, -1 );
348  c->name = rofi_latin_to_utf8_strdup ( wcr.instance_name, -1 );
349  pd->name_len = MAX ( c->name ? g_utf8_strlen ( c->name, -1 ) : 0, pd->name_len );
350  xcb_icccm_get_wm_class_reply_wipe ( &wcr );
351  }
352 
353  xcb_get_property_cookie_t cc = xcb_icccm_get_wm_hints ( xcb->connection, c->window );
354  xcb_icccm_wm_hints_t r;
355  if ( xcb_icccm_get_wm_hints_reply ( xcb->connection, cc, &r, NULL ) ) {
356  c->hint_flags = r.flags;
357  }
358 
359  winlist_append ( cache_client, c->window, c );
360  g_free ( attr );
361  return c;
362 }
363 static int window_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
364 {
365  ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
366  int match = 1;
367  const winlist *ids = ( winlist * ) rmpd->ids;
368  // Want to pull directly out of cache, X calls are not thread safe.
369  int idx = winlist_find ( cache_client, ids->array[index] );
370  g_assert ( idx >= 0 );
371  client *c = cache_client->data[idx];
372 
373  if ( tokens ) {
374  for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
375  int test = 0;
376  // Dirty hack. Normally helper_token_match does _all_ the matching,
377  // Now we want it to match only one item at the time.
378  // If hack not in place it would not match queries spanning multiple fields.
379  // e.g. when searching 'title element' and 'class element'
380  rofi_int_matcher *ftokens[2] = { tokens[j], NULL };
381  if ( c->title != NULL && c->title[0] != '\0' && matching_window_fields[WIN_MATCH_FIELD_TITLE].enabled ) {
382  test = helper_token_match ( ftokens, c->title );
383  }
384 
385  if ( test == tokens[j]->invert && c->class != NULL && c->class[0] != '\0' && matching_window_fields[WIN_MATCH_FIELD_CLASS].enabled ) {
386  test = helper_token_match ( ftokens, c->class );
387  }
388 
389  if ( test == tokens[j]->invert && c->role != NULL && c->role[0] != '\0' && matching_window_fields[WIN_MATCH_FIELD_ROLE].enabled ) {
390  test = helper_token_match ( ftokens, c->role );
391  }
392 
393  if ( test == tokens[j]->invert && c->name != NULL && c->name[0] != '\0' && matching_window_fields[WIN_MATCH_FIELD_NAME].enabled ) {
394  test = helper_token_match ( ftokens, c->name );
395  }
396  if ( test == tokens[j]->invert && c->wmdesktopstr != NULL && c->wmdesktopstr[0] != '\0' && matching_window_fields[WIN_MATCH_FIELD_DESKTOP].enabled ) {
397  test = helper_token_match ( ftokens, c->wmdesktopstr );
398  }
399 
400  if ( test == 0 ) {
401  match = 0;
402  }
403  }
404  }
405 
406  return match;
407 }
408 
409 static void window_mode_parse_fields ()
410 {
411  window_matching_fields_parsed = TRUE;
412  char *savept = NULL;
413  // Make a copy, as strtok will modify it.
414  char *switcher_str = g_strdup ( config.window_match_fields );
415  const char * const sep = ",#";
416  // Split token on ','. This modifies switcher_str.
417  for ( unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++ ) {
418  matching_window_fields[i].enabled = FALSE;
419  }
420  for ( char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL;
421  token = strtok_r ( NULL, sep, &savept ) ) {
422  if ( strcmp ( token, "all" ) == 0 ) {
423  for ( unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++ ) {
424  matching_window_fields[i].enabled = TRUE;
425  }
426  break;
427  }
428  else {
429  gboolean matched = FALSE;
430  for ( unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++ ) {
431  const char * field_name = matching_window_fields[i].field_name;
432  if ( strcmp ( token, field_name ) == 0 ) {
433  matching_window_fields[i].enabled = TRUE;
434  matched = TRUE;
435  }
436  }
437  if ( !matched ) {
438  g_warning ( "Invalid window field name :%s", token );
439  }
440  }
441  }
442  // Free string that was modified by strtok_r
443  g_free ( switcher_str );
444 }
445 
446 static unsigned int window_mode_get_num_entries ( const Mode *sw )
447 {
448  const ModeModePrivateData *pd = (const ModeModePrivateData *) mode_get_private_data ( sw );
449 
450  return pd->ids ? pd->ids->len : 0;
451 }
456 const char *invalid_desktop_name = "n/a";
457 static const char * _window_name_list_entry ( const char *str, uint32_t length, int entry )
458 {
459  uint32_t offset = 0;
460  int index = 0;
461  while ( index < entry && offset < length ) {
462  if ( str[offset] == 0 ) {
463  index++;
464  }
465  offset++;
466  }
467  if ( offset >= length ) {
468  return invalid_desktop_name;
469  }
470  return &str[offset];
471 }
472 static void _window_mode_load_data ( Mode *sw, unsigned int cd )
473 {
474  ModeModePrivateData *pd = (ModeModePrivateData *) mode_get_private_data ( sw );
475  // find window list
476  xcb_window_t curr_win_id;
477  int found = 0;
478 
479  // Create cache
480 
481  x11_cache_create ();
482  xcb_get_property_cookie_t c = xcb_ewmh_get_active_window ( &( xcb->ewmh ), xcb->screen_nbr );
483  if ( !xcb_ewmh_get_active_window_reply ( &xcb->ewmh, c, &curr_win_id, NULL ) ) {
484  curr_win_id = 0;
485  }
486 
487  // Get the current desktop.
488  unsigned int current_desktop = 0;
489  c = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
490  if ( !xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, c, &current_desktop, NULL ) ) {
491  current_desktop = 0;
492  }
493 
494  c = xcb_ewmh_get_client_list_stacking ( &xcb->ewmh, 0 );
495  xcb_ewmh_get_windows_reply_t clients = { 0, };
496  if ( xcb_ewmh_get_client_list_stacking_reply ( &xcb->ewmh, c, &clients, NULL ) ) {
497  found = 1;
498  }
499  else {
500  c = xcb_ewmh_get_client_list ( &xcb->ewmh, xcb->screen_nbr );
501  if ( xcb_ewmh_get_client_list_reply ( &xcb->ewmh, c, &clients, NULL ) ) {
502  found = 1;
503  }
504  }
505  if ( !found ) {
506  return;
507  }
508 
509  if ( clients.windows_len > 0 ) {
510  int i;
511  // windows we actually display. May be slightly different to _NET_CLIENT_LIST_STACKING
512  // if we happen to have a window destroyed while we're working...
513  pd->ids = winlist_new ();
514 
515  xcb_get_property_cookie_t c = xcb_ewmh_get_desktop_names ( &xcb->ewmh, xcb->screen_nbr );
516  xcb_ewmh_get_utf8_strings_reply_t names;
517  int has_names = FALSE;
518  if ( xcb_ewmh_get_desktop_names_reply ( &xcb->ewmh, c, &names, NULL ) ) {
519  has_names = TRUE;
520  }
521  // calc widths of fields
522  for ( i = clients.windows_len - 1; i > -1; i-- ) {
523  client *c = window_client ( pd, clients.windows[i] );
524  if ( ( c != NULL )
525  && !c->xattr.override_redirect
526  && !client_has_window_type ( c, xcb->ewmh._NET_WM_WINDOW_TYPE_DOCK )
527  && !client_has_window_type ( c, xcb->ewmh._NET_WM_WINDOW_TYPE_DESKTOP )
528  && !client_has_state ( c, xcb->ewmh._NET_WM_STATE_SKIP_PAGER )
529  && !client_has_state ( c, xcb->ewmh._NET_WM_STATE_SKIP_TASKBAR ) ) {
530  pd->clf_len = MAX ( pd->clf_len, ( c->class != NULL ) ? ( g_utf8_strlen ( c->class, -1 ) ) : 0 );
531 
532  if ( client_has_state ( c, xcb->ewmh._NET_WM_STATE_DEMANDS_ATTENTION ) ) {
533  c->demands = TRUE;
534  }
535  if ( ( c->hint_flags & XCB_ICCCM_WM_HINT_X_URGENCY ) != 0 ) {
536  c->demands = TRUE;
537  }
538 
539  if ( c->window == curr_win_id ) {
540  c->active = TRUE;
541  }
542  // find client's desktop.
543  xcb_get_property_cookie_t cookie;
544  xcb_get_property_reply_t *r;
545 
546  c->wmdesktop = 0xFFFFFFFF;
547  cookie =
548  xcb_get_property ( xcb->connection, 0, c->window, xcb->ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 0,
549  1 );
550  r = xcb_get_property_reply ( xcb->connection, cookie, NULL );
551  if ( r ) {
552  if ( r->type == XCB_ATOM_CARDINAL ) {
553  c->wmdesktop = *( (uint32_t *) xcb_get_property_value ( r ) );
554  }
555  free ( r );
556  }
557  if ( c->wmdesktop != 0xFFFFFFFF ) {
558  if ( has_names ) {
560  char *output = NULL;
561  if ( pango_parse_markup ( _window_name_list_entry ( names.strings, names.strings_len,
562  c->wmdesktop ), -1, 0, NULL, &output, NULL, NULL ) ) {
563  c->wmdesktopstr = output;
564  }
565  else {
566  c->wmdesktopstr = g_strdup ( "Invalid name" );
567  }
568  }
569  else {
570  c->wmdesktopstr = g_strdup ( _window_name_list_entry ( names.strings, names.strings_len, c->wmdesktop ) );
571  }
572  }
573  else {
574  c->wmdesktopstr = g_strdup_printf ( "%u", (uint32_t) c->wmdesktop );
575  }
576  }
577  else {
578  c->wmdesktopstr = g_strdup ( "" );
579  }
580  pd->wmdn_len = MAX ( pd->wmdn_len, g_utf8_strlen ( c->wmdesktopstr, -1 ) );
581  if ( cd && c->wmdesktop != current_desktop ) {
582  continue;
583  }
584  winlist_append ( pd->ids, c->window, NULL );
585  }
586  }
587 
588  if ( has_names ) {
589  xcb_ewmh_get_utf8_strings_reply_wipe ( &names );
590  }
591  }
592  xcb_ewmh_get_windows_reply_wipe ( &clients );
593 }
594 static int window_mode_init ( Mode *sw )
595 {
596  if ( mode_get_private_data ( sw ) == NULL ) {
597  ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
598  pd->window_regex = g_regex_new ( "{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL );
599  mode_set_private_data ( sw, (void *) pd );
600  _window_mode_load_data ( sw, FALSE );
601  if ( !window_matching_fields_parsed ) {
602  window_mode_parse_fields ();
603  }
604  }
605  return TRUE;
606 }
607 static int window_mode_init_cd ( Mode *sw )
608 {
609  if ( mode_get_private_data ( sw ) == NULL ) {
610  ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
611  pd->window_regex = g_regex_new ( "{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL );
612  mode_set_private_data ( sw, (void *) pd );
613  _window_mode_load_data ( sw, TRUE );
614  if ( !window_matching_fields_parsed ) {
615  window_mode_parse_fields ();
616  }
617  }
618  return TRUE;
619 }
620 
621 static inline int act_on_window ( xcb_window_t window )
622 {
623  int retv = TRUE;
624  char **args = NULL;
625  int argc = 0;
626  char window_regex[100]; /* We are probably safe here */
627 
628  g_snprintf ( window_regex, sizeof window_regex, "%d", window );
629 
630  helper_parse_setup ( config.window_command, &args, &argc, "{window}", window_regex, (char *) 0 );
631 
632  GError *error = NULL;
633  g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
634  if ( error != NULL ) {
635  char *msg = g_strdup_printf ( "Failed to execute action for window: '%s'\nError: '%s'", window_regex, error->message );
636  rofi_view_error_dialog ( msg, FALSE );
637  g_free ( msg );
638  // print error.
639  g_error_free ( error );
640  retv = FALSE;
641  }
642 
643  // Free the args list.
644  g_strfreev ( args );
645  return retv;
646 }
647 
648 static ModeMode window_mode_result ( Mode *sw, int mretv, G_GNUC_UNUSED char **input,
649  unsigned int selected_line )
650 {
651  ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
652  ModeMode retv = MODE_EXIT;
653  if ( ( mretv & ( MENU_OK ) ) ) {
654  if ( mretv & MENU_CUSTOM_ACTION ) {
655  act_on_window ( rmpd->ids->array[selected_line] );
656  }
657  else {
658  rofi_view_hide ();
660  // Get the desktop of the client to switch to
661  uint32_t wmdesktop = 0;
662  xcb_get_property_cookie_t cookie;
663  xcb_get_property_reply_t *r;
664  // Get the current desktop.
665  unsigned int current_desktop = 0;
666  xcb_get_property_cookie_t c = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
667  if ( !xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, c, &current_desktop, NULL ) ) {
668  current_desktop = 0;
669  }
670 
671  cookie = xcb_get_property ( xcb->connection, 0, rmpd->ids->array[selected_line],
672  xcb->ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 0, 1 );
673  r = xcb_get_property_reply ( xcb->connection, cookie, NULL );
674  if ( r && r->type == XCB_ATOM_CARDINAL ) {
675  wmdesktop = *( (uint32_t *) xcb_get_property_value ( r ) );
676  }
677  if ( r && r->type != XCB_ATOM_CARDINAL ) {
678  // Assume the client is on all desktops.
679  wmdesktop = current_desktop;
680  }
681  free ( r );
682 
683  // If we have to switch the desktop, do
684  if ( wmdesktop != current_desktop ) {
685  xcb_ewmh_request_change_current_desktop ( &xcb->ewmh,
686  xcb->screen_nbr, wmdesktop, XCB_CURRENT_TIME );
687  }
688  }
689  // Activate the window
690  xcb_ewmh_request_change_active_window ( &xcb->ewmh, xcb->screen_nbr, rmpd->ids->array[selected_line],
691  XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER,
692  XCB_CURRENT_TIME, rofi_view_get_window () );
693  xcb_flush ( xcb->connection );
694  }
695  }
696  else if ( ( mretv & ( MENU_ENTRY_DELETE ) ) == MENU_ENTRY_DELETE ) {
697  xcb_ewmh_request_close_window ( &( xcb->ewmh ), xcb->screen_nbr, rmpd->ids->array[selected_line], XCB_CURRENT_TIME, XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER );
698  xcb_flush ( xcb->connection );
699  }
700  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
701  GError *error = NULL;
702  gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
703  gsize lf_cmd_size = 0;
704  gchar *lf_cmd = g_locale_from_utf8 ( *input, -1, NULL, &lf_cmd_size, &error );
705  if ( error != NULL ) {
706  g_warning ( "Failed to convert command to locale encoding: %s", error->message );
707  g_error_free ( error );
708  return RELOAD_DIALOG;
709  }
710 
711  RofiHelperExecuteContext context = { .name = NULL };
712  if ( !helper_execute_command ( NULL, lf_cmd, run_in_term, run_in_term ? &context : NULL ) ) {
713  retv = RELOAD_DIALOG;
714  }
715  g_free ( lf_cmd );
716  }
717  return retv;
718 }
719 
720 static void window_mode_destroy ( Mode *sw )
721 {
722  ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
723  if ( rmpd != NULL ) {
724  winlist_free ( rmpd->ids );
725  x11_cache_free ();
726  g_free ( rmpd->cache );
727  g_regex_unref ( rmpd->window_regex );
728  g_free ( rmpd );
729  mode_set_private_data ( sw, NULL );
730  }
731 }
732 struct arg
733 {
734  const ModeModePrivateData *pd;
735  client *c;
736 };
737 
738 static void helper_eval_add_str ( GString *str, const char *input, int l, int max_len )
739 {
740  // g_utf8 does not work with NULL string.
741  const char *input_nn = input ? input : "";
742  // Both l and max_len are in characters, not bytes.
743  int nc = g_utf8_strlen ( input_nn, -1 );
744  int spaces = 0;
745  if ( l == 0 ) {
746  spaces = MAX ( 0, max_len - nc );
747  g_string_append ( str, input_nn );
748  }
749  else {
750  if ( nc > l ) {
751  int bl = g_utf8_offset_to_pointer ( input_nn, l ) - input_nn;
752  g_string_append_len ( str, input_nn, bl );
753  }
754  else {
755  spaces = l - nc;
756  g_string_append ( str, input_nn );
757  }
758  }
759  while ( spaces-- ) {
760  g_string_append_c ( str, ' ' );
761  }
762 }
763 static gboolean helper_eval_cb ( const GMatchInfo *info, GString *str, gpointer data )
764 {
765  struct arg *d = (struct arg *) data;
766  gchar *match;
767  // Get the match
768  match = g_match_info_fetch ( info, 0 );
769  if ( match != NULL ) {
770  int l = 0;
771  if ( match[2] == ':' ) {
772  l = (int) g_ascii_strtoll ( &match[3], NULL, 10 );
773  if ( l < 0 && config.menu_width < 0 ) {
774  l = -config.menu_width + l;
775  }
776  if ( l < 0 ) {
777  l = 0;
778  }
779  }
780  if ( match[1] == 'w' ) {
781  helper_eval_add_str ( str, d->c->wmdesktopstr, l, d->pd->wmdn_len );
782  }
783  else if ( match[1] == 'c' ) {
784  helper_eval_add_str ( str, d->c->class, l, d->pd->clf_len );
785  }
786  else if ( match[1] == 't' ) {
787  helper_eval_add_str ( str, d->c->title, l, d->pd->title_len );
788  }
789  else if ( match[1] == 'n' ) {
790  helper_eval_add_str ( str, d->c->name, l, d->pd->name_len );
791  }
792  else if ( match[1] == 'r' ) {
793  helper_eval_add_str ( str, d->c->role, l, d->pd->role_len );
794  }
795  g_free ( match );
796  }
797  return FALSE;
798 }
799 static char * _generate_display_string ( const ModeModePrivateData *pd, client *c )
800 {
801  struct arg d = { pd, c };
802  char *res = g_regex_replace_eval ( pd->window_regex, config.window_format, -1, 0, 0,
803  helper_eval_cb, &d, NULL );
804  return g_strchomp ( res );
805 }
806 
807 static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, G_GNUC_UNUSED GList **list, int get_entry )
808 {
809  ModeModePrivateData *rmpd = mode_get_private_data ( sw );
810  client *c = window_client ( rmpd, rmpd->ids->array[selected_line] );
811  if ( c == NULL ) {
812  return get_entry ? g_strdup ( "Window has fanished" ) : NULL;
813  }
814  if ( c->demands ) {
815  *state |= URGENT;
816  }
817  if ( c->active ) {
818  *state |= ACTIVE;
819  }
820  return get_entry ? _generate_display_string ( rmpd, c ) : NULL;
821 }
822 
826 static cairo_user_data_key_t data_key;
827 
833 static cairo_surface_t * draw_surface_from_data ( int width, int height, uint32_t *data )
834 {
835  unsigned long int len = width * height;
836  unsigned long int i;
837  uint32_t *buffer = g_new0 ( uint32_t, len );
838  cairo_surface_t *surface;
839 
840  /* Cairo wants premultiplied alpha, meh :( */
841  for ( i = 0; i < len; i++ ) {
842  uint8_t a = ( data[i] >> 24 ) & 0xff;
843  double alpha = a / 255.0;
844  uint8_t r = ( ( data[i] >> 16 ) & 0xff ) * alpha;
845  uint8_t g = ( ( data[i] >> 8 ) & 0xff ) * alpha;
846  uint8_t b = ( ( data[i] >> 0 ) & 0xff ) * alpha;
847  buffer[i] = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b;
848  }
849 
850  surface = cairo_image_surface_create_for_data ( (unsigned char *) buffer,
851  CAIRO_FORMAT_ARGB32,
852  width,
853  height,
854  width * 4 );
855  /* This makes sure that buffer will be freed */
856  cairo_surface_set_user_data ( surface, &data_key, buffer, g_free );
857 
858  return surface;
859 }
860 static cairo_surface_t * ewmh_window_icon_from_reply ( xcb_get_property_reply_t *r, uint32_t preferred_size )
861 {
862  uint32_t *data, *end, *found_data = 0;
863  uint32_t found_size = 0;
864 
865  if ( !r || r->type != XCB_ATOM_CARDINAL || r->format != 32 || r->length < 2 ) {
866  return 0;
867  }
868 
869  data = (uint32_t *) xcb_get_property_value ( r );
870  if ( !data ) {
871  return 0;
872  }
873 
874  end = data + r->length;
875 
876  /* Goes over the icon data and picks the icon that best matches the size preference.
877  * In case the size match is not exact, picks the closest bigger size if present,
878  * closest smaller size otherwise.
879  */
880  while ( data + 1 < end ) {
881  /* check whether the data size specified by width and height fits into the array we got */
882  uint64_t data_size = (uint64_t) data[0] * data[1];
883  if ( data_size > (uint64_t) ( end - data - 2 ) ) {
884  break;
885  }
886 
887  /* use the greater of the two dimensions to match against the preferred size */
888  uint32_t size = MAX ( data[0], data[1] );
889 
890  /* pick the icon if it's a better match than the one we already have */
891  gboolean found_icon_too_small = found_size < preferred_size;
892  gboolean found_icon_too_large = found_size > preferred_size;
893  gboolean icon_empty = data[0] == 0 || data[1] == 0;
894  gboolean better_because_bigger = found_icon_too_small && size > found_size;
895  gboolean better_because_smaller = found_icon_too_large &&
896  size >= preferred_size && size < found_size;
897  if ( !icon_empty && ( better_because_bigger || better_because_smaller || found_size == 0 ) ) {
898  found_data = data;
899  found_size = size;
900  }
901 
902  data += data_size + 2;
903  }
904 
905  if ( !found_data ) {
906  return 0;
907  }
908 
909  return draw_surface_from_data ( found_data[0], found_data[1], found_data + 2 );
910 }
912 static cairo_surface_t * get_net_wm_icon ( xcb_window_t xid, uint32_t preferred_size )
913 {
914  xcb_get_property_cookie_t cookie = xcb_get_property_unchecked (
915  xcb->connection, FALSE, xid,
916  xcb->ewmh._NET_WM_ICON, XCB_ATOM_CARDINAL, 0, UINT32_MAX );
917  xcb_get_property_reply_t *r = xcb_get_property_reply ( xcb->connection, cookie, NULL );
918  cairo_surface_t *surface = ewmh_window_icon_from_reply ( r, preferred_size );
919  free ( r );
920  return surface;
921 }
922 static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int size )
923 {
924  ModeModePrivateData *rmpd = mode_get_private_data ( sw );
925  client *c = window_client ( rmpd, rmpd->ids->array[selected_line] );
926  if ( config.window_thumbnail && c->thumbnail_checked == FALSE ) {
927  c->icon = x11_helper_get_screenshot_surface_window ( c->window, size );
928  c->thumbnail_checked = TRUE;
929  }
930  if ( c->icon == NULL && c->icon_checked == FALSE ) {
931  c->icon = get_net_wm_icon ( rmpd->ids->array[selected_line], size );
932  c->icon_checked = TRUE;
933  }
934  if ( c->icon == NULL && c->class ) {
935  if ( c->icon_fetch_uid > 0 ) {
936  return rofi_icon_fetcher_get ( c->icon_fetch_uid );
937  }
938  c->icon_fetch_uid = rofi_icon_fetcher_query ( c->class, size );
939  return rofi_icon_fetcher_get ( c->icon_fetch_uid );
940  }
941  return c->icon;
942 }
943 
944 #include "mode-private.h"
945 Mode window_mode =
946 {
947  .name = "window",
948  .cfg_name_key = "display-window",
949  ._init = window_mode_init,
950  ._get_num_entries = window_mode_get_num_entries,
951  ._result = window_mode_result,
952  ._destroy = window_mode_destroy,
953  ._token_match = window_match,
954  ._get_display_value = _get_display_value,
955  ._get_icon = _get_icon,
956  ._get_completion = NULL,
957  ._preprocess_input = NULL,
958  .private_data = NULL,
959  .free = NULL
960 };
961 Mode window_mode_cd =
962 {
963  .name = "windowcd",
964  .cfg_name_key = "display-windowcd",
965  ._init = window_mode_init_cd,
966  ._get_num_entries = window_mode_get_num_entries,
967  ._result = window_mode_result,
968  ._destroy = window_mode_destroy,
969  ._token_match = window_match,
970  ._get_display_value = _get_display_value,
971  ._get_icon = _get_icon,
972  ._get_completion = NULL,
973  ._preprocess_input = NULL,
974  .private_data = NULL,
975  .free = NULL
976 };
977 
978 #endif // WINDOW_MODE
char * window_command
Definition: settings.h:96
static cairo_surface_t * _get_icon(const Mode *sw, unsigned int selected_line, int height)
Definition: filebrowser.c:351
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
Definition: xcb.c:116
Definition: mode.h:69
char * window_format
Definition: settings.h:179
xcb_stuff * xcb
Definition: xcb.c:86
ModeMode
Definition: mode.h:49
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
Definition: helper.c:1042
xcb_ewmh_connection_t ewmh
Definition: xcb-internal.h:48
int screen_nbr
Definition: xcb-internal.h:50
Definition: mode.h:52
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
void * mode_get_private_data(const Mode *mode)
Definition: mode.c:139
void rofi_view_hide(void)
Definition: view.c:1941
xcb_window_t rofi_view_get_window(void)
Definition: view.c:2082
int menu_width
Definition: settings.h:66
char * window_match_fields
Definition: settings.h:98
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:488
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
Definition: helper.c:779
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:1898
WindowManagerQuirk current_window_manager
Definition: xcb.c:73
xcb_connection_t * connection
Definition: xcb-internal.h:47
int helper_parse_setup(char *string, char ***output, int *length,...)
Definition: helper.c:81
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
Definition: xcb.c:222
void mode_set_private_data(Mode *mode, void *pd)
Definition: mode.c:145
gboolean window_thumbnail
Definition: settings.h:198
const gchar * name
Definition: helper.h:269
struct _icon icon
Definition: icon.h:44
Settings config
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:312
char * name
Definition: mode-private.h:156
xcb_atom_t netatoms[NUM_NETATOMS]
Definition: xcb.c:98