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