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