2 #define I3__FILE__ "handlers.c"
17 #include <xcb/randr.h>
18 #include <X11/XKBlib.h>
19 #define SN_API_NOT_YET_FROZEN 1
20 #include <libsn/sn-monitor.h>
42 event->added = time(NULL);
53 time_t now = time(NULL);
55 if ((now - event->
added) > 5) {
60 }
else event =
SLIST_NEXT(event, ignore_events);
96 ELOG(
"ERROR: No such screen\n");
100 if (output->
con == NULL) {
101 ELOG(
"ERROR: The screen is not recognized by i3 (no container associated)\n");
127 DLOG(
"enter_notify for %08x, mode = %d, detail %d, serial %d\n",
128 event->event, event->mode, event->detail, event->sequence);
129 DLOG(
"coordinates %d, %d\n", event->event_x, event->event_y);
130 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
131 DLOG(
"This was not a normal notify, ignoring\n");
137 DLOG(
"Event ignored\n");
141 bool enter_child =
false;
150 DLOG(
"Getting screen at %d x %d\n", event->root_x, event->root_y);
156 DLOG(
"Ignoring, this is a dock client\n");
162 if (layout == L_DEFAULT) {
166 LOG(
"using child %p / %s instead!\n", child, child->
name);
173 if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
177 DLOG(
"enter_notify for a client on a different workspace but the same screen, ignoring\n");
211 if (event->child != 0)
216 DLOG(
"MotionNotify for an unknown container, checking if it crosses screen boundaries.\n");
224 if (con->
layout != L_DEFAULT)
251 if (event->request != XCB_MAPPING_KEYBOARD &&
252 event->request != XCB_MAPPING_MODIFIER)
255 DLOG(
"Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
256 xcb_refresh_keyboard_mapping(
keysyms, event);
272 xcb_get_window_attributes_cookie_t cookie;
274 cookie = xcb_get_window_attributes_unchecked(
conn, event->window);
276 DLOG(
"window = 0x%08x, serial is %d.\n", event->window, event->sequence);
295 DLOG(
"window 0x%08x wants to be at %dx%d with %dx%d\n",
296 event->window, event->x, event->y, event->width, event->height);
301 DLOG(
"Configure request for unmanaged window, can do that.\n");
306 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
307 if (event->value_mask & mask_member) { \
308 mask |= mask_member; \
309 values[c++] = event->event_member; \
321 xcb_configure_window(
conn, event->window, mask, values);
327 DLOG(
"Configure request!\n");
347 bsr.
y += deco_height;
348 bsr.
height -= deco_height;
354 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
355 newrect.
x =
event->x + (-1) * bsr.
x;
356 DLOG(
"proposed x = %d, new x is %d\n", event->x, newrect.
x);
358 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
359 newrect.
y =
event->y + (-1) * bsr.
y;
360 DLOG(
"proposed y = %d, new y is %d\n", event->y, newrect.
y);
362 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
363 newrect.
width =
event->width + (-1) * bsr.
width;
365 DLOG(
"proposed width = %d, new width is %d (x11 border %d)\n",
368 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
371 DLOG(
"proposed height = %d, new height is %d (x11 border %d)\n",
375 DLOG(
"Container is a floating leaf node, will do that.\n");
382 DLOG(
"Dock window, only height reconfiguration allowed\n");
383 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
384 DLOG(
"Height given, changing\n");
402 int handle_configure_event(
void *prophs, xcb_connection_t *
conn, xcb_configure_notify_event_t *event) {
403 DLOG(
"configure_event, sequence %d\n", event->sequence);
418 DLOG(
"RandR screen change\n");
424 ipc_send_event(
"output", I3_IPC_EVENT_OUTPUT,
"{\"change\":\"unspecified\"}");
435 DLOG(
"UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
436 xcb_get_input_focus_cookie_t cookie;
443 LOG(
"Not a managed window, ignoring UnmapNotify event\n");
450 cookie = xcb_get_input_focus(
conn);
451 DLOG(
"ignore_unmap = %d for frame of container %p\n", con->
ignore_unmap, con);
456 cookie = xcb_get_input_focus(
conn);
489 free(xcb_get_input_focus_reply(
conn, cookie, NULL));
502 DLOG(
"destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
504 xcb_unmap_notify_event_t unmap;
505 unmap.sequence =
event->sequence;
506 unmap.event =
event->event;
507 unmap.window =
event->window;
517 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
535 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
552 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
567 static int handle_windowclass_change(
void *data, xcb_connection_t *
conn, uint8_t
state,
568 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
586 DLOG(
"window = %08x\n", event->window);
589 LOG(
"expose event for unknown window, ignoring\n");
598 event->x, event->y, event->x, event->y,
599 event->width, event->height);
612 if (sn_xcb_display_process_event(
sndisplay, (xcb_generic_event_t*)event))
615 LOG(
"ClientMessage for window 0x%08x\n", event->window);
616 if (event->type == A__NET_WM_STATE) {
617 if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
618 DLOG(
"atom in clientmessage is %d, fullscreen is %d\n",
619 event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
620 DLOG(
"not about fullscreen atom\n");
626 DLOG(
"Could not get window for client message\n");
637 DLOG(
"toggling fullscreen\n");
642 }
else if (event->type == A__NET_ACTIVE_WINDOW) {
643 DLOG(
"_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
646 DLOG(
"Could not get window for client message\n");
652 DLOG(
"Workspace not visible, ignoring _NET_ACTIVE_WINDOW\n");
661 }
else if (event->type == A_I3_SYNC) {
662 xcb_window_t window =
event->data.data32[0];
663 uint32_t rnd =
event->data.data32[1];
664 DLOG(
"[i3 sync protocol] Sending random value %d back to X11 window 0x%08x\n", rnd, window);
667 xcb_client_message_event_t *ev = reply;
669 ev->response_type = XCB_CLIENT_MESSAGE;
671 ev->type = A_I3_SYNC;
673 ev->data.data32[0] = window;
674 ev->data.data32[1] = rnd;
676 xcb_send_event(conn,
false, window, XCB_EVENT_MASK_NO_EVENT, (
char*)ev);
680 DLOG(
"unhandled clientmessage\n");
686 int handle_window_type(
void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
687 xcb_atom_t atom, xcb_get_property_reply_t *property) {
690 ELOG(
"_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
703 xcb_atom_t name, xcb_get_property_reply_t *reply) {
706 DLOG(
"Received WM_NORMAL_HINTS for unknown client\n");
710 xcb_size_hints_t size_hints;
722 DLOG(
"Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
725 bool changed =
false;
727 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
732 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
739 DLOG(
"resize increments changed\n");
742 int base_width = 0, base_height = 0;
748 base_width = size_hints.base_width;
749 base_height = size_hints.base_height;
752 base_width = size_hints.min_width;
753 base_height = size_hints.min_height;
760 DLOG(
"client's base_height changed to %d\n", base_height);
761 DLOG(
"client's base_width changed to %d\n", base_width);
767 (size_hints.min_aspect_num <= 0) ||
768 (size_hints.min_aspect_den <= 0)) {
769 goto render_and_return;
776 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
777 double max_aspect = (
double)size_hints.max_aspect_num / size_hints.min_aspect_den;
779 DLOG(
"Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
780 DLOG(
"width = %f, height = %f\n", width, height);
783 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
784 goto render_and_return;
787 if ((width / height) < min_aspect) {
794 }
else if ((width / height) > max_aspect) {
801 }
else goto render_and_return;
814 static bool handle_hints(
void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
815 xcb_atom_t name, xcb_get_property_reply_t *reply) {
818 DLOG(
"Received WM_HINTS for unknown client\n");
832 DLOG(
"Ignoring urgency flag for current client\n");
842 con->
urgent = hint_urgent;
844 DLOG(
"Discarding urgency WM_HINT because timer is running\n");
858 LOG(
"Urgency flag changed to %d\n", con->
urgent);
883 xcb_atom_t name, xcb_get_property_reply_t *prop) {
887 DLOG(
"No such window\n");
892 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
909 xcb_atom_t name, xcb_get_property_reply_t *prop) {
915 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
933 DLOG(
"focus change in, for window 0x%08x\n", event->event);
937 DLOG(
"That is con %p / %s\n", con, con->
name);
939 if (event->mode == XCB_NOTIFY_MODE_GRAB ||
940 event->mode == XCB_NOTIFY_MODE_UNGRAB) {
941 DLOG(
"FocusIn event for grab/ungrab, ignoring\n");
945 if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
946 DLOG(
"notify detail is pointer, ignoring this event\n");
951 DLOG(
"focus matches the currently focused window, not doing anything\n");
957 DLOG(
"This is a dock client, not focusing.\n");
961 DLOG(
"focus is different, updating decorations\n");
979 typedef bool (*
cb_property_handler_t)(
void *data, xcb_connection_t *c, uint8_t
state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
996 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
1007 property_handlers[0].
atom = A__NET_WM_NAME;
1011 property_handlers[4].
atom = A_WM_CLIENT_LEADER;
1013 property_handlers[6].
atom = A_WM_WINDOW_ROLE;
1018 xcb_get_property_reply_t *propr = NULL;
1021 if (property_handlers[c].atom != atom)
1024 handler = &property_handlers[c];
1028 if (handler == NULL) {
1033 if (state != XCB_PROPERTY_DELETE) {
1034 xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->
long_len);
1035 propr = xcb_get_property_reply(conn, cookie, 0);
1039 if (!handler->
cb(NULL, conn, state, window, atom, propr))
1050 type ==
randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1057 case XCB_KEY_RELEASE:
1061 case XCB_BUTTON_PRESS:
1065 case XCB_MAP_REQUEST:
1069 case XCB_UNMAP_NOTIFY:
1073 case XCB_DESTROY_NOTIFY:
1081 case XCB_MOTION_NOTIFY:
1086 case XCB_ENTER_NOTIFY:
1093 case XCB_CLIENT_MESSAGE:
1098 case XCB_CONFIGURE_REQUEST:
1103 case XCB_MAPPING_NOTIFY:
1111 case XCB_PROPERTY_NOTIFY: {
1112 xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;