i3
workspace.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "workspace.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * workspace.c: Modifying workspaces, accessing them, moving containers to
10  * workspaces.
11  *
12  */
13 #include "all.h"
14 #include "yajl_utils.h"
15 
16 #include <yajl/yajl_gen.h>
17 
18 /* Stores a copy of the name of the last used workspace for the workspace
19  * back-and-forth switching. */
20 static char *previous_workspace_name = 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  DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
35  output->rect.width, output->rect.height, ws->layout);
36  } else {
37  ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
38  }
39 }
40 
41 /*
42  * Returns a pointer to the workspace with the given number (starting at 0),
43  * creating the workspace if necessary (by allocating the necessary amount of
44  * memory and initializing the data structures correctly).
45  *
46  */
47 Con *workspace_get(const char *num, bool *created) {
48  Con *output, *workspace = NULL;
49 
50  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
51  GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
52 
53  if (workspace == NULL) {
54  LOG("Creating new workspace \"%s\"\n", num);
55  /* unless an assignment is found, we will create this workspace on the current output */
56  output = con_get_output(focused);
57  /* look for assignments */
58  struct Workspace_Assignment *assignment;
60  if (strcmp(assignment->name, num) != 0)
61  continue;
62 
63  LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
64  GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
65  break;
66  }
67  Con *content = output_get_content(output);
68  LOG("got output %p with content %p\n", output, content);
69  /* We need to attach this container after setting its type. con_attach
70  * will handle CT_WORKSPACEs differently */
71  workspace = con_new(NULL, NULL);
72  char *name;
73  sasprintf(&name, "[i3 con] workspace %s", num);
74  x_set_name(workspace, name);
75  free(name);
76  workspace->type = CT_WORKSPACE;
77  FREE(workspace->name);
78  workspace->name = sstrdup(num);
80  /* We set ->num to the number if this workspace’s name begins with a
81  * positive number. Otherwise it’s a named ws and num will be -1. */
82  char *endptr = NULL;
83  long parsed_num = strtol(num, &endptr, 10);
84  if (parsed_num == LONG_MIN ||
85  parsed_num == LONG_MAX ||
86  parsed_num < 0 ||
87  endptr == num)
88  workspace->num = -1;
89  else workspace->num = parsed_num;
90  LOG("num = %d\n", workspace->num);
91 
92  workspace->parent = content;
94 
95  con_attach(workspace, content, false);
96 
97  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
98  if (created != NULL)
99  *created = true;
100  }
101  else if (created != NULL) {
102  *created = false;
103  }
104 
105  return workspace;
106 }
107 
108 /*
109  * Returns a pointer to a new workspace in the given output. The workspace
110  * is created attached to the tree hierarchy through the given content
111  * container.
112  *
113  */
115  /* add a workspace to this output */
116  Con *out, *current;
117  char *name;
118  bool exists = true;
119  Con *ws = con_new(NULL, NULL);
120  ws->type = CT_WORKSPACE;
121 
122  /* try the configured workspace bindings first to find a free name */
123  Binding *bind;
125  DLOG("binding with command %s\n", bind->command);
126  if (strlen(bind->command) < strlen("workspace ") ||
127  strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
128  continue;
129  DLOG("relevant command = %s\n", bind->command);
130  char *target = bind->command + strlen("workspace ");
131  /* We check if this is the workspace
132  * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
133  * Beware: The workspace names "next", "prev", "next_on_output",
134  * "prev_on_output", "number", "back_and_forth" and "current" are OK,
135  * so we check before stripping the double quotes */
136  if (strncasecmp(target, "next", strlen("next")) == 0 ||
137  strncasecmp(target, "prev", strlen("prev")) == 0 ||
138  strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
139  strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
140  strncasecmp(target, "number", strlen("number")) == 0 ||
141  strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
142  strncasecmp(target, "current", strlen("current")) == 0)
143  continue;
144  if (*target == '"')
145  target++;
146  FREE(ws->name);
147  ws->name = strdup(target);
148  if (ws->name[strlen(ws->name)-1] == '"')
149  ws->name[strlen(ws->name)-1] = '\0';
150  DLOG("trying name *%s*\n", ws->name);
151 
152  /* Ensure that this workspace is not assigned to a different output —
153  * otherwise we would create it, then move it over to its output, then
154  * find a new workspace, etc… */
155  bool assigned = false;
156  struct Workspace_Assignment *assignment;
158  if (strcmp(assignment->name, ws->name) != 0 ||
159  strcmp(assignment->output, output->name) == 0)
160  continue;
161 
162  assigned = true;
163  break;
164  }
165 
166  if (assigned)
167  continue;
168 
169  current = NULL;
170  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
171  GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
172 
173  exists = (current != NULL);
174  if (!exists) {
175  /* Set ->num to the number of the workspace, if the name actually
176  * is a number or starts with a number */
177  char *endptr = NULL;
178  long parsed_num = strtol(ws->name, &endptr, 10);
179  if (parsed_num == LONG_MIN ||
180  parsed_num == LONG_MAX ||
181  parsed_num < 0 ||
182  endptr == ws->name)
183  ws->num = -1;
184  else ws->num = parsed_num;
185  LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
186 
187  break;
188  }
189  }
190 
191  if (exists) {
192  /* get the next unused workspace number */
193  DLOG("Getting next unused workspace by number\n");
194  int c = 0;
195  while (exists) {
196  c++;
197 
198  FREE(ws->name);
199  sasprintf(&(ws->name), "%d", c);
200 
201  current = NULL;
202  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
203  GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
204  exists = (current != NULL);
205 
206  DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
207  }
208  ws->num = c;
209  }
210  con_attach(ws, content, false);
211 
212  sasprintf(&name, "[i3 con] workspace %s", ws->name);
213  x_set_name(ws, name);
214  free(name);
215 
216  ws->fullscreen_mode = CF_OUTPUT;
217 
220 
221  return ws;
222 }
223 
224 
225 /*
226  * Returns true if the workspace is currently visible. Especially important for
227  * multi-monitor environments, as they can have multiple currenlty active
228  * workspaces.
229  *
230  */
232  Con *output = con_get_output(ws);
233  if (output == NULL)
234  return false;
235  Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
236  LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
237  return (fs == ws);
238 }
239 
240 /*
241  * XXX: we need to clean up all this recursive walking code.
242  *
243  */
244 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
245  Con *current;
246 
247  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
248  if (current != exclude &&
249  current->sticky_group != NULL &&
250  current->window != NULL &&
251  strcmp(current->sticky_group, sticky_group) == 0)
252  return current;
253 
254  Con *recurse = _get_sticky(current, sticky_group, exclude);
255  if (recurse != NULL)
256  return recurse;
257  }
258 
259  TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
260  if (current != exclude &&
261  current->sticky_group != NULL &&
262  current->window != NULL &&
263  strcmp(current->sticky_group, sticky_group) == 0)
264  return current;
265 
266  Con *recurse = _get_sticky(current, sticky_group, exclude);
267  if (recurse != NULL)
268  return recurse;
269  }
270 
271  return NULL;
272 }
273 
274 /*
275  * Reassigns all child windows in sticky containers. Called when the user
276  * changes workspaces.
277  *
278  * XXX: what about sticky containers which contain containers?
279  *
280  */
281 static void workspace_reassign_sticky(Con *con) {
282  Con *current;
283  /* 1: go through all containers */
284 
285  /* handle all children and floating windows of this node */
286  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
287  if (current->sticky_group == NULL) {
288  workspace_reassign_sticky(current);
289  continue;
290  }
291 
292  LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
293  /* 2: find a window which we can re-assign */
294  Con *output = con_get_output(current);
295  Con *src = _get_sticky(output, current->sticky_group, current);
296 
297  if (src == NULL) {
298  LOG("No window found for this sticky group\n");
299  workspace_reassign_sticky(current);
300  continue;
301  }
302 
303  x_move_win(src, current);
304  current->window = src->window;
305  current->mapped = true;
306  src->window = NULL;
307  src->mapped = false;
308 
309  x_reparent_child(current, src);
310 
311  LOG("re-assigned window from src %p to dest %p\n", src, current);
312  }
313 
314  TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
315  workspace_reassign_sticky(current);
316 }
317 
318 /*
319  * Callback to reset the urgent flag of the given con to false. May be started by
320  * _workspace_show to avoid urgency hints being lost by switching to a workspace
321  * focusing the con.
322  *
323  */
324 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
325  Con *con = w->data;
326 
327  DLOG("Resetting urgency flag of con %p by timer\n", con);
328  con->urgent = false;
331  tree_render();
332 
333  ev_timer_stop(main_loop, con->urgency_timer);
334  FREE(con->urgency_timer);
335 }
336 
337 /*
338  * For the "focus" event we send, along the usual "change" field, also the
339  * current and previous workspace, in "current" and "old" respectively.
340  */
341 static void ipc_send_workspace_focus_event(Con *current, Con *old) {
342  setlocale(LC_NUMERIC, "C");
343  yajl_gen gen = ygenalloc();
344 
345  y(map_open);
346 
347  ystr("change");
348  ystr("focus");
349 
350  ystr("current");
351  dump_node(gen, current, false);
352 
353  ystr("old");
354  if (old == NULL)
355  y(null);
356  else
357  dump_node(gen, old, false);
358 
359  y(map_close);
360 
361  const unsigned char *payload;
362  ylength length;
363  y(get_buf, &payload, &length);
364 
365  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
366  y(free);
367  setlocale(LC_NUMERIC, "");
368 }
369 
370 static void _workspace_show(Con *workspace) {
371  Con *current, *old = NULL;
372 
373  /* safe-guard against showing i3-internal workspaces like __i3_scratch */
374  if (con_is_internal(workspace))
375  return;
376 
377  /* disable fullscreen for the other workspaces and get the workspace we are
378  * currently on. */
379  TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
380  if (current->fullscreen_mode == CF_OUTPUT)
381  old = current;
382  current->fullscreen_mode = CF_NONE;
383  }
384 
385  /* enable fullscreen for the target workspace. If it happens to be the
386  * same one we are currently on anyways, we can stop here. */
387  workspace->fullscreen_mode = CF_OUTPUT;
388  current = con_get_workspace(focused);
389  if (workspace == current) {
390  DLOG("Not switching, already there.\n");
391  return;
392  }
393 
394  /* Remember currently focused workspace for switching back to it later with
395  * the 'workspace back_and_forth' command.
396  * NOTE: We have to duplicate the name as the original will be freed when
397  * the corresponding workspace is cleaned up.
398  * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
399  * focused) are skipped, see bug #868. */
400  if (current && !con_is_internal(current)) {
402  if (current) {
404  DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
405  }
406  }
407 
408  workspace_reassign_sticky(workspace);
409 
410  DLOG("switching to %p / %s\n", workspace, workspace->name);
411  Con *next = con_descend_focused(workspace);
412 
413  /* Memorize current output */
414  Con *old_output = con_get_output(focused);
415 
416  /* Display urgency hint for a while if the newly visible workspace would
417  * focus and thereby immediately destroy it */
418  if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
419  /* focus for now… */
420  con_focus(next);
421 
422  /* … but immediately reset urgency flags; they will be set to false by
423  * the timer callback in case the container is focused at the time of
424  * its expiration */
425  focused->urgent = true;
426  workspace->urgent = true;
427 
428  if (focused->urgency_timer == NULL) {
429  DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
430  focused, workspace);
431  focused->urgency_timer = scalloc(sizeof(struct ev_timer));
432  /* use a repeating timer to allow for easy resets */
435  focused->urgency_timer->data = focused;
436  ev_timer_start(main_loop, focused->urgency_timer);
437  } else {
438  DLOG("Resetting urgency timer of con %p on workspace %p\n",
439  focused, workspace);
440  ev_timer_again(main_loop, focused->urgency_timer);
441  }
442  } else
443  con_focus(next);
444 
445  ipc_send_workspace_focus_event(workspace, old);
446 
447  DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
448  /* Close old workspace if necessary. This must be done *after* doing
449  * urgency handling, because tree_close() will do a con_focus() on the next
450  * client, which will clear the urgency flag too early. Also, there is no
451  * way for con_focus() to know about when to clear urgency immediately and
452  * when to defer it. */
453  if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
454  /* check if this workspace is currently visible */
455  if (!workspace_is_visible(old)) {
456  LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
457  tree_close(old, DONT_KILL_WINDOW, false, false);
458  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
459  }
460  }
461 
462  workspace->fullscreen_mode = CF_OUTPUT;
463  LOG("focused now = %p / %s\n", focused, focused->name);
464 
465  /* Set mouse pointer */
466  Con *new_output = con_get_output(focused);
467  if (old_output != new_output) {
468  x_set_warp_to(&next->rect);
469  }
470 
471  /* Update the EWMH hints */
473 }
474 
475 /*
476  * Switches to the given workspace
477  *
478  */
479 void workspace_show(Con *workspace) {
480  _workspace_show(workspace);
481 }
482 
483 /*
484  * Looks up the workspace by name and switches to it.
485  *
486  */
487 void workspace_show_by_name(const char *num) {
488  Con *workspace;
489  workspace = workspace_get(num, NULL);
490  _workspace_show(workspace);
491 }
492 
493 /*
494  * Focuses the next workspace.
495  *
496  */
498  Con *current = con_get_workspace(focused);
499  Con *next = NULL;
500  Con *output;
501 
502  if (current->num == -1) {
503  /* If currently a named workspace, find next named workspace. */
504  next = TAILQ_NEXT(current, nodes);
505  } else {
506  /* If currently a numbered workspace, find next numbered workspace. */
507  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
508  /* Skip outputs starting with __, they are internal. */
509  if (con_is_internal(output))
510  continue;
512  if (child->type != CT_WORKSPACE)
513  continue;
514  if (child->num == -1)
515  break;
516  /* Need to check child against current and next because we are
517  * traversing multiple lists and thus are not guaranteed the
518  * relative order between the list of workspaces. */
519  if (current->num < child->num && (!next || child->num < next->num))
520  next = child;
521  }
522  }
523  }
524 
525  /* Find next named workspace. */
526  if (!next) {
527  bool found_current = false;
528  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
529  /* Skip outputs starting with __, they are internal. */
530  if (con_is_internal(output))
531  continue;
533  if (child->type != CT_WORKSPACE)
534  continue;
535  if (child == current) {
536  found_current = 1;
537  } else if (child->num == -1 && (current->num != -1 || found_current)) {
538  next = child;
539  goto workspace_next_end;
540  }
541  }
542  }
543  }
544 
545  /* Find first workspace. */
546  if (!next) {
547  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
548  /* Skip outputs starting with __, they are internal. */
549  if (con_is_internal(output))
550  continue;
552  if (child->type != CT_WORKSPACE)
553  continue;
554  if (!next || (child->num != -1 && child->num < next->num))
555  next = child;
556  }
557  }
558  }
559 workspace_next_end:
560  return next;
561 }
562 
563 /*
564  * Focuses the previous workspace.
565  *
566  */
568  Con *current = con_get_workspace(focused);
569  Con *prev = NULL;
570  Con *output;
571 
572  if (current->num == -1) {
573  /* If named workspace, find previous named workspace. */
574  prev = TAILQ_PREV(current, nodes_head, nodes);
575  if (prev && prev->num != -1)
576  prev = NULL;
577  } else {
578  /* If numbered workspace, find previous numbered workspace. */
579  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
580  /* Skip outputs starting with __, they are internal. */
581  if (con_is_internal(output))
582  continue;
584  if (child->type != CT_WORKSPACE || child->num == -1)
585  continue;
586  /* Need to check child against current and previous because we
587  * are traversing multiple lists and thus are not guaranteed
588  * the relative order between the list of workspaces. */
589  if (current->num > child->num && (!prev || child->num > prev->num))
590  prev = child;
591  }
592  }
593  }
594 
595  /* Find previous named workspace. */
596  if (!prev) {
597  bool found_current = false;
598  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
599  /* Skip outputs starting with __, they are internal. */
600  if (con_is_internal(output))
601  continue;
603  if (child->type != CT_WORKSPACE)
604  continue;
605  if (child == current) {
606  found_current = true;
607  } else if (child->num == -1 && (current->num != -1 || found_current)) {
608  prev = child;
609  goto workspace_prev_end;
610  }
611  }
612  }
613  }
614 
615  /* Find last workspace. */
616  if (!prev) {
617  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
618  /* Skip outputs starting with __, they are internal. */
619  if (con_is_internal(output))
620  continue;
622  if (child->type != CT_WORKSPACE)
623  continue;
624  if (!prev || child->num > prev->num)
625  prev = child;
626  }
627  }
628  }
629 
630 workspace_prev_end:
631  return prev;
632 }
633 
634 
635 /*
636  * Focuses the next workspace on the same output.
637  *
638  */
640  Con *current = con_get_workspace(focused);
641  Con *next = NULL;
643 
644  if (current->num == -1) {
645  /* If currently a named workspace, find next named workspace. */
646  next = TAILQ_NEXT(current, nodes);
647  } else {
648  /* If currently a numbered workspace, find next numbered workspace. */
650  if (child->type != CT_WORKSPACE)
651  continue;
652  if (child->num == -1)
653  break;
654  /* Need to check child against current and next because we are
655  * traversing multiple lists and thus are not guaranteed the
656  * relative order between the list of workspaces. */
657  if (current->num < child->num && (!next || child->num < next->num))
658  next = child;
659  }
660  }
661 
662  /* Find next named workspace. */
663  if (!next) {
664  bool found_current = false;
666  if (child->type != CT_WORKSPACE)
667  continue;
668  if (child == current) {
669  found_current = 1;
670  } else if (child->num == -1 && (current->num != -1 || found_current)) {
671  next = child;
672  goto workspace_next_on_output_end;
673  }
674  }
675  }
676 
677  /* Find first workspace. */
678  if (!next) {
680  if (child->type != CT_WORKSPACE)
681  continue;
682  if (!next || (child->num != -1 && child->num < next->num))
683  next = child;
684  }
685  }
686 workspace_next_on_output_end:
687  return next;
688 }
689 
690 /*
691  * Focuses the previous workspace on same output.
692  *
693  */
695  Con *current = con_get_workspace(focused);
696  Con *prev = NULL;
698  DLOG("output = %s\n", output->name);
699 
700  if (current->num == -1) {
701  /* If named workspace, find previous named workspace. */
702  prev = TAILQ_PREV(current, nodes_head, nodes);
703  if (prev && prev->num != -1)
704  prev = NULL;
705  } else {
706  /* If numbered workspace, find previous numbered workspace. */
708  if (child->type != CT_WORKSPACE || child->num == -1)
709  continue;
710  /* Need to check child against current and previous because we
711  * are traversing multiple lists and thus are not guaranteed
712  * the relative order between the list of workspaces. */
713  if (current->num > child->num && (!prev || child->num > prev->num))
714  prev = child;
715  }
716  }
717 
718  /* Find previous named workspace. */
719  if (!prev) {
720  bool found_current = false;
722  if (child->type != CT_WORKSPACE)
723  continue;
724  if (child == current) {
725  found_current = true;
726  } else if (child->num == -1 && (current->num != -1 || found_current)) {
727  prev = child;
728  goto workspace_prev_on_output_end;
729  }
730  }
731  }
732 
733  /* Find last workspace. */
734  if (!prev) {
736  if (child->type != CT_WORKSPACE)
737  continue;
738  if (!prev || child->num > prev->num)
739  prev = child;
740  }
741  }
742 
743 workspace_prev_on_output_end:
744  return prev;
745 }
746 
747 /*
748  * Focuses the previously focused workspace.
749  *
750  */
753  DLOG("No previous workspace name set. Not switching.");
754  return;
755  }
756 
758 }
759 
760 /*
761  * Returns the previously focused workspace con, or NULL if unavailable.
762  *
763  */
766  DLOG("no previous workspace name set.");
767  return NULL;
768  }
769 
770  Con *workspace;
771  workspace = workspace_get(previous_workspace_name, NULL);
772 
773  return workspace;
774 }
775 
776 static bool get_urgency_flag(Con *con) {
777  Con *child;
778  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
779  if (child->urgent || get_urgency_flag(child))
780  return true;
781 
782  TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
783  if (child->urgent || get_urgency_flag(child))
784  return true;
785 
786  return false;
787 }
788 
789 /*
790  * Goes through all clients on the given workspace and updates the workspace’s
791  * urgent flag accordingly.
792  *
793  */
795  bool old_flag = ws->urgent;
796  ws->urgent = get_urgency_flag(ws);
797  DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
798 
799  if (old_flag != ws->urgent)
800  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
801 }
802 
803 /*
804  * 'Forces' workspace orientation by moving all cons into a new split-con with
805  * the same layout as the workspace and then changing the workspace layout.
806  *
807  */
808 void ws_force_orientation(Con *ws, orientation_t orientation) {
809  /* 1: create a new split container */
810  Con *split = con_new(NULL, NULL);
811  split->parent = ws;
812 
813  /* 2: copy layout from workspace */
814  split->layout = ws->layout;
815 
816  Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
817 
818  /* 3: move the existing cons of this workspace below the new con */
819  DLOG("Moving cons\n");
820  while (!TAILQ_EMPTY(&(ws->nodes_head))) {
821  Con *child = TAILQ_FIRST(&(ws->nodes_head));
822  con_detach(child);
823  con_attach(child, split, true);
824  }
825 
826  /* 4: switch workspace layout */
827  ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
828  DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
829 
830  /* 5: attach the new split container to the workspace */
831  DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
832  con_attach(split, ws, false);
833 
834  /* 6: fix the percentages */
835  con_fix_percent(ws);
836 
837  if (old_focused)
838  con_focus(old_focused);
839 }
840 
841 /*
842  * Called when a new con (with a window, not an empty or split con) should be
843  * attached to the workspace (for example when managing a new window or when
844  * moving an existing window to the workspace level).
845  *
846  * Depending on the workspace_layout setting, this function either returns the
847  * workspace itself (default layout) or creates a new stacked/tabbed con and
848  * returns that.
849  *
850  */
852  DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
853 
854  if (ws->workspace_layout == L_DEFAULT) {
855  DLOG("Default layout, just attaching it to the workspace itself.\n");
856  return ws;
857  }
858 
859  DLOG("Non-default layout, creating a new split container\n");
860  /* 1: create a new split container */
861  Con *new = con_new(NULL, NULL);
862  new->parent = ws;
863 
864  /* 2: set the requested layout on the split con */
865  new->layout = ws->workspace_layout;
866 
867  /* 4: attach the new split container to the workspace */
868  DLOG("Attaching new split %p to workspace %p\n", new, ws);
869  con_attach(new, ws, false);
870 
871  return new;
872 }
873 
881  if (TAILQ_EMPTY(&(ws->nodes_head))) {
882  ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
883  return NULL;
884  }
885 
886  Con *new = con_new(NULL, NULL);
887  new->parent = ws;
888  new->layout = ws->layout;
889 
890  DLOG("Moving children of workspace %p / %s into container %p\n",
891  ws, ws->name, new);
892 
893  Con *child;
894  while (!TAILQ_EMPTY(&(ws->nodes_head))) {
895  child = TAILQ_FIRST(&(ws->nodes_head));
896  con_detach(child);
897  con_attach(child, new, true);
898  }
899 
900  con_attach(new, ws, true);
901 
902  return new;
903 }