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