]> git.sur5r.net Git - i3/i3/blob - src/x.c
Rendering fixes for stacking mode
[i3/i3] / src / x.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 /* Stores the X11 window ID of the currently focused window */
8 static xcb_window_t focused_id = XCB_NONE;
9
10 /*
11  * Describes the X11 state we may modify (map state, position, window stack).
12  * There is one entry per container. The state represents the current situation
13  * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
14  * which represents the order that will be pushed to X11, while old_state_head
15  * represents the current order). It will be updated in x_push_changes().
16  *
17  */
18 typedef struct con_state {
19     xcb_window_t id;
20     bool mapped;
21
22     /* For reparenting, we have a flag (need_reparent) and the X ID of the old
23      * frame this window was in. The latter is necessary because we need to
24      * ignore UnmapNotify events (by changing the window event mask). */
25     bool need_reparent;
26     xcb_window_t old_frame;
27
28     Rect rect;
29     Rect window_rect;
30
31     bool initial;
32
33     CIRCLEQ_ENTRY(con_state) state;
34     CIRCLEQ_ENTRY(con_state) old_state;
35 } con_state;
36
37 CIRCLEQ_HEAD(state_head, con_state) state_head =
38     CIRCLEQ_HEAD_INITIALIZER(state_head);
39
40 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
41     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
42
43 /*
44  * Returns the container state for the given frame. This function always
45  * returns a container state (otherwise, there is a bug in the code and the
46  * container state of a container for which x_con_init() was not called was
47  * requested).
48  *
49  */
50 static con_state *state_for_frame(xcb_window_t window) {
51     con_state *state;
52     CIRCLEQ_FOREACH(state, &state_head, state)
53         if (state->id == window)
54             return state;
55
56     /* TODO: better error handling? */
57     ELOG("No state found\n");
58     assert(false);
59     return NULL;
60 }
61
62 /*
63  * Initializes the X11 part for the given container. Called exactly once for
64  * every container from con_new().
65  *
66  */
67 void x_con_init(Con *con) {
68     /* TODO: maybe create the window when rendering first? we could then even
69      * get the initial geometry right */
70
71     uint32_t mask = 0;
72     uint32_t values[2];
73
74     /* our own frames should not be managed */
75     mask |= XCB_CW_OVERRIDE_REDIRECT;
76     values[0] = 1;
77
78     /* We want to know when… */
79     mask |= XCB_CW_EVENT_MASK;
80     values[1] = FRAME_EVENT_MASK;
81
82     Rect dims = { -15, -15, 10, 10 };
83     con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, -1, false, mask, values);
84     con->gc = xcb_generate_id(conn);
85     xcb_create_gc(conn, con->gc, con->frame, 0, 0);
86
87     struct con_state *state = scalloc(sizeof(struct con_state));
88     state->id = con->frame;
89     state->mapped = false;
90     state->initial = true;
91     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
92     CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
93     LOG("adding new state for window id 0x%08x\n", state->id);
94 }
95
96 /*
97  * Re-initializes the associated X window state for this container. You have
98  * to call this when you assign a client to an empty container to ensure that
99  * its state gets updated correctly.
100  *
101  */
102 void x_reinit(Con *con) {
103     struct con_state *state;
104
105     if ((state = state_for_frame(con->frame)) == NULL) {
106         ELOG("window state not found\n");
107         return;
108     }
109
110     LOG("resetting state %p to initial\n", state);
111     state->initial = true;
112     memset(&(state->window_rect), 0, sizeof(Rect));
113 }
114
115 /*
116  * Reparents the child window of the given container (necessary for sticky
117  * containers). The reparenting happens in the next call of x_push_changes().
118  *
119  */
120 void x_reparent_child(Con *con, Con *old) {
121     struct con_state *state;
122     if ((state = state_for_frame(con->frame)) == NULL) {
123         ELOG("window state for con not found\n");
124         return;
125     }
126
127     state->need_reparent = true;
128     state->old_frame = old->frame;
129 }
130
131 /*
132  * Moves a child window from Container src to Container dest.
133  *
134  */
135 void x_move_win(Con *src, Con *dest) {
136     struct con_state *state_src, *state_dest;
137
138     if ((state_src = state_for_frame(src->frame)) == NULL) {
139         ELOG("window state for src not found\n");
140         return;
141     }
142
143     if ((state_dest = state_for_frame(dest->frame)) == NULL) {
144         ELOG("window state for dest not found\n");
145         return;
146     }
147
148     Rect zero;
149     memset(&zero, 0, sizeof(Rect));
150     if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
151         memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
152         LOG("COPYING RECT\n");
153     }
154 }
155
156 /*
157  * Kills the window decoration associated with the given container.
158  *
159  */
160 void x_con_kill(Con *con) {
161     con_state *state;
162
163     xcb_destroy_window(conn, con->frame);
164     state = state_for_frame(con->frame);
165     CIRCLEQ_REMOVE(&state_head, state, state);
166     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
167 }
168
169 /*
170  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
171  *
172  */
173 static bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
174     xcb_get_property_cookie_t cookie;
175     xcb_get_wm_protocols_reply_t protocols;
176     bool result = false;
177
178     cookie = xcb_get_wm_protocols_unchecked(conn, window, atoms[WM_PROTOCOLS]);
179     if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
180         return false;
181
182     /* Check if the client’s protocols have the requested atom set */
183     for (uint32_t i = 0; i < protocols.atoms_len; i++)
184         if (protocols.atoms[i] == atom)
185             result = true;
186
187     xcb_get_wm_protocols_reply_wipe(&protocols);
188
189     return result;
190 }
191
192 /*
193  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
194  *
195  */
196 void x_window_kill(xcb_window_t window) {
197     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
198     if (!window_supports_protocol(window, atoms[WM_DELETE_WINDOW])) {
199         LOG("Killing window the hard way\n");
200         xcb_kill_client(conn, window);
201         return;
202     }
203
204     xcb_client_message_event_t ev;
205
206     memset(&ev, 0, sizeof(xcb_client_message_event_t));
207
208     ev.response_type = XCB_CLIENT_MESSAGE;
209     ev.window = window;
210     ev.type = atoms[WM_PROTOCOLS];
211     ev.format = 32;
212     ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
213     ev.data.data32[1] = XCB_CURRENT_TIME;
214
215     LOG("Sending WM_DELETE to the client\n");
216     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
217     xcb_flush(conn);
218 }
219
220 /*
221  * Draws the decoration of the given container onto its parent.
222  *
223  */
224 void x_draw_decoration(Con *con) {
225     /* this code needs to run for:
226      *  • leaf containers
227      *  • non-leaf containers which are in a stacking container
228      */
229     if (!con_is_leaf(con) && con->parent->layout != L_STACKED)
230         return;
231     DLOG("decoration should be rendered for con %p\n", con);
232
233     /* 1: find out which colors to use */
234     struct Colortriple *color;
235     if (con->urgent)
236         color = &config.client.urgent;
237     else if (con == focused)
238         color = &config.client.focused;
239     else if (con == TAILQ_FIRST(&(con->parent->focus_head)))
240         color = &config.client.focused_inactive;
241     else
242         color = &config.client.unfocused;
243
244     Con *parent = con->parent;
245     int border_style = con_border_style(con);
246
247     /* 2: draw a rectangle in border color around the client */
248     if (border_style != BS_NONE && con_is_leaf(con)) {
249         Rect br = con_border_style_rect(con);
250         Rect *r = &(con->rect);
251 #if 0
252         DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
253         DLOG("border_rect spans (%d, %d) with %d x %d\n", border_rect.x, border_rect.y, border_rect.width, border_rect.height);
254         DLOG("window_rect spans (%d, %d) with %d x %d\n", con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
255 #endif
256
257         /* This polygon represents the border around the child window (left,
258          * bottom and right part). We don’t just fill the whole rectangle
259          * because some childs are not freely resizable and we want their
260          * background color to "shine through". */
261         xcb_change_gc_single(conn, con->gc, XCB_GC_FOREGROUND, color->background);
262         xcb_point_t points[] = {
263             { 0,                          0 },
264             { 0,                          r->height },
265             { r->width,                   r->height },
266             { r->width,                   0 },
267             { r->width + br.width + br.x, 0 },
268             { r->width + br.width + br.x, r->height + br.height + br.y },
269             { br.x,                       r->height + br.height },
270             { br.x,                       0 }
271         };
272         xcb_fill_poly(conn, con->frame, con->gc, XCB_POLY_SHAPE_COMPLEX, XCB_COORD_MODE_ORIGIN, 8, points);
273
274         /* 1pixel border needs an additional line at the top */
275         if (border_style == BS_1PIXEL) {
276             xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
277             xcb_poly_fill_rectangle(conn, con->frame, con->gc, 1, &topline);
278         }
279     }
280
281     /* if this is a borderless/1pixel window, we don’t * need to render the
282      * decoration. */
283     if (border_style != BS_NORMAL) {
284         DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
285         return;
286     }
287
288     /* 3: paint the bar */
289     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->background);
290     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
291     xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
292
293     /* 4: draw the two lines in border color */
294     xcb_draw_line(conn, parent->frame, parent->gc, color->border,
295             con->deco_rect.x, /* x */
296             con->deco_rect.y, /* y */
297             con->deco_rect.x + con->deco_rect.width, /* to_x */
298             con->deco_rect.y); /* to_y */
299     xcb_draw_line(conn, parent->frame, parent->gc, color->border,
300             con->deco_rect.x, /* x */
301             con->deco_rect.y + con->deco_rect.height - 1, /* y */
302             con->deco_rect.x + con->deco_rect.width, /* to_x */
303             con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
304
305     /* 5: draw the title */
306     xcb_change_gc_single(conn, parent->gc, XCB_GC_BACKGROUND, color->background);
307     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->text);
308
309     struct Window *win = con->window;
310     if (win == NULL || win->name_x == NULL) {
311         /* this is a non-leaf container, we need to make up a good description */
312         // TODO: use a good description instead of just "another container"
313         xcb_image_text_8(
314             conn,
315             strlen("another container"),
316             parent->frame,
317             parent->gc,
318             con->deco_rect.x + 2,
319             con->deco_rect.y + 14, /* TODO: hardcoded */
320             "another container"
321         );
322         return;
323     }
324
325     int indent_level = 0,
326         indent_mult = 0;
327     Con *il_parent = con->parent;
328     if (il_parent->type != L_STACKED) {
329         while (1) {
330             DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
331             if (il_parent->layout == L_STACKED)
332                 indent_level++;
333             if (il_parent->type == CT_WORKSPACE)
334                 break;
335             il_parent = il_parent->parent;
336             indent_mult++;
337         }
338     }
339     DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
340     int indent_px = (indent_level * 5) * indent_mult;
341
342     if (win->uses_net_wm_name)
343         xcb_image_text_16(
344             conn,
345             win->name_len,
346             parent->frame,
347             parent->gc,
348             con->deco_rect.x + 2 + indent_px,
349             con->deco_rect.y + 14, /* TODO: hardcoded */
350             (xcb_char2b_t*)win->name_x
351         );
352     else
353         xcb_image_text_8(
354             conn,
355             win->name_len,
356             parent->frame,
357             parent->gc,
358             con->deco_rect.x + 2 + indent_px,
359             con->deco_rect.y + 14, /* TODO: hardcoded */
360             win->name_x
361         );
362 }
363
364 /*
365  * This function pushes the properties of each node of the layout tree to
366  * X11 if they have changed (like the map state, position of the window, …).
367  * It recursively traverses all children of the given node.
368  *
369  */
370 static void x_push_node(Con *con) {
371     Con *current;
372     con_state *state;
373
374     LOG("Pushing changes for node %p / %s\n", con, con->name);
375     state = state_for_frame(con->frame);
376
377     /* reparent the child window (when the window was moved due to a sticky
378      * container) */
379     if (state->need_reparent && con->window != NULL) {
380         LOG("Reparenting child window\n");
381
382         /* Temporarily set the event masks to XCB_NONE so that we won’t get
383          * UnmapNotify events (otherwise the handler would close the container).
384          * These events are generated automatically when reparenting. */
385         uint32_t values[] = { XCB_NONE };
386         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
387         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
388
389         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
390
391         values[0] = FRAME_EVENT_MASK;
392         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
393         values[0] = CHILD_EVENT_MASK;
394         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
395
396         state->old_frame = XCB_NONE;
397         state->need_reparent = false;
398     }
399
400     /* map/unmap if map state changed, also ensure that the child window
401      * is changed if we are mapped *and* in initial state (meaning the
402      * container was empty before, but now got a child) */
403     if (state->mapped != con->mapped || (con->mapped && state->initial)) {
404         if (!con->mapped) {
405             xcb_void_cookie_t cookie;
406             cookie = xcb_unmap_window(conn, con->frame);
407             LOG("unmapping container (serial %d)\n", cookie.sequence);
408             /* Ignore enter_notifies which are generated when unmapping */
409             add_ignore_event(cookie.sequence);
410         } else {
411             xcb_void_cookie_t cookie;
412             if (state->initial && con->window != NULL) {
413                 cookie = xcb_map_window(conn, con->window->id);
414                 LOG("mapping child window (serial %d)\n", cookie.sequence);
415                 /* Ignore enter_notifies which are generated when mapping */
416                 add_ignore_event(cookie.sequence);
417             }
418             cookie = xcb_map_window(conn, con->frame);
419             LOG("mapping container (serial %d)\n", cookie.sequence);
420             /* Ignore enter_notifies which are generated when mapping */
421             add_ignore_event(cookie.sequence);
422         }
423         state->mapped = con->mapped;
424     }
425
426     bool fake_notify = false;
427     /* set new position if rect changed */
428     if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
429         LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
430         xcb_set_window_rect(conn, con->frame, con->rect);
431         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
432         fake_notify = true;
433     }
434
435     /* dito, but for child windows */
436     if (con->window != NULL &&
437         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
438         LOG("setting window rect (%d, %d, %d, %d)\n",
439             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
440         xcb_set_window_rect(conn, con->window->id, con->window_rect);
441         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
442         fake_notify = true;
443     }
444
445     if (fake_notify) {
446         LOG("Sending fake configure notify\n");
447         fake_absolute_configure_notify(con);
448     }
449
450     /* handle all children and floating windows of this node */
451     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
452         x_push_node(current);
453
454     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
455         x_push_node(current);
456
457     if (con->type != CT_ROOT && con->type != CT_OUTPUT)
458         x_draw_decoration(con);
459 }
460
461 /*
462  * Pushes all changes (state of each node, see x_push_node() and the window
463  * stack) to X11.
464  *
465  */
466 void x_push_changes(Con *con) {
467     con_state *state;
468
469     LOG("\n\n PUSHING CHANGES\n\n");
470     x_push_node(con);
471
472     LOG("-- PUSHING WINDOW STACK --\n");
473     bool order_changed = false;
474     /* X11 correctly represents the stack if we push it from bottom to top */
475     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
476         LOG("stack: 0x%08x\n", state->id);
477         con_state *prev = CIRCLEQ_PREV(state, state);
478         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
479         if (prev != old_prev)
480             order_changed = true;
481         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
482             state->initial = false;
483             LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
484             uint32_t mask = 0;
485             mask |= XCB_CONFIG_WINDOW_SIBLING;
486             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
487             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
488
489             xcb_configure_window(conn, prev->id, mask, values);
490         }
491     }
492
493     xcb_window_t to_focus = focused->frame;
494     if (focused->window != NULL)
495         to_focus = focused->window->id;
496
497     if (focused_id != to_focus) {
498         LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
499         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
500     }
501
502     xcb_flush(conn);
503     LOG("\n\n ENDING CHANGES\n\n");
504
505     /* save the current stack as old stack */
506     CIRCLEQ_FOREACH(state, &state_head, state) {
507         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
508         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
509     }
510     CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
511         LOG("old stack: 0x%08x\n", state->id);
512     }
513 }
514
515 /*
516  * Raises the specified container in the internal stack of X windows. The
517  * next call to x_push_changes() will make the change visible in X11.
518  *
519  */
520 void x_raise_con(Con *con) {
521     con_state *state;
522     LOG("raising in new stack: %p / %s\n", con, con->name);
523     state = state_for_frame(con->frame);
524
525     LOG("found state entry, moving to top\n");
526     CIRCLEQ_REMOVE(&state_head, state, state);
527     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
528 }