rofi  1.5.2
xcb.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
6  * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28 
30 #define G_LOG_DOMAIN "X11Helper"
31 
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <stdint.h>
38 #include <glib.h>
39 #include <cairo.h>
40 #include <cairo-xcb.h>
41 #include <librsvg/rsvg.h>
42 
43 #include <xcb/xcb.h>
44 #include <xcb/xcb_aux.h>
45 #include <xcb/randr.h>
46 #include <xcb/xinerama.h>
47 #include <xcb/xcb_ewmh.h>
48 #include <xcb/xproto.h>
49 #include <xcb/xkb.h>
50 #include <xkbcommon/xkbcommon.h>
51 #include <xkbcommon/xkbcommon-x11.h>
53 #define SN_API_NOT_YET_FROZEN
54 
55 #define sn_launcher_context_set_application_id sn_launcher_set_application_id
56 #include "rofi-types.h"
57 #include <libsn/sn.h>
58 #include "display.h"
59 #include "xcb-internal.h"
60 #include "xcb.h"
61 #include "settings.h"
62 #include "helper.h"
63 #include "timings.h"
64 
65 #include <rofi.h>
66 
68 #define RANDR_PREF_MAJOR_VERSION 1
69 
70 #define RANDR_PREF_MINOR_VERSION 5
71 
73 #define INTERSECT( x, y, x1, y1, w1, h1 ) ( ( ( ( x ) >= ( x1 ) ) && ( ( x ) < ( x1 + w1 ) ) ) && ( ( ( y ) >= ( y1 ) ) && ( ( y ) < ( y1 + h1 ) ) ) )
75 
79 struct _xcb_stuff xcb_int = {
80  .connection = NULL,
81  .screen = NULL,
82  .screen_nbr = -1,
83  .sndisplay = NULL,
84  .sncontext = NULL,
85  .monitors = NULL
86 };
88 
92 xcb_depth_t *depth = NULL;
93 xcb_visualtype_t *visual = NULL;
94 xcb_colormap_t map = XCB_COLORMAP_NONE;
98 static xcb_visualtype_t *root_visual = NULL;
99 xcb_atom_t netatoms[NUM_NETATOMS];
100 const char *netatom_names[] = { EWMH_ATOMS ( ATOM_CHAR ) };
101 
106 cairo_surface_t *x11_helper_get_screenshot_surface ( void )
107 {
108  return cairo_xcb_surface_create ( xcb->connection,
110  xcb->screen->width_in_pixels, xcb->screen->height_in_pixels );
111 }
112 
113 static xcb_pixmap_t get_root_pixmap ( xcb_connection_t *c,
114  xcb_screen_t *screen,
115  xcb_atom_t atom )
116 {
117  xcb_get_property_cookie_t cookie;
118  xcb_get_property_reply_t *reply;
119  xcb_pixmap_t rootpixmap = XCB_NONE;
120 
121  cookie = xcb_get_property ( c,
122  0,
123  screen->root,
124  atom,
125  XCB_ATOM_PIXMAP,
126  0,
127  1 );
128 
129  reply = xcb_get_property_reply ( c, cookie, NULL );
130 
131  if ( reply ) {
132  if ( xcb_get_property_value_length ( reply ) == sizeof ( xcb_pixmap_t ) ) {
133  memcpy ( &rootpixmap, xcb_get_property_value ( reply ), sizeof ( xcb_pixmap_t ) );
134  }
135  free ( reply );
136  }
137 
138  return rootpixmap;
139 }
140 
141 cairo_surface_t * x11_helper_get_bg_surface ( void )
142 {
143  xcb_pixmap_t pm = get_root_pixmap ( xcb->connection, xcb->screen, netatoms[ESETROOT_PMAP_ID] );
144  if ( pm == XCB_NONE ) {
145  return NULL;
146  }
147  return cairo_xcb_surface_create ( xcb->connection, pm, root_visual,
148  xcb->screen->width_in_pixels, xcb->screen->height_in_pixels );
149 }
150 
151 // retrieve a text property from a window
152 // technically we could use window_get_prop(), but this is better for character set support
153 char* window_get_text_prop ( xcb_window_t w, xcb_atom_t atom )
154 {
155  xcb_get_property_cookie_t c = xcb_get_property ( xcb->connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX );
156  xcb_get_property_reply_t *r = xcb_get_property_reply ( xcb->connection, c, NULL );
157  if ( r ) {
158  if ( xcb_get_property_value_length ( r ) > 0 ) {
159  char *str = NULL;
160  if ( r->type == netatoms[UTF8_STRING] ) {
161  str = g_strndup ( xcb_get_property_value ( r ), xcb_get_property_value_length ( r ) );
162  }
163  else if ( r->type == netatoms[STRING] ) {
164  str = rofi_latin_to_utf8_strdup ( xcb_get_property_value ( r ), xcb_get_property_value_length ( r ) );
165  }
166  else {
167  str = g_strdup ( "Invalid encoding." );
168  }
169 
170  free ( r );
171  return str;
172  }
173  free ( r );
174  }
175  return NULL;
176 }
177 
178 void window_set_atom_prop ( xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count )
179 {
180  xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, w, prop, XCB_ATOM_ATOM, 32, count, atoms );
181 }
182 
183 /****
184  * Code used to get monitor layout.
185  */
186 
190 static void x11_monitor_free ( workarea *m )
191 {
192  g_free ( m->name );
193  g_free ( m );
194 }
195 
196 static void x11_monitors_free ( void )
197 {
198  while ( xcb->monitors != NULL ) {
199  workarea *m = xcb->monitors;
200  xcb->monitors = m->next;
201  x11_monitor_free ( m );
202  }
203 }
204 
208 static workarea * x11_get_monitor_from_output ( xcb_randr_output_t out )
209 {
210  xcb_randr_get_output_info_reply_t *op_reply;
211  xcb_randr_get_crtc_info_reply_t *crtc_reply;
212  xcb_randr_get_output_info_cookie_t it = xcb_randr_get_output_info ( xcb->connection, out, XCB_CURRENT_TIME );
213  op_reply = xcb_randr_get_output_info_reply ( xcb->connection, it, NULL );
214  if ( op_reply->crtc == XCB_NONE ) {
215  free ( op_reply );
216  return NULL;
217  }
218  xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info ( xcb->connection, op_reply->crtc, XCB_CURRENT_TIME );
219  crtc_reply = xcb_randr_get_crtc_info_reply ( xcb->connection, ct, NULL );
220  if ( !crtc_reply ) {
221  free ( op_reply );
222  return NULL;
223  }
224  workarea *retv = g_malloc0 ( sizeof ( workarea ) );
225  retv->x = crtc_reply->x;
226  retv->y = crtc_reply->y;
227  retv->w = crtc_reply->width;
228  retv->h = crtc_reply->height;
229 
230  retv->mw = op_reply->mm_width;
231  retv->mh = op_reply->mm_height;
232 
233  char *tname = (char *) xcb_randr_get_output_info_name ( op_reply );
234  int tname_len = xcb_randr_get_output_info_name_length ( op_reply );
235 
236  retv->name = g_malloc0 ( ( tname_len + 1 ) * sizeof ( char ) );
237  memcpy ( retv->name, tname, tname_len );
238 
239  free ( crtc_reply );
240  free ( op_reply );
241  return retv;
242 }
243 
244 #if ( ( ( XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION ) && ( XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION ) ) \
245  || XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION )
246 
253 static workarea *x11_get_monitor_from_randr_monitor ( xcb_randr_monitor_info_t *mon )
254 {
255  // Query to the name of the monitor.
256  xcb_generic_error_t *err;
257  xcb_get_atom_name_cookie_t anc = xcb_get_atom_name ( xcb->connection, mon->name );
258  xcb_get_atom_name_reply_t *atom_reply = xcb_get_atom_name_reply ( xcb->connection, anc, &err );
259  if ( err != NULL ) {
260  g_warning ( "Could not get RandR monitor name: X11 error code %d\n", err->error_code );
261  free ( err );
262  return NULL;
263  }
264  workarea *retv = g_malloc0 ( sizeof ( workarea ) );
265 
266  // Is primary monitor.
267  retv->primary = mon->primary;
268 
269  // Position and size.
270  retv->x = mon->x;
271  retv->y = mon->y;
272  retv->w = mon->width;
273  retv->h = mon->height;
274 
275  // Physical
276  retv->mw = mon->width_in_millimeters;
277  retv->mh = mon->height_in_millimeters;
278 
279  // Name
280  retv->name = g_strdup_printf ( "%.*s", xcb_get_atom_name_name_length ( atom_reply ), xcb_get_atom_name_name ( atom_reply ) );
281 
282  // Free name atom.
283  free ( atom_reply );
284 
285  return retv;
286 }
287 #endif
288 
289 static int x11_is_extension_present ( const char *extension )
290 {
291  xcb_query_extension_cookie_t randr_cookie = xcb_query_extension ( xcb->connection, strlen ( extension ), extension );
292 
293  xcb_query_extension_reply_t *randr_reply = xcb_query_extension_reply ( xcb->connection, randr_cookie, NULL );
294 
295  int present = randr_reply->present;
296 
297  free ( randr_reply );
298 
299  return present;
300 }
301 
303 {
304  xcb_xinerama_query_screens_cookie_t screens_cookie = xcb_xinerama_query_screens_unchecked (
305  xcb->connection
306  );
307 
308  xcb_xinerama_query_screens_reply_t *screens_reply = xcb_xinerama_query_screens_reply (
309  xcb->connection,
310  screens_cookie,
311  NULL
312  );
313 
314  xcb_xinerama_screen_info_iterator_t screens_iterator = xcb_xinerama_query_screens_screen_info_iterator (
315  screens_reply
316  );
317 
318  for (; screens_iterator.rem > 0; xcb_xinerama_screen_info_next ( &screens_iterator ) ) {
319  workarea *w = g_malloc0 ( sizeof ( workarea ) );
320 
321  w->x = screens_iterator.data->x_org;
322  w->y = screens_iterator.data->y_org;
323  w->w = screens_iterator.data->width;
324  w->h = screens_iterator.data->height;
325 
326  w->next = xcb->monitors;
327  xcb->monitors = w;
328  }
329 
330  int index = 0;
331  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
332  iter->monitor_id = index++;
333  }
334 
335  free ( screens_reply );
336 }
337 
339 {
340  if ( xcb->monitors ) {
341  return;
342  }
343  // If RANDR is not available, try Xinerama
344  if ( !x11_is_extension_present ( "RANDR" ) ) {
345  // Check if xinerama is available.
346  if ( x11_is_extension_present ( "XINERAMA" ) ) {
347  g_debug ( "Query XINERAMA for monitor layout." );
349  return;
350  }
351  g_debug ( "No RANDR or Xinerama available for getting monitor layout." );
352  return;
353  }
354  g_debug ( "Query RANDR for monitor layout." );
355 
356  g_debug ( "Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION );
357 #if ( ( ( XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION ) && ( XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION ) ) \
358  || XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION )
359  xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version ( xcb->connection,
361  xcb_randr_query_version_reply_t *rversion = xcb_randr_query_version_reply ( xcb->connection, cversion, NULL );
362  if ( rversion ) {
363  g_debug ( "Found randr version: %d.%d", rversion->major_version, rversion->minor_version );
364  // Check if we are 1.5 and up.
365  if ( ( ( rversion->major_version == XCB_RANDR_MAJOR_VERSION ) && ( rversion->minor_version >= XCB_RANDR_MINOR_VERSION ) ) ||
366  ( rversion->major_version > XCB_RANDR_MAJOR_VERSION ) ) {
367  xcb_randr_get_monitors_cookie_t t = xcb_randr_get_monitors ( xcb->connection, xcb->screen->root, 1 );
368  xcb_randr_get_monitors_reply_t *mreply = xcb_randr_get_monitors_reply ( xcb->connection, t, NULL );
369  if ( mreply ) {
370  xcb_randr_monitor_info_iterator_t iter = xcb_randr_get_monitors_monitors_iterator ( mreply );
371  while ( iter.rem > 0 ) {
372  workarea *w = x11_get_monitor_from_randr_monitor ( iter.data );
373  if ( w ) {
374  w->next = xcb->monitors;
375  xcb->monitors = w;
376  }
377  xcb_randr_monitor_info_next ( &iter );
378  }
379  free ( mreply );
380  }
381  }
382  free ( rversion );
383  }
384 #endif
385 
386  // If no monitors found.
387  if ( xcb->monitors == NULL ) {
388  xcb_randr_get_screen_resources_current_reply_t *res_reply;
389  xcb_randr_get_screen_resources_current_cookie_t src;
390  src = xcb_randr_get_screen_resources_current ( xcb->connection, xcb->screen->root );
391  res_reply = xcb_randr_get_screen_resources_current_reply ( xcb->connection, src, NULL );
392  if ( !res_reply ) {
393  return; //just report error
394  }
395  int mon_num = xcb_randr_get_screen_resources_current_outputs_length ( res_reply );
396  xcb_randr_output_t *ops = xcb_randr_get_screen_resources_current_outputs ( res_reply );
397 
398  // Get primary.
399  xcb_randr_get_output_primary_cookie_t pc = xcb_randr_get_output_primary ( xcb->connection, xcb->screen->root );
400  xcb_randr_get_output_primary_reply_t *pc_rep = xcb_randr_get_output_primary_reply ( xcb->connection, pc, NULL );
401 
402  for ( int i = mon_num - 1; i >= 0; i-- ) {
403  workarea *w = x11_get_monitor_from_output ( ops[i] );
404  if ( w ) {
405  w->next = xcb->monitors;
406  xcb->monitors = w;
407  if ( pc_rep && pc_rep->output == ops[i] ) {
408  w->primary = TRUE;
409  }
410  }
411  }
412  // If exists, free primary output reply.
413  if ( pc_rep ) {
414  free ( pc_rep );
415  }
416  free ( res_reply );
417  }
418 
419  // Number monitor
420  int index = 0;
421  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
422  iter->monitor_id = index++;
423  }
424 }
425 
427 {
428  int is_term = isatty ( fileno ( stdout ) );
429  printf ( "Monitor layout:\n" );
430  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
431  printf ( "%s ID%s: %d", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->monitor_id );
432  if ( iter->primary ) {
433  printf ( " (primary)" );
434  }
435  printf ( "\n" );
436  printf ( "%s name%s: %s\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->name );
437  printf ( "%s position%s: %d,%d\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->x, iter->y );
438  printf ( "%s size%s: %d,%d\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->w, iter->h );
439  if ( iter->mw > 0 && iter->mh > 0 ) {
440  printf ( "%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
441  ( is_term ) ? color_bold : "",
442  is_term ? color_reset : "",
443  iter->mw,
444  iter->mh,
445  iter->w * 25.4 / (double) iter->mw,
446  iter->h * 25.4 / (double) iter->mh
447  );
448  }
449  printf ( "\n" );
450  }
451 }
452 
453 void display_startup_notification ( RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data )
454 {
455  if ( context == NULL ) {
456  return;
457  }
458 
459  SnLauncherContext *sncontext;
460 
461  sncontext = sn_launcher_context_new ( xcb->sndisplay, xcb->screen_nbr );
462 
463  sn_launcher_context_set_name ( sncontext, context->name );
464  sn_launcher_context_set_description ( sncontext, context->description );
465  if ( context->binary != NULL ) {
466  sn_launcher_context_set_binary_name ( sncontext, context->binary );
467  }
468  if ( context->icon != NULL ) {
469  sn_launcher_context_set_icon_name ( sncontext, context->icon );
470  }
471  if ( context->app_id != NULL ) {
473  }
474  if ( context->wmclass != NULL ) {
475  sn_launcher_context_set_wmclass ( sncontext, context->wmclass );
476  }
477 
478  xcb_get_property_cookie_t c;
479  unsigned int current_desktop = 0;
480 
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  sn_launcher_context_set_workspace ( sncontext, current_desktop );
484  }
485 
486  sn_launcher_context_initiate ( sncontext, "rofi", context->command, xcb->last_timestamp );
487 
488  *child_setup = (GSpawnChildSetupFunc) sn_launcher_context_setup_child_process;
489  *user_data = sncontext;
490 }
491 
492 static int monitor_get_dimension ( int monitor_id, workarea *mon )
493 {
494  memset ( mon, 0, sizeof ( workarea ) );
495  mon->w = xcb->screen->width_in_pixels;
496  mon->h = xcb->screen->height_in_pixels;
497 
498  workarea *iter = NULL;
499  for ( iter = xcb->monitors; iter; iter = iter->next ) {
500  if ( iter->monitor_id == monitor_id ) {
501  *mon = *iter;
502  return TRUE;
503  }
504  }
505  return FALSE;
506 }
507 // find the dimensions of the monitor displaying point x,y
508 static void monitor_dimensions ( int x, int y, workarea *mon )
509 {
510  memset ( mon, 0, sizeof ( workarea ) );
511  mon->w = xcb->screen->width_in_pixels;
512  mon->h = xcb->screen->height_in_pixels;
513 
514  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
515  if ( INTERSECT ( x, y, iter->x, iter->y, iter->w, iter->h ) ) {
516  *mon = *iter;
517  break;
518  }
519  }
520 }
521 
531 static int pointer_get ( xcb_window_t root, int *x, int *y )
532 {
533  *x = 0;
534  *y = 0;
535  xcb_query_pointer_cookie_t c = xcb_query_pointer ( xcb->connection, root );
536  xcb_query_pointer_reply_t *r = xcb_query_pointer_reply ( xcb->connection, c, NULL );
537  if ( r ) {
538  *x = r->root_x;
539  *y = r->root_y;
540  free ( r );
541  return TRUE;
542  }
543 
544  return FALSE;
545 }
546 static int monitor_active_from_winid ( xcb_drawable_t id, workarea *mon )
547 {
548  xcb_window_t root = xcb->screen->root;
549  xcb_get_geometry_cookie_t c = xcb_get_geometry ( xcb->connection, id );
550  xcb_get_geometry_reply_t *r = xcb_get_geometry_reply ( xcb->connection, c, NULL );
551  if ( r ) {
552  xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates ( xcb->connection, id, root, r->x, r->y );
553  xcb_translate_coordinates_reply_t *t = xcb_translate_coordinates_reply ( xcb->connection, ct, NULL );
554  if ( t ) {
555  // place the menu above the window
556  // if some window is focused, place menu above window, else fall
557  // back to selected monitor.
558  mon->x = t->dst_x - r->x;
559  mon->y = t->dst_y - r->y;
560  mon->w = r->width;
561  mon->h = r->height;
562  free ( r );
563  free ( t );
564  return TRUE;
565  }
566  free ( r );
567  }
568  return FALSE;
569 }
570 static int monitor_active_from_id_focused ( int mon_id, workarea *mon )
571 {
572  int retv = FALSE;
573  xcb_window_t active_window;
574  xcb_get_property_cookie_t awc;
575  awc = xcb_ewmh_get_active_window ( &xcb->ewmh, xcb->screen_nbr );
576  if ( !xcb_ewmh_get_active_window_reply ( &xcb->ewmh, awc, &active_window, NULL ) ) {
577  g_debug ( "Failed to get active window, falling back to mouse location (-5)." );
578  return retv;
579  }
580  xcb_query_tree_cookie_t tree_cookie = xcb_query_tree ( xcb->connection, active_window );
581  xcb_query_tree_reply_t *tree_reply = xcb_query_tree_reply ( xcb->connection, tree_cookie, NULL );
582  if ( !tree_reply ) {
583  g_debug ( "Failed to get parent window, falling back to mouse location (-5)." );
584  return retv;
585  }
586  // get geometry.
587  xcb_get_geometry_cookie_t c = xcb_get_geometry ( xcb->connection, active_window );
588  xcb_get_geometry_reply_t *r = xcb_get_geometry_reply ( xcb->connection, c, NULL );
589  if ( !r ) {
590  g_debug ( "Failed to get geometry of active window, falling back to mouse location (-5)." );
591  free ( tree_reply );
592  return retv;
593  }
594  xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates ( xcb->connection, tree_reply->parent, r->root, r->x, r->y );
595  xcb_translate_coordinates_reply_t *t = xcb_translate_coordinates_reply ( xcb->connection, ct, NULL );
596  if ( t ) {
597  if ( mon_id == -2 ) {
598  // place the menu above the window
599  // if some window is focused, place menu above window, else fall
600  // back to selected monitor.
601  mon->x = t->dst_x - r->x;
602  mon->y = t->dst_y - r->y;
603  mon->w = r->width;
604  mon->h = r->height;
605  retv = TRUE;
606  }
607  else if ( mon_id == -4 ) {
608  monitor_dimensions ( t->dst_x, t->dst_y, mon );
609  retv = TRUE;
610  }
611  free ( t );
612  }
613  else {
614  g_debug ( "Failed to get translate position of active window, falling back to mouse location (-5)." );
615  }
616  free ( r );
617  free ( tree_reply );
618  return retv;
619 }
620 static int monitor_active_from_id ( int mon_id, workarea *mon )
621 {
622  xcb_window_t root = xcb->screen->root;
623  int x, y;
624  // At mouse position.
625  if ( mon_id == -3 ) {
626  if ( pointer_get ( root, &x, &y ) ) {
627  monitor_dimensions ( x, y, mon );
628  mon->x = x;
629  mon->y = y;
630  return TRUE;
631  }
632  }
633  // Focused monitor
634  else if ( mon_id == -1 ) {
635  // Get the current desktop.
636  unsigned int current_desktop = 0;
637  xcb_get_property_cookie_t gcdc;
638  gcdc = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
639  if ( xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, gcdc, &current_desktop, NULL ) ) {
640  xcb_get_property_cookie_t c = xcb_ewmh_get_desktop_viewport ( &xcb->ewmh, xcb->screen_nbr );
641  xcb_ewmh_get_desktop_viewport_reply_t vp;
642  if ( xcb_ewmh_get_desktop_viewport_reply ( &xcb->ewmh, c, &vp, NULL ) ) {
643  if ( current_desktop < vp.desktop_viewport_len ) {
644  monitor_dimensions ( vp.desktop_viewport[current_desktop].x,
645  vp.desktop_viewport[current_desktop].y, mon );
646  xcb_ewmh_get_desktop_viewport_reply_wipe ( &vp );
647  return TRUE;
648  }
649  else {
650  g_debug ( "Viewport does not exist for current desktop: %d, falling back to mouse location (-5)", current_desktop );
651  }
652  xcb_ewmh_get_desktop_viewport_reply_wipe ( &vp );
653  }
654  else {
655  g_debug ( "Failed to get viewport for current desktop: %d, falling back to mouse location (-5).", current_desktop );
656  }
657  }
658  else {
659  g_debug ( "Failed to get current desktop, falling back to mouse location (-5)." );
660  }
661  }
662  else if ( mon_id == -2 || mon_id == -4 ) {
663  if ( monitor_active_from_id_focused ( mon_id, mon ) ) {
664  return TRUE;
665  }
666  }
667  // Monitor that has mouse pointer.
668  else if ( mon_id == -5 ) {
669  if ( pointer_get ( root, &x, &y ) ) {
670  monitor_dimensions ( x, y, mon );
671  return TRUE;
672  }
673  // This is our give up point.
674  return FALSE;
675  }
676  g_debug ( "Failed to find monitor, fall back to monitor showing mouse." );
677  return monitor_active_from_id ( -5, mon );
678 }
679 
680 // determine which monitor holds the active window, or failing that the mouse pointer
682 {
683  if ( config.monitor != NULL ) {
684  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
685  if ( g_strcmp0 ( config.monitor, iter->name ) == 0 ) {
686  *mon = *iter;
687  return TRUE;
688  }
689  }
690  }
691  // Grab primary.
692  if ( g_strcmp0 ( config.monitor, "primary" ) == 0 ) {
693  for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
694  if ( iter->primary ) {
695  *mon = *iter;
696  return TRUE;
697  }
698  }
699  }
700  if ( g_str_has_prefix ( config.monitor, "wid:" ) ) {
701  char *end = NULL;
702  xcb_drawable_t win = g_ascii_strtoll ( config.monitor + 4, &end, 0 );
703  if ( end != config.monitor ) {
704  if ( monitor_active_from_winid ( win, mon ) ) {
705  return TRUE;
706  }
707  }
708  }
709  {
710  // IF fail, fall back to classic mode.
711  char *end = NULL;
712  gint64 mon_id = g_ascii_strtoll ( config.monitor, &end, 0 );
713  if ( end != config.monitor ) {
714  if ( mon_id >= 0 ) {
715  if ( monitor_get_dimension ( mon_id, mon ) ) {
716  return TRUE;
717  }
718  g_warning ( "Failed to find selected monitor." );
719  }
720  else {
721  return monitor_active_from_id ( mon_id, mon );
722  }
723  }
724  }
725  // Fallback.
726  monitor_dimensions ( 0, 0, mon );
727  return FALSE;
728 }
729 
736 static void rofi_view_paste ( RofiViewState *state, xcb_selection_notify_event_t *xse )
737 {
738  if ( xse->property == XCB_ATOM_NONE ) {
739  g_warning ( "Failed to convert selection" );
740  }
741  else if ( xse->property == xcb->ewmh.UTF8_STRING ) {
742  gchar *text = window_get_text_prop ( xse->requestor, xcb->ewmh.UTF8_STRING );
743  if ( text != NULL && text[0] != '\0' ) {
744  unsigned int dl = strlen ( text );
745  // Strip new line
746  for ( unsigned int i = 0; i < dl; i++ ) {
747  if ( text[i] == '\n' ) {
748  text[i] = '\0';
749  }
750  }
751  rofi_view_handle_text ( state, text );
752  }
753  g_free ( text );
754  }
755  else {
756  g_warning ( "Failed" );
757  }
758 }
759 
760 static gboolean x11_button_to_nk_bindings_button ( guint32 x11_button, NkBindingsMouseButton *button )
761 {
762  switch ( x11_button )
763  {
764  case 1:
765  *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
766  break;
767  case 3:
768  *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
769  break;
770  case 2:
771  *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
772  break;
773  case 8:
774  *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
775  break;
776  case 9:
777  *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
778  break;
779  case 4:
780  case 5:
781  case 6:
782  case 7:
783  return FALSE;
784  default:
785  *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
786  }
787  return TRUE;
788 }
789 
790 static gboolean x11_button_to_nk_bindings_scroll ( guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps )
791 {
792  *steps = 1;
793  switch ( x11_button )
794  {
795  case 4:
796  *steps = -1;
797  /* fallthrough */
798  case 5:
799  *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
800  break;
801  case 6:
802  *steps = -1;
803  /* fallthrough */
804  case 7:
805  *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
806  break;
807  default:
808  return FALSE;
809  }
810  return TRUE;
811 }
812 
816 static void main_loop_x11_event_handler_view ( xcb_generic_event_t *event )
817 {
819  if ( state == NULL ) {
820  return;
821  }
822 
823  switch ( event->response_type & ~0x80 )
824  {
825  case XCB_EXPOSE:
827  break;
828  case XCB_CONFIGURE_NOTIFY:
829  {
830  xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *) event;
831  rofi_view_temp_configure_notify ( state, xce );
832  break;
833  }
834  case XCB_MOTION_NOTIFY:
835  {
836  if ( config.click_to_exit == TRUE ) {
837  xcb->mouse_seen = TRUE;
838  }
839  xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *) event;
840  rofi_view_handle_mouse_motion ( state, xme->event_x, xme->event_y );
841  break;
842  }
843  case XCB_BUTTON_PRESS:
844  {
845  xcb_button_press_event_t *bpe = (xcb_button_press_event_t *) event;
846  NkBindingsMouseButton button;
847  NkBindingsScrollAxis axis;
848  gint32 steps;
849 
850  xcb->last_timestamp = bpe->time;
851  rofi_view_handle_mouse_motion ( state, bpe->event_x, bpe->event_y );
852  if ( x11_button_to_nk_bindings_button ( bpe->detail, &button ) ) {
853  nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time );
854  }
855  else if ( x11_button_to_nk_bindings_scroll ( bpe->detail, &axis, &steps ) ) {
856  nk_bindings_seat_handle_scroll ( xcb->bindings_seat, NULL, axis, steps );
857  }
858  break;
859  }
860  case XCB_BUTTON_RELEASE:
861  {
862  xcb_button_release_event_t *bre = (xcb_button_release_event_t *) event;
863  NkBindingsMouseButton button;
864 
865  xcb->last_timestamp = bre->time;
866  if ( x11_button_to_nk_bindings_button ( bre->detail, &button ) ) {
867  nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_RELEASE, bre->time );
868  }
869  if ( config.click_to_exit == TRUE ) {
870  if ( !xcb->mouse_seen ) {
871  rofi_view_temp_click_to_exit ( state, bre->event );
872  }
873  xcb->mouse_seen = FALSE;
874  }
875  break;
876  }
877  // Paste event.
878  case XCB_SELECTION_NOTIFY:
879  rofi_view_paste ( state, (xcb_selection_notify_event_t *) event );
880  break;
881  case XCB_KEYMAP_NOTIFY:
882  {
883  xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *) event;
884  for ( gint32 by = 0; by < 31; ++by ) {
885  for ( gint8 bi = 0; bi < 7; ++bi ) {
886  if ( kne->keys[by] & ( 1 << bi ) ) {
887  // X11 keycodes starts at 8
888  nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, ( 8 * by + bi ) + 8, NK_BINDINGS_KEY_STATE_PRESSED );
889  }
890  }
891  }
892  break;
893  }
894  case XCB_KEY_PRESS:
895  {
896  xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *) event;
897  gchar *text;
898 
899  xcb->last_timestamp = xkpe->time;
900  text = nk_bindings_seat_handle_key_with_modmask ( xcb->bindings_seat, NULL, xkpe->state, xkpe->detail, NK_BINDINGS_KEY_STATE_PRESS );
901  if ( text != NULL ) {
902  rofi_view_handle_text ( state, text );
903  }
904  break;
905  }
906  case XCB_KEY_RELEASE:
907  {
908  xcb_key_release_event_t *xkre = (xcb_key_release_event_t *) event;
909  xcb->last_timestamp = xkre->time;
910  nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, xkre->detail, NK_BINDINGS_KEY_STATE_RELEASE );
911  break;
912  }
913  default:
914  break;
915  }
916  rofi_view_maybe_update ( state );
917 }
918 
919 static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data )
920 {
921  if ( ev == NULL ) {
922  int status = xcb_connection_has_error ( xcb->connection );
923  if ( status > 0 ) {
924  g_warning ( "The XCB connection to X server had a fatal error: %d", status );
925  g_main_loop_quit ( xcb->main_loop );
926  return G_SOURCE_REMOVE;
927  }
928  else {
929  g_warning ( "main_loop_x11_event_handler: ev == NULL, status == %d", status );
930  return G_SOURCE_CONTINUE;
931  }
932  }
933  uint8_t type = ev->response_type & ~0x80;
934  if ( type == xcb->xkb.first_event ) {
935  switch ( ev->pad0 )
936  {
937  case XCB_XKB_MAP_NOTIFY:
938  {
939  struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, 0 );
940  struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id );
941  nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state );
942  xkb_keymap_unref ( keymap );
943  xkb_state_unref ( state );
944  break;
945  }
946  case XCB_XKB_STATE_NOTIFY:
947  {
948  xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *) ev;
949  nk_bindings_seat_update_mask ( xcb->bindings_seat, NULL,
950  ksne->baseMods,
951  ksne->latchedMods,
952  ksne->lockedMods,
953  ksne->baseGroup,
954  ksne->latchedGroup,
955  ksne->lockedGroup );
957  break;
958  }
959  }
960  return G_SOURCE_CONTINUE;
961  }
962  if ( xcb->sndisplay != NULL ) {
963  sn_xcb_display_process_event ( xcb->sndisplay, ev );
964  }
966  return G_SOURCE_CONTINUE;
967 }
968 
969 static int take_pointer ( xcb_window_t w, int iters )
970 {
971  int i = 0;
972  while ( TRUE ) {
973  if ( xcb_connection_has_error ( xcb->connection ) ) {
974  g_warning ( "Connection has error" );
975  exit ( EXIT_FAILURE );
976  }
977  xcb_grab_pointer_cookie_t cc = xcb_grab_pointer ( xcb->connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
978  XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE, XCB_CURRENT_TIME );
979  xcb_grab_pointer_reply_t *r = xcb_grab_pointer_reply ( xcb->connection, cc, NULL );
980  if ( r ) {
981  if ( r->status == XCB_GRAB_STATUS_SUCCESS ) {
982  free ( r );
983  return 1;
984  }
985  free ( r );
986  }
987  if ( ( ++i ) > iters ) {
988  break;
989  }
990  usleep ( 1000 );
991  }
992  return 0;
993 }
994 
995 static int take_keyboard ( xcb_window_t w, int iters )
996 {
997  int i = 0;
998  while ( TRUE ) {
999  if ( xcb_connection_has_error ( xcb->connection ) ) {
1000  g_warning ( "Connection has error" );
1001  exit ( EXIT_FAILURE );
1002  }
1003  xcb_grab_keyboard_cookie_t cc = xcb_grab_keyboard ( xcb->connection,
1004  1, w, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
1005  XCB_GRAB_MODE_ASYNC );
1006  xcb_grab_keyboard_reply_t *r = xcb_grab_keyboard_reply ( xcb->connection, cc, NULL );
1007  if ( r ) {
1008  if ( r->status == XCB_GRAB_STATUS_SUCCESS ) {
1009  free ( r );
1010  return 1;
1011  }
1012  free ( r );
1013  }
1014  if ( ( ++i ) > iters ) {
1015  break;
1016  }
1017  usleep ( 1000 );
1018  }
1019  return 0;
1020 }
1021 
1022 static void release_keyboard ( void )
1023 {
1024  xcb_ungrab_keyboard ( xcb->connection, XCB_CURRENT_TIME );
1025 }
1026 static void release_pointer ( void )
1027 {
1028  xcb_ungrab_pointer ( xcb->connection, XCB_CURRENT_TIME );
1029 }
1030 
1032 static int error_trap_depth = 0;
1033 static void error_trap_push ( G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay )
1034 {
1035  ++error_trap_depth;
1036 }
1037 
1038 static void error_trap_pop ( G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay )
1039 {
1040  if ( error_trap_depth == 0 ) {
1041  g_warning ( "Error trap underflow!" );
1042  exit ( EXIT_FAILURE );
1043  }
1044 
1045  xcb_flush ( xdisplay );
1046  --error_trap_depth;
1047 }
1048 
1053 {
1054  // X atom values
1055  for ( int i = 0; i < NUM_NETATOMS; i++ ) {
1056  xcb_intern_atom_cookie_t cc = xcb_intern_atom ( xcb->connection, 0, strlen ( netatom_names[i] ), netatom_names[i] );
1057  xcb_intern_atom_reply_t *r = xcb_intern_atom_reply ( xcb->connection, cc, NULL );
1058  if ( r ) {
1059  netatoms[i] = r->atom;
1060  free ( r );
1061  }
1062  }
1063 }
1064 
1066 {
1067  xcb_window_t wm_win = 0;
1068  xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked ( &xcb->ewmh,
1070 
1071  if ( xcb_ewmh_get_supporting_wm_check_reply ( &xcb->ewmh, cc, &wm_win, NULL ) ) {
1072  xcb_ewmh_get_utf8_strings_reply_t wtitle;
1073  xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name_unchecked ( &( xcb->ewmh ), wm_win );
1074  if ( xcb_ewmh_get_wm_name_reply ( &( xcb->ewmh ), cookie, &wtitle, (void *) 0 ) ) {
1075  if ( wtitle.strings_len > 0 ) {
1076  g_debug ( "Found window manager: %s", wtitle.strings );
1077  if ( g_strcmp0 ( wtitle.strings, "i3" ) == 0 ) {
1079  }
1080  }
1081  xcb_ewmh_get_utf8_strings_reply_wipe ( &wtitle );
1082  }
1083  }
1084 }
1085 
1086 gboolean display_setup ( GMainLoop *main_loop, NkBindings *bindings )
1087 {
1088  // Get DISPLAY, first env, then argument.
1089  // We never modify display_str content.
1090  char *display_str = ( char *) g_getenv ( "DISPLAY" );
1091  find_arg_str ( "-display", &display_str );
1092 
1093  xcb->main_loop = main_loop;
1094  xcb->source = g_water_xcb_source_new ( g_main_loop_get_context ( xcb->main_loop ), display_str, &xcb->screen_nbr, main_loop_x11_event_handler, NULL, NULL );
1095  if ( xcb->source == NULL ) {
1096  g_warning ( "Failed to open display: %s", display_str );
1097  return FALSE;
1098  }
1099  xcb->connection = g_water_xcb_source_get_connection ( xcb->source );
1100 
1101  TICK_N ( "Open Display" );
1102 
1103  xcb->screen = xcb_aux_get_screen ( xcb->connection, xcb->screen_nbr );
1104 
1106 
1107  xcb_intern_atom_cookie_t *ac = xcb_ewmh_init_atoms ( xcb->connection, &xcb->ewmh );
1108  xcb_generic_error_t *errors = NULL;
1109  xcb_ewmh_init_atoms_replies ( &xcb->ewmh, ac, &errors );
1110  if ( errors ) {
1111  g_warning ( "Failed to create EWMH atoms" );
1112  free ( errors );
1113  }
1114  // Discover the current active window manager.
1116  TICK_N ( "Setup XCB" );
1117 
1118  if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
1119  XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xcb->xkb.first_event, NULL ) < 0 ) {
1120  g_warning ( "cannot setup XKB extension!" );
1121  return FALSE;
1122  }
1123 
1124  xcb->xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb->connection );
1125 
1126  enum
1127  {
1128  required_events =
1129  ( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1130  XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
1131  XCB_XKB_EVENT_TYPE_STATE_NOTIFY ),
1132 
1133  required_nkn_details =
1134  ( XCB_XKB_NKN_DETAIL_KEYCODES ),
1135 
1136  required_map_parts =
1137  ( XCB_XKB_MAP_PART_KEY_TYPES |
1138  XCB_XKB_MAP_PART_KEY_SYMS |
1139  XCB_XKB_MAP_PART_MODIFIER_MAP |
1140  XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1141  XCB_XKB_MAP_PART_KEY_ACTIONS |
1142  XCB_XKB_MAP_PART_VIRTUAL_MODS |
1143  XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ),
1144 
1145  required_state_details =
1146  ( XCB_XKB_STATE_PART_MODIFIER_BASE |
1147  XCB_XKB_STATE_PART_MODIFIER_LATCH |
1148  XCB_XKB_STATE_PART_MODIFIER_LOCK |
1149  XCB_XKB_STATE_PART_GROUP_BASE |
1150  XCB_XKB_STATE_PART_GROUP_LATCH |
1151  XCB_XKB_STATE_PART_GROUP_LOCK ),
1152  };
1153 
1154  static const xcb_xkb_select_events_details_t details = {
1155  .affectNewKeyboard = required_nkn_details,
1156  .newKeyboardDetails = required_nkn_details,
1157  .affectState = required_state_details,
1158  .stateDetails = required_state_details,
1159  };
1160  xcb_xkb_select_events ( xcb->connection, xcb->xkb.device_id, required_events, /* affectWhich */
1161  0, /* clear */
1162  required_events, /* selectAll */
1163  required_map_parts, /* affectMap */
1164  required_map_parts, /* map */
1165  &details );
1166 
1167  xcb->bindings_seat = nk_bindings_seat_new ( bindings, XKB_CONTEXT_NO_FLAGS );
1168  struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS );
1169  if ( keymap == NULL ) {
1170  g_warning ( "Failed to get Keymap for current keyboard device." );
1171  return FALSE;
1172  }
1173  struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id );
1174  if ( state == NULL ) {
1175  g_warning ( "Failed to get state object for current keyboard device." );
1176  return FALSE;
1177  }
1178 
1179  nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state );
1180  xkb_state_unref ( state );
1181  xkb_keymap_unref ( keymap );
1182 
1183  // determine numlock mask so we can bind on keys with and without it
1185 
1186  if ( xcb_connection_has_error ( xcb->connection ) ) {
1187  g_warning ( "Connection has error" );
1188  return FALSE;
1189  }
1190 
1191  // startup not.
1192  xcb->sndisplay = sn_xcb_display_new ( xcb->connection, error_trap_push, error_trap_pop );
1193  if ( xcb_connection_has_error ( xcb->connection ) ) {
1194  g_warning ( "Connection has error" );
1195  return FALSE;
1196  }
1197 
1198  if ( xcb->sndisplay != NULL ) {
1199  xcb->sncontext = sn_launchee_context_new_from_environment ( xcb->sndisplay, xcb->screen_nbr );
1200  }
1201  if ( xcb_connection_has_error ( xcb->connection ) ) {
1202  g_warning ( "Connection has error" );
1203  return FALSE;
1204  }
1205 
1206  return TRUE;
1207 }
1208 
1210 {
1211  xcb_depth_t *root_depth = NULL;
1212  xcb_depth_iterator_t depth_iter;
1213  for ( depth_iter = xcb_screen_allowed_depths_iterator ( xcb->screen ); depth_iter.rem; xcb_depth_next ( &depth_iter ) ) {
1214  xcb_depth_t *d = depth_iter.data;
1215 
1216  xcb_visualtype_iterator_t visual_iter;
1217  for ( visual_iter = xcb_depth_visuals_iterator ( d ); visual_iter.rem; xcb_visualtype_next ( &visual_iter ) ) {
1218  xcb_visualtype_t *v = visual_iter.data;
1219  if ( ( v->bits_per_rgb_value == 8 ) && ( d->depth == 32 ) && ( v->_class == XCB_VISUAL_CLASS_TRUE_COLOR ) ) {
1220  depth = d;
1221  visual = v;
1222  }
1223  if ( xcb->screen->root_visual == v->visual_id ) {
1224  root_depth = d;
1225  root_visual = v;
1226  }
1227  }
1228  }
1229  if ( visual != NULL ) {
1230  xcb_void_cookie_t c;
1231  xcb_generic_error_t *e;
1232  map = xcb_generate_id ( xcb->connection );
1233  c = xcb_create_colormap_checked ( xcb->connection, XCB_COLORMAP_ALLOC_NONE, map, xcb->screen->root, visual->visual_id );
1234  e = xcb_request_check ( xcb->connection, c );
1235  if ( e ) {
1236  depth = NULL;
1237  visual = NULL;
1238  free ( e );
1239  }
1240  }
1241 
1242  if ( visual == NULL ) {
1243  depth = root_depth;
1244  visual = root_visual;
1245  map = xcb->screen->default_colormap;
1246  }
1247 }
1248 
1250 unsigned int lazy_grab_retry_count_kb = 0;
1252 unsigned int lazy_grab_retry_count_pt = 0;
1253 static gboolean lazy_grab_pointer ( G_GNUC_UNUSED gpointer data )
1254 {
1255  // After 5 sec.
1256  if ( lazy_grab_retry_count_pt > ( 5 * 1000 ) ) {
1257  g_warning ( "Failed to grab pointer after %u times. Giving up.", lazy_grab_retry_count_pt );
1258  return G_SOURCE_REMOVE;
1259  }
1260  if ( take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
1261  return G_SOURCE_REMOVE;
1262  }
1264  return G_SOURCE_CONTINUE;
1265 }
1266 static gboolean lazy_grab_keyboard ( G_GNUC_UNUSED gpointer data )
1267 {
1268  // After 5 sec.
1269  if ( lazy_grab_retry_count_kb > ( 5 * 1000 ) ) {
1270  g_warning ( "Failed to grab keyboard after %u times. Giving up.", lazy_grab_retry_count_kb );
1271  g_main_loop_quit ( xcb->main_loop );
1272  return G_SOURCE_REMOVE;
1273  }
1274  if ( take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
1275  return G_SOURCE_REMOVE;
1276  }
1278  return G_SOURCE_CONTINUE;
1279 }
1280 
1281 gboolean display_late_setup ( void )
1282 {
1284 
1288  // Try to grab the keyboard as early as possible.
1289  // We grab this using the rootwindow (as dmenu does it).
1290  // this seems to result in the smallest delay for most people.
1291  if ( find_arg ( "-normal-window" ) >= 0 ) {
1292  return TRUE;
1293  }
1294  if ( find_arg ( "-no-lazy-grab" ) >= 0 ) {
1295  if ( !take_keyboard ( xcb_stuff_get_root_window (), 500 ) ) {
1296  g_warning ( "Failed to grab keyboard, even after %d uS.", 500 * 1000 );
1297  return FALSE;
1298  }
1299  if ( !take_pointer ( xcb_stuff_get_root_window (), 100 ) ) {
1300  g_warning ( "Failed to grab mouse pointer, even after %d uS.", 100 * 1000 );
1301  }
1302  }
1303  else {
1304  if ( !take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
1305  g_timeout_add ( 1, lazy_grab_keyboard, NULL );
1306  }
1307  if ( !take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
1308  g_timeout_add ( 1, lazy_grab_pointer, NULL );
1309  }
1310  }
1311  return TRUE;
1312 }
1313 
1314 xcb_window_t xcb_stuff_get_root_window ( void )
1315 {
1316  return xcb->screen->root;
1317 }
1318 
1320 {
1321  release_keyboard ( );
1322  release_pointer ( );
1323  xcb_flush ( xcb->connection );
1324 }
1325 
1326 void display_cleanup ( void )
1327 {
1328  if ( xcb->connection == NULL ) {
1329  return;
1330  }
1331 
1332  g_debug ( "Cleaning up XCB and XKB" );
1333 
1334  nk_bindings_seat_free ( xcb->bindings_seat );
1335  if ( xcb->sncontext != NULL ) {
1336  sn_launchee_context_unref ( xcb->sncontext );
1337  xcb->sncontext = NULL;
1338  }
1339  if ( xcb->sndisplay != NULL ) {
1340  sn_display_unref ( xcb->sndisplay );
1341  xcb->sndisplay = NULL;
1342  }
1343  x11_monitors_free ();
1344  xcb_ewmh_connection_wipe ( &( xcb->ewmh ) );
1345  xcb_flush ( xcb->connection );
1346  xcb_aux_sync ( xcb->connection );
1347  g_water_xcb_source_free ( xcb->source );
1348  xcb->source = NULL;
1349  xcb->connection = NULL;
1350  xcb->screen = NULL;
1351  xcb->screen_nbr = 0;
1352 }
1353 
1354 void x11_disable_decoration ( xcb_window_t window )
1355 {
1356  // Flag used to indicate we are setting the decoration type.
1357  const uint32_t MWM_HINTS_DECORATIONS = ( 1 << 1 );
1358  // Motif property data structure
1359  struct MotifWMHints
1360  {
1361  uint32_t flags;
1362  uint32_t functions;
1363  uint32_t decorations;
1364  int32_t inputMode;
1365  uint32_t state;
1366  };
1367 
1368  struct MotifWMHints hints;
1369  hints.flags = MWM_HINTS_DECORATIONS;
1370  hints.decorations = 0;
1371  hints.functions = 0;
1372  hints.inputMode = 0;
1373  hints.state = 0;
1374 
1375  xcb_atom_t ha = netatoms[_MOTIF_WM_HINTS];
1376  xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, window, ha, ha, 32, 5, &hints );
1377 }
xcb_depth_t * depth
Definition: xcb.c:92
Definition: xcb.h:171
#define INTERSECT(x, y, x1, y1, w1, h1)
Definition: xcb.c:73
GMainLoop * main_loop
Definition: rofi.c:114
int x
Definition: xcb.h:106
NkBindings * bindings
Definition: rofi.c:111
static workarea * x11_get_monitor_from_output(xcb_randr_output_t out)
Definition: xcb.c:208
WindowManagerQuirk
Definition: xcb.h:168
char * monitor
Definition: settings.h:145
const gchar * wmclass
Definition: helper.h:279
int mw
Definition: xcb.h:113
void display_early_cleanup(void)
Definition: xcb.c:1319
struct _workarea * next
Definition: xcb.h:117
#define sn_launcher_context_set_application_id
Definition: xcb.c:55
void x11_disable_decoration(xcb_window_t window)
Definition: xcb.c:1354
static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay)
Definition: xcb.c:1038
void rofi_view_temp_configure_notify(RofiViewState *state, xcb_configure_notify_event_t *xce)
Definition: view.c:1442
static void release_pointer(void)
Definition: xcb.c:1026
static xcb_visualtype_t * root_visual
Definition: xcb.c:98
Definition: xcb.h:99
static int take_keyboard(xcb_window_t w, int iters)
Definition: xcb.c:995
xcb_window_t xcb_stuff_get_root_window(void)
Definition: xcb.c:1314
int monitor_id
Definition: xcb.h:102
unsigned int lazy_grab_retry_count_kb
Definition: xcb.c:1250
xcb_stuff * xcb
Definition: xcb.c:87
void display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
Definition: xcb.c:453
static void x11_monitors_free(void)
Definition: xcb.c:196
static int take_pointer(xcb_window_t w, int iters)
Definition: xcb.c:969
void rofi_view_temp_click_to_exit(RofiViewState *state, xcb_window_t target)
Definition: view.c:1473
static void release_keyboard(void)
Definition: xcb.c:1022
uint8_t first_event
Definition: xcb-internal.h:57
RofiViewState * rofi_view_get_active(void)
Definition: view.c:447
cairo_surface_t * x11_helper_get_bg_surface(void)
Definition: xcb.c:141
unsigned int lazy_grab_retry_count_pt
Definition: xcb.c:1252
int find_arg_str(const char *const key, char **val)
Definition: helper.c:306
xcb_ewmh_connection_t ewmh
Definition: xcb-internal.h:48
int y
Definition: xcb.h:108
int screen_nbr
Definition: xcb-internal.h:50
xcb_timestamp_t last_timestamp
Definition: xcb-internal.h:61
void rofi_view_frame_callback(void)
Definition: view.c:1483
void display_cleanup(void)
Definition: xcb.c:1326
void rofi_view_handle_mouse_motion(RofiViewState *state, gint x, gint y)
Definition: view.c:1405
int w
Definition: xcb.h:110
static void x11_create_visual_and_colormap(void)
Definition: xcb.c:1209
static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data)
Definition: xcb.c:1253
int click_to_exit
Definition: settings.h:170
static int error_trap_depth
Definition: xcb.c:1032
static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen, xcb_atom_t atom)
Definition: xcb.c:113
gboolean display_setup(GMainLoop *main_loop, NkBindings *bindings)
Definition: xcb.c:1086
unsigned long long count
Definition: view.c:116
NkBindingsSeat * bindings_seat
Definition: xcb-internal.h:62
static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data)
Definition: xcb.c:919
char * name
Definition: xcb.h:115
const gchar * binary
Definition: helper.h:271
static int monitor_active_from_winid(xcb_drawable_t id, workarea *mon)
Definition: xcb.c:546
static int monitor_active_from_id_focused(int mon_id, workarea *mon)
Definition: xcb.c:570
void rofi_view_handle_text(RofiViewState *state, char *text)
Definition: view.c:1398
#define RANDR_PREF_MAJOR_VERSION
Definition: xcb.c:68
static int x11_is_extension_present(const char *extension)
Definition: xcb.c:289
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
Definition: helper.c:755
static void error_trap_push(G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay)
Definition: xcb.c:1033
static int pointer_get(xcb_window_t root, int *x, int *y)
Definition: xcb.c:531
static gboolean x11_button_to_nk_bindings_button(guint32 x11_button, NkBindingsMouseButton *button)
Definition: xcb.c:760
int h
Definition: xcb.h:112
static void rofi_view_paste(RofiViewState *state, xcb_selection_notify_event_t *xse)
Definition: xcb.c:736
const gchar * app_id
Definition: helper.h:277
int32_t device_id
Definition: xcb-internal.h:59
#define ATOM_CHAR(x)
Definition: xcb.h:75
static void x11_create_frequently_used_atoms(void)
Definition: xcb.c:1052
int primary
Definition: xcb.h:104
gboolean mouse_seen
Definition: xcb-internal.h:63
WindowManagerQuirk current_window_manager
Definition: xcb.c:74
GWaterXcbSource * source
Definition: xcb-internal.h:46
static void main_loop_x11_event_handler_view(xcb_generic_event_t *event)
Definition: xcb.c:816
xcb_connection_t * connection
Definition: xcb-internal.h:47
const gchar * command
Definition: helper.h:281
const gchar * description
Definition: helper.h:273
xcb_colormap_t map
Definition: xcb.c:94
MenuFlags flags
Definition: view.c:108
void display_dump_monitor_layout(void)
Definition: xcb.c:426
struct _xcb_stuff xcb_int
Definition: xcb.c:79
static void x11_build_monitor_layout_xinerama()
Definition: xcb.c:302
static void x11_build_monitor_layout()
Definition: xcb.c:338
#define RANDR_PREF_MINOR_VERSION
Definition: xcb.c:70
static int monitor_get_dimension(int monitor_id, workarea *mon)
Definition: xcb.c:492
int mh
Definition: xcb.h:113
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
Definition: xcb.c:153
int monitor_active(workarea *mon)
Definition: xcb.c:681
xcb_visualtype_t * visual
Definition: xcb.c:93
void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count)
Definition: xcb.c:178
xcb_screen_t * screen
Definition: xcb-internal.h:49
static gboolean x11_button_to_nk_bindings_scroll(guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps)
Definition: xcb.c:790
const gchar * name
Definition: helper.h:269
void rofi_view_maybe_update(RofiViewState *state)
Definition: view.c:1415
Definition: xcb.h:90
SnLauncheeContext * sncontext
Definition: xcb-internal.h:52
Settings config
int find_arg(const char *const key)
Definition: helper.c:296
struct _xcb_stuff::@5 xkb
static void x11_monitor_free(workarea *m)
Definition: xcb.c:190
static void monitor_dimensions(int x, int y, workarea *mon)
Definition: xcb.c:508
static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data)
Definition: xcb.c:1266
#define color_bold
Definition: rofi.h:92
const char * netatom_names[]
Definition: xcb.c:100
#define TICK_N(a)
Definition: timings.h:94
const gchar * icon
Definition: helper.h:275
#define color_reset
Definition: rofi.h:90
static void x11_helper_discover_window_manager(void)
Definition: xcb.c:1065
static int monitor_active_from_id(int mon_id, workarea *mon)
Definition: xcb.c:620
struct _workarea * monitors
Definition: xcb-internal.h:53
SnDisplay * sndisplay
Definition: xcb-internal.h:51
GMainLoop * main_loop
Definition: xcb-internal.h:45
workarea mon
Definition: view.c:112
xcb_atom_t netatoms[NUM_NETATOMS]
Definition: xcb.c:99
gboolean display_late_setup(void)
Definition: xcb.c:1281
cairo_surface_t * x11_helper_get_screenshot_surface(void)
Definition: xcb.c:106