]> git.lizzy.rs Git - bspwm.git/blob - events.c
Simply history removal
[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();
43             break;
44         default:
45             break;
46     }
47 }
48
49 void map_request(xcb_generic_event_t *evt)
50 {
51     xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
52
53     PRINTF("map request %X\n", e->window);
54
55     manage_window(mon, mon->desk, e->window);
56 }
57
58 void configure_request(xcb_generic_event_t *evt)
59 {
60     xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
61
62     PRINTF("configure request %X\n", e->window);
63
64     window_location_t loc;
65     bool is_managed = locate_window(e->window, &loc);
66
67     if (!is_managed || is_floating(loc.node->client)) {
68         uint16_t mask = 0;
69         uint32_t values[7];
70         unsigned short i = 0;
71
72         if (e->value_mask & XCB_CONFIG_WINDOW_X) {
73             mask |= XCB_CONFIG_WINDOW_X;
74             values[i++] = e->x;
75             if (is_managed)
76                 loc.node->client->floating_rectangle.x = e->x;
77         }
78
79         if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
80             mask |= XCB_CONFIG_WINDOW_Y;
81             values[i++] = e->y;
82             if (is_managed)
83                 loc.node->client->floating_rectangle.y = e->y;
84         }
85
86         if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
87             mask |= XCB_CONFIG_WINDOW_WIDTH;
88             values[i++] = e->width;
89             if (is_managed)
90                 loc.node->client->floating_rectangle.width = e->width;
91         }
92
93         if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
94             mask |= XCB_CONFIG_WINDOW_HEIGHT;
95             values[i++] = e->height;
96             if (is_managed)
97                 loc.node->client->floating_rectangle.height = e->height;
98         }
99
100         if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
101             mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
102             values[i++] = e->border_width;
103         }
104
105         if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
106             mask |= XCB_CONFIG_WINDOW_SIBLING;
107             values[i++] = e->sibling;
108         }
109
110         if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
111             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
112             values[i++] = e->stack_mode;
113         }
114
115         xcb_configure_window(dpy, e->window, mask, values);
116         if (is_managed)
117             window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
118     } else {
119         xcb_configure_notify_event_t evt;
120         xcb_rectangle_t rect;
121         unsigned int bw;
122         xcb_window_t win = loc.node->client->window;
123
124         if (is_tiled(loc.node->client)) {
125             rect = loc.node->client->tiled_rectangle;
126             bw = border_width;
127         } else {
128             rect = loc.monitor->rectangle;
129             bw = 0;
130         }
131
132         evt.response_type = XCB_CONFIGURE_NOTIFY;
133         evt.event = win;
134         evt.window = win;
135         evt.above_sibling = XCB_NONE;
136         evt.x = rect.x;
137         evt.y = rect.y;
138         evt.width = rect.width;
139         evt.height = rect.height;
140         evt.border_width = bw;
141         evt.override_redirect = false;
142
143         xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
144     }
145 }
146
147 void destroy_notify(xcb_generic_event_t *evt)
148 {
149     xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
150
151     PRINTF("destroy notify %X\n", e->window);
152
153     window_location_t loc;
154     if (locate_window(e->window, &loc)) {
155         remove_node(loc.desktop, loc.node);
156         arrange(loc.monitor, loc.desktop);
157     }
158 }
159
160 void unmap_notify(xcb_generic_event_t *evt)
161 {
162     xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
163
164     PRINTF("unmap notify %X\n", e->window);
165
166     window_location_t loc;
167     if (locate_window(e->window, &loc)) {
168         remove_node(loc.desktop, loc.node);
169         arrange(loc.monitor, loc.desktop);
170     }
171 }
172
173 void property_notify(xcb_generic_event_t *evt)
174 {
175     xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
176     xcb_icccm_wm_hints_t hints;
177
178     /* PRINTF("property notify %X\n", e->window); */
179
180     if (e->atom != XCB_ATOM_WM_HINTS)
181         return;
182
183     window_location_t loc;
184     if (locate_window(e->window, &loc)
185             && xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1)
186         set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
187 }
188
189 void client_message(xcb_generic_event_t *evt)
190 {
191     xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
192
193     PRINTF("client message %X %u\n", e->window, e->type);
194
195     if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
196         desktop_location_t loc;
197         if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
198             select_monitor(loc.monitor);
199             select_desktop(loc.desktop);
200         }
201         return;
202     }
203
204     window_location_t loc;
205     if (!locate_window(e->window, &loc))
206         return;
207
208     if (e->type == ewmh->_NET_WM_STATE) {
209         handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
210         handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
211     } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
212         if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node)
213             toggle_fullscreen(loc.monitor, loc.desktop->focus->client);
214         select_monitor(loc.monitor);
215         select_desktop(loc.desktop);
216         focus_node(loc.monitor, loc.desktop, loc.node, true);
217         arrange(loc.monitor, loc.desktop);
218     }
219 }
220
221 void enter_notify(xcb_generic_event_t *evt)
222 {
223     xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
224     xcb_window_t win = e->event;
225
226     PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
227
228     if (e->mode != XCB_NOTIFY_MODE_NORMAL
229             || (mon->desk->focus != NULL && mon->desk->focus->client->window == win))
230         return;
231
232     enable_motion_recorder();
233 }
234
235 void motion_notify(void)
236 {
237     PUTS("motion notify");
238
239     disable_motion_recorder();
240
241     xcb_window_t win = XCB_NONE;
242     query_pointer(&win, NULL);
243     if (win != XCB_NONE)
244         window_focus(win);
245 }
246
247 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
248 {
249     if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
250         bool fs = n->client->fullscreen;
251         if (action == XCB_EWMH_WM_STATE_TOGGLE
252                 || (fs && action == XCB_EWMH_WM_STATE_REMOVE)
253                 || (!fs && action == XCB_EWMH_WM_STATE_ADD)) {
254             toggle_fullscreen(m, n->client);
255             arrange(m, d);
256         }
257     } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
258         if (action == XCB_EWMH_WM_STATE_ADD)
259             set_urgency(m, d, n, true);
260         else if (action == XCB_EWMH_WM_STATE_REMOVE)
261             set_urgency(m, d, n, false);
262         else if (action == XCB_EWMH_WM_STATE_TOGGLE)
263             set_urgency(m, d, n, !n->client->urgent);
264     }
265 }
266
267 void grab_pointer(pointer_action_t pac)
268 {
269     PRINTF("grab pointer %u\n", pac);
270
271     xcb_window_t win = XCB_NONE;
272     xcb_point_t pos;
273
274     query_pointer(&win, &pos);
275
276     if (win == XCB_NONE)
277         return;
278
279     window_location_t loc;
280     if (locate_window(win, &loc)) {
281         client_t *c = NULL;
282         frozen_pointer->position = pos;
283         frozen_pointer->action = pac;
284         c = loc.node->client;
285         frozen_pointer->monitor = loc.monitor;
286         frozen_pointer->desktop = loc.desktop;
287         frozen_pointer->node = loc.node;
288         frozen_pointer->client = c;
289         frozen_pointer->window = c->window;
290         frozen_pointer->horizontal_fence = NULL;
291         frozen_pointer->vertical_fence = NULL;
292
293         switch (pac)  {
294             case ACTION_FOCUS:
295                 focus_node(loc.monitor, loc.desktop, loc.node, true);
296                 break;
297             case ACTION_MOVE:
298             case ACTION_RESIZE_SIDE:
299             case ACTION_RESIZE_CORNER:
300                 if (is_tiled(c)) {
301                     frozen_pointer->rectangle = c->tiled_rectangle;
302                     frozen_pointer->is_tiled = true;
303                 } else if (is_floating(c)) {
304                     frozen_pointer->rectangle = c->floating_rectangle;
305                     frozen_pointer->is_tiled = false;
306                 } else {
307                     frozen_pointer->action = ACTION_NONE;
308                     return;
309                 }
310                 if (pac == ACTION_RESIZE_SIDE) {
311                     float W = frozen_pointer->rectangle.width;
312                     float H = frozen_pointer->rectangle.height;
313                     float ratio = W / H;
314                     float x = pos.x - frozen_pointer->rectangle.x;
315                     float y = pos.y - frozen_pointer->rectangle.y;
316                     float diag_a = ratio * y;
317                     float diag_b = W - diag_a;
318                     if (x < diag_a) {
319                         if (x < diag_b)
320                             frozen_pointer->side = SIDE_LEFT;
321                         else
322                             frozen_pointer->side = SIDE_BOTTOM;
323                     } else {
324                         if (x < diag_b)
325                             frozen_pointer->side = SIDE_TOP;
326                         else
327                             frozen_pointer->side = SIDE_RIGHT;
328                     }
329                 } else if (pac == ACTION_RESIZE_CORNER) {
330                     int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
331                     int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
332                     if (pos.x > mid_x) {
333                         if (pos.y > mid_y)
334                             frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
335                         else
336                             frozen_pointer->corner = CORNER_TOP_RIGHT;
337                     } else {
338                         if (pos.y > mid_y)
339                             frozen_pointer->corner = CORNER_BOTTOM_LEFT;
340                         else
341                             frozen_pointer->corner = CORNER_TOP_LEFT;
342                     }
343                 }
344                 if (frozen_pointer->is_tiled) {
345                     if (pac == ACTION_RESIZE_SIDE) {
346                         switch (frozen_pointer->side) {
347                             case SIDE_TOP:
348                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
349                                 break;
350                             case SIDE_RIGHT:
351                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
352                                 break;
353                             case SIDE_BOTTOM:
354                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
355                                 break;
356                             case SIDE_LEFT:
357                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
358                                 break;
359                         }
360                     } else if (pac == ACTION_RESIZE_CORNER) {
361                         switch (frozen_pointer->corner) {
362                             case CORNER_TOP_LEFT:
363                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
364                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
365                                 break;
366                             case CORNER_TOP_RIGHT:
367                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
368                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
369                                 break;
370                             case CORNER_BOTTOM_RIGHT:
371                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
372                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
373                                 break;
374                             case CORNER_BOTTOM_LEFT:
375                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
376                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
377                                 break;
378                         }
379                     }
380                     if (frozen_pointer->horizontal_fence != NULL)
381                         frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
382                     if (frozen_pointer->vertical_fence != NULL)
383                         frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
384                 }
385                 break;
386             case ACTION_NONE:
387                 break;
388         }
389     } else {
390         frozen_pointer->action = ACTION_NONE;
391     }
392 }
393
394 void track_pointer(int root_x, int root_y)
395 {
396     if (frozen_pointer->action == ACTION_NONE)
397         return;
398
399     int16_t delta_x, delta_y, x, y, w, h;
400     uint16_t width, height;
401
402     pointer_action_t pac = frozen_pointer->action;
403     monitor_t *m = frozen_pointer->monitor;
404     desktop_t *d = frozen_pointer->desktop;
405     node_t *n = frozen_pointer->node;
406     client_t *c = frozen_pointer->client;
407     xcb_window_t win = frozen_pointer->window;
408     xcb_rectangle_t rect = frozen_pointer->rectangle;
409     node_t *vertical_fence = frozen_pointer->vertical_fence;
410     node_t *horizontal_fence = frozen_pointer->horizontal_fence;
411
412     x = y = 0;
413     w = h = 1;
414
415     delta_x = root_x - frozen_pointer->position.x;
416     delta_y = root_y - frozen_pointer->position.y;
417
418     switch (pac) {
419         case ACTION_MOVE:
420             if (frozen_pointer->is_tiled) {
421                 xcb_window_t pwin = XCB_NONE;
422                 query_pointer(&pwin, NULL);
423                 if (pwin == win)
424                     return;
425                 window_location_t loc;
426                 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
427                 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
428                     swap_nodes(n, loc.node);
429                     arrange(m, d);
430                 } else {
431                     if (is_managed && loc.monitor == m) {
432                         return;
433                     } else if (!is_managed) {
434                         xcb_point_t pt = (xcb_point_t) {root_x, root_y};
435                         monitor_t *pmon = monitor_from_point(pt);
436                         if (pmon == NULL || pmon == m) {
437                             return;
438                         } else {
439                             loc.monitor = pmon;
440                             loc.desktop = pmon->desk;
441                         }
442                     }
443                     transfer_node(m, d, loc.monitor, loc.desktop, n);
444                     arrange(m, d);
445                     arrange(loc.monitor, loc.desktop);
446                     frozen_pointer->monitor = loc.monitor;
447                     frozen_pointer->desktop = loc.desktop;
448                 }
449             } else {
450                 x = rect.x + delta_x;
451                 y = rect.y + delta_y;
452                 window_move(win, x, y);
453             }
454             break;
455         case ACTION_RESIZE_SIDE:
456         case ACTION_RESIZE_CORNER:
457             if (frozen_pointer->is_tiled) {
458                 if (vertical_fence != NULL) {
459                     double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
460                     sr = MAX(0, sr);
461                     sr = MIN(1, sr);
462                     vertical_fence->split_ratio = sr;
463                 }
464                 if (horizontal_fence != NULL) {
465                     double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
466                     sr = MAX(0, sr);
467                     sr = MIN(1, sr);
468                     horizontal_fence->split_ratio = sr;
469                 }
470                 arrange(mon, mon->desk);
471             } else {
472                 if (pac == ACTION_RESIZE_SIDE) {
473                     switch (frozen_pointer->side) {
474                         case SIDE_TOP:
475                             x = rect.x;
476                             y = rect.y + delta_y;
477                             w = rect.width;
478                             h = rect.height - delta_y;
479                             break;
480                         case SIDE_RIGHT:
481                             x = rect.x;
482                             y = rect.y;
483                             w = rect.width + delta_x;
484                             h = rect.height;
485                             break;
486                         case SIDE_BOTTOM:
487                             x = rect.x;
488                             y = rect.y;
489                             w = rect.width;
490                             h = rect.height + delta_y;
491                             break;
492                         case SIDE_LEFT:
493                             x = rect.x + delta_x;
494                             y = rect.y;
495                             w = rect.width - delta_x;
496                             h = rect.height;
497                             break;
498                     }
499                     width = MAX(1, w);
500                     height = MAX(1, h);
501                     window_move_resize(win, x, y, width, height);
502                     c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
503                     window_draw_border(n, d->focus == n, mon == m);
504                 } else if (pac == ACTION_RESIZE_CORNER) {
505                     switch (frozen_pointer->corner) {
506                         case CORNER_TOP_LEFT:
507                             x = rect.x + delta_x;
508                             y = rect.y + delta_y;
509                             w = rect.width - delta_x;
510                             h = rect.height - delta_y;
511                             break;
512                         case CORNER_TOP_RIGHT:
513                             x = rect.x;
514                             y = rect.y + delta_y;
515                             w = rect.width + delta_x;
516                             h = rect.height - delta_y;
517                             break;
518                         case CORNER_BOTTOM_LEFT:
519                             x = rect.x + delta_x;
520                             y = rect.y;
521                             w = rect.width - delta_x;
522                             h = rect.height + delta_y;
523                             break;
524                         case CORNER_BOTTOM_RIGHT:
525                             x = rect.x;
526                             y = rect.y;
527                             w = rect.width + delta_x;
528                             h = rect.height + delta_y;
529                             break;
530                     }
531                     width = MAX(1, w);
532                     height = MAX(1, h);
533                     window_move_resize(win, x, y, width, height);
534                     c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
535                     window_draw_border(n, d->focus == n, mon == m);
536                 }
537             }
538             break;
539         case ACTION_FOCUS:
540         case ACTION_NONE:
541             break;
542     }
543 }
544
545 void ungrab_pointer(void)
546 {
547     PUTS("ungrab pointer");
548
549     if (frozen_pointer->action == ACTION_NONE || frozen_pointer->is_tiled)
550         return;
551
552     update_floating_rectangle(frozen_pointer->client);
553     monitor_t *m = underlying_monitor(frozen_pointer->client);
554     if (m != NULL && m != frozen_pointer->monitor) {
555         transfer_node(frozen_pointer->monitor, frozen_pointer->desktop, m, m->desk, frozen_pointer->node);
556         select_monitor(m);
557     }
558 }