]> git.lizzy.rs Git - bspwm.git/blob - events.c
Try not to freeze the server
[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 <stdbool.h>
27 #include "bspwm.h"
28 #include "ewmh.h"
29 #include "monitor.h"
30 #include "query.h"
31 #include "settings.h"
32 #include "subscribe.h"
33 #include "tree.h"
34 #include "window.h"
35 #include "pointer.h"
36 #include "events.h"
37
38 void handle_event(xcb_generic_event_t *evt)
39 {
40         uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
41         switch (resp_type) {
42                 case XCB_MAP_REQUEST:
43                         map_request(evt);
44                         break;
45                 case XCB_DESTROY_NOTIFY:
46                         destroy_notify(evt);
47                         break;
48                 case XCB_UNMAP_NOTIFY:
49                         unmap_notify(evt);
50                         break;
51                 case XCB_CLIENT_MESSAGE:
52                         client_message(evt);
53                         break;
54                 case XCB_CONFIGURE_REQUEST:
55                         configure_request(evt);
56                         break;
57                 case XCB_PROPERTY_NOTIFY:
58                         property_notify(evt);
59                         break;
60                 case XCB_ENTER_NOTIFY:
61                         enter_notify(evt);
62                         break;
63                 case XCB_BUTTON_PRESS:
64                         button_press(evt);
65                         break;
66                 case XCB_MOTION_NOTIFY:
67                         motion_notify(evt);
68                         break;
69                 case XCB_FOCUS_IN:
70                         focus_in(evt);
71                         break;
72                 case 0:
73                         process_error(evt);
74                         break;
75                 default:
76                         if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
77                                 update_monitors();
78                         }
79                         break;
80         }
81 }
82
83 void map_request(xcb_generic_event_t *evt)
84 {
85         xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
86
87         schedule_window(e->window);
88 }
89
90 void configure_request(xcb_generic_event_t *evt)
91 {
92         xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
93
94         coordinates_t loc;
95         bool is_managed = locate_window(e->window, &loc);
96         client_t *c = (is_managed ? loc.node->client : NULL);
97         uint16_t width, height;
98
99         if (!is_managed) {
100                 uint16_t mask = 0;
101                 uint32_t values[7];
102                 unsigned short i = 0;
103
104                 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
105                         mask |= XCB_CONFIG_WINDOW_X;
106                         values[i++] = e->x;
107                 }
108
109                 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
110                         mask |= XCB_CONFIG_WINDOW_Y;
111                         values[i++] = e->y;
112                 }
113
114                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
115                         mask |= XCB_CONFIG_WINDOW_WIDTH;
116                         values[i++] = e->width;
117                 }
118
119                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
120                         mask |= XCB_CONFIG_WINDOW_HEIGHT;
121                         values[i++] = e->height;
122                 }
123
124                 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
125                         mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
126                         values[i++] = e->border_width;
127                 }
128
129                 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
130                         mask |= XCB_CONFIG_WINDOW_SIBLING;
131                         values[i++] = e->sibling;
132                 }
133
134                 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
135                         mask |= XCB_CONFIG_WINDOW_STACK_MODE;
136                         values[i++] = e->stack_mode;
137                 }
138
139                 xcb_configure_window(dpy, e->window, mask, values);
140
141         } else if (IS_FLOATING(c)) {
142                 width = c->floating_rectangle.width;
143                 height = c->floating_rectangle.height;
144
145                 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
146                         c->floating_rectangle.x = e->x;
147                 }
148
149                 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
150                         c->floating_rectangle.y = e->y;
151                 }
152
153                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
154                         width = e->width;
155                 }
156
157                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
158                         height = e->height;
159                 }
160
161                 apply_size_hints(c, &width, &height);
162                 c->floating_rectangle.width = width;
163                 c->floating_rectangle.height = height;
164                 xcb_rectangle_t r = c->floating_rectangle;
165
166                 window_move_resize(e->window, r.x, r.y, r.width, r.height);
167
168                 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc.monitor->id, loc.desktop->id, e->window, r.width, r.height, r.x, r.y);
169
170                 monitor_t *m = monitor_from_client(c);
171                 if (m != loc.monitor) {
172                         transfer_node(loc.monitor, loc.desktop, loc.node, m, m->desk, m->desk->focus);
173                 }
174         } else {
175                 if (c->state == STATE_PSEUDO_TILED) {
176                         width = c->floating_rectangle.width;
177                         height = c->floating_rectangle.height;
178                         if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
179                                 width = e->width;
180                         }
181                         if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
182                                 height = e->height;
183                         }
184                         apply_size_hints(c, &width, &height);
185                         if (width != c->floating_rectangle.width || height != c->floating_rectangle.height) {
186                                 c->floating_rectangle.width = width;
187                                 c->floating_rectangle.height = height;
188                                 arrange(loc.monitor, loc.desktop);
189                         }
190                 }
191
192
193                 xcb_configure_notify_event_t evt;
194                 unsigned int bw = c->border_width;
195
196                 xcb_rectangle_t r = IS_FULLSCREEN(c) ? loc.monitor->rectangle : c->tiled_rectangle;
197
198                 evt.response_type = XCB_CONFIGURE_NOTIFY;
199                 evt.event = e->window;
200                 evt.window = e->window;
201                 evt.above_sibling = XCB_NONE;
202                 evt.x = r.x;
203                 evt.y = r.y;
204                 evt.width = r.width;
205                 evt.height = r.height;
206                 evt.border_width = bw;
207                 evt.override_redirect = false;
208
209                 xcb_send_event(dpy, false, e->window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
210         }
211 }
212
213 void destroy_notify(xcb_generic_event_t *evt)
214 {
215         xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
216
217         unmanage_window(e->window);
218 }
219
220 void unmap_notify(xcb_generic_event_t *evt)
221 {
222         xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
223
224         unmanage_window(e->window);
225 }
226
227 void property_notify(xcb_generic_event_t *evt)
228 {
229         xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
230
231         if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS) {
232                 return;
233         }
234
235         coordinates_t loc;
236         if (!locate_window(e->window, &loc)) {
237                         return;
238         }
239
240         if (e->atom == XCB_ATOM_WM_HINTS) {
241                 xcb_icccm_wm_hints_t hints;
242                 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
243                     (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
244                         set_urgent(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
245         } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
246                 client_t *c = loc.node->client;
247                 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &c->size_hints, NULL) == 1) {
248                         arrange(loc.monitor, loc.desktop);
249                 }
250         }
251 }
252
253 void client_message(xcb_generic_event_t *evt)
254 {
255         xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
256
257         if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
258                 coordinates_t loc;
259                 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
260                         focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
261                 }
262                 return;
263         }
264
265         coordinates_t loc;
266         if (!locate_window(e->window, &loc)) {
267                 return;
268         }
269
270         if (e->type == ewmh->_NET_WM_STATE) {
271                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
272                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
273         } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
274                 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
275                     loc.node == mon->desk->focus) {
276                         return;
277                 }
278                 focus_node(loc.monitor, loc.desktop, loc.node);
279         } else if (e->type == ewmh->_NET_WM_DESKTOP) {
280                 coordinates_t dloc;
281                 if (ewmh_locate_desktop(e->data.data32[0], &dloc)) {
282                         transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
283                 }
284         } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
285                 close_node(loc.node);
286         }
287 }
288
289 void focus_in(xcb_generic_event_t *evt)
290 {
291         xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
292
293         if (e->mode == XCB_NOTIFY_MODE_GRAB || e->mode == XCB_NOTIFY_MODE_UNGRAB
294             || e->detail == XCB_NOTIFY_DETAIL_POINTER || e->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT
295             || e->detail == XCB_NOTIFY_DETAIL_NONE) {
296                 return;
297         }
298
299         if (mon->desk->focus != NULL && e->event == mon->desk->focus->id) {
300                 return;
301         }
302
303         coordinates_t loc;
304         if (locate_window(e->event, &loc)) {
305                 // prevent input focus stealing
306                 update_input_focus();
307         }
308 }
309
310 void button_press(xcb_generic_event_t *evt)
311 {
312         xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
313         bool replay = false;
314         switch (e->detail) {
315                 case XCB_BUTTON_INDEX_1:
316                         if (click_to_focus && cleaned_mask(e->state) == XCB_NONE) {
317                                 replay = true;
318                                 grab_pointer(ACTION_FOCUS);
319                         } else {
320                                 grab_pointer(pointer_actions[0]);
321                         }
322                         break;
323                 case XCB_BUTTON_INDEX_2:
324                         grab_pointer(pointer_actions[1]);
325                         break;
326                 case XCB_BUTTON_INDEX_3:
327                         grab_pointer(pointer_actions[2]);
328                         break;
329         }
330         xcb_allow_events(dpy, replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, e->time);
331         xcb_flush(dpy);
332 }
333
334 void enter_notify(xcb_generic_event_t *evt)
335 {
336         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
337         xcb_window_t win = e->event;
338
339         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
340             (mon->desk->focus != NULL &&
341              mon->desk->focus->id == win)) {
342                 return;
343         }
344
345         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
346
347         if (wa == NULL) {
348                 return;
349         }
350
351         if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
352                 enable_motion_recorder();
353         } else {
354                 disable_motion_recorder();
355         }
356 }
357
358 void motion_notify(xcb_generic_event_t *evt)
359 {
360         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
361
362         static uint16_t last_motion_x = 0, last_motion_y = 0;
363         static xcb_timestamp_t last_motion_time = 0;
364
365         int64_t dtime = e->time - last_motion_time;
366
367         if (dtime > 1000) {
368                 last_motion_time = e->time;
369                 last_motion_x = e->event_x;
370                 last_motion_y = e->event_y;
371                 return;
372         }
373
374         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
375         if (mdist < 10) {
376                 return;
377         }
378
379         xcb_window_t win = XCB_NONE;
380         xcb_point_t pt = {e->root_x, e->root_y};
381         query_pointer(&win, NULL);
382
383         bool pfm_backup = pointer_follows_monitor;
384         bool pff_backup = pointer_follows_focus;
385         auto_raise = false;
386         pointer_follows_monitor = false;
387         pointer_follows_focus = false;
388         coordinates_t loc;
389         if (locate_window(win, &loc)) {
390                 if (loc.node != mon->desk->focus) {
391                         focus_node(loc.monitor, loc.desktop, loc.node);
392                 }
393         } else {
394                 monitor_t *m = monitor_from_point(pt);
395                 if (m != NULL && m != mon) {
396                         focus_node(m, m->desk, m->desk->focus);
397                 }
398         }
399         pointer_follows_monitor = pfm_backup;
400         pointer_follows_focus = pff_backup;
401         auto_raise = true;
402
403         disable_motion_recorder();
404 }
405
406 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
407 {
408         if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
409                 if (action == XCB_EWMH_WM_STATE_ADD) {
410                         set_state(m, d, n, STATE_FULLSCREEN);
411                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
412                         if (n->client->state == STATE_FULLSCREEN) {
413                                 set_state(m, d, n, n->client->last_state);
414                         }
415                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
416                         set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
417                 }
418                 arrange(m, d);
419         } else if (state == ewmh->_NET_WM_STATE_BELOW) {
420                 if (action == XCB_EWMH_WM_STATE_ADD) {
421                         set_layer(m, d, n, LAYER_BELOW);
422                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
423                         if (n->client->layer == LAYER_BELOW) {
424                                 set_layer(m, d, n, n->client->last_layer);
425                         }
426                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
427                         set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
428                 }
429         } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
430                 if (action == XCB_EWMH_WM_STATE_ADD) {
431                         set_layer(m, d, n, LAYER_ABOVE);
432                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
433                         if (n->client->layer == LAYER_ABOVE) {
434                                 set_layer(m, d, n, n->client->last_layer);
435                         }
436                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
437                         set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
438                 }
439         } else if (state == ewmh->_NET_WM_STATE_STICKY) {
440                 if (action == XCB_EWMH_WM_STATE_ADD) {
441                         set_sticky(m, d, n, true);
442                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
443                         set_sticky(m, d, n, false);
444                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
445                         set_sticky(m, d, n, !n->sticky);
446                 }
447         } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
448                 if (action == XCB_EWMH_WM_STATE_ADD) {
449                         set_urgent(m, d, n, true);
450                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
451                         set_urgent(m, d, n, false);
452                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
453                         set_urgent(m, d, n, !n->client->urgent);
454                 }
455 #define HANDLE_WM_STATE(s)  \
456         } else if (state == ewmh->_NET_WM_STATE_##s) { \
457                 if (action == XCB_EWMH_WM_STATE_ADD) { \
458                         n->client->wm_flags |= WM_FLAG_##s; \
459                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
460                         n->client->wm_flags &= ~WM_FLAG_##s; \
461                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
462                         n->client->wm_flags ^= WM_FLAG_##s; \
463                 } \
464                 ewmh_wm_state_update(n);
465         HANDLE_WM_STATE(MODAL)
466         HANDLE_WM_STATE(MAXIMIZED_VERT)
467         HANDLE_WM_STATE(MAXIMIZED_HORZ)
468         HANDLE_WM_STATE(SHADED)
469         HANDLE_WM_STATE(SKIP_TASKBAR)
470         HANDLE_WM_STATE(SKIP_PAGER)
471         HANDLE_WM_STATE(HIDDEN)
472         }
473 #undef HANDLE_WM_STATE
474 }
475
476 void process_error(xcb_generic_event_t *evt)
477 {
478         xcb_request_error_t *e = (xcb_request_error_t *) evt;
479         warn("Failed request: %s, %s: 0x%08X.\n", xcb_event_get_request_label(e->major_opcode), xcb_event_get_error_label(e->error_code), e->bad_value);
480 }