]> git.lizzy.rs Git - bspwm.git/blob - events.c
New setting: merge_overlapping_monitors
[bspwm.git] / events.c
1 /* Copyright (c) 2012-2014, 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  * The views and conclusions contained in the software and documentation are those
25  * of the authors and should not be interpreted as representing official policies,
26  * either expressed or implied, of the FreeBSD Project.
27  */
28
29 #include <stdlib.h>
30 #include "bspwm.h"
31 #include "ewmh.h"
32 #include "monitor.h"
33 #include "query.h"
34 #include "settings.h"
35 #include "tree.h"
36 #include "window.h"
37 #include "events.h"
38
39 void handle_event(xcb_generic_event_t *evt)
40 {
41         uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
42         switch (resp_type) {
43                 case XCB_MAP_REQUEST:
44                         map_request(evt);
45                         break;
46                 case XCB_DESTROY_NOTIFY:
47                         destroy_notify(evt);
48                         break;
49                 case XCB_UNMAP_NOTIFY:
50                         unmap_notify(evt);
51                         break;
52                 case XCB_CLIENT_MESSAGE:
53                         client_message(evt);
54                         break;
55                 case XCB_CONFIGURE_REQUEST:
56                         configure_request(evt);
57                         break;
58                 case XCB_PROPERTY_NOTIFY:
59                         property_notify(evt);
60                         break;
61                 case XCB_ENTER_NOTIFY:
62                         enter_notify(evt);
63                         break;
64                 case XCB_MOTION_NOTIFY:
65                         motion_notify(evt);
66                         break;
67                 case XCB_FOCUS_IN:
68                         focus_in(evt);
69                         break;
70                 default:
71                         if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
72                                 update_monitors();
73                         break;
74         }
75 }
76
77 void map_request(xcb_generic_event_t *evt)
78 {
79         xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
80
81         PRINTF("map request %X\n", e->window);
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         PRINTF("configure request %X\n", e->window);
91
92         coordinates_t loc;
93         bool is_managed = locate_window(e->window, &loc);
94         client_t *c = (is_managed ? loc.node->client : NULL);
95         int w = 0, h = 0;
96
97         if (is_managed && !is_floating(c)) {
98                 if (e->value_mask & XCB_CONFIG_WINDOW_X)
99                         c->floating_rectangle.x = e->x;
100                 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
101                         c->floating_rectangle.y = e->y;
102                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
103                         w = e->width;
104                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
105                         h = e->height;
106
107                 if (w != 0) {
108                         restrain_floating_width(c, &w);
109                         c->floating_rectangle.width = w;
110                 }
111
112                 if (h != 0) {
113                         restrain_floating_height(c, &h);
114                         c->floating_rectangle.height = h;
115                 }
116
117                 xcb_configure_notify_event_t evt;
118                 xcb_rectangle_t rect;
119                 xcb_window_t win = c->window;
120                 unsigned int bw = c->border_width;
121
122                 if (c->fullscreen)
123                         rect = loc.monitor->rectangle;
124                 else
125                         rect = c->tiled_rectangle;
126
127                 evt.response_type = XCB_CONFIGURE_NOTIFY;
128                 evt.event = win;
129                 evt.window = win;
130                 evt.above_sibling = XCB_NONE;
131                 evt.x = rect.x;
132                 evt.y = rect.y;
133                 evt.width = rect.width;
134                 evt.height = rect.height;
135                 evt.border_width = bw;
136                 evt.override_redirect = false;
137
138                 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
139
140                 if (c->pseudo_tiled)
141                         arrange(loc.monitor, loc.desktop);
142         } else {
143                 uint16_t mask = 0;
144                 uint32_t values[7];
145                 unsigned short i = 0;
146
147                 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
148                         mask |= XCB_CONFIG_WINDOW_X;
149                         values[i++] = e->x;
150                         if (is_managed)
151                                 c->floating_rectangle.x = e->x;
152                 }
153
154                 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
155                         mask |= XCB_CONFIG_WINDOW_Y;
156                         values[i++] = e->y;
157                         if (is_managed)
158                                 c->floating_rectangle.y = e->y;
159                 }
160
161                 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
162                         mask |= XCB_CONFIG_WINDOW_WIDTH;
163                         w = e->width;
164                         if (is_managed) {
165                                 restrain_floating_width(c, &w);
166                                 c->floating_rectangle.width = w;
167                         }
168                         values[i++] = w;
169                 }
170
171                 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
172                         mask |= XCB_CONFIG_WINDOW_HEIGHT;
173                         h = e->height;
174                         if (is_managed) {
175                                 restrain_floating_height(c, &h);
176                                 c->floating_rectangle.height = h;
177                         }
178                         values[i++] = h;
179                 }
180
181                 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
182                         mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
183                         values[i++] = e->border_width;
184                 }
185
186                 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
187                         mask |= XCB_CONFIG_WINDOW_SIBLING;
188                         values[i++] = e->sibling;
189                 }
190
191                 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
192                         mask |= XCB_CONFIG_WINDOW_STACK_MODE;
193                         values[i++] = e->stack_mode;
194                 }
195
196                 xcb_configure_window(dpy, e->window, mask, values);
197         }
198
199         if (is_managed)
200                 translate_client(monitor_from_client(c), loc.monitor, c);
201 }
202
203 void destroy_notify(xcb_generic_event_t *evt)
204 {
205         xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
206
207         PRINTF("destroy notify %X\n", e->window);
208
209         unmanage_window(e->window);
210 }
211
212 void unmap_notify(xcb_generic_event_t *evt)
213 {
214         xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
215
216         PRINTF("unmap notify %X\n", e->window);
217
218         unmanage_window(e->window);
219 }
220
221 void property_notify(xcb_generic_event_t *evt)
222 {
223         xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
224
225         /* PRINTF("property notify %X\n", e->window); */
226
227         if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
228                 return;
229
230         coordinates_t loc;
231         if (!locate_window(e->window, &loc))
232                         return;
233
234         if (e->atom == XCB_ATOM_WM_HINTS) {
235                 xcb_icccm_wm_hints_t hints;
236                 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
237                     (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
238                         set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
239         } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
240                 client_t *c = loc.node->client;
241                 xcb_size_hints_t size_hints;
242                 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
243                     (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
244                         c->min_width = size_hints.min_width;
245                         c->max_width = size_hints.max_width;
246                         c->min_height = size_hints.min_height;
247                         c->max_height = size_hints.max_height;
248                         int w = c->floating_rectangle.width;
249                         int h = c->floating_rectangle.height;
250                         restrain_floating_size(c, &w, &h);
251                         c->floating_rectangle.width = w;
252                         c->floating_rectangle.height = h;
253                         arrange(loc.monitor, loc.desktop);
254                 }
255         }
256 }
257
258 void client_message(xcb_generic_event_t *evt)
259 {
260         xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
261
262         PRINTF("client message %X %u\n", e->window, e->type);
263
264         if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
265                 coordinates_t loc;
266                 if (ewmh_locate_desktop(e->data.data32[0], &loc))
267                         focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
268                 return;
269         }
270
271         coordinates_t loc;
272         if (!locate_window(e->window, &loc))
273                 return;
274
275         if (e->type == ewmh->_NET_WM_STATE) {
276                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
277                 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
278         } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
279                 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
280                     loc.node == mon->desk->focus)
281                         return;
282                 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
283                         set_fullscreen(loc.desktop->focus, false);
284                         arrange(loc.monitor, loc.desktop);
285                 }
286                 focus_node(loc.monitor, loc.desktop, loc.node);
287         } else if (e->type == ewmh->_NET_WM_DESKTOP) {
288                 coordinates_t dloc;
289                 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
290                         transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
291         } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
292                 window_close(loc.node);
293         }
294 }
295
296 void focus_in(xcb_generic_event_t *evt)
297 {
298         xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
299
300         /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
301
302         if (e->mode == XCB_NOTIFY_MODE_GRAB ||
303             e->mode == XCB_NOTIFY_MODE_UNGRAB)
304                 return;
305         /* prevent focus stealing */
306         if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
307              e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
308              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
309              e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
310             (mon->desk->focus == NULL ||
311              mon->desk->focus->client->window != e->event))
312                 update_input_focus();
313 }
314
315 void enter_notify(xcb_generic_event_t *evt)
316 {
317         xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
318         xcb_window_t win = e->event;
319
320         PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
321
322         if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
323             (mon->desk->focus != NULL &&
324              mon->desk->focus->client->window == win))
325                 return;
326
327         enable_motion_recorder();
328 }
329
330 void motion_notify(xcb_generic_event_t *evt)
331 {
332         PUTS("motion notify");
333
334         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
335
336         int dtime = e->time - last_motion_time;
337         if (dtime > 1000) {
338                 last_motion_time = e->time;
339                 last_motion_x = e->event_x;
340                 last_motion_y = e->event_y;
341                 return;
342         }
343
344         int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
345         if (mdist < 10)
346                 return;
347
348         disable_motion_recorder();
349
350         xcb_window_t win = XCB_NONE;
351         xcb_point_t pt = {e->root_x, e->root_y};
352         query_pointer(&win, NULL);
353
354         bool backup = pointer_follows_monitor;
355         auto_raise = false;
356         pointer_follows_monitor = false;
357         if (!window_focus(win)) {
358                 monitor_t *m = monitor_from_point(pt);
359                 if (m != NULL && m != mon)
360                         focus_node(m, m->desk, m->desk->focus);
361         }
362         pointer_follows_monitor = backup;
363         auto_raise = true;
364 }
365
366 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
367 {
368         if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
369                 if (action == XCB_EWMH_WM_STATE_ADD)
370                         set_fullscreen(n, true);
371                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
372                         set_fullscreen(n, false);
373                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
374                         set_fullscreen(n, !n->client->fullscreen);
375                 arrange(m, d);
376         } else if (state == ewmh->_NET_WM_STATE_STICKY) {
377                 if (action == XCB_EWMH_WM_STATE_ADD)
378                         set_sticky(m, d, n, true);
379                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
380                         set_sticky(m, d, n, false);
381                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
382                         set_sticky(m, d, n, !n->client->sticky);
383         } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
384                 if (action == XCB_EWMH_WM_STATE_ADD)
385                         set_urgency(m, d, n, true);
386                 else if (action == XCB_EWMH_WM_STATE_REMOVE)
387                         set_urgency(m, d, n, false);
388                 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
389                         set_urgency(m, d, n, !n->client->urgent);
390         }
391 }