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