]> git.lizzy.rs Git - bspwm.git/blob - events.c
Fix typo
[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     switch (e->detail) {
314                 case XCB_BUTTON_INDEX_1:
315                         if (click_to_focus && cleaned_mask(e->state) == XCB_NONE) {
316                                 xcb_allow_events(dpy, XCB_ALLOW_REPLAY_POINTER, e->time);
317                                 xcb_flush(dpy);
318                                 grab_pointer(ACTION_FOCUS);
319                         } else {
320                                 grab_pointer(ACTION_MOVE);
321                         }
322                         break;
323                 case XCB_BUTTON_INDEX_2:
324                         grab_pointer(ACTION_RESIZE_SIDE);
325                         break;
326                 case XCB_BUTTON_INDEX_3:
327                         grab_pointer(ACTION_RESIZE_CORNER);
328                         break;
329         }
330 }
331
332 void enter_notify(xcb_generic_event_t *evt)
333 {
334         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
335         xcb_window_t win = e->event;
336
337         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
338             (mon->desk->focus != NULL &&
339              mon->desk->focus->id == win)) {
340                 return;
341         }
342
343         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
344
345         if (wa == NULL) {
346                 return;
347         }
348
349         if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
350                 enable_motion_recorder();
351         } else {
352                 disable_motion_recorder();
353         }
354 }
355
356 void motion_notify(xcb_generic_event_t *evt)
357 {
358         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
359
360         static uint16_t last_motion_x = 0, last_motion_y = 0;
361         static xcb_timestamp_t last_motion_time = 0;
362
363         int64_t dtime = e->time - last_motion_time;
364
365         if (dtime > 1000) {
366                 last_motion_time = e->time;
367                 last_motion_x = e->event_x;
368                 last_motion_y = e->event_y;
369                 return;
370         }
371
372         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
373         if (mdist < 10) {
374                 return;
375         }
376
377         xcb_window_t win = XCB_NONE;
378         xcb_point_t pt = {e->root_x, e->root_y};
379         query_pointer(&win, NULL);
380
381         bool pfm_backup = pointer_follows_monitor;
382         bool pff_backup = pointer_follows_focus;
383         auto_raise = false;
384         pointer_follows_monitor = false;
385         pointer_follows_focus = false;
386         coordinates_t loc;
387         if (locate_window(win, &loc)) {
388                 if (loc.node != mon->desk->focus) {
389                         focus_node(loc.monitor, loc.desktop, loc.node);
390                 }
391         } else {
392                 monitor_t *m = monitor_from_point(pt);
393                 if (m != NULL && m != mon) {
394                         focus_node(m, m->desk, m->desk->focus);
395                 }
396         }
397         pointer_follows_monitor = pfm_backup;
398         pointer_follows_focus = pff_backup;
399         auto_raise = true;
400
401         disable_motion_recorder();
402 }
403
404 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
405 {
406         if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
407                 if (action == XCB_EWMH_WM_STATE_ADD) {
408                         set_state(m, d, n, STATE_FULLSCREEN);
409                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
410                         if (n->client->state == STATE_FULLSCREEN) {
411                                 set_state(m, d, n, n->client->last_state);
412                         }
413                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
414                         set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
415                 }
416                 arrange(m, d);
417         } else if (state == ewmh->_NET_WM_STATE_BELOW) {
418                 if (action == XCB_EWMH_WM_STATE_ADD) {
419                         set_layer(m, d, n, LAYER_BELOW);
420                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
421                         if (n->client->layer == LAYER_BELOW) {
422                                 set_layer(m, d, n, n->client->last_layer);
423                         }
424                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
425                         set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
426                 }
427         } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
428                 if (action == XCB_EWMH_WM_STATE_ADD) {
429                         set_layer(m, d, n, LAYER_ABOVE);
430                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
431                         if (n->client->layer == LAYER_ABOVE) {
432                                 set_layer(m, d, n, n->client->last_layer);
433                         }
434                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
435                         set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
436                 }
437         } else if (state == ewmh->_NET_WM_STATE_STICKY) {
438                 if (action == XCB_EWMH_WM_STATE_ADD) {
439                         set_sticky(m, d, n, true);
440                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
441                         set_sticky(m, d, n, false);
442                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
443                         set_sticky(m, d, n, !n->sticky);
444                 }
445         } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
446                 if (action == XCB_EWMH_WM_STATE_ADD) {
447                         set_urgent(m, d, n, true);
448                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
449                         set_urgent(m, d, n, false);
450                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
451                         set_urgent(m, d, n, !n->client->urgent);
452                 }
453 #define HANDLE_WM_STATE(s)  \
454         } else if (state == ewmh->_NET_WM_STATE_##s) { \
455                 if (action == XCB_EWMH_WM_STATE_ADD) { \
456                         n->client->wm_flags |= WM_FLAG_##s; \
457                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
458                         n->client->wm_flags &= ~WM_FLAG_##s; \
459                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
460                         n->client->wm_flags ^= WM_FLAG_##s; \
461                 } \
462                 ewmh_wm_state_update(n);
463         HANDLE_WM_STATE(MODAL)
464         HANDLE_WM_STATE(MAXIMIZED_VERT)
465         HANDLE_WM_STATE(MAXIMIZED_HORZ)
466         HANDLE_WM_STATE(SHADED)
467         HANDLE_WM_STATE(SKIP_TASKBAR)
468         HANDLE_WM_STATE(SKIP_PAGER)
469         HANDLE_WM_STATE(HIDDEN)
470         }
471 #undef HANDLE_WM_STATE
472 }
473
474 void process_error(xcb_generic_event_t *evt)
475 {
476         xcb_request_error_t *e = (xcb_request_error_t *) evt;
477         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);
478 }