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