]> git.sur5r.net Git - i3/i3/blob - src/x.c
Add more documentation to functions/header files
[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     Rect rect;
22     Rect window_rect;
23
24     bool initial;
25
26     CIRCLEQ_ENTRY(con_state) state;
27     CIRCLEQ_ENTRY(con_state) old_state;
28 } con_state;
29
30 CIRCLEQ_HEAD(state_head, con_state) state_head =
31     CIRCLEQ_HEAD_INITIALIZER(state_head);
32
33 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
34     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
35
36 /*
37  * Returns the container state for the given frame. This function always
38  * returns a container state (otherwise, there is a bug in the code and the
39  * container state of a container for which x_con_init() was not called was
40  * requested).
41  *
42  */
43 static con_state *state_for_frame(xcb_window_t window) {
44     con_state *state;
45     CIRCLEQ_FOREACH(state, &state_head, state)
46         if (state->id == window)
47             return state;
48
49     /* TODO: better error handling? */
50     ELOG("No state found\n");
51     assert(false);
52     return NULL;
53 }
54
55 /*
56  * Initializes the X11 part for the given container. Called exactly once for
57  * every container from con_new().
58  *
59  */
60 void x_con_init(Con *con) {
61     /* TODO: maybe create the window when rendering first? we could then even
62      * get the initial geometry right */
63
64     uint32_t mask = 0;
65     uint32_t values[2];
66
67     /* our own frames should not be managed */
68     mask |= XCB_CW_OVERRIDE_REDIRECT;
69     values[0] = 1;
70
71     /* We want to know when… */
72     mask |= XCB_CW_EVENT_MASK;
73     values[1] = FRAME_EVENT_MASK;
74
75     Rect dims = { -15, -15, 10, 10 };
76     con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, -1, false, mask, values);
77     con->gc = xcb_generate_id(conn);
78     xcb_create_gc(conn, con->gc, con->frame, 0, 0);
79
80     struct con_state *state = scalloc(sizeof(struct con_state));
81     state->id = con->frame;
82     state->mapped = false;
83     state->initial = true;
84     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
85     CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
86     LOG("adding new state for window id 0x%08x\n", state->id);
87 }
88
89 /*
90  * Re-initializes the associated X window state for this container. You have
91  * to call this when you assign a client to an empty container to ensure that
92  * its state gets updated correctly.
93  *
94  */
95 void x_reinit(Con *con) {
96     struct con_state *state;
97
98     if ((state = state_for_frame(con->frame)) == NULL) {
99         ELOG("window state not found\n");
100         return;
101     }
102
103     LOG("resetting state %p to initial\n", state);
104     state->initial = true;
105     memset(&(state->window_rect), 0, sizeof(Rect));
106 }
107
108 /*
109  * Kills the window decoration associated with the given container.
110  *
111  */
112 void x_con_kill(Con *con) {
113     con_state *state;
114
115     xcb_destroy_window(conn, con->frame);
116     state = state_for_frame(con->frame);
117     CIRCLEQ_REMOVE(&state_head, state, state);
118     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
119 }
120
121 /*
122  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
123  *
124  */
125 static bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
126     xcb_get_property_cookie_t cookie;
127     xcb_get_wm_protocols_reply_t protocols;
128     bool result = false;
129
130     cookie = xcb_get_wm_protocols_unchecked(conn, window, atoms[WM_PROTOCOLS]);
131     if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
132         return false;
133
134     /* Check if the client’s protocols have the requested atom set */
135     for (uint32_t i = 0; i < protocols.atoms_len; i++)
136         if (protocols.atoms[i] == atom)
137             result = true;
138
139     xcb_get_wm_protocols_reply_wipe(&protocols);
140
141     return result;
142 }
143
144 /*
145  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
146  *
147  */
148 void x_window_kill(xcb_window_t window) {
149     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
150     if (!window_supports_protocol(window, atoms[WM_DELETE_WINDOW])) {
151         LOG("Killing window the hard way\n");
152         xcb_kill_client(conn, window);
153         return;
154     }
155
156     xcb_client_message_event_t ev;
157
158     memset(&ev, 0, sizeof(xcb_client_message_event_t));
159
160     ev.response_type = XCB_CLIENT_MESSAGE;
161     ev.window = window;
162     ev.type = atoms[WM_PROTOCOLS];
163     ev.format = 32;
164     ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
165     ev.data.data32[1] = XCB_CURRENT_TIME;
166
167     LOG("Sending WM_DELETE to the client\n");
168     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
169     xcb_flush(conn);
170 }
171
172 /*
173  * Draws the decoration of the given container onto its parent.
174  *
175  */
176 void x_draw_decoration(Con *con) {
177     Con *parent;
178
179     parent = con->parent;
180
181     if (con == focused)
182         xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
183     else xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#0C0C0C"));
184     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
185     xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
186
187     if (con->window == NULL)
188         return;
189
190     i3Window *win = con->window;
191
192     if (win->name_x == NULL) {
193         LOG("not rendering decoration, not yet known\n");
194         return;
195     }
196
197
198     LOG("should render text %s onto %p / %s\n", win->name_json, parent, parent->name);
199
200     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
201     if (win->uses_net_wm_name)
202         xcb_image_text_16(
203             conn,
204             win->name_len,
205             parent->frame,
206             parent->gc,
207             con->deco_rect.x,
208             con->deco_rect.y + 14, /* TODO: hardcoded */
209             (xcb_char2b_t*)win->name_x
210         );
211     else
212         xcb_image_text_8(
213             conn,
214             win->name_len,
215             parent->frame,
216             parent->gc,
217             con->deco_rect.x,
218             con->deco_rect.y + 14, /* TODO: hardcoded */
219             win->name_x
220         );
221 }
222
223 /*
224  * This function pushes the properties of each node of the layout tree to
225  * X11 if they have changed (like the map state, position of the window, …).
226  * It recursively traverses all children of the given node.
227  *
228  */
229 static void x_push_node(Con *con) {
230     Con *current;
231     con_state *state;
232
233     LOG("Pushing changes for node %p / %s\n", con, con->name);
234     state = state_for_frame(con->frame);
235
236     /* map/unmap if map state changed, also ensure that the child window
237      * is changed if we are mapped *and* in initial state (meaning the
238      * container was empty before, but now got a child) */
239     if (state->mapped != con->mapped || (con->mapped && state->initial)) {
240         if (!con->mapped) {
241             xcb_void_cookie_t cookie;
242             cookie = xcb_unmap_window(conn, con->frame);
243             LOG("unmapping container (serial %d)\n", cookie.sequence);
244             /* Ignore enter_notifies which are generated when unmapping */
245             add_ignore_event(cookie.sequence);
246         } else {
247             xcb_void_cookie_t cookie;
248             if (state->initial && con->window != NULL) {
249                 cookie = xcb_map_window(conn, con->window->id);
250                 LOG("mapping child window (serial %d)\n", cookie.sequence);
251                 /* Ignore enter_notifies which are generated when mapping */
252                 add_ignore_event(cookie.sequence);
253             }
254             cookie = xcb_map_window(conn, con->frame);
255             LOG("mapping container (serial %d)\n", cookie.sequence);
256             /* Ignore enter_notifies which are generated when mapping */
257             add_ignore_event(cookie.sequence);
258         }
259         state->mapped = con->mapped;
260     }
261
262     bool fake_notify = false;
263     /* set new position if rect changed */
264     if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
265         LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
266         xcb_set_window_rect(conn, con->frame, con->rect);
267         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
268         fake_notify = true;
269     }
270
271     /* dito, but for child windows */
272     if (memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
273         LOG("setting window rect (%d, %d, %d, %d)\n",
274             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
275         xcb_set_window_rect(conn, con->window->id, con->window_rect);
276         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
277         fake_notify = true;
278     }
279
280     if (fake_notify) {
281         LOG("Sending fake configure notify\n");
282         fake_absolute_configure_notify(con);
283     }
284
285     /* handle all children and floating windows of this node */
286     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
287         x_push_node(current);
288
289     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
290         x_push_node(current);
291
292     if (con->type != CT_ROOT && con->type != CT_OUTPUT)
293         x_draw_decoration(con);
294 }
295
296 /*
297  * Pushes all changes (state of each node, see x_push_node() and the window
298  * stack) to X11.
299  *
300  */
301 void x_push_changes(Con *con) {
302     con_state *state;
303
304     LOG("\n\n PUSHING CHANGES\n\n");
305     x_push_node(con);
306
307     LOG("-- PUSHING WINDOW STACK --\n");
308     bool order_changed = false;
309     /* X11 correctly represents the stack if we push it from bottom to top */
310     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
311         LOG("stack: 0x%08x\n", state->id);
312         con_state *prev = CIRCLEQ_PREV(state, state);
313         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
314         if (prev != old_prev)
315             order_changed = true;
316         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
317             state->initial = false;
318             LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
319             uint32_t mask = 0;
320             mask |= XCB_CONFIG_WINDOW_SIBLING;
321             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
322             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
323
324             xcb_configure_window(conn, prev->id, mask, values);
325         }
326     }
327
328     xcb_window_t to_focus = focused->frame;
329     if (focused->window != NULL)
330         to_focus = focused->window->id;
331
332     if (focused_id != to_focus) {
333         LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
334         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
335     }
336
337     xcb_flush(conn);
338     LOG("\n\n ENDING CHANGES\n\n");
339
340     /* save the current stack as old stack */
341     CIRCLEQ_FOREACH(state, &state_head, state) {
342         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
343         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
344     }
345     CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
346         LOG("old stack: 0x%08x\n", state->id);
347     }
348 }
349
350 /*
351  * Raises the specified container in the internal stack of X windows. The
352  * next call to x_push_changes() will make the change visible in X11.
353  *
354  */
355 void x_raise_con(Con *con) {
356     con_state *state;
357     LOG("raising in new stack: %p / %s\n", con, con->name);
358     state = state_for_frame(con->frame);
359
360     LOG("found state entry, moving to top\n");
361     CIRCLEQ_REMOVE(&state_head, state, state);
362     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
363 }