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