]> git.lizzy.rs Git - bspwm.git/blob - window.c
Merge pull request #31 from Stebalien/arrange-after-kill
[bspwm.git] / window.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <xcb/xcb.h>
6 #include <xcb/xcb_event.h>
7 #include <xcb/xcb_icccm.h>
8 #include "types.h"
9 #include "tree.h"
10 #include "bspwm.h"
11 #include "settings.h"
12 #include "ewmh.h"
13 #include "rules.h"
14 #include "window.h"
15
16 void center(xcb_rectangle_t a, xcb_rectangle_t *b)
17 {
18     if (b->width < a.width)
19         b->x = a.x + (a.width - b->width) / 2;
20     if (b->height < a.height)
21         b->y = a.y + (a.height - b->height) / 2;
22 }
23
24 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
25 {
26     return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width)
27             && a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
28 }
29
30 bool might_cover(desktop_t *d, node_t *n)
31 {
32     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root))
33         if (f != n && is_floating(f->client) && contains(n->client->floating_rectangle, f->client->floating_rectangle))
34             return true;
35     return false;
36 }
37
38 bool locate_window(xcb_window_t win, window_location_t *loc)
39 {
40     for (monitor_t *m = mon_head; m != NULL; m = m->next)
41         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
42             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
43                 if (n->client->window == win) {
44                     loc->monitor = m;
45                     loc->desktop = d;
46                     loc->node = n;
47                     return true;
48                 }
49     return false;
50 }
51
52 bool locate_desktop(char *name, desktop_location_t *loc)
53 {
54     for (monitor_t *m = mon_head; m != NULL; m = m->next)
55         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
56             if (strcmp(d->name, name) == 0) {
57                 loc->monitor = m;
58                 loc->desktop = d;
59                 return true;
60             }
61     return false;
62 }
63
64 bool is_inside(monitor_t *m, xcb_point_t pt)
65 {
66     xcb_rectangle_t r = m->rectangle;
67     return (r.x <= pt.x && pt.x < (r.x + r.width)
68             && r.y <= pt.y && pt.y < (r.y + r.height));
69 }
70
71 xcb_rectangle_t get_rectangle(client_t *c)
72 {
73     if (is_tiled(c))
74         return c->tiled_rectangle;
75     else
76         return c->floating_rectangle;
77 }
78
79 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
80 {
81     xcb_rectangle_t rect = get_rectangle(c);
82     switch (dir) {
83         case DIR_RIGHT:
84             pt->x = rect.x + rect.width;
85             pt->y = rect.y + (rect.height / 2);
86             break;
87         case DIR_DOWN:
88             pt->x = rect.x + (rect.width / 2);
89             pt->y = rect.y + rect.height;
90             break;
91         case DIR_LEFT:
92             pt->x = rect.x;
93             pt->y = rect.y + (rect.height / 2);
94             break;
95         case DIR_UP:
96             pt->x = rect.x + (rect.width / 2);
97             pt->y = rect.y;
98             break;
99     }
100 }
101
102 monitor_t *monitor_from_point(xcb_point_t pt)
103 {
104     for (monitor_t *m = mon_head; m != NULL; m = m->next)
105         if (is_inside(m, pt))
106             return m;
107     return NULL;
108 }
109
110 monitor_t *underlying_monitor(client_t *c)
111 {
112     xcb_point_t pt = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
113     return monitor_from_point(pt);
114 }
115
116 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
117 {
118     window_location_t loc;
119     xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
120     uint8_t override_redirect = 0;
121
122     if (wa != NULL) {
123         override_redirect = wa->override_redirect;
124         free(wa);
125     }
126
127     if (override_redirect || locate_window(win, &loc))
128         return;
129
130     bool floating = false, follow = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
131
132     handle_rules(win, &m, &d, &floating, &follow, &transient, &fullscreen, &takes_focus, &manage);
133
134     if (!manage) {
135         disable_shadow(win);
136         window_show(win);
137         return;
138     }
139
140     client_t *c = make_client(win);
141     update_floating_rectangle(c);
142
143     xcb_icccm_get_wm_class_reply_t reply;
144     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
145         strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
146         xcb_icccm_get_wm_class_reply_wipe(&reply);
147     }
148
149     if (c->transient)
150         floating = true;
151
152     node_t *n = make_node();
153     n->client = c;
154
155     insert_node(m, d, n, d->focus);
156
157     disable_shadow(c->window);
158
159     if (floating)
160         toggle_floating(d, n);
161
162     if (d->focus != NULL && d->focus->client->fullscreen)
163         toggle_fullscreen(d, d->focus);
164
165     if (fullscreen)
166         toggle_fullscreen(d, n);
167
168     if (is_tiled(c))
169         window_lower(c->window);
170
171     c->transient = transient;
172
173     bool give_focus = takes_focus && (d == mon->desk || follow);
174
175     if (give_focus)
176         focus_node(m, d, n);
177     else if (takes_focus)
178         pseudo_focus(d, n);
179
180     xcb_rectangle_t *frect = &n->client->floating_rectangle;
181     if (frect->x == 0 && frect->y == 0)
182         center(m->rectangle, frect);
183
184     fit_monitor(m, n->client);
185
186     arrange(m, d);
187
188     if (d == m->desk && visible)
189         window_show(c->window);
190
191     /* the same function is already called in `focus_node` but has no effects on unmapped windows */
192     if (give_focus)
193         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
194
195     uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
196     xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
197
198     num_clients++;
199     ewmh_set_wm_desktop(n, d);
200     ewmh_update_client_list();
201 }
202
203 void adopt_orphans(void)
204 {
205     xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
206     if (qtr == NULL)
207         return;
208
209     PUTS("adopt orphans");
210
211     int len = xcb_query_tree_children_length(qtr);
212     xcb_window_t *wins = xcb_query_tree_children(qtr);
213     for (int i = 0; i < len; i++) {
214         uint32_t idx;
215         xcb_window_t win = wins[i];
216         window_hide(win);
217         if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
218             desktop_location_t loc;
219             if (ewmh_locate_desktop(idx, &loc))
220                 manage_window(loc.monitor, loc.desktop, win);
221             else
222                 manage_window(mon, mon->desk, win);
223         }
224     }
225     free(qtr);
226 }
227
228 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
229 {
230     if (n == NULL || border_width < 1 || n->client->border_width < 1)
231         return;
232
233     xcb_window_t win = n->client->window;
234     uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
235
236     if (n->split_mode == MODE_AUTOMATIC) {
237         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
238     } else {
239         xcb_rectangle_t actual_rectangle = get_rectangle(n->client);
240
241         uint16_t width = actual_rectangle.width;
242         uint16_t height = actual_rectangle.height;
243
244         uint16_t full_width = width + 2 * border_width;
245         uint16_t full_height = height + 2 * border_width;
246
247         xcb_rectangle_t border_rectangles[] =
248         {
249             { width, 0, 2 * border_width, height + 2 * border_width },
250             { 0, height, width + 2 * border_width, 2 * border_width }
251         };
252
253         xcb_rectangle_t *presel_rectangles;
254
255         uint8_t win_depth = root_depth;
256         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
257         if (geo != NULL)
258             win_depth = geo->depth;
259         free(geo);
260
261         xcb_pixmap_t pix = xcb_generate_id(dpy);
262         xcb_create_pixmap(dpy, win_depth, pix, win, full_width, full_height);
263
264         xcb_gcontext_t gc = xcb_generate_id(dpy);
265         xcb_create_gc(dpy, gc, pix, 0, NULL);
266
267         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
268         xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(border_rectangles), border_rectangles);
269
270         uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
271         presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
272         switch (n->split_dir) {
273             case DIR_UP:
274                 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
275                 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
276                 break;
277             case DIR_DOWN:
278                 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
279                 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
280                 break;
281             case DIR_LEFT:
282                 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
283                 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
284                 break;
285             case DIR_RIGHT:
286                 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
287                 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
288                 break;
289         }
290
291         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
292         xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
293         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
294         free(presel_rectangles);
295         xcb_free_gc(dpy, gc);
296         xcb_free_pixmap(dpy, pix);
297     }
298 }
299
300 void window_close(node_t *n)
301 {
302     if (n == NULL || n->client->locked)
303         return;
304
305     PRINTF("close window %X\n", n->client->window);
306
307     xcb_atom_t WM_DELETE_WINDOW;
308     xcb_window_t win = n->client->window;
309     xcb_client_message_event_t e;
310
311     xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
312     if (reply) {
313         WM_DELETE_WINDOW = reply->atom;
314         free(reply);
315     } else {
316         warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
317         return;
318     }
319
320     e.response_type = XCB_CLIENT_MESSAGE;
321     e.window = win;
322     e.format = 32;
323     e.sequence = 0;
324     e.type = ewmh->WM_PROTOCOLS;
325     e.data.data32[0] = WM_DELETE_WINDOW;
326     e.data.data32[1] = XCB_CURRENT_TIME;
327
328     xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
329 }
330
331 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
332 {
333     if (n == NULL)
334         return;
335
336     xcb_window_t win = n->client->window;
337     PRINTF("kill window %X\n", win);
338
339     xcb_kill_client(dpy, win);
340     remove_node(d, n);
341     arrange(m, d);
342 }
343
344 void toggle_fullscreen(desktop_t *d, node_t *n)
345 {
346     if (n == NULL)
347         return;
348
349     client_t *c = n->client;
350
351     PRINTF("toggle fullscreen %X\n", c->window);
352
353     if (c->fullscreen) {
354         c->fullscreen = false;
355         xcb_atom_t values[] = {XCB_NONE};
356         xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
357         stack(d, n);
358     } else {
359         c->fullscreen = true;
360         xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
361         xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
362         window_raise(c->window);
363     }
364 }
365
366 void toggle_floating(desktop_t *d, node_t *n)
367 {
368     if (n == NULL || n->client->transient || n->client->fullscreen)
369         return;
370
371     PRINTF("toggle floating %X\n", n->client->window);
372
373     n->split_mode = MODE_AUTOMATIC;
374     client_t *c = n->client;
375     c->floating = !c->floating;
376     n->vacant = !n->vacant;
377     update_vacant_state(n->parent);
378     if (c->floating) {
379         enable_shadow(c->window);
380         unrotate_brother(n);
381     } else {
382         disable_shadow(c->window);
383         rotate_brother(n);
384     }
385     stack(d, n);
386 }
387
388 void toggle_locked(monitor_t *m, desktop_t *d, node_t *n)
389 {
390     if (n == NULL)
391         return;
392
393     client_t *c = n->client;
394
395     PRINTF("toggle locked %X\n", c->window);
396
397     c->locked = !c->locked;
398     window_draw_border(n, d->focus == n, m == mon);
399 }
400
401 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
402 {
403     if (value && mon->desk->focus == n)
404         return;
405     n->client->urgent = value;
406     window_draw_border(n, d->focus == n, m == mon);
407     put_status();
408 }
409
410 void set_shadow(xcb_window_t win, uint32_t value)
411 {
412     if (!apply_shadow_property)
413         return;
414     xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, 1, &value);
415 }
416
417 void enable_shadow(xcb_window_t win)
418 {
419     set_shadow(win, 1);
420 }
421
422 void disable_shadow(xcb_window_t win)
423 {
424     set_shadow(win, 0);
425 }
426
427 void list_windows(char *rsp)
428 {
429     char line[MAXLEN];
430
431     for (monitor_t *m = mon_head; m != NULL; m = m->next)
432         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
433             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
434                 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
435                 strncat(rsp, line, REMLEN(rsp));
436             }
437 }
438
439 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
440 {
441     if (c == NULL)
442         return 0;
443
444     if (focused_monitor && focused_window) {
445         if (c->locked)
446             return focused_locked_border_color_pxl;
447         else
448             return focused_border_color_pxl;
449     } else if (focused_window) {
450         if (c->urgent)
451             return urgent_border_color_pxl;
452         else if (c->locked)
453             return active_locked_border_color_pxl;
454         else
455             return active_border_color_pxl;
456     } else {
457         if (c->urgent)
458             return urgent_border_color_pxl;
459         else if (c->locked)
460             return normal_locked_border_color_pxl;
461         else
462             return normal_border_color_pxl;
463     }
464 }
465
466 void update_floating_rectangle(client_t *c)
467 {
468     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
469
470     if (geo != NULL)
471         c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
472     else
473         c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
474
475     free(geo);
476 }
477
478
479 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
480 {
481     window_lower(motion_recorder);
482     xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
483     if (qpr != NULL) {
484         if (win != NULL)
485             *win = qpr->child;
486         if (pt != NULL)
487             *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
488         free(qpr);
489     }
490     window_raise(motion_recorder);
491 }
492
493 void window_focus(xcb_window_t win)
494 {
495     window_location_t loc;
496     if (locate_window(win, &loc)) {
497         if (loc.node == mon->desk->focus)
498             return;
499         focus_node(loc.monitor, loc.desktop, loc.node);
500     }
501 }
502
503 void window_border_width(xcb_window_t win, uint32_t bw)
504 {
505     uint32_t values[] = {bw};
506     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
507 }
508
509 void window_move(xcb_window_t win, int16_t x, int16_t y)
510 {
511     uint32_t values[] = {x, y};
512     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
513 }
514
515 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
516 {
517     uint32_t values[] = {w, h};
518     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
519 }
520
521 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
522 {
523     uint32_t values[] = {x, y, w, h};
524     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
525 }
526
527 void window_raise(xcb_window_t win)
528 {
529     uint32_t values[] = {XCB_STACK_MODE_ABOVE};
530     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
531 }
532
533 void stack_tiled(desktop_t *d)
534 {
535     for (node_list_t *a = d->history->head; a != NULL; a = a->next)
536         if (a->latest && is_tiled(a->node->client))
537             window_lower(a->node->client->window);
538 }
539
540 void stack(desktop_t *d, node_t *n)
541 {
542     if (!is_tiled(n->client)) {
543         if (!adaptative_raise || !might_cover(d, n))
544             window_raise(n->client->window);
545     } else {
546         stack_tiled(d);
547     }
548 }
549
550 void window_lower(xcb_window_t win)
551 {
552     uint32_t values[] = {XCB_STACK_MODE_BELOW};
553     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
554 }
555
556 void window_set_visibility(xcb_window_t win, bool visible)
557 {
558     uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
559     uint32_t values_on[] = {ROOT_EVENT_MASK};
560     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
561     if (visible)
562         xcb_map_window(dpy, win);
563     else
564         xcb_unmap_window(dpy, win);
565     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
566 }
567
568 void window_hide(xcb_window_t win)
569 {
570     window_set_visibility(win, false);
571 }
572
573 void window_show(xcb_window_t win)
574 {
575     window_set_visibility(win, true);
576 }
577
578 void toggle_visibility(void)
579 {
580     if (visible)
581         clear_input_focus();
582     visible = !visible;
583     for (monitor_t *m = mon_head; m != NULL; m = m->next)
584         for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
585             window_set_visibility(n->client->window, visible);
586     if (visible)
587         update_input_focus();
588 }
589
590 void desktop_show(desktop_t *d)
591 {
592     if (!visible)
593         return;
594     for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
595         window_show(n->client->window);
596 }
597
598 void desktop_hide(desktop_t *d)
599 {
600     if (!visible)
601         return;
602     for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
603         window_hide(n->client->window);
604 }
605
606 void enable_motion_recorder(void)
607 {
608     PUTS("enable motion recorder");
609     window_raise(motion_recorder);
610     window_show(motion_recorder);
611 }
612
613 void disable_motion_recorder(void)
614 {
615     PUTS("disable motion recorder");
616     window_hide(motion_recorder);
617 }
618
619 void update_motion_recorder(void)
620 {
621     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
622
623     if (geo != NULL) {
624         window_resize(motion_recorder, geo->width, geo->height);
625         PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
626     }
627
628     free(geo);
629 }
630
631 void update_input_focus(void)
632 {
633     set_input_focus(mon->desk->focus);
634 }
635
636 void set_input_focus(node_t *n)
637 {
638     if (n == NULL)
639         return;
640     xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
641 }
642
643 void clear_input_focus(void)
644 {
645     xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
646 }
647
648 void center_pointer(monitor_t *m)
649 {
650     int16_t cx = m->rectangle.x + m->rectangle.width / 2;
651     int16_t cy = m->rectangle.y + m->rectangle.height / 2;
652     window_lower(motion_recorder);
653     xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
654     window_raise(motion_recorder);
655 }