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
69 #define RANDR_PREF_MAJOR_VERSION 1
71 #define RANDR_PREF_MINOR_VERSION 5
74 #define INTERSECT(x, y, x1, y1, w1, h1) \
75 ((((x) >= (x1)) && ((x) < (x1 + w1))) && (((y) >= (y1)) && ((y) < (y1 + h1))))
95 xcb_colormap_t
map = XCB_COLORMAP_NONE;
116 {
"default",
"left_ptr"}, {
"pointer",
"hand"}, {
"text",
"xterm"}};
119 xcb_depth_iterator_t d;
120 d = xcb_screen_allowed_depths_iterator(s);
121 for (; d.rem; xcb_depth_next(&d)) {
122 xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator(d.data);
123 for (; v.rem; xcb_visualtype_next(&v)) {
124 if (v.data->visual_id ==
visual) {
138 int size = 2 * (int)(radius) + 1;
139 uint32_t *kernel = (uint32_t *)(g_malloc(
sizeof(uint32_t) * (size + 1)));
140 double radiusf = fabs(radius) + 1.0;
141 double value = -radius;
145 if (deviation == 0.0) {
146 deviation = sqrt(-(radiusf * radiusf) / (2.0 * log(1.0 / 255.0)));
151 for (i = 0; i < size; i++) {
152 kernel[1 + i] = INT16_MAX / (2.506628275 * deviation) *
153 exp(-((value * value) / (2.0 * (deviation * deviation))));
155 sum += kernel[1 + i];
167 uint32_t *kernel = 0;
168 cairo_format_t format;
169 unsigned int channels;
171 if (cairo_surface_status(surface)) {
175 uint8_t *data = cairo_image_surface_get_data(surface);
176 format = cairo_image_surface_get_format(surface);
177 const int width = cairo_image_surface_get_width(surface);
178 const int height = cairo_image_surface_get_height(surface);
179 const int stride = cairo_image_surface_get_stride(surface);
181 if (format == CAIRO_FORMAT_ARGB32) {
187 horzBlur = (uint32_t *)(g_malloc(
sizeof(uint32_t) * height * stride));
194 uint32_t *horzBlur_ptr = horzBlur;
195 for (
int iY = 0; iY < height; iY++) {
196 const int iYs = iY * stride;
197 for (
int iX = 0; iX < width; iX++) {
202 int offset = (int)(kernel[0]) / -2;
204 for (
int i = 0; i < (int)(kernel[0]); i++) {
207 if (x < 0 || x >= width) {
212 uint8_t *dataPtr = &data[iYs + x * channels];
213 const uint32_t kernip1 = kernel[i + 1];
215 blue += kernip1 * dataPtr[0];
216 green += kernip1 * dataPtr[1];
217 red += kernip1 * dataPtr[2];
218 alpha += kernip1 * dataPtr[3];
222 *horzBlur_ptr++ = blue / sum;
223 *horzBlur_ptr++ = green / sum;
224 *horzBlur_ptr++ = red / sum;
225 *horzBlur_ptr++ = alpha / sum;
231 for (
int iY = 0; iY < height; iY++) {
232 for (
int iX = 0; iX < width; iX++) {
237 int offset = (int)(kernel[0]) / -2;
239 const int iXs = iX * channels;
240 for (
int i = 0; i < (int)(kernel[0]); i++) {
243 if (y < 0 || y >= height) {
248 uint32_t *dataPtr = &horzBlur[y * stride + iXs];
249 const uint32_t kernip1 = kernel[i + 1];
251 blue += kernip1 * dataPtr[0];
252 green += kernip1 * dataPtr[1];
253 red += kernip1 * dataPtr[2];
254 alpha += kernip1 * dataPtr[3];
259 *data++ = blue / sum;
260 *data++ = green / sum;
262 *data++ = alpha / sum;
275 xcb_get_geometry_cookie_t cookie;
276 xcb_get_geometry_reply_t *reply;
279 reply = xcb_get_geometry_reply(
xcb->
connection, cookie, NULL);
284 xcb_get_window_attributes_cookie_t attributesCookie =
286 xcb_get_window_attributes_reply_t *attributes =
287 xcb_get_window_attributes_reply(
xcb->
connection, attributesCookie, NULL);
288 if (attributes == NULL || (attributes->map_state != XCB_MAP_STATE_VIEWABLE)) {
299 cairo_surface_t *t = cairo_xcb_surface_create(
xcb->
connection, window, vt,
300 reply->width, reply->height);
302 if (cairo_surface_status(t) != CAIRO_STATUS_SUCCESS) {
303 cairo_surface_destroy(t);
309 int max = MAX(reply->width, reply->height);
310 double scale = (double)size / max;
312 cairo_surface_t *s2 = cairo_surface_create_similar_image(
313 t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale);
316 if (cairo_surface_status(s2) != CAIRO_STATUS_SUCCESS) {
317 cairo_surface_destroy(t);
321 cairo_t *d = cairo_create(s2);
322 cairo_scale(d, scale, scale);
323 cairo_set_source_surface(d, t, 0, 0);
327 cairo_surface_destroy(t);
342 xcb_get_property_cookie_t cookie;
343 xcb_get_property_reply_t *reply;
344 xcb_pixmap_t rootpixmap = XCB_NONE;
346 cookie = xcb_get_property(c, 0, screen->root, atom, XCB_ATOM_PIXMAP, 0, 1);
348 reply = xcb_get_property_reply(c, cookie, NULL);
351 if (xcb_get_property_value_length(reply) ==
sizeof(xcb_pixmap_t)) {
352 memcpy(&rootpixmap, xcb_get_property_value(reply),
sizeof(xcb_pixmap_t));
363 if (pm == XCB_NONE) {
375 xcb_get_property_cookie_t c = xcb_get_property(
376 xcb->
connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
377 xcb_get_property_reply_t *r =
380 if (xcb_get_property_value_length(r) > 0) {
382 if (r->type ==
netatoms[UTF8_STRING]) {
383 str = g_strndup(xcb_get_property_value(r),
384 xcb_get_property_value_length(r));
385 }
else if (r->type ==
netatoms[STRING]) {
387 xcb_get_property_value_length(r));
389 str = g_strdup(
"Invalid encoding.");
402 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, w, prop,
403 XCB_ATOM_ATOM, 32,
count, atoms);
430 xcb_randr_get_output_info_reply_t *op_reply;
431 xcb_randr_get_crtc_info_reply_t *crtc_reply;
432 xcb_randr_get_output_info_cookie_t it =
433 xcb_randr_get_output_info(
xcb->
connection, out, XCB_CURRENT_TIME);
434 op_reply = xcb_randr_get_output_info_reply(
xcb->
connection, it, NULL);
435 if (op_reply->crtc == XCB_NONE) {
439 xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info(
441 crtc_reply = xcb_randr_get_crtc_info_reply(
xcb->
connection, ct, NULL);
447 retv->
x = crtc_reply->x;
448 retv->
y = crtc_reply->y;
449 retv->
w = crtc_reply->width;
450 retv->
h = crtc_reply->height;
452 retv->
mw = op_reply->mm_width;
453 retv->
mh = op_reply->mm_height;
455 char *tname = (
char *)xcb_randr_get_output_info_name(op_reply);
456 int tname_len = xcb_randr_get_output_info_name_length(op_reply);
458 retv->
name = g_malloc0((tname_len + 1) *
sizeof(
char));
459 memcpy(retv->
name, tname, tname_len);
466 #if (((XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION) && \
467 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
468 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
477 x11_get_monitor_from_randr_monitor(xcb_randr_monitor_info_t *
mon) {
479 xcb_generic_error_t *err;
480 xcb_get_atom_name_cookie_t anc =
482 xcb_get_atom_name_reply_t *atom_reply =
485 g_warning(
"Could not get RandR monitor name: X11 error code %d\n",
498 retv->
w =
mon->width;
499 retv->
h =
mon->height;
502 retv->
mw =
mon->width_in_millimeters;
503 retv->
mh =
mon->height_in_millimeters;
507 g_strdup_printf(
"%.*s", xcb_get_atom_name_name_length(atom_reply),
508 xcb_get_atom_name_name(atom_reply));
518 xcb_query_extension_cookie_t randr_cookie =
519 xcb_query_extension(
xcb->
connection, strlen(extension), extension);
521 xcb_query_extension_reply_t *randr_reply =
522 xcb_query_extension_reply(
xcb->
connection, randr_cookie, NULL);
524 int present = randr_reply->present;
532 xcb_xinerama_query_screens_cookie_t screens_cookie =
535 xcb_xinerama_query_screens_reply_t *screens_reply =
536 xcb_xinerama_query_screens_reply(
xcb->
connection, screens_cookie, NULL);
538 xcb_xinerama_screen_info_iterator_t screens_iterator =
539 xcb_xinerama_query_screens_screen_info_iterator(screens_reply);
541 for (; screens_iterator.rem > 0;
542 xcb_xinerama_screen_info_next(&screens_iterator)) {
545 w->
x = screens_iterator.data->x_org;
546 w->
y = screens_iterator.data->y_org;
547 w->
w = screens_iterator.data->width;
548 w->
h = screens_iterator.data->height;
570 g_debug(
"Query XINERAMA for monitor layout.");
574 g_debug(
"No RANDR or Xinerama available for getting monitor layout.");
577 g_debug(
"Query RANDR for monitor layout.");
579 g_debug(
"Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION,
580 XCB_RANDR_MINOR_VERSION);
581 #if (((XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION) && \
582 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
583 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
584 xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version(
586 xcb_randr_query_version_reply_t *rversion =
587 xcb_randr_query_version_reply(
xcb->
connection, cversion, NULL);
589 g_debug(
"Found randr version: %d.%d", rversion->major_version,
590 rversion->minor_version);
595 xcb_randr_get_monitors_cookie_t t =
597 xcb_randr_get_monitors_reply_t *mreply =
600 xcb_randr_monitor_info_iterator_t iter =
601 xcb_randr_get_monitors_monitors_iterator(mreply);
602 while (iter.rem > 0) {
603 workarea *w = x11_get_monitor_from_randr_monitor(iter.data);
608 xcb_randr_monitor_info_next(&iter);
619 xcb_randr_get_screen_resources_current_reply_t *res_reply;
620 xcb_randr_get_screen_resources_current_cookie_t src;
621 src = xcb_randr_get_screen_resources_current(
xcb->
connection,
623 res_reply = xcb_randr_get_screen_resources_current_reply(
xcb->
connection,
629 xcb_randr_get_screen_resources_current_outputs_length(res_reply);
630 xcb_randr_output_t *ops =
631 xcb_randr_get_screen_resources_current_outputs(res_reply);
634 xcb_randr_get_output_primary_cookie_t pc =
636 xcb_randr_get_output_primary_reply_t *pc_rep =
637 xcb_randr_get_output_primary_reply(
xcb->
connection, pc, NULL);
639 for (
int i = mon_num - 1; i >= 0; i--) {
644 if (pc_rep && pc_rep->output == ops[i]) {
659 iter->monitor_id = index++;
664 int is_term = isatty(fileno(stdout));
665 printf(
"Monitor layout:\n");
667 printf(
"%s ID%s: %d", (is_term) ?
color_bold :
"",
670 printf(
" (primary)");
673 printf(
"%s name%s: %s\n", (is_term) ?
color_bold :
"",
675 printf(
"%s position%s: %d,%d\n", (is_term) ?
color_bold :
"",
677 printf(
"%s size%s: %d,%d\n", (is_term) ?
color_bold :
"",
679 if (iter->mw > 0 && iter->mh > 0) {
680 printf(
"%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
682 iter->mh, iter->w * 25.4 / (
double)iter->mw,
683 iter->h * 25.4 / (
double)iter->mh);
690 GSpawnChildSetupFunc *child_setup,
691 gpointer *user_data) {
692 if (context == NULL) {
696 SnLauncherContext *sncontext;
700 sn_launcher_context_set_name(sncontext, context->
name);
701 sn_launcher_context_set_description(sncontext, context->
description);
702 if (context->
binary != NULL) {
703 sn_launcher_context_set_binary_name(sncontext, context->
binary);
705 if (context->
icon != NULL) {
706 sn_launcher_context_set_icon_name(sncontext, context->
icon);
708 if (context->
app_id != NULL) {
711 if (context->
wmclass != NULL) {
712 sn_launcher_context_set_wmclass(sncontext, context->
wmclass);
715 xcb_get_property_cookie_t c;
716 unsigned int current_desktop = 0;
719 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
721 sn_launcher_context_set_workspace(sncontext, current_desktop);
724 sn_launcher_context_initiate(sncontext,
"rofi", context->
command,
727 *child_setup = (GSpawnChildSetupFunc)sn_launcher_context_setup_child_process;
728 *user_data = sncontext;
752 if (
INTERSECT(x, y, iter->x, iter->y, iter->w, iter->h)) {
772 xcb_query_pointer_cookie_t c = xcb_query_pointer(
xcb->
connection, root);
773 xcb_query_pointer_reply_t *r =
786 xcb_get_geometry_cookie_t c = xcb_get_geometry(
xcb->
connection,
id);
787 xcb_get_geometry_reply_t *r =
790 xcb_translate_coordinates_cookie_t ct =
791 xcb_translate_coordinates(
xcb->
connection,
id, root, r->x, r->y);
792 xcb_translate_coordinates_reply_t *t =
798 mon->
x = t->dst_x - r->x;
799 mon->
y = t->dst_y - r->y;
812 xcb_window_t active_window;
813 xcb_get_property_cookie_t awc;
815 if (!xcb_ewmh_get_active_window_reply(&
xcb->
ewmh, awc, &active_window,
818 "Failed to get active window, falling back to mouse location (-5).");
821 xcb_query_tree_cookie_t tree_cookie =
823 xcb_query_tree_reply_t *tree_reply =
827 "Failed to get parent window, falling back to mouse location (-5).");
831 xcb_get_geometry_cookie_t c =
833 xcb_get_geometry_reply_t *r =
836 g_debug(
"Failed to get geometry of active window, falling back to mouse "
841 xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates(
843 xcb_translate_coordinates_reply_t *t =
850 mon->
x = t->dst_x - r->x;
851 mon->
y = t->dst_y - r->y;
861 }
else if (mon_id == -4) {
867 g_debug(
"Failed to get translate position of active window, falling back "
868 "to mouse location (-5).");
887 else if (mon_id == -1) {
889 unsigned int current_desktop = 0;
890 xcb_get_property_cookie_t gcdc;
892 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, gcdc, ¤t_desktop,
894 xcb_get_property_cookie_t c =
896 xcb_ewmh_get_desktop_viewport_reply_t vp;
897 if (xcb_ewmh_get_desktop_viewport_reply(&
xcb->
ewmh, c, &vp, NULL)) {
898 if (current_desktop < vp.desktop_viewport_len) {
900 vp.desktop_viewport[current_desktop].y,
mon);
901 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
904 g_debug(
"Viewport does not exist for current desktop: %d, falling "
905 "back to mouse location (-5)",
907 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
909 g_debug(
"Failed to get viewport for current desktop: %d, falling back "
910 "to mouse location (-5).",
914 g_debug(
"Failed to get current desktop, falling back to mouse location "
917 }
else if (mon_id == -2 || mon_id == -4) {
923 else if (mon_id == -5) {
931 g_debug(
"Failed to find monitor, fall back to monitor showing mouse.");
957 xcb_drawable_t win = g_ascii_strtoll(
config.
monitor + 4, &end, 0);
973 g_warning(
"Failed to find selected monitor.");
991 xcb_selection_notify_event_t *xse) {
992 if (xse->property == XCB_ATOM_NONE) {
993 g_warning(
"Failed to convert selection");
994 }
else if (xse->property ==
xcb->
ewmh.UTF8_STRING) {
996 if (text != NULL && text[0] !=
'\0') {
997 unsigned int dl = strlen(text);
999 for (
unsigned int i = 0; i < dl; i++) {
1000 if (text[i] ==
'\n') {
1008 g_warning(
"Failed");
1014 NkBindingsMouseButton *button) {
1015 switch (x11_button) {
1017 *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
1020 *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
1023 *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
1026 *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
1029 *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
1037 *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
1043 NkBindingsScrollAxis *axis,
1046 switch (x11_button) {
1051 *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
1057 *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
1070 if (state == NULL) {
1074 switch (event->response_type & ~0x80) {
1075 case XCB_CLIENT_MESSAGE: {
1076 xcb_client_message_event_t *cme = (xcb_client_message_event_t *)event;
1077 xcb_atom_t atom = cme->data.data32[0];
1078 xcb_timestamp_t
time = cme->data.data32[1];
1079 if (atom ==
netatoms[WM_TAKE_FOCUS]) {
1080 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_NONE, cme->window,
1089 case XCB_CONFIGURE_NOTIFY: {
1090 xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *)event;
1094 case XCB_MOTION_NOTIFY: {
1095 xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *)event;
1096 gboolean button_mask = xme->state & XCB_EVENT_MASK_BUTTON_1_MOTION;
1104 case XCB_BUTTON_PRESS: {
1105 xcb_button_press_event_t *bpe = (xcb_button_press_event_t *)event;
1106 NkBindingsMouseButton button;
1107 NkBindingsScrollAxis axis;
1114 NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time);
1120 case XCB_BUTTON_RELEASE: {
1121 xcb_button_release_event_t *bre = (xcb_button_release_event_t *)event;
1122 NkBindingsMouseButton button;
1127 NK_BINDINGS_BUTTON_STATE_RELEASE,
1139 case XCB_SELECTION_NOTIFY:
1142 case XCB_KEYMAP_NOTIFY: {
1143 xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *)event;
1144 for (gint32 by = 0; by < 31; ++by) {
1145 for (gint8 bi = 0; bi < 7; ++bi) {
1146 if (kne->keys[by] & (1 << bi)) {
1150 NK_BINDINGS_KEY_STATE_PRESSED);
1156 case XCB_KEY_PRESS: {
1157 xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *)event;
1161 text = nk_bindings_seat_handle_key_with_modmask(
1163 NK_BINDINGS_KEY_STATE_PRESS);
1169 case XCB_KEY_RELEASE: {
1170 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1173 NK_BINDINGS_KEY_STATE_RELEASE);
1183 G_GNUC_UNUSED gpointer user_data) {
1187 g_warning(
"The XCB connection to X server had a fatal error: %d", status);
1189 return G_SOURCE_REMOVE;
1191 g_warning(
"main_loop_x11_event_handler: ev == NULL, status == %d", status);
1192 return G_SOURCE_CONTINUE;
1194 uint8_t type = ev->response_type & ~0x80;
1197 case XCB_XKB_MAP_NOTIFY: {
1198 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1201 struct xkb_state *state = xkb_x11_state_new_from_device(
1204 xkb_keymap_unref(keymap);
1205 xkb_state_unref(state);
1208 case XCB_XKB_STATE_NOTIFY: {
1209 xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *)ev;
1211 ksne->latchedMods, ksne->lockedMods,
1212 ksne->baseGroup, ksne->latchedGroup,
1218 return G_SOURCE_CONTINUE;
1224 return G_SOURCE_CONTINUE;
1232 xcb_generic_error_t *error;
1233 xcb_get_input_focus_reply_t *freply;
1234 xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus(
xcb->
connection);
1235 freply = xcb_get_input_focus_reply(
xcb->
connection, fcookie, &error);
1236 if (error != NULL) {
1237 g_warning(
"Could not get input focus (error %d), will revert focus to best "
1245 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT, w,
1255 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT,
1264 g_warning(
"Connection has error");
1267 xcb_grab_pointer_cookie_t cc =
1268 xcb_grab_pointer(
xcb->
connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
1269 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE,
1271 xcb_grab_pointer_reply_t *r =
1274 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1280 if ((++i) > iters) {
1283 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1284 nanosleep(&del, NULL);
1293 g_warning(
"Connection has error");
1296 xcb_grab_keyboard_cookie_t cc =
1298 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1299 xcb_grab_keyboard_reply_t *r =
1302 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1308 if ((++i) > iters) {
1311 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1312 nanosleep(&del, NULL);
1327 G_GNUC_UNUSED xcb_connection_t *xdisplay) {
1332 xcb_connection_t *xdisplay) {
1334 g_warning(
"Error trap underflow!");
1338 xcb_flush(xdisplay);
1348 xcb_intern_atom_cookie_t cc = xcb_intern_atom(
1350 xcb_intern_atom_reply_t *r =
1360 xcb_window_t wm_win = 0;
1361 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1364 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1365 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1366 xcb_get_property_cookie_t cookie =
1367 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1368 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1369 if (wtitle.strings_len > 0) {
1370 g_debug(
"Found window manager: |%s|", wtitle.strings);
1371 if (g_strcmp0(wtitle.strings,
"i3") == 0) {
1374 }
else if (g_strcmp0(wtitle.strings,
"bspwm") == 0) {
1378 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1386 char *display_str = (
char *)g_getenv(
"DISPLAY");
1394 g_warning(
"Failed to open display: %s", display_str);
1405 xcb_intern_atom_cookie_t *ac =
1407 xcb_generic_error_t *errors = NULL;
1408 xcb_ewmh_init_atoms_replies(&
xcb->
ewmh, ac, &errors);
1410 g_warning(
"Failed to create EWMH atoms");
1417 if (xkb_x11_setup_xkb_extension(
1419 XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
1421 g_warning(
"cannot setup XKB extension!");
1429 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1430 XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
1432 required_nkn_details = (XCB_XKB_NKN_DETAIL_KEYCODES),
1434 required_map_parts =
1435 (XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
1436 XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1437 XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
1438 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
1440 required_state_details =
1441 (XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
1442 XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
1443 XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK),
1446 static const xcb_xkb_select_events_details_t details = {
1447 .affectNewKeyboard = required_nkn_details,
1448 .newKeyboardDetails = required_nkn_details,
1449 .affectState = required_state_details,
1450 .stateDetails = required_state_details,
1461 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1464 if (keymap == NULL) {
1465 g_warning(
"Failed to get Keymap for current keyboard device.");
1468 struct xkb_state *state = xkb_x11_state_new_from_device(
1470 if (state == NULL) {
1471 g_warning(
"Failed to get state object for current keyboard device.");
1476 xkb_state_unref(state);
1477 xkb_keymap_unref(keymap);
1483 g_warning(
"Connection has error");
1491 g_warning(
"Connection has error");
1500 g_warning(
"Connection has error");
1508 xcb_depth_t *root_depth = NULL;
1509 xcb_depth_iterator_t depth_iter;
1510 for (depth_iter = xcb_screen_allowed_depths_iterator(
xcb->
screen);
1511 depth_iter.rem; xcb_depth_next(&depth_iter)) {
1512 xcb_depth_t *d = depth_iter.data;
1514 xcb_visualtype_iterator_t visual_iter;
1515 for (visual_iter = xcb_depth_visuals_iterator(d); visual_iter.rem;
1516 xcb_visualtype_next(&visual_iter)) {
1517 xcb_visualtype_t *v = visual_iter.data;
1518 if ((v->bits_per_rgb_value == 8) && (d->depth == 32) &&
1519 (v->_class == XCB_VISUAL_CLASS_TRUE_COLOR)) {
1523 if (
xcb->
screen->root_visual == v->visual_id) {
1530 xcb_void_cookie_t c;
1531 xcb_generic_error_t *e;
1533 c = xcb_create_colormap_checked(
xcb->
connection, XCB_COLORMAP_ALLOC_NONE,
1551 xcb_cursor_context_t *ctx;
1560 if (
cursors[i] == XCB_CURSOR_NONE) {
1566 xcb_cursor_context_free(ctx);
1576 g_warning(
"Failed to grab pointer after %u times. Giving up.",
1578 return G_SOURCE_REMOVE;
1581 return G_SOURCE_REMOVE;
1584 return G_SOURCE_CONTINUE;
1589 g_warning(
"Failed to grab keyboard after %u times. Giving up.",
1592 return G_SOURCE_REMOVE;
1595 return G_SOURCE_REMOVE;
1598 return G_SOURCE_CONTINUE;
1612 if (
find_arg(
"-normal-window") >= 0) {
1615 if (
find_arg(
"-no-lazy-grab") >= 0) {
1617 g_warning(
"Failed to grab keyboard, even after %d uS.", 500 * 1000);
1621 g_warning(
"Failed to grab mouse pointer, even after %d uS.", 100 * 1000);
1647 g_debug(
"Cleaning up XCB and XKB");
1659 xcb_ewmh_connection_wipe(&(
xcb->
ewmh));
1671 const uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
1673 struct MotifWMHints {
1676 uint32_t decorations;
1681 struct MotifWMHints hints;
1682 hints.flags = MWM_HINTS_DECORATIONS;
1683 hints.decorations = 0;
1684 hints.functions = 0;
1685 hints.inputMode = 0;
1688 xcb_atom_t ha =
netatoms[_MOTIF_WM_HINTS];
1689 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, window, ha, ha,
1698 if (
cursors[type] == XCB_CURSOR_NONE) {
1702 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)
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