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