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