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