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