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