]> git.lizzy.rs Git - bspwm.git/blob - events.c
Remove obsolete headers
[bspwm.git] / events.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <xcb/xcb.h>
5 #include <xcb/xcb_event.h>
6 #include <xcb/xcb_icccm.h>
7 #include "types.h"
8 #include "bspwm.h"
9 #include "settings.h"
10 #include "helpers.h"
11 #include "window.h"
12 #include "events.h"
13 #include "tree.h"
14 #include "rules.h"
15 #include "ewmh.h"
16
17 void handle_event(xcb_generic_event_t *evt)
18 {
19     switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
20         case XCB_MAP_REQUEST:
21             map_request(evt);
22             break;
23         case XCB_DESTROY_NOTIFY:
24             destroy_notify(evt);
25             break;
26         case XCB_UNMAP_NOTIFY:
27             unmap_notify(evt);
28             break;
29         case XCB_CLIENT_MESSAGE:
30             client_message(evt);
31             break;
32         case XCB_CONFIGURE_REQUEST:
33             configure_request(evt);
34             break;
35         case XCB_PROPERTY_NOTIFY:
36             property_notify(evt);
37             break;
38         case XCB_ENTER_NOTIFY:
39             enter_notify(evt);
40             break;
41         case XCB_MOTION_NOTIFY:
42             motion_notify(evt);
43             break;
44         case XCB_BUTTON_RELEASE:
45             button_release();
46             break;
47         default:
48             break;
49     }
50 }
51
52 void map_request(xcb_generic_event_t *evt)
53 {
54     xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
55
56     PRINTF("map request %X\n", e->window);
57
58     manage_window(mon, mon->desk, e->window);
59 }
60
61 void configure_request(xcb_generic_event_t *evt)
62 {
63     xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
64
65     PRINTF("configure request %X\n", e->window);
66
67     window_location_t loc;
68     bool is_managed = locate_window(e->window, &loc);
69
70     if (!is_managed || is_floating(loc.node->client)) {
71         uint16_t mask = 0;
72         uint32_t values[7];
73         unsigned short i = 0;
74
75         if (e->value_mask & XCB_CONFIG_WINDOW_X) {
76             mask |= XCB_CONFIG_WINDOW_X;
77             values[i++] = e->x;
78             if (is_managed)
79                 loc.node->client->floating_rectangle.x = e->x;
80         }
81
82         if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
83             mask |= XCB_CONFIG_WINDOW_Y;
84             values[i++] = e->y;
85             if (is_managed)
86                 loc.node->client->floating_rectangle.y = e->y;
87         }
88
89         if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
90             mask |= XCB_CONFIG_WINDOW_WIDTH;
91             values[i++] = e->width;
92             if (is_managed)
93                 loc.node->client->floating_rectangle.width = e->width;
94         }
95
96         if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
97             mask |= XCB_CONFIG_WINDOW_HEIGHT;
98             values[i++] = e->height;
99             if (is_managed)
100                 loc.node->client->floating_rectangle.height = e->height;
101         }
102
103         if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
104             mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
105             values[i++] = e->border_width;
106         }
107
108         if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
109             mask |= XCB_CONFIG_WINDOW_SIBLING;
110             values[i++] = e->sibling;
111         }
112
113         if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
114             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
115             values[i++] = e->stack_mode;
116         }
117
118         xcb_configure_window(dpy, e->window, mask, values);
119         if (is_managed)
120             window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
121     } else {
122         xcb_configure_notify_event_t evt;
123         xcb_rectangle_t rect;
124         unsigned int bw;
125         xcb_window_t win = loc.node->client->window;
126
127         if (is_tiled(loc.node->client)) {
128             rect = loc.node->client->tiled_rectangle;
129             bw = border_width;
130         } else {
131             rect = loc.monitor->rectangle;
132             bw = 0;
133         }
134
135         evt.response_type = XCB_CONFIGURE_NOTIFY;
136         evt.event = win;
137         evt.window = win;
138         evt.above_sibling = XCB_NONE;
139         evt.x = rect.x;
140         evt.y = rect.y;
141         evt.width = rect.width;
142         evt.height = rect.height;
143         evt.border_width = bw;
144         evt.override_redirect = false;
145
146         xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
147     }
148 }
149
150 void destroy_notify(xcb_generic_event_t *evt)
151 {
152     xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
153
154     PRINTF("destroy notify %X\n", e->window);
155
156     window_location_t loc;
157     if (locate_window(e->window, &loc)) {
158         remove_node(loc.desktop, loc.node);
159         arrange(loc.monitor, loc.desktop);
160     }
161 }
162
163 void unmap_notify(xcb_generic_event_t *evt)
164 {
165     xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
166
167     PRINTF("unmap notify %X\n", e->window);
168
169     window_location_t loc;
170     if (locate_window(e->window, &loc)) {
171         remove_node(loc.desktop, loc.node);
172         arrange(loc.monitor, loc.desktop);
173     }
174 }
175
176 void property_notify(xcb_generic_event_t *evt)
177 {
178     xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
179     xcb_icccm_wm_hints_t hints;
180
181     /* PRINTF("property notify %X\n", e->window); */
182
183     if (e->atom != XCB_ATOM_WM_HINTS)
184         return;
185
186     window_location_t loc;
187     if (locate_window(e->window, &loc)) {
188         if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1) {
189             uint32_t urgent = xcb_icccm_wm_hints_get_urgency(&hints);
190             if (urgent != 0 && loc.node != mon->desk->focus) {
191                 loc.node->client->urgent = urgent;
192                 put_status();
193                 if (loc.monitor->desk == loc.desktop)
194                     arrange(loc.monitor, loc.desktop);
195             }
196         }
197     }
198 }
199
200 void enter_notify(xcb_generic_event_t *evt)
201 {
202     xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
203
204     PRINTF("enter notify %X %d %d\n", e->event, e->mode, e->detail);
205
206     if (!focus_follows_mouse 
207             || (e->mode != XCB_NOTIFY_MODE_NORMAL && e->detail == XCB_NOTIFY_DETAIL_INFERIOR)
208             || (pointer_position.x == e->root_x && pointer_position.y == e->root_y)
209             || last_entered == e->event)
210         return;
211
212     window_location_t loc;
213     if (locate_window(e->event, &loc)) {
214         select_monitor(loc.monitor);
215         focus_node(loc.monitor, loc.desktop, loc.node, true);
216         pointer_position = (xcb_point_t) {e->root_x, e->root_y};
217         last_entered = e->event;
218     }
219 }
220
221 void client_message(xcb_generic_event_t *evt)
222 {
223     xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
224
225     PRINTF("client message %X %u\n", e->window, e->type);
226
227     if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
228         desktop_location_t loc;
229         if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
230             select_monitor(loc.monitor);
231             select_desktop(loc.desktop);
232         }
233         return;
234     }
235
236     window_location_t loc;
237     if (!locate_window(e->window, &loc))
238         return;
239
240     if (e->type == ewmh->_NET_WM_STATE) {
241         handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
242         handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
243     } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
244         if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node)
245             toggle_fullscreen(loc.monitor, loc.desktop->focus->client);
246         select_monitor(loc.monitor);
247         select_desktop(loc.desktop);
248         focus_node(loc.monitor, loc.desktop, loc.node, true);
249         arrange(loc.monitor, loc.desktop);
250     }
251 }
252
253 void mouse_do(mouse_action_t mac)
254 {
255     PRINTF("mouse action %u\n", mac);
256
257     xcb_window_t win;
258     xcb_point_t pos;
259
260     xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
261     if (qpr != NULL) {
262         pos = (xcb_point_t) {qpr->root_x, qpr->root_y};
263         win = qpr->child;
264         free(qpr);
265     } else {
266         return;
267     }
268
269     window_location_t loc;
270     if (locate_window(win, &loc)) {
271         client_t *c = loc.node->client;
272         switch (mac)  {
273             case MOUSE_FOCUS:
274                 focus_node(loc.monitor, loc.desktop, loc.node, true);
275                 break;
276             case MOUSE_MOVE:
277             case MOUSE_RESIZE:
278                 if (is_tiled(loc.node->client)) {
279                     loc.node->client->floating_rectangle = loc.node->client->tiled_rectangle;
280                     toggle_floating(loc.node);
281                     arrange(loc.monitor, loc.desktop);
282                 } else if (loc.node->client->fullscreen) {
283                     return;
284                 }
285                 frozen_pointer->monitor = loc.monitor;
286                 frozen_pointer->desktop = loc.desktop;
287                 frozen_pointer->node = loc.node;
288                 frozen_pointer->rectangle = c->floating_rectangle;
289                 frozen_pointer->position = pos;
290                 frozen_pointer->action = mac;
291                 if (mac == MOUSE_RESIZE) {
292                     int16_t mid_x, mid_y;
293                     mid_x = c->floating_rectangle.x + (c->floating_rectangle.width / 2);
294                     mid_y = c->floating_rectangle.y + (c->floating_rectangle.height / 2);
295                     if (pos.x > mid_x) {
296                         if (pos.y > mid_y)
297                             frozen_pointer->corner = BOTTOM_RIGHT;
298                         else
299                             frozen_pointer->corner = TOP_RIGHT;
300                     } else {
301                         if (pos.y > mid_y)
302                             frozen_pointer->corner = BOTTOM_LEFT;
303                         else
304                             frozen_pointer->corner = TOP_LEFT;
305                     }
306                 }
307                 xcb_grab_pointer(dpy, false, root, XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_CURRENT_TIME);
308                 break;
309         }
310     }
311 }
312
313 void motion_notify(xcb_generic_event_t *evt)
314 {
315     xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
316
317     int16_t delta_x, delta_y, x, y, w, h;
318     uint16_t width, height;
319
320     monitor_t *m = frozen_pointer->monitor;
321     desktop_t *d = frozen_pointer->desktop;
322     node_t *n = frozen_pointer->node;
323     client_t *c = n->client;
324     xcb_rectangle_t rect = frozen_pointer->rectangle;
325     xcb_window_t win = c->window;
326
327     x = y = 0;
328     w = h = 1;
329
330     /* PRINTF("motion notify %X\n", win); */
331
332     delta_x = e->root_x - frozen_pointer->position.x;
333     delta_y = e->root_y - frozen_pointer->position.y;
334
335     switch (frozen_pointer->action) {
336         case MOUSE_MOVE:
337             x = rect.x + delta_x;
338             y = rect.y + delta_y;
339             window_move(win, x, y);
340             break;
341         case MOUSE_RESIZE:
342             switch (frozen_pointer->corner) {
343                 case TOP_LEFT:
344                     x = rect.x + delta_x;
345                     y = rect.y + delta_y;
346                     w = rect.width - delta_x;
347                     h = rect.height - delta_y;
348                     break;
349                 case TOP_RIGHT:
350                     x = rect.x;
351                     y = rect.y + delta_y;
352                     w = rect.width + delta_x;
353                     h = rect.height - delta_y;
354                     break;
355                 case BOTTOM_LEFT:
356                     x = rect.x + delta_x;
357                     y = rect.y;
358                     w = rect.width - delta_x;
359                     h = rect.height + delta_y;
360                     break;
361                 case BOTTOM_RIGHT:
362                     x = rect.x;
363                     y = rect.y;
364                     w = rect.width + delta_x;
365                     h = rect.height + delta_y;
366                     break;
367             }
368             width = MAX(1, w);
369             height = MAX(1, h);
370             window_move_resize(win, x, y, width, height);
371             c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
372             window_draw_border(n, d->focus == n, mon == m);
373             break;
374         case MOUSE_FOCUS:
375             break;
376     }
377 }
378
379 void button_release(void)
380 {
381     PUTS("button release");
382
383     xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
384     update_floating_rectangle(frozen_pointer->node->client);
385     monitor_t *m = underlying_monitor(frozen_pointer->node->client);
386     if (m != NULL && m != frozen_pointer->monitor) {
387         transfer_node(frozen_pointer->monitor, frozen_pointer->desktop, m, m->desk, frozen_pointer->node);
388         select_monitor(m);
389     }
390 }
391
392 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
393 {
394     if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
395         bool fs = n->client->fullscreen;
396         if (action == XCB_EWMH_WM_STATE_TOGGLE
397                 || (fs && action == XCB_EWMH_WM_STATE_REMOVE)
398                 || (!fs && action == XCB_EWMH_WM_STATE_ADD)) {
399             toggle_fullscreen(m, n->client);
400             arrange(m, d);
401         }
402     }
403 }