1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 void handle_event(xcb_generic_event_t *evt)
37 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
42 case XCB_DESTROY_NOTIFY:
45 case XCB_UNMAP_NOTIFY:
48 case XCB_CLIENT_MESSAGE:
51 case XCB_CONFIGURE_REQUEST:
52 configure_request(evt);
54 case XCB_PROPERTY_NOTIFY:
57 case XCB_ENTER_NOTIFY:
60 case XCB_MOTION_NOTIFY:
67 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
73 void map_request(xcb_generic_event_t *evt)
75 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
77 PRINTF("map request %X\n", e->window);
79 schedule_window(e->window);
82 void configure_request(xcb_generic_event_t *evt)
84 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
86 PRINTF("configure request %X\n", e->window);
89 bool is_managed = locate_window(e->window, &loc);
90 client_t *c = (is_managed ? loc.node->client : NULL);
93 if (is_managed && !is_floating(c)) {
94 if (e->value_mask & XCB_CONFIG_WINDOW_X)
95 c->floating_rectangle.x = e->x;
96 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
97 c->floating_rectangle.y = e->y;
98 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
100 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
104 restrain_floating_width(c, &w);
105 c->floating_rectangle.width = w;
109 restrain_floating_height(c, &h);
110 c->floating_rectangle.height = h;
113 xcb_configure_notify_event_t evt;
114 xcb_rectangle_t rect;
115 xcb_window_t win = c->window;
116 unsigned int bw = c->border_width;
119 rect = loc.monitor->rectangle;
121 rect = c->tiled_rectangle;
123 evt.response_type = XCB_CONFIGURE_NOTIFY;
126 evt.above_sibling = XCB_NONE;
129 evt.width = rect.width;
130 evt.height = rect.height;
131 evt.border_width = bw;
132 evt.override_redirect = false;
134 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
137 arrange(loc.monitor, loc.desktop);
141 unsigned short i = 0;
143 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
144 mask |= XCB_CONFIG_WINDOW_X;
147 c->floating_rectangle.x = e->x;
150 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
151 mask |= XCB_CONFIG_WINDOW_Y;
154 c->floating_rectangle.y = e->y;
157 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
158 mask |= XCB_CONFIG_WINDOW_WIDTH;
161 restrain_floating_width(c, &w);
162 c->floating_rectangle.width = w;
167 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
168 mask |= XCB_CONFIG_WINDOW_HEIGHT;
171 restrain_floating_height(c, &h);
172 c->floating_rectangle.height = h;
177 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
178 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
179 values[i++] = e->border_width;
182 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
183 mask |= XCB_CONFIG_WINDOW_SIBLING;
184 values[i++] = e->sibling;
187 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
188 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
189 values[i++] = e->stack_mode;
192 xcb_configure_window(dpy, e->window, mask, values);
196 translate_client(monitor_from_client(c), loc.monitor, c);
199 void destroy_notify(xcb_generic_event_t *evt)
201 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
203 PRINTF("destroy notify %X\n", e->window);
205 unmanage_window(e->window);
208 void unmap_notify(xcb_generic_event_t *evt)
210 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
212 PRINTF("unmap notify %X\n", e->window);
214 unmanage_window(e->window);
217 void property_notify(xcb_generic_event_t *evt)
219 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
221 /* PRINTF("property notify %X\n", e->window); */
223 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
227 if (!locate_window(e->window, &loc))
230 if (e->atom == XCB_ATOM_WM_HINTS) {
231 xcb_icccm_wm_hints_t hints;
232 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
233 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
234 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
235 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
236 client_t *c = loc.node->client;
237 xcb_size_hints_t size_hints;
238 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
239 (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
240 c->min_width = size_hints.min_width;
241 c->max_width = size_hints.max_width;
242 c->min_height = size_hints.min_height;
243 c->max_height = size_hints.max_height;
244 int w = c->floating_rectangle.width;
245 int h = c->floating_rectangle.height;
246 restrain_floating_size(c, &w, &h);
247 c->floating_rectangle.width = w;
248 c->floating_rectangle.height = h;
249 arrange(loc.monitor, loc.desktop);
254 void client_message(xcb_generic_event_t *evt)
256 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
258 PRINTF("client message %X %u\n", e->window, e->type);
260 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
262 if (ewmh_locate_desktop(e->data.data32[0], &loc))
263 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
268 if (!locate_window(e->window, &loc))
271 if (e->type == ewmh->_NET_WM_STATE) {
272 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
273 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
274 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
275 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
276 loc.node == mon->desk->focus)
278 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
279 set_fullscreen(loc.desktop->focus, false);
280 arrange(loc.monitor, loc.desktop);
282 focus_node(loc.monitor, loc.desktop, loc.node);
283 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
285 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
286 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
287 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
288 window_close(loc.node);
292 void focus_in(xcb_generic_event_t *evt)
294 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
296 /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
298 if (e->mode == XCB_NOTIFY_MODE_GRAB ||
299 e->mode == XCB_NOTIFY_MODE_UNGRAB)
301 /* prevent focus stealing */
302 if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
303 e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
304 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
305 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
306 (mon->desk->focus == NULL ||
307 mon->desk->focus->client->window != e->event))
308 update_input_focus();
311 void enter_notify(xcb_generic_event_t *evt)
313 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
314 xcb_window_t win = e->event;
316 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
318 if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
319 (mon->desk->focus != NULL &&
320 mon->desk->focus->client->window == win))
323 enable_motion_recorder();
326 void motion_notify(xcb_generic_event_t *evt)
328 PUTS("motion notify");
330 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
332 int dtime = e->time - last_motion_time;
334 last_motion_time = e->time;
335 last_motion_x = e->event_x;
336 last_motion_y = e->event_y;
340 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
344 disable_motion_recorder();
346 xcb_window_t win = XCB_NONE;
347 xcb_point_t pt = {e->root_x, e->root_y};
348 query_pointer(&win, NULL);
350 bool backup = pointer_follows_monitor;
352 pointer_follows_monitor = false;
353 if (!window_focus(win)) {
354 monitor_t *m = monitor_from_point(pt);
355 if (m != NULL && m != mon)
356 focus_node(m, m->desk, m->desk->focus);
358 pointer_follows_monitor = backup;
362 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
364 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
365 if (action == XCB_EWMH_WM_STATE_ADD)
366 set_fullscreen(n, true);
367 else if (action == XCB_EWMH_WM_STATE_REMOVE)
368 set_fullscreen(n, false);
369 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
370 set_fullscreen(n, !n->client->fullscreen);
372 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
373 if (action == XCB_EWMH_WM_STATE_ADD)
374 set_sticky(m, d, n, true);
375 else if (action == XCB_EWMH_WM_STATE_REMOVE)
376 set_sticky(m, d, n, false);
377 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
378 set_sticky(m, d, n, !n->client->sticky);
379 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
380 if (action == XCB_EWMH_WM_STATE_ADD)
381 set_urgency(m, d, n, true);
382 else if (action == XCB_EWMH_WM_STATE_REMOVE)
383 set_urgency(m, d, n, false);
384 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
385 set_urgency(m, d, n, !n->client->urgent);