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