]> git.sur5r.net Git - i3/i3/blob - src/x.c
Paint the window decorations using the theme.
[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     if (!con_is_leaf(con) || (con->type != CT_CON && con->type != CT_FLOATING_CON))
226         return;
227
228     /* 1: find out which colors to use */
229     struct Colortriple *color;
230     if (con->urgent)
231         color = &config.client.urgent;
232     else if (con == focused)
233         color = &config.client.focused;
234     else if (con == TAILQ_FIRST(&(con->parent->focus_head)))
235         color = &config.client.focused_inactive;
236     else
237         color = &config.client.unfocused;
238
239     Con *parent = con->parent;
240
241     /* 2: draw a rectangle in border color around the client */
242     if (con->border_style != BS_NONE) {
243         xcb_change_gc_single(conn, con->gc, XCB_GC_FOREGROUND, color->background);
244         xcb_rectangle_t rect = { 0, 0, con->rect.width, con->rect.height };
245         xcb_poly_fill_rectangle(conn, con->frame, con->gc, 1, &rect);
246     }
247     if (con->border_style != BS_NORMAL)
248         return;
249
250     /* 3: paint the bar */
251     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->background);
252     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
253     xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
254
255     /* 4: draw the two lines in border color */
256     xcb_draw_line(conn, parent->frame, parent->gc, color->border,
257             con->deco_rect.x, /* x */
258             con->deco_rect.y, /* y */
259             con->deco_rect.x + con->deco_rect.width, /* to_x */
260             con->deco_rect.y); /* to_y */
261     xcb_draw_line(conn, parent->frame, parent->gc, color->border,
262             con->deco_rect.x, /* x */
263             con->deco_rect.y + con->deco_rect.height - 1, /* y */
264             con->deco_rect.x + con->deco_rect.width, /* to_x */
265             con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
266
267     /* 5: draw the title */
268     struct Window *win = con->window;
269     if (win == NULL || win ->name_x == NULL)
270         return;
271     xcb_change_gc_single(conn, parent->gc, XCB_GC_BACKGROUND, color->background);
272     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->text);
273     if (win->uses_net_wm_name)
274         xcb_image_text_16(
275             conn,
276             win->name_len,
277             parent->frame,
278             parent->gc,
279             con->deco_rect.x,
280             con->deco_rect.y + 14, /* TODO: hardcoded */
281             (xcb_char2b_t*)win->name_x
282         );
283     else
284         xcb_image_text_8(
285             conn,
286             win->name_len,
287             parent->frame,
288             parent->gc,
289             con->deco_rect.x,
290             con->deco_rect.y + 14, /* TODO: hardcoded */
291             win->name_x
292         );
293 }
294
295 /*
296  * This function pushes the properties of each node of the layout tree to
297  * X11 if they have changed (like the map state, position of the window, …).
298  * It recursively traverses all children of the given node.
299  *
300  */
301 static void x_push_node(Con *con) {
302     Con *current;
303     con_state *state;
304
305     LOG("Pushing changes for node %p / %s\n", con, con->name);
306     state = state_for_frame(con->frame);
307
308     /* reparent the child window (when the window was moved due to a sticky
309      * container) */
310     if (state->need_reparent && con->window != NULL) {
311         LOG("Reparenting child window\n");
312
313         /* Temporarily set the event masks to XCB_NONE so that we won’t get
314          * UnmapNotify events (otherwise the handler would close the container).
315          * These events are generated automatically when reparenting. */
316         uint32_t values[] = { XCB_NONE };
317         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
318         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
319
320         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
321
322         values[0] = FRAME_EVENT_MASK;
323         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
324         values[0] = CHILD_EVENT_MASK;
325         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
326
327         state->old_frame = XCB_NONE;
328         state->need_reparent = false;
329     }
330
331     /* map/unmap if map state changed, also ensure that the child window
332      * is changed if we are mapped *and* in initial state (meaning the
333      * container was empty before, but now got a child) */
334     if (state->mapped != con->mapped || (con->mapped && state->initial)) {
335         if (!con->mapped) {
336             xcb_void_cookie_t cookie;
337             cookie = xcb_unmap_window(conn, con->frame);
338             LOG("unmapping container (serial %d)\n", cookie.sequence);
339             /* Ignore enter_notifies which are generated when unmapping */
340             add_ignore_event(cookie.sequence);
341         } else {
342             xcb_void_cookie_t cookie;
343             if (state->initial && con->window != NULL) {
344                 cookie = xcb_map_window(conn, con->window->id);
345                 LOG("mapping child window (serial %d)\n", cookie.sequence);
346                 /* Ignore enter_notifies which are generated when mapping */
347                 add_ignore_event(cookie.sequence);
348             }
349             cookie = xcb_map_window(conn, con->frame);
350             LOG("mapping container (serial %d)\n", cookie.sequence);
351             /* Ignore enter_notifies which are generated when mapping */
352             add_ignore_event(cookie.sequence);
353         }
354         state->mapped = con->mapped;
355     }
356
357     bool fake_notify = false;
358     /* set new position if rect changed */
359     if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
360         LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
361         xcb_set_window_rect(conn, con->frame, con->rect);
362         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
363         fake_notify = true;
364     }
365
366     /* dito, but for child windows */
367     if (con->window != NULL &&
368         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
369         LOG("setting window rect (%d, %d, %d, %d)\n",
370             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
371         xcb_set_window_rect(conn, con->window->id, con->window_rect);
372         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
373         fake_notify = true;
374     }
375
376     if (fake_notify) {
377         LOG("Sending fake configure notify\n");
378         fake_absolute_configure_notify(con);
379     }
380
381     /* handle all children and floating windows of this node */
382     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
383         x_push_node(current);
384
385     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
386         x_push_node(current);
387
388     if (con->type != CT_ROOT && con->type != CT_OUTPUT)
389         x_draw_decoration(con);
390 }
391
392 /*
393  * Pushes all changes (state of each node, see x_push_node() and the window
394  * stack) to X11.
395  *
396  */
397 void x_push_changes(Con *con) {
398     con_state *state;
399
400     LOG("\n\n PUSHING CHANGES\n\n");
401     x_push_node(con);
402
403     LOG("-- PUSHING WINDOW STACK --\n");
404     bool order_changed = false;
405     /* X11 correctly represents the stack if we push it from bottom to top */
406     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
407         LOG("stack: 0x%08x\n", state->id);
408         con_state *prev = CIRCLEQ_PREV(state, state);
409         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
410         if (prev != old_prev)
411             order_changed = true;
412         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
413             state->initial = false;
414             LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
415             uint32_t mask = 0;
416             mask |= XCB_CONFIG_WINDOW_SIBLING;
417             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
418             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
419
420             xcb_configure_window(conn, prev->id, mask, values);
421         }
422     }
423
424     xcb_window_t to_focus = focused->frame;
425     if (focused->window != NULL)
426         to_focus = focused->window->id;
427
428     if (focused_id != to_focus) {
429         LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
430         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
431     }
432
433     xcb_flush(conn);
434     LOG("\n\n ENDING CHANGES\n\n");
435
436     /* save the current stack as old stack */
437     CIRCLEQ_FOREACH(state, &state_head, state) {
438         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
439         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
440     }
441     CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
442         LOG("old stack: 0x%08x\n", state->id);
443     }
444 }
445
446 /*
447  * Raises the specified container in the internal stack of X windows. The
448  * next call to x_push_changes() will make the change visible in X11.
449  *
450  */
451 void x_raise_con(Con *con) {
452     con_state *state;
453     LOG("raising in new stack: %p / %s\n", con, con->name);
454     state = state_for_frame(con->frame);
455
456     LOG("found state entry, moving to top\n");
457     CIRCLEQ_REMOVE(&state_head, state, state);
458     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
459 }