]> git.lizzy.rs Git - bspwm.git/blob - events.c
Restore the tiled rectangles of nodes and clients
[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                 case 0:
67                         process_error(evt);
68                         break;
69                 default:
70                         if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
71                                 update_monitors();
72                         break;
73         }
74 }
75
76 void map_request(xcb_generic_event_t *evt)
77 {
78         xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
79
80         PRINTF("map request %X\n", e->window);
81
82         schedule_window(e->window);
83 }
84
85 void configure_request(xcb_generic_event_t *evt)
86 {
87         xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
88
89         PRINTF("configure request %X\n", e->window);
90
91         coordinates_t loc;
92         bool is_managed = locate_window(e->window, &loc);
93         client_t *c = (is_managed ? loc.node->client : NULL);
94         int w = 0, h = 0;
95
96         if (is_managed && !IS_FLOATING(c)) {
97                 if (e->value_mask & XCB_CONFIG_WINDOW_X)
98                         c->floating_rectangle.x = e->x;
99                 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
100                         c->floating_rectangle.y = e->y;
101                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
102                         w = e->width;
103                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
104                         h = e->height;
105
106                 if (w != 0) {
107                         restrain_floating_width(c, &w);
108                         c->floating_rectangle.width = w;
109                 }
110
111                 if (h != 0) {
112                         restrain_floating_height(c, &h);
113                         c->floating_rectangle.height = h;
114                 }
115
116                 xcb_configure_notify_event_t evt;
117                 xcb_window_t win = c->window;
118                 unsigned int bw = c->border_width;
119
120                 xcb_rectangle_t rect = get_rectangle(loc.monitor, c);
121
122                 evt.response_type = XCB_CONFIGURE_NOTIFY;
123                 evt.event = win;
124                 evt.window = win;
125                 evt.above_sibling = XCB_NONE;
126                 evt.x = rect.x;
127                 evt.y = rect.y;
128                 evt.width = rect.width;
129                 evt.height = rect.height;
130                 evt.border_width = bw;
131                 evt.override_redirect = false;
132
133                 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
134
135                 if (c->state == STATE_PSEUDO_TILED) {
136                         arrange(loc.monitor, loc.desktop);
137                 }
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
200 void destroy_notify(xcb_generic_event_t *evt)
201 {
202         xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
203
204         PRINTF("destroy notify %X\n", e->window);
205
206         unmanage_window(e->window);
207 }
208
209 void unmap_notify(xcb_generic_event_t *evt)
210 {
211         xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
212
213         PRINTF("unmap notify %X\n", e->window);
214
215         unmanage_window(e->window);
216 }
217
218 void property_notify(xcb_generic_event_t *evt)
219 {
220         xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
221
222         /* PRINTF("property notify %X\n", e->window); */
223
224         if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
225                 return;
226
227         coordinates_t loc;
228         if (!locate_window(e->window, &loc))
229                         return;
230
231         if (e->atom == XCB_ATOM_WM_HINTS) {
232                 xcb_icccm_wm_hints_t hints;
233                 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
234                     (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
235                         set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
236         } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
237                 client_t *c = loc.node->client;
238                 xcb_size_hints_t size_hints;
239                 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
240                     (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
241                         c->min_width = size_hints.min_width;
242                         c->max_width = size_hints.max_width;
243                         c->min_height = size_hints.min_height;
244                         c->max_height = size_hints.max_height;
245                         int w = c->floating_rectangle.width;
246                         int h = c->floating_rectangle.height;
247                         restrain_floating_size(c, &w, &h);
248                         c->floating_rectangle.width = w;
249                         c->floating_rectangle.height = h;
250                         arrange(loc.monitor, loc.desktop);
251                 }
252         }
253 }
254
255 void client_message(xcb_generic_event_t *evt)
256 {
257         xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
258
259         PRINTF("client message %X %u\n", e->window, e->type);
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                 return;
266         }
267
268         coordinates_t loc;
269         if (!locate_window(e->window, &loc))
270                 return;
271
272         if (e->type == ewmh->_NET_WM_STATE) {
273                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
274                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
275         } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
276                 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
277                     loc.node == mon->desk->focus)
278                         return;
279                 focus_node(loc.monitor, loc.desktop, loc.node);
280         } else if (e->type == ewmh->_NET_WM_DESKTOP) {
281                 coordinates_t dloc;
282                 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
283                         transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
284         } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
285                 window_close(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         /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
294
295         if (e->mode == XCB_NOTIFY_MODE_GRAB ||
296             e->mode == XCB_NOTIFY_MODE_UNGRAB)
297                 return;
298         /* prevent focus stealing */
299         if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
300              e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
301              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
302              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
303             (mon->desk->focus == NULL ||
304              mon->desk->focus->client->window != e->event))
305                 update_input_focus();
306 }
307
308 void enter_notify(xcb_generic_event_t *evt)
309 {
310         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
311         xcb_window_t win = e->event;
312
313         PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
314
315         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
316             (mon->desk->focus != NULL &&
317              mon->desk->focus->client->window == win)) {
318                 return;
319         }
320
321         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
322
323         if (wa == NULL) {
324                 return;
325         }
326
327         if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
328                 enable_motion_recorder();
329         } else {
330                 disable_motion_recorder();
331         }
332 }
333
334 void motion_notify(xcb_generic_event_t *evt)
335 {
336         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
337
338         PRINTF("motion notify %X %i %i\n", e->event, e->root_x, e->root_y);
339
340         int dtime = e->time - last_motion_time;
341         if (dtime > 1000) {
342                 last_motion_time = e->time;
343                 last_motion_x = e->event_x;
344                 last_motion_y = e->event_y;
345                 return;
346         }
347
348         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
349         if (mdist < 10) {
350                 return;
351         }
352
353         xcb_window_t win = XCB_NONE;
354         xcb_point_t pt = {e->root_x, e->root_y};
355         query_pointer(&win, NULL);
356
357         bool pfm_backup = pointer_follows_monitor;
358         bool pff_backup = pointer_follows_focus;
359         auto_raise = false;
360         pointer_follows_monitor = false;
361         pointer_follows_focus = false;
362         coordinates_t loc;
363         if (locate_window(win, &loc)) {
364                 if (loc.node != mon->desk->focus) {
365                         focus_node(loc.monitor, loc.desktop, loc.node);
366                 }
367         } else {
368                 monitor_t *m = monitor_from_point(pt);
369                 if (m != NULL && m != mon) {
370                         focus_node(m, m->desk, m->desk->focus);
371                 }
372         }
373         pointer_follows_monitor = pfm_backup;
374         pointer_follows_focus = pff_backup;
375         auto_raise = true;
376
377         disable_motion_recorder();
378 }
379
380 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
381 {
382         if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
383                 if (action == XCB_EWMH_WM_STATE_ADD) {
384                         set_state(m, d, n, STATE_FULLSCREEN);
385                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
386                         set_state(m, d, n, n->client->last_state);
387                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
388                         set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
389                 }
390                 arrange(m, d);
391         } else if (state == ewmh->_NET_WM_STATE_BELOW) {
392                 if (action == XCB_EWMH_WM_STATE_ADD) {
393                         set_layer(m, d, n, LAYER_BELOW);
394                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
395                         set_layer(m, d, n, LAYER_NORMAL);
396                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
397                         set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
398                 }
399         } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
400                 if (action == XCB_EWMH_WM_STATE_ADD) {
401                         set_layer(m, d, n, LAYER_ABOVE);
402                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
403                         set_layer(m, d, n, n->client->last_layer);
404                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
405                         set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
406                 }
407         } else if (state == ewmh->_NET_WM_STATE_STICKY) {
408                 if (action == XCB_EWMH_WM_STATE_ADD) {
409                         set_sticky(m, d, n, true);
410                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
411                         set_sticky(m, d, n, false);
412                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
413                         set_sticky(m, d, n, !n->client->sticky);
414                 }
415         } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
416                 if (action == XCB_EWMH_WM_STATE_ADD) {
417                         set_urgency(m, d, n, true);
418                 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
419                         set_urgency(m, d, n, false);
420                 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
421                         set_urgency(m, d, n, !n->client->urgent);
422                 }
423         }
424 }
425
426 void process_error(xcb_generic_event_t *evt)
427 {
428         xcb_request_error_t *e = (xcb_request_error_t *) evt;
429         warn("Failed request: %s, %s: %d.\n", xcb_event_get_request_label(e->major_opcode), xcb_event_get_error_label(e->error_code), e->bad_value);
430 }