30#define G_LOG_DOMAIN "X11Helper"
34#include <xcb-imdkit/encoding.h>
35#include <xcb/xcb_keysyms.h>
48#include <xcb/xcb_aux.h>
49#include <xcb/xcb_cursor.h>
50#include <xcb/xcb_ewmh.h>
51#include <xcb/xinerama.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
77#define RANDR_PREF_MAJOR_VERSION 1
79#define RANDR_PREF_MINOR_VERSION 5
82#define INTERSECT(x, y, x1, y1, w1, h1) \
83 ((((x) >= (x1)) && ((x) < (x1 + w1))) && (((y) >= (y1)) && ((y) < (y1 + h1))))
108xcb_colormap_t
map = XCB_COLORMAP_NONE;
129 {
"default",
"left_ptr"}, {
"pointer",
"hand"}, {
"text",
"xterm"}};
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) {
151 int size = 2 * (int)(radius) + 1;
152 uint32_t *kernel = (uint32_t *)(g_malloc(
sizeof(uint32_t) * (size + 1)));
153 double radiusf = fabs(radius) + 1.0;
154 double value = -radius;
158 if (deviation == 0.0) {
159 deviation = sqrt(-(radiusf * radiusf) / (2.0 * log(1.0 / 255.0)));
164 for (i = 0; i < size; i++) {
165 kernel[1 + i] = INT16_MAX / (2.506628275 * deviation) *
166 exp(-((value * value) / (2.0 * (deviation * deviation))));
168 sum += kernel[1 + i];
180 uint32_t *kernel = 0;
181 cairo_format_t format;
182 unsigned int channels;
184 if (cairo_surface_status(surface)) {
188 uint8_t *data = cairo_image_surface_get_data(surface);
189 format = cairo_image_surface_get_format(surface);
190 const int width = cairo_image_surface_get_width(surface);
191 const int height = cairo_image_surface_get_height(surface);
192 const int stride = cairo_image_surface_get_stride(surface);
194 if (format == CAIRO_FORMAT_ARGB32) {
200 horzBlur = (uint32_t *)(g_malloc(
sizeof(uint32_t) * height * stride));
207 uint32_t *horzBlur_ptr = horzBlur;
208 for (
int iY = 0; iY < height; iY++) {
209 const int iYs = iY * stride;
210 for (
int iX = 0; iX < width; iX++) {
215 int offset = (int)(kernel[0]) / -2;
217 for (
int i = 0; i < (int)(kernel[0]); i++) {
220 if (x < 0 || x >= width) {
225 uint8_t *dataPtr = &data[iYs + x * channels];
226 const uint32_t kernip1 = kernel[i + 1];
228 blue += kernip1 * dataPtr[0];
229 green += kernip1 * dataPtr[1];
230 red += kernip1 * dataPtr[2];
231 alpha += kernip1 * dataPtr[3];
235 *horzBlur_ptr++ = blue / sum;
236 *horzBlur_ptr++ = green / sum;
237 *horzBlur_ptr++ = red / sum;
238 *horzBlur_ptr++ = alpha / sum;
244 for (
int iY = 0; iY < height; iY++) {
245 for (
int iX = 0; iX < width; iX++) {
250 int offset = (int)(kernel[0]) / -2;
252 const int iXs = iX * channels;
253 for (
int i = 0; i < (int)(kernel[0]); i++) {
256 if (y < 0 || y >= height) {
261 uint32_t *dataPtr = &horzBlur[y * stride + iXs];
262 const uint32_t kernip1 = kernel[i + 1];
264 blue += kernip1 * dataPtr[0];
265 green += kernip1 * dataPtr[1];
266 red += kernip1 * dataPtr[2];
267 alpha += kernip1 * dataPtr[3];
272 *data++ = blue / sum;
273 *data++ = green / sum;
275 *data++ = alpha / sum;
288 xcb_get_geometry_cookie_t cookie;
289 xcb_get_geometry_reply_t *reply;
292 reply = xcb_get_geometry_reply(
xcb->
connection, cookie, NULL);
297 xcb_get_window_attributes_cookie_t attributesCookie =
299 xcb_get_window_attributes_reply_t *attributes =
300 xcb_get_window_attributes_reply(
xcb->
connection, attributesCookie, NULL);
301 if (attributes == NULL || (attributes->map_state != XCB_MAP_STATE_VIEWABLE)) {
312 cairo_surface_t *t = cairo_xcb_surface_create(
xcb->
connection, window, vt,
313 reply->width, reply->height);
315 if (cairo_surface_status(t) != CAIRO_STATUS_SUCCESS) {
316 cairo_surface_destroy(t);
322 int max = MAX(reply->width, reply->height);
323 double scale = (double)size / max;
325 cairo_surface_t *s2 = cairo_surface_create_similar_image(
326 t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale);
329 if (cairo_surface_status(s2) != CAIRO_STATUS_SUCCESS) {
330 cairo_surface_destroy(t);
334 cairo_t *d = cairo_create(s2);
335 cairo_scale(d, scale, scale);
336 cairo_set_source_surface(d, t, 0, 0);
340 cairo_surface_destroy(t);
355 xcb_get_property_cookie_t cookie;
356 xcb_get_property_reply_t *reply;
357 xcb_pixmap_t rootpixmap = XCB_NONE;
359 cookie = xcb_get_property(c, 0, screen->root, atom, XCB_ATOM_PIXMAP, 0, 1);
361 reply = xcb_get_property_reply(c, cookie, NULL);
364 if (xcb_get_property_value_length(reply) ==
sizeof(xcb_pixmap_t)) {
365 memcpy(&rootpixmap, xcb_get_property_value(reply),
sizeof(xcb_pixmap_t));
376 if (pm == XCB_NONE) {
388 xcb_get_property_cookie_t c = xcb_get_property(
389 xcb->
connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
390 xcb_get_property_reply_t *r =
393 if (xcb_get_property_value_length(r) > 0) {
395 if (r->type ==
netatoms[UTF8_STRING]) {
396 str = g_strndup(xcb_get_property_value(r),
397 xcb_get_property_value_length(r));
398 }
else if (r->type ==
netatoms[STRING]) {
400 xcb_get_property_value_length(r));
402 str = g_strdup(
"Invalid encoding.");
415 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, w, prop,
416 XCB_ATOM_ATOM, 32,
count, atoms);
445 double ratio_res = w->
w / (double)w->
h;
446 double ratio_size = w->
mw / (double)w->
mh;
448 if ((ratio_res < 1.0 && ratio_size > 1.0) ||
449 (ratio_res > 1.0 && ratio_size < 1.0)) {
460 xcb_randr_get_output_info_reply_t *op_reply;
461 xcb_randr_get_crtc_info_reply_t *crtc_reply;
462 xcb_randr_get_output_info_cookie_t it =
463 xcb_randr_get_output_info(
xcb->
connection, out, XCB_CURRENT_TIME);
464 op_reply = xcb_randr_get_output_info_reply(
xcb->
connection, it, NULL);
465 if (op_reply->crtc == XCB_NONE) {
469 xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info(
471 crtc_reply = xcb_randr_get_crtc_info_reply(
xcb->
connection, ct, NULL);
477 retv->
x = crtc_reply->x;
478 retv->
y = crtc_reply->y;
479 retv->
w = crtc_reply->width;
480 retv->
h = crtc_reply->height;
482 retv->
mw = op_reply->mm_width;
483 retv->
mh = op_reply->mm_height;
486 char *tname = (
char *)xcb_randr_get_output_info_name(op_reply);
487 int tname_len = xcb_randr_get_output_info_name_length(op_reply);
489 retv->
name = g_malloc0((tname_len + 1) *
sizeof(
char));
490 memcpy(retv->
name, tname, tname_len);
497#if (((XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION) && \
498 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
499 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
508x11_get_monitor_from_randr_monitor(xcb_randr_monitor_info_t *
mon) {
510 xcb_generic_error_t *err;
511 xcb_get_atom_name_cookie_t anc =
513 xcb_get_atom_name_reply_t *atom_reply =
516 g_warning(
"Could not get RandR monitor name: X11 error code %d\n",
529 retv->
w =
mon->width;
530 retv->
h =
mon->height;
533 retv->
mw =
mon->width_in_millimeters;
534 retv->
mh =
mon->height_in_millimeters;
539 g_strdup_printf(
"%.*s", xcb_get_atom_name_name_length(atom_reply),
540 xcb_get_atom_name_name(atom_reply));
550 xcb_query_extension_cookie_t randr_cookie =
551 xcb_query_extension(
xcb->
connection, strlen(extension), extension);
553 xcb_query_extension_reply_t *randr_reply =
554 xcb_query_extension_reply(
xcb->
connection, randr_cookie, NULL);
556 int present = randr_reply->present;
564 xcb_xinerama_query_screens_cookie_t screens_cookie =
567 xcb_xinerama_query_screens_reply_t *screens_reply =
568 xcb_xinerama_query_screens_reply(
xcb->
connection, screens_cookie, NULL);
570 xcb_xinerama_screen_info_iterator_t screens_iterator =
571 xcb_xinerama_query_screens_screen_info_iterator(screens_reply);
573 for (; screens_iterator.rem > 0;
574 xcb_xinerama_screen_info_next(&screens_iterator)) {
577 w->
x = screens_iterator.data->x_org;
578 w->
y = screens_iterator.data->y_org;
579 w->
w = screens_iterator.data->width;
580 w->
h = screens_iterator.data->height;
588 iter->monitor_id = index++;
602 g_debug(
"Query XINERAMA for monitor layout.");
606 g_debug(
"No RANDR or Xinerama available for getting monitor layout.");
609 g_debug(
"Query RANDR for monitor layout.");
611 g_debug(
"Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION,
612 XCB_RANDR_MINOR_VERSION);
613#if (((XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION) && \
614 (XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION)) || \
615 XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION)
616 xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version(
618 xcb_randr_query_version_reply_t *rversion =
619 xcb_randr_query_version_reply(
xcb->
connection, cversion, NULL);
621 g_debug(
"Found randr version: %d.%d", rversion->major_version,
622 rversion->minor_version);
627 xcb_randr_get_monitors_cookie_t t =
629 xcb_randr_get_monitors_reply_t *mreply =
632 xcb_randr_monitor_info_iterator_t iter =
633 xcb_randr_get_monitors_monitors_iterator(mreply);
634 while (iter.rem > 0) {
635 workarea *w = x11_get_monitor_from_randr_monitor(iter.data);
640 xcb_randr_monitor_info_next(&iter);
651 xcb_randr_get_screen_resources_current_reply_t *res_reply;
652 xcb_randr_get_screen_resources_current_cookie_t src;
653 src = xcb_randr_get_screen_resources_current(
xcb->
connection,
655 res_reply = xcb_randr_get_screen_resources_current_reply(
xcb->
connection,
661 xcb_randr_get_screen_resources_current_outputs_length(res_reply);
662 xcb_randr_output_t *ops =
663 xcb_randr_get_screen_resources_current_outputs(res_reply);
666 xcb_randr_get_output_primary_cookie_t pc =
668 xcb_randr_get_output_primary_reply_t *pc_rep =
669 xcb_randr_get_output_primary_reply(
xcb->
connection, pc, NULL);
671 for (
int i = mon_num - 1; i >= 0; i--) {
676 if (pc_rep && pc_rep->output == ops[i]) {
691 iter->monitor_id = index++;
696 int is_term = isatty(fileno(stdout));
697 printf(
"Monitor layout:\n");
699 printf(
"%s ID%s: %d", (is_term) ?
color_bold :
"",
702 printf(
" (primary)");
705 printf(
"%s name%s: %s\n", (is_term) ?
color_bold :
"",
707 printf(
"%s position%s: %d,%d\n", (is_term) ?
color_bold :
"",
709 printf(
"%s size%s: %d,%d\n", (is_term) ?
color_bold :
"",
711 if (iter->mw > 0 && iter->mh > 0) {
712 printf(
"%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
714 iter->mh, iter->w * 25.4 / (
double)iter->mw,
715 iter->h * 25.4 / (
double)iter->mh);
722 GSpawnChildSetupFunc *child_setup,
723 gpointer *user_data) {
724 if (context == NULL) {
728 SnLauncherContext *sncontext;
732 sn_launcher_context_set_name(sncontext, context->
name);
733 sn_launcher_context_set_description(sncontext, context->
description);
734 if (context->
binary != NULL) {
735 sn_launcher_context_set_binary_name(sncontext, context->
binary);
737 if (context->
icon != NULL) {
738 sn_launcher_context_set_icon_name(sncontext, context->
icon);
740 if (context->
app_id != NULL) {
743 if (context->
wmclass != NULL) {
744 sn_launcher_context_set_wmclass(sncontext, context->
wmclass);
747 xcb_get_property_cookie_t c;
748 unsigned int current_desktop = 0;
751 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
753 sn_launcher_context_set_workspace(sncontext, current_desktop);
756 sn_launcher_context_initiate(sncontext,
"rofi", context->
command,
759 *child_setup = (GSpawnChildSetupFunc)sn_launcher_context_setup_child_process;
760 *user_data = sncontext;
780 g_error(
"%s: mon == NULL", __func__);
788 if (
INTERSECT(x, y, iter->x, iter->y, iter->w, iter->h)) {
808 xcb_query_pointer_cookie_t c = xcb_query_pointer(
xcb->
connection, root);
809 xcb_query_pointer_reply_t *r =
823 g_error(
"%s: mon == NULL", __func__);
827 xcb_get_geometry_cookie_t c = xcb_get_geometry(
xcb->
connection,
id);
828 xcb_get_geometry_reply_t *r =
831 xcb_translate_coordinates_cookie_t ct =
832 xcb_translate_coordinates(
xcb->
connection,
id, root, r->x, r->y);
833 xcb_translate_coordinates_reply_t *t =
839 mon->
x = t->dst_x - r->x;
840 mon->
y = t->dst_y - r->y;
853 xcb_window_t active_window;
854 xcb_get_property_cookie_t awc;
856 g_error(
"%s: mon == NULL", __func__);
860 if (!xcb_ewmh_get_active_window_reply(&
xcb->
ewmh, awc, &active_window,
863 "Failed to get active window, falling back to mouse location (-5).");
866 xcb_query_tree_cookie_t tree_cookie =
868 xcb_query_tree_reply_t *tree_reply =
872 "Failed to get parent window, falling back to mouse location (-5).");
876 xcb_get_geometry_cookie_t c =
878 xcb_get_geometry_reply_t *r =
881 g_debug(
"Failed to get geometry of active window, falling back to mouse "
886 if (tree_reply->parent != r->root) {
887 xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates(
889 xcb_translate_coordinates_reply_t *t =
896 g_debug(
"Failed to get translate position of active window, falling back "
897 "to mouse location (-5).");
907 mon->
x = r->x + r->border_width;
908 mon->
y = r->y + r->border_width;
912 }
else if (mon_id == -4) {
913 g_debug(
"Find monitor at location: %d %d", r->x, r->y);
926 g_error(
"%s: mon == NULL", __func__);
929 g_debug(
"Monitor id: %d", mon_id);
940 else if (mon_id == -1) {
941 g_debug(
"rofi on current monitor");
943 unsigned int current_desktop = 0;
944 xcb_get_property_cookie_t gcdc;
946 if (xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, gcdc, ¤t_desktop,
948 g_debug(
"Found current desktop: %u", current_desktop);
949 xcb_get_property_cookie_t c =
951 xcb_ewmh_get_desktop_viewport_reply_t vp;
952 if (xcb_ewmh_get_desktop_viewport_reply(&
xcb->
ewmh, c, &vp, NULL)) {
953 g_debug(
"Found %d number of desktops", vp.desktop_viewport_len);
954 if (current_desktop < vp.desktop_viewport_len) {
955 g_debug(
"Found viewport for desktop: %d %d",
956 vp.desktop_viewport[current_desktop].x,
957 vp.desktop_viewport[current_desktop].y);
959 vp.desktop_viewport[current_desktop].y,
mon);
960 g_debug(
"Found monitor @: %d %d %dx%d",
mon->
x,
mon->
y,
mon->
w,
962 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
965 g_debug(
"Viewport does not exist for current desktop: %d, falling "
966 "back to mouse location (-5)",
968 xcb_ewmh_get_desktop_viewport_reply_wipe(&vp);
970 g_debug(
"Failed to get viewport for current desktop: %d, falling back "
971 "to mouse location (-5).",
975 g_debug(
"Failed to get current desktop, falling back to mouse location "
978 }
else if (mon_id == -2 || mon_id == -4) {
984 else if (mon_id == -5) {
992 g_debug(
"Failed to find monitor, fall back to monitor showing mouse.");
1006 g_error(
"%s: mon == NULL", __func__);
1009 g_debug(
"Monitor active");
1025 g_debug(
"Monitor lookup by name failed: %s",
config.
monitor);
1029 if (iter->primary) {
1039 xcb_drawable_t win = g_ascii_strtoll(
config.
monitor + 4, &end, 0);
1059 g_warning(
"Failed to find selected monitor.");
1076 xcb_get_atom_name_cookie_t cookie;
1077 xcb_get_atom_name_reply_t *reply;
1086 cookie = xcb_get_atom_name(conn, atom);
1087 reply = xcb_get_atom_name_reply(conn, cookie, NULL);
1091 length = xcb_get_atom_name_name_length(reply);
1092 name = xcb_get_atom_name_name(reply);
1094 (*out) = g_strndup(name, length);
1111 xcb_selection_notify_event_t *xse) {
1112 if (xse->property == XCB_ATOM_NONE) {
1113 g_debug(
"Failed to convert selection");
1114 }
else if (xse->property ==
xcb->
ewmh.UTF8_STRING) {
1116 if (text != NULL && text[0] !=
'\0') {
1117 unsigned int dl = strlen(text);
1119 for (
unsigned int i = 0; i < dl; i++) {
1120 if (text[i] ==
'\n') {
1130 g_debug(
"rofi_view_paste: Got unknown atom: %s", out);
1133 g_debug(
"rofi_view_paste: Got unknown, unnamed: %s", out);
1140 NkBindingsMouseButton *button) {
1141 switch (x11_button) {
1143 *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
1146 *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
1149 *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
1152 *button = NK_BINDINGS_MOUSE_BUTTON_BACK;
1155 *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
1163 *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
1169 NkBindingsScrollAxis *axis,
1172 switch (x11_button) {
1177 *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
1183 *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
1194 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"press handler %d", xkpe->detail);
1198 text = nk_bindings_seat_handle_key_with_modmask(
1200 NK_BINDINGS_KEY_STATE_PRESS);
1203 NK_BINDINGS_KEY_STATE_PRESS);
1213 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"release handler %d", xkre->detail);
1216 NK_BINDINGS_KEY_STATE_RELEASE);
1224 if (state == NULL) {
1228 switch (event->response_type & ~0x80) {
1229 case XCB_CLIENT_MESSAGE: {
1230 xcb_client_message_event_t *cme = (xcb_client_message_event_t *)event;
1231 xcb_atom_t atom = cme->data.data32[0];
1232 xcb_timestamp_t
time = cme->data.data32[1];
1233 if (atom ==
netatoms[WM_TAKE_FOCUS]) {
1234 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_NONE, cme->window,
1240 case XCB_DESTROY_NOTIFY: {
1241 xcb_window_t win = ((xcb_destroy_notify_event_t *)event)->window;
1244 window_client_handle_signal(win, FALSE);
1251 case XCB_CREATE_NOTIFY: {
1252 xcb_window_t win = ((xcb_create_notify_event_t *)event)->window;
1255 window_client_handle_signal(win, TRUE);
1263 case XCB_CONFIGURE_NOTIFY: {
1264 xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *)event;
1268 case XCB_MOTION_NOTIFY: {
1269 xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *)event;
1270 gboolean button_mask = xme->state & XCB_EVENT_MASK_BUTTON_1_MOTION;
1275 case XCB_BUTTON_PRESS: {
1276 xcb_button_press_event_t *bpe = (xcb_button_press_event_t *)event;
1277 NkBindingsMouseButton button;
1278 NkBindingsScrollAxis axis;
1285 NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time);
1292 case XCB_SELECTION_CLEAR: {
1293 g_debug(
"Selection Clear.");
1296 case XCB_SELECTION_REQUEST: {
1297 g_debug(
"Selection Request.");
1298 xcb_selection_request_event_t *req = (xcb_selection_request_event_t *)event;
1299 if (req->selection ==
netatoms[CLIPBOARD]) {
1300 xcb_atom_t targets[2];
1301 xcb_selection_notify_event_t selection_notify = {
1302 .response_type = XCB_SELECTION_NOTIFY,
1305 .requestor = req->requestor,
1306 .selection = req->selection,
1307 .target = req->target,
1308 .property = XCB_ATOM_NONE,
1313 if (req->target ==
netatoms[UTF8_STRING]) {
1314 g_debug(
"Selection Request UTF-8.");
1316 req->requestor, req->property,
1319 selection_notify.property = req->property;
1320 }
else if (req->target ==
netatoms[TARGETS]) {
1321 g_debug(
"Selection Request Targets.");
1323 targets[0] =
netatoms[UTF8_STRING];
1325 req->requestor, req->property, XCB_ATOM_ATOM, 32,
1327 selection_notify.property = req->property;
1333 req->requestor, XCB_EVENT_MASK_NO_EVENT,
1334 (
const char *)&selection_notify);
1338 case XCB_BUTTON_RELEASE: {
1339 xcb_button_release_event_t *bre = (xcb_button_release_event_t *)event;
1340 NkBindingsMouseButton button;
1345 NK_BINDINGS_BUTTON_STATE_RELEASE,
1357 case XCB_SELECTION_NOTIFY:
1360 case XCB_KEYMAP_NOTIFY: {
1361 xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *)event;
1362 for (gint32 by = 0; by < 31; ++by) {
1363 for (gint8 bi = 0; bi < 7; ++bi) {
1364 if (kne->keys[by] & (1 << bi)) {
1368 NK_BINDINGS_KEY_STATE_PRESSED);
1374 case XCB_KEY_PRESS: {
1375 xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *)event;
1378 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"press key %d to xim", xkpe->detail);
1379 xcb_xim_forward_event(
xcb->im,
xcb->ic, xkpe);
1388 case XCB_KEY_RELEASE: {
1389 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1392 g_log(
"IMDKit", G_LOG_LEVEL_DEBUG,
"release key %d to xim", xkre->detail);
1398 xcb_keysym_t sym = xcb_key_press_lookup_keysym(
xcb->syms, xkre, 0);
1399 if (xcb_is_modifier_key(sym)) {
1400 struct timespec five_millis = {.tv_sec = 0, .tv_nsec = 5000000};
1401 nanosleep(&five_millis, NULL);
1403 xcb_xim_forward_event(
xcb->im,
xcb->ic, xkre);
1419void x11_event_handler_fowarding(G_GNUC_UNUSED xcb_xim_t *im,
1420 G_GNUC_UNUSED xcb_xic_t ic,
1421 xcb_key_press_event_t *event,
1422 G_GNUC_UNUSED
void *user_data) {
1424 if (state == NULL) {
1428 uint8_t type =
event->response_type & ~0x80;
1429 if (type == XCB_KEY_PRESS) {
1431 }
else if (type == XCB_KEY_RELEASE) {
1432 xcb_key_release_event_t *xkre = (xcb_key_release_event_t *)event;
1440 G_GNUC_UNUSED gpointer user_data) {
1444 g_warning(
"The XCB connection to X server had a fatal error: %d", status);
1446 return G_SOURCE_REMOVE;
1452 return G_SOURCE_CONTINUE;
1456 if (
xcb->im && xcb_xim_filter_event(
xcb->im, ev))
1457 return G_SOURCE_CONTINUE;
1460 uint8_t type = ev->response_type & ~0x80;
1463 case XCB_XKB_MAP_NOTIFY: {
1464 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1467 struct xkb_state *state = xkb_x11_state_new_from_device(
1470 xkb_keymap_unref(keymap);
1471 xkb_state_unref(state);
1474 case XCB_XKB_STATE_NOTIFY: {
1475 xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *)ev;
1477 ksne->latchedMods, ksne->lockedMods,
1478 ksne->baseGroup, ksne->latchedGroup,
1484 return G_SOURCE_CONTINUE;
1491 return G_SOURCE_CONTINUE;
1499 xcb_generic_error_t *error;
1500 xcb_get_input_focus_reply_t *freply;
1501 xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus(
xcb->
connection);
1502 freply = xcb_get_input_focus_reply(
xcb->
connection, fcookie, &error);
1503 if (error != NULL) {
1504 g_warning(
"Could not get input focus (error %d), will revert focus to best "
1512 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT, w,
1522 xcb_set_input_focus(
xcb->
connection, XCB_INPUT_FOCUS_POINTER_ROOT,
1531 g_warning(
"Connection has error");
1534 xcb_grab_pointer_cookie_t cc =
1535 xcb_grab_pointer(
xcb->
connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
1536 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE,
1538 xcb_grab_pointer_reply_t *r =
1541 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1547 if ((++i) > iters) {
1550 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1551 nanosleep(&del, NULL);
1560 g_warning(
"Connection has error");
1563 xcb_grab_keyboard_cookie_t cc =
1565 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1566 xcb_grab_keyboard_reply_t *r =
1569 if (r->status == XCB_GRAB_STATUS_SUCCESS) {
1575 if ((++i) > iters) {
1578 struct timespec del = {.tv_sec = 0, .tv_nsec = 1000000};
1579 nanosleep(&del, NULL);
1594 G_GNUC_UNUSED xcb_connection_t *xdisplay) {
1599 xcb_connection_t *xdisplay) {
1601 g_warning(
"Error trap underflow!");
1605 xcb_flush(xdisplay);
1615 xcb_intern_atom_cookie_t cc = xcb_intern_atom(
1617 xcb_intern_atom_reply_t *r =
1628 xcb_window_t wm_win = 0;
1629 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1632 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1633 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1634 xcb_get_property_cookie_t cookie =
1635 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1636 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1637 if (wtitle.strings_len > 0) {
1638 retv = g_strndup(wtitle.strings, wtitle.strings_len);
1640 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1647 xcb_window_t wm_win = 0;
1648 xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked(
1651 if (xcb_ewmh_get_supporting_wm_check_reply(&
xcb->
ewmh, cc, &wm_win, NULL)) {
1652 xcb_ewmh_get_utf8_strings_reply_t wtitle;
1653 xcb_get_property_cookie_t cookie =
1654 xcb_ewmh_get_wm_name_unchecked(&(
xcb->
ewmh), wm_win);
1655 if (xcb_ewmh_get_wm_name_reply(&(
xcb->
ewmh), cookie, &wtitle, (
void *)0)) {
1656 if (wtitle.strings_len > 0) {
1658 char *str = g_strndup(wtitle.strings, wtitle.strings_len);
1659 g_debug(
"Found window manager: |%s|", str);
1660 if (g_strcmp0(str,
"i3") == 0) {
1666 xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
1674 char *display_str = (
char *)g_getenv(
"DISPLAY");
1679 xcb_compound_text_init();
1685 g_warning(
"Failed to open display: %s", display_str);
1695#ifndef XCB_IMDKIT_1_0_3_LOWER
1696 xcb_xim_set_use_compound_text(
xcb->im,
true);
1697 xcb_xim_set_use_utf8_string(
xcb->im,
true);
1707 xcb_intern_atom_cookie_t *ac =
1709 xcb_generic_error_t *errors = NULL;
1710 xcb_ewmh_init_atoms_replies(&
xcb->
ewmh, ac, &errors);
1712 g_warning(
"Failed to create EWMH atoms");
1719 if (xkb_x11_setup_xkb_extension(
1721 XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
1723 g_warning(
"cannot setup XKB extension!");
1731 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
1732 XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
1734 required_nkn_details = (XCB_XKB_NKN_DETAIL_KEYCODES),
1736 required_map_parts =
1737 (XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
1738 XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
1739 XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
1740 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
1742 required_state_details =
1743 (XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
1744 XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
1745 XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK),
1748 static const xcb_xkb_select_events_details_t details = {
1749 .affectNewKeyboard = required_nkn_details,
1750 .newKeyboardDetails = required_nkn_details,
1751 .affectState = required_state_details,
1752 .stateDetails = required_state_details,
1763 struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device(
1766 if (keymap == NULL) {
1767 g_warning(
"Failed to get Keymap for current keyboard device.");
1770 struct xkb_state *state = xkb_x11_state_new_from_device(
1772 if (state == NULL) {
1773 g_warning(
"Failed to get state object for current keyboard device.");
1778 xkb_state_unref(state);
1779 xkb_keymap_unref(keymap);
1785 g_warning(
"Connection has error");
1789 uint32_t val[] = {XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
1792 XCB_CW_EVENT_MASK, val);
1798 g_warning(
"Connection has error");
1807 g_warning(
"Connection has error");
1815 xcb_depth_t *root_depth = NULL;
1816 xcb_depth_iterator_t depth_iter;
1817 for (depth_iter = xcb_screen_allowed_depths_iterator(
xcb->
screen);
1818 depth_iter.rem; xcb_depth_next(&depth_iter)) {
1819 xcb_depth_t *d = depth_iter.data;
1821 xcb_visualtype_iterator_t visual_iter;
1822 for (visual_iter = xcb_depth_visuals_iterator(d); visual_iter.rem;
1823 xcb_visualtype_next(&visual_iter)) {
1824 xcb_visualtype_t *v = visual_iter.data;
1825 if ((v->bits_per_rgb_value == 8) && (d->depth == 32) &&
1826 (v->_class == XCB_VISUAL_CLASS_TRUE_COLOR)) {
1830 if (
xcb->
screen->root_visual == v->visual_id) {
1837 xcb_void_cookie_t c;
1838 xcb_generic_error_t *e;
1840 c = xcb_create_colormap_checked(
xcb->
connection, XCB_COLORMAP_ALLOC_NONE,
1858 xcb_cursor_context_t *ctx;
1867 if (
cursors[i] == XCB_CURSOR_NONE) {
1873 xcb_cursor_context_free(ctx);
1883 g_warning(
"Failed to grab pointer after %u times. Giving up.",
1885 return G_SOURCE_REMOVE;
1888 return G_SOURCE_REMOVE;
1891 return G_SOURCE_CONTINUE;
1896 g_warning(
"Failed to grab keyboard after %u times. Giving up.",
1899 return G_SOURCE_REMOVE;
1902 return G_SOURCE_REMOVE;
1905 return G_SOURCE_CONTINUE;
1919 if (
find_arg(
"-normal-window") >= 0 ||
find_arg(
"-transient-window") >= 0) {
1922 if (
find_arg(
"-no-lazy-grab") >= 0) {
1924 g_warning(
"Failed to grab keyboard, even after %d uS.", 500 * 1000);
1928 g_warning(
"Failed to grab mouse pointer, even after %d uS.", 100 * 1000);
1954 g_debug(
"Cleaning up XCB and XKB");
1966 xcb_ewmh_connection_wipe(&(
xcb->
ewmh));
1970 xcb_xim_close(
xcb->im);
1971 xcb_xim_destroy(
xcb->im);
1983 const uint32_t MWM_HINTS_DECORATIONS = (1 << 1);
1985 struct MotifWMHints {
1988 uint32_t decorations;
1993 struct MotifWMHints hints;
1994 hints.flags = MWM_HINTS_DECORATIONS;
1995 hints.decorations = 0;
1996 hints.functions = 0;
1997 hints.inputMode = 0;
2000 xcb_atom_t ha =
netatoms[_MOTIF_WM_HINTS];
2001 xcb_change_property(
xcb->
connection, XCB_PROP_MODE_REPLACE, window, ha, ha,
2010 if (
cursors[type] == XCB_CURSOR_NONE) {
2014 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
gboolean xserver_i300_workaround
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[]
#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)
static void x11_workarea_fix_rotation(workarea *w)
void display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)
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)
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)
static bool get_atom_name(xcb_connection_t *conn, xcb_atom_t atom, char **out)
struct _xcb_stuff xcb_int
static uint32_t * create_kernel(double radius, double deviation, uint32_t *sum2)
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)
cairo_surface_t * x11_helper_get_bg_surface(void)
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)
#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
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)
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 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 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 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)
xcb_atom_t netatoms[NUM_NETATOMS]
xcb_visualtype_t * visual
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
@ WM_PANGO_WORKSPACE_NAMES
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP