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-2011 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 
13 /* Stores a copy of the name of the last used workspace for the workspace
14  * back-and-forth switching. */
15 static char *previous_workspace_name = NULL;
16 
17 /*
18  * Returns a pointer to the workspace with the given number (starting at 0),
19  * creating the workspace if necessary (by allocating the necessary amount of
20  * memory and initializing the data structures correctly).
21  *
22  */
23 Con *workspace_get(const char *num, bool *created) {
24  Con *output, *workspace = NULL;
25 
26  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
27  GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
28 
29  if (workspace == NULL) {
30  LOG("Creating new workspace \"%s\"\n", num);
31  /* unless an assignment is found, we will create this workspace on the current output */
32  output = con_get_output(focused);
33  /* look for assignments */
34  struct Workspace_Assignment *assignment;
36  if (strcmp(assignment->name, num) != 0)
37  continue;
38 
39  LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
40  GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
41  break;
42  }
43  Con *content = output_get_content(output);
44  LOG("got output %p with content %p\n", output, content);
45  /* We need to attach this container after setting its type. con_attach
46  * will handle CT_WORKSPACEs differently */
47  workspace = con_new(NULL, NULL);
48  char *name;
49  sasprintf(&name, "[i3 con] workspace %s", num);
50  x_set_name(workspace, name);
51  free(name);
52  workspace->type = CT_WORKSPACE;
53  FREE(workspace->name);
54  workspace->name = sstrdup(num);
55  /* We set ->num to the number if this workspace’s name begins with a
56  * positive number. Otherwise it’s a named ws and num will be -1. */
57  char *endptr = NULL;
58  long parsed_num = strtol(num, &endptr, 10);
59  if (parsed_num == LONG_MIN ||
60  parsed_num == LONG_MAX ||
61  parsed_num < 0 ||
62  endptr == num)
63  workspace->num = -1;
64  else workspace->num = parsed_num;
65  LOG("num = %d\n", workspace->num);
66 
67  /* If default_orientation is set to NO_ORIENTATION we
68  * determine workspace orientation from workspace size.
69  * Otherwise we just set the orientation to default_orientation. */
71  workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
72  DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
73  workspace->rect.width, workspace->rect.height, workspace->orientation);
74  } else {
76  }
77 
78  con_attach(workspace, content, false);
79 
80  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
81  if (created != NULL)
82  *created = true;
83  }
84  else if (created != NULL) {
85  *created = false;
86  }
87 
88  return workspace;
89 }
90 
91 /*
92  * Returns a pointer to a new workspace in the given output. The workspace
93  * is created attached to the tree hierarchy through the given content
94  * container.
95  *
96  */
98  /* add a workspace to this output */
99  Con *out, *current;
100  char *name;
101  bool exists = true;
102  Con *ws = con_new(NULL, NULL);
103  ws->type = CT_WORKSPACE;
104 
105  /* try the configured workspace bindings first to find a free name */
106  Binding *bind;
108  DLOG("binding with command %s\n", bind->command);
109  if (strlen(bind->command) < strlen("workspace ") ||
110  strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
111  continue;
112  DLOG("relevant command = %s\n", bind->command);
113  char *target = bind->command + strlen("workspace ");
114  /* We check if this is the workspace
115  * next/prev/next_on_output/prev_on_output/back_and_forth command.
116  * Beware: The workspace names "next", "prev", "next_on_output",
117  * "prev_on_output" and "back_and_forth" are OK, so we check before
118  * stripping the double quotes */
119  if (strncasecmp(target, "next", strlen("next")) == 0 ||
120  strncasecmp(target, "prev", strlen("prev")) == 0 ||
121  strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
122  strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
123  strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0)
124  continue;
125  if (*target == '"')
126  target++;
127  FREE(ws->name);
128  ws->name = strdup(target);
129  if (ws->name[strlen(ws->name)-1] == '"')
130  ws->name[strlen(ws->name)-1] = '\0';
131  DLOG("trying name *%s*\n", ws->name);
132 
133  /* Ensure that this workspace is not assigned to a different output —
134  * otherwise we would create it, then move it over to its output, then
135  * find a new workspace, etc… */
136  bool assigned = false;
137  struct Workspace_Assignment *assignment;
139  if (strcmp(assignment->name, ws->name) != 0 ||
140  strcmp(assignment->output, output->name) == 0)
141  continue;
142 
143  assigned = true;
144  break;
145  }
146 
147  if (assigned)
148  continue;
149 
150  current = NULL;
151  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
152  GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
153 
154  exists = (current != NULL);
155  if (!exists) {
156  /* Set ->num to the number of the workspace, if the name actually
157  * is a number or starts with a number */
158  char *endptr = NULL;
159  long parsed_num = strtol(ws->name, &endptr, 10);
160  if (parsed_num == LONG_MIN ||
161  parsed_num == LONG_MAX ||
162  parsed_num < 0 ||
163  endptr == ws->name)
164  ws->num = -1;
165  else ws->num = parsed_num;
166  LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
167 
168  break;
169  }
170  }
171 
172  if (exists) {
173  /* get the next unused workspace number */
174  DLOG("Getting next unused workspace by number\n");
175  int c = 0;
176  while (exists) {
177  c++;
178 
179  FREE(ws->name);
180  sasprintf(&(ws->name), "%d", c);
181 
182  current = NULL;
183  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
184  GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
185  exists = (current != NULL);
186 
187  DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
188  }
189  ws->num = c;
190  }
191  con_attach(ws, content, false);
192 
193  sasprintf(&name, "[i3 con] workspace %s", ws->name);
194  x_set_name(ws, name);
195  free(name);
196 
197  ws->fullscreen_mode = CF_OUTPUT;
198 
199  /* If default_orientation is set to NO_ORIENTATION we determine
200  * orientation depending on output resolution. */
202  ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
203  DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
204  output->rect.width, output->rect.height, ws->orientation);
205  } else {
207  }
208 
209  return ws;
210 }
211 
212 /*
213  * Returns true if the workspace is currently visible. Especially important for
214  * multi-monitor environments, as they can have multiple currenlty active
215  * workspaces.
216  *
217  */
219  Con *output = con_get_output(ws);
220  if (output == NULL)
221  return false;
222  Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
223  LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
224  return (fs == ws);
225 }
226 
227 /*
228  * XXX: we need to clean up all this recursive walking code.
229  *
230  */
231 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
232  Con *current;
233 
234  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
235  if (current != exclude &&
236  current->sticky_group != NULL &&
237  current->window != NULL &&
238  strcmp(current->sticky_group, sticky_group) == 0)
239  return current;
240 
241  Con *recurse = _get_sticky(current, sticky_group, exclude);
242  if (recurse != NULL)
243  return recurse;
244  }
245 
246  TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
247  if (current != exclude &&
248  current->sticky_group != NULL &&
249  current->window != NULL &&
250  strcmp(current->sticky_group, sticky_group) == 0)
251  return current;
252 
253  Con *recurse = _get_sticky(current, sticky_group, exclude);
254  if (recurse != NULL)
255  return recurse;
256  }
257 
258  return NULL;
259 }
260 
261 /*
262  * Reassigns all child windows in sticky containers. Called when the user
263  * changes workspaces.
264  *
265  * XXX: what about sticky containers which contain containers?
266  *
267  */
268 static void workspace_reassign_sticky(Con *con) {
269  Con *current;
270  /* 1: go through all containers */
271 
272  /* handle all children and floating windows of this node */
273  TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
274  if (current->sticky_group == NULL) {
275  workspace_reassign_sticky(current);
276  continue;
277  }
278 
279  LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
280  /* 2: find a window which we can re-assign */
281  Con *output = con_get_output(current);
282  Con *src = _get_sticky(output, current->sticky_group, current);
283 
284  if (src == NULL) {
285  LOG("No window found for this sticky group\n");
286  workspace_reassign_sticky(current);
287  continue;
288  }
289 
290  x_move_win(src, current);
291  current->window = src->window;
292  current->mapped = true;
293  src->window = NULL;
294  src->mapped = false;
295 
296  x_reparent_child(current, src);
297 
298  LOG("re-assigned window from src %p to dest %p\n", src, current);
299  }
300 
301  TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
302  workspace_reassign_sticky(current);
303 }
304 
305 
306 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
307  Con *current, *old = NULL;
308 
309  /* safe-guard against showing i3-internal workspaces like __i3_scratch */
310  if (workspace->name[0] == '_' && workspace->name[1] == '_')
311  return;
312 
313  /* disable fullscreen for the other workspaces and get the workspace we are
314  * currently on. */
315  TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
316  if (current->fullscreen_mode == CF_OUTPUT)
317  old = current;
318  current->fullscreen_mode = CF_NONE;
319  }
320 
321  /* enable fullscreen for the target workspace. If it happens to be the
322  * same one we are currently on anyways, we can stop here. */
323  workspace->fullscreen_mode = CF_OUTPUT;
324  current = con_get_workspace(focused);
325  if (workspace == current) {
326  DLOG("Not switching, already there.\n");
327  return;
328  }
329 
330  /* Remember currently focused workspace for switching back to it later with
331  * the 'workspace back_and_forth' command.
332  * NOTE: We have to duplicate the name as the original will be freed when
333  * the corresponding workspace is cleaned up. */
334 
336  if (current)
338 
339  workspace_reassign_sticky(workspace);
340 
341  LOG("switching to %p\n", workspace);
342  Con *next = con_descend_focused(workspace);
343 
344  if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
345  /* check if this workspace is currently visible */
346  if (!workspace_is_visible(old)) {
347  LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
348  tree_close(old, DONT_KILL_WINDOW, false, false);
349  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
350  changed_num_workspaces = true;
351  }
352  }
353 
354  /* Memorize current output */
355  Con *old_output = con_get_output(focused);
356 
357  con_focus(next);
358  workspace->fullscreen_mode = CF_OUTPUT;
359  LOG("focused now = %p / %s\n", focused, focused->name);
360 
361  /* Set mouse pointer */
362  Con *new_output = con_get_output(focused);
363  if (old_output != new_output) {
364  x_set_warp_to(&next->rect);
365  }
366 
367  /* Update the EWMH hints */
369 
370  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
371 }
372 
373 /*
374  * Switches to the given workspace
375  *
376  */
377 void workspace_show(Con *workspace) {
378  _workspace_show(workspace, false);
379 }
380 
381 /*
382  * Looks up the workspace by name and switches to it.
383  *
384  */
385 void workspace_show_by_name(const char *num) {
386  Con *workspace;
387  bool changed_num_workspaces;
388  workspace = workspace_get(num, &changed_num_workspaces);
389  _workspace_show(workspace, changed_num_workspaces);
390 }
391 
392 /*
393  * Focuses the next workspace.
394  *
395  */
397  Con *current = con_get_workspace(focused);
398  Con *next = NULL;
399  Con *output;
400 
401  if (current->num == -1) {
402  /* If currently a named workspace, find next named workspace. */
403  next = TAILQ_NEXT(current, nodes);
404  } else {
405  /* If currently a numbered workspace, find next numbered workspace. */
406  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
407  /* Skip outputs starting with __, they are internal. */
408  if (output->name[0] == '_' && output->name[1] == '_')
409  continue;
411  if (child->type != CT_WORKSPACE)
412  continue;
413  if (child->num == -1)
414  break;
415  /* Need to check child against current and next because we are
416  * traversing multiple lists and thus are not guaranteed the
417  * relative order between the list of workspaces. */
418  if (current->num < child->num && (!next || child->num < next->num))
419  next = child;
420  }
421  }
422  }
423 
424  /* Find next named workspace. */
425  if (!next) {
426  bool found_current = false;
427  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
428  /* Skip outputs starting with __, they are internal. */
429  if (output->name[0] == '_' && output->name[1] == '_')
430  continue;
432  if (child->type != CT_WORKSPACE)
433  continue;
434  if (child == current) {
435  found_current = 1;
436  } else if (child->num == -1 && (current->num != -1 || found_current)) {
437  next = child;
438  goto workspace_next_end;
439  }
440  }
441  }
442  }
443 
444  /* Find first workspace. */
445  if (!next) {
446  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
447  /* Skip outputs starting with __, they are internal. */
448  if (output->name[0] == '_' && output->name[1] == '_')
449  continue;
451  if (child->type != CT_WORKSPACE)
452  continue;
453  if (!next || (child->num != -1 && child->num < next->num))
454  next = child;
455  }
456  }
457  }
458 workspace_next_end:
459  return next;
460 }
461 
462 /*
463  * Focuses the previous workspace.
464  *
465  */
467  Con *current = con_get_workspace(focused);
468  Con *prev = NULL;
469  Con *output;
470 
471  if (current->num == -1) {
472  /* If named workspace, find previous named workspace. */
473  prev = TAILQ_PREV(current, nodes_head, nodes);
474  if (prev && prev->num != -1)
475  prev = NULL;
476  } else {
477  /* If numbered workspace, find previous numbered workspace. */
478  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
479  /* Skip outputs starting with __, they are internal. */
480  if (output->name[0] == '_' && output->name[1] == '_')
481  continue;
483  if (child->type != CT_WORKSPACE || child->num == -1)
484  continue;
485  /* Need to check child against current and previous because we
486  * are traversing multiple lists and thus are not guaranteed
487  * the relative order between the list of workspaces. */
488  if (current->num > child->num && (!prev || child->num > prev->num))
489  prev = child;
490  }
491  }
492  }
493 
494  /* Find previous named workspace. */
495  if (!prev) {
496  bool found_current = false;
497  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
498  /* Skip outputs starting with __, they are internal. */
499  if (output->name[0] == '_' && output->name[1] == '_')
500  continue;
502  if (child->type != CT_WORKSPACE)
503  continue;
504  if (child == current) {
505  found_current = true;
506  } else if (child->num == -1 && (current->num != -1 || found_current)) {
507  prev = child;
508  goto workspace_prev_end;
509  }
510  }
511  }
512  }
513 
514  /* Find last workspace. */
515  if (!prev) {
516  TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
517  /* Skip outputs starting with __, they are internal. */
518  if (output->name[0] == '_' && output->name[1] == '_')
519  continue;
521  if (child->type != CT_WORKSPACE)
522  continue;
523  if (!prev || child->num > prev->num)
524  prev = child;
525  }
526  }
527  }
528 
529 workspace_prev_end:
530  return prev;
531 }
532 
533 
534 /*
535  * Focuses the next workspace on the same output.
536  *
537  */
539  Con *current = con_get_workspace(focused);
540  Con *next = NULL;
542 
543  if (current->num == -1) {
544  /* If currently a named workspace, find next named workspace. */
545  next = TAILQ_NEXT(current, nodes);
546  } else {
547  /* If currently a numbered workspace, find next numbered workspace. */
549  if (child->type != CT_WORKSPACE)
550  continue;
551  if (child->num == -1)
552  break;
553  /* Need to check child against current and next because we are
554  * traversing multiple lists and thus are not guaranteed the
555  * relative order between the list of workspaces. */
556  if (current->num < child->num && (!next || child->num < next->num))
557  next = child;
558  }
559  }
560 
561  /* Find next named workspace. */
562  if (!next) {
563  bool found_current = false;
565  if (child->type != CT_WORKSPACE)
566  continue;
567  if (child == current) {
568  found_current = 1;
569  } else if (child->num == -1 && (current->num != -1 || found_current)) {
570  next = child;
571  goto workspace_next_on_output_end;
572  }
573  }
574  }
575 
576  /* Find first workspace. */
577  if (!next) {
579  if (child->type != CT_WORKSPACE)
580  continue;
581  if (!next || (child->num != -1 && child->num < next->num))
582  next = child;
583  }
584  }
585 workspace_next_on_output_end:
586  return next;
587 }
588 
589 /*
590  * Focuses the previous workspace on same output.
591  *
592  */
594  Con *current = con_get_workspace(focused);
595  Con *prev = NULL;
597  DLOG("output = %s\n", output->name);
598 
599  if (current->num == -1) {
600  /* If named workspace, find previous named workspace. */
601  prev = TAILQ_PREV(current, nodes_head, nodes);
602  if (prev && prev->num != -1)
603  prev = NULL;
604  } else {
605  /* If numbered workspace, find previous numbered workspace. */
607  if (child->type != CT_WORKSPACE || child->num == -1)
608  continue;
609  /* Need to check child against current and previous because we
610  * are traversing multiple lists and thus are not guaranteed
611  * the relative order between the list of workspaces. */
612  if (current->num > child->num && (!prev || child->num > prev->num))
613  prev = child;
614  }
615  }
616 
617  /* Find previous named workspace. */
618  if (!prev) {
619  bool found_current = false;
621  if (child->type != CT_WORKSPACE)
622  continue;
623  if (child == current) {
624  found_current = true;
625  } else if (child->num == -1 && (current->num != -1 || found_current)) {
626  prev = child;
627  goto workspace_prev_on_output_end;
628  }
629  }
630  }
631 
632  /* Find last workspace. */
633  if (!prev) {
635  if (child->type != CT_WORKSPACE)
636  continue;
637  if (!prev || child->num > prev->num)
638  prev = child;
639  }
640  }
641 
642 workspace_prev_on_output_end:
643  return prev;
644 }
645 
646 /*
647  * Focuses the previously focused workspace.
648  *
649  */
652  DLOG("No previous workspace name set. Not switching.");
653  return;
654  }
655 
657 }
658 
659 static bool get_urgency_flag(Con *con) {
660  Con *child;
661  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
662  if (child->urgent || get_urgency_flag(child))
663  return true;
664 
665  TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
666  if (child->urgent || get_urgency_flag(child))
667  return true;
668 
669  return false;
670 }
671 
672 /*
673  * Goes through all clients on the given workspace and updates the workspace’s
674  * urgent flag accordingly.
675  *
676  */
678  bool old_flag = ws->urgent;
679  ws->urgent = get_urgency_flag(ws);
680  DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
681 
682  if (old_flag != ws->urgent)
683  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
684 }
685 
686 /*
687  * 'Forces' workspace orientation by moving all cons into a new split-con with
688  * the same orientation as the workspace and then changing the workspace
689  * orientation.
690  *
691  */
692 void ws_force_orientation(Con *ws, orientation_t orientation) {
693  /* 1: create a new split container */
694  Con *split = con_new(NULL, NULL);
695  split->parent = ws;
696 
697  /* 2: copy layout and orientation from workspace */
698  split->layout = ws->layout;
699  split->orientation = ws->orientation;
700 
701  Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
702 
703  /* 3: move the existing cons of this workspace below the new con */
704  DLOG("Moving cons\n");
705  while (!TAILQ_EMPTY(&(ws->nodes_head))) {
706  Con *child = TAILQ_FIRST(&(ws->nodes_head));
707  con_detach(child);
708  con_attach(child, split, true);
709  }
710 
711  /* 4: switch workspace orientation */
712  ws->orientation = orientation;
713 
714  /* 5: attach the new split container to the workspace */
715  DLOG("Attaching new split to ws\n");
716  con_attach(split, ws, false);
717 
718  /* 6: fix the percentages */
719  con_fix_percent(ws);
720 
721  if (old_focused)
722  con_focus(old_focused);
723 }
724 
725 /*
726  * Called when a new con (with a window, not an empty or split con) should be
727  * attached to the workspace (for example when managing a new window or when
728  * moving an existing window to the workspace level).
729  *
730  * Depending on the workspace_layout setting, this function either returns the
731  * workspace itself (default layout) or creates a new stacked/tabbed con and
732  * returns that.
733  *
734  */
736  DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
737 
738  if (config.default_layout == L_DEFAULT) {
739  DLOG("Default layout, just attaching it to the workspace itself.\n");
740  return ws;
741  }
742 
743  DLOG("Non-default layout, creating a new split container\n");
744  /* 1: create a new split container */
745  Con *new = con_new(NULL, NULL);
746  new->parent = ws;
747 
748  /* 2: set the requested layout on the split con */
750 
751  /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
752  * to be set. Otherwise, this con will not be interpreted as a split
753  * container. */
755  new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
756  } else {
757  new->orientation = config.default_orientation;
758  }
759 
760  /* 4: attach the new split container to the workspace */
761  DLOG("Attaching new split %p to workspace %p\n", new, ws);
762  con_attach(new, ws, false);
763 
764  return new;
765 }