]> git.lizzy.rs Git - bspwm.git/blob - window.c
Stack tiling windows via focus history
[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))
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))
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 monitor_t *monitor_from_point(xcb_point_t pt)
72 {
73     for (monitor_t *m = mon_head; m != NULL; m = m->next)
74         if (is_inside(m, pt))
75             return m;
76     return NULL;
77 }
78
79 monitor_t *underlying_monitor(client_t *c)
80 {
81     xcb_point_t pt = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
82     return monitor_from_point(pt);
83 }
84
85 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
86 {
87     window_location_t loc;
88     xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
89     uint8_t override_redirect = 0;
90
91     if (wa != NULL) {
92         override_redirect = wa->override_redirect;
93         free(wa);
94     }
95
96     if (override_redirect || locate_window(win, &loc))
97         return;
98
99     bool floating = false, follow = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
100
101     handle_rules(win, &m, &d, &floating, &follow, &transient, &fullscreen, &takes_focus, &manage);
102
103     if (!manage) {
104         disable_shadow(win);
105         window_show(win);
106         return;
107     }
108
109     client_t *c = make_client(win);
110     update_floating_rectangle(c);
111
112     xcb_icccm_get_wm_class_reply_t reply;
113     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
114         strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
115         xcb_icccm_get_wm_class_reply_wipe(&reply);
116     }
117
118     if (c->transient)
119         floating = true;
120
121     node_t *birth = make_node();
122     birth->client = c;
123
124     if (floating)
125         split_mode = MODE_MANUAL;
126
127     insert_node(m, d, birth);
128
129     disable_shadow(c->window);
130
131     if (floating)
132         toggle_floating(birth);
133
134     if (d->focus != NULL && d->focus->client->fullscreen)
135         toggle_fullscreen(m, d->focus->client);
136
137     if (fullscreen)
138         toggle_fullscreen(m, birth->client);
139
140     if (is_tiled(c))
141         window_lower(c->window);
142
143     c->transient = transient;
144
145     if (takes_focus)
146         focus_node(m, d, birth, false);
147
148     xcb_rectangle_t *frect = &birth->client->floating_rectangle;
149     if (frect->x == 0 && frect->y == 0)
150         center(m->rectangle, frect);
151
152     fit_monitor(m, birth->client);
153
154     arrange(m, d);
155
156     if (d == m->desk && visible)
157         window_show(c->window);
158
159     if (takes_focus)
160         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
161
162     uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
163     xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
164
165     if (follow) {
166         select_monitor(m);
167         select_desktop(d);
168     }
169
170     num_clients++;
171     ewmh_set_wm_desktop(birth, d);
172     ewmh_update_client_list();
173 }
174
175 void adopt_orphans(void)
176 {
177     xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
178     if (qtr == NULL)
179         return;
180     int len = xcb_query_tree_children_length(qtr);
181     xcb_window_t *wins = xcb_query_tree_children(qtr);
182     for (int i = 0; i < len; i++) {
183         uint32_t idx;
184         xcb_window_t win = wins[i];
185         window_hide(win);
186         if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
187             desktop_location_t loc;
188             if (ewmh_locate_desktop(idx, &loc))
189                 manage_window(loc.monitor, loc.desktop, win);
190             else
191                 manage_window(mon, mon->desk, win);
192         }
193     }
194     free(qtr);
195 }
196
197 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
198 {
199     if (n == NULL || border_width < 1 || n->client->border_width < 1)
200         return;
201
202     xcb_window_t win = n->client->window;
203     uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
204
205     if (split_mode == MODE_AUTOMATIC || !focused_monitor || !focused_window) {
206         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
207     } else {
208         xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
209
210         uint16_t width = actual_rectangle.width;
211         uint16_t height = actual_rectangle.height;
212
213         uint16_t full_width = width + 2 * border_width;
214         uint16_t full_height = height + 2 * border_width;
215
216         xcb_rectangle_t border_rectangles[] =
217         {
218             { width, 0, 2 * border_width, height + 2 * border_width },
219             { 0, height, width + 2 * border_width, 2 * border_width }
220         };
221
222         xcb_rectangle_t *presel_rectangles;
223
224         uint8_t win_depth = root_depth;
225         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
226         if (geo != NULL)
227             win_depth = geo->depth;
228         free(geo);
229
230         xcb_pixmap_t pix = xcb_generate_id(dpy);
231         xcb_create_pixmap(dpy, win_depth, pix, win, full_width, full_height);
232
233         xcb_gcontext_t gc = xcb_generate_id(dpy);
234         xcb_create_gc(dpy, gc, pix, 0, NULL);
235
236         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
237         xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(border_rectangles), border_rectangles);
238
239         uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
240         presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
241         switch (split_dir) {
242             case DIR_UP:
243                 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
244                 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
245                 break;
246             case DIR_DOWN:
247                 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
248                 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
249                 break;
250             case DIR_LEFT:
251                 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
252                 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
253                 break;
254             case DIR_RIGHT:
255                 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
256                 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
257                 break;
258         }
259
260         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
261         xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
262         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
263         free(presel_rectangles);
264         xcb_free_gc(dpy, gc);
265         xcb_free_pixmap(dpy, pix);
266     }
267 }
268
269 void window_close(node_t *n)
270 {
271     if (n == NULL || n->client->locked)
272         return;
273
274     PRINTF("close window %X\n", n->client->window);
275
276     xcb_atom_t WM_DELETE_WINDOW;
277     xcb_window_t win = n->client->window;
278     xcb_client_message_event_t e;
279
280     xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
281     if (reply) {
282         WM_DELETE_WINDOW = reply->atom;
283         free(reply);
284     } else {
285         warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
286         return;
287     }
288
289     e.response_type = XCB_CLIENT_MESSAGE;
290     e.window = win;
291     e.format = 32;
292     e.sequence = 0;
293     e.type = ewmh->WM_PROTOCOLS;
294     e.data.data32[0] = WM_DELETE_WINDOW;
295     e.data.data32[1] = XCB_CURRENT_TIME;
296
297     xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
298 }
299
300 void window_kill(desktop_t *d, node_t *n)
301 {
302     if (n == NULL)
303         return;
304
305     PRINTF("kill window %X\n", n->client->window);
306
307     xcb_kill_client(dpy, n->client->window);
308     remove_node(d, n);
309 }
310
311 void toggle_fullscreen(monitor_t *m, client_t *c)
312 {
313     PRINTF("toggle fullscreen %X\n", c->window);
314
315     if (c->fullscreen) {
316         c->fullscreen = false;
317         xcb_atom_t values[] = {XCB_NONE};
318         xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
319         if (is_tiled(c))
320             window_lower(c->window);
321     } else {
322         c->fullscreen = true;
323         xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
324         xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
325         window_raise(c->window);
326         window_border_width(c->window, 0);
327         xcb_rectangle_t r = m->rectangle;
328         window_move_resize(c->window, r.x, r.y, r.width, r.height);
329     }
330     update_current();
331 }
332
333 void toggle_floating(node_t *n)
334 {
335     if (n == NULL || n->client->transient || n->client->fullscreen)
336         return;
337
338     PRINTF("toggle floating %X\n", n->client->window);
339
340     client_t *c = n->client;
341     c->floating = !c->floating;
342     n->vacant = !n->vacant;
343     update_vacant_state(n->parent);
344     if (c->floating)
345         window_raise(c->window);
346     else if (is_tiled(c))
347         window_lower(c->window);
348     if (c->floating)
349         enable_shadow(c->window);
350     else
351         disable_shadow(c->window);
352     update_current();
353 }
354
355 void toggle_locked(client_t *c)
356 {
357     PRINTF("toggle locked %X\n", c->window);
358
359     c->locked = !c->locked;
360 }
361
362 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
363 {
364     if (value && mon->desk->focus == n)
365         return;
366     n->client->urgent = value;
367     put_status();
368     if (m->desk == d)
369         arrange(m, d);
370 }
371
372 void set_shadow(xcb_window_t win, uint32_t value)
373 {
374     if (!apply_shadow_property)
375         return;
376     xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, 1, &value);
377 }
378
379 void enable_shadow(xcb_window_t win)
380 {
381     set_shadow(win, 1);
382 }
383
384 void disable_shadow(xcb_window_t win)
385 {
386     set_shadow(win, 0);
387 }
388
389 void list_windows(char *rsp)
390 {
391     char line[MAXLEN];
392
393     for (monitor_t *m = mon_head; m != NULL; m = m->next)
394         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
395             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
396                 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
397                 strncat(rsp, line, REMLEN(rsp));
398             }
399 }
400
401 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
402 {
403     if (c == NULL)
404         return 0;
405
406     if (focused_monitor && focused_window) {
407         if (c->locked)
408             return focused_locked_border_color_pxl;
409         else
410             return focused_border_color_pxl;
411     } else if (focused_window) {
412         if (c->urgent)
413             return urgent_border_color_pxl;
414         else if (c->locked)
415             return active_locked_border_color_pxl;
416         else
417             return active_border_color_pxl;
418     } else {
419         if (c->urgent)
420             return urgent_border_color_pxl;
421         else if (c->locked)
422             return normal_locked_border_color_pxl;
423         else
424             return normal_border_color_pxl;
425     }
426 }
427
428 void update_floating_rectangle(client_t *c)
429 {
430     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
431
432     if (geo != NULL)
433         c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
434     else
435         c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
436
437     free(geo);
438 }
439
440
441 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
442 {
443     window_lower(motion_recorder);
444     xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
445     if (qpr != NULL) {
446         if (win != NULL)
447             *win = qpr->child;
448         if (pt != NULL)
449             *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
450         free(qpr);
451     }
452     window_raise(motion_recorder);
453 }
454
455 void window_focus(xcb_window_t win)
456 {
457     window_location_t loc;
458     if (locate_window(win, &loc)) {
459         if (loc.node == mon->desk->focus)
460             return;
461         select_monitor(loc.monitor);
462         select_desktop(loc.desktop);
463         focus_node(loc.monitor, loc.desktop, loc.node, true);
464     }
465 }
466
467 void window_border_width(xcb_window_t win, uint32_t bw)
468 {
469     uint32_t values[] = {bw};
470     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
471 }
472
473 void window_move(xcb_window_t win, int16_t x, int16_t y)
474 {
475     uint32_t values[] = {x, y};
476     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
477 }
478
479 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
480 {
481     uint32_t values[] = {x, y, w, h};
482     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
483 }
484
485 void window_raise(xcb_window_t win)
486 {
487     uint32_t values[] = {XCB_STACK_MODE_ABOVE};
488     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
489 }
490
491 void stack_tiled(desktop_t *d)
492 {
493     for (node_list_t *x = d->history->tail; x != NULL; x = x->prev)
494         if (x->latest && is_tiled(x->node->client))
495             window_lower(x->node->client->window);
496 }
497
498 void window_lower(xcb_window_t win)
499 {
500     uint32_t values[] = {XCB_STACK_MODE_BELOW};
501     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
502 }
503
504 void window_set_visibility(xcb_window_t win, bool visible) {
505     uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
506     uint32_t values_on[] = {ROOT_EVENT_MASK};
507     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
508     if (visible)
509         xcb_map_window(dpy, win);
510     else
511         xcb_unmap_window(dpy, win);
512     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
513 }
514
515 void window_hide(xcb_window_t win)
516 {
517     window_set_visibility(win, false);
518 }
519
520 void window_show(xcb_window_t win)
521 {
522     window_set_visibility(win, true);
523 }
524
525 void toggle_visibility(void)
526 {
527     if (visible)
528         clear_input_focus();
529     visible = !visible;
530     for (monitor_t *m = mon_head; m != NULL; m = m->next)
531         for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n))
532             window_set_visibility(n->client->window, visible);
533     if (visible)
534         update_current();
535 }
536
537 void enable_motion_recorder(void)
538 {
539     window_raise(motion_recorder);
540     window_show(motion_recorder);
541 }
542
543 void disable_motion_recorder(void)
544 {
545     window_hide(motion_recorder);
546 }
547
548 void clear_input_focus(void)
549 {
550     xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
551 }