]> git.lizzy.rs Git - bspwm.git/blob - events.c
055b30d2bef66eb06f16fff712deb1d55c7cacc1
[bspwm.git] / events.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdlib.h>
26 #include "bspwm.h"
27 #include "ewmh.h"
28 #include "monitor.h"
29 #include "query.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "window.h"
33 #include "events.h"
34
35 void handle_event(xcb_generic_event_t *evt)
36 {
37         uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
38         switch (resp_type) {
39                 case XCB_MAP_REQUEST:
40                         map_request(evt);
41                         break;
42                 case XCB_DESTROY_NOTIFY:
43                         destroy_notify(evt);
44                         break;
45                 case XCB_UNMAP_NOTIFY:
46                         unmap_notify(evt);
47                         break;
48                 case XCB_CLIENT_MESSAGE:
49                         client_message(evt);
50                         break;
51                 case XCB_CONFIGURE_REQUEST:
52                         configure_request(evt);
53                         break;
54                 case XCB_PROPERTY_NOTIFY:
55                         property_notify(evt);
56                         break;
57                 case XCB_ENTER_NOTIFY:
58                         enter_notify(evt);
59                         break;
60                 case XCB_MOTION_NOTIFY:
61                         motion_notify(evt);
62                         break;
63                 case XCB_FOCUS_IN:
64                         focus_in(evt);
65                         break;
66                 default:
67                         if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
68                                 update_monitors();
69                         break;
70         }
71 }
72
73 void map_request(xcb_generic_event_t *evt)
74 {
75         xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
76
77         PRINTF("map request %X\n", e->window);
78
79         schedule_window(e->window);
80 }
81
82 void configure_request(xcb_generic_event_t *evt)
83 {
84         xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
85
86         PRINTF("configure request %X\n", e->window);
87
88         coordinates_t loc;
89         bool is_managed = locate_window(e->window, &loc);
90         client_t *c = (is_managed ? loc.node->client : NULL);
91         int w = 0, h = 0;
92
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)
99                         w = e->width;
100                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
101                         h = e->height;
102
103                 if (w != 0) {
104                         restrain_floating_width(c, &w);
105                         c->floating_rectangle.width = w;
106                 }
107
108                 if (h != 0) {
109                         restrain_floating_height(c, &h);
110                         c->floating_rectangle.height = h;
111                 }
112
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;
117
118                 if (c->fullscreen)
119                         rect = loc.monitor->rectangle;
120                 else
121                         rect = c->tiled_rectangle;
122
123                 evt.response_type = XCB_CONFIGURE_NOTIFY;
124                 evt.event = win;
125                 evt.window = win;
126                 evt.above_sibling = XCB_NONE;
127                 evt.x = rect.x;
128                 evt.y = rect.y;
129                 evt.width = rect.width;
130                 evt.height = rect.height;
131                 evt.border_width = bw;
132                 evt.override_redirect = false;
133
134                 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
135
136                 if (c->pseudo_tiled)
137                         arrange(loc.monitor, loc.desktop);
138         } else {
139                 uint16_t mask = 0;
140                 uint32_t values[7];
141                 unsigned short i = 0;
142
143                 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
144                         mask |= XCB_CONFIG_WINDOW_X;
145                         values[i++] = e->x;
146                         if (is_managed)
147                                 c->floating_rectangle.x = e->x;
148                 }
149
150                 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
151                         mask |= XCB_CONFIG_WINDOW_Y;
152                         values[i++] = e->y;
153                         if (is_managed)
154                                 c->floating_rectangle.y = e->y;
155                 }
156
157                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
158                         mask |= XCB_CONFIG_WINDOW_WIDTH;
159                         w = e->width;
160                         if (is_managed) {
161                                 restrain_floating_width(c, &w);
162                                 c->floating_rectangle.width = w;
163                         }
164                         values[i++] = w;
165                 }
166
167                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
168                         mask |= XCB_CONFIG_WINDOW_HEIGHT;
169                         h = e->height;
170                         if (is_managed) {
171                                 restrain_floating_height(c, &h);
172                                 c->floating_rectangle.height = h;
173                         }
174                         values[i++] = h;
175                 }
176
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;
180                 }
181
182                 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
183                         mask |= XCB_CONFIG_WINDOW_SIBLING;
184                         values[i++] = e->sibling;
185                 }
186
187                 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
188                         mask |= XCB_CONFIG_WINDOW_STACK_MODE;
189                         values[i++] = e->stack_mode;
190                 }
191
192                 xcb_configure_window(dpy, e->window, mask, values);
193         }
194
195         if (is_managed)
196                 translate_client(monitor_from_client(c), loc.monitor, c);
197 }
198
199 void destroy_notify(xcb_generic_event_t *evt)
200 {
201         xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
202
203         PRINTF("destroy notify %X\n", e->window);
204
205         unmanage_window(e->window);
206 }
207
208 void unmap_notify(xcb_generic_event_t *evt)
209 {
210         xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
211
212         PRINTF("unmap notify %X\n", e->window);
213
214         unmanage_window(e->window);
215 }
216
217 void property_notify(xcb_generic_event_t *evt)
218 {
219         xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
220
221         /* PRINTF("property notify %X\n", e->window); */
222
223         if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
224                 return;
225
226         coordinates_t loc;
227         if (!locate_window(e->window, &loc))
228                         return;
229
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);
250                 }
251         }
252 }
253
254 void client_message(xcb_generic_event_t *evt)
255 {
256         xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
257
258         PRINTF("client message %X %u\n", e->window, e->type);
259
260         if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
261                 coordinates_t loc;
262                 if (ewmh_locate_desktop(e->data.data32[0], &loc))
263                         focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
264                 return;
265         }
266
267         coordinates_t loc;
268         if (!locate_window(e->window, &loc))
269                 return;
270
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)
277                         return;
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);
281                 }
282                 focus_node(loc.monitor, loc.desktop, loc.node);
283         } else if (e->type == ewmh->_NET_WM_DESKTOP) {
284                 coordinates_t dloc;
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);
289         }
290 }
291
292 void focus_in(xcb_generic_event_t *evt)
293 {
294         xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
295
296         /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
297
298         if (e->mode == XCB_NOTIFY_MODE_GRAB ||
299             e->mode == XCB_NOTIFY_MODE_UNGRAB)
300                 return;
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();
309 }
310
311 void enter_notify(xcb_generic_event_t *evt)
312 {
313         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
314         xcb_window_t win = e->event;
315
316         PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
317
318         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
319             (mon->desk->focus != NULL &&
320              mon->desk->focus->client->window == win))
321                 return;
322
323         enable_motion_recorder();
324 }
325
326 void motion_notify(xcb_generic_event_t *evt)
327 {
328         PUTS("motion notify");
329
330         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
331
332         int dtime = e->time - last_motion_time;
333         if (dtime > 1000) {
334                 last_motion_time = e->time;
335                 last_motion_x = e->event_x;
336                 last_motion_y = e->event_y;
337                 return;
338         }
339
340         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
341         if (mdist < 10)
342                 return;
343
344         disable_motion_recorder();
345
346         xcb_window_t win = XCB_NONE;
347         xcb_point_t pt = {e->root_x, e->root_y};
348         query_pointer(&win, NULL);
349
350         bool backup = pointer_follows_monitor;
351         auto_raise = false;
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);
357         }
358         pointer_follows_monitor = backup;
359         auto_raise = true;
360 }
361
362 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
363 {
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);
371                 arrange(m, d);
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);
386         }
387 }