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