i3
workspace.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  * workspace.c: Modifying workspaces, accessing them, moving containers to
8  * workspaces.
9  *
10  */
11 #include "all.h"
12 #include "yajl_utils.h"
13 
14 /* Stores a copy of the name of the last used workspace for the workspace
15  * back-and-forth switching. */
16 static char *previous_workspace_name = NULL;
17 
18 /* NULL-terminated list of workspace names (in order) extracted from
19  * keybindings. */
20 static char **binding_workspace_names = NULL;
21 
22 /*
23  * Sets ws->layout to splith/splitv if default_orientation was specified in the
24  * configfile. Otherwise, it uses splith/splitv depending on whether the output
25  * is higher than wide.
26  *
27  */
29  /* If default_orientation is set to NO_ORIENTATION we determine
30  * orientation depending on output resolution. */
32  Con *output = con_get_output(ws);
33  ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
34  ws->rect = output->rect;
35  DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
36  output->rect.width, output->rect.height, ws->layout);
37  } else {
39  }
40 }
41 
42 /*
43  * Returns a pointer to the workspace with the given number (starting at 0),
44  * creating the workspace if necessary (by allocating the necessary amount of
45  * memory and initializing the data structures correctly).
46  *
47  */
48 Con *workspace_get(const char *num, bool *created) {
49  Con *output, *workspace = NULL;
50 
51  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
52  GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
53 
54  if (workspace == NULL) {
55  LOG("Creating new workspace \"%s\"\n", num);
56  /* unless an assignment is found, we will create this workspace on the current output */
57  output = con_get_output(focused);
58  /* look for assignments */
59  struct Workspace_Assignment *assignment;
60 
61  /* We set workspace->num to the number if this workspace’s name begins
62  * with a positive number. Otherwise it’s a named ws and num will be
63  * -1. */
64  long parsed_num = ws_name_to_number(num);
65 
67  if (strcmp(assignment->name, num) == 0) {
68  DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
69  GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
70  break;
71  } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
72  DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
73  GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
74  }
75  }
76 
77  Con *content = output_get_content(output);
78  LOG("got output %p with content %p\n", output, content);
79  /* We need to attach this container after setting its type. con_attach
80  * will handle CT_WORKSPACEs differently */
81  workspace = con_new(NULL, NULL);
82  char *name;
83  sasprintf(&name, "[i3 con] workspace %s", num);
84  x_set_name(workspace, name);
85  free(name);
86  workspace->type = CT_WORKSPACE;
87  FREE(workspace->name);
88  workspace->name = sstrdup(num);
90  workspace->num = parsed_num;
91  LOG("num = %d\n", workspace->num);
92 
93  workspace->parent = content;
95 
96  con_attach(workspace, content, false);
97 
98  ipc_send_workspace_event("init", workspace, NULL);
103  if (created != NULL)
104  *created = true;
105  } else if (created != NULL) {
106  *created = false;
107  }
108 
109  return workspace;
110 }
111 
112 /*
113  * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
114  * workspace web”), so that when an output needs a workspace, i3 can start with
115  * the first configured one. Needs to be called before reorder_bindings() so
116  * that the config-file order is used, not the i3-internal order.
117  *
118  */
120  Binding *bind;
121  int n = 0;
122  if (binding_workspace_names != NULL) {
123  for (int i = 0; binding_workspace_names[i] != NULL; i++) {
124  free(binding_workspace_names[i]);
125  }
127  }
129  DLOG("binding with command %s\n", bind->command);
130  if (strlen(bind->command) < strlen("workspace ") ||
131  strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
132  continue;
133  DLOG("relevant command = %s\n", bind->command);
134  const char *target = bind->command + strlen("workspace ");
135  while (*target == ' ' || *target == '\t')
136  target++;
137  /* We check if this is the workspace
138  * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
139  * Beware: The workspace names "next", "prev", "next_on_output",
140  * "prev_on_output", "number", "back_and_forth" and "current" are OK,
141  * so we check before stripping the double quotes */
142  if (strncasecmp(target, "next", strlen("next")) == 0 ||
143  strncasecmp(target, "prev", strlen("prev")) == 0 ||
144  strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
145  strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
146  strncasecmp(target, "number", strlen("number")) == 0 ||
147  strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
148  strncasecmp(target, "current", strlen("current")) == 0)
149  continue;
150  char *target_name = parse_string(&target, false);
151  if (target_name == NULL)
152  continue;
153  if (strncasecmp(target_name, "__", strlen("__")) == 0) {
154  LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
155  free(target_name);
156  continue;
157  }
158  DLOG("Saving workspace name \"%s\"\n", target_name);
159 
161  binding_workspace_names[n - 1] = target_name;
162  }
164  binding_workspace_names[n - 1] = NULL;
165 }
166 
167 /*
168  * Returns a pointer to a new workspace in the given output. The workspace
169  * is created attached to the tree hierarchy through the given content
170  * container.
171  *
172  */
174  /* add a workspace to this output */
175  Con *out, *current;
176  char *name;
177  bool exists = true;
178  Con *ws = con_new(NULL, NULL);
179  ws->type = CT_WORKSPACE;
180 
181  /* try the configured workspace bindings first to find a free name */
182  for (int n = 0; binding_workspace_names[n] != NULL; n++) {
183  char *target_name = binding_workspace_names[n];
184  /* Ensure that this workspace is not assigned to a different output —
185  * otherwise we would create it, then move it over to its output, then
186  * find a new workspace, etc… */
187  bool assigned = false;
188  struct Workspace_Assignment *assignment;
190  if (strcmp(assignment->name, target_name) != 0 ||
191  strcmp(assignment->output, output_primary_name(output)) == 0)
192  continue;
193 
194  assigned = true;
195  break;
196  }
197 
198  if (assigned)
199  continue;
200 
201  current = NULL;
202  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
203  GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
204  exists = (current != NULL);
205  if (!exists) {
206  ws->name = sstrdup(target_name);
207  /* Set ->num to the number of the workspace, if the name actually
208  * is a number or starts with a number */
209  ws->num = ws_name_to_number(ws->name);
210  LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
211 
212  break;
213  }
214  }
215 
216  if (exists) {
217  /* get the next unused workspace number */
218  DLOG("Getting next unused workspace by number\n");
219  int c = 0;
220  while (exists) {
221  c++;
222 
223  ws->num = c;
224 
225  current = NULL;
226  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
227  GREP_FIRST(current, output_get_content(out), child->num == ws->num);
228  exists = (current != NULL);
229 
230  DLOG("result for ws %d: exists = %d\n", c, exists);
231  }
232  sasprintf(&(ws->name), "%d", c);
233  }
234  con_attach(ws, content, false);
235 
236  sasprintf(&name, "[i3 con] workspace %s", ws->name);
237  x_set_name(ws, name);
238  free(name);
239 
241 
244 
245  return ws;
246 }
247 
248 /*
249  * Returns true if the workspace is currently visible. Especially important for
250  * multi-monitor environments, as they can have multiple currenlty active
251  * workspaces.
252  *
253  */
255  Con *output = con_get_output(ws);
256  if (output == NULL)
257  return false;
258  Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
259  LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
260  return (fs == ws);
261 }
262 
263 /*
264  * XXX: we need to clean up all this recursive walking code.
265  *
266  */
267 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
268  Con *current;
269 
270  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
271  if (current != exclude &&
272  current->sticky_group != NULL &&
273  current->window != NULL &&
274  strcmp(current->sticky_group, sticky_group) == 0)
275  return current;
276 
277  Con *recurse = _get_sticky(current, sticky_group, exclude);
278  if (recurse != NULL)
279  return recurse;
280  }
281 
282  TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
283  if (current != exclude &&
284  current->sticky_group != NULL &&
285  current->window != NULL &&
286  strcmp(current->sticky_group, sticky_group) == 0)
287  return current;
288 
289  Con *recurse = _get_sticky(current, sticky_group, exclude);
290  if (recurse != NULL)
291  return recurse;
292  }
293 
294  return NULL;
295 }
296 
297 /*
298  * Reassigns all child windows in sticky containers. Called when the user
299  * changes workspaces.
300  *
301  * XXX: what about sticky containers which contain containers?
302  *
303  */
304 static void workspace_reassign_sticky(Con *con) {
305  Con *current;
306  /* 1: go through all containers */
307 
308  /* handle all children and floating windows of this node */
309  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
310  if (current->sticky_group == NULL) {
311  workspace_reassign_sticky(current);
312  continue;
313  }
314 
315  LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
316  /* 2: find a window which we can re-assign */
317  Con *output = con_get_output(current);
318  Con *src = _get_sticky(output, current->sticky_group, current);
319 
320  if (src == NULL) {
321  LOG("No window found for this sticky group\n");
322  workspace_reassign_sticky(current);
323  continue;
324  }
325 
326  x_move_win(src, current);
327  current->window = src->window;
328  current->mapped = true;
329  src->window = NULL;
330  src->mapped = false;
331 
332  x_reparent_child(current, src);
333 
334  LOG("re-assigned window from src %p to dest %p\n", src, current);
335  }
336 
337  TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
338  workspace_reassign_sticky(current);
339 }
340 
341 /*
342  * Callback to reset the urgent flag of the given con to false. May be started by
343  * _workspace_show to avoid urgency hints being lost by switching to a workspace
344  * focusing the con.
345  *
346  */
347 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
348  Con *con = w->data;
349 
350  ev_timer_stop(main_loop, con->urgency_timer);
351  FREE(con->urgency_timer);
352 
353  if (con->urgent) {
354  DLOG("Resetting urgency flag of con %p by timer\n", con);
355  con_set_urgency(con, false);
358  ipc_send_window_event("urgent", con);
359  tree_render();
360  }
361 }
362 
363 static void _workspace_show(Con *workspace) {
364  Con *current, *old = NULL;
365  Con *old_focus = focused;
366 
367  /* safe-guard against showing i3-internal workspaces like __i3_scratch */
368  if (con_is_internal(workspace))
369  return;
370 
371  /* disable fullscreen for the other workspaces and get the workspace we are
372  * currently on. */
373  TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
374  if (current->fullscreen_mode == CF_OUTPUT)
375  old = current;
376  current->fullscreen_mode = CF_NONE;
377  }
378 
379  /* enable fullscreen for the target workspace. If it happens to be the
380  * same one we are currently on anyways, we can stop here. */
381  workspace->fullscreen_mode = CF_OUTPUT;
382  current = con_get_workspace(focused);
383  if (workspace == current) {
384  DLOG("Not switching, already there.\n");
385  return;
386  }
387 
388  /* Remember currently focused workspace for switching back to it later with
389  * the 'workspace back_and_forth' command.
390  * NOTE: We have to duplicate the name as the original will be freed when
391  * the corresponding workspace is cleaned up.
392  * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
393  * focused) are skipped, see bug #868. */
394  if (current && !con_is_internal(current)) {
396  if (current) {
398  DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
399  }
400  }
401 
402  workspace_reassign_sticky(workspace);
403 
404  DLOG("switching to %p / %s\n", workspace, workspace->name);
405  Con *next = con_descend_focused(workspace);
406 
407  /* Memorize current output */
408  Con *old_output = con_get_output(focused);
409 
410  /* Display urgency hint for a while if the newly visible workspace would
411  * focus and thereby immediately destroy it */
412  if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
413  /* focus for now… */
414  next->urgent = false;
415  con_focus(next);
416 
417  /* … but immediately reset urgency flags; they will be set to false by
418  * the timer callback in case the container is focused at the time of
419  * its expiration */
420  focused->urgent = true;
421  workspace->urgent = true;
422 
423  if (focused->urgency_timer == NULL) {
424  DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
425  focused, workspace);
426  focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
427  /* use a repeating timer to allow for easy resets */
430  focused->urgency_timer->data = focused;
431  ev_timer_start(main_loop, focused->urgency_timer);
432  } else {
433  DLOG("Resetting urgency timer of con %p on workspace %p\n",
434  focused, workspace);
435  ev_timer_again(main_loop, focused->urgency_timer);
436  }
437  } else
438  con_focus(next);
439 
440  ipc_send_workspace_event("focus", workspace, current);
441 
442  DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
443  /* Close old workspace if necessary. This must be done *after* doing
444  * urgency handling, because tree_close_internal() will do a con_focus() on the next
445  * client, which will clear the urgency flag too early. Also, there is no
446  * way for con_focus() to know about when to clear urgency immediately and
447  * when to defer it. */
448  if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
449  /* check if this workspace is currently visible */
450  if (!workspace_is_visible(old)) {
451  LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
452  yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
453  tree_close_internal(old, DONT_KILL_WINDOW, false, false);
454 
455  const unsigned char *payload;
456  ylength length;
457  y(get_buf, &payload, &length);
458  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
459 
460  y(free);
461 
462  /* Avoid calling output_push_sticky_windows later with a freed container. */
463  if (old == old_focus) {
464  old_focus = NULL;
465  }
466 
471  }
472  }
473 
474  workspace->fullscreen_mode = CF_OUTPUT;
475  LOG("focused now = %p / %s\n", focused, focused->name);
476 
477  /* Set mouse pointer */
478  Con *new_output = con_get_output(focused);
479  if (old_output != new_output) {
480  x_set_warp_to(&next->rect);
481  }
482 
483  /* Update the EWMH hints */
485 
486  /* Push any sticky windows to the now visible workspace. */
487  output_push_sticky_windows(old_focus);
488 }
489 
490 /*
491  * Switches to the given workspace
492  *
493  */
494 void workspace_show(Con *workspace) {
495  _workspace_show(workspace);
496 }
497 
498 /*
499  * Looks up the workspace by name and switches to it.
500  *
501  */
502 void workspace_show_by_name(const char *num) {
503  Con *workspace;
504  workspace = workspace_get(num, NULL);
505  _workspace_show(workspace);
506 }
507 
508 /*
509  * Focuses the next workspace.
510  *
511  */
513  Con *current = con_get_workspace(focused);
514  Con *next = NULL, *first = NULL, *first_opposite = NULL;
515  Con *output;
516 
517  if (current->num == -1) {
518  /* If currently a named workspace, find next named workspace. */
519  if ((next = TAILQ_NEXT(current, nodes)) != NULL)
520  return next;
521  bool found_current = false;
522  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
523  /* Skip outputs starting with __, they are internal. */
524  if (con_is_internal(output))
525  continue;
527  if (child->type != CT_WORKSPACE)
528  continue;
529  if (!first)
530  first = child;
531  if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
532  first_opposite = child;
533  if (child == current) {
534  found_current = true;
535  } else if (child->num == -1 && found_current) {
536  next = child;
537  return next;
538  }
539  }
540  }
541  } else {
542  /* If currently a numbered workspace, find next numbered workspace. */
543  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
544  /* Skip outputs starting with __, they are internal. */
545  if (con_is_internal(output))
546  continue;
548  if (child->type != CT_WORKSPACE)
549  continue;
550  if (!first || (child->num != -1 && child->num < first->num))
551  first = child;
552  if (!first_opposite && child->num == -1)
553  first_opposite = child;
554  if (child->num == -1)
555  break;
556  /* Need to check child against current and next because we are
557  * traversing multiple lists and thus are not guaranteed the
558  * relative order between the list of workspaces. */
559  if (current->num < child->num && (!next || child->num < next->num))
560  next = child;
561  }
562  }
563  }
564 
565  if (!next)
566  next = first_opposite ? first_opposite : first;
567 
568  return next;
569 }
570 
571 /*
572  * Focuses the previous workspace.
573  *
574  */
576  Con *current = con_get_workspace(focused);
577  Con *prev = NULL, *first_opposite = NULL, *last = NULL;
578  Con *output;
579 
580  if (current->num == -1) {
581  /* If named workspace, find previous named workspace. */
582  prev = TAILQ_PREV(current, nodes_head, nodes);
583  if (prev && prev->num != -1)
584  prev = NULL;
585  if (!prev) {
586  bool found_current = false;
587  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
588  /* Skip outputs starting with __, they are internal. */
589  if (con_is_internal(output))
590  continue;
592  if (child->type != CT_WORKSPACE)
593  continue;
594  if (!last)
595  last = child;
596  if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
597  first_opposite = child;
598  if (child == current) {
599  found_current = true;
600  } else if (child->num == -1 && found_current) {
601  prev = child;
602  return prev;
603  }
604  }
605  }
606  }
607  } else {
608  /* If numbered workspace, find previous numbered workspace. */
609  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
610  /* Skip outputs starting with __, they are internal. */
611  if (con_is_internal(output))
612  continue;
614  if (child->type != CT_WORKSPACE)
615  continue;
616  if (!last || (child->num != -1 && last->num < child->num))
617  last = child;
618  if (!first_opposite && child->num == -1)
619  first_opposite = child;
620  if (child->num == -1)
621  continue;
622  /* Need to check child against current and previous because we
623  * are traversing multiple lists and thus are not guaranteed
624  * the relative order between the list of workspaces. */
625  if (current->num > child->num && (!prev || child->num > prev->num))
626  prev = child;
627  }
628  }
629  }
630 
631  if (!prev)
632  prev = first_opposite ? first_opposite : last;
633 
634  return prev;
635 }
636 
637 /*
638  * Focuses the next workspace on the same output.
639  *
640  */
642  Con *current = con_get_workspace(focused);
643  Con *next = NULL;
645 
646  if (current->num == -1) {
647  /* If currently a named workspace, find next named workspace. */
648  next = TAILQ_NEXT(current, nodes);
649  } else {
650  /* If currently a numbered workspace, find next numbered workspace. */
652  if (child->type != CT_WORKSPACE)
653  continue;
654  if (child->num == -1)
655  break;
656  /* Need to check child against current and next because we are
657  * traversing multiple lists and thus are not guaranteed the
658  * relative order between the list of workspaces. */
659  if (current->num < child->num && (!next || child->num < next->num))
660  next = child;
661  }
662  }
663 
664  /* Find next named workspace. */
665  if (!next) {
666  bool found_current = false;
668  if (child->type != CT_WORKSPACE)
669  continue;
670  if (child == current) {
671  found_current = true;
672  } else if (child->num == -1 && (current->num != -1 || found_current)) {
673  next = child;
674  goto workspace_next_on_output_end;
675  }
676  }
677  }
678 
679  /* Find first workspace. */
680  if (!next) {
682  if (child->type != CT_WORKSPACE)
683  continue;
684  if (!next || (child->num != -1 && child->num < next->num))
685  next = child;
686  }
687  }
688 workspace_next_on_output_end:
689  return next;
690 }
691 
692 /*
693  * Focuses the previous workspace on same output.
694  *
695  */
697  Con *current = con_get_workspace(focused);
698  Con *prev = NULL;
700  DLOG("output = %s\n", output->name);
701 
702  if (current->num == -1) {
703  /* If named workspace, find previous named workspace. */
704  prev = TAILQ_PREV(current, nodes_head, nodes);
705  if (prev && prev->num != -1)
706  prev = NULL;
707  } else {
708  /* If numbered workspace, find previous numbered workspace. */
710  if (child->type != CT_WORKSPACE || child->num == -1)
711  continue;
712  /* Need to check child against current and previous because we
713  * are traversing multiple lists and thus are not guaranteed
714  * the relative order between the list of workspaces. */
715  if (current->num > child->num && (!prev || child->num > prev->num))
716  prev = child;
717  }
718  }
719 
720  /* Find previous named workspace. */
721  if (!prev) {
722  bool found_current = false;
724  if (child->type != CT_WORKSPACE)
725  continue;
726  if (child == current) {
727  found_current = true;
728  } else if (child->num == -1 && (current->num != -1 || found_current)) {
729  prev = child;
730  goto workspace_prev_on_output_end;
731  }
732  }
733  }
734 
735  /* Find last workspace. */
736  if (!prev) {
738  if (child->type != CT_WORKSPACE)
739  continue;
740  if (!prev || child->num > prev->num)
741  prev = child;
742  }
743  }
744 
745 workspace_prev_on_output_end:
746  return prev;
747 }
748 
749 /*
750  * Focuses the previously focused workspace.
751  *
752  */
755  DLOG("No previous workspace name set. Not switching.\n");
756  return;
757  }
758 
760 }
761 
762 /*
763  * Returns the previously focused workspace con, or NULL if unavailable.
764  *
765  */
768  DLOG("No previous workspace name set.\n");
769  return NULL;
770  }
771 
772  Con *workspace;
773  workspace = workspace_get(previous_workspace_name, NULL);
774 
775  return workspace;
776 }
777 
778 static bool get_urgency_flag(Con *con) {
779  Con *child;
780  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
781  if (child->urgent || get_urgency_flag(child))
782  return true;
783 
784  TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
785  if (child->urgent || get_urgency_flag(child))
786  return true;
787 
788  return false;
789 }
790 
791 /*
792  * Goes through all clients on the given workspace and updates the workspace’s
793  * urgent flag accordingly.
794  *
795  */
797  bool old_flag = ws->urgent;
798  ws->urgent = get_urgency_flag(ws);
799  DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
800 
801  if (old_flag != ws->urgent)
802  ipc_send_workspace_event("urgent", ws, NULL);
803 }
804 
805 /*
806  * 'Forces' workspace orientation by moving all cons into a new split-con with
807  * the same layout as the workspace and then changing the workspace layout.
808  *
809  */
810 void ws_force_orientation(Con *ws, orientation_t orientation) {
811  /* 1: create a new split container */
812  Con *split = con_new(NULL, NULL);
813  split->parent = ws;
814 
815  /* 2: copy layout from workspace */
816  split->layout = ws->layout;
817 
818  /* 3: move the existing cons of this workspace below the new con */
819  Con **focus_order = get_focus_order(ws);
820 
821  DLOG("Moving cons\n");
822  while (!TAILQ_EMPTY(&(ws->nodes_head))) {
823  Con *child = TAILQ_FIRST(&(ws->nodes_head));
824  con_detach(child);
825  con_attach(child, split, true);
826  }
827 
828  set_focus_order(split, focus_order);
829  free(focus_order);
830 
831  /* 4: switch workspace layout */
832  ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
833  DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
834 
835  /* 5: attach the new split container to the workspace */
836  DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
837  con_attach(split, ws, false);
838 
839  /* 6: fix the percentages */
840  con_fix_percent(ws);
841 }
842 
843 /*
844  * Called when a new con (with a window, not an empty or split con) should be
845  * attached to the workspace (for example when managing a new window or when
846  * moving an existing window to the workspace level).
847  *
848  * Depending on the workspace_layout setting, this function either returns the
849  * workspace itself (default layout) or creates a new stacked/tabbed con and
850  * returns that.
851  *
852  */
854  DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
855 
856  if (ws->workspace_layout == L_DEFAULT) {
857  DLOG("Default layout, just attaching it to the workspace itself.\n");
858  return ws;
859  }
860 
861  DLOG("Non-default layout, creating a new split container\n");
862  /* 1: create a new split container */
863  Con *new = con_new(NULL, NULL);
864  new->parent = ws;
865 
866  /* 2: set the requested layout on the split con */
867  new->layout = ws->workspace_layout;
868 
869  /* 4: attach the new split container to the workspace */
870  DLOG("Attaching new split %p to workspace %p\n", new, ws);
871  con_attach(new, ws, false);
872 
873  /* 5: fix the percentages */
874  con_fix_percent(ws);
875 
876  return new;
877 }
878 
886  if (TAILQ_EMPTY(&(ws->nodes_head))) {
887  ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
888  return NULL;
889  }
890 
891  Con *new = con_new(NULL, NULL);
892  new->parent = ws;
893  new->layout = ws->layout;
894 
895  Con **focus_order = get_focus_order(ws);
896 
897  DLOG("Moving children of workspace %p / %s into container %p\n",
898  ws, ws->name, new);
899  Con *child;
900  while (!TAILQ_EMPTY(&(ws->nodes_head))) {
901  child = TAILQ_FIRST(&(ws->nodes_head));
902  con_detach(child);
903  con_attach(child, new, true);
904  }
905 
906  set_focus_order(new, focus_order);
907  free(focus_order);
908 
909  con_attach(new, ws, true);
910 
911  return new;
912 }
913 
918 bool workspace_move_to_output(Con *ws, const char *name) {
919  LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
920 
921  Output *current_output = get_output_for_con(ws);
922  if (current_output == NULL) {
923  ELOG("Cannot get current output. This is a bug in i3.\n");
924  return false;
925  }
926 
927  Output *output = get_output_from_string(current_output, name);
928  if (!output) {
929  ELOG("Could not get output from string \"%s\"\n", name);
930  return false;
931  }
932 
933  Con *content = output_get_content(output->con);
934  LOG("got output %p with content %p\n", output, content);
935 
936  Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
937  LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
938 
939  bool workspace_was_visible = workspace_is_visible(ws);
940  if (con_num_children(ws->parent) == 1) {
941  LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
942 
943  /* check if we can find a workspace assigned to this output */
944  bool used_assignment = false;
945  struct Workspace_Assignment *assignment;
947  if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
948  continue;
949 
950  /* check if this workspace is already attached to the tree */
951  Con *workspace = NULL, *out;
952  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
953  GREP_FIRST(workspace, output_get_content(out),
954  !strcasecmp(child->name, assignment->name));
955  if (workspace != NULL)
956  continue;
957 
958  /* so create the workspace referenced to by this assignment */
959  LOG("Creating workspace from assignment %s.\n", assignment->name);
960  workspace_get(assignment->name, NULL);
961  used_assignment = true;
962  break;
963  }
964 
965  /* if we couldn't create the workspace using an assignment, create
966  * it on the output */
967  if (!used_assignment)
968  create_workspace_on_output(current_output, ws->parent);
969 
970  /* notify the IPC listeners */
971  ipc_send_workspace_event("init", ws, NULL);
972  }
973  DLOG("Detaching\n");
974 
975  /* detach from the old output and attach to the new output */
976  Con *old_content = ws->parent;
977  con_detach(ws);
978  if (workspace_was_visible) {
979  /* The workspace which we just detached was visible, so focus
980  * the next one in the focus-stack. */
981  Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
982  LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
983  workspace_show(focus_ws);
984  }
985  con_attach(ws, content, false);
986 
987  /* fix the coordinates of the floating containers */
988  Con *floating_con;
989  TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
990  floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
991 
992  ipc_send_workspace_event("move", ws, NULL);
993  if (workspace_was_visible) {
994  /* Focus the moved workspace on the destination output. */
995  workspace_show(ws);
996  }
997 
998  /* NB: We cannot simply work with previously_visible_ws since it might
999  * have been cleaned up by workspace_show() already, depending on the
1000  * focus order/number of other workspaces on the output.
1001  * Instead, we loop through the available workspaces and only work with
1002  * previously_visible_ws if we still find it. */
1003  TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1004  if (ws != previously_visible_ws)
1005  continue;
1006 
1007  /* Call the on_remove_child callback of the workspace which previously
1008  * was visible on the destination output. Since it is no longer
1009  * visible, it might need to get cleaned up. */
1010  CALL(previously_visible_ws, on_remove_child);
1011  break;
1012  }
1013 
1014  return true;
1015 }
static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents)
Definition: workspace.c:347
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:931
void set_focus_order(Con *con, Con **focus_order)
Clear the container&#39;s focus stack and re-add it using the provided container array.
Definition: con.c:848
Con * _get_sticky(Con *con, const char *sticky_group, Con *exclude)
Definition: workspace.c:267
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition: con.c:207
#define NODES_FOREACH(head)
Definition: util.h:32
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...
Con * focused
Definition: tree.c:13
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
void workspace_back_and_forth(void)
Focuses the previously focused workspace.
Definition: workspace.c:753
Con * workspace_encapsulate(Con *ws)
Creates a new container and re-parents all of children from the given workspace into it...
Definition: workspace.c:885
char * sticky_group
Definition: data.h:657
Output * get_output_for_con(Con *con)
Returns the output for the given con.
Definition: output.c:55
#define CALL(obj, member,...)
Definition: util.h:56
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition: ewmh.c:182
#define TAILQ_EMPTY(head)
Definition: queue.h:344
#define ELOG(fmt,...)
Definition: libi3.h:99
layout_t default_layout
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:603
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
#define LOG(fmt,...)
Definition: libi3.h:94
bool workspace_move_to_output(Con *ws, const char *name)
Move the given workspace to the specified output.
Definition: workspace.c:918
char * command
Command, like in command mode.
Definition: data.h:334
struct Rect rect
Definition: data.h:639
bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus)
Closes the given container including all children.
Definition: tree.c:201
Output * get_output_from_string(Output *current_output, const char *output_str)
Returns an &#39;output&#39; corresponding to one of left/right/down/up or a specific output name...
Definition: output.c:31
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:494
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:223
struct Window * window
Definition: data.h:671
bool urgent
Definition: data.h:608
Con ** get_focus_order(Con *con)
Iterate over the container&#39;s focus stack and return an array with the containers inside it...
Definition: con.c:828
struct bindings_head * bindings
Definition: main.c:76
void ewmh_update_number_of_desktops(void)
Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of noninternal workspaces.
Definition: ewmh.c:32
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:1391
enum Con::@20 type
static void _workspace_show(Con *workspace)
Definition: workspace.c:363
void ewmh_update_current_desktop(void)
Updates _NET_CURRENT_DESKTOP with the current desktop number.
Definition: ewmh.c:21
#define FREE(pointer)
Definition: util.h:50
Definition: data.h:60
size_t ylength
Definition: yajl_utils.h:24
static void _workspace_apply_default_orientation(Con *ws)
Definition: workspace.c:28
nodes_head
Definition: data.h:684
int default_orientation
Default orientation for new containers.
void ewmh_update_desktop_viewport(void)
Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that define the top left corne...
Definition: ewmh.c:91
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:1213
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
Definition: floating.c:922
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:419
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:405
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:872
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
void ws_force_orientation(Con *ws, orientation_t orientation)
&#39;Forces&#39; workspace orientation by moving all cons into a new split-con with the same orientation as t...
Definition: workspace.c:810
#define TAILQ_FIRST(head)
Definition: queue.h:336
Con * workspace_attach_to(Con *ws)
Called when a new con (with a window, not an empty or split con) should be attached to the workspace ...
Definition: workspace.c:853
floating_head
Definition: data.h:681
fullscreen_mode_t fullscreen_mode
Definition: data.h:692
char * name
Definition: data.h:649
void x_move_win(Con *src, Con *dest)
Moves a child window from Container src to Container dest.
Definition: x.c:212
layout_t layout
Definition: data.h:713
void ewmh_update_desktop_names(void)
Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops.
Definition: ewmh.c:53
void con_set_urgency(Con *con, bool urgent)
Set urgency flag to the container, all the parent containers and the workspace.
Definition: con.c:2192
bool mapped
Definition: data.h:604
void output_push_sticky_windows(Con *to_focus)
Iterates over all outputs and pushes sticky windows to the currently visible workspace on that output...
Definition: output.c:76
Con * workspace_prev_on_output(void)
Returns the previous workspace on the same output.
Definition: workspace.c:696
#define NODES_FOREACH_REVERSE(head)
Definition: util.h:36
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:516
void workspace_show_by_name(const char *num)
Looks up the workspace by name and switches to it.
Definition: workspace.c:502
Stores which workspace (by name or number) goes to which output.
Definition: data.h:207
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
static void workspace_reassign_sticky(Con *con)
Definition: workspace.c:304
Definition: data.h:92
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition: x.c:1259
Definition: data.h:97
layout_t workspace_layout
Definition: data.h:713
An Output is a physical output on your graphics driver.
Definition: data.h:375
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1549
uint32_t height
Definition: data.h:161
uint32_t width
Definition: data.h:160
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:282
Definition: data.h:588
void ipc_send_event(const char *event, uint32_t message_type, const char *payload)
Sends the specified event to all IPC clients which are currently connected and subscribed to this kin...
Definition: ipc.c:46
yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old)
Generates a json workspace event.
Definition: ipc.c:1342
static char ** binding_workspace_names
Definition: workspace.c:20
uint32_t y
Definition: data.h:128
Con * con_new(Con *parent, i3Window *window)
Definition: con.c:70
Con * workspace_next(void)
Returns the next workspace.
Definition: workspace.c:512
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
#define DLOG(fmt,...)
Definition: libi3.h:104
void extract_workspace_names_from_bindings(void)
Extracts workspace names from keybindings (e.g.
Definition: workspace.c:119
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition: con.c:468
void workspace_update_urgent_flag(Con *ws)
Goes through all clients on the given workspace and updates the workspace’s urgent flag accordingly...
Definition: workspace.c:796
Con * con
Pointer to the Con which represents this output.
Definition: data.h:396
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
Definition: util.c:102
struct ws_assignments_head ws_assignments
Definition: main.c:89
orientation_t
Definition: data.h:59
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition: data.h:633
struct ev_loop * main_loop
Definition: main.c:68
Definition: data.h:98
Con * workspace_back_and_forth_get(void)
Returns the previously focused workspace con, or NULL if unavailable.
Definition: workspace.c:766
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:199
struct ev_timer * urgency_timer
Definition: data.h:674
Config config
Definition: config.c:17
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:502
void ipc_send_workspace_event(const char *change, Con *current, Con *old)
For the workspace events we send, along with the usual "change" field, also the workspace container i...
Definition: ipc.c:1375
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition: output.c:51
#define GREP_FIRST(dest, head, condition)
Definition: util.h:41
Con * workspace_prev(void)
Returns the previous workspace.
Definition: workspace.c:575
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition: workspace.c:254
Con * workspace_get(const char *num, bool *created)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
Definition: workspace.c:48
void x_reparent_child(Con *con, Con *old)
Reparents the child window of the given container (necessary for sticky containers).
Definition: x.c:197
Con * workspace_next_on_output(void)
Returns the next workspace on the same output.
Definition: workspace.c:641
static char * previous_workspace_name
Definition: workspace.c:16
Con * create_workspace_on_output(Output *output, Con *content)
Returns a pointer to a new workspace in the given output.
Definition: workspace.c:173
struct Con * parent
Definition: data.h:635
struct Con * croot
Definition: tree.c:12
focus_head
Definition: data.h:687
void con_update_parents_urgency(Con *con)
Make all parent containers urgent if con is urgent or clear the urgent flag of all parent containers ...
Definition: con.c:2164
static bool get_urgency_flag(Con *con)
Definition: workspace.c:778
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
Definition: queue.h:352