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