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