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