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