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