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