rofi 2.1.2
display.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-2023 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
34#ifdef XCB_IMDKIT
35#include <xcb-imdkit/encoding.h>
36#include <xcb/xcb_keysyms.h>
37#endif
38#include <cairo-xcb.h>
39#include <cairo.h>
40#include <glib.h>
41#include <math.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <xcb/randr.h>
48#include <xcb/xcb.h>
49#include <xcb/xcb_aux.h>
50#include <xcb/xcb_cursor.h>
51#include <xcb/xcb_ewmh.h>
52#include <xcb/xinerama.h>
53#include <xcb/xkb.h>
54#include <xcb/xproto.h>
55#include <xkbcommon/xkbcommon-x11.h>
56#include <xkbcommon/xkbcommon.h>
58#define SN_API_NOT_YET_FROZEN
61#define sn_launcher_context_set_application_id sn_launcher_set_application_id
62#include "display-internal.h"
63#include "display.h"
64#include "helper.h"
65#include "rofi-types.h"
66#include "settings.h"
67#include "timings.h"
68#include "xcb-internal.h"
69#include "xcb.h"
70#include <libsn/sn.h>
71#include <stdbool.h>
72
73#include "mode.h"
74#include "modes/window.h"
75
76#include <rofi.h>
77
79#define RANDR_PREF_MAJOR_VERSION 1
81#define RANDR_PREF_MINOR_VERSION 5
82
84#define INTERSECT(x, y, x1, y1, w1, h1) \
85 ((((x) >= (x1)) && ((x) < (x1 + w1))) && (((y) >= (y1)) && ((y) < (y1 + h1))))
86
88
92struct _xcb_stuff xcb_int = {.connection = NULL,
93 .screen = NULL,
94#ifdef XCB_IMDKIT
95 .im = NULL,
96 .syms = NULL,
97#endif
98 .screen_nbr = -1,
99 .sndisplay = NULL,
100 .sncontext = NULL,
101 .monitors = NULL,
102 .clipboard = NULL};
104
108xcb_depth_t *depth = NULL;
109xcb_visualtype_t *visual = NULL;
110xcb_colormap_t map = XCB_COLORMAP_NONE;
114static xcb_visualtype_t *root_visual = NULL;
117
121xcb_cursor_t cursors[NUM_CURSORS] = {XCB_CURSOR_NONE, XCB_CURSOR_NONE,
122 XCB_CURSOR_NONE};
123
125const struct {
127 const char *css_name;
129 const char *traditional_name;
130} cursor_names[] = {
131 {"default", "left_ptr"}, {"pointer", "hand"}, {"text", "xterm"}};
132
133static xcb_visualtype_t *lookup_visual(xcb_screen_t *s, xcb_visualid_t vis) {
134 xcb_depth_iterator_t d;
135 d = xcb_screen_allowed_depths_iterator(s);
136 for (; d.rem; xcb_depth_next(&d)) {
137 xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator(d.data);
138 for (; v.rem; xcb_visualtype_next(&v)) {
139 if (v.data->visual_id == vis) {
140 return v.data;
141 }
142 }
143 }
144 return 0;
145}
146
147/* This blur function was originally created my MacSlow and published on his
148 * website: http://macslow.thepimp.net. I'm not entirely sure he's proud of it,
149 * but it has proved immeasurably useful for me. */
150
151static uint32_t *create_kernel(int radius, double deviation, uint32_t *sum2) {
152 int size = 2 * (radius) + 1;
153 uint32_t *kernel = (uint32_t *)(g_malloc(sizeof(uint32_t) * (size + 1)));
154 double radiusf = abs(radius) + 1.0;
155 double value = -radius;
156 double sum = 0.0;
157 int i;
158
159 if (deviation == 0.0) {
160 deviation = sqrt(-(radiusf * radiusf) / (2.0 * log(1.0 / 255.0)));
161 }
162
163 kernel[0] = size;
164
165 for (i = 0; i < size; i++) {
166 kernel[1 + i] = INT16_MAX / (2.506628275 * deviation) *
167 exp(-((value * value) / (2.0 * (deviation * deviation))));
168
169 sum += kernel[1 + i];
170 value += 1.0;
171 }
172
173 *sum2 = sum;
174
175 return kernel;
176}
177
178void cairo_image_surface_blur(cairo_surface_t *surface, int radius,
179 double deviation) {
180 uint32_t *horzBlur;
181 uint32_t *kernel = 0;
182 cairo_format_t format;
183 unsigned int channels;
184
185 if (cairo_surface_status(surface)) {
186 return;
187 }
188
189 uint8_t *data = cairo_image_surface_get_data(surface);
190 format = cairo_image_surface_get_format(surface);
191 const int width = cairo_image_surface_get_width(surface);
192 const int height = cairo_image_surface_get_height(surface);
193 const int stride = cairo_image_surface_get_stride(surface);
194 if (radius > (height / 2)) {
195 radius = height / 2 - 1;
196 }
197 if (radius > (width / 2)) {
198 radius = width / 2 - 1;
199 }
200 if (radius < 1) {
201 return;
202 }
203
204 if (format == CAIRO_FORMAT_ARGB32) {
205 channels = 4;
206 } else {
207 return;
208 }
209
210 horzBlur = (uint32_t *)(g_malloc(sizeof(uint32_t) * height * stride));
211 TICK();
212 uint32_t sum = 0;
213 kernel = create_kernel(radius, deviation, &sum);
214 TICK_N("BLUR: kernel");
215
216 /* Horizontal pass. */
217 uint32_t *horzBlur_ptr = horzBlur;
218 for (int iY = 0; iY < height; iY++) {
219 const int iYs = iY * stride;
220 for (int iX = 0; iX < width; iX++) {
221 uint32_t red = 0;
222 uint32_t green = 0;
223 uint32_t blue = 0;
224 uint32_t alpha = 0;
225 int offset = (int)(kernel[0]) / -2;
226
227 for (int i = 0; i < (int)(kernel[0]); i++) {
228 int x = iX + offset;
229
230 if (x < 0 || x >= width) {
231 offset++;
232 continue;
233 }
234
235 uint8_t *dataPtr = &data[iYs + x * channels];
236 const uint32_t kernip1 = kernel[i + 1];
237
238 blue += kernip1 * dataPtr[0];
239 green += kernip1 * dataPtr[1];
240 red += kernip1 * dataPtr[2];
241 alpha += kernip1 * dataPtr[3];
242 offset++;
243 }
244
245 *horzBlur_ptr++ = blue / sum;
246 *horzBlur_ptr++ = green / sum;
247 *horzBlur_ptr++ = red / sum;
248 *horzBlur_ptr++ = alpha / sum;
249 }
250 }
251 TICK_N("BLUR: hori");
252
253 /* Vertical pass. */
254 for (int iY = 0; iY < height; iY++) {
255 for (int iX = 0; iX < width; iX++) {
256 uint32_t red = 0;
257 uint32_t green = 0;
258 uint32_t blue = 0;
259 uint32_t alpha = 0;
260 int offset = (int)(kernel[0]) / -2;
261
262 const int iXs = iX * channels;
263 for (int i = 0; i < (int)(kernel[0]); i++) {
264 int y = iY + offset;
265
266 if (y < 0 || y >= height) {
267 offset++;
268 continue;
269 }
270
271 uint32_t *dataPtr = &horzBlur[y * stride + iXs];
272 const uint32_t kernip1 = kernel[i + 1];
273
274 blue += kernip1 * dataPtr[0];
275 green += kernip1 * dataPtr[1];
276 red += kernip1 * dataPtr[2];
277 alpha += kernip1 * dataPtr[3];
278
279 offset++;
280 }
281
282 *data++ = blue / sum;
283 *data++ = green / sum;
284 *data++ = red / sum;
285 *data++ = alpha / sum;
286 }
287 }
288 TICK_N("BLUR: vert");
289
290 free(kernel);
291 free(horzBlur);
292
293 return;
294}
295
297 int size) {
298 xcb_get_geometry_cookie_t cookie;
299 xcb_get_geometry_reply_t *reply;
300
301 cookie = xcb_get_geometry(xcb->connection, window);
302 reply = xcb_get_geometry_reply(xcb->connection, cookie, NULL);
303 if (reply == NULL) {
304 return NULL;
305 }
306
307 xcb_get_window_attributes_cookie_t attributesCookie =
308 xcb_get_window_attributes(xcb->connection, window);
309 xcb_get_window_attributes_reply_t *attributes =
310 xcb_get_window_attributes_reply(xcb->connection, attributesCookie, NULL);
311 if (attributes == NULL || (attributes->map_state != XCB_MAP_STATE_VIEWABLE)) {
312 free(reply);
313 if (attributes) {
314 free(attributes);
315 }
316 return NULL;
317 }
318 // Create a cairo surface for the window.
319 xcb_visualtype_t *vt = lookup_visual(xcb->screen, attributes->visual);
320 free(attributes);
321
322 cairo_surface_t *t = cairo_xcb_surface_create(xcb->connection, window, vt,
323 reply->width, reply->height);
324
325 if (cairo_surface_status(t) != CAIRO_STATUS_SUCCESS) {
326 cairo_surface_destroy(t);
327 free(reply);
328 return NULL;
329 }
330
331 // Scale the image, as we don't want to keep large one around.
332 int max = MAX(reply->width, reply->height);
333 double scale = (double)size / max;
334
335 cairo_surface_t *s2 = cairo_surface_create_similar_image(
336 t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale);
337 free(reply);
338
339 if (cairo_surface_status(s2) != CAIRO_STATUS_SUCCESS) {
340 cairo_surface_destroy(t);
341 return NULL;
342 }
343 // Paint it in.
344 cairo_t *d = cairo_create(s2);
345 cairo_scale(d, scale, scale);
346 cairo_set_source_surface(d, t, 0, 0);
347 cairo_paint(d);
348 cairo_destroy(d);
349
350 cairo_surface_destroy(t);
351 return s2;
352}
353
357cairo_surface_t *x11_helper_get_screenshot_surface(void) {
358 return cairo_xcb_surface_create(xcb->connection, xcb_stuff_get_root_window(),
359 root_visual, xcb->screen->width_in_pixels,
360 xcb->screen->height_in_pixels);
361}
362
363static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen,
364 xcb_atom_t atom) {
365 xcb_get_property_cookie_t cookie;
366 xcb_get_property_reply_t *reply;
367 xcb_pixmap_t rootpixmap = XCB_NONE;
368
369 cookie = xcb_get_property(c, 0, screen->root, atom, XCB_ATOM_PIXMAP, 0, 1);
370
371 reply = xcb_get_property_reply(c, cookie, NULL);
372
373 if (reply) {
374 if (xcb_get_property_value_length(reply) == sizeof(xcb_pixmap_t)) {
375 memcpy(&rootpixmap, xcb_get_property_value(reply), sizeof(xcb_pixmap_t));
376 }
377 free(reply);
378 }
379
380 return rootpixmap;
381}
382
383cairo_surface_t *x11_helper_get_bg_surface(void) {
384 xcb_pixmap_t pm =
385 get_root_pixmap(xcb->connection, xcb->screen, netatoms[ESETROOT_PMAP_ID]);
386 if (pm == XCB_NONE) {
387 return NULL;
388 }
389 return cairo_xcb_surface_create(xcb->connection, pm, root_visual,
390 xcb->screen->width_in_pixels,
391 xcb->screen->height_in_pixels);
392}
393
394// retrieve a text property from a window
395// technically we could use window_get_prop(), but this is better for character
396// set support
397char *window_get_text_prop(xcb_window_t w, xcb_atom_t atom) {
398 xcb_get_property_cookie_t c = xcb_get_property(
399 xcb->connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
400 xcb_get_property_reply_t *r =
401 xcb_get_property_reply(xcb->connection, c, NULL);
402 if (r) {
403 if (xcb_get_property_value_length(r) > 0) {
404 char *str = NULL;
405 if (r->type == netatoms[UTF8_STRING]) {
406 str = rofi_force_utf8(xcb_get_property_value(r),
407 xcb_get_property_value_length(r));
408 } else if (r->type == netatoms[STRING]) {
409 str = rofi_latin_to_utf8_strdup(xcb_get_property_value(r),
410 xcb_get_property_value_length(r));
411 } else {
412 str = g_strdup("Invalid encoding.");
413 }
414
415 free(r);
416 return str;
417 }
418 free(r);
419 }
420 return NULL;
421}
422
423void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms,
424 int count) {
425 xcb_change_property(xcb->connection, XCB_PROP_MODE_REPLACE, w, prop,
426 XCB_ATOM_ATOM, 32, count, atoms);
427}
428
429/****
430 * Code used to get monitor layout.
431 */
432
436static void x11_monitor_free(workarea *m) {
437 g_free(m->name);
438 g_free(m);
439}
440
441static void x11_monitors_free(void) {
442 while (xcb->monitors != NULL) {
443 workarea *m = xcb->monitors;
444 xcb->monitors = m->next;
446 }
447}
448
455 double ratio_res = w->w / (double)w->h;
456 double ratio_size = w->mw / (double)w->mh;
457
458 if ((ratio_res < 1.0 && ratio_size > 1.0) ||
459 (ratio_res > 1.0 && ratio_size < 1.0)) {
460 // Oposite ratios, swap them.
461 int nh = w->mw;
462 w->mw = w->mh;
463 w->mh = nh;
464 }
465}
466
469static workarea *x11_get_monitor_from_output(xcb_randr_output_t out) {
470 xcb_randr_get_output_info_reply_t *op_reply;
471 xcb_randr_get_crtc_info_reply_t *crtc_reply;
472 xcb_randr_get_output_info_cookie_t it =
473 xcb_randr_get_output_info(xcb->connection, out, XCB_CURRENT_TIME);
474 op_reply = xcb_randr_get_output_info_reply(xcb->connection, it, NULL);
475 if (op_reply->crtc == XCB_NONE) {
476 free(op_reply);
477 return NULL;
478 }
479 xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info(
480 xcb->connection, op_reply->crtc, XCB_CURRENT_TIME);
481 crtc_reply = xcb_randr_get_crtc_info_reply(xcb->connection, ct, NULL);
482 if (!crtc_reply) {
483 free(op_reply);
484 return NULL;
485 }
486 workarea *retv = g_malloc0(sizeof(workarea));
487 retv->x = crtc_reply->x;
488 retv->y = crtc_reply->y;
489 retv->w = crtc_reply->width;
490 retv->h = crtc_reply->height;
491
492 retv->mw = op_reply->mm_width;
493 retv->mh = op_reply->mm_height;
495
496 char *tname = (char *)xcb_randr_get_output_info_name(op_reply);
497 int tname_len = xcb_randr_get_output_info_name_length(op_reply);
498
499 retv->name = g_malloc0((tname_len + 1) * sizeof(char));
500 memcpy(retv->name, tname, tname_len);
501
502 free(crtc_reply);
503 free(op_reply);
504 return retv;
505}
506
507#if (((XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION) && \
508 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
509 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
517static workarea *
518x11_get_monitor_from_randr_monitor(xcb_randr_monitor_info_t *mon) {
519 // Query to the name of the monitor.
520 xcb_generic_error_t *err;
521 xcb_get_atom_name_cookie_t anc =
522 xcb_get_atom_name(xcb->connection, mon->name);
523 xcb_get_atom_name_reply_t *atom_reply =
524 xcb_get_atom_name_reply(xcb->connection, anc, &err);
525 if (err != NULL) {
526 g_warning("Could not get RandR monitor name: X11 error code %d\n",
527 err->error_code);
528 free(err);
529 return NULL;
530 }
531 workarea *retv = g_malloc0(sizeof(workarea));
532
533 // Is primary monitor.
534 retv->primary = mon->primary;
535
536 // Position and size.
537 retv->x = mon->x;
538 retv->y = mon->y;
539 retv->w = mon->width;
540 retv->h = mon->height;
541
542 // Physical
543 retv->mw = mon->width_in_millimeters;
544 retv->mh = mon->height_in_millimeters;
546
547 // Name
548 retv->name =
549 g_strdup_printf("%.*s", xcb_get_atom_name_name_length(atom_reply),
550 xcb_get_atom_name_name(atom_reply));
551
552 // Free name atom.
553 free(atom_reply);
554
555 return retv;
556}
557#endif
558
559static int x11_is_extension_present(const char *extension) {
560 xcb_query_extension_cookie_t randr_cookie =
561 xcb_query_extension(xcb->connection, strlen(extension), extension);
562
563 xcb_query_extension_reply_t *randr_reply =
564 xcb_query_extension_reply(xcb->connection, randr_cookie, NULL);
565
566 int present = randr_reply->present;
567
568 free(randr_reply);
569
570 return present;
571}
572
574 xcb_xinerama_query_screens_cookie_t screens_cookie =
575 xcb_xinerama_query_screens_unchecked(xcb->connection);
576
577 xcb_xinerama_query_screens_reply_t *screens_reply =
578 xcb_xinerama_query_screens_reply(xcb->connection, screens_cookie, NULL);
579
580 xcb_xinerama_screen_info_iterator_t screens_iterator =
581 xcb_xinerama_query_screens_screen_info_iterator(screens_reply);
582
583 for (; screens_iterator.rem > 0;
584 xcb_xinerama_screen_info_next(&screens_iterator)) {
585 workarea *w = g_malloc0(sizeof(workarea));
586
587 w->x = screens_iterator.data->x_org;
588 w->y = screens_iterator.data->y_org;
589 w->w = screens_iterator.data->width;
590 w->h = screens_iterator.data->height;
591
592 w->next = xcb->monitors;
593 xcb->monitors = w;
594 }
595
596 int index = 0;
597 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
598 iter->monitor_id = index++;
599 }
600
601 free(screens_reply);
602}
603
604static void x11_build_monitor_layout(void) {
605 if (xcb->monitors) {
606 return;
607 }
608 // If RANDR is not available, try Xinerama
609 if (!x11_is_extension_present("RANDR")) {
610 // Check if xinerama is available.
611 if (x11_is_extension_present("XINERAMA")) {
612 g_debug("Query XINERAMA for monitor layout.");
614 return;
615 }
616 g_debug("No RANDR or Xinerama available for getting monitor layout.");
617 return;
618 }
619 g_debug("Query RANDR for monitor layout.");
620
621 g_debug("Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION,
622 XCB_RANDR_MINOR_VERSION);
623#if (((XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION) && \
624 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
625 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
626 xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version(
628 xcb_randr_query_version_reply_t *rversion =
629 xcb_randr_query_version_reply(xcb->connection, cversion, NULL);
630 if (rversion) {
631 g_debug("Found randr version: %d.%d", rversion->major_version,
632 rversion->minor_version);
633 // Check if we are 1.5 and up.
634 if (((rversion->major_version == RANDR_PREF_MAJOR_VERSION) &&
635 (rversion->minor_version >= RANDR_PREF_MINOR_VERSION)) ||
636 (rversion->major_version > RANDR_PREF_MAJOR_VERSION)) {
637 xcb_randr_get_monitors_cookie_t t =
638 xcb_randr_get_monitors(xcb->connection, xcb->screen->root, 1);
639 xcb_randr_get_monitors_reply_t *mreply =
640 xcb_randr_get_monitors_reply(xcb->connection, t, NULL);
641 if (mreply) {
642 xcb_randr_monitor_info_iterator_t iter =
643 xcb_randr_get_monitors_monitors_iterator(mreply);
644 while (iter.rem > 0) {
645 workarea *w = x11_get_monitor_from_randr_monitor(iter.data);
646 if (w) {
647 w->next = xcb->monitors;
648 xcb->monitors = w;
649 }
650 xcb_randr_monitor_info_next(&iter);
651 }
652 free(mreply);
653 }
654 }
655 free(rversion);
656 }
657#endif
658
659 // If no monitors found.
660 if (xcb->monitors == NULL) {
661 xcb_randr_get_screen_resources_current_reply_t *res_reply;
662 xcb_randr_get_screen_resources_current_cookie_t src;
663 src = xcb_randr_get_screen_resources_current(xcb->connection,
664 xcb->screen->root);
665 res_reply = xcb_randr_get_screen_resources_current_reply(xcb->connection,
666 src, NULL);
667 if (!res_reply) {
668 return; // just report error
669 }
670 int mon_num =
671 xcb_randr_get_screen_resources_current_outputs_length(res_reply);
672 xcb_randr_output_t *ops =
673 xcb_randr_get_screen_resources_current_outputs(res_reply);
674
675 // Get primary.
676 xcb_randr_get_output_primary_cookie_t pc =
677 xcb_randr_get_output_primary(xcb->connection, xcb->screen->root);
678 xcb_randr_get_output_primary_reply_t *pc_rep =
679 xcb_randr_get_output_primary_reply(xcb->connection, pc, NULL);
680
681 for (int i = mon_num - 1; i >= 0; i--) {
683 if (w) {
684 w->next = xcb->monitors;
685 xcb->monitors = w;
686 if (pc_rep && pc_rep->output == ops[i]) {
687 w->primary = TRUE;
688 }
689 }
690 }
691 // If exists, free primary output reply.
692 if (pc_rep) {
693 free(pc_rep);
694 }
695 free(res_reply);
696 }
697
698 // Number monitor
699 int index = 0;
700 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
701 iter->monitor_id = index++;
702 }
703}
704
706 int is_term = isatty(fileno(stdout));
707 printf("Monitor layout:\n");
708 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
709 printf("%s ID%s: %d", (is_term) ? color_bold : "",
710 is_term ? color_reset : "", iter->monitor_id);
711 if (iter->primary) {
712 printf(" (primary)");
713 }
714 printf("\n");
715 printf("%s name%s: %s\n", (is_term) ? color_bold : "",
716 is_term ? color_reset : "", iter->name);
717 printf("%s position%s: %d,%d\n", (is_term) ? color_bold : "",
718 is_term ? color_reset : "", iter->x, iter->y);
719 printf("%s size%s: %d,%d\n", (is_term) ? color_bold : "",
720 is_term ? color_reset : "", iter->w, iter->h);
721 if (iter->mw > 0 && iter->mh > 0) {
722 printf("%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
723 (is_term) ? color_bold : "", is_term ? color_reset : "", iter->mw,
724 iter->mh, iter->w * 25.4 / (double)iter->mw,
725 iter->h * 25.4 / (double)iter->mh);
726 }
727 printf("\n");
728 }
729}
730
732 GSpawnChildSetupFunc *child_setup,
733 gpointer *user_data) {
734 if (context == NULL) {
735 return;
736 }
737
738 SnLauncherContext *sncontext;
739
740 sncontext = sn_launcher_context_new(xcb->sndisplay, xcb->screen_nbr);
741
742 sn_launcher_context_set_name(sncontext, context->name);
743 sn_launcher_context_set_description(sncontext, context->description);
744 if (context->binary != NULL) {
745 sn_launcher_context_set_binary_name(sncontext, context->binary);
746 }
747 if (context->icon != NULL) {
748 sn_launcher_context_set_icon_name(sncontext, context->icon);
749 }
750 if (context->app_id != NULL) {
752 }
753 if (context->wmclass != NULL) {
754 sn_launcher_context_set_wmclass(sncontext, context->wmclass);
755 }
756
757 xcb_get_property_cookie_t c;
758 unsigned int current_desktop = 0;
759
760 c = xcb_ewmh_get_current_desktop(&xcb->ewmh, xcb->screen_nbr);
761 if (xcb_ewmh_get_current_desktop_reply(&xcb->ewmh, c, &current_desktop,
762 NULL)) {
763 sn_launcher_context_set_workspace(sncontext, current_desktop);
764 }
765
766 sn_launcher_context_initiate(sncontext, "rofi", context->command,
768
769 *child_setup = (GSpawnChildSetupFunc)sn_launcher_context_setup_child_process;
770 *user_data = sncontext;
771}
772
773static int monitor_get_dimension(int monitor_id, workarea *mon) {
774 memset(mon, 0, sizeof(workarea));
775 mon->w = xcb->screen->width_in_pixels;
776 mon->h = xcb->screen->height_in_pixels;
777
778 workarea *iter = NULL;
779 for (iter = xcb->monitors; iter; iter = iter->next) {
780 if (iter->monitor_id == monitor_id) {
781 *mon = *iter;
782 return TRUE;
783 }
784 }
785 return FALSE;
786}
787// find the dimensions of the monitor displaying point x,y
788static void monitor_dimensions(int x, int y, workarea *mon) {
789 if (mon == NULL) {
790 g_error("%s: mon == NULL", __func__);
791 return;
792 }
793 memset(mon, 0, sizeof(workarea));
794 mon->w = xcb->screen->width_in_pixels;
795 mon->h = xcb->screen->height_in_pixels;
796
797 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
798 if (INTERSECT(x, y, iter->x, iter->y, iter->w, iter->h)) {
799 *mon = *iter;
800 break;
801 }
802 }
803}
804
815static int pointer_get(xcb_window_t root, int *x, int *y) {
816 *x = 0;
817 *y = 0;
818 xcb_query_pointer_cookie_t c = xcb_query_pointer(xcb->connection, root);
819 xcb_query_pointer_reply_t *r =
820 xcb_query_pointer_reply(xcb->connection, c, NULL);
821 if (r) {
822 *x = r->root_x;
823 *y = r->root_y;
824 free(r);
825 return TRUE;
826 }
827
828 return FALSE;
829}
830static int monitor_active_from_winid(xcb_drawable_t id, workarea *mon) {
831 if (mon == NULL) {
832 g_error("%s: mon == NULL", __func__);
833 return FALSE;
834 }
835 xcb_window_t root = xcb->screen->root;
836 xcb_get_geometry_cookie_t c = xcb_get_geometry(xcb->connection, id);
837 xcb_get_geometry_reply_t *r =
838 xcb_get_geometry_reply(xcb->connection, c, NULL);
839 if (r) {
840 xcb_translate_coordinates_cookie_t ct =
841 xcb_translate_coordinates(xcb->connection, id, root, r->x, r->y);
842 xcb_translate_coordinates_reply_t *t =
843 xcb_translate_coordinates_reply(xcb->connection, ct, NULL);
844 if (t) {
845 // place the menu above the window
846 // if some window is focused, place menu above window, else fall
847 // back to selected monitor.
848 mon->x = t->dst_x - r->x;
849 mon->y = t->dst_y - r->y;
850 mon->w = r->width;
851 mon->h = r->height;
852 free(r);
853 free(t);
854 return TRUE;
855 }
856 free(r);
857 }
858 return FALSE;
859}
860static int monitor_active_from_id_focused(int mon_id, workarea *mon) {
861 int retv = FALSE;
862 xcb_window_t active_window;
863 xcb_get_property_cookie_t awc;
864 if (mon == NULL) {
865 g_error("%s: mon == NULL", __func__);
866 return retv;
867 }
868 awc = xcb_ewmh_get_active_window(&xcb->ewmh, xcb->screen_nbr);
869 if (!xcb_ewmh_get_active_window_reply(&xcb->ewmh, awc, &active_window,
870 NULL)) {
871 g_debug(
872 "Failed to get active window, falling back to mouse location (-5).");
873 return retv;
874 }
875 xcb_query_tree_cookie_t tree_cookie =
876 xcb_query_tree(xcb->connection, active_window);
877 xcb_query_tree_reply_t *tree_reply =
878 xcb_query_tree_reply(xcb->connection, tree_cookie, NULL);
879 if (!tree_reply) {
880 g_debug(
881 "Failed to get parent window, falling back to mouse location (-5).");
882 return retv;
883 }
884 // get geometry.
885 xcb_get_geometry_cookie_t c =
886 xcb_get_geometry(xcb->connection, active_window);
887 xcb_get_geometry_reply_t *r =
888 xcb_get_geometry_reply(xcb->connection, c, NULL);
889 if (!r) {
890 g_debug("Failed to get geometry of active window, falling back to mouse "
891 "location (-5).");
892 free(tree_reply);
893 return retv;
894 }
895 if (tree_reply->parent != r->root) {
896 xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates(
897 xcb->connection, tree_reply->parent, r->root, r->x, r->y);
898 xcb_translate_coordinates_reply_t *t =
899 xcb_translate_coordinates_reply(xcb->connection, ct, NULL);
900 if (t) {
901 r->x = t->dst_x;
902 r->y = t->dst_y;
903 free(t);
904 } else {
905 g_debug("Failed to get translate position of active window, falling back "
906 "to mouse location (-5).");
907 free(r);
908 free(tree_reply);
909 return retv;
910 }
911 }
912 if (mon_id == -2) {
913 // place the menu above the window
914 // if some window is focused, place menu above window, else fall
915 // back to selected monitor.
916 mon->x = r->x + r->border_width;
917 mon->y = r->y + r->border_width;
918 mon->w = r->width;
919 mon->h = r->height;
920 retv = TRUE;
921 } else if (mon_id == -4) {
922 g_debug("Find monitor at location: %d %d", r->x, r->y);
923 monitor_dimensions(r->x, r->y, mon);
924 retv = TRUE;
925 }
926 g_debug("mon pos: %d %d %d-%d", mon->x, mon->y, mon->w, mon->h);
927 free(r);
928 free(tree_reply);
929 return retv;
930}
931static int monitor_active_from_id(int mon_id, workarea *mon) {
932 if (xcb == NULL || xcb->screen == NULL) {
933 g_warning("XCB structure is uninitialized, monitor info not available.");
934 return FALSE;
935 }
936 xcb_window_t root = xcb->screen->root;
937 int x, y;
938 if (mon == NULL) {
939 g_error("%s: mon == NULL", __func__);
940 return FALSE;
941 }
942 g_debug("Monitor id: %d", mon_id);
943 // At mouse position.
944 if (mon_id == -3) {
945 if (pointer_get(root, &x, &y)) {
946 monitor_dimensions(x, y, mon);
947 mon->x = x;
948 mon->y = y;
949 return TRUE;
950 }
951 }
952 // Focused monitor
953 else if (mon_id == -1) {
954 g_debug("rofi on current monitor");
955 // Get the current desktop.
956 unsigned int current_desktop = 0;
957 xcb_get_property_cookie_t gcdc;
958 gcdc = xcb_ewmh_get_current_desktop(&xcb->ewmh, xcb->screen_nbr);
959 if (xcb_ewmh_get_current_desktop_reply(&xcb->ewmh, gcdc, &current_desktop,
960 NULL)) {
961 g_debug("Found current desktop: %u", current_desktop);
962 xcb_get_property_cookie_t c =
963 xcb_ewmh_get_desktop_viewport(&xcb->ewmh, xcb->screen_nbr);
964 xcb_ewmh_get_desktop_viewport_reply_t vp;
965 if (xcb_ewmh_get_desktop_viewport_reply(&xcb->ewmh, c, &vp, NULL)) {
966 g_debug("Found %d number of desktops", vp.desktop_viewport_len);
967 if (current_desktop < vp.desktop_viewport_len) {
968 g_debug("Found viewport for desktop: %d %d",
969 vp.desktop_viewport[current_desktop].x,
970 vp.desktop_viewport[current_desktop].y);
971 monitor_dimensions(vp.desktop_viewport[current_desktop].x,
972 vp.desktop_viewport[current_desktop].y, mon);
973 g_debug("Found monitor @: %d %d %dx%d", mon->x, mon->y, mon->w,
974 mon->h);
975 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
976 return TRUE;
977 } else {
978 g_debug("Viewport does not exist for current desktop: %d, falling "
979 "back to mouse location (-5)",
980 current_desktop);
981 }
982 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
983 } else {
984 g_debug("Failed to get viewport for current desktop: %d, falling back "
985 "to mouse location (-5).",
986 current_desktop);
987 }
988 } else {
989 g_debug("Failed to get current desktop, falling back to mouse location "
990 "(-5).");
991 }
992 } else if (mon_id == -2 || mon_id == -4) {
993 if (monitor_active_from_id_focused(mon_id, mon)) {
994 return TRUE;
995 }
996 }
997 // Monitor that has mouse pointer.
998 else if (mon_id == -5) {
999 if (pointer_get(root, &x, &y)) {
1000 monitor_dimensions(x, y, mon);
1001 return TRUE;
1002 }
1003 // This is our give up point.
1004 return FALSE;
1005 }
1006 g_debug("Failed to find monitor, fall back to monitor showing mouse.");
1007 return monitor_active_from_id(-5, mon);
1008}
1009
1011gboolean mon_set = FALSE;
1014 0,
1015};
1017 if (mon == NULL) {
1018 g_error("%s: mon == NULL", __func__);
1019 return FALSE;
1020 }
1021 g_debug("Monitor active");
1022 if (mon_set) {
1023 *mon = mon_cache;
1024 return TRUE;
1025 }
1026 if (config.monitor != NULL) {
1027 g_debug("Monitor lookup by name : %s", config.monitor);
1028 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
1029 if (g_strcmp0(config.monitor, iter->name) == 0) {
1030 *mon = *iter;
1031 mon_cache = *mon;
1032 mon_set = TRUE;
1033 return TRUE;
1034 }
1035 }
1036 }
1037 g_debug("Monitor lookup by name failed: %s", config.monitor);
1038 // Grab primary.
1039 if (g_strcmp0(config.monitor, "primary") == 0) {
1040 for (workarea *iter = xcb->monitors; iter; iter = iter->next) {
1041 if (iter->primary) {
1042 *mon = *iter;
1043 mon_cache = *mon;
1044 mon_set = TRUE;
1045 return TRUE;
1046 }
1047 }
1048 }
1049 if (g_str_has_prefix(config.monitor, "wid:")) {
1050 char *end = NULL;
1051 xcb_drawable_t win = g_ascii_strtoll(config.monitor + 4, &end, 0);
1052 if (end != config.monitor) {
1053 if (monitor_active_from_winid(win, mon)) {
1054 mon_cache = *mon;
1055 mon_set = TRUE;
1056 return TRUE;
1057 }
1058 }
1059 }
1060 {
1061 // IF fail, fall back to classic mode.
1062 char *end = NULL;
1063 gint64 mon_id = g_ascii_strtoll(config.monitor, &end, 0);
1064 if (end != config.monitor) {
1065 if (mon_id >= 0) {
1066 if (monitor_get_dimension(mon_id, mon)) {
1067 mon_cache = *mon;
1068 mon_set = TRUE;
1069 return TRUE;
1070 }
1071 g_warning("Failed to find selected monitor.");
1072 } else {
1073 int val = monitor_active_from_id(mon_id, mon);
1074 mon_cache = *mon;
1075 mon_set = TRUE;
1076 return val;
1077 }
1078 }
1079 }
1080 // Fallback.
1081 monitor_dimensions(0, 0, mon);
1082 mon_cache = *mon;
1083 mon_set = TRUE;
1084 return FALSE;
1085}
1086
1087static bool get_atom_name(xcb_connection_t *conn, xcb_atom_t atom, char **out) {
1088 xcb_get_atom_name_cookie_t cookie;
1089 xcb_get_atom_name_reply_t *reply;
1090 int length;
1091 char *name;
1092
1093 if (atom == 0) {
1094 *out = NULL;
1095 return true;
1096 }
1097
1098 cookie = xcb_get_atom_name(conn, atom);
1099 reply = xcb_get_atom_name_reply(conn, cookie, NULL);
1100 if (!reply)
1101 return false;
1102
1103 length = xcb_get_atom_name_name_length(reply);
1104 name = xcb_get_atom_name_name(reply);
1105
1106 (*out) = g_strndup(name, length);
1107 if (!(*out)) {
1108 free(reply);
1109 return false;
1110 }
1111
1112 free(reply);
1113 return true;
1114}
1115
1123 xcb_selection_notify_event_t *xse) {
1124 if (xse->property == XCB_ATOM_NONE) {
1125 g_debug("Failed to convert selection");
1126 } else if (xse->property == xcb->ewmh.UTF8_STRING) {
1127 gchar *text = window_get_text_prop(xse->requestor, xcb->ewmh.UTF8_STRING);
1128 if (text != NULL && text[0] != '\0') {
1129 unsigned int dl = strlen(text);
1130 // Strip new line
1131 for (unsigned int i = 0; i < dl; i++) {
1132 if (text[i] == '\n') {
1133 text[i] = '\0';
1134 }
1135 }
1136 rofi_view_handle_text(state, text);
1137 }
1138 g_free(text);
1139 } else {
1140 char *out = NULL;
1141 if (get_atom_name(xcb->connection, xse->property, &out)) {
1142 g_debug("rofi_view_paste: Got unknown atom: %s", out);
1143 g_free(out);
1144 } else {
1145 g_debug("rofi_view_paste: Got unknown, unnamed: %s", out);
1146 }
1147 }
1148}
1149
1150static gboolean
1152 NkBindingsMouseButton *button) {
1153 switch (x11_button) {
1154 case 1:
1155 *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
1156 break;
1157 case 3:
1158 *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
1159 break;
1160 case 2:
1161 *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
1162 break;
1163 case 8:
1164 *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
1165 break;
1166 case 9:
1167 *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
1168 break;
1169 case 4:
1170 case 5:
1171 case 6:
1172 case 7:
1173 return FALSE;
1174 default:
1175 *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
1176 }
1177 return TRUE;
1178}
1179
1180static gboolean x11_button_to_nk_bindings_scroll(guint32 x11_button,
1181 NkBindingsScrollAxis *axis,
1182 gint32 *steps) {
1183 *steps = 1;
1184 switch (x11_button) {
1185 case 4:
1186 *steps = -1;
1188 case 5:
1189 *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
1190 break;
1191 case 6:
1192 *steps = -1;
1194 case 7:
1195 *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
1196 break;
1197 default:
1198 return FALSE;
1199 }
1200 return TRUE;
1201}
1202
1203static void rofi_key_press_event_handler(xcb_key_press_event_t *xkpe,
1204 RofiViewState *state) {
1205 gchar *text;
1206 g_log("IMDKit", G_LOG_LEVEL_DEBUG, "press handler %d", xkpe->detail);
1207
1208 xcb->last_timestamp = xkpe->time;
1210 text = nk_bindings_seat_handle_key_with_modmask(
1211 xcb->bindings_seat, NULL, xkpe->state, xkpe->detail,
1212 NK_BINDINGS_KEY_STATE_PRESS);
1213 } else {
1214 text = nk_bindings_seat_handle_key(xcb->bindings_seat, NULL, xkpe->detail,
1215 NK_BINDINGS_KEY_STATE_PRESS);
1216 }
1217 if (text != NULL) {
1218 rofi_view_handle_text(state, text);
1219 g_free(text);
1220 }
1221}
1222
1223static void rofi_key_release_event_handler(xcb_key_release_event_t *xkre,
1224 G_GNUC_UNUSED RofiViewState *state) {
1225 g_log("IMDKit", G_LOG_LEVEL_DEBUG, "release handler %d", xkre->detail);
1226 xcb->last_timestamp = xkre->time;
1227 nk_bindings_seat_handle_key(xcb->bindings_seat, NULL, xkre->detail,
1228 NK_BINDINGS_KEY_STATE_RELEASE);
1229}
1230
1234static void main_loop_x11_event_handler_view(xcb_generic_event_t *event) {
1236 if (state == NULL) {
1237 return;
1238 }
1239
1240 switch (event->response_type & ~0x80) {
1241 case XCB_CLIENT_MESSAGE: {
1242 xcb_client_message_event_t *cme = (xcb_client_message_event_t *)event;
1243 xcb_atom_t atom = cme->data.data32[0];
1244 xcb_timestamp_t time = cme->data.data32[1];
1245 if (atom == netatoms[WM_TAKE_FOCUS]) {
1246 xcb_set_input_focus(xcb->connection, XCB_INPUT_FOCUS_NONE, cme->window,
1247 time);
1248 xcb_flush(xcb->connection);
1249 }
1250 break;
1251 }
1252 case XCB_DESTROY_NOTIFY: {
1253 xcb_window_t win = ((xcb_destroy_notify_event_t *)event)->window;
1254 if (win != rofi_view_get_window()) {
1255#ifdef WINDOW_MODE
1256 window_client_handle_signal(win, FALSE);
1257#endif
1258 } else {
1259 g_main_loop_quit(xcb->main_loop);
1260 }
1261 break;
1262 }
1263 case XCB_CREATE_NOTIFY: {
1264 xcb_window_t win = ((xcb_create_notify_event_t *)event)->window;
1265 if (win != rofi_view_get_window()) {
1266#ifdef WINDOW_MODE
1267 window_client_handle_signal(win, TRUE);
1268#endif
1269 }
1270 break;
1271 }
1272 case XCB_EXPOSE:
1274 break;
1275 case XCB_CONFIGURE_NOTIFY: {
1278 break;
1279 }
1280 case XCB_MOTION_NOTIFY: {
1281 xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *)event;
1282 gboolean button_mask = xme->state & XCB_EVENT_MASK_BUTTON_1_MOTION;
1283 rofi_view_handle_mouse_motion(state, xme->event_x, xme->event_y,
1284 !button_mask && config.hover_select);
1285 break;
1286 }
1287 case XCB_BUTTON_PRESS: {
1288 xcb_button_press_event_t *bpe = (xcb_button_press_event_t *)event;
1289 NkBindingsMouseButton button;
1290 NkBindingsScrollAxis axis;
1291 gint32 steps;
1292
1293 xcb->last_timestamp = bpe->time;
1294 rofi_view_handle_mouse_motion(state, bpe->event_x, bpe->event_y, FALSE);
1295 if (x11_button_to_nk_bindings_button(bpe->detail, &button)) {
1296 nk_bindings_seat_handle_button(xcb->bindings_seat, NULL, button,
1297 NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time);
1298 } else if (x11_button_to_nk_bindings_scroll(bpe->detail, &axis, &steps)) {
1299 nk_bindings_seat_handle_scroll(xcb->bindings_seat, NULL, axis, steps);
1300 }
1301 xcb->mouse_seen++;
1302 break;
1303 }
1304 case XCB_SELECTION_CLEAR: {
1305 g_debug("Selection Clear.");
1307 } break;
1308 case XCB_SELECTION_REQUEST: {
1309 g_debug("Selection Request.");
1310 xcb_selection_request_event_t *req = (xcb_selection_request_event_t *)event;
1311 if (req->selection == netatoms[CLIPBOARD]) {
1312 xcb_atom_t targets[2];
1313 xcb_selection_notify_event_t selection_notify = {
1314 .response_type = XCB_SELECTION_NOTIFY,
1315 .sequence = 0,
1316 .time = req->time,
1317 .requestor = req->requestor,
1318 .selection = req->selection,
1319 .target = req->target,
1320 .property = XCB_ATOM_NONE};
1321 // If no clipboard, we return NONE.
1322 if (xcb->clipboard) {
1323 // Request for UTF-8
1324 if (req->target == netatoms[UTF8_STRING]) {
1325 g_debug("Selection Request UTF-8.");
1326 xcb_change_property(xcb->connection, XCB_PROP_MODE_REPLACE,
1327 req->requestor, req->property,
1328 netatoms[UTF8_STRING], 8,
1329 strlen(xcb->clipboard) + 1, xcb->clipboard);
1330 selection_notify.property = req->property;
1331 } else if (req->target == netatoms[TARGETS]) {
1332 g_debug("Selection Request Targets.");
1333 // We currently only support UTF8 from clipboard. So indicate this.
1334 targets[0] = netatoms[UTF8_STRING];
1335 xcb_change_property(xcb->connection, XCB_PROP_MODE_REPLACE,
1336 req->requestor, req->property, XCB_ATOM_ATOM, 32,
1337 1, targets);
1338 selection_notify.property = req->property;
1339 }
1340 }
1341
1342 xcb_send_event(xcb->connection,
1343 0, // propagate
1344 req->requestor, XCB_EVENT_MASK_NO_EVENT,
1345 (const char *)&selection_notify);
1346 xcb_flush(xcb->connection);
1347 }
1348 } break;
1349 case XCB_BUTTON_RELEASE: {
1350 xcb_button_release_event_t *bre = (xcb_button_release_event_t *)event;
1351 NkBindingsMouseButton button;
1352
1353 xcb->last_timestamp = bre->time;
1354 if (x11_button_to_nk_bindings_button(bre->detail, &button)) {
1355 nk_bindings_seat_handle_button(xcb->bindings_seat, NULL, button,
1356 NK_BINDINGS_BUTTON_STATE_RELEASE,
1357 bre->time);
1358 }
1359 if (config.click_to_exit == TRUE) {
1360 if (!xcb->mouse_seen) {
1361 rofi_view_temp_click_to_exit(state, bre->event);
1362 }
1363 xcb->mouse_seen--;
1364 }
1365 break;
1366 }
1367 // Paste event.
1368 case XCB_SELECTION_NOTIFY:
1369 rofi_view_paste(state, (xcb_selection_notify_event_t *)event);
1370 break;
1371 case XCB_KEYMAP_NOTIFY: {
1372 xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *)event;
1373 for (gint32 by = 0; by < 31; ++by) {
1374 for (gint8 bi = 0; bi < 7; ++bi) {
1375 if (kne->keys[by] & (1 << bi)) {
1376 // X11 keycodes starts at 8
1377 nk_bindings_seat_handle_key(xcb->bindings_seat, NULL,
1378 (8 * by + bi) + 8,
1379 NK_BINDINGS_KEY_STATE_PRESSED);
1380 }
1381 }
1382 }
1383 break;
1384 }
1385 case XCB_KEY_PRESS: {
1386 xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *)event;
1387#ifdef XCB_IMDKIT
1388 if (config.enable_imdkit && xcb->ic) {
1389 g_log("IMDKit", G_LOG_LEVEL_DEBUG, "press key %d to xim", xkpe->detail);
1390 xcb_xim_forward_event(xcb->im, xcb->ic, xkpe);
1391 return;
1392 } else
1393#endif
1394 {
1395 rofi_key_press_event_handler(xkpe, state);
1396 }
1397 break;
1398 }
1399 case XCB_KEY_RELEASE: {
1400 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1401#ifdef XCB_IMDKIT
1402 if (config.enable_imdkit && xcb->ic) {
1403 g_log("IMDKit", G_LOG_LEVEL_DEBUG, "release key %d to xim", xkre->detail);
1404
1405 // Check if the keysym is a modifier key (e.g., Shift, Ctrl, Alt). If it
1406 // is, sleep for 5 milliseconds as a workaround for XCB XIM limitation.
1407 // This sleep helps to ensure that XCB XIM can properly handle subsequent
1408 // key events that may occur rapidly after a modifier key is pressed.
1409 xcb_keysym_t sym = xcb_key_press_lookup_keysym(xcb->syms, xkre, 0);
1410 if (xcb_is_modifier_key(sym)) {
1411 struct timespec five_millis = {.tv_sec = 0, .tv_nsec = 5000000};
1412 nanosleep(&five_millis, NULL);
1413 }
1414 xcb_xim_forward_event(xcb->im, xcb->ic, xkre);
1415 return;
1416 } else
1417#endif
1418 {
1419 rofi_key_release_event_handler(xkre, state);
1420 }
1421 break;
1422 }
1423 default:
1424 break;
1425 }
1427}
1428
1429#ifdef XCB_IMDKIT
1430void x11_event_handler_fowarding(G_GNUC_UNUSED xcb_xim_t *im,
1431 G_GNUC_UNUSED xcb_xic_t ic,
1432 xcb_key_press_event_t *event,
1433 G_GNUC_UNUSED void *user_data) {
1435 if (state == NULL) {
1436 return;
1437 }
1438
1439 uint8_t type = event->response_type & ~0x80;
1440 if (type == XCB_KEY_PRESS) {
1441 rofi_key_press_event_handler(event, state);
1442 } else if (type == XCB_KEY_RELEASE) {
1443 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1444 rofi_key_release_event_handler(xkre, state);
1445 }
1447}
1448#endif
1449
1450static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev,
1451 G_GNUC_UNUSED gpointer user_data) {
1452 if (ev == NULL) {
1453 int status = xcb_connection_has_error(xcb->connection);
1454 if (status > 0) {
1455 g_warning("The XCB connection to X server had a fatal error: %d", status);
1456 g_main_loop_quit(xcb->main_loop);
1457 return G_SOURCE_REMOVE;
1458 }
1459 // DD: it seems this handler often gets dispatched while the queue in GWater
1460 // is empty. resulting in a NULL for ev. This seems not an error.
1461 // g_warning("main_loop_x11_event_handler: ev == NULL, status == %d",
1462 // status);
1463 return G_SOURCE_CONTINUE;
1464 }
1465
1466#ifdef XCB_IMDKIT
1467 if (config.enable_imdkit && xcb->im && xcb_xim_filter_event(xcb->im, ev))
1468 return G_SOURCE_CONTINUE;
1469#endif
1470
1471 uint8_t type = ev->response_type & ~0x80;
1472 if (type == xcb->xkb.first_event) {
1473 switch (ev->pad0) {
1474 case XCB_XKB_MAP_NOTIFY: {
1475 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1476 nk_bindings_seat_get_context(xcb->bindings_seat), xcb->connection,
1477 xcb->xkb.device_id, 0);
1478 struct xkb_state *state = xkb_x11_state_new_from_device(
1479 keymap, xcb->connection, xcb->xkb.device_id);
1480 nk_bindings_seat_update_keymap(xcb->bindings_seat, keymap, state);
1481 xkb_keymap_unref(keymap);
1482 xkb_state_unref(state);
1483 break;
1484 }
1485 case XCB_XKB_STATE_NOTIFY: {
1486 xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *)ev;
1487 nk_bindings_seat_update_mask(xcb->bindings_seat, NULL, ksne->baseMods,
1488 ksne->latchedMods, ksne->lockedMods,
1489 ksne->baseGroup, ksne->latchedGroup,
1490 ksne->lockedGroup);
1492 break;
1493 }
1494 }
1495 return G_SOURCE_CONTINUE;
1496 }
1497 if (xcb->sndisplay != NULL) {
1498 sn_xcb_display_process_event(xcb->sndisplay, ev);
1499 }
1500
1502 return G_SOURCE_CONTINUE;
1503}
1504
1505static int take_pointer(xcb_window_t w, int iters) {
1506 int i = 0;
1507 while (TRUE) {
1508 if (xcb_connection_has_error(xcb->connection)) {
1509 g_warning("Connection has error");
1510 exit(EXIT_FAILURE);
1511 }
1512 xcb_grab_pointer_cookie_t cc =
1513 xcb_grab_pointer(xcb->connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
1514 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE,
1515 XCB_CURRENT_TIME);
1516 xcb_grab_pointer_reply_t *r =
1517 xcb_grab_pointer_reply(xcb->connection, cc, NULL);
1518 if (r) {
1519 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1520 free(r);
1521 return 1;
1522 }
1523 free(r);
1524 }
1525 if ((++i) > iters) {
1526 break;
1527 }
1528 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1529 nanosleep(&del, NULL);
1530 }
1531 return 0;
1532}
1533
1534static int take_keyboard(xcb_window_t w, int iters) {
1535 int i = 0;
1536 while (TRUE) {
1537 if (xcb_connection_has_error(xcb->connection)) {
1538 g_warning("Connection has error");
1539 exit(EXIT_FAILURE);
1540 }
1541 xcb_grab_keyboard_cookie_t cc =
1542 xcb_grab_keyboard(xcb->connection, 1, w, XCB_CURRENT_TIME,
1543 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1544 xcb_grab_keyboard_reply_t *r =
1545 xcb_grab_keyboard_reply(xcb->connection, cc, NULL);
1546 if (r) {
1547 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1548 free(r);
1549 return 1;
1550 }
1551 free(r);
1552 }
1553 if ((++i) > iters) {
1554 break;
1555 }
1556 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1557 nanosleep(&del, NULL);
1558 }
1559 return 0;
1560}
1561
1562static void release_keyboard(void) {
1563 xcb_ungrab_keyboard(xcb->connection, XCB_CURRENT_TIME);
1564}
1565static void release_pointer(void) {
1566 xcb_ungrab_pointer(xcb->connection, XCB_CURRENT_TIME);
1567}
1568
1570static int error_trap_depth = 0;
1571static void error_trap_push(G_GNUC_UNUSED SnDisplay *display,
1572 G_GNUC_UNUSED xcb_connection_t *xdisplay) {
1574}
1575
1576static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display,
1577 xcb_connection_t *xdisplay) {
1578 if (error_trap_depth == 0) {
1579 g_warning("Error trap underflow!");
1580 exit(EXIT_FAILURE);
1581 }
1582
1583 xcb_flush(xdisplay);
1585}
1586
1591 // X atom values
1592 for (int i = 0; i < NUM_NETATOMS; i++) {
1593 xcb_intern_atom_cookie_t cc = xcb_intern_atom(
1594 xcb->connection, 0, strlen(netatom_names[i]), netatom_names[i]);
1595 xcb_intern_atom_reply_t *r =
1596 xcb_intern_atom_reply(xcb->connection, cc, NULL);
1597 if (r) {
1598 netatoms[i] = r->atom;
1599 free(r);
1600 }
1601 }
1602}
1603
1605 char *retv = NULL;
1606 xcb_window_t wm_win = 0;
1607 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1609
1610 if (xcb_ewmh_get_supporting_wm_check_reply(&xcb->ewmh, cc, &wm_win, NULL)) {
1611 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1612 xcb_get_property_cookie_t cookie =
1613 xcb_ewmh_get_wm_name_unchecked(&(xcb->ewmh), wm_win);
1614 if (xcb_ewmh_get_wm_name_reply(&(xcb->ewmh), cookie, &wtitle, (void *)0)) {
1615 if (wtitle.strings_len > 0) {
1616 retv = g_strndup(wtitle.strings, wtitle.strings_len);
1617 }
1618 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1619 }
1620 }
1621 return retv;
1622}
1623
1625 xcb_window_t wm_win = 0;
1626 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1628
1629 if (xcb_ewmh_get_supporting_wm_check_reply(&xcb->ewmh, cc, &wm_win, NULL)) {
1630 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1631 xcb_get_property_cookie_t cookie =
1632 xcb_ewmh_get_wm_name_unchecked(&(xcb->ewmh), wm_win);
1633 if (xcb_ewmh_get_wm_name_reply(&(xcb->ewmh), cookie, &wtitle, (void *)0)) {
1634 if (wtitle.strings_len > 0) {
1635 // Copy the string and add terminating '\0'.
1636 char *str = g_strndup(wtitle.strings, wtitle.strings_len);
1637 g_debug("Found window manager: |%s|", str);
1638 if (g_strcmp0(str, "i3") == 0) {
1641 }
1642 g_free(str);
1643 }
1644 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1645 }
1646 }
1647}
1648
1649static gboolean xcb_display_setup(GMainLoop *main_loop, NkBindings *bindings) {
1650 // Get DISPLAY, first env, then argument.
1651 // We never modify display_str content.
1652 char *display_str = (char *)g_getenv("DISPLAY");
1653 find_arg_str("-display", &display_str);
1654
1656#ifdef XCB_IMDKIT
1657 if (config.enable_imdkit) {
1658 xcb_compound_text_init();
1659 }
1660#endif
1661 xcb->source = g_water_xcb_source_new(g_main_loop_get_context(xcb->main_loop),
1662 display_str, &xcb->screen_nbr,
1663 main_loop_x11_event_handler, NULL, NULL);
1664 if (xcb->source == NULL) {
1665 g_warning("Failed to open display: %s", display_str);
1666 return FALSE;
1667 }
1668 xcb->connection = g_water_xcb_source_get_connection(xcb->source);
1669#ifdef XCB_IMDKIT
1670 if (config.enable_imdkit) {
1671 xcb->im = xcb_xim_create(xcb->connection, xcb->screen_nbr, NULL);
1672 xcb->syms = xcb_key_symbols_alloc(xcb->connection);
1673 } else {
1674 xcb->im = NULL;
1675 xcb->syms = NULL;
1676 }
1677#endif
1678
1679#ifdef XCB_IMDKIT
1680#ifndef XCB_IMDKIT_1_0_3_LOWER
1681 if (config.enable_imdkit) {
1682 xcb_xim_set_use_compound_text(xcb->im, true);
1683 xcb_xim_set_use_utf8_string(xcb->im, true);
1684 }
1685#endif
1686#endif
1687
1688 TICK_N("Open Display");
1689
1690 xcb->screen = xcb_aux_get_screen(xcb->connection, xcb->screen_nbr);
1691
1693
1694 xcb_intern_atom_cookie_t *ac =
1695 xcb_ewmh_init_atoms(xcb->connection, &xcb->ewmh);
1696 xcb_generic_error_t *errors = NULL;
1697 xcb_ewmh_init_atoms_replies(&xcb->ewmh, ac, &errors);
1698 if (errors) {
1699 g_warning("Failed to create EWMH atoms");
1700 free(errors);
1701 }
1702 // Discover the current active window manager.
1704 TICK_N("Setup XCB");
1705
1706 if (xkb_x11_setup_xkb_extension(
1707 xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION,
1708 XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
1709 NULL, NULL, &xcb->xkb.first_event, NULL) < 0) {
1710 g_warning("cannot setup XKB extension!");
1711 return FALSE;
1712 }
1713
1714 xcb->xkb.device_id = xkb_x11_get_core_keyboard_device_id(xcb->connection);
1715
1716 enum {
1717 required_events =
1718 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1719 XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
1720
1721 required_nkn_details = (XCB_XKB_NKN_DETAIL_KEYCODES),
1722
1723 required_map_parts =
1724 (XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
1725 XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1726 XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
1727 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
1728
1729 required_state_details =
1730 (XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
1731 XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
1732 XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK),
1733 };
1734
1735 static const xcb_xkb_select_events_details_t details = {
1736 .affectNewKeyboard = required_nkn_details,
1737 .newKeyboardDetails = required_nkn_details,
1738 .affectState = required_state_details,
1739 .stateDetails = required_state_details,
1740 };
1741 xcb_xkb_select_events(xcb->connection, xcb->xkb.device_id,
1742 required_events, /* affectWhich */
1743 0, /* clear */
1744 required_events, /* selectAll */
1745 required_map_parts, /* affectMap */
1746 required_map_parts, /* map */
1747 &details);
1748
1749 xcb->bindings_seat = nk_bindings_seat_new(bindings, XKB_CONTEXT_NO_FLAGS);
1750 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1751 nk_bindings_seat_get_context(xcb->bindings_seat), xcb->connection,
1752 xcb->xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS);
1753 if (keymap == NULL) {
1754 g_warning("Failed to get Keymap for current keyboard device.");
1755 return FALSE;
1756 }
1757 struct xkb_state *state = xkb_x11_state_new_from_device(
1758 keymap, xcb->connection, xcb->xkb.device_id);
1759 if (state == NULL) {
1760 g_warning("Failed to get state object for current keyboard device.");
1761 return FALSE;
1762 }
1763
1764 nk_bindings_seat_update_keymap(xcb->bindings_seat, keymap, state);
1765 xkb_state_unref(state);
1766 xkb_keymap_unref(keymap);
1767
1768 // determine numlock mask so we can bind on keys with and without it
1770
1771 if (xcb_connection_has_error(xcb->connection)) {
1772 g_warning("Connection has error");
1773 return FALSE;
1774 }
1775
1776 uint32_t val[] = {XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
1777 xcb_change_window_attributes(xcb->connection, xcb_stuff_get_root_window(),
1778 XCB_CW_EVENT_MASK, val);
1779 // startup not.
1780 xcb->sndisplay =
1781 sn_xcb_display_new(xcb->connection, error_trap_push, error_trap_pop);
1782 if (xcb_connection_has_error(xcb->connection)) {
1783 g_warning("Connection has error");
1784 return FALSE;
1785 }
1786
1787 if (xcb->sndisplay != NULL) {
1788 xcb->sncontext = sn_launchee_context_new_from_environment(xcb->sndisplay,
1789 xcb->screen_nbr);
1790 }
1791 if (xcb_connection_has_error(xcb->connection)) {
1792 g_warning("Connection has error");
1793 return FALSE;
1794 }
1795
1796 return TRUE;
1797}
1798
1800 xcb_depth_t *root_depth = NULL;
1801 xcb_depth_iterator_t depth_iter;
1802 for (depth_iter = xcb_screen_allowed_depths_iterator(xcb->screen);
1803 depth_iter.rem; xcb_depth_next(&depth_iter)) {
1804 xcb_depth_t *d = depth_iter.data;
1805
1806 xcb_visualtype_iterator_t visual_iter;
1807 for (visual_iter = xcb_depth_visuals_iterator(d); visual_iter.rem;
1808 xcb_visualtype_next(&visual_iter)) {
1809 xcb_visualtype_t *v = visual_iter.data;
1810 if ((v->bits_per_rgb_value == 8) && (d->depth == 32) &&
1811 (v->_class == XCB_VISUAL_CLASS_TRUE_COLOR)) {
1812 depth = d;
1813 visual = v;
1814 }
1815 if (xcb->screen->root_visual == v->visual_id) {
1816 root_depth = d;
1817 root_visual = v;
1818 }
1819 }
1820 }
1821 if (visual != NULL) {
1822 xcb_void_cookie_t c;
1823 xcb_generic_error_t *e;
1824 map = xcb_generate_id(xcb->connection);
1825 c = xcb_create_colormap_checked(xcb->connection, XCB_COLORMAP_ALLOC_NONE,
1826 map, xcb->screen->root, visual->visual_id);
1827 e = xcb_request_check(xcb->connection, c);
1828 if (e) {
1829 depth = NULL;
1830 visual = NULL;
1831 free(e);
1832 }
1833 }
1834
1835 if (visual == NULL) {
1836 depth = root_depth;
1838 map = xcb->screen->default_colormap;
1839 }
1840}
1841
1842static void x11_lookup_cursors(void) {
1843 xcb_cursor_context_t *ctx;
1844
1845 if (xcb_cursor_context_new(xcb->connection, xcb->screen, &ctx) < 0) {
1846 return;
1847 }
1848
1849 for (int i = 0; i < NUM_CURSORS; ++i) {
1850 cursors[i] = xcb_cursor_load_cursor(ctx, cursor_names[i].css_name);
1851
1852 if (cursors[i] == XCB_CURSOR_NONE) {
1853 cursors[i] =
1854 xcb_cursor_load_cursor(ctx, cursor_names[i].traditional_name);
1855 }
1856 }
1857
1858 xcb_cursor_context_free(ctx);
1859}
1860
1865static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data) {
1866 // After 5 sec.
1867 if (lazy_grab_retry_count_pt > (5 * 1000)) {
1868 g_warning("Failed to grab pointer after %u times. Giving up.",
1870 return G_SOURCE_REMOVE;
1871 }
1873 return G_SOURCE_REMOVE;
1874 }
1876 return G_SOURCE_CONTINUE;
1877}
1878static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data) {
1879 // After 5 sec.
1880 if (lazy_grab_retry_count_kb > (5 * 1000)) {
1881 g_warning("Failed to grab keyboard after %u times. Giving up.",
1883 g_main_loop_quit(xcb->main_loop);
1884 return G_SOURCE_REMOVE;
1885 }
1887 return G_SOURCE_REMOVE;
1888 }
1890 return G_SOURCE_CONTINUE;
1891}
1892
1893static gboolean xcb_display_late_setup(void) {
1895
1897
1901 // Try to grab the keyboard as early as possible.
1902 // We grab this using the rootwindow (as dmenu does it).
1903 // this seems to result in the smallest delay for most people.
1904 if (find_arg("-normal-window") >= 0 || find_arg("-transient-window") >= 0) {
1905 return TRUE;
1906 }
1907 if (find_arg("-no-lazy-grab") >= 0) {
1909 g_warning("Failed to grab keyboard, even after %d uS.", 500 * 1000);
1910 return FALSE;
1911 }
1913 g_warning("Failed to grab mouse pointer, even after %d uS.", 100 * 1000);
1914 }
1915 } else {
1917 g_timeout_add(1, lazy_grab_keyboard, NULL);
1918 }
1920 g_timeout_add(1, lazy_grab_pointer, NULL);
1921 }
1922 }
1923 return TRUE;
1924}
1925
1927
1928static void xcb_display_early_cleanup(void) {
1931 xcb_flush(xcb->connection);
1932}
1933
1934static void xcb_display_cleanup(void) {
1935 if (xcb->connection == NULL) {
1936 return;
1937 }
1938
1939 g_debug("Cleaning up XCB and XKB");
1940
1941 nk_bindings_seat_free(xcb->bindings_seat);
1942 if (xcb->sncontext != NULL) {
1943 sn_launchee_context_unref(xcb->sncontext);
1944 xcb->sncontext = NULL;
1945 }
1946 if (xcb->sndisplay != NULL) {
1947 sn_display_unref(xcb->sndisplay);
1948 xcb->sndisplay = NULL;
1949 }
1951 xcb_ewmh_connection_wipe(&(xcb->ewmh));
1952 xcb_flush(xcb->connection);
1953 xcb_aux_sync(xcb->connection);
1954#ifdef XCB_IMDKIT
1955 if (config.enable_imdkit) {
1956 xcb_xim_close(xcb->im);
1957 xcb_xim_destroy(xcb->im);
1958 xcb->im = NULL;
1959 }
1960#endif
1961 g_water_xcb_source_free(xcb->source);
1962 xcb->source = NULL;
1963 xcb->connection = NULL;
1964 xcb->screen = NULL;
1965 xcb->screen_nbr = 0;
1966}
1967
1969 // Flag used to indicate we are setting the decoration type.
1970 const uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
1971 // Motif property data structure
1972 struct MotifWMHints {
1973 uint32_t flags;
1974 uint32_t functions;
1975 uint32_t decorations;
1976 int32_t inputMode;
1977 uint32_t state;
1978 };
1979
1980 struct MotifWMHints hints;
1981 hints.flags = MWM_HINTS_DECORATIONS;
1982 hints.decorations = 0;
1983 hints.functions = 0;
1984 hints.inputMode = 0;
1985 hints.state = 0;
1986
1987 xcb_atom_t ha = netatoms[_MOTIF_WM_HINTS];
1988 xcb_change_property(xcb->connection, XCB_PROP_MODE_REPLACE, window, ha, ha,
1989 32, 5, &hints);
1990}
1991
1993 if (type < 0 || type >= NUM_CURSORS) {
1994 return;
1995 }
1996
1997 if (cursors[type] == XCB_CURSOR_NONE) {
1998 return;
1999 }
2000
2001 xcb_change_window_attributes(xcb->connection, window, XCB_CW_CURSOR,
2002 &(cursors[type]));
2003}
2004void xcb_stuff_set_clipboard(char *data) {
2005 g_free(xcb->clipboard);
2006 xcb->clipboard = data;
2007}
2008
2009static void xcb_display_set_input_focus(guint w) {
2010 if (config.steal_focus != TRUE) {
2011 xcb->focus_revert = 0;
2012 return;
2013 }
2014 xcb_generic_error_t *error;
2015 xcb_get_input_focus_reply_t *freply;
2016 xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus(xcb->connection);
2017 freply = xcb_get_input_focus_reply(xcb->connection, fcookie, &error);
2018 if (error != NULL) {
2019 g_warning("Could not get input focus (error %d), will revert focus to best "
2020 "effort",
2021 error->error_code);
2022 free(error);
2023 xcb->focus_revert = 0;
2024 } else {
2025 xcb->focus_revert = freply->focus;
2026 }
2027 xcb_set_input_focus(xcb->connection, XCB_INPUT_FOCUS_POINTER_ROOT, w,
2028 XCB_CURRENT_TIME);
2029 xcb_flush(xcb->connection);
2030}
2031
2033 if (xcb->focus_revert == 0) {
2034 return;
2035 }
2036
2037 xcb_set_input_focus(xcb->connection, XCB_INPUT_FOCUS_POINTER_ROOT,
2038 xcb->focus_revert, XCB_CURRENT_TIME);
2039 xcb_flush(xcb->connection);
2040}
2041
2042static guint xcb_display_scale(void) { return 1; }
2043
2044static const struct _view_proxy *xcb_display_view_proxy(void) {
2045 return xcb_view_proxy;
2046}
2047
2049 .setup = xcb_display_setup,
2050 .late_setup = xcb_display_late_setup,
2051 .early_cleanup = xcb_display_early_cleanup,
2052 .cleanup = xcb_display_cleanup,
2053 .dump_monitor_layout = xcb_display_dump_monitor_layout,
2054 .startup_notification = xcb_display_startup_notification,
2055 .monitor_active = xcb_display_monitor_active,
2056 .set_input_focus = xcb_display_set_input_focus,
2057 .revert_input_focus = xcb_display_revert_input_focus,
2058 .scale = xcb_display_scale,
2059
2060 .view = xcb_display_view_proxy,
2061};
2062
struct _display_proxy display_proxy
struct _workarea workarea
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
Definition helper.c:856
#define rofi_fallthrough
Definition helper.h:540
int find_arg_str(const char *const key, char **val)
Definition helper.c:334
int find_arg(const char *const key)
Definition helper.c:325
char * rofi_force_utf8(const gchar *data, ssize_t length)
Definition helper.c:862
#define color_reset
Definition rofi.h:123
#define color_bold
Definition rofi.h:125
#define TICK()
Definition timings.h:64
#define TICK_N(a)
Definition timings.h:69
xcb_window_t rofi_view_get_window(void)
Definition view.c:2227
RofiViewState * rofi_view_get_active(void)
Definition view.c:321
void rofi_view_handle_text(RofiViewState *state, const char *text)
Definition view.c:1463
void rofi_view_handle_mouse_motion(RofiViewState *state, gint x, gint y, gboolean find_mouse_target)
Definition view.c:1500
void rofi_view_temp_click_to_exit(RofiViewState *state, xcb_window_t target)
Definition view.c:2187
void rofi_view_temp_configure_notify(RofiViewState *state, xcb_configure_notify_event_t *xce)
Definition view.c:2182
void rofi_view_frame_callback(void)
Definition view.c:2191
void rofi_view_maybe_update(RofiViewState *state)
Definition view.c:1560
const view_proxy * xcb_view_proxy
Definition view.c:1003
NkBindings * bindings
Definition rofi.c:140
GMainLoop * main_loop
Definition rofi.c:146
Settings config
const gchar * binary
Definition helper.h:336
const gchar * wmclass
Definition helper.h:344
const gchar * app_id
Definition helper.h:342
const gchar * description
Definition helper.h:338
const gchar * name
Definition helper.h:334
const gchar * icon
Definition helper.h:340
const gchar * command
Definition helper.h:346
gboolean xserver_i300_workaround
Definition settings.h:221
gboolean steal_focus
Definition settings.h:212
int click_to_exit
Definition settings.h:178
gboolean enable_imdkit
Definition settings.h:225
gboolean hover_select
Definition settings.h:152
char * monitor
Definition settings.h:167
const char * css_name
const char * traditional_name
int w
Definition display.h:48
int x
Definition display.h:44
int monitor_id
Definition display.h:40
char * name
Definition display.h:53
int mh
Definition display.h:51
struct _workarea * next
Definition display.h:55
int h
Definition display.h:50
int mw
Definition display.h:51
int primary
Definition display.h:42
int y
Definition display.h:46
GWaterXcbSource * source
xcb_connection_t * connection
SnLauncheeContext * sncontext
struct _workarea * monitors
uint32_t mouse_seen
xcb_timestamp_t last_timestamp
xcb_ewmh_connection_t ewmh
xcb_screen_t * screen
NkBindingsSeat * bindings_seat
struct _xcb_stuff::@124022330146077150033143244062260205253000356231 xkb
GMainLoop * main_loop
char * clipboard
xcb_window_t focus_revert
SnDisplay * sndisplay
static display_proxy display_
Definition display.c:2224
int xcb_timestamp_t
Definition xcb-dummy.h:10
int xcb_window_t
Definition xcb-dummy.h:9
int xcb_configure_notify_event_t
Definition xcb-dummy.h:8
xcb_colormap_t map
Definition display.c:110
display_proxy *const xcb_proxy
Definition display.c:2063
static int xcb_display_monitor_active(workarea *mon)
Definition display.c:1016
static int take_pointer(xcb_window_t w, int iters)
Definition display.c:1505
#define RANDR_PREF_MAJOR_VERSION
Definition display.c:79
static int x11_is_extension_present(const char *extension)
Definition display.c:559
static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data)
Definition display.c:1865
static void x11_workarea_fix_rotation(workarea *w)
Definition display.c:454
cairo_surface_t * x11_helper_get_screenshot_surface(void)
Definition display.c:357
static void x11_build_monitor_layout(void)
Definition display.c:604
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
Definition display.c:397
static gboolean xcb_display_setup(GMainLoop *main_loop, NkBindings *bindings)
Definition display.c:1649
xcb_stuff * xcb
Definition display.c:103
static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen, xcb_atom_t atom)
Definition display.c:363
static void release_pointer(void)
Definition display.c:1565
static void xcb_display_set_input_focus(guint w)
Definition display.c:2009
static bool get_atom_name(xcb_connection_t *conn, xcb_atom_t atom, char **out)
Definition display.c:1087
struct _xcb_stuff xcb_int
Definition display.c:92
xcb_depth_t * depth
Definition display.c:108
static int monitor_active_from_winid(xcb_drawable_t id, workarea *mon)
Definition display.c:830
static void x11_create_visual_and_colormap(void)
Definition display.c:1799
static gboolean x11_button_to_nk_bindings_scroll(guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps)
Definition display.c:1180
static void monitor_dimensions(int x, int y, workarea *mon)
Definition display.c:788
cairo_surface_t * x11_helper_get_bg_surface(void)
Definition display.c:383
static void x11_lookup_cursors(void)
Definition display.c:1842
static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data)
Definition display.c:1878
#define INTERSECT(x, y, x1, y1, w1, h1)
Definition display.c:84
static workarea * x11_get_monitor_from_output(xcb_randr_output_t out)
Definition display.c:469
static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay)
Definition display.c:1576
void x11_set_cursor(xcb_window_t window, X11CursorType type)
Definition display.c:1992
workarea mon_cache
Definition display.c:1013
static void x11_build_monitor_layout_xinerama(void)
Definition display.c:573
static xcb_visualtype_t * lookup_visual(xcb_screen_t *s, xcb_visualid_t vis)
Definition display.c:133
unsigned int lazy_grab_retry_count_pt
Definition display.c:1864
static xcb_visualtype_t * root_visual
Definition display.c:114
static uint32_t * create_kernel(int radius, double deviation, uint32_t *sum2)
Definition display.c:151
char * x11_helper_get_window_manager(void)
Definition display.c:1604
static void x11_create_frequently_used_atoms(void)
Definition display.c:1590
static int monitor_active_from_id_focused(int mon_id, workarea *mon)
Definition display.c:860
void cairo_image_surface_blur(cairo_surface_t *surface, int radius, double deviation)
Definition display.c:178
static gboolean x11_button_to_nk_bindings_button(guint32 x11_button, NkBindingsMouseButton *button)
Definition display.c:1151
xcb_cursor_t cursors[NUM_CURSORS]
Definition display.c:121
static void x11_monitor_free(workarea *m)
Definition display.c:436
static void x11_helper_discover_window_manager(void)
Definition display.c:1624
static const struct _view_proxy * xcb_display_view_proxy(void)
Definition display.c:2044
static int monitor_get_dimension(int monitor_id, workarea *mon)
Definition display.c:773
static void xcb_display_dump_monitor_layout(void)
Definition display.c:705
static int take_keyboard(xcb_window_t w, int iters)
Definition display.c:1534
xcb_window_t xcb_stuff_get_root_window(void)
Definition display.c:1926
gboolean mon_set
Definition display.c:1011
unsigned int lazy_grab_retry_count_kb
Definition display.c:1862
static void rofi_view_paste(RofiViewState *state, xcb_selection_notify_event_t *xse)
Definition display.c:1122
static gboolean xcb_display_late_setup(void)
Definition display.c:1893
void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count)
Definition display.c:423
static void xcb_display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
Definition display.c:731
const char * netatom_names[]
Definition display.c:116
static int monitor_active_from_id(int mon_id, workarea *mon)
Definition display.c:931
static void x11_monitors_free(void)
Definition display.c:441
static guint xcb_display_scale(void)
Definition display.c:2042
const struct @106130035057033371167230156075074055250362361056 cursor_names[]
static void error_trap_push(G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay)
Definition display.c:1571
WindowManagerQuirk current_window_manager
Definition display.c:87
static int pointer_get(xcb_window_t root, int *x, int *y)
Definition display.c:815
#define RANDR_PREF_MINOR_VERSION
Definition display.c:81
#define sn_launcher_context_set_application_id
Definition display.c:61
static void rofi_key_press_event_handler(xcb_key_press_event_t *xkpe, RofiViewState *state)
Definition display.c:1203
static void rofi_key_release_event_handler(xcb_key_release_event_t *xkre, G_GNUC_UNUSED RofiViewState *state)
Definition display.c:1223
void xcb_stuff_set_clipboard(char *data)
Definition display.c:2004
static int error_trap_depth
Definition display.c:1570
static void release_keyboard(void)
Definition display.c:1562
static void xcb_display_cleanup(void)
Definition display.c:1934
static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data)
Definition display.c:1450
static void xcb_display_early_cleanup(void)
Definition display.c:1928
void x11_disable_decoration(xcb_window_t window)
Definition display.c:1968
static void main_loop_x11_event_handler_view(xcb_generic_event_t *event)
Definition display.c:1234
xcb_atom_t netatoms[NUM_NETATOMS]
Definition display.c:115
xcb_visualtype_t * visual
Definition display.c:109
static void xcb_display_revert_input_focus(void)
Definition display.c:2032
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
Definition display.c:296
struct _xcb_stuff xcb_stuff
Definition xcb.h:40
#define EWMH_ATOMS(X)
Definition xcb.h:89
#define ATOM_CHAR(x)
Definition xcb.h:86
@ NUM_NETATOMS
Definition xcb.h:95
WindowManagerQuirk
Definition xcb.h:161
@ WM_PANGO_WORKSPACE_NAMES
Definition xcb.h:167
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP
Definition xcb.h:165
@ WM_EWHM
Definition xcb.h:163
X11CursorType
Definition xcb.h:140
@ NUM_CURSORS
Definition xcb.h:147