30#define G_LOG_DOMAIN "X11Helper"
35#include <xcb-imdkit/encoding.h>
36#include <xcb/xcb_keysyms.h>
49#include <xcb/xcb_aux.h>
50#include <xcb/xcb_cursor.h>
51#include <xcb/xcb_ewmh.h>
52#include <xcb/xinerama.h>
54#include <xcb/xproto.h>
55#include <xkbcommon/xkbcommon-x11.h>
56#include <xkbcommon/xkbcommon.h>
58#define SN_API_NOT_YET_FROZEN
61#define sn_launcher_context_set_application_id sn_launcher_set_application_id
79#define RANDR_PREF_MAJOR_VERSION 1
81#define RANDR_PREF_MINOR_VERSION 5
84#define INTERSECT(x, y, x1, y1, w1, h1) \
85 ((((x) >= (x1)) && ((x) < (x1 + w1))) && (((y) >= (y1)) && ((y) < (y1 + h1))))
110xcb_colormap_t
map = XCB_COLORMAP_NONE;
131 {
"default",
"left_ptr"}, {
"pointer",
"hand"}, {
"text",
"xterm"}};
133static xcb_visualtype_t *
lookup_visual(xcb_screen_t *s, xcb_visualid_t vis) {
134 xcb_depth_iterator_t d;
135 d = xcb_screen_allowed_depths_iterator(s);
136 for (; d.rem; xcb_depth_next(&d)) {
137 xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator(d.data);
138 for (; v.rem; xcb_visualtype_next(&v)) {
139 if (v.data->visual_id == vis) {
151static uint32_t *
create_kernel(
int radius,
double deviation, uint32_t *sum2) {
152 int size = 2 * (radius) + 1;
153 uint32_t *kernel = (uint32_t *)(g_malloc(
sizeof(uint32_t) * (size + 1)));
154 double radiusf = abs(radius) + 1.0;
155 double value = -radius;
159 if (deviation == 0.0) {
160 deviation = sqrt(-(radiusf * radiusf) / (2.0 * log(1.0 / 255.0)));
165 for (i = 0; i < size; i++) {
166 kernel[1 + i] = INT16_MAX / (2.506628275 * deviation) *
167 exp(-((value * value) / (2.0 * (deviation * deviation))));
169 sum += kernel[1 + i];
181 uint32_t *kernel = 0;
182 cairo_format_t format;
183 unsigned int channels;
185 if (cairo_surface_status(surface)) {
189 uint8_t *data = cairo_image_surface_get_data(surface);
190 format = cairo_image_surface_get_format(surface);
191 const int width = cairo_image_surface_get_width(surface);
192 const int height = cairo_image_surface_get_height(surface);
193 const int stride = cairo_image_surface_get_stride(surface);
194 if (radius > (height / 2)) {
195 radius = height / 2 - 1;
197 if (radius > (width / 2)) {
198 radius = width / 2 - 1;
204 if (format == CAIRO_FORMAT_ARGB32) {
210 horzBlur = (uint32_t *)(g_malloc(
sizeof(uint32_t) * height * stride));
217 uint32_t *horzBlur_ptr = horzBlur;
218 for (
int iY = 0; iY < height; iY++) {
219 const int iYs = iY * stride;
220 for (
int iX = 0; iX < width; iX++) {
225 int offset = (int)(kernel[0]) / -2;
227 for (
int i = 0; i < (int)(kernel[0]); i++) {
230 if (x < 0 || x >= width) {
235 uint8_t *dataPtr = &data[iYs + x * channels];
236 const uint32_t kernip1 = kernel[i + 1];
238 blue += kernip1 * dataPtr[0];
239 green += kernip1 * dataPtr[1];
240 red += kernip1 * dataPtr[2];
241 alpha += kernip1 * dataPtr[3];
245 *horzBlur_ptr++ = blue / sum;
246 *horzBlur_ptr++ = green / sum;
247 *horzBlur_ptr++ = red / sum;
248 *horzBlur_ptr++ = alpha / sum;
254 for (
int iY = 0; iY < height; iY++) {
255 for (
int iX = 0; iX < width; iX++) {
260 int offset = (int)(kernel[0]) / -2;
262 const int iXs = iX * channels;
263 for (
int i = 0; i < (int)(kernel[0]); i++) {
266 if (y < 0 || y >= height) {
271 uint32_t *dataPtr = &horzBlur[y * stride + iXs];
272 const uint32_t kernip1 = kernel[i + 1];
274 blue += kernip1 * dataPtr[0];
275 green += kernip1 * dataPtr[1];
276 red += kernip1 * dataPtr[2];
277 alpha += kernip1 * dataPtr[3];
282 *data++ = blue / sum;
283 *data++ = green / sum;
285 *data++ = alpha / sum;
298 xcb_get_geometry_cookie_t cookie;
299 xcb_get_geometry_reply_t *reply;
302 reply = xcb_get_geometry_reply(
xcb->
connection, cookie, NULL);
307 xcb_get_window_attributes_cookie_t attributesCookie =
309 xcb_get_window_attributes_reply_t *attributes =
310 xcb_get_window_attributes_reply(
xcb->
connection, attributesCookie, NULL);
311 if (attributes == NULL || (attributes->map_state != XCB_MAP_STATE_VIEWABLE)) {
322 cairo_surface_t *t = cairo_xcb_surface_create(
xcb->
connection, window, vt,
323 reply->width, reply->height);
325 if (cairo_surface_status(t) != CAIRO_STATUS_SUCCESS) {
326 cairo_surface_destroy(t);
332 int max = MAX(reply->width, reply->height);
333 double scale = (double)size / max;
335 cairo_surface_t *s2 = cairo_surface_create_similar_image(
336 t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale);
339 if (cairo_surface_status(s2) != CAIRO_STATUS_SUCCESS) {
340 cairo_surface_destroy(t);
344 cairo_t *d = cairo_create(s2);
345 cairo_scale(d, scale, scale);
346 cairo_set_source_surface(d, t, 0, 0);
350 cairo_surface_destroy(t);
365 xcb_get_property_cookie_t cookie;
366 xcb_get_property_reply_t *reply;
367 xcb_pixmap_t rootpixmap = XCB_NONE;
369 cookie = xcb_get_property(c, 0, screen->root, atom, XCB_ATOM_PIXMAP, 0, 1);
371 reply = xcb_get_property_reply(c, cookie, NULL);
374 if (xcb_get_property_value_length(reply) ==
sizeof(xcb_pixmap_t)) {
375 memcpy(&rootpixmap, xcb_get_property_value(reply),
sizeof(xcb_pixmap_t));
386 if (pm == XCB_NONE) {
398 xcb_get_property_cookie_t c = xcb_get_property(
399 xcb->
connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
400 xcb_get_property_reply_t *r =
403 if (xcb_get_property_value_length(r) > 0) {
405 if (r->type ==
netatoms[UTF8_STRING]) {
407 xcb_get_property_value_length(r));
408 }
else if (r->type ==
netatoms[STRING]) {
410 xcb_get_property_value_length(r));
412 str = g_strdup(
"Invalid encoding.");
425 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, w, prop,
426 XCB_ATOM_ATOM, 32, count, atoms);
455 double ratio_res = w->
w / (double)w->
h;
456 double ratio_size = w->
mw / (double)w->
mh;
458 if ((ratio_res < 1.0 && ratio_size > 1.0) ||
459 (ratio_res > 1.0 && ratio_size < 1.0)) {
470 xcb_randr_get_output_info_reply_t *op_reply;
471 xcb_randr_get_crtc_info_reply_t *crtc_reply;
472 xcb_randr_get_output_info_cookie_t it =
473 xcb_randr_get_output_info(
xcb->
connection, out, XCB_CURRENT_TIME);
474 op_reply = xcb_randr_get_output_info_reply(
xcb->
connection, it, NULL);
475 if (op_reply->crtc == XCB_NONE) {
479 xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info(
481 crtc_reply = xcb_randr_get_crtc_info_reply(
xcb->
connection, ct, NULL);
487 retv->
x = crtc_reply->x;
488 retv->
y = crtc_reply->y;
489 retv->
w = crtc_reply->width;
490 retv->
h = crtc_reply->height;
492 retv->
mw = op_reply->mm_width;
493 retv->
mh = op_reply->mm_height;
496 char *tname = (
char *)xcb_randr_get_output_info_name(op_reply);
497 int tname_len = xcb_randr_get_output_info_name_length(op_reply);
499 retv->
name = g_malloc0((tname_len + 1) *
sizeof(
char));
500 memcpy(retv->
name, tname, tname_len);
507#if (((XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION) && \
508 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
509 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
518x11_get_monitor_from_randr_monitor(xcb_randr_monitor_info_t *mon) {
520 xcb_generic_error_t *err;
521 xcb_get_atom_name_cookie_t anc =
523 xcb_get_atom_name_reply_t *atom_reply =
526 g_warning(
"Could not get RandR monitor name: X11 error code %d\n",
539 retv->
w = mon->width;
540 retv->
h = mon->height;
543 retv->
mw = mon->width_in_millimeters;
544 retv->
mh = mon->height_in_millimeters;
549 g_strdup_printf(
"%.*s", xcb_get_atom_name_name_length(atom_reply),
550 xcb_get_atom_name_name(atom_reply));
560 xcb_query_extension_cookie_t randr_cookie =
561 xcb_query_extension(
xcb->
connection, strlen(extension), extension);
563 xcb_query_extension_reply_t *randr_reply =
564 xcb_query_extension_reply(
xcb->
connection, randr_cookie, NULL);
566 int present = randr_reply->present;
574 xcb_xinerama_query_screens_cookie_t screens_cookie =
577 xcb_xinerama_query_screens_reply_t *screens_reply =
578 xcb_xinerama_query_screens_reply(
xcb->
connection, screens_cookie, NULL);
580 xcb_xinerama_screen_info_iterator_t screens_iterator =
581 xcb_xinerama_query_screens_screen_info_iterator(screens_reply);
583 for (; screens_iterator.rem > 0;
584 xcb_xinerama_screen_info_next(&screens_iterator)) {
587 w->
x = screens_iterator.data->x_org;
588 w->
y = screens_iterator.data->y_org;
589 w->
w = screens_iterator.data->width;
590 w->
h = screens_iterator.data->height;
598 iter->monitor_id = index++;
612 g_debug(
"Query XINERAMA for monitor layout.");
616 g_debug(
"No RANDR or Xinerama available for getting monitor layout.");
619 g_debug(
"Query RANDR for monitor layout.");
621 g_debug(
"Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION,
622 XCB_RANDR_MINOR_VERSION);
623#if (((XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION) && \
624 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
625 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
626 xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version(
628 xcb_randr_query_version_reply_t *rversion =
629 xcb_randr_query_version_reply(
xcb->
connection, cversion, NULL);
631 g_debug(
"Found randr version: %d.%d", rversion->major_version,
632 rversion->minor_version);
637 xcb_randr_get_monitors_cookie_t t =
639 xcb_randr_get_monitors_reply_t *mreply =
642 xcb_randr_monitor_info_iterator_t iter =
643 xcb_randr_get_monitors_monitors_iterator(mreply);
644 while (iter.rem > 0) {
645 workarea *w = x11_get_monitor_from_randr_monitor(iter.data);
650 xcb_randr_monitor_info_next(&iter);
661 xcb_randr_get_screen_resources_current_reply_t *res_reply;
662 xcb_randr_get_screen_resources_current_cookie_t src;
663 src = xcb_randr_get_screen_resources_current(
xcb->
connection,
665 res_reply = xcb_randr_get_screen_resources_current_reply(
xcb->
connection,
671 xcb_randr_get_screen_resources_current_outputs_length(res_reply);
672 xcb_randr_output_t *ops =
673 xcb_randr_get_screen_resources_current_outputs(res_reply);
676 xcb_randr_get_output_primary_cookie_t pc =
678 xcb_randr_get_output_primary_reply_t *pc_rep =
679 xcb_randr_get_output_primary_reply(
xcb->
connection, pc, NULL);
681 for (
int i = mon_num - 1; i >= 0; i--) {
686 if (pc_rep && pc_rep->output == ops[i]) {
701 iter->monitor_id = index++;
706 int is_term = isatty(fileno(stdout));
707 printf(
"Monitor layout:\n");
709 printf(
"%s ID%s: %d", (is_term) ?
color_bold :
"",
712 printf(
" (primary)");
715 printf(
"%s name%s: %s\n", (is_term) ?
color_bold :
"",
717 printf(
"%s position%s: %d,%d\n", (is_term) ?
color_bold :
"",
719 printf(
"%s size%s: %d,%d\n", (is_term) ?
color_bold :
"",
721 if (iter->mw > 0 && iter->mh > 0) {
722 printf(
"%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
724 iter->mh, iter->w * 25.4 / (
double)iter->mw,
725 iter->h * 25.4 / (
double)iter->mh);
732 GSpawnChildSetupFunc *child_setup,
733 gpointer *user_data) {
734 if (context == NULL) {
738 SnLauncherContext *sncontext;
742 sn_launcher_context_set_name(sncontext, context->
name);
743 sn_launcher_context_set_description(sncontext, context->
description);
744 if (context->
binary != NULL) {
745 sn_launcher_context_set_binary_name(sncontext, context->
binary);
747 if (context->
icon != NULL) {
748 sn_launcher_context_set_icon_name(sncontext, context->
icon);
750 if (context->
app_id != NULL) {
753 if (context->
wmclass != NULL) {
754 sn_launcher_context_set_wmclass(sncontext, context->
wmclass);
757 xcb_get_property_cookie_t c;
758 unsigned int current_desktop = 0;
761 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
763 sn_launcher_context_set_workspace(sncontext, current_desktop);
766 sn_launcher_context_initiate(sncontext,
"rofi", context->
command,
769 *child_setup = (GSpawnChildSetupFunc)sn_launcher_context_setup_child_process;
770 *user_data = sncontext;
790 g_error(
"%s: mon == NULL", __func__);
798 if (
INTERSECT(x, y, iter->x, iter->y, iter->w, iter->h)) {
818 xcb_query_pointer_cookie_t c = xcb_query_pointer(
xcb->
connection, root);
819 xcb_query_pointer_reply_t *r =
832 g_error(
"%s: mon == NULL", __func__);
836 xcb_get_geometry_cookie_t c = xcb_get_geometry(
xcb->
connection,
id);
837 xcb_get_geometry_reply_t *r =
840 xcb_translate_coordinates_cookie_t ct =
841 xcb_translate_coordinates(
xcb->
connection,
id, root, r->x, r->y);
842 xcb_translate_coordinates_reply_t *t =
848 mon->
x = t->dst_x - r->x;
849 mon->
y = t->dst_y - r->y;
863 xcb_get_property_cookie_t awc;
865 g_error(
"%s: mon == NULL", __func__);
869 if (!xcb_ewmh_get_active_window_reply(&
xcb->
ewmh, awc, &active_window,
872 "Failed to get active window, falling back to mouse location (-5).");
875 xcb_query_tree_cookie_t tree_cookie =
877 xcb_query_tree_reply_t *tree_reply =
881 "Failed to get parent window, falling back to mouse location (-5).");
885 xcb_get_geometry_cookie_t c =
887 xcb_get_geometry_reply_t *r =
890 g_debug(
"Failed to get geometry of active window, falling back to mouse "
895 if (tree_reply->parent != r->root) {
896 xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates(
898 xcb_translate_coordinates_reply_t *t =
905 g_debug(
"Failed to get translate position of active window, falling back "
906 "to mouse location (-5).");
916 mon->
x = r->x + r->border_width;
917 mon->
y = r->y + r->border_width;
921 }
else if (mon_id == -4) {
922 g_debug(
"Find monitor at location: %d %d", r->x, r->y);
926 g_debug(
"mon pos: %d %d %d-%d", mon->
x, mon->
y, mon->
w, mon->
h);
933 g_warning(
"XCB structure is uninitialized, monitor info not available.");
939 g_error(
"%s: mon == NULL", __func__);
942 g_debug(
"Monitor id: %d", mon_id);
953 else if (mon_id == -1) {
954 g_debug(
"rofi on current monitor");
956 unsigned int current_desktop = 0;
957 xcb_get_property_cookie_t gcdc;
959 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, gcdc, ¤t_desktop,
961 g_debug(
"Found current desktop: %u", current_desktop);
962 xcb_get_property_cookie_t c =
964 xcb_ewmh_get_desktop_viewport_reply_t vp;
965 if (xcb_ewmh_get_desktop_viewport_reply(&
xcb->
ewmh, c, &vp, NULL)) {
966 g_debug(
"Found %d number of desktops", vp.desktop_viewport_len);
967 if (current_desktop < vp.desktop_viewport_len) {
968 g_debug(
"Found viewport for desktop: %d %d",
969 vp.desktop_viewport[current_desktop].x,
970 vp.desktop_viewport[current_desktop].y);
972 vp.desktop_viewport[current_desktop].y, mon);
973 g_debug(
"Found monitor @: %d %d %dx%d", mon->
x, mon->
y, mon->
w,
975 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
978 g_debug(
"Viewport does not exist for current desktop: %d, falling "
979 "back to mouse location (-5)",
982 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
984 g_debug(
"Failed to get viewport for current desktop: %d, falling back "
985 "to mouse location (-5).",
989 g_debug(
"Failed to get current desktop, falling back to mouse location "
992 }
else if (mon_id == -2 || mon_id == -4) {
998 else if (mon_id == -5) {
1006 g_debug(
"Failed to find monitor, fall back to monitor showing mouse.");
1018 g_error(
"%s: mon == NULL", __func__);
1021 g_debug(
"Monitor active");
1037 g_debug(
"Monitor lookup by name failed: %s",
config.
monitor);
1041 if (iter->primary) {
1051 xcb_drawable_t win = g_ascii_strtoll(
config.
monitor + 4, &end, 0);
1071 g_warning(
"Failed to find selected monitor.");
1088 xcb_get_atom_name_cookie_t cookie;
1089 xcb_get_atom_name_reply_t *reply;
1098 cookie = xcb_get_atom_name(conn, atom);
1099 reply = xcb_get_atom_name_reply(conn, cookie, NULL);
1103 length = xcb_get_atom_name_name_length(reply);
1104 name = xcb_get_atom_name_name(reply);
1106 (*out) = g_strndup(name, length);
1123 xcb_selection_notify_event_t *xse) {
1124 if (xse->property == XCB_ATOM_NONE) {
1125 g_debug(
"Failed to convert selection");
1126 }
else if (xse->property ==
xcb->
ewmh.UTF8_STRING) {
1128 if (text != NULL && text[0] !=
'\0') {
1129 unsigned int dl = strlen(text);
1131 for (
unsigned int i = 0; i < dl; i++) {
1132 if (text[i] ==
'\n') {
1142 g_debug(
"rofi_view_paste: Got unknown atom: %s", out);
1145 g_debug(
"rofi_view_paste: Got unknown, unnamed: %s", out);
1152 NkBindingsMouseButton *button) {
1153 switch (x11_button) {
1155 *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
1158 *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
1161 *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
1164 *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
1167 *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
1175 *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
1181 NkBindingsScrollAxis *axis,
1184 switch (x11_button) {
1189 *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
1195 *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
1206 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"press handler %d", xkpe->detail);
1210 text = nk_bindings_seat_handle_key_with_modmask(
1212 NK_BINDINGS_KEY_STATE_PRESS);
1215 NK_BINDINGS_KEY_STATE_PRESS);
1225 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"release handler %d", xkre->detail);
1228 NK_BINDINGS_KEY_STATE_RELEASE);
1236 if (state == NULL) {
1240 switch (event->response_type & ~0x80) {
1241 case XCB_CLIENT_MESSAGE: {
1242 xcb_client_message_event_t *cme = (xcb_client_message_event_t *)event;
1243 xcb_atom_t atom = cme->data.data32[0];
1245 if (atom ==
netatoms[WM_TAKE_FOCUS]) {
1246 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_NONE, cme->window,
1252 case XCB_DESTROY_NOTIFY: {
1253 xcb_window_t win = ((xcb_destroy_notify_event_t *)event)->window;
1256 window_client_handle_signal(win, FALSE);
1263 case XCB_CREATE_NOTIFY: {
1264 xcb_window_t win = ((xcb_create_notify_event_t *)event)->window;
1267 window_client_handle_signal(win, TRUE);
1275 case XCB_CONFIGURE_NOTIFY: {
1280 case XCB_MOTION_NOTIFY: {
1281 xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *)event;
1282 gboolean button_mask = xme->state & XCB_EVENT_MASK_BUTTON_1_MOTION;
1287 case XCB_BUTTON_PRESS: {
1288 xcb_button_press_event_t *bpe = (xcb_button_press_event_t *)event;
1289 NkBindingsMouseButton button;
1290 NkBindingsScrollAxis axis;
1297 NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time);
1304 case XCB_SELECTION_CLEAR: {
1305 g_debug(
"Selection Clear.");
1308 case XCB_SELECTION_REQUEST: {
1309 g_debug(
"Selection Request.");
1310 xcb_selection_request_event_t *req = (xcb_selection_request_event_t *)event;
1311 if (req->selection ==
netatoms[CLIPBOARD]) {
1312 xcb_atom_t targets[2];
1313 xcb_selection_notify_event_t selection_notify = {
1314 .response_type = XCB_SELECTION_NOTIFY,
1317 .requestor = req->requestor,
1318 .selection = req->selection,
1319 .target = req->target,
1320 .property = XCB_ATOM_NONE};
1324 if (req->target ==
netatoms[UTF8_STRING]) {
1325 g_debug(
"Selection Request UTF-8.");
1327 req->requestor, req->property,
1330 selection_notify.property = req->property;
1331 }
else if (req->target ==
netatoms[TARGETS]) {
1332 g_debug(
"Selection Request Targets.");
1334 targets[0] =
netatoms[UTF8_STRING];
1336 req->requestor, req->property, XCB_ATOM_ATOM, 32,
1338 selection_notify.property = req->property;
1344 req->requestor, XCB_EVENT_MASK_NO_EVENT,
1345 (
const char *)&selection_notify);
1349 case XCB_BUTTON_RELEASE: {
1350 xcb_button_release_event_t *bre = (xcb_button_release_event_t *)event;
1351 NkBindingsMouseButton button;
1356 NK_BINDINGS_BUTTON_STATE_RELEASE,
1368 case XCB_SELECTION_NOTIFY:
1371 case XCB_KEYMAP_NOTIFY: {
1372 xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *)event;
1373 for (gint32 by = 0; by < 31; ++by) {
1374 for (gint8 bi = 0; bi < 7; ++bi) {
1375 if (kne->keys[by] & (1 << bi)) {
1379 NK_BINDINGS_KEY_STATE_PRESSED);
1385 case XCB_KEY_PRESS: {
1386 xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *)event;
1389 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"press key %d to xim", xkpe->detail);
1390 xcb_xim_forward_event(
xcb->im,
xcb->ic, xkpe);
1399 case XCB_KEY_RELEASE: {
1400 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1403 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"release key %d to xim", xkre->detail);
1409 xcb_keysym_t sym = xcb_key_press_lookup_keysym(
xcb->syms, xkre, 0);
1410 if (xcb_is_modifier_key(sym)) {
1411 struct timespec five_millis = {.tv_sec = 0, .tv_nsec = 5000000};
1412 nanosleep(&five_millis, NULL);
1414 xcb_xim_forward_event(
xcb->im,
xcb->ic, xkre);
1430void x11_event_handler_fowarding(G_GNUC_UNUSED xcb_xim_t *im,
1431 G_GNUC_UNUSED xcb_xic_t ic,
1432 xcb_key_press_event_t *event,
1433 G_GNUC_UNUSED
void *user_data) {
1435 if (state == NULL) {
1439 uint8_t type =
event->response_type & ~0x80;
1440 if (type == XCB_KEY_PRESS) {
1442 }
else if (type == XCB_KEY_RELEASE) {
1443 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1451 G_GNUC_UNUSED gpointer user_data) {
1455 g_warning(
"The XCB connection to X server had a fatal error: %d", status);
1457 return G_SOURCE_REMOVE;
1463 return G_SOURCE_CONTINUE;
1468 return G_SOURCE_CONTINUE;
1471 uint8_t type = ev->response_type & ~0x80;
1472 if (type ==
xcb->
xkb.first_event) {
1474 case XCB_XKB_MAP_NOTIFY: {
1475 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1478 struct xkb_state *state = xkb_x11_state_new_from_device(
1481 xkb_keymap_unref(keymap);
1482 xkb_state_unref(state);
1485 case XCB_XKB_STATE_NOTIFY: {
1486 xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *)ev;
1488 ksne->latchedMods, ksne->lockedMods,
1489 ksne->baseGroup, ksne->latchedGroup,
1495 return G_SOURCE_CONTINUE;
1502 return G_SOURCE_CONTINUE;
1509 g_warning(
"Connection has error");
1512 xcb_grab_pointer_cookie_t cc =
1513 xcb_grab_pointer(
xcb->
connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
1514 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE,
1516 xcb_grab_pointer_reply_t *r =
1519 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1525 if ((++i) > iters) {
1528 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1529 nanosleep(&del, NULL);
1538 g_warning(
"Connection has error");
1541 xcb_grab_keyboard_cookie_t cc =
1543 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1544 xcb_grab_keyboard_reply_t *r =
1547 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1553 if ((++i) > iters) {
1556 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1557 nanosleep(&del, NULL);
1572 G_GNUC_UNUSED xcb_connection_t *xdisplay) {
1577 xcb_connection_t *xdisplay) {
1579 g_warning(
"Error trap underflow!");
1583 xcb_flush(xdisplay);
1593 xcb_intern_atom_cookie_t cc = xcb_intern_atom(
1595 xcb_intern_atom_reply_t *r =
1607 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1610 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1611 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1612 xcb_get_property_cookie_t cookie =
1613 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1614 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1615 if (wtitle.strings_len > 0) {
1616 retv = g_strndup(wtitle.strings, wtitle.strings_len);
1618 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1626 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1629 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1630 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1631 xcb_get_property_cookie_t cookie =
1632 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1633 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1634 if (wtitle.strings_len > 0) {
1636 char *str = g_strndup(wtitle.strings, wtitle.strings_len);
1637 g_debug(
"Found window manager: |%s|", str);
1638 if (g_strcmp0(str,
"i3") == 0) {
1644 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1652 char *display_str = (
char *)g_getenv(
"DISPLAY");
1658 xcb_compound_text_init();
1665 g_warning(
"Failed to open display: %s", display_str);
1680#ifndef XCB_IMDKIT_1_0_3_LOWER
1682 xcb_xim_set_use_compound_text(
xcb->im,
true);
1683 xcb_xim_set_use_utf8_string(
xcb->im,
true);
1694 xcb_intern_atom_cookie_t *ac =
1696 xcb_generic_error_t *errors = NULL;
1697 xcb_ewmh_init_atoms_replies(&
xcb->
ewmh, ac, &errors);
1699 g_warning(
"Failed to create EWMH atoms");
1706 if (xkb_x11_setup_xkb_extension(
1708 XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
1709 NULL, NULL, &
xcb->
xkb.first_event, NULL) < 0) {
1710 g_warning(
"cannot setup XKB extension!");
1718 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1719 XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
1721 required_nkn_details = (XCB_XKB_NKN_DETAIL_KEYCODES),
1723 required_map_parts =
1724 (XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
1725 XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1726 XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
1727 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
1729 required_state_details =
1730 (XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
1731 XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
1732 XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK),
1735 static const xcb_xkb_select_events_details_t details = {
1736 .affectNewKeyboard = required_nkn_details,
1737 .newKeyboardDetails = required_nkn_details,
1738 .affectState = required_state_details,
1739 .stateDetails = required_state_details,
1750 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1752 xcb->
xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS);
1753 if (keymap == NULL) {
1754 g_warning(
"Failed to get Keymap for current keyboard device.");
1757 struct xkb_state *state = xkb_x11_state_new_from_device(
1759 if (state == NULL) {
1760 g_warning(
"Failed to get state object for current keyboard device.");
1765 xkb_state_unref(state);
1766 xkb_keymap_unref(keymap);
1772 g_warning(
"Connection has error");
1776 uint32_t val[] = {XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
1778 XCB_CW_EVENT_MASK, val);
1783 g_warning(
"Connection has error");
1792 g_warning(
"Connection has error");
1800 xcb_depth_t *root_depth = NULL;
1801 xcb_depth_iterator_t depth_iter;
1802 for (depth_iter = xcb_screen_allowed_depths_iterator(
xcb->
screen);
1803 depth_iter.rem; xcb_depth_next(&depth_iter)) {
1804 xcb_depth_t *d = depth_iter.data;
1806 xcb_visualtype_iterator_t visual_iter;
1807 for (visual_iter = xcb_depth_visuals_iterator(d); visual_iter.rem;
1808 xcb_visualtype_next(&visual_iter)) {
1809 xcb_visualtype_t *v = visual_iter.data;
1810 if ((v->bits_per_rgb_value == 8) && (d->depth == 32) &&
1811 (v->_class == XCB_VISUAL_CLASS_TRUE_COLOR)) {
1815 if (
xcb->
screen->root_visual == v->visual_id) {
1822 xcb_void_cookie_t c;
1823 xcb_generic_error_t *e;
1825 c = xcb_create_colormap_checked(
xcb->
connection, XCB_COLORMAP_ALLOC_NONE,
1843 xcb_cursor_context_t *ctx;
1852 if (
cursors[i] == XCB_CURSOR_NONE) {
1854 xcb_cursor_load_cursor(ctx,
cursor_names[i].traditional_name);
1858 xcb_cursor_context_free(ctx);
1868 g_warning(
"Failed to grab pointer after %u times. Giving up.",
1870 return G_SOURCE_REMOVE;
1873 return G_SOURCE_REMOVE;
1876 return G_SOURCE_CONTINUE;
1881 g_warning(
"Failed to grab keyboard after %u times. Giving up.",
1884 return G_SOURCE_REMOVE;
1887 return G_SOURCE_REMOVE;
1890 return G_SOURCE_CONTINUE;
1904 if (
find_arg(
"-normal-window") >= 0 ||
find_arg(
"-transient-window") >= 0) {
1907 if (
find_arg(
"-no-lazy-grab") >= 0) {
1909 g_warning(
"Failed to grab keyboard, even after %d uS.", 500 * 1000);
1913 g_warning(
"Failed to grab mouse pointer, even after %d uS.", 100 * 1000);
1939 g_debug(
"Cleaning up XCB and XKB");
1951 xcb_ewmh_connection_wipe(&(
xcb->
ewmh));
1956 xcb_xim_close(
xcb->im);
1957 xcb_xim_destroy(
xcb->im);
1970 const uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
1972 struct MotifWMHints {
1975 uint32_t decorations;
1980 struct MotifWMHints hints;
1981 hints.flags = MWM_HINTS_DECORATIONS;
1982 hints.decorations = 0;
1983 hints.functions = 0;
1984 hints.inputMode = 0;
1987 xcb_atom_t ha =
netatoms[_MOTIF_WM_HINTS];
1988 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, window, ha, ha,
1997 if (
cursors[type] == XCB_CURSOR_NONE) {
2001 xcb_change_window_attributes(
xcb->
connection, window, XCB_CW_CURSOR,
2014 xcb_generic_error_t *error;
2015 xcb_get_input_focus_reply_t *freply;
2016 xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus(
xcb->
connection);
2017 freply = xcb_get_input_focus_reply(
xcb->
connection, fcookie, &error);
2018 if (error != NULL) {
2019 g_warning(
"Could not get input focus (error %d), will revert focus to best "
2027 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT, w,
2037 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT,
struct _display_proxy display_proxy
struct _workarea workarea
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)
char * rofi_force_utf8(const gchar *data, ssize_t length)
xcb_window_t rofi_view_get_window(void)
RofiViewState * rofi_view_get_active(void)
void rofi_view_handle_text(RofiViewState *state, const 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 view_proxy * xcb_view_proxy
const gchar * description
gboolean xserver_i300_workaround
const char * traditional_name
xcb_connection_t * connection
SnLauncheeContext * sncontext
struct _workarea * monitors
xcb_timestamp_t last_timestamp
xcb_ewmh_connection_t ewmh
NkBindingsSeat * bindings_seat
struct _xcb_stuff::@124022330146077150033143244062260205253000356231 xkb
xcb_window_t focus_revert
static display_proxy display_
int xcb_configure_notify_event_t
display_proxy *const xcb_proxy
static int xcb_display_monitor_active(workarea *mon)
static int take_pointer(xcb_window_t w, int iters)
#define RANDR_PREF_MAJOR_VERSION
static int x11_is_extension_present(const char *extension)
static gboolean lazy_grab_pointer(G_GNUC_UNUSED gpointer data)
static void x11_workarea_fix_rotation(workarea *w)
cairo_surface_t * x11_helper_get_screenshot_surface(void)
static void x11_build_monitor_layout(void)
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
static gboolean xcb_display_setup(GMainLoop *main_loop, NkBindings *bindings)
static xcb_pixmap_t get_root_pixmap(xcb_connection_t *c, xcb_screen_t *screen, xcb_atom_t atom)
static void release_pointer(void)
static void xcb_display_set_input_focus(guint w)
static bool get_atom_name(xcb_connection_t *conn, xcb_atom_t atom, char **out)
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)
cairo_surface_t * x11_helper_get_bg_surface(void)
static void x11_lookup_cursors(void)
static gboolean lazy_grab_keyboard(G_GNUC_UNUSED gpointer data)
#define INTERSECT(x, y, x1, y1, w1, h1)
static workarea * x11_get_monitor_from_output(xcb_randr_output_t out)
static void error_trap_pop(G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay)
void x11_set_cursor(xcb_window_t window, X11CursorType type)
static void x11_build_monitor_layout_xinerama(void)
static xcb_visualtype_t * lookup_visual(xcb_screen_t *s, xcb_visualid_t vis)
unsigned int lazy_grab_retry_count_pt
static xcb_visualtype_t * root_visual
static uint32_t * create_kernel(int radius, double deviation, uint32_t *sum2)
char * x11_helper_get_window_manager(void)
static void x11_create_frequently_used_atoms(void)
static int monitor_active_from_id_focused(int mon_id, workarea *mon)
void cairo_image_surface_blur(cairo_surface_t *surface, int radius, double deviation)
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)
static void x11_helper_discover_window_manager(void)
static const struct _view_proxy * xcb_display_view_proxy(void)
static int monitor_get_dimension(int monitor_id, workarea *mon)
static void xcb_display_dump_monitor_layout(void)
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)
static gboolean xcb_display_late_setup(void)
void window_set_atom_prop(xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count)
static void xcb_display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
const char * netatom_names[]
static int monitor_active_from_id(int mon_id, workarea *mon)
static void x11_monitors_free(void)
static guint xcb_display_scale(void)
const struct @106130035057033371167230156075074055250362361056 cursor_names[]
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 void rofi_key_press_event_handler(xcb_key_press_event_t *xkpe, RofiViewState *state)
static void rofi_key_release_event_handler(xcb_key_release_event_t *xkre, G_GNUC_UNUSED RofiViewState *state)
void xcb_stuff_set_clipboard(char *data)
static int error_trap_depth
static void release_keyboard(void)
static void xcb_display_cleanup(void)
static gboolean main_loop_x11_event_handler(xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data)
static void xcb_display_early_cleanup(void)
void x11_disable_decoration(xcb_window_t window)
static void main_loop_x11_event_handler_view(xcb_generic_event_t *event)
xcb_atom_t netatoms[NUM_NETATOMS]
xcb_visualtype_t * visual
static void xcb_display_revert_input_focus(void)
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
struct _xcb_stuff xcb_stuff
@ WM_PANGO_WORKSPACE_NAMES
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP