1 /* Copyright (c) 2012-2014, 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.
24 * The views and conclusions contained in the software and documentation are those
25 * of the authors and should not be interpreted as representing official policies,
26 * either expressed or implied, of the FreeBSD Project.
39 void handle_event(xcb_generic_event_t *evt)
41 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
46 case XCB_DESTROY_NOTIFY:
49 case XCB_UNMAP_NOTIFY:
52 case XCB_CLIENT_MESSAGE:
55 case XCB_CONFIGURE_REQUEST:
56 configure_request(evt);
58 case XCB_PROPERTY_NOTIFY:
61 case XCB_ENTER_NOTIFY:
64 case XCB_MOTION_NOTIFY:
71 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
77 void map_request(xcb_generic_event_t *evt)
79 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
81 PRINTF("map request %X\n", e->window);
83 schedule_window(e->window);
86 void configure_request(xcb_generic_event_t *evt)
88 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
90 PRINTF("configure request %X\n", e->window);
93 bool is_managed = locate_window(e->window, &loc);
94 client_t *c = (is_managed ? loc.node->client : NULL);
97 if (is_managed && !is_floating(c)) {
98 if (e->value_mask & XCB_CONFIG_WINDOW_X)
99 c->floating_rectangle.x = e->x;
100 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
101 c->floating_rectangle.y = e->y;
102 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
104 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
108 restrain_floating_width(c, &w);
109 c->floating_rectangle.width = w;
113 restrain_floating_height(c, &h);
114 c->floating_rectangle.height = h;
117 xcb_configure_notify_event_t evt;
118 xcb_rectangle_t rect;
119 xcb_window_t win = c->window;
120 unsigned int bw = c->border_width;
123 rect = loc.monitor->rectangle;
125 rect = c->tiled_rectangle;
127 evt.response_type = XCB_CONFIGURE_NOTIFY;
130 evt.above_sibling = XCB_NONE;
133 evt.width = rect.width;
134 evt.height = rect.height;
135 evt.border_width = bw;
136 evt.override_redirect = false;
138 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
141 arrange(loc.monitor, loc.desktop);
145 unsigned short i = 0;
147 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
148 mask |= XCB_CONFIG_WINDOW_X;
151 c->floating_rectangle.x = e->x;
154 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
155 mask |= XCB_CONFIG_WINDOW_Y;
158 c->floating_rectangle.y = e->y;
161 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
162 mask |= XCB_CONFIG_WINDOW_WIDTH;
165 restrain_floating_width(c, &w);
166 c->floating_rectangle.width = w;
171 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
172 mask |= XCB_CONFIG_WINDOW_HEIGHT;
175 restrain_floating_height(c, &h);
176 c->floating_rectangle.height = h;
181 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
182 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
183 values[i++] = e->border_width;
186 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
187 mask |= XCB_CONFIG_WINDOW_SIBLING;
188 values[i++] = e->sibling;
191 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
192 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
193 values[i++] = e->stack_mode;
196 xcb_configure_window(dpy, e->window, mask, values);
200 translate_client(monitor_from_client(c), loc.monitor, c);
203 void destroy_notify(xcb_generic_event_t *evt)
205 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
207 PRINTF("destroy notify %X\n", e->window);
209 unmanage_window(e->window);
212 void unmap_notify(xcb_generic_event_t *evt)
214 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
216 PRINTF("unmap notify %X\n", e->window);
218 unmanage_window(e->window);
221 void property_notify(xcb_generic_event_t *evt)
223 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
225 /* PRINTF("property notify %X\n", e->window); */
227 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
231 if (!locate_window(e->window, &loc))
234 if (e->atom == XCB_ATOM_WM_HINTS) {
235 xcb_icccm_wm_hints_t hints;
236 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
237 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
238 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
239 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
240 client_t *c = loc.node->client;
241 xcb_size_hints_t size_hints;
242 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
243 (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
244 c->min_width = size_hints.min_width;
245 c->max_width = size_hints.max_width;
246 c->min_height = size_hints.min_height;
247 c->max_height = size_hints.max_height;
248 int w = c->floating_rectangle.width;
249 int h = c->floating_rectangle.height;
250 restrain_floating_size(c, &w, &h);
251 c->floating_rectangle.width = w;
252 c->floating_rectangle.height = h;
253 arrange(loc.monitor, loc.desktop);
258 void client_message(xcb_generic_event_t *evt)
260 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
262 PRINTF("client message %X %u\n", e->window, e->type);
264 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
266 if (ewmh_locate_desktop(e->data.data32[0], &loc))
267 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
272 if (!locate_window(e->window, &loc))
275 if (e->type == ewmh->_NET_WM_STATE) {
276 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
277 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
278 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
279 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
280 loc.node == mon->desk->focus)
282 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
283 set_fullscreen(loc.desktop->focus, false);
284 arrange(loc.monitor, loc.desktop);
286 focus_node(loc.monitor, loc.desktop, loc.node);
287 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
289 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
290 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
291 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
292 window_close(loc.node);
296 void focus_in(xcb_generic_event_t *evt)
298 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
300 /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
302 if (e->mode == XCB_NOTIFY_MODE_GRAB ||
303 e->mode == XCB_NOTIFY_MODE_UNGRAB)
305 /* prevent focus stealing */
306 if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
307 e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
308 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
309 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
310 (mon->desk->focus == NULL ||
311 mon->desk->focus->client->window != e->event))
312 update_input_focus();
315 void enter_notify(xcb_generic_event_t *evt)
317 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
318 xcb_window_t win = e->event;
320 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
322 if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
323 (mon->desk->focus != NULL &&
324 mon->desk->focus->client->window == win))
327 enable_motion_recorder();
330 void motion_notify(xcb_generic_event_t *evt)
332 PUTS("motion notify");
334 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
336 int dtime = e->time - last_motion_time;
338 last_motion_time = e->time;
339 last_motion_x = e->event_x;
340 last_motion_y = e->event_y;
344 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
348 disable_motion_recorder();
350 xcb_window_t win = XCB_NONE;
351 xcb_point_t pt = {e->root_x, e->root_y};
352 query_pointer(&win, NULL);
354 bool backup = pointer_follows_monitor;
356 pointer_follows_monitor = false;
357 if (!window_focus(win)) {
358 monitor_t *m = monitor_from_point(pt);
359 if (m != NULL && m != mon)
360 focus_node(m, m->desk, m->desk->focus);
362 pointer_follows_monitor = backup;
366 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
368 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
369 if (action == XCB_EWMH_WM_STATE_ADD)
370 set_fullscreen(n, true);
371 else if (action == XCB_EWMH_WM_STATE_REMOVE)
372 set_fullscreen(n, false);
373 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
374 set_fullscreen(n, !n->client->fullscreen);
376 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
377 if (action == XCB_EWMH_WM_STATE_ADD)
378 set_sticky(m, d, n, true);
379 else if (action == XCB_EWMH_WM_STATE_REMOVE)
380 set_sticky(m, d, n, false);
381 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
382 set_sticky(m, d, n, !n->client->sticky);
383 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
384 if (action == XCB_EWMH_WM_STATE_ADD)
385 set_urgency(m, d, n, true);
386 else if (action == XCB_EWMH_WM_STATE_REMOVE)
387 set_urgency(m, d, n, false);
388 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
389 set_urgency(m, d, n, !n->client->urgent);