30 #define G_LOG_DOMAIN "X11Helper"
33 #include <cairo-xcb.h>
42 #include <xcb/randr.h>
44 #include <xcb/xcb_aux.h>
45 #include <xcb/xcb_cursor.h>
46 #include <xcb/xcb_ewmh.h>
47 #include <xcb/xinerama.h>
49 #include <xcb/xproto.h>
50 #include <xkbcommon/xkbcommon-x11.h>
51 #include <xkbcommon/xkbcommon.h>
53 #define SN_API_NOT_YET_FROZEN
56 #define sn_launcher_context_set_application_id sn_launcher_set_application_id
72 #define RANDR_PREF_MAJOR_VERSION 1
74 #define RANDR_PREF_MINOR_VERSION 5
77 #define INTERSECT(x, y, x1, y1, w1, h1) \
78 ((((x) >= (x1)) && ((x) < (x1 + w1))) && (((y) >= (y1)) && ((y) < (y1 + h1))))
98 xcb_colormap_t
map = XCB_COLORMAP_NONE;
119 {
"default",
"left_ptr"}, {
"pointer",
"hand"}, {
"text",
"xterm"}};
122 xcb_depth_iterator_t d;
123 d = xcb_screen_allowed_depths_iterator(s);
124 for (; d.rem; xcb_depth_next(&d)) {
125 xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator(d.data);
126 for (; v.rem; xcb_visualtype_next(&v)) {
127 if (v.data->visual_id ==
visual) {
141 int size = 2 * (int)(radius) + 1;
142 uint32_t *kernel = (uint32_t *)(g_malloc(
sizeof(uint32_t) * (size + 1)));
143 double radiusf = fabs(radius) + 1.0;
144 double value = -radius;
148 if (deviation == 0.0) {
149 deviation = sqrt(-(radiusf * radiusf) / (2.0 * log(1.0 / 255.0)));
154 for (i = 0; i < size; i++) {
155 kernel[1 + i] = INT16_MAX / (2.506628275 * deviation) *
156 exp(-((value * value) / (2.0 * (deviation * deviation))));
158 sum += kernel[1 + i];
170 uint32_t *kernel = 0;
171 cairo_format_t format;
172 unsigned int channels;
174 if (cairo_surface_status(surface)) {
178 uint8_t *data = cairo_image_surface_get_data(surface);
179 format = cairo_image_surface_get_format(surface);
180 const int width = cairo_image_surface_get_width(surface);
181 const int height = cairo_image_surface_get_height(surface);
182 const int stride = cairo_image_surface_get_stride(surface);
184 if (format == CAIRO_FORMAT_ARGB32) {
190 horzBlur = (uint32_t *)(g_malloc(
sizeof(uint32_t) * height * stride));
197 uint32_t *horzBlur_ptr = horzBlur;
198 for (
int iY = 0; iY < height; iY++) {
199 const int iYs = iY * stride;
200 for (
int iX = 0; iX < width; iX++) {
205 int offset = (int)(kernel[0]) / -2;
207 for (
int i = 0; i < (int)(kernel[0]); i++) {
210 if (x < 0 || x >= width) {
215 uint8_t *dataPtr = &data[iYs + x * channels];
216 const uint32_t kernip1 = kernel[i + 1];
218 blue += kernip1 * dataPtr[0];
219 green += kernip1 * dataPtr[1];
220 red += kernip1 * dataPtr[2];
221 alpha += kernip1 * dataPtr[3];
225 *horzBlur_ptr++ = blue / sum;
226 *horzBlur_ptr++ = green / sum;
227 *horzBlur_ptr++ = red / sum;
228 *horzBlur_ptr++ = alpha / sum;
234 for (
int iY = 0; iY < height; iY++) {
235 for (
int iX = 0; iX < width; iX++) {
240 int offset = (int)(kernel[0]) / -2;
242 const int iXs = iX * channels;
243 for (
int i = 0; i < (int)(kernel[0]); i++) {
246 if (y < 0 || y >= height) {
251 uint32_t *dataPtr = &horzBlur[y * stride + iXs];
252 const uint32_t kernip1 = kernel[i + 1];
254 blue += kernip1 * dataPtr[0];
255 green += kernip1 * dataPtr[1];
256 red += kernip1 * dataPtr[2];
257 alpha += kernip1 * dataPtr[3];
262 *data++ = blue / sum;
263 *data++ = green / sum;
265 *data++ = alpha / sum;
278 xcb_get_geometry_cookie_t cookie;
279 xcb_get_geometry_reply_t *reply;
282 reply = xcb_get_geometry_reply(
xcb->
connection, cookie, NULL);
287 xcb_get_window_attributes_cookie_t attributesCookie =
289 xcb_get_window_attributes_reply_t *attributes =
290 xcb_get_window_attributes_reply(
xcb->
connection, attributesCookie, NULL);
291 if (attributes == NULL || (attributes->map_state != XCB_MAP_STATE_VIEWABLE)) {
302 cairo_surface_t *t = cairo_xcb_surface_create(
xcb->
connection, window, vt,
303 reply->width, reply->height);
305 if (cairo_surface_status(t) != CAIRO_STATUS_SUCCESS) {
306 cairo_surface_destroy(t);
312 int max = MAX(reply->width, reply->height);
313 double scale = (double)size / max;
315 cairo_surface_t *s2 = cairo_surface_create_similar_image(
316 t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale);
319 if (cairo_surface_status(s2) != CAIRO_STATUS_SUCCESS) {
320 cairo_surface_destroy(t);
324 cairo_t *d = cairo_create(s2);
325 cairo_scale(d, scale, scale);
326 cairo_set_source_surface(d, t, 0, 0);
330 cairo_surface_destroy(t);
345 xcb_get_property_cookie_t cookie;
346 xcb_get_property_reply_t *reply;
347 xcb_pixmap_t rootpixmap = XCB_NONE;
349 cookie = xcb_get_property(c, 0, screen->root, atom, XCB_ATOM_PIXMAP, 0, 1);
351 reply = xcb_get_property_reply(c, cookie, NULL);
354 if (xcb_get_property_value_length(reply) ==
sizeof(xcb_pixmap_t)) {
355 memcpy(&rootpixmap, xcb_get_property_value(reply),
sizeof(xcb_pixmap_t));
366 if (pm == XCB_NONE) {
378 xcb_get_property_cookie_t c = xcb_get_property(
379 xcb->
connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
380 xcb_get_property_reply_t *r =
383 if (xcb_get_property_value_length(r) > 0) {
385 if (r->type ==
netatoms[UTF8_STRING]) {
386 str = g_strndup(xcb_get_property_value(r),
387 xcb_get_property_value_length(r));
388 }
else if (r->type ==
netatoms[STRING]) {
390 xcb_get_property_value_length(r));
392 str = g_strdup(
"Invalid encoding.");
405 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, w, prop,
406 XCB_ATOM_ATOM, 32,
count, atoms);
433 xcb_randr_get_output_info_reply_t *op_reply;
434 xcb_randr_get_crtc_info_reply_t *crtc_reply;
435 xcb_randr_get_output_info_cookie_t it =
436 xcb_randr_get_output_info(
xcb->
connection, out, XCB_CURRENT_TIME);
437 op_reply = xcb_randr_get_output_info_reply(
xcb->
connection, it, NULL);
438 if (op_reply->crtc == XCB_NONE) {
442 xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info(
444 crtc_reply = xcb_randr_get_crtc_info_reply(
xcb->
connection, ct, NULL);
450 retv->
x = crtc_reply->x;
451 retv->
y = crtc_reply->y;
452 retv->
w = crtc_reply->width;
453 retv->
h = crtc_reply->height;
455 retv->
mw = op_reply->mm_width;
456 retv->
mh = op_reply->mm_height;
458 char *tname = (
char *)xcb_randr_get_output_info_name(op_reply);
459 int tname_len = xcb_randr_get_output_info_name_length(op_reply);
461 retv->
name = g_malloc0((tname_len + 1) *
sizeof(
char));
462 memcpy(retv->
name, tname, tname_len);
469 #if (((XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION) && \
470 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
471 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
480 x11_get_monitor_from_randr_monitor(xcb_randr_monitor_info_t *
mon) {
482 xcb_generic_error_t *err;
483 xcb_get_atom_name_cookie_t anc =
485 xcb_get_atom_name_reply_t *atom_reply =
488 g_warning(
"Could not get RandR monitor name: X11 error code %d\n",
501 retv->
w =
mon->width;
502 retv->
h =
mon->height;
505 retv->
mw =
mon->width_in_millimeters;
506 retv->
mh =
mon->height_in_millimeters;
510 g_strdup_printf(
"%.*s", xcb_get_atom_name_name_length(atom_reply),
511 xcb_get_atom_name_name(atom_reply));
521 xcb_query_extension_cookie_t randr_cookie =
522 xcb_query_extension(
xcb->
connection, strlen(extension), extension);
524 xcb_query_extension_reply_t *randr_reply =
525 xcb_query_extension_reply(
xcb->
connection, randr_cookie, NULL);
527 int present = randr_reply->present;
535 xcb_xinerama_query_screens_cookie_t screens_cookie =
538 xcb_xinerama_query_screens_reply_t *screens_reply =
539 xcb_xinerama_query_screens_reply(
xcb->
connection, screens_cookie, NULL);
541 xcb_xinerama_screen_info_iterator_t screens_iterator =
542 xcb_xinerama_query_screens_screen_info_iterator(screens_reply);
544 for (; screens_iterator.rem > 0;
545 xcb_xinerama_screen_info_next(&screens_iterator)) {
548 w->
x = screens_iterator.data->x_org;
549 w->
y = screens_iterator.data->y_org;
550 w->
w = screens_iterator.data->width;
551 w->
h = screens_iterator.data->height;
573 g_debug(
"Query XINERAMA for monitor layout.");
577 g_debug(
"No RANDR or Xinerama available for getting monitor layout.");
580 g_debug(
"Query RANDR for monitor layout.");
582 g_debug(
"Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION,
583 XCB_RANDR_MINOR_VERSION);
584 #if (((XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION) && \
585 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
586 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
587 xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version(
589 xcb_randr_query_version_reply_t *rversion =
590 xcb_randr_query_version_reply(
xcb->
connection, cversion, NULL);
592 g_debug(
"Found randr version: %d.%d", rversion->major_version,
593 rversion->minor_version);
598 xcb_randr_get_monitors_cookie_t t =
600 xcb_randr_get_monitors_reply_t *mreply =
603 xcb_randr_monitor_info_iterator_t iter =
604 xcb_randr_get_monitors_monitors_iterator(mreply);
605 while (iter.rem > 0) {
606 workarea *w = x11_get_monitor_from_randr_monitor(iter.data);
611 xcb_randr_monitor_info_next(&iter);
622 xcb_randr_get_screen_resources_current_reply_t *res_reply;
623 xcb_randr_get_screen_resources_current_cookie_t src;
624 src = xcb_randr_get_screen_resources_current(
xcb->
connection,
626 res_reply = xcb_randr_get_screen_resources_current_reply(
xcb->
connection,
632 xcb_randr_get_screen_resources_current_outputs_length(res_reply);
633 xcb_randr_output_t *ops =
634 xcb_randr_get_screen_resources_current_outputs(res_reply);
637 xcb_randr_get_output_primary_cookie_t pc =
639 xcb_randr_get_output_primary_reply_t *pc_rep =
640 xcb_randr_get_output_primary_reply(
xcb->
connection, pc, NULL);
642 for (
int i = mon_num - 1; i >= 0; i--) {
647 if (pc_rep && pc_rep->output == ops[i]) {
662 iter->monitor_id = index++;
667 int is_term = isatty(fileno(stdout));
668 printf(
"Monitor layout:\n");
670 printf(
"%s ID%s: %d", (is_term) ?
color_bold :
"",
673 printf(
" (primary)");
676 printf(
"%s name%s: %s\n", (is_term) ?
color_bold :
"",
678 printf(
"%s position%s: %d,%d\n", (is_term) ?
color_bold :
"",
680 printf(
"%s size%s: %d,%d\n", (is_term) ?
color_bold :
"",
682 if (iter->mw > 0 && iter->mh > 0) {
683 printf(
"%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
685 iter->mh, iter->w * 25.4 / (
double)iter->mw,
686 iter->h * 25.4 / (
double)iter->mh);
693 GSpawnChildSetupFunc *child_setup,
694 gpointer *user_data) {
695 if (context == NULL) {
699 SnLauncherContext *sncontext;
703 sn_launcher_context_set_name(sncontext, context->
name);
704 sn_launcher_context_set_description(sncontext, context->
description);
705 if (context->
binary != NULL) {
706 sn_launcher_context_set_binary_name(sncontext, context->
binary);
708 if (context->
icon != NULL) {
709 sn_launcher_context_set_icon_name(sncontext, context->
icon);
711 if (context->
app_id != NULL) {
714 if (context->
wmclass != NULL) {
715 sn_launcher_context_set_wmclass(sncontext, context->
wmclass);
718 xcb_get_property_cookie_t c;
719 unsigned int current_desktop = 0;
722 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
724 sn_launcher_context_set_workspace(sncontext, current_desktop);
727 sn_launcher_context_initiate(sncontext,
"rofi", context->
command,
730 *child_setup = (GSpawnChildSetupFunc)sn_launcher_context_setup_child_process;
731 *user_data = sncontext;
751 g_error(
"%s: mon == NULL", __FUNCTION__);
759 if (
INTERSECT(x, y, iter->x, iter->y, iter->w, iter->h)) {
779 xcb_query_pointer_cookie_t c = xcb_query_pointer(
xcb->
connection, root);
780 xcb_query_pointer_reply_t *r =
794 g_error(
"%s: mon == NULL", __FUNCTION__);
798 xcb_get_geometry_cookie_t c = xcb_get_geometry(
xcb->
connection,
id);
799 xcb_get_geometry_reply_t *r =
802 xcb_translate_coordinates_cookie_t ct =
803 xcb_translate_coordinates(
xcb->
connection,
id, root, r->x, r->y);
804 xcb_translate_coordinates_reply_t *t =
810 mon->
x = t->dst_x - r->x;
811 mon->
y = t->dst_y - r->y;
824 xcb_window_t active_window;
825 xcb_get_property_cookie_t awc;
827 g_error(
"%s: mon == NULL", __FUNCTION__);
831 if (!xcb_ewmh_get_active_window_reply(&
xcb->
ewmh, awc, &active_window,
834 "Failed to get active window, falling back to mouse location (-5).");
837 xcb_query_tree_cookie_t tree_cookie =
839 xcb_query_tree_reply_t *tree_reply =
843 "Failed to get parent window, falling back to mouse location (-5).");
847 xcb_get_geometry_cookie_t c =
849 xcb_get_geometry_reply_t *r =
852 g_debug(
"Failed to get geometry of active window, falling back to mouse "
857 xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates(
859 xcb_translate_coordinates_reply_t *t =
866 mon->
x = t->dst_x - r->x;
867 mon->
y = t->dst_y - r->y;
877 }
else if (mon_id == -4) {
878 g_debug(
"Find monitor at location: %d %d", t->dst_x, t->dst_y);
885 g_debug(
"Failed to get translate position of active window, falling back "
886 "to mouse location (-5).");
896 g_error(
"%s: mon == NULL", __FUNCTION__);
899 g_debug(
"Monitor id: %d", mon_id);
910 else if (mon_id == -1) {
911 g_debug(
"rofi on current monitor");
913 unsigned int current_desktop = 0;
914 xcb_get_property_cookie_t gcdc;
916 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, gcdc, ¤t_desktop,
918 g_debug(
"Found current desktop: %u", current_desktop);
919 xcb_get_property_cookie_t c =
921 xcb_ewmh_get_desktop_viewport_reply_t vp;
922 if (xcb_ewmh_get_desktop_viewport_reply(&
xcb->
ewmh, c, &vp, NULL)) {
923 g_debug(
"Found %d number of desktops", vp.desktop_viewport_len);
924 if (current_desktop < vp.desktop_viewport_len) {
925 g_debug(
"Found viewport for desktop: %d %d",
926 vp.desktop_viewport[current_desktop].x,
927 vp.desktop_viewport[current_desktop].y);
929 vp.desktop_viewport[current_desktop].y,
mon);
930 g_debug(
"Found monitor @: %d %d %dx%d",
mon->
x,
mon->
y,
mon->
w,
932 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
935 g_debug(
"Viewport does not exist for current desktop: %d, falling "
936 "back to mouse location (-5)",
938 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
940 g_debug(
"Failed to get viewport for current desktop: %d, falling back "
941 "to mouse location (-5).",
945 g_debug(
"Failed to get current desktop, falling back to mouse location "
948 }
else if (mon_id == -2 || mon_id == -4) {
954 else if (mon_id == -5) {
962 g_debug(
"Failed to find monitor, fall back to monitor showing mouse.");
975 g_error(
"%s: mon == NULL", __FUNCTION__);
978 g_debug(
"Monitor active");
996 g_debug(
"Monitor lookup by name failed: %s",
config.
monitor);
1000 if (iter->primary) {
1010 xcb_drawable_t win = g_ascii_strtoll(
config.
monitor + 4, &end, 0);
1030 g_warning(
"Failed to find selected monitor.");
1053 xcb_selection_notify_event_t *xse) {
1054 if (xse->property == XCB_ATOM_NONE) {
1055 g_warning(
"Failed to convert selection");
1056 }
else if (xse->property ==
xcb->
ewmh.UTF8_STRING) {
1058 if (text != NULL && text[0] !=
'\0') {
1059 unsigned int dl = strlen(text);
1061 for (
unsigned int i = 0; i < dl; i++) {
1062 if (text[i] ==
'\n') {
1070 g_warning(
"Failed");
1076 NkBindingsMouseButton *button) {
1077 switch (x11_button) {
1079 *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
1082 *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
1085 *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
1088 *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
1091 *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
1099 *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
1105 NkBindingsScrollAxis *axis,
1108 switch (x11_button) {
1113 *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
1119 *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
1132 if (state == NULL) {
1136 switch (event->response_type & ~0x80) {
1137 case XCB_CLIENT_MESSAGE: {
1138 xcb_client_message_event_t *cme = (xcb_client_message_event_t *)event;
1139 xcb_atom_t atom = cme->data.data32[0];
1140 xcb_timestamp_t
time = cme->data.data32[1];
1141 if (atom ==
netatoms[WM_TAKE_FOCUS]) {
1142 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_NONE, cme->window,
1148 case XCB_DESTROY_NOTIFY: {
1149 xcb_window_t win = ((xcb_destroy_notify_event_t *)event)->window;
1152 window_client_handle_signal(win, FALSE);
1159 case XCB_CREATE_NOTIFY: {
1160 xcb_window_t win = ((xcb_create_notify_event_t *)event)->window;
1163 window_client_handle_signal(win, TRUE);
1171 case XCB_CONFIGURE_NOTIFY: {
1172 xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *)event;
1176 case XCB_MOTION_NOTIFY: {
1177 xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *)event;
1178 gboolean button_mask = xme->state & XCB_EVENT_MASK_BUTTON_1_MOTION;
1186 case XCB_BUTTON_PRESS: {
1187 xcb_button_press_event_t *bpe = (xcb_button_press_event_t *)event;
1188 NkBindingsMouseButton button;
1189 NkBindingsScrollAxis axis;
1196 NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time);
1202 case XCB_BUTTON_RELEASE: {
1203 xcb_button_release_event_t *bre = (xcb_button_release_event_t *)event;
1204 NkBindingsMouseButton button;
1209 NK_BINDINGS_BUTTON_STATE_RELEASE,
1221 case XCB_SELECTION_NOTIFY:
1224 case XCB_KEYMAP_NOTIFY: {
1225 xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *)event;
1226 for (gint32 by = 0; by < 31; ++by) {
1227 for (gint8 bi = 0; bi < 7; ++bi) {
1228 if (kne->keys[by] & (1 << bi)) {
1232 NK_BINDINGS_KEY_STATE_PRESSED);
1238 case XCB_KEY_PRESS: {
1239 xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *)event;
1243 text = nk_bindings_seat_handle_key_with_modmask(
1245 NK_BINDINGS_KEY_STATE_PRESS);
1252 case XCB_KEY_RELEASE: {
1253 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1256 NK_BINDINGS_KEY_STATE_RELEASE);
1266 G_GNUC_UNUSED gpointer user_data) {
1270 g_warning(
"The XCB connection to X server had a fatal error: %d", status);
1272 return G_SOURCE_REMOVE;
1274 g_warning(
"main_loop_x11_event_handler: ev == NULL, status == %d", status);
1275 return G_SOURCE_CONTINUE;
1277 uint8_t type = ev->response_type & ~0x80;
1280 case XCB_XKB_MAP_NOTIFY: {
1281 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1284 struct xkb_state *state = xkb_x11_state_new_from_device(
1287 xkb_keymap_unref(keymap);
1288 xkb_state_unref(state);
1291 case XCB_XKB_STATE_NOTIFY: {
1292 xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *)ev;
1294 ksne->latchedMods, ksne->lockedMods,
1295 ksne->baseGroup, ksne->latchedGroup,
1301 return G_SOURCE_CONTINUE;
1307 return G_SOURCE_CONTINUE;
1315 xcb_generic_error_t *error;
1316 xcb_get_input_focus_reply_t *freply;
1317 xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus(
xcb->
connection);
1318 freply = xcb_get_input_focus_reply(
xcb->
connection, fcookie, &error);
1319 if (error != NULL) {
1320 g_warning(
"Could not get input focus (error %d), will revert focus to best "
1328 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT, w,
1338 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT,
1347 g_warning(
"Connection has error");
1350 xcb_grab_pointer_cookie_t cc =
1351 xcb_grab_pointer(
xcb->
connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
1352 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE,
1354 xcb_grab_pointer_reply_t *r =
1357 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1363 if ((++i) > iters) {
1366 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1367 nanosleep(&del, NULL);
1376 g_warning(
"Connection has error");
1379 xcb_grab_keyboard_cookie_t cc =
1381 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1382 xcb_grab_keyboard_reply_t *r =
1385 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1391 if ((++i) > iters) {
1394 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1395 nanosleep(&del, NULL);
1410 G_GNUC_UNUSED xcb_connection_t *xdisplay) {
1415 xcb_connection_t *xdisplay) {
1417 g_warning(
"Error trap underflow!");
1421 xcb_flush(xdisplay);
1431 xcb_intern_atom_cookie_t cc = xcb_intern_atom(
1433 xcb_intern_atom_reply_t *r =
1443 xcb_window_t wm_win = 0;
1444 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1447 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1448 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1449 xcb_get_property_cookie_t cookie =
1450 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1451 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1452 if (wtitle.strings_len > 0) {
1453 g_debug(
"Found window manager: |%s|", wtitle.strings);
1454 if (g_strcmp0(wtitle.strings,
"i3") == 0) {
1457 }
else if (g_strcmp0(wtitle.strings,
"bspwm") == 0) {
1461 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1469 char *display_str = (
char *)g_getenv(
"DISPLAY");
1477 g_warning(
"Failed to open display: %s", display_str);
1488 xcb_intern_atom_cookie_t *ac =
1490 xcb_generic_error_t *errors = NULL;
1491 xcb_ewmh_init_atoms_replies(&
xcb->
ewmh, ac, &errors);
1493 g_warning(
"Failed to create EWMH atoms");
1500 if (xkb_x11_setup_xkb_extension(
1502 XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
1504 g_warning(
"cannot setup XKB extension!");
1512 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1513 XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
1515 required_nkn_details = (XCB_XKB_NKN_DETAIL_KEYCODES),
1517 required_map_parts =
1518 (XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
1519 XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1520 XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
1521 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
1523 required_state_details =
1524 (XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
1525 XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
1526 XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK),
1529 static const xcb_xkb_select_events_details_t details = {
1530 .affectNewKeyboard = required_nkn_details,
1531 .newKeyboardDetails = required_nkn_details,
1532 .affectState = required_state_details,
1533 .stateDetails = required_state_details,
1544 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1547 if (keymap == NULL) {
1548 g_warning(
"Failed to get Keymap for current keyboard device.");
1551 struct xkb_state *state = xkb_x11_state_new_from_device(
1553 if (state == NULL) {
1554 g_warning(
"Failed to get state object for current keyboard device.");
1559 xkb_state_unref(state);
1560 xkb_keymap_unref(keymap);
1566 g_warning(
"Connection has error");
1570 uint32_t val[] = {XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
1573 XCB_CW_EVENT_MASK, val);
1579 g_warning(
"Connection has error");
1588 g_warning(
"Connection has error");
1596 xcb_depth_t *root_depth = NULL;
1597 xcb_depth_iterator_t depth_iter;
1598 for (depth_iter = xcb_screen_allowed_depths_iterator(
xcb->
screen);
1599 depth_iter.rem; xcb_depth_next(&depth_iter)) {
1600 xcb_depth_t *d = depth_iter.data;
1602 xcb_visualtype_iterator_t visual_iter;
1603 for (visual_iter = xcb_depth_visuals_iterator(d); visual_iter.rem;
1604 xcb_visualtype_next(&visual_iter)) {
1605 xcb_visualtype_t *v = visual_iter.data;
1606 if ((v->bits_per_rgb_value == 8) && (d->depth == 32) &&
1607 (v->_class == XCB_VISUAL_CLASS_TRUE_COLOR)) {
1611 if (
xcb->
screen->root_visual == v->visual_id) {
1618 xcb_void_cookie_t c;
1619 xcb_generic_error_t *e;
1621 c = xcb_create_colormap_checked(
xcb->
connection, XCB_COLORMAP_ALLOC_NONE,
1639 xcb_cursor_context_t *ctx;
1648 if (
cursors[i] == XCB_CURSOR_NONE) {
1654 xcb_cursor_context_free(ctx);
1664 g_warning(
"Failed to grab pointer after %u times. Giving up.",
1666 return G_SOURCE_REMOVE;
1669 return G_SOURCE_REMOVE;
1672 return G_SOURCE_CONTINUE;
1677 g_warning(
"Failed to grab keyboard after %u times. Giving up.",
1680 return G_SOURCE_REMOVE;
1683 return G_SOURCE_REMOVE;
1686 return G_SOURCE_CONTINUE;
1700 if (
find_arg(
"-normal-window") >= 0) {
1703 if (
find_arg(
"-no-lazy-grab") >= 0) {
1705 g_warning(
"Failed to grab keyboard, even after %d uS.", 500 * 1000);
1709 g_warning(
"Failed to grab mouse pointer, even after %d uS.", 100 * 1000);
1735 g_debug(
"Cleaning up XCB and XKB");
1747 xcb_ewmh_connection_wipe(&(
xcb->
ewmh));
1759 const uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
1761 struct MotifWMHints {
1764 uint32_t decorations;
1769 struct MotifWMHints hints;
1770 hints.flags = MWM_HINTS_DECORATIONS;
1771 hints.decorations = 0;
1772 hints.functions = 0;
1773 hints.inputMode = 0;
1776 xcb_atom_t ha =
netatoms[_MOTIF_WM_HINTS];
1777 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, window, ha, ha,
1786 if (
cursors[type] == XCB_CURSOR_NONE) {
1790 xcb_change_window_attributes(
xcb->
connection, window, XCB_CW_CURSOR,
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
int find_arg_str(const char *const key, char **val)
int find_arg(const char *const key)
xcb_window_t rofi_view_get_window(void)
RofiViewState * rofi_view_get_active(void)
void rofi_view_handle_text(RofiViewState *state, char *text)
void rofi_view_handle_mouse_motion(RofiViewState *state, gint x, gint y, gboolean find_mouse_target)
void rofi_view_temp_click_to_exit(RofiViewState *state, xcb_window_t target)
void rofi_view_temp_configure_notify(RofiViewState *state, xcb_configure_notify_event_t *xce)
void rofi_view_frame_callback(void)
void rofi_view_maybe_update(RofiViewState *state)
const gchar * description
xcb_connection_t * connection
SnLauncheeContext * sncontext
struct _workarea * monitors
xcb_timestamp_t last_timestamp
xcb_ewmh_connection_t ewmh
struct _xcb_stuff::@8 xkb
NkBindingsSeat * bindings_seat
xcb_window_t focus_revert
gboolean display_late_setup(void)
static int take_pointer(xcb_window_t w, int iters)
int monitor_active(workarea *mon)
const struct @4 cursor_names[]
static void x11_build_monitor_layout_xinerama()
#define RANDR_PREF_MAJOR_VERSION
const char * traditional_name
void display_early_cleanup(void)
static int x11_is_extension_present(const char *extension)
static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data)
cairo_surface_t * x11_helper_get_screenshot_surface(void)
void display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
static void x11_build_monitor_layout()
void display_cleanup(void)
static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen, xcb_atom_t atom)
static void release_pointer(void)
struct _xcb_stuff xcb_int
static int monitor_active_from_winid(xcb_drawable_t id, workarea *mon)
static void x11_create_visual_and_colormap(void)
static gboolean x11_button_to_nk_bindings_scroll(guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps)
static void monitor_dimensions(int x, int y, workarea *mon)
void rofi_xcb_revert_input_focus(void)
static uint32_t * create_kernel(double radius, double deviation, uint32_t *sum2)
static void x11_lookup_cursors(void)
void rofi_xcb_set_input_focus(xcb_window_t w)
static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data)
static xcb_visualtype_t * lookup_visual(xcb_screen_t *s, xcb_visualid_t visual)
#define INTERSECT(x, y, x1, y1, w1, h1)
static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay)
void x11_set_cursor(xcb_window_t window, X11CursorType type)
cairo_surface_t * x11_helper_get_bg_surface(void)
unsigned int lazy_grab_retry_count_pt
static xcb_visualtype_t * root_visual
static void x11_create_frequently_used_atoms(void)
static int monitor_active_from_id_focused(int mon_id, workarea *mon)
static gboolean x11_button_to_nk_bindings_button(guint32 x11_button, NkBindingsMouseButton *button)
xcb_cursor_t cursors[NUM_CURSORS]
static void x11_monitor_free(workarea *m)
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
static void x11_helper_discover_window_manager(void)
static int monitor_get_dimension(int monitor_id, workarea *mon)
static int take_keyboard(xcb_window_t w, int iters)
xcb_window_t xcb_stuff_get_root_window(void)
unsigned int lazy_grab_retry_count_kb
static void rofi_view_paste(RofiViewState *state, xcb_selection_notify_event_t *xse)
void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count)
const char * netatom_names[]
void cairo_image_surface_blur(cairo_surface_t *surface, double radius, double deviation)
static int monitor_active_from_id(int mon_id, workarea *mon)
static void x11_monitors_free(void)
gboolean display_setup(GMainLoop *main_loop, NkBindings *bindings)
void display_dump_monitor_layout(void)
static void error_trap_push(G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay)
WindowManagerQuirk current_window_manager
static int pointer_get(xcb_window_t root, int *x, int *y)
#define RANDR_PREF_MINOR_VERSION
#define sn_launcher_context_set_application_id
static int error_trap_depth
static void release_keyboard(void)
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data)
void x11_disable_decoration(xcb_window_t window)
static void main_loop_x11_event_handler_view(xcb_generic_event_t *event)
static workarea * x11_get_monitor_from_output(xcb_randr_output_t out)
xcb_atom_t netatoms[NUM_NETATOMS]
xcb_visualtype_t * visual
@ WM_PANGO_WORKSPACE_NAMES
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP