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