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