i3
x.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * x.c: Interface to X11, transfers our in-memory state to X11 (see also
8  * render.c). Basically a big state machine.
9  *
10  */
11 #include "all.h"
12 
13 #include <unistd.h>
14 
15 #ifndef MAX
16 #define MAX(x, y) ((x) > (y) ? (x) : (y))
17 #endif
18 
19 /* Stores the X11 window ID of the currently focused window */
20 xcb_window_t focused_id = XCB_NONE;
21 
22 /* Because 'focused_id' might be reset to force input focus, we separately keep
23  * track of the X11 window ID to be able to always tell whether the focused
24  * window actually changed. */
25 static xcb_window_t last_focused = XCB_NONE;
26 
27 /* Stores coordinates to warp mouse pointer to if set */
28 static Rect *warp_to;
29 
30 /*
31  * Describes the X11 state we may modify (map state, position, window stack).
32  * There is one entry per container. The state represents the current situation
33  * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
34  * which represents the order that will be pushed to X11, while old_state_head
35  * represents the current order). It will be updated in x_push_changes().
36  *
37  */
38 typedef struct con_state {
39  xcb_window_t id;
40  bool mapped;
41  bool unmap_now;
43  bool is_hidden;
44 
45  /* The con for which this state is. */
46  Con *con;
47 
48  /* For reparenting, we have a flag (need_reparent) and the X ID of the old
49  * frame this window was in. The latter is necessary because we need to
50  * ignore UnmapNotify events (by changing the window event mask). */
52  xcb_window_t old_frame;
53 
54  /* The container was child of floating container during the previous call of
55  * x_push_node(). This is used to remove the shape when the container is no
56  * longer floating. */
58 
61 
62  bool initial;
63 
64  char *name;
65 
67  CIRCLEQ_ENTRY(con_state) old_state;
68  TAILQ_ENTRY(con_state) initial_mapping_order;
70 
71 CIRCLEQ_HEAD(state_head, con_state) state_head =
72  CIRCLEQ_HEAD_INITIALIZER(state_head);
73 
74 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
75  CIRCLEQ_HEAD_INITIALIZER(old_state_head);
76 
77 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
78  TAILQ_HEAD_INITIALIZER(initial_mapping_head);
79 
80 /*
81  * Returns the container state for the given frame. This function always
82  * returns a container state (otherwise, there is a bug in the code and the
83  * container state of a container for which x_con_init() was not called was
84  * requested).
85  *
86  */
87 static con_state *state_for_frame(xcb_window_t window) {
89  CIRCLEQ_FOREACH (state, &state_head, state) {
90  if (state->id == window) {
91  return state;
92  }
93  }
94 
95  /* TODO: better error handling? */
96  ELOG("No state found for window 0x%08x\n", window);
97  assert(false);
98  return NULL;
99 }
100 
101 /*
102  * Changes the atoms on the root window and the windows themselves to properly
103  * reflect the current focus for ewmh compliance.
104  *
105  */
106 static void change_ewmh_focus(xcb_window_t new_focus, xcb_window_t old_focus) {
107  if (new_focus == old_focus) {
108  return;
109  }
110 
111  ewmh_update_active_window(new_focus);
112 
113  if (new_focus != XCB_WINDOW_NONE) {
114  ewmh_update_focused(new_focus, true);
115  }
116 
117  if (old_focus != XCB_WINDOW_NONE) {
118  ewmh_update_focused(old_focus, false);
119  }
120 }
121 
122 /*
123  * Initializes the X11 part for the given container. Called exactly once for
124  * every container from con_new().
125  *
126  */
127 void x_con_init(Con *con) {
128  /* TODO: maybe create the window when rendering first? we could then even
129  * get the initial geometry right */
130 
131  uint32_t mask = 0;
132  uint32_t values[5];
133 
134  xcb_visualid_t visual = get_visualid_by_depth(con->depth);
135  xcb_colormap_t win_colormap;
136  if (con->depth != root_depth) {
137  /* We need to create a custom colormap. */
138  win_colormap = xcb_generate_id(conn);
139  xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
140  con->colormap = win_colormap;
141  } else {
142  /* Use the default colormap. */
143  win_colormap = colormap;
144  con->colormap = XCB_NONE;
145  }
146 
147  /* We explicitly set a background color and border color (even though we
148  * don’t even have a border) because the X11 server requires us to when
149  * using 32 bit color depths, see
150  * https://stackoverflow.com/questions/3645632 */
151  mask |= XCB_CW_BACK_PIXEL;
152  values[0] = root_screen->black_pixel;
153 
154  mask |= XCB_CW_BORDER_PIXEL;
155  values[1] = root_screen->black_pixel;
156 
157  /* our own frames should not be managed */
158  mask |= XCB_CW_OVERRIDE_REDIRECT;
159  values[2] = 1;
160 
161  /* see include/xcb.h for the FRAME_EVENT_MASK */
162  mask |= XCB_CW_EVENT_MASK;
163  values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
164 
165  mask |= XCB_CW_COLORMAP;
166  values[4] = win_colormap;
167 
168  Rect dims = {-15, -15, 10, 10};
169  xcb_window_t frame_id = create_window(conn, dims, con->depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
170  draw_util_surface_init(conn, &(con->frame), frame_id, get_visualtype_by_id(visual), dims.width, dims.height);
171  xcb_change_property(conn,
172  XCB_PROP_MODE_REPLACE,
173  con->frame.id,
174  XCB_ATOM_WM_CLASS,
175  XCB_ATOM_STRING,
176  8,
177  (strlen("i3-frame") + 1) * 2,
178  "i3-frame\0i3-frame\0");
179 
180  struct con_state *state = scalloc(1, sizeof(struct con_state));
181  state->id = con->frame.id;
182  state->mapped = false;
183  state->initial = true;
184  DLOG("Adding window 0x%08x to lists\n", state->id);
185  CIRCLEQ_INSERT_HEAD(&state_head, state, state);
186  CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
187  TAILQ_INSERT_TAIL(&initial_mapping_head, state, initial_mapping_order);
188  DLOG("adding new state for window id 0x%08x\n", state->id);
189 }
190 
191 /*
192  * Re-initializes the associated X window state for this container. You have
193  * to call this when you assign a client to an empty container to ensure that
194  * its state gets updated correctly.
195  *
196  */
197 void x_reinit(Con *con) {
198  struct con_state *state;
199 
200  if ((state = state_for_frame(con->frame.id)) == NULL) {
201  ELOG("window state not found\n");
202  return;
203  }
204 
205  DLOG("resetting state %p to initial\n", state);
206  state->initial = true;
207  state->child_mapped = false;
208  state->con = con;
209  memset(&(state->window_rect), 0, sizeof(Rect));
210 }
211 
212 /*
213  * Reparents the child window of the given container (necessary for sticky
214  * containers). The reparenting happens in the next call of x_push_changes().
215  *
216  */
217 void x_reparent_child(Con *con, Con *old) {
218  struct con_state *state;
219  if ((state = state_for_frame(con->frame.id)) == NULL) {
220  ELOG("window state for con not found\n");
221  return;
222  }
223 
224  state->need_reparent = true;
225  state->old_frame = old->frame.id;
226 }
227 
228 /*
229  * Moves a child window from Container src to Container dest.
230  *
231  */
232 void x_move_win(Con *src, Con *dest) {
233  struct con_state *state_src, *state_dest;
234 
235  if ((state_src = state_for_frame(src->frame.id)) == NULL) {
236  ELOG("window state for src not found\n");
237  return;
238  }
239 
240  if ((state_dest = state_for_frame(dest->frame.id)) == NULL) {
241  ELOG("window state for dest not found\n");
242  return;
243  }
244 
245  state_dest->con = state_src->con;
246  state_src->con = NULL;
247 
248  if (rect_equals(state_dest->window_rect, (Rect){0, 0, 0, 0})) {
249  memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
250  DLOG("COPYING RECT\n");
251  }
252 }
253 
254 static void _x_con_kill(Con *con) {
255  con_state *state;
256 
257  if (con->colormap != XCB_NONE) {
258  xcb_free_colormap(conn, con->colormap);
259  }
260 
263  xcb_free_pixmap(conn, con->frame_buffer.id);
264  con->frame_buffer.id = XCB_NONE;
265  state = state_for_frame(con->frame.id);
266  CIRCLEQ_REMOVE(&state_head, state, state);
267  CIRCLEQ_REMOVE(&old_state_head, state, old_state);
268  TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
269  FREE(state->name);
270  free(state);
271 
272  /* Invalidate focused_id to correctly focus new windows with the same ID */
273  if (con->frame.id == focused_id) {
274  focused_id = XCB_NONE;
275  }
276  if (con->frame.id == last_focused) {
277  last_focused = XCB_NONE;
278  }
279 }
280 
281 /*
282  * Kills the window decoration associated with the given container.
283  *
284  */
286  _x_con_kill(con);
287  xcb_destroy_window(conn, con->frame.id);
288 }
289 
290 /*
291  * Completely reinitializes the container's frame, without destroying the old window.
292  *
293  */
295  _x_con_kill(con);
296  x_con_init(con);
297 }
298 
299 /*
300  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
301  *
302  */
303 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
304  xcb_get_property_cookie_t cookie;
305  xcb_icccm_get_wm_protocols_reply_t protocols;
306  bool result = false;
307 
308  cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
309  if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
310  return false;
311 
312  /* Check if the client’s protocols have the requested atom set */
313  for (uint32_t i = 0; i < protocols.atoms_len; i++)
314  if (protocols.atoms[i] == atom)
315  result = true;
316 
317  xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
318 
319  return result;
320 }
321 
322 /*
323  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
324  *
325  */
326 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
327  /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
328  if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
329  if (kill_window == KILL_WINDOW) {
330  LOG("Killing specific window 0x%08x\n", window);
331  xcb_destroy_window(conn, window);
332  } else {
333  LOG("Killing the X11 client which owns window 0x%08x\n", window);
334  xcb_kill_client(conn, window);
335  }
336  return;
337  }
338 
339  /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
340  * In order to properly initialize these bytes, we allocate 32 bytes even
341  * though we only need less for an xcb_configure_notify_event_t */
342  void *event = scalloc(32, 1);
343  xcb_client_message_event_t *ev = event;
344 
345  ev->response_type = XCB_CLIENT_MESSAGE;
346  ev->window = window;
347  ev->type = A_WM_PROTOCOLS;
348  ev->format = 32;
349  ev->data.data32[0] = A_WM_DELETE_WINDOW;
350  ev->data.data32[1] = XCB_CURRENT_TIME;
351 
352  LOG("Sending WM_DELETE to the client\n");
353  xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
354  xcb_flush(conn);
355  free(event);
356 }
357 
358 static void x_draw_title_border(Con *con, struct deco_render_params *p) {
359  assert(con->parent != NULL);
360 
361  Rect *dr = &(con->deco_rect);
362 
363  /* Left */
365  dr->x, dr->y, 1, dr->height);
366 
367  /* Right */
369  dr->x + dr->width - 1, dr->y, 1, dr->height);
370 
371  /* Top */
373  dr->x, dr->y, dr->width, 1);
374 
375  /* Bottom */
377  dr->x, dr->y + dr->height - 1, dr->width, 1);
378 }
379 
381  assert(con->parent != NULL);
382 
383  Rect *dr = &(con->deco_rect);
384 
385  /* Redraw the right border to cut off any text that went past it.
386  * This is necessary when the text was drawn using XCB since cutting text off
387  * automatically does not work there. For pango rendering, this isn't necessary. */
388  if (!font_is_pango()) {
389  /* We actually only redraw the far right two pixels as that is the
390  * distance we keep from the edge (not the entire border width).
391  * Redrawing the entire border would cause text to be cut off. */
393  dr->x + dr->width - 2 * logical_px(1),
394  dr->y,
395  2 * logical_px(1),
396  dr->height);
397  }
398 
399  /* Redraw the border. */
401 }
402 
403 /*
404  * Get rectangles representing the border around the child window. Some borders
405  * are adjacent to the screen-edge and thus not returned. Return value is the
406  * number of rectangles.
407  *
408  */
409 static size_t x_get_border_rectangles(Con *con, xcb_rectangle_t rectangles[4]) {
410  size_t count = 0;
411  int border_style = con_border_style(con);
412 
413  if (border_style != BS_NONE && con_is_leaf(con)) {
416 
417  if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
418  rectangles[count++] = (xcb_rectangle_t){
419  .x = 0,
420  .y = 0,
421  .width = br.x,
422  .height = con->rect.height,
423  };
424  }
425  if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
426  rectangles[count++] = (xcb_rectangle_t){
427  .x = con->rect.width + (br.width + br.x),
428  .y = 0,
429  .width = -(br.width + br.x),
430  .height = con->rect.height,
431  };
432  }
433  if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
434  rectangles[count++] = (xcb_rectangle_t){
435  .x = br.x,
436  .y = con->rect.height + (br.height + br.y),
437  .width = con->rect.width + br.width,
438  .height = -(br.height + br.y),
439  };
440  }
441  /* pixel border have an additional line at the top */
442  if (border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
443  rectangles[count++] = (xcb_rectangle_t){
444  .x = br.x,
445  .y = 0,
446  .width = con->rect.width + br.width,
447  .height = br.y,
448  };
449  }
450  }
451 
452  return count;
453 }
454 
455 /*
456  * Draws the decoration of the given container onto its parent.
457  *
458  */
460  Con *parent = con->parent;
461  bool leaf = con_is_leaf(con);
462 
463  /* This code needs to run for:
464  * • leaf containers
465  * • non-leaf containers which are in a stacked/tabbed container
466  *
467  * It does not need to run for:
468  * • direct children of outputs or dockareas
469  * • floating containers (they don’t have a decoration)
470  */
471  if ((!leaf &&
472  parent->layout != L_STACKED &&
473  parent->layout != L_TABBED) ||
474  parent->type == CT_OUTPUT ||
475  parent->type == CT_DOCKAREA ||
476  con->type == CT_FLOATING_CON)
477  return;
478 
479  /* Skip containers whose height is 0 (for example empty dockareas) */
480  if (con->rect.height == 0)
481  return;
482 
483  /* Skip containers whose pixmap has not yet been created (can happen when
484  * decoration rendering happens recursively for a window for which
485  * x_push_node() was not yet called) */
486  if (leaf && con->frame_buffer.id == XCB_NONE)
487  return;
488 
489  /* 1: build deco_params and compare with cache */
490  struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
491 
492  /* find out which colors to use */
493  if (con->urgent)
494  p->color = &config.client.urgent;
495  else if (con == focused || con_inside_focused(con))
496  p->color = &config.client.focused;
497  else if (con == TAILQ_FIRST(&(parent->focus_head)))
499  else
501 
502  p->border_style = con_border_style(con);
503 
504  Rect *r = &(con->rect);
505  Rect *w = &(con->window_rect);
506  p->con_rect = (struct width_height){r->width, r->height};
507  p->con_window_rect = (struct width_height){w->width, w->height};
508  p->con_deco_rect = con->deco_rect;
510  p->con_is_leaf = con_is_leaf(con);
511  p->parent_layout = con->parent->layout;
512 
513  if (con->deco_render_params != NULL &&
514  (con->window == NULL || !con->window->name_x_changed) &&
515  !parent->pixmap_recreated &&
516  !con->pixmap_recreated &&
517  !con->mark_changed &&
518  memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
519  free(p);
520  goto copy_pixmaps;
521  }
522 
523  Con *next = con;
524  while ((next = TAILQ_NEXT(next, nodes))) {
525  FREE(next->deco_render_params);
526  }
527 
528  FREE(con->deco_render_params);
529  con->deco_render_params = p;
530 
531  if (con->window != NULL && con->window->name_x_changed)
532  con->window->name_x_changed = false;
533 
534  parent->pixmap_recreated = false;
535  con->pixmap_recreated = false;
536  con->mark_changed = false;
537 
538  /* 2: draw the client.background, but only for the parts around the window_rect */
539  if (con->window != NULL) {
540  /* top area */
542  0, 0, r->width, w->y);
543  /* bottom area */
545  0, w->y + w->height, r->width, r->height - (w->y + w->height));
546  /* left area */
548  0, 0, w->x, r->height);
549  /* right area */
551  w->x + w->width, 0, r->width - (w->x + w->width), r->height);
552  }
553 
554  /* 3: draw a rectangle in border color around the client */
555  if (p->border_style != BS_NONE && p->con_is_leaf) {
556  /* Fill the border. We don’t just fill the whole rectangle because some
557  * children are not freely resizable and we want their background color
558  * to "shine through". */
559  xcb_rectangle_t rectangles[4];
560  size_t rectangles_count = x_get_border_rectangles(con, rectangles);
561  for (size_t i = 0; i < rectangles_count; i++) {
563  rectangles[i].x,
564  rectangles[i].y,
565  rectangles[i].width,
566  rectangles[i].height);
567  }
568 
569  /* Highlight the side of the border at which the next window will be
570  * opened if we are rendering a single window within a split container
571  * (which is undistinguishable from a single window outside a split
572  * container otherwise. */
573  Rect br = con_border_style_rect(con);
574  if (TAILQ_NEXT(con, nodes) == NULL &&
575  TAILQ_PREV(con, nodes_head, nodes) == NULL &&
576  con->parent->type != CT_FLOATING_CON) {
577  if (p->parent_layout == L_SPLITH) {
579  r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height);
580  } else if (p->parent_layout == L_SPLITV) {
582  br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
583  }
584  }
585  }
586 
587  /* If the parent hasn't been set up yet, skip the decoration rendering
588  * for now. */
589  if (parent->frame_buffer.id == XCB_NONE)
590  goto copy_pixmaps;
591 
592  /* For the first child, we clear the parent pixmap to ensure there's no
593  * garbage left on there. This is important to avoid tearing when using
594  * transparency. */
595  if (con == TAILQ_FIRST(&(con->parent->nodes_head))) {
598  }
599 
600  /* if this is a borderless/1pixel window, we don’t need to render the
601  * decoration. */
602  if (p->border_style != BS_NORMAL)
603  goto copy_pixmaps;
604 
605  /* 4: paint the bar */
607  con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height);
608 
609  /* 5: draw title border */
610  x_draw_title_border(con, p);
611 
612  /* 6: draw the title */
613  int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
614 
615  const int title_padding = logical_px(2);
616  const int deco_width = (int)con->deco_rect.width;
617  int mark_width = 0;
618  if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
619  char *formatted_mark = sstrdup("");
620  bool had_visible_mark = false;
621 
622  mark_t *mark;
623  TAILQ_FOREACH (mark, &(con->marks_head), marks) {
624  if (mark->name[0] == '_')
625  continue;
626  had_visible_mark = true;
627 
628  char *buf;
629  sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
630  free(formatted_mark);
631  formatted_mark = buf;
632  }
633 
634  if (had_visible_mark) {
635  i3String *mark = i3string_from_utf8(formatted_mark);
636  mark_width = predict_text_width(mark);
637 
638  int mark_offset_x = (config.title_align == ALIGN_RIGHT)
639  ? title_padding
640  : deco_width - mark_width - title_padding;
641 
642  draw_util_text(mark, &(parent->frame_buffer),
643  p->color->text, p->color->background,
644  con->deco_rect.x + mark_offset_x,
645  con->deco_rect.y + text_offset_y, mark_width);
646  I3STRING_FREE(mark);
647 
648  mark_width += title_padding;
649  }
650 
651  FREE(formatted_mark);
652  }
653 
654  i3String *title = NULL;
655  struct Window *win = con->window;
656  if (win == NULL) {
657  if (con->title_format == NULL) {
658  char *_title;
659  char *tree = con_get_tree_representation(con);
660  sasprintf(&_title, "i3: %s", tree);
661  free(tree);
662 
663  title = i3string_from_utf8(_title);
664  FREE(_title);
665  } else {
666  title = con_parse_title_format(con);
667  }
668  } else {
669  title = con->title_format == NULL ? win->name : con_parse_title_format(con);
670  }
671  if (title == NULL) {
672  goto copy_pixmaps;
673  }
674 
675  int title_offset_x;
676  switch (config.title_align) {
677  case ALIGN_LEFT:
678  /* (pad)[text ](pad)[mark + its pad) */
679  title_offset_x = title_padding;
680  break;
681  case ALIGN_CENTER:
682  /* (pad)[ text ](pad)[mark + its pad)
683  * To center the text inside its allocated space, the surface
684  * between the brackets, we use the formula
685  * (surface_width - predict_text_width) / 2
686  * where surface_width = deco_width - 2 * pad - mark_width
687  * so, offset = pad + (surface_width - predict_text_width) / 2 =
688  * = … = (deco_width - mark_width - predict_text_width) / 2 */
689  title_offset_x = max(title_padding, (deco_width - mark_width - predict_text_width(title)) / 2);
690  break;
691  case ALIGN_RIGHT:
692  /* [mark + its pad](pad)[ text](pad) */
693  title_offset_x = max(title_padding + mark_width, deco_width - title_padding - predict_text_width(title));
694  break;
695  }
696 
697  draw_util_text(title, &(parent->frame_buffer),
698  p->color->text, p->color->background,
699  con->deco_rect.x + title_offset_x,
700  con->deco_rect.y + text_offset_y,
701  deco_width - mark_width - 2 * title_padding);
702 
703  if (win == NULL || con->title_format != NULL) {
704  I3STRING_FREE(title);
705  }
706 
708 copy_pixmaps:
709  draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
710 }
711 
712 /*
713  * Recursively calls x_draw_decoration. This cannot be done in x_push_node
714  * because x_push_node uses focus order to recurse (see the comment above)
715  * while drawing the decoration needs to happen in the actual order.
716  *
717  */
718 void x_deco_recurse(Con *con) {
719  Con *current;
720  bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
721  TAILQ_EMPTY(&(con->floating_head));
722  con_state *state = state_for_frame(con->frame.id);
723 
724  if (!leaf) {
725  TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
726  x_deco_recurse(current);
727  }
728 
729  TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
730  x_deco_recurse(current);
731  }
732 
733  if (state->mapped) {
734  draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
735  }
736  }
737 
738  if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
739  (!leaf || con->mapped))
740  x_draw_decoration(con);
741 }
742 
743 /*
744  * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
745  *
746  */
747 static void set_hidden_state(Con *con) {
748  if (con->window == NULL) {
749  return;
750  }
751 
752  con_state *state = state_for_frame(con->frame.id);
753  bool should_be_hidden = con_is_hidden(con);
754  if (should_be_hidden == state->is_hidden)
755  return;
756 
757  if (should_be_hidden) {
758  DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
759  xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
760  } else {
761  DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
762  xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
763  }
764 
765  state->is_hidden = should_be_hidden;
766 }
767 
768 /*
769  * Set the container frame shape as the union of the window shape and the
770  * shape of the frame borders.
771  */
772 static void x_shape_frame(Con *con, xcb_shape_sk_t shape_kind) {
773  assert(con->window);
774 
775  xcb_shape_combine(conn, XCB_SHAPE_SO_SET, shape_kind, shape_kind,
776  con->frame.id,
777  con->window_rect.x + con->border_width,
778  con->window_rect.y + con->border_width,
779  con->window->id);
780  xcb_rectangle_t rectangles[4];
781  size_t rectangles_count = x_get_border_rectangles(con, rectangles);
782  if (rectangles_count) {
783  xcb_shape_rectangles(conn, XCB_SHAPE_SO_UNION, shape_kind,
784  XCB_CLIP_ORDERING_UNSORTED, con->frame.id,
785  0, 0, rectangles_count, rectangles);
786  }
787 }
788 
789 /*
790  * Reset the container frame shape.
791  */
792 static void x_unshape_frame(Con *con, xcb_shape_sk_t shape_kind) {
793  assert(con->window);
794 
795  xcb_shape_mask(conn, XCB_SHAPE_SO_SET, shape_kind, con->frame.id, 0, 0, XCB_PIXMAP_NONE);
796 }
797 
798 /*
799  * Shape or unshape container frame based on the con state.
800  */
801 static void set_shape_state(Con *con, bool need_reshape) {
802  if (!shape_supported || con->window == NULL) {
803  return;
804  }
805 
806  struct con_state *state;
807  if ((state = state_for_frame(con->frame.id)) == NULL) {
808  ELOG("window state for con %p not found\n", con);
809  return;
810  }
811 
812  if (need_reshape && con_is_floating(con)) {
813  /* We need to reshape the window frame only if it already has shape. */
814  if (con->window->shaped) {
815  x_shape_frame(con, XCB_SHAPE_SK_BOUNDING);
816  }
817  if (con->window->input_shaped) {
818  x_shape_frame(con, XCB_SHAPE_SK_INPUT);
819  }
820  }
821 
822  if (state->was_floating && !con_is_floating(con)) {
823  /* Remove the shape when container is no longer floating. */
824  if (con->window->shaped) {
825  x_unshape_frame(con, XCB_SHAPE_SK_BOUNDING);
826  }
827  if (con->window->input_shaped) {
828  x_unshape_frame(con, XCB_SHAPE_SK_INPUT);
829  }
830  }
831 }
832 
833 /*
834  * This function pushes the properties of each node of the layout tree to
835  * X11 if they have changed (like the map state, position of the window, …).
836  * It recursively traverses all children of the given node.
837  *
838  */
840  Con *current;
841  con_state *state;
842  Rect rect = con->rect;
843 
844  //DLOG("Pushing changes for node %p / %s\n", con, con->name);
845  state = state_for_frame(con->frame.id);
846 
847  if (state->name != NULL) {
848  DLOG("pushing name %s for con %p\n", state->name, con);
849 
850  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame.id,
851  XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
852  FREE(state->name);
853  }
854 
855  if (con->window == NULL) {
856  /* Calculate the height of all window decorations which will be drawn on to
857  * this frame. */
858  uint32_t max_y = 0, max_height = 0;
859  TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
860  Rect *dr = &(current->deco_rect);
861  if (dr->y >= max_y && dr->height >= max_height) {
862  max_y = dr->y;
863  max_height = dr->height;
864  }
865  }
866  rect.height = max_y + max_height;
867  if (rect.height == 0)
868  con->mapped = false;
869  }
870 
871  bool need_reshape = false;
872 
873  /* reparent the child window (when the window was moved due to a sticky
874  * container) */
875  if (state->need_reparent && con->window != NULL) {
876  DLOG("Reparenting child window\n");
877 
878  /* Temporarily set the event masks to XCB_NONE so that we won’t get
879  * UnmapNotify events (otherwise the handler would close the container).
880  * These events are generated automatically when reparenting. */
881  uint32_t values[] = {XCB_NONE};
882  xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
883  xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
884 
885  xcb_reparent_window(conn, con->window->id, con->frame.id, 0, 0);
886 
887  values[0] = FRAME_EVENT_MASK;
888  xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
889  values[0] = CHILD_EVENT_MASK;
890  xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
891 
892  state->old_frame = XCB_NONE;
893  state->need_reparent = false;
894 
895  con->ignore_unmap++;
896  DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
898 
899  need_reshape = true;
900  }
901 
902  /* We need to update shape when window frame dimensions is updated. */
903  need_reshape |= state->rect.width != rect.width ||
904  state->rect.height != rect.height ||
905  state->window_rect.width != con->window_rect.width ||
906  state->window_rect.height != con->window_rect.height;
907 
908  /* We need to set shape when container becomes floating. */
909  need_reshape |= con_is_floating(con) && !state->was_floating;
910 
911  /* The pixmap of a borderless leaf container will not be used except
912  * for the titlebar in a stack or tabs (issue #1013). */
913  bool is_pixmap_needed = (con->border_style != BS_NONE ||
914  !con_is_leaf(con) ||
915  con->parent->layout == L_STACKED ||
916  con->parent->layout == L_TABBED);
917 
918  /* The root con and output cons will never require a pixmap. In particular for the
919  * __i3 output, this will likely not work anyway because it might be ridiculously
920  * large, causing an XCB_ALLOC error. */
921  if (con->type == CT_ROOT || con->type == CT_OUTPUT)
922  is_pixmap_needed = false;
923 
924  bool fake_notify = false;
925  /* Set new position if rect changed (and if height > 0) or if the pixmap
926  * needs to be recreated */
927  if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (!rect_equals(state->rect, rect) &&
928  rect.height > 0)) {
929  /* We first create the new pixmap, then render to it, set it as the
930  * background and only afterwards change the window size. This reduces
931  * flickering. */
932 
933  bool has_rect_changed = (state->rect.x != rect.x || state->rect.y != rect.y ||
934  state->rect.width != rect.width || state->rect.height != rect.height);
935 
936  /* Check if the container has an unneeded pixmap left over from
937  * previously having a border or titlebar. */
938  if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
940  xcb_free_pixmap(conn, con->frame_buffer.id);
941  con->frame_buffer.id = XCB_NONE;
942  }
943 
944  if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
945  if (con->frame_buffer.id == XCB_NONE) {
946  con->frame_buffer.id = xcb_generate_id(conn);
947  } else {
949  xcb_free_pixmap(conn, con->frame_buffer.id);
950  }
951 
952  uint16_t win_depth = root_depth;
953  if (con->window)
954  win_depth = con->window->depth;
955 
956  /* Ensure we have valid dimensions for our surface. */
957  // TODO This is probably a bug in the condition above as we should never enter this path
958  // for height == 0. Also, we should probably handle width == 0 the same way.
959  int width = MAX((int32_t)rect.width, 1);
960  int height = MAX((int32_t)rect.height, 1);
961 
962  xcb_create_pixmap(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
964  get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
965 
966  /* For the graphics context, we disable GraphicsExposure events.
967  * Those will be sent when a CopyArea request cannot be fulfilled
968  * properly due to parts of the source being unmapped or otherwise
969  * unavailable. Since we always copy from pixmaps to windows, this
970  * is not a concern for us. */
971  xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
972 
973  draw_util_surface_set_size(&(con->frame), width, height);
974  con->pixmap_recreated = true;
975 
976  /* Don’t render the decoration for windows inside a stack which are
977  * not visible right now */
978  // TODO Should this work the same way for L_TABBED?
979  if (!con->parent ||
980  con->parent->layout != L_STACKED ||
981  TAILQ_FIRST(&(con->parent->focus_head)) == con)
982  /* Render the decoration now to make the correct decoration visible
983  * from the very first moment. Later calls will be cached, so this
984  * doesn’t hurt performance. */
986  }
987 
988  DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
989  /* flush to ensure that the following commands are sent in a single
990  * buffer and will be processed directly afterwards (the contents of a
991  * window get lost when resizing it, therefore we want to provide it as
992  * fast as possible) */
993  xcb_flush(conn);
995  if (con->frame_buffer.id != XCB_NONE) {
997  }
998  xcb_flush(conn);
999 
1000  memcpy(&(state->rect), &rect, sizeof(Rect));
1001  fake_notify = true;
1002  }
1003 
1004  /* dito, but for child windows */
1005  if (con->window != NULL &&
1006  !rect_equals(state->window_rect, con->window_rect)) {
1007  DLOG("setting window rect (%d, %d, %d, %d)\n",
1010  memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
1011  fake_notify = true;
1012  }
1013 
1014  set_shape_state(con, need_reshape);
1015 
1016  /* Map if map state changed, also ensure that the child window
1017  * is changed if we are mapped and there is a new, unmapped child window.
1018  * Unmaps are handled in x_push_node_unmaps(). */
1019  if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
1020  con->mapped) {
1021  xcb_void_cookie_t cookie;
1022 
1023  if (con->window != NULL) {
1024  /* Set WM_STATE_NORMAL because GTK applications don’t want to
1025  * drag & drop if we don’t. Also, xprop(1) needs it. */
1026  long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
1027  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
1028  A_WM_STATE, A_WM_STATE, 32, 2, data);
1029  }
1030 
1031  uint32_t values[1];
1032  if (!state->child_mapped && con->window != NULL) {
1033  cookie = xcb_map_window(conn, con->window->id);
1034 
1035  /* We are interested in EnterNotifys as soon as the window is
1036  * mapped */
1037  values[0] = CHILD_EVENT_MASK;
1038  xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
1039  DLOG("mapping child window (serial %d)\n", cookie.sequence);
1040  state->child_mapped = true;
1041  }
1042 
1043  cookie = xcb_map_window(conn, con->frame.id);
1044 
1045  values[0] = FRAME_EVENT_MASK;
1046  xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
1047 
1048  /* copy the pixmap contents to the frame window immediately after mapping */
1049  if (con->frame_buffer.id != XCB_NONE) {
1051  }
1052  xcb_flush(conn);
1053 
1054  DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
1055  state->mapped = con->mapped;
1056  }
1057 
1058  state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
1059  state->was_floating = con_is_floating(con);
1060 
1061  if (fake_notify) {
1062  DLOG("Sending fake configure notify\n");
1064  }
1065 
1067 
1068  /* Handle all children and floating windows of this node. We recurse
1069  * in focus order to display the focused client in a stack first when
1070  * switching workspaces (reduces flickering). */
1071  TAILQ_FOREACH (current, &(con->focus_head), focused) {
1072  x_push_node(current);
1073  }
1074 }
1075 
1076 /*
1077  * Same idea as in x_push_node(), but this function only unmaps windows. It is
1078  * necessary to split this up to handle new fullscreen clients properly: The
1079  * new window needs to be mapped and focus needs to be set *before* the
1080  * underlying windows are unmapped. Otherwise, focus will revert to the
1081  * PointerRoot and will then be set to the new window, generating unnecessary
1082  * FocusIn/FocusOut events.
1083  *
1084  */
1085 static void x_push_node_unmaps(Con *con) {
1086  Con *current;
1087  con_state *state;
1088 
1089  //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
1090  state = state_for_frame(con->frame.id);
1091 
1092  /* map/unmap if map state changed, also ensure that the child window
1093  * is changed if we are mapped *and* in initial state (meaning the
1094  * container was empty before, but now got a child) */
1095  if (state->unmap_now) {
1096  xcb_void_cookie_t cookie;
1097  if (con->window != NULL) {
1098  /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
1099  long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
1100  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
1101  A_WM_STATE, A_WM_STATE, 32, 2, data);
1102  }
1103 
1104  cookie = xcb_unmap_window(conn, con->frame.id);
1105  DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
1106  /* we need to increase ignore_unmap for this container (if it
1107  * contains a window) and for every window "under" this one which
1108  * contains a window */
1109  if (con->window != NULL) {
1110  con->ignore_unmap++;
1111  DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
1112  }
1113  state->mapped = con->mapped;
1114  }
1115 
1116  /* handle all children and floating windows of this node */
1117  TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
1118  x_push_node_unmaps(current);
1119  }
1120 
1121  TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
1122  x_push_node_unmaps(current);
1123  }
1124 }
1125 
1126 /*
1127  * Returns true if the given container is currently attached to its parent.
1128  *
1129  * TODO: Remove once #1185 has been fixed
1130  */
1131 static bool is_con_attached(Con *con) {
1132  if (con->parent == NULL)
1133  return false;
1134 
1135  Con *current;
1136  TAILQ_FOREACH (current, &(con->parent->nodes_head), nodes) {
1137  if (current == con)
1138  return true;
1139  }
1140 
1141  return false;
1142 }
1143 
1144 /*
1145  * Pushes all changes (state of each node, see x_push_node() and the window
1146  * stack) to X11.
1147  *
1148  * NOTE: We need to push the stack first so that the windows have the correct
1149  * stacking order. This is relevant for workspace switching where we map the
1150  * windows because mapping may generate EnterNotify events. When they are
1151  * generated in the wrong order, this will cause focus problems when switching
1152  * workspaces.
1153  *
1154  */
1156  con_state *state;
1157  xcb_query_pointer_cookie_t pointercookie;
1158 
1159  /* If we need to warp later, we request the pointer position as soon as possible */
1160  if (warp_to) {
1161  pointercookie = xcb_query_pointer(conn, root);
1162  }
1163 
1164  DLOG("-- PUSHING WINDOW STACK --\n");
1165  //DLOG("Disabling EnterNotify\n");
1166  /* We need to keep SubstructureRedirect around, otherwise clients can send
1167  * ConfigureWindow requests and get them applied directly instead of having
1168  * them become ConfigureRequests that i3 handles. */
1169  uint32_t values[1] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT};
1170  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1171  if (state->mapped)
1172  xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1173  }
1174  //DLOG("Done, EnterNotify disabled\n");
1175  bool order_changed = false;
1176  bool stacking_changed = false;
1177 
1178  /* count first, necessary to (re)allocate memory for the bottom-to-top
1179  * stack afterwards */
1180  int cnt = 0;
1181  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1182  if (con_has_managed_window(state->con)) {
1183  cnt++;
1184  }
1185  }
1186 
1187  /* The bottom-to-top window stack of all windows which are managed by i3.
1188  * Used for x_get_window_stack(). */
1189  static xcb_window_t *client_list_windows = NULL;
1190  static int client_list_count = 0;
1191 
1192  if (cnt != client_list_count) {
1193  client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1194  client_list_count = cnt;
1195  }
1196 
1197  xcb_window_t *walk = client_list_windows;
1198 
1199  /* X11 correctly represents the stack if we push it from bottom to top */
1200  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1201  if (con_has_managed_window(state->con))
1202  memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1203 
1204  //DLOG("stack: 0x%08x\n", state->id);
1205  con_state *prev = CIRCLEQ_PREV(state, state);
1206  con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1207  if (prev != old_prev)
1208  order_changed = true;
1209  if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1210  stacking_changed = true;
1211  //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1212  uint32_t mask = 0;
1213  mask |= XCB_CONFIG_WINDOW_SIBLING;
1214  mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1215  uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1216 
1217  xcb_configure_window(conn, prev->id, mask, values);
1218  }
1219  state->initial = false;
1220  }
1221 
1222  /* If we re-stacked something (or a new window appeared), we need to update
1223  * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1224  if (stacking_changed) {
1225  DLOG("Client list changed (%i clients)\n", cnt);
1226  ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1227 
1228  walk = client_list_windows;
1229 
1230  /* reorder by initial mapping */
1231  TAILQ_FOREACH (state, &initial_mapping_head, initial_mapping_order) {
1232  if (con_has_managed_window(state->con))
1233  *walk++ = state->con->window->id;
1234  }
1235 
1236  ewmh_update_client_list(client_list_windows, client_list_count);
1237  }
1238 
1239  DLOG("PUSHING CHANGES\n");
1240  x_push_node(con);
1241 
1242  if (warp_to) {
1243  xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1244  if (!pointerreply) {
1245  ELOG("Could not query pointer position, not warping pointer\n");
1246  } else {
1247  int mid_x = warp_to->x + (warp_to->width / 2);
1248  int mid_y = warp_to->y + (warp_to->height / 2);
1249 
1250  Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1251  Output *target = get_output_containing(mid_x, mid_y);
1252  if (current != target) {
1253  /* Ignore MotionNotify events generated by warping */
1254  xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1255  xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1256  xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1257  }
1258 
1259  free(pointerreply);
1260  }
1261  warp_to = NULL;
1262  }
1263 
1264  //DLOG("Re-enabling EnterNotify\n");
1265  values[0] = FRAME_EVENT_MASK;
1266  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1267  if (state->mapped)
1268  xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1269  }
1270  //DLOG("Done, EnterNotify re-enabled\n");
1271 
1273 
1274  xcb_window_t to_focus = focused->frame.id;
1275  if (focused->window != NULL)
1276  to_focus = focused->window->id;
1277 
1278  if (focused_id != to_focus) {
1279  if (!focused->mapped) {
1280  DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1281  /* Invalidate focused_id to correctly focus new windows with the same ID */
1282  focused_id = XCB_NONE;
1283  } else {
1284  if (focused->window != NULL &&
1287  DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1290 
1292 
1294  ipc_send_window_event("focus", focused);
1295  } else {
1296  DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1297  /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1298  * no focus change events for our own focus changes. We only want
1299  * these generated by the clients. */
1300  if (focused->window != NULL) {
1301  values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1302  xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1303  }
1304  xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, last_timestamp);
1305  if (focused->window != NULL) {
1306  values[0] = CHILD_EVENT_MASK;
1307  xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1308  }
1309 
1311 
1312  if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1313  ipc_send_window_event("focus", focused);
1314  }
1315 
1317  }
1318  }
1319 
1320  if (focused_id == XCB_NONE) {
1321  /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1322  * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1323  DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1324  xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, last_timestamp);
1325  change_ewmh_focus(XCB_WINDOW_NONE, last_focused);
1326 
1328  last_focused = XCB_NONE;
1329  }
1330 
1331  xcb_flush(conn);
1332  DLOG("ENDING CHANGES\n");
1333 
1334  /* Disable EnterWindow events for windows which will be unmapped in
1335  * x_push_node_unmaps() now. Unmapping windows happens when switching
1336  * workspaces. We want to avoid getting EnterNotifies during that phase
1337  * because they would screw up our focus. One of these cases is having a
1338  * stack with two windows. If the first window is focused and gets
1339  * unmapped, the second one appears under the cursor and therefore gets an
1340  * EnterNotify event. */
1341  values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1342  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1343  if (!state->unmap_now)
1344  continue;
1345  xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1346  }
1347 
1348  /* Push all pending unmaps */
1350 
1351  /* save the current stack as old stack */
1352  CIRCLEQ_FOREACH (state, &state_head, state) {
1353  CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1354  CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1355  }
1356  //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1357  // DLOG("old stack: 0x%08x\n", state->id);
1358  //}
1359 
1360  xcb_flush(conn);
1361 }
1362 
1363 /*
1364  * Raises the specified container in the internal stack of X windows. The
1365  * next call to x_push_changes() will make the change visible in X11.
1366  *
1367  */
1369  con_state *state;
1370  state = state_for_frame(con->frame.id);
1371  //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1372 
1373  CIRCLEQ_REMOVE(&state_head, state, state);
1374  CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1375 }
1376 
1377 /*
1378  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1379  * of the given name. Used for properly tagging the windows for easily spotting
1380  * i3 windows in xwininfo -root -all.
1381  *
1382  */
1383 void x_set_name(Con *con, const char *name) {
1384  struct con_state *state;
1385 
1386  if ((state = state_for_frame(con->frame.id)) == NULL) {
1387  ELOG("window state not found\n");
1388  return;
1389  }
1390 
1391  FREE(state->name);
1392  state->name = sstrdup(name);
1393 }
1394 
1395 /*
1396  * Set up the I3_SHMLOG_PATH atom.
1397  *
1398  */
1400  if (*shmlogname == '\0') {
1401  xcb_delete_property(conn, root, A_I3_SHMLOG_PATH);
1402  } else {
1403  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1404  A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1405  strlen(shmlogname), shmlogname);
1406  }
1407 }
1408 
1409 /*
1410  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1411  *
1412  */
1413 void x_set_i3_atoms(void) {
1414  pid_t pid = getpid();
1415  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1416  (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1418  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1419  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1422 }
1423 
1424 /*
1425  * Set warp_to coordinates. This will trigger on the next call to
1426  * x_push_changes().
1427  *
1428  */
1431  warp_to = rect;
1432 }
1433 
1434 /*
1435  * Applies the given mask to the event mask of every i3 window decoration X11
1436  * window. This is useful to disable EnterNotify while resizing so that focus
1437  * is untouched.
1438  *
1439  */
1440 void x_mask_event_mask(uint32_t mask) {
1441  uint32_t values[] = {FRAME_EVENT_MASK & mask};
1442 
1443  con_state *state;
1444  CIRCLEQ_FOREACH_REVERSE (state, &state_head, state) {
1445  if (state->mapped)
1446  xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1447  }
1448 }
1449 
1450 /*
1451  * Enables or disables nonrectangular shape of the container frame.
1452  */
1453 void x_set_shape(Con *con, xcb_shape_sk_t kind, bool enable) {
1454  struct con_state *state;
1455  if ((state = state_for_frame(con->frame.id)) == NULL) {
1456  ELOG("window state for con %p not found\n", con);
1457  return;
1458  }
1459 
1460  switch (kind) {
1461  case XCB_SHAPE_SK_BOUNDING:
1462  con->window->shaped = enable;
1463  break;
1464  case XCB_SHAPE_SK_INPUT:
1465  con->window->input_shaped = enable;
1466  break;
1467  default:
1468  ELOG("Received unknown shape event kind for con %p. This is a bug.\n",
1469  con);
1470  return;
1471  }
1472 
1473  if (con_is_floating(con)) {
1474  if (enable) {
1475  x_shape_frame(con, kind);
1476  } else {
1477  x_unshape_frame(con, kind);
1478  }
1479 
1480  xcb_flush(conn);
1481  }
1482 }
#define y(x,...)
Definition: commands.c:18
static cmdp_state state
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:595
bool con_has_managed_window(Con *con)
Returns true when this con is a leaf node with a managed X11 window (e.g., excluding dock containers)
Definition: con.c:368
bool con_is_hidden(Con *con)
This will only return true for containers which have some parent with a tabbed / stacked parent of wh...
Definition: con.c:403
Rect con_border_style_rect(Con *con)
Returns a "relative" Rect which contains the amount of pixels that need to be added to the original R...
Definition: con.c:1656
i3String * con_parse_title_format(Con *con)
Returns the window title considering the current title format.
Definition: con.c:2295
int con_border_style(Con *con)
Use this function to get a container’s border style.
Definition: con.c:1736
bool con_inside_focused(Con *con)
Checks if the given container is inside a focused container.
Definition: con.c:637
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:360
adjacent_t con_adjacent_borders(Con *con)
Returns adjacent borders of the window.
Definition: con.c:1707
char * con_get_tree_representation(Con *con)
Create a string representing the subtree under con.
Definition: con.c:2232
Config config
Definition: config.c:17
char * current_configpath
Definition: config.c:15
void ewmh_update_active_window(xcb_window_t window)
Updates _NET_ACTIVE_WINDOW with the currently focused window.
Definition: ewmh.c:207
void ewmh_update_client_list(xcb_window_t *list, int num_windows)
Updates the _NET_CLIENT_LIST hint.
Definition: ewmh.c:247
void ewmh_update_focused(xcb_window_t window, bool is_focused)
Set or remove _NEW_WM_STATE_FOCUSED on the window.
Definition: ewmh.c:293
xcb_window_t ewmh_window
The EWMH support window that is used to indicate that an EWMH-compliant window manager is present.
Definition: ewmh.c:14
void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows)
Updates the _NET_CLIENT_LIST_STACKING hint.
Definition: ewmh.c:263
char * current_socketpath
Definition: ipc.c:26
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container,...
Definition: ipc.c:1630
struct pending_marks * marks
static Con * to_focus
Definition: load_layout.c:22
char * shmlogname
Definition: log.c:43
xcb_timestamp_t last_timestamp
The last timestamp we got from X11 (timestamps are included in some events and are used for some thin...
Definition: main.c:61
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:51
xcb_colormap_t colormap
Definition: main.c:71
uint8_t root_depth
Definition: main.c:69
xcb_window_t root
Definition: main.c:64
xcb_screen_t * root_screen
Definition: main.c:63
bool shape_supported
Definition: main.c:99
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Definition: randr.c:116
struct Con * focused
Definition: tree.c:13
bool rect_equals(Rect a, Rect b)
Definition: util.c:59
int max(int a, int b)
Definition: util.c:28
static Rect * warp_to
Definition: x.c:28
static void change_ewmh_focus(xcb_window_t new_focus, xcb_window_t old_focus)
Definition: x.c:106
static void x_shape_frame(Con *con, xcb_shape_sk_t shape_kind)
Definition: x.c:772
void x_con_init(Con *con)
Initializes the X11 part for the given container.
Definition: x.c:127
static size_t x_get_border_rectangles(Con *con, xcb_rectangle_t rectangles[4])
Definition: x.c:409
static void x_push_node_unmaps(Con *con)
Definition: x.c:1085
void x_move_win(Con *src, Con *dest)
Moves a child window from Container src to Container dest.
Definition: x.c:232
struct con_state con_state
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:718
xcb_window_t focused_id
Stores the X11 window ID of the currently focused window.
Definition: x.c:20
static void x_draw_title_border(Con *con, struct deco_render_params *p)
Definition: x.c:358
static void set_hidden_state(Con *con)
Definition: x.c:747
void update_shmlog_atom(void)
Set up the SHMLOG_PATH atom.
Definition: x.c:1399
void x_reparent_child(Con *con, Con *old)
Reparents the child window of the given container (necessary for sticky containers).
Definition: x.c:217
static void x_draw_decoration_after_title(Con *con, struct deco_render_params *p)
Definition: x.c:380
static xcb_window_t last_focused
Definition: x.c:25
void x_con_reframe(Con *con)
Definition: x.c:294
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition: x.c:1429
void x_reinit(Con *con)
Re-initializes the associated X window state for this container.
Definition: x.c:197
void x_window_kill(xcb_window_t window, kill_window_t kill_window)
Kills the given X11 window using WM_DELETE_WINDOW (if supported).
Definition: x.c:326
void x_raise_con(Con *con)
Raises the specified container in the internal stack of X windows.
Definition: x.c:1368
static void set_shape_state(Con *con, bool need_reshape)
Definition: x.c:801
static void _x_con_kill(Con *con)
Definition: x.c:254
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name.
Definition: x.c:1383
CIRCLEQ_HEAD(state_head, con_state)
Definition: x.c:71
#define MAX(x, y)
Definition: x.c:16
void x_draw_decoration(Con *con)
Draws the decoration of the given container onto its parent.
Definition: x.c:459
void x_push_node(Con *con)
This function pushes the properties of each node of the layout tree to X11 if they have changed (like...
Definition: x.c:839
void x_set_i3_atoms(void)
Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
Definition: x.c:1413
static void x_unshape_frame(Con *con, xcb_shape_sk_t shape_kind)
Definition: x.c:792
void x_mask_event_mask(uint32_t mask)
Applies the given mask to the event mask of every i3 window decoration X11 window.
Definition: x.c:1440
void x_push_changes(Con *con)
Pushes all changes (state of each node, see x_push_node() and the window stack) to X11.
Definition: x.c:1155
static bool is_con_attached(Con *con)
Definition: x.c:1131
void x_con_kill(Con *con)
Kills the window decoration associated with the given container.
Definition: x.c:285
bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom)
Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
Definition: x.c:303
void x_set_shape(Con *con, xcb_shape_sk_t kind, bool enable)
Enables or disables nonrectangular shape of the container frame.
Definition: x.c:1453
xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t depth, xcb_visualid_t visual, uint16_t window_class, enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values)
Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking...
Definition: xcb.c:19
xcb_visualid_t get_visualid_by_depth(uint16_t depth)
Get visualid with specified depth.
Definition: xcb.c:212
xcb_visualtype_t * get_visualtype_by_id(xcb_visualid_t visual_id)
Get visual type specified by visualid.
Definition: xcb.c:191
void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r)
Configures the given window to have the size/position specified by given rect.
Definition: xcb.c:105
void send_take_focus(xcb_window_t window, xcb_timestamp_t timestamp)
Sends the WM_TAKE_FOCUS ClientMessage to the given window.
Definition: xcb.c:82
void xcb_add_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Add an atom to a list of atoms the given property defines.
Definition: xcb.c:235
void xcb_remove_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Remove an atom from a list of atoms the given property defines without removing any other potentially...
Definition: xcb.c:245
void fake_absolute_configure_notify(Con *con)
Generates a configure_notify_event with absolute coordinates (relative to the X root window,...
Definition: xcb.c:63
@ POINTER_WARPING_NONE
Definition: data.h:132
@ L_STACKED
Definition: data.h:92
@ L_TABBED
Definition: data.h:93
@ L_SPLITH
Definition: data.h:97
@ L_SPLITV
Definition: data.h:96
adjacent_t
describes if the window is adjacent to the output (physical screen) edges.
Definition: data.h:72
@ ADJ_LEFT_SCREEN_EDGE
Definition: data.h:73
@ ADJ_LOWER_SCREEN_EDGE
Definition: data.h:76
@ ADJ_RIGHT_SCREEN_EDGE
Definition: data.h:74
@ ADJ_UPPER_SCREEN_EDGE
Definition: data.h:75
@ BS_NONE
Definition: data.h:62
@ BS_PIXEL
Definition: data.h:63
@ BS_NORMAL
Definition: data.h:61
kill_window_t
parameter to specify whether tree_close_internal() and x_window_kill() should kill only this specific...
Definition: data.h:67
@ KILL_WINDOW
Definition: data.h:68
void draw_util_surface_init(xcb_connection_t *conn, surface_t *surface, xcb_drawable_t drawable, xcb_visualtype_t *visual, int width, int height)
Initialize the surface to represent the given drawable.
struct _i3String i3String
Opaque data structure for storing strings.
Definition: libi3.h:48
void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y, double dest_x, double dest_y, double width, double height)
Copies a surface onto another surface.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width)
Draw the given text using libi3.
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define DLOG(fmt,...)
Definition: libi3.h:104
void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface)
Destroys the surface.
#define LOG(fmt,...)
Definition: libi3.h:94
#define ELOG(fmt,...)
Definition: libi3.h:99
#define COLOR_TRANSPARENT
Definition: libi3.h:429
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
#define I3STRING_FREE(str)
Securely i3string_free by setting the pointer to NULL to prevent accidentally using freed memory.
Definition: libi3.h:242
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
void draw_util_surface_set_size(surface_t *surface, int width, int height)
Resize the surface to the given size.
void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h)
Draws a filled rectangle.
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
void draw_util_clear_surface(surface_t *surface, color_t color)
Clears a surface with the given color.
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define CIRCLEQ_FOREACH_REVERSE(var, head, field)
Definition: queue.h:476
#define CIRCLEQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:512
#define TAILQ_HEAD(name, type)
Definition: queue.h:318
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
#define CIRCLEQ_HEAD_INITIALIZER(head)
Definition: queue.h:448
#define TAILQ_FIRST(head)
Definition: queue.h:336
#define CIRCLEQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:523
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
#define CIRCLEQ_ENTRY(type)
Definition: queue.h:454
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_EMPTY(head)
Definition: queue.h:344
#define CIRCLEQ_END(head)
Definition: queue.h:465
#define CIRCLEQ_PREV(elm, field)
Definition: queue.h:467
#define CIRCLEQ_REMOVE(head, elm, field)
Definition: queue.h:534
#define CIRCLEQ_FOREACH(var, head, field)
Definition: queue.h:471
#define TAILQ_ENTRY(type)
Definition: queue.h:327
#define FREE(pointer)
Definition: util.h:47
#define CHILD_EVENT_MASK
The XCB_CW_EVENT_MASK for the child (= real window)
Definition: xcb.h:28
#define ROOT_EVENT_MASK
Definition: xcb.h:42
#define FRAME_EVENT_MASK
The XCB_CW_EVENT_MASK for its frame.
Definition: xcb.h:33
@ XCURSOR_CURSOR_POINTER
Definition: xcursor.h:17
Definition: x.c:38
xcb_window_t old_frame
Definition: x.c:52
bool need_reparent
Definition: x.c:51
Con * con
Definition: x.c:46
bool was_floating
Definition: x.c:57
xcb_window_t id
Definition: x.c:39
Rect rect
Definition: x.c:59
bool is_hidden
Definition: x.c:43
char * name
Definition: x.c:64
Rect window_rect
Definition: x.c:60
bool child_mapped
Definition: x.c:42
bool initial
Definition: x.c:62
bool mapped
Definition: x.c:40
bool unmap_now
Definition: x.c:41
color_t border
Definition: configuration.h:52
color_t child_border
Definition: configuration.h:56
color_t indicator
Definition: configuration.h:55
color_t background
Definition: configuration.h:53
color_t text
Definition: configuration.h:54
enum Config::@7 title_align
Title alignment options.
i3Font font
Definition: configuration.h:93
hide_edge_borders_mode_t hide_edge_borders
Remove borders if they are adjacent to the screen edge.
struct Config::config_client client
warping_t mouse_warping
By default, when switching focus to a window on a different output (e.g.
bool show_marks
Specifies whether or not marks should be displayed in the window decoration.
struct Colortriple focused
struct Colortriple unfocused
struct Colortriple urgent
struct Colortriple focused_inactive
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:155
uint32_t height
Definition: data.h:159
uint32_t x
Definition: data.h:156
uint32_t y
Definition: data.h:157
uint32_t width
Definition: data.h:158
Stores a width/height pair, used as part of deco_render_params to check whether the rects width/heigh...
Definition: data.h:179
uint32_t w
Definition: data.h:180
Stores the parameters for rendering a window decoration.
Definition: data.h:190
int border_style
Definition: data.h:192
struct Colortriple * color
Definition: data.h:191
bool con_is_leaf
Definition: data.h:198
color_t background
Definition: data.h:196
layout_t parent_layout
Definition: data.h:197
struct width_height con_rect
Definition: data.h:193
Rect con_deco_rect
Definition: data.h:195
struct width_height con_window_rect
Definition: data.h:194
An Output is a physical output on your graphics driver.
Definition: data.h:360
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:393
bool input_shaped
The window has a nonrectangular input shape.
Definition: data.h:474
i3String * name
The name of the window.
Definition: data.h:410
bool name_x_changed
Flag to force re-rendering the decoration upon changes.
Definition: data.h:418
xcb_window_t id
Definition: data.h:394
bool doesnt_accept_focus
Whether this window accepts focus.
Definition: data.h:428
bool shaped
The window has a nonrectangular shape.
Definition: data.h:472
bool needs_take_focus
Whether the application needs to receive WM_TAKE_FOCUS.
Definition: data.h:424
uint16_t depth
Depth of the window.
Definition: data.h:448
Definition: data.h:594
char * name
Definition: data.h:595
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:604
struct Con * parent
Definition: data.h:636
struct Rect deco_rect
Definition: data.h:646
int border_width
Definition: data.h:668
struct Rect rect
Definition: data.h:640
enum Con::@20 type
xcb_colormap_t colormap
Definition: data.h:751
bool pixmap_recreated
Definition: data.h:621
layout_t layout
Definition: data.h:708
bool mapped
Definition: data.h:605
uint8_t ignore_unmap
This counter contains the number of UnmapNotify events for this container (or, more precisely,...
Definition: data.h:616
struct Rect window_rect
Definition: data.h:643
struct Window * window
Definition: data.h:671
char * title_format
The format with which the window's name should be displayed.
Definition: data.h:653
surface_t frame
Definition: data.h:619
border_style_t border_style
Definition: data.h:709
char * name
Definition: data.h:650
uint16_t depth
Definition: data.h:748
surface_t frame_buffer
Definition: data.h:620
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:677
bool mark_changed
Definition: data.h:663
bool urgent
Definition: data.h:609
int height
The height of the font, built from font_ascent + font_descent.
Definition: libi3.h:67
xcb_gcontext_t gc
Definition: libi3.h:572
xcb_drawable_t id
Definition: libi3.h:569