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