rofi  1.5.4
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 == RANDR_PREF_MAJOR_VERSION ) && ( rversion->minor_version >= RANDR_PREF_MINOR_VERSION ) ) ||
366  ( rversion->major_version > RANDR_PREF_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  struct timespec del = {
991  .tv_sec = 0,
992  .tv_nsec = 1000000
993  };
994  nanosleep ( &del, NULL );
995  }
996  return 0;
997 }
998 
999 static int take_keyboard ( xcb_window_t w, int iters )
1000 {
1001  int i = 0;
1002  while ( TRUE ) {
1003  if ( xcb_connection_has_error ( xcb->connection ) ) {
1004  g_warning ( "Connection has error" );
1005  exit ( EXIT_FAILURE );
1006  }
1007  xcb_grab_keyboard_cookie_t cc = xcb_grab_keyboard ( xcb->connection,
1008  1, w, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
1009  XCB_GRAB_MODE_ASYNC );
1010  xcb_grab_keyboard_reply_t *r = xcb_grab_keyboard_reply ( xcb->connection, cc, NULL );
1011  if ( r ) {
1012  if ( r->status == XCB_GRAB_STATUS_SUCCESS ) {
1013  free ( r );
1014  return 1;
1015  }
1016  free ( r );
1017  }
1018  if ( ( ++i ) > iters ) {
1019  break;
1020  }
1021  struct timespec del = {
1022  .tv_sec = 0,
1023  .tv_nsec = 1000000
1024  };
1025  nanosleep ( &del, NULL );
1026  }
1027  return 0;
1028 }
1029 
1030 static void release_keyboard ( void )
1031 {
1032  xcb_ungrab_keyboard ( xcb->connection, XCB_CURRENT_TIME );
1033 }
1034 static void release_pointer ( void )
1035 {
1036  xcb_ungrab_pointer ( xcb->connection, XCB_CURRENT_TIME );
1037 }
1038 
1040 static int error_trap_depth = 0;
1041 static void error_trap_push ( G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay )
1042 {
1043  ++error_trap_depth;
1044 }
1045 
1046 static void error_trap_pop ( G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay )
1047 {
1048  if ( error_trap_depth == 0 ) {
1049  g_warning ( "Error trap underflow!" );
1050  exit ( EXIT_FAILURE );
1051  }
1052 
1053  xcb_flush ( xdisplay );
1054  --error_trap_depth;
1055 }
1056 
1061 {
1062  // X atom values
1063  for ( int i = 0; i < NUM_NETATOMS; i++ ) {
1064  xcb_intern_atom_cookie_t cc = xcb_intern_atom ( xcb->connection, 0, strlen ( netatom_names[i] ), netatom_names[i] );
1065  xcb_intern_atom_reply_t *r = xcb_intern_atom_reply ( xcb->connection, cc, NULL );
1066  if ( r ) {
1067  netatoms[i] = r->atom;
1068  free ( r );
1069  }
1070  }
1071 }
1072 
1074 {
1075  xcb_window_t wm_win = 0;
1076  xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked ( &xcb->ewmh,
1078 
1079  if ( xcb_ewmh_get_supporting_wm_check_reply ( &xcb->ewmh, cc, &wm_win, NULL ) ) {
1080  xcb_ewmh_get_utf8_strings_reply_t wtitle;
1081  xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name_unchecked ( &( xcb->ewmh ), wm_win );
1082  if ( xcb_ewmh_get_wm_name_reply ( &( xcb->ewmh ), cookie, &wtitle, (void *) 0 ) ) {
1083  if ( wtitle.strings_len > 0 ) {
1084  g_debug ( "Found window manager: %s", wtitle.strings );
1085  if ( g_strcmp0 ( wtitle.strings, "i3" ) == 0 ) {
1087  }
1088  }
1089  xcb_ewmh_get_utf8_strings_reply_wipe ( &wtitle );
1090  }
1091  }
1092 }
1093 
1094 gboolean display_setup ( GMainLoop *main_loop, NkBindings *bindings )
1095 {
1096  // Get DISPLAY, first env, then argument.
1097  // We never modify display_str content.
1098  char *display_str = ( char *) g_getenv ( "DISPLAY" );
1099  find_arg_str ( "-display", &display_str );
1100 
1101  xcb->main_loop = main_loop;
1102  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 );
1103  if ( xcb->source == NULL ) {
1104  g_warning ( "Failed to open display: %s", display_str );
1105  return FALSE;
1106  }
1107  xcb->connection = g_water_xcb_source_get_connection ( xcb->source );
1108 
1109  TICK_N ( "Open Display" );
1110 
1111  xcb->screen = xcb_aux_get_screen ( xcb->connection, xcb->screen_nbr );
1112 
1114 
1115  xcb_intern_atom_cookie_t *ac = xcb_ewmh_init_atoms ( xcb->connection, &xcb->ewmh );
1116  xcb_generic_error_t *errors = NULL;
1117  xcb_ewmh_init_atoms_replies ( &xcb->ewmh, ac, &errors );
1118  if ( errors ) {
1119  g_warning ( "Failed to create EWMH atoms" );
1120  free ( errors );
1121  }
1122  // Discover the current active window manager.
1124  TICK_N ( "Setup XCB" );
1125 
1126  if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
1127  XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xcb->xkb.first_event, NULL ) < 0 ) {
1128  g_warning ( "cannot setup XKB extension!" );
1129  return FALSE;
1130  }
1131 
1132  xcb->xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb->connection );
1133 
1134  enum
1135  {
1136  required_events =
1137  ( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1138  XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
1139  XCB_XKB_EVENT_TYPE_STATE_NOTIFY ),
1140 
1141  required_nkn_details =
1142  ( XCB_XKB_NKN_DETAIL_KEYCODES ),
1143 
1144  required_map_parts =
1145  ( XCB_XKB_MAP_PART_KEY_TYPES |
1146  XCB_XKB_MAP_PART_KEY_SYMS |
1147  XCB_XKB_MAP_PART_MODIFIER_MAP |
1148  XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1149  XCB_XKB_MAP_PART_KEY_ACTIONS |
1150  XCB_XKB_MAP_PART_VIRTUAL_MODS |
1151  XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ),
1152 
1153  required_state_details =
1154  ( XCB_XKB_STATE_PART_MODIFIER_BASE |
1155  XCB_XKB_STATE_PART_MODIFIER_LATCH |
1156  XCB_XKB_STATE_PART_MODIFIER_LOCK |
1157  XCB_XKB_STATE_PART_GROUP_BASE |
1158  XCB_XKB_STATE_PART_GROUP_LATCH |
1159  XCB_XKB_STATE_PART_GROUP_LOCK ),
1160  };
1161 
1162  static const xcb_xkb_select_events_details_t details = {
1163  .affectNewKeyboard = required_nkn_details,
1164  .newKeyboardDetails = required_nkn_details,
1165  .affectState = required_state_details,
1166  .stateDetails = required_state_details,
1167  };
1168  xcb_xkb_select_events ( xcb->connection, xcb->xkb.device_id, required_events, /* affectWhich */
1169  0, /* clear */
1170  required_events, /* selectAll */
1171  required_map_parts, /* affectMap */
1172  required_map_parts, /* map */
1173  &details );
1174 
1175  xcb->bindings_seat = nk_bindings_seat_new ( bindings, XKB_CONTEXT_NO_FLAGS );
1176  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 );
1177  if ( keymap == NULL ) {
1178  g_warning ( "Failed to get Keymap for current keyboard device." );
1179  return FALSE;
1180  }
1181  struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id );
1182  if ( state == NULL ) {
1183  g_warning ( "Failed to get state object for current keyboard device." );
1184  return FALSE;
1185  }
1186 
1187  nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state );
1188  xkb_state_unref ( state );
1189  xkb_keymap_unref ( keymap );
1190 
1191  // determine numlock mask so we can bind on keys with and without it
1193 
1194  if ( xcb_connection_has_error ( xcb->connection ) ) {
1195  g_warning ( "Connection has error" );
1196  return FALSE;
1197  }
1198 
1199  // startup not.
1200  xcb->sndisplay = sn_xcb_display_new ( xcb->connection, error_trap_push, error_trap_pop );
1201  if ( xcb_connection_has_error ( xcb->connection ) ) {
1202  g_warning ( "Connection has error" );
1203  return FALSE;
1204  }
1205 
1206  if ( xcb->sndisplay != NULL ) {
1207  xcb->sncontext = sn_launchee_context_new_from_environment ( xcb->sndisplay, xcb->screen_nbr );
1208  }
1209  if ( xcb_connection_has_error ( xcb->connection ) ) {
1210  g_warning ( "Connection has error" );
1211  return FALSE;
1212  }
1213 
1214  return TRUE;
1215 }
1216 
1218 {
1219  xcb_depth_t *root_depth = NULL;
1220  xcb_depth_iterator_t depth_iter;
1221  for ( depth_iter = xcb_screen_allowed_depths_iterator ( xcb->screen ); depth_iter.rem; xcb_depth_next ( &depth_iter ) ) {
1222  xcb_depth_t *d = depth_iter.data;
1223 
1224  xcb_visualtype_iterator_t visual_iter;
1225  for ( visual_iter = xcb_depth_visuals_iterator ( d ); visual_iter.rem; xcb_visualtype_next ( &visual_iter ) ) {
1226  xcb_visualtype_t *v = visual_iter.data;
1227  if ( ( v->bits_per_rgb_value == 8 ) && ( d->depth == 32 ) && ( v->_class == XCB_VISUAL_CLASS_TRUE_COLOR ) ) {
1228  depth = d;
1229  visual = v;
1230  }
1231  if ( xcb->screen->root_visual == v->visual_id ) {
1232  root_depth = d;
1233  root_visual = v;
1234  }
1235  }
1236  }
1237  if ( visual != NULL ) {
1238  xcb_void_cookie_t c;
1239  xcb_generic_error_t *e;
1240  map = xcb_generate_id ( xcb->connection );
1241  c = xcb_create_colormap_checked ( xcb->connection, XCB_COLORMAP_ALLOC_NONE, map, xcb->screen->root, visual->visual_id );
1242  e = xcb_request_check ( xcb->connection, c );
1243  if ( e ) {
1244  depth = NULL;
1245  visual = NULL;
1246  free ( e );
1247  }
1248  }
1249 
1250  if ( visual == NULL ) {
1251  depth = root_depth;
1252  visual = root_visual;
1253  map = xcb->screen->default_colormap;
1254  }
1255 }
1256 
1258 unsigned int lazy_grab_retry_count_kb = 0;
1260 unsigned int lazy_grab_retry_count_pt = 0;
1261 static gboolean lazy_grab_pointer ( G_GNUC_UNUSED gpointer data )
1262 {
1263  // After 5 sec.
1264  if ( lazy_grab_retry_count_pt > ( 5 * 1000 ) ) {
1265  g_warning ( "Failed to grab pointer after %u times. Giving up.", lazy_grab_retry_count_pt );
1266  return G_SOURCE_REMOVE;
1267  }
1268  if ( take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
1269  return G_SOURCE_REMOVE;
1270  }
1272  return G_SOURCE_CONTINUE;
1273 }
1274 static gboolean lazy_grab_keyboard ( G_GNUC_UNUSED gpointer data )
1275 {
1276  // After 5 sec.
1277  if ( lazy_grab_retry_count_kb > ( 5 * 1000 ) ) {
1278  g_warning ( "Failed to grab keyboard after %u times. Giving up.", lazy_grab_retry_count_kb );
1279  g_main_loop_quit ( xcb->main_loop );
1280  return G_SOURCE_REMOVE;
1281  }
1282  if ( take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
1283  return G_SOURCE_REMOVE;
1284  }
1286  return G_SOURCE_CONTINUE;
1287 }
1288 
1289 gboolean display_late_setup ( void )
1290 {
1292 
1296  // Try to grab the keyboard as early as possible.
1297  // We grab this using the rootwindow (as dmenu does it).
1298  // this seems to result in the smallest delay for most people.
1299  if ( find_arg ( "-normal-window" ) >= 0 ) {
1300  return TRUE;
1301  }
1302  if ( find_arg ( "-no-lazy-grab" ) >= 0 ) {
1303  if ( !take_keyboard ( xcb_stuff_get_root_window (), 500 ) ) {
1304  g_warning ( "Failed to grab keyboard, even after %d uS.", 500 * 1000 );
1305  return FALSE;
1306  }
1307  if ( !take_pointer ( xcb_stuff_get_root_window (), 100 ) ) {
1308  g_warning ( "Failed to grab mouse pointer, even after %d uS.", 100 * 1000 );
1309  }
1310  }
1311  else {
1312  if ( !take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
1313  g_timeout_add ( 1, lazy_grab_keyboard, NULL );
1314  }
1315  if ( !take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
1316  g_timeout_add ( 1, lazy_grab_pointer, NULL );
1317  }
1318  }
1319  return TRUE;
1320 }
1321 
1322 xcb_window_t xcb_stuff_get_root_window ( void )
1323 {
1324  return xcb->screen->root;
1325 }
1326 
1328 {
1329  release_keyboard ( );
1330  release_pointer ( );
1331  xcb_flush ( xcb->connection );
1332 }
1333 
1334 void display_cleanup ( void )
1335 {
1336  if ( xcb->connection == NULL ) {
1337  return;
1338  }
1339 
1340  g_debug ( "Cleaning up XCB and XKB" );
1341 
1342  nk_bindings_seat_free ( xcb->bindings_seat );
1343  if ( xcb->sncontext != NULL ) {
1344  sn_launchee_context_unref ( xcb->sncontext );
1345  xcb->sncontext = NULL;
1346  }
1347  if ( xcb->sndisplay != NULL ) {
1348  sn_display_unref ( xcb->sndisplay );
1349  xcb->sndisplay = NULL;
1350  }
1351  x11_monitors_free ();
1352  xcb_ewmh_connection_wipe ( &( xcb->ewmh ) );
1353  xcb_flush ( xcb->connection );
1354  xcb_aux_sync ( xcb->connection );
1355  g_water_xcb_source_free ( xcb->source );
1356  xcb->source = NULL;
1357  xcb->connection = NULL;
1358  xcb->screen = NULL;
1359  xcb->screen_nbr = 0;
1360 }
1361 
1362 void x11_disable_decoration ( xcb_window_t window )
1363 {
1364  // Flag used to indicate we are setting the decoration type.
1365  const uint32_t MWM_HINTS_DECORATIONS = ( 1 << 1 );
1366  // Motif property data structure
1367  struct MotifWMHints
1368  {
1369  uint32_t flags;
1370  uint32_t functions;
1371  uint32_t decorations;
1372  int32_t inputMode;
1373  uint32_t state;
1374  };
1375 
1376  struct MotifWMHints hints;
1377  hints.flags = MWM_HINTS_DECORATIONS;
1378  hints.decorations = 0;
1379  hints.functions = 0;
1380  hints.inputMode = 0;
1381  hints.state = 0;
1382 
1383  xcb_atom_t ha = netatoms[_MOTIF_WM_HINTS];
1384  xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, window, ha, ha, 32, 5, &hints );
1385 }
xcb_int
struct _xcb_stuff xcb_int
Definition: xcb.c:79
take_pointer
static int take_pointer(xcb_window_t w, int iters)
Definition: xcb.c:969
rofi_view_frame_callback
void rofi_view_frame_callback(void)
Definition: view.c:1481
xcb
xcb_stuff * xcb
Definition: xcb.c:87
depth
xcb_depth_t * depth
Definition: xcb.c:92
sn_launcher_context_set_application_id
#define sn_launcher_context_set_application_id
Definition: xcb.c:55
_workarea::x
int x
Definition: xcb.h:106
_xcb_stuff::screen
xcb_screen_t * screen
Definition: xcb-internal.h:49
settings.h
_workarea::mw
int mw
Definition: xcb.h:113
lazy_grab_retry_count_kb
unsigned int lazy_grab_retry_count_kb
Definition: xcb.c:1258
monitor_active_from_id
static int monitor_active_from_id(int mon_id, workarea *mon)
Definition: xcb.c:620
RofiHelperExecuteContext::command
const gchar * command
Definition: helper.h:281
rofi_view_get_active
RofiViewState * rofi_view_get_active(void)
Definition: view.c:447
x11_disable_decoration
void x11_disable_decoration(xcb_window_t window)
Definition: xcb.c:1362
x11_monitors_free
static void x11_monitors_free(void)
Definition: xcb.c:196
_xcb_stuff::first_event
uint8_t first_event
Definition: xcb-internal.h:57
Settings::monitor
char * monitor
Definition: settings.h:147
_workarea::y
int y
Definition: xcb.h:108
display_late_setup
gboolean display_late_setup(void)
Definition: xcb.c:1289
mon
workarea mon
Definition: view.c:112
RofiHelperExecuteContext
Definition: helper.h:267
rofi_view_maybe_update
void rofi_view_maybe_update(RofiViewState *state)
Definition: view.c:1413
rofi-types.h
_workarea::monitor_id
int monitor_id
Definition: xcb.h:102
display_cleanup
void display_cleanup(void)
Definition: xcb.c:1334
_xcb_stuff
Definition: xcb-internal.h:44
x11_create_visual_and_colormap
static void x11_create_visual_and_colormap(void)
Definition: xcb.c:1217
rofi_view_handle_text
void rofi_view_handle_text(RofiViewState *state, char *text)
Definition: view.c:1396
_workarea::next
struct _workarea * next
Definition: xcb.h:117
_xcb_stuff::sncontext
SnLauncheeContext * sncontext
Definition: xcb-internal.h:52
lazy_grab_retry_count_pt
unsigned int lazy_grab_retry_count_pt
Definition: xcb.c:1260
_xcb_stuff::main_loop
GMainLoop * main_loop
Definition: xcb-internal.h:45
_xcb_stuff::mouse_seen
gboolean mouse_seen
Definition: xcb-internal.h:63
WM_DO_NOT_CHANGE_CURRENT_DESKTOP
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP
Definition: xcb.h:173
WM_PANGO_WORKSPACE_NAMES
@ WM_PANGO_WORKSPACE_NAMES
Definition: xcb.h:175
Settings::click_to_exit
int click_to_exit
Definition: settings.h:172
find_arg
int find_arg(const char *const key)
Definition: helper.c:267
error_trap_pop
static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay)
Definition: xcb.c:1046
_workarea
Definition: xcb.h:100
timings.h
x11_create_frequently_used_atoms
static void x11_create_frequently_used_atoms(void)
Definition: xcb.c:1060
flags
MenuFlags flags
Definition: view.c:108
_workarea::h
int h
Definition: xcb.h:112
x11_button_to_nk_bindings_button
static gboolean x11_button_to_nk_bindings_button(guint32 x11_button, NkBindingsMouseButton *button)
Definition: xcb.c:760
_xcb_stuff::ewmh
xcb_ewmh_connection_t ewmh
Definition: xcb-internal.h:48
_xcb_stuff::last_timestamp
xcb_timestamp_t last_timestamp
Definition: xcb-internal.h:61
rofi_view_handle_mouse_motion
void rofi_view_handle_mouse_motion(RofiViewState *state, gint x, gint y)
Definition: view.c:1403
release_keyboard
static void release_keyboard(void)
Definition: xcb.c:1030
monitor_active
int monitor_active(workarea *mon)
Definition: xcb.c:681
release_pointer
static void release_pointer(void)
Definition: xcb.c:1034
RofiHelperExecuteContext::description
const gchar * description
Definition: helper.h:273
EWMH_ATOMS
@ EWMH_ATOMS
Definition: xcb.h:90
rofi_view_temp_configure_notify
void rofi_view_temp_configure_notify(RofiViewState *state, xcb_configure_notify_event_t *xce)
Definition: view.c:1440
TICK_N
#define TICK_N(a)
Definition: timings.h:68
color_bold
#define color_bold
Definition: rofi.h:92
display_dump_monitor_layout
void display_dump_monitor_layout(void)
Definition: xcb.c:426
x11_build_monitor_layout_xinerama
static void x11_build_monitor_layout_xinerama()
Definition: xcb.c:302
monitor_get_dimension
static int monitor_get_dimension(int monitor_id, workarea *mon)
Definition: xcb.c:492
main_loop_x11_event_handler
static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data)
Definition: xcb.c:919
visual
xcb_visualtype_t * visual
Definition: xcb.c:93
window_set_atom_prop
void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count)
Definition: xcb.c:178
_workarea::w
int w
Definition: xcb.h:110
x11_helper_get_bg_surface
cairo_surface_t * x11_helper_get_bg_surface(void)
Definition: xcb.c:141
_xcb_stuff::source
GWaterXcbSource * source
Definition: xcb-internal.h:46
RofiHelperExecuteContext::app_id
const gchar * app_id
Definition: helper.h:277
rofi.h
x11_is_extension_present
static int x11_is_extension_present(const char *extension)
Definition: xcb.c:289
_xcb_stuff::bindings_seat
NkBindingsSeat * bindings_seat
Definition: xcb-internal.h:62
error_trap_depth
static int error_trap_depth
Definition: xcb.c:1040
WM_EWHM
@ WM_EWHM
Definition: xcb.h:171
_xcb_stuff::sndisplay
SnDisplay * sndisplay
Definition: xcb-internal.h:51
x11_monitor_free
static void x11_monitor_free(workarea *m)
Definition: xcb.c:190
rofi_view_temp_click_to_exit
void rofi_view_temp_click_to_exit(RofiViewState *state, xcb_window_t target)
Definition: view.c:1471
monitor_dimensions
static void monitor_dimensions(int x, int y, workarea *mon)
Definition: xcb.c:508
pointer_get
static int pointer_get(xcb_window_t root, int *x, int *y)
Definition: xcb.c:531
lazy_grab_keyboard
static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data)
Definition: xcb.c:1274
_workarea::name
char * name
Definition: xcb.h:115
ATOM_CHAR
#define ATOM_CHAR(x)
Definition: xcb.h:75
rofi_view_paste
static void rofi_view_paste(RofiViewState *state, xcb_selection_notify_event_t *xse)
Definition: xcb.c:736
x11_helper_get_screenshot_surface
cairo_surface_t * x11_helper_get_screenshot_surface(void)
Definition: xcb.c:106
xcb.h
WindowManagerQuirk
WindowManagerQuirk
Definition: xcb.h:169
monitor_active_from_winid
static int monitor_active_from_winid(xcb_drawable_t id, workarea *mon)
Definition: xcb.c:546
find_arg_str
int find_arg_str(const char *const key, char **val)
Definition: helper.c:277
lazy_grab_pointer
static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data)
Definition: xcb.c:1261
display_setup
gboolean display_setup(GMainLoop *main_loop, NkBindings *bindings)
Definition: xcb.c:1094
_xcb_stuff::connection
xcb_connection_t * connection
Definition: xcb-internal.h:47
RANDR_PREF_MAJOR_VERSION
#define RANDR_PREF_MAJOR_VERSION
Definition: xcb.c:68
display.h
_xcb_stuff::screen_nbr
int screen_nbr
Definition: xcb-internal.h:50
netatom_names
const char * netatom_names[]
Definition: xcb.c:100
netatoms
xcb_atom_t netatoms[NUM_NETATOMS]
Definition: xcb.c:99
RofiHelperExecuteContext::icon
const gchar * icon
Definition: helper.h:275
_xcb_stuff::device_id
int32_t device_id
Definition: xcb-internal.h:59
bindings
NkBindings * bindings
Definition: rofi.c:112
window_get_text_prop
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
Definition: xcb.c:153
count
unsigned long long count
Definition: view.c:116
display_early_cleanup
void display_early_cleanup(void)
Definition: xcb.c:1327
INTERSECT
#define INTERSECT(x, y, x1, y1, w1, h1)
Definition: xcb.c:73
rofi_latin_to_utf8_strdup
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
Definition: helper.c:726
_workarea::primary
int primary
Definition: xcb.h:104
get_root_pixmap
static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen, xcb_atom_t atom)
Definition: xcb.c:113
map
xcb_colormap_t map
Definition: xcb.c:94
RofiHelperExecuteContext::wmclass
const gchar * wmclass
Definition: helper.h:279
_workarea::mh
int mh
Definition: xcb.h:113
take_keyboard
static int take_keyboard(xcb_window_t w, int iters)
Definition: xcb.c:999
x11_button_to_nk_bindings_scroll
static gboolean x11_button_to_nk_bindings_scroll(guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps)
Definition: xcb.c:790
xcb_stuff_get_root_window
xcb_window_t xcb_stuff_get_root_window(void)
Definition: xcb.c:1322
main_loop
GMainLoop * main_loop
Definition: rofi.c:115
color_reset
#define color_reset
Definition: rofi.h:90
monitor_active_from_id_focused
static int monitor_active_from_id_focused(int mon_id, workarea *mon)
Definition: xcb.c:570
current_window_manager
WindowManagerQuirk current_window_manager
Definition: xcb.c:74
main_loop_x11_event_handler_view
static void main_loop_x11_event_handler_view(xcb_generic_event_t *event)
Definition: xcb.c:816
helper.h
xcb-internal.h
display_startup_notification
void display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
Definition: xcb.c:453
x11_get_monitor_from_output
static workarea * x11_get_monitor_from_output(xcb_randr_output_t out)
Definition: xcb.c:208
NUM_NETATOMS
@ NUM_NETATOMS
Definition: xcb.h:90
error_trap_push
static void error_trap_push(G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay)
Definition: xcb.c:1041
_xcb_stuff::xkb
struct _xcb_stuff::@5 xkb
RofiViewState
Definition: view-internal.h:48
RofiHelperExecuteContext::binary
const gchar * binary
Definition: helper.h:271
x11_build_monitor_layout
static void x11_build_monitor_layout()
Definition: xcb.c:338
root_visual
static xcb_visualtype_t * root_visual
Definition: xcb.c:98
RANDR_PREF_MINOR_VERSION
#define RANDR_PREF_MINOR_VERSION
Definition: xcb.c:70
config
Settings config
RofiHelperExecuteContext::name
const gchar * name
Definition: helper.h:269
x11_helper_discover_window_manager
static void x11_helper_discover_window_manager(void)
Definition: xcb.c:1073
_xcb_stuff::monitors
struct _workarea * monitors
Definition: xcb-internal.h:53