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