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