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