]> git.sur5r.net Git - i3/i3/blob - src/x.c
2ed8fd11fb6b928d012339a21872294e3bf468cc
[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(true);
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 void x_con_kill(Con *con) {
90     con_state *state;
91
92     xcb_destroy_window(conn, con->frame);
93     state = state_for_frame(con->frame);
94     CIRCLEQ_REMOVE(&state_head, state, state);
95     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
96 }
97
98 /*
99  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
100  *
101  */
102 static bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
103     xcb_get_property_cookie_t cookie;
104     xcb_get_wm_protocols_reply_t protocols;
105     bool result = false;
106
107     cookie = xcb_get_wm_protocols_unchecked(conn, window, atoms[WM_PROTOCOLS]);
108     if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
109         return false;
110
111     /* Check if the client’s protocols have the requested atom set */
112     for (uint32_t i = 0; i < protocols.atoms_len; i++)
113         if (protocols.atoms[i] == atom)
114             result = true;
115
116     xcb_get_wm_protocols_reply_wipe(&protocols);
117
118     return result;
119 }
120
121 void x_window_kill(xcb_window_t window) {
122     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
123     if (!window_supports_protocol(window, atoms[WM_DELETE_WINDOW])) {
124         LOG("Killing window the hard way\n");
125         xcb_kill_client(conn, window);
126         return;
127     }
128
129     xcb_client_message_event_t ev;
130
131     memset(&ev, 0, sizeof(xcb_client_message_event_t));
132
133     ev.response_type = XCB_CLIENT_MESSAGE;
134     ev.window = window;
135     ev.type = atoms[WM_PROTOCOLS];
136     ev.format = 32;
137     ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
138     ev.data.data32[1] = XCB_CURRENT_TIME;
139
140     LOG("Sending WM_DELETE to the client\n");
141     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
142     xcb_flush(conn);
143 }
144
145 void x_draw_decoration(Con *con) {
146     Con *parent;
147
148     parent = con->parent;
149
150     if (con == focused)
151         xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
152     else xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#0C0C0C"));
153     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
154     xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
155
156     if (con->window == NULL) {
157         return;
158     }
159
160     if (con->window->class == NULL) {
161         LOG("not rendering decoration, not yet known\n");
162         return;
163     }
164
165
166     LOG("should render text %s onto %p / %s\n", con->window->class, parent, parent->name);
167
168     xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
169     xcb_image_text_8(
170         conn,
171         strlen(con->window->class),
172         parent->frame,
173         parent->gc,
174         con->deco_rect.x,
175         con->deco_rect.y + 14,
176         con->window->class
177     );
178 }
179
180 /*
181  * This function pushes the properties of each node of the layout tree to
182  * X11 if they have changed (like the map state, position of the window, …).
183  * It recursively traverses all children of the given node.
184  *
185  */
186 static void x_push_node(Con *con) {
187     Con *current;
188     con_state *state;
189
190     LOG("Pushing changes for node %p / %s\n", con, con->name);
191     state = state_for_frame(con->frame);
192
193     /* map/unmap if map state changed */
194     if (state->mapped != con->mapped) {
195         if (!con->mapped) {
196             LOG("unmapping container\n");
197             xcb_unmap_window(conn, con->frame);
198         } else {
199             if (state->initial && con->window != NULL) {
200                 LOG("mapping child window\n");
201                 xcb_map_window(conn, con->window->id);
202             }
203             LOG("mapping container\n");
204             xcb_map_window(conn, con->frame);
205         }
206         state->mapped = con->mapped;
207     }
208
209     /* set new position if rect changed */
210     if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
211         LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
212         xcb_set_window_rect(conn, con->frame, con->rect);
213         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
214     }
215
216     /* dito, but for child windows */
217     if (memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
218         LOG("setting window rect (%d, %d, %d, %d)\n",
219             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
220         xcb_set_window_rect(conn, con->window->id, con->window_rect);
221         memcpy(&(state->rect), &(con->rect), sizeof(Rect));
222     }
223
224     /* handle all children and floating windows of this node */
225     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
226         x_push_node(current);
227
228     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
229         x_push_node(current);
230
231     if (con->type != CT_ROOT && con->type != CT_OUTPUT)
232         x_draw_decoration(con);
233 }
234
235 /*
236  * Pushes all changes (state of each node, see x_push_node() and the window
237  * stack) to X11.
238  *
239  */
240 void x_push_changes(Con *con) {
241     con_state *state;
242
243     LOG("\n\n PUSHING CHANGES\n\n");
244     x_push_node(con);
245
246     LOG("-- PUSHING FOCUS STACK --\n");
247     /* X11 correctly represents the stack if we push it from bottom to top */
248     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
249         LOG("stack: 0x%08x\n", state->id);
250         con_state *prev = CIRCLEQ_PREV(state, state);
251         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
252         if ((state->initial || prev != old_prev) && prev != CIRCLEQ_END(&state_head)) {
253             state->initial = false;
254             LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
255             uint32_t mask = 0;
256             mask |= XCB_CONFIG_WINDOW_SIBLING;
257             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
258             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
259
260             xcb_configure_window(conn, prev->id, mask, values);
261         }
262     }
263
264     xcb_window_t to_focus = focused->frame;
265     if (focused->window != NULL)
266         to_focus = focused->window->id;
267
268     if (focused_id != to_focus) {
269         LOG("Updating focus\n");
270         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
271     }
272
273     xcb_flush(conn);
274     LOG("\n\n ENDING CHANGES\n\n");
275
276     /* save the current stack as old stack */
277     CIRCLEQ_FOREACH(state, &state_head, state) {
278         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
279         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
280     }
281     CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
282         LOG("old stack: 0x%08x\n", state->id);
283     }
284 }
285
286 /*
287  * Raises the specified container in the internal stack of X windows. The
288  * next call to x_push_changes() will make the change visible in X11.
289  *
290  */
291 void x_raise_con(Con *con) {
292     con_state *state;
293     LOG("raising in new stack: %p / %s\n", con, con->name);
294     state = state_for_frame(con->frame);
295
296     LOG("found state entry, moving to top\n");
297     CIRCLEQ_REMOVE(&state_head, state, state);
298     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
299 }