5 #include <xcb/xcb_event.h>
6 #include <xcb/xcb_icccm.h>
17 void handle_event(xcb_generic_event_t *evt)
19 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
23 case XCB_DESTROY_NOTIFY:
26 case XCB_UNMAP_NOTIFY:
29 case XCB_CLIENT_MESSAGE:
32 case XCB_CONFIGURE_REQUEST:
33 configure_request(evt);
35 case XCB_PROPERTY_NOTIFY:
38 case XCB_ENTER_NOTIFY:
41 case XCB_MOTION_NOTIFY:
44 case XCB_BUTTON_RELEASE:
52 void map_request(xcb_generic_event_t *evt)
54 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
56 PRINTF("map request %X\n", e->window);
58 manage_window(mon, mon->desk, e->window);
61 void configure_request(xcb_generic_event_t *evt)
63 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
65 PRINTF("configure request %X\n", e->window);
67 window_location_t loc;
68 bool is_managed = locate_window(e->window, &loc);
70 if (!is_managed || is_floating(loc.node->client)) {
75 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
76 mask |= XCB_CONFIG_WINDOW_X;
79 loc.node->client->floating_rectangle.x = e->x;
82 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
83 mask |= XCB_CONFIG_WINDOW_Y;
86 loc.node->client->floating_rectangle.y = e->y;
89 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
90 mask |= XCB_CONFIG_WINDOW_WIDTH;
91 values[i++] = e->width;
93 loc.node->client->floating_rectangle.width = e->width;
96 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
97 mask |= XCB_CONFIG_WINDOW_HEIGHT;
98 values[i++] = e->height;
100 loc.node->client->floating_rectangle.height = e->height;
103 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
104 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
105 values[i++] = e->border_width;
108 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
109 mask |= XCB_CONFIG_WINDOW_SIBLING;
110 values[i++] = e->sibling;
113 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
114 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
115 values[i++] = e->stack_mode;
118 xcb_configure_window(dpy, e->window, mask, values);
120 window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
122 xcb_configure_notify_event_t evt;
123 xcb_rectangle_t rect;
125 xcb_window_t win = loc.node->client->window;
127 if (is_tiled(loc.node->client)) {
128 rect = loc.node->client->tiled_rectangle;
131 rect = loc.monitor->rectangle;
135 evt.response_type = XCB_CONFIGURE_NOTIFY;
138 evt.above_sibling = XCB_NONE;
141 evt.width = rect.width;
142 evt.height = rect.height;
143 evt.border_width = bw;
144 evt.override_redirect = false;
146 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
150 void destroy_notify(xcb_generic_event_t *evt)
152 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
154 PRINTF("destroy notify %X\n", e->window);
156 window_location_t loc;
157 if (locate_window(e->window, &loc)) {
158 remove_node(loc.desktop, loc.node);
159 arrange(loc.monitor, loc.desktop);
163 void unmap_notify(xcb_generic_event_t *evt)
165 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
167 PRINTF("unmap notify %X\n", e->window);
169 window_location_t loc;
170 if (locate_window(e->window, &loc)) {
171 remove_node(loc.desktop, loc.node);
172 arrange(loc.monitor, loc.desktop);
176 void property_notify(xcb_generic_event_t *evt)
178 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
179 xcb_icccm_wm_hints_t hints;
181 /* PRINTF("property notify %X\n", e->window); */
183 if (e->atom != XCB_ATOM_WM_HINTS)
186 window_location_t loc;
187 if (locate_window(e->window, &loc)) {
188 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1) {
189 uint32_t urgent = xcb_icccm_wm_hints_get_urgency(&hints);
190 if (urgent != 0 && loc.node != mon->desk->focus) {
191 loc.node->client->urgent = urgent;
193 if (loc.monitor->desk == loc.desktop)
194 arrange(loc.monitor, loc.desktop);
200 void enter_notify(xcb_generic_event_t *evt)
202 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
204 PRINTF("enter notify %X %d %d\n", e->event, e->mode, e->detail);
206 if (!focus_follows_mouse
207 || (e->mode != XCB_NOTIFY_MODE_NORMAL && e->detail == XCB_NOTIFY_DETAIL_INFERIOR)
208 || (pointer_position.x == e->root_x && pointer_position.y == e->root_y)
209 || last_entered == e->event)
212 window_location_t loc;
213 if (locate_window(e->event, &loc)) {
214 select_monitor(loc.monitor);
215 focus_node(loc.monitor, loc.desktop, loc.node, true);
216 pointer_position = (xcb_point_t) {e->root_x, e->root_y};
217 last_entered = e->event;
221 void client_message(xcb_generic_event_t *evt)
223 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
225 PRINTF("client message %X %u\n", e->window, e->type);
227 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
228 desktop_location_t loc;
229 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
230 select_monitor(loc.monitor);
231 select_desktop(loc.desktop);
236 window_location_t loc;
237 if (!locate_window(e->window, &loc))
240 if (e->type == ewmh->_NET_WM_STATE) {
241 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
242 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
243 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
244 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node)
245 toggle_fullscreen(loc.monitor, loc.desktop->focus->client);
246 select_monitor(loc.monitor);
247 select_desktop(loc.desktop);
248 focus_node(loc.monitor, loc.desktop, loc.node, true);
249 arrange(loc.monitor, loc.desktop);
253 void mouse_do(mouse_action_t mac)
255 PRINTF("mouse action %u\n", mac);
260 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
262 pos = (xcb_point_t) {qpr->root_x, qpr->root_y};
269 window_location_t loc;
270 if (locate_window(win, &loc)) {
271 client_t *c = loc.node->client;
274 focus_node(loc.monitor, loc.desktop, loc.node, true);
278 if (is_tiled(loc.node->client)) {
279 loc.node->client->floating_rectangle = loc.node->client->tiled_rectangle;
280 toggle_floating(loc.node);
281 arrange(loc.monitor, loc.desktop);
282 } else if (loc.node->client->fullscreen) {
285 frozen_pointer->monitor = loc.monitor;
286 frozen_pointer->desktop = loc.desktop;
287 frozen_pointer->node = loc.node;
288 frozen_pointer->rectangle = c->floating_rectangle;
289 frozen_pointer->position = pos;
290 frozen_pointer->action = mac;
291 if (mac == MOUSE_RESIZE) {
292 int16_t mid_x, mid_y;
293 mid_x = c->floating_rectangle.x + (c->floating_rectangle.width / 2);
294 mid_y = c->floating_rectangle.y + (c->floating_rectangle.height / 2);
297 frozen_pointer->corner = BOTTOM_RIGHT;
299 frozen_pointer->corner = TOP_RIGHT;
302 frozen_pointer->corner = BOTTOM_LEFT;
304 frozen_pointer->corner = TOP_LEFT;
307 xcb_grab_pointer(dpy, false, root, XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_CURRENT_TIME);
313 void motion_notify(xcb_generic_event_t *evt)
315 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
317 int16_t delta_x, delta_y, x, y, w, h;
318 uint16_t width, height;
320 monitor_t *m = frozen_pointer->monitor;
321 desktop_t *d = frozen_pointer->desktop;
322 node_t *n = frozen_pointer->node;
323 client_t *c = n->client;
324 xcb_rectangle_t rect = frozen_pointer->rectangle;
325 xcb_window_t win = c->window;
330 /* PRINTF("motion notify %X\n", win); */
332 delta_x = e->root_x - frozen_pointer->position.x;
333 delta_y = e->root_y - frozen_pointer->position.y;
335 switch (frozen_pointer->action) {
337 x = rect.x + delta_x;
338 y = rect.y + delta_y;
339 window_move(win, x, y);
342 switch (frozen_pointer->corner) {
344 x = rect.x + delta_x;
345 y = rect.y + delta_y;
346 w = rect.width - delta_x;
347 h = rect.height - delta_y;
351 y = rect.y + delta_y;
352 w = rect.width + delta_x;
353 h = rect.height - delta_y;
356 x = rect.x + delta_x;
358 w = rect.width - delta_x;
359 h = rect.height + delta_y;
364 w = rect.width + delta_x;
365 h = rect.height + delta_y;
370 window_move_resize(win, x, y, width, height);
371 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
372 window_draw_border(n, d->focus == n, mon == m);
379 void button_release(void)
381 PUTS("button release");
383 xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
384 update_floating_rectangle(frozen_pointer->node->client);
385 monitor_t *m = underlying_monitor(frozen_pointer->node->client);
386 if (m != NULL && m != frozen_pointer->monitor) {
387 transfer_node(frozen_pointer->monitor, frozen_pointer->desktop, m, m->desk, frozen_pointer->node);
392 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
394 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
395 bool fs = n->client->fullscreen;
396 if (action == XCB_EWMH_WM_STATE_TOGGLE
397 || (fs && action == XCB_EWMH_WM_STATE_REMOVE)
398 || (!fs && action == XCB_EWMH_WM_STATE_ADD)) {
399 toggle_fullscreen(m, n->client);