]> git.lizzy.rs Git - bspwm.git/blob - events.c
Unlink the socket before binding
[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_rectangle_t rect;
118                 xcb_window_t win = c->window;
119                 unsigned int bw = c->border_width;
120
121                 if (c->fullscreen)
122                         rect = loc.monitor->rectangle;
123                 else
124                         rect = c->tiled_rectangle;
125
126                 evt.response_type = XCB_CONFIGURE_NOTIFY;
127                 evt.event = win;
128                 evt.window = win;
129                 evt.above_sibling = XCB_NONE;
130                 evt.x = rect.x;
131                 evt.y = rect.y;
132                 evt.width = rect.width;
133                 evt.height = rect.height;
134                 evt.border_width = bw;
135                 evt.override_redirect = false;
136
137                 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
138
139                 if (c->pseudo_tiled)
140                         arrange(loc.monitor, loc.desktop);
141         } else {
142                 uint16_t mask = 0;
143                 uint32_t values[7];
144                 unsigned short i = 0;
145
146                 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
147                         mask |= XCB_CONFIG_WINDOW_X;
148                         values[i++] = e->x;
149                         if (is_managed)
150                                 c->floating_rectangle.x = e->x;
151                 }
152
153                 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
154                         mask |= XCB_CONFIG_WINDOW_Y;
155                         values[i++] = e->y;
156                         if (is_managed)
157                                 c->floating_rectangle.y = e->y;
158                 }
159
160                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
161                         mask |= XCB_CONFIG_WINDOW_WIDTH;
162                         w = e->width;
163                         if (is_managed) {
164                                 restrain_floating_width(c, &w);
165                                 c->floating_rectangle.width = w;
166                         }
167                         values[i++] = w;
168                 }
169
170                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
171                         mask |= XCB_CONFIG_WINDOW_HEIGHT;
172                         h = e->height;
173                         if (is_managed) {
174                                 restrain_floating_height(c, &h);
175                                 c->floating_rectangle.height = h;
176                         }
177                         values[i++] = h;
178                 }
179
180                 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
181                         mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
182                         values[i++] = e->border_width;
183                 }
184
185                 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
186                         mask |= XCB_CONFIG_WINDOW_SIBLING;
187                         values[i++] = e->sibling;
188                 }
189
190                 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
191                         mask |= XCB_CONFIG_WINDOW_STACK_MODE;
192                         values[i++] = e->stack_mode;
193                 }
194
195                 xcb_configure_window(dpy, e->window, mask, values);
196         }
197
198         if (is_managed)
199                 translate_client(monitor_from_client(c), loc.monitor, c);
200 }
201
202 void destroy_notify(xcb_generic_event_t *evt)
203 {
204         xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
205
206         PRINTF("destroy notify %X\n", e->window);
207
208         unmanage_window(e->window);
209 }
210
211 void unmap_notify(xcb_generic_event_t *evt)
212 {
213         xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
214
215         PRINTF("unmap notify %X\n", e->window);
216
217         unmanage_window(e->window);
218 }
219
220 void property_notify(xcb_generic_event_t *evt)
221 {
222         xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
223
224         /* PRINTF("property notify %X\n", e->window); */
225
226         if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
227                 return;
228
229         coordinates_t loc;
230         if (!locate_window(e->window, &loc))
231                         return;
232
233         if (e->atom == XCB_ATOM_WM_HINTS) {
234                 xcb_icccm_wm_hints_t hints;
235                 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
236                     (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
237                         set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
238         } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
239                 client_t *c = loc.node->client;
240                 xcb_size_hints_t size_hints;
241                 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
242                     (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
243                         c->min_width = size_hints.min_width;
244                         c->max_width = size_hints.max_width;
245                         c->min_height = size_hints.min_height;
246                         c->max_height = size_hints.max_height;
247                         int w = c->floating_rectangle.width;
248                         int h = c->floating_rectangle.height;
249                         restrain_floating_size(c, &w, &h);
250                         c->floating_rectangle.width = w;
251                         c->floating_rectangle.height = h;
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         PRINTF("client message %X %u\n", e->window, e->type);
262
263         if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
264                 coordinates_t loc;
265                 if (ewmh_locate_desktop(e->data.data32[0], &loc))
266                         focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
267                 return;
268         }
269
270         coordinates_t loc;
271         if (!locate_window(e->window, &loc))
272                 return;
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                 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
282                         set_fullscreen(loc.desktop->focus, false);
283                         arrange(loc.monitor, loc.desktop);
284                 }
285                 focus_node(loc.monitor, loc.desktop, loc.node);
286         } else if (e->type == ewmh->_NET_WM_DESKTOP) {
287                 coordinates_t dloc;
288                 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
289                         transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
290         } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
291                 window_close(loc.node);
292         }
293 }
294
295 void focus_in(xcb_generic_event_t *evt)
296 {
297         xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
298
299         /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
300
301         if (e->mode == XCB_NOTIFY_MODE_GRAB ||
302             e->mode == XCB_NOTIFY_MODE_UNGRAB)
303                 return;
304         /* prevent focus stealing */
305         if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
306              e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
307              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
308              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
309             (mon->desk->focus == NULL ||
310              mon->desk->focus->client->window != e->event))
311                 update_input_focus();
312 }
313
314 void enter_notify(xcb_generic_event_t *evt)
315 {
316         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
317         xcb_window_t win = e->event;
318
319         PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
320
321         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
322             (mon->desk->focus != NULL &&
323              mon->desk->focus->client->window == win)) {
324                 return;
325         }
326
327         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
328
329         if (wa == NULL) {
330                 return;
331         }
332
333         if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
334                 enable_motion_recorder();
335         } else {
336                 disable_motion_recorder();
337         }
338 }
339
340 void motion_notify(xcb_generic_event_t *evt)
341 {
342         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
343
344         PRINTF("motion notify %X %i %i\n", e->event, e->root_x, e->root_y);
345
346         int dtime = e->time - last_motion_time;
347         if (dtime > 1000) {
348                 last_motion_time = e->time;
349                 last_motion_x = e->event_x;
350                 last_motion_y = e->event_y;
351                 return;
352         }
353
354         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
355         if (mdist < 10) {
356                 return;
357         }
358
359         xcb_window_t win = XCB_NONE;
360         xcb_point_t pt = {e->root_x, e->root_y};
361         query_pointer(&win, NULL);
362
363         bool pfm_backup = pointer_follows_monitor;
364         bool pff_backup = pointer_follows_focus;
365         auto_raise = false;
366         pointer_follows_monitor = false;
367         pointer_follows_focus = false;
368         coordinates_t loc;
369         if (locate_window(win, &loc)) {
370                 if (loc.node != mon->desk->focus) {
371                         focus_node(loc.monitor, loc.desktop, loc.node);
372                 }
373         } else {
374                 monitor_t *m = monitor_from_point(pt);
375                 if (m != NULL && m != mon) {
376                         focus_node(m, m->desk, m->desk->focus);
377                 }
378         }
379         pointer_follows_monitor = pfm_backup;
380         pointer_follows_focus = pff_backup;
381         auto_raise = true;
382
383         disable_motion_recorder();
384 }
385
386 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
387 {
388         if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
389                 if (action == XCB_EWMH_WM_STATE_ADD)
390                         set_fullscreen(n, true);
391                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
392                         set_fullscreen(n, false);
393                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
394                         set_fullscreen(n, !n->client->fullscreen);
395                 arrange(m, d);
396         } else if (state == ewmh->_NET_WM_STATE_STICKY) {
397                 if (action == XCB_EWMH_WM_STATE_ADD)
398                         set_sticky(m, d, n, true);
399                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
400                         set_sticky(m, d, n, false);
401                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
402                         set_sticky(m, d, n, !n->client->sticky);
403         } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
404                 if (action == XCB_EWMH_WM_STATE_ADD)
405                         set_urgency(m, d, n, true);
406                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
407                         set_urgency(m, d, n, false);
408                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
409                         set_urgency(m, d, n, !n->client->urgent);
410         }
411 }
412
413 void process_error(xcb_generic_event_t *evt)
414 {
415         xcb_request_error_t *e = (xcb_request_error_t *) evt;
416         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);
417 }