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