]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Bugfix: Fix segfault when starting i3 (Thanks pnutzh4x0r)
[i3/i3] / src / workspace.c
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: Functions for modifying workspaces
8  *
9  */
10 #include "all.h"
11
12 /* Stores a copy of the name of the last used workspace for the workspace
13  * back-and-forth switching. */
14 static char *previous_workspace_name = NULL;
15
16 /*
17  * Returns a pointer to the workspace with the given number (starting at 0),
18  * creating the workspace if necessary (by allocating the necessary amount of
19  * memory and initializing the data structures correctly).
20  *
21  */
22 Con *workspace_get(const char *num, bool *created) {
23     Con *output, *workspace = NULL;
24
25     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
26         GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
27
28     if (workspace == NULL) {
29         LOG("Creating new workspace \"%s\"\n", num);
30         /* unless an assignment is found, we will create this workspace on the current output */
31         output = con_get_output(focused);
32         /* look for assignments */
33         struct Workspace_Assignment *assignment;
34         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
35             if (strcmp(assignment->name, num) != 0)
36                 continue;
37
38             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
39             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
40             break;
41         }
42         Con *content = output_get_content(output);
43         LOG("got output %p with content %p\n", output, content);
44         /* We need to attach this container after setting its type. con_attach
45          * will handle CT_WORKSPACEs differently */
46         workspace = con_new(NULL, NULL);
47         char *name;
48         asprintf(&name, "[i3 con] workspace %s", num);
49         x_set_name(workspace, name);
50         free(name);
51         workspace->type = CT_WORKSPACE;
52         FREE(workspace->name);
53         workspace->name = sstrdup(num);
54         /* We set ->num to the number if this workspace’s name consists only of
55          * a positive number. Otherwise it’s a named ws and num will be -1. */
56         char *endptr = NULL;
57         long parsed_num = strtol(num, &endptr, 10);
58         if (parsed_num == LONG_MIN ||
59             parsed_num == LONG_MAX ||
60             parsed_num < 0 ||
61             endptr == num)
62             workspace->num = -1;
63         else workspace->num = parsed_num;
64         LOG("num = %d\n", workspace->num);
65
66         /* If default_orientation is set to NO_ORIENTATION we
67          * determine workspace orientation from workspace size.
68          * Otherwise we just set the orientation to default_orientation. */
69         if (config.default_orientation == NO_ORIENTATION) {
70             workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
71             DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
72                  workspace->rect.width, workspace->rect.height, workspace->orientation);
73         } else {
74             workspace->orientation = config.default_orientation;
75         }
76
77         con_attach(workspace, content, false);
78
79         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
80         if (created != NULL)
81             *created = true;
82     }
83     else if (created != NULL) {
84         *created = false;
85     }
86
87     return workspace;
88 }
89
90 /*
91  * Returns true if the workspace is currently visible. Especially important for
92  * multi-monitor environments, as they can have multiple currenlty active
93  * workspaces.
94  *
95  */
96 bool workspace_is_visible(Con *ws) {
97     Con *output = con_get_output(ws);
98     if (output == NULL)
99         return false;
100     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
101     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
102     return (fs == ws);
103 }
104
105 /*
106  * XXX: we need to clean up all this recursive walking code.
107  *
108  */
109 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
110     Con *current;
111
112     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
113         if (current != exclude &&
114             current->sticky_group != NULL &&
115             current->window != NULL &&
116             strcmp(current->sticky_group, sticky_group) == 0)
117             return current;
118
119         Con *recurse = _get_sticky(current, sticky_group, exclude);
120         if (recurse != NULL)
121             return recurse;
122     }
123
124     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
125         if (current != exclude &&
126             current->sticky_group != NULL &&
127             current->window != NULL &&
128             strcmp(current->sticky_group, sticky_group) == 0)
129             return current;
130
131         Con *recurse = _get_sticky(current, sticky_group, exclude);
132         if (recurse != NULL)
133             return recurse;
134     }
135
136     return NULL;
137 }
138
139 /*
140  * Reassigns all child windows in sticky containers. Called when the user
141  * changes workspaces.
142  *
143  * XXX: what about sticky containers which contain containers?
144  *
145  */
146 static void workspace_reassign_sticky(Con *con) {
147     Con *current;
148     /* 1: go through all containers */
149
150     /* handle all children and floating windows of this node */
151     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
152         if (current->sticky_group == NULL) {
153             workspace_reassign_sticky(current);
154             continue;
155         }
156
157         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
158         /* 2: find a window which we can re-assign */
159         Con *output = con_get_output(current);
160         Con *src = _get_sticky(output, current->sticky_group, current);
161
162         if (src == NULL) {
163             LOG("No window found for this sticky group\n");
164             workspace_reassign_sticky(current);
165             continue;
166         }
167
168         x_move_win(src, current);
169         current->window = src->window;
170         current->mapped = true;
171         src->window = NULL;
172         src->mapped = false;
173
174         x_reparent_child(current, src);
175
176         LOG("re-assigned window from src %p to dest %p\n", src, current);
177     }
178
179     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
180         workspace_reassign_sticky(current);
181 }
182
183
184 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
185     Con *current, *old = NULL;
186
187     /* disable fullscreen for the other workspaces and get the workspace we are
188      * currently on. */
189     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
190         if (current->fullscreen_mode == CF_OUTPUT)
191             old = current;
192         current->fullscreen_mode = CF_NONE;
193     }
194
195     /* enable fullscreen for the target workspace. If it happens to be the
196      * same one we are currently on anyways, we can stop here. */
197     workspace->fullscreen_mode = CF_OUTPUT;
198     current = con_get_workspace(focused);
199     if (workspace == current) {
200         DLOG("Not switching, already there.\n");
201         return;
202     }
203
204     /* Remember currently focused workspace for switching back to it later with
205      * the 'workspace back_and_forth' command.
206      * NOTE: We have to duplicate the name as the original will be freed when
207      * the corresponding workspace is cleaned up. */
208
209     FREE(previous_workspace_name);
210     if (current)
211         previous_workspace_name = sstrdup(current->name);
212
213     workspace_reassign_sticky(workspace);
214
215     LOG("switching to %p\n", workspace);
216     Con *next = con_descend_focused(workspace);
217
218     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
219         /* check if this workspace is currently visible */
220         if (!workspace_is_visible(old)) {
221             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
222             tree_close(old, DONT_KILL_WINDOW, false, false);
223             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
224             changed_num_workspaces = true;
225         }
226     }
227
228     /* Memorize current output */
229     Con *old_output = con_get_output(focused);
230
231     con_focus(next);
232     workspace->fullscreen_mode = CF_OUTPUT;
233     LOG("focused now = %p / %s\n", focused, focused->name);
234
235     /* Set mouse pointer */
236     Con *new_output = con_get_output(focused);
237     if (old_output != new_output) {
238         x_set_warp_to(&next->rect);
239     }
240
241     /* Update the EWMH hints */
242     if (changed_num_workspaces)
243         ewmh_update_workarea();
244     ewmh_update_current_desktop();
245
246     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
247 }
248
249 /*
250  * Switches to the given workspace
251  *
252  */
253 void workspace_show(Con *workspace) {
254     _workspace_show(workspace, false);
255 }
256
257 /*
258  * Looks up the workspace by name and switches to it.
259  *
260  */
261 void workspace_show_by_name(const char *num) {
262     Con *workspace;
263     bool changed_num_workspaces;
264     workspace = workspace_get(num, &changed_num_workspaces);
265     _workspace_show(workspace, changed_num_workspaces);
266 }
267
268 /*
269  * Focuses the next workspace.
270  *
271  */
272 Con* workspace_next() {
273     Con *current = con_get_workspace(focused);
274     Con *next = NULL;
275     Con *output;
276
277     if (current->num == -1) {
278         /* If currently a named workspace, find next named workspace. */
279         next = TAILQ_NEXT(current, nodes);
280     } else {
281         /* If currently a numbered workspace, find next numbered workspace. */
282         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
283             NODES_FOREACH(output_get_content(output)) {
284                 if (child->type != CT_WORKSPACE)
285                     continue;
286                 if (child->num == -1)
287                     break;
288                 /* Need to check child against current and next because we are
289                  * traversing multiple lists and thus are not guaranteed the
290                  * relative order between the list of workspaces. */
291                 if (current->num < child->num && (!next || child->num < next->num))
292                     next = child;
293             }
294     }
295
296     /* Find next named workspace. */
297     if (!next) {
298         bool found_current = false;
299         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
300             NODES_FOREACH(output_get_content(output)) {
301                 if (child->type != CT_WORKSPACE)
302                     continue;
303                 if (child == current) {
304                     found_current = 1;
305                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
306                     next = child;
307                     goto workspace_next_end;
308                 }
309             }
310     }
311
312     /* Find first workspace. */
313     if (!next) {
314         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
315             NODES_FOREACH(output_get_content(output)) {
316                 if (child->type != CT_WORKSPACE)
317                     continue;
318                 if (!next || (child->num != -1 && child->num < next->num))
319                     next = child;
320             }
321     }
322 workspace_next_end:
323     return next;
324 }
325
326 /*
327  * Focuses the previous workspace.
328  *
329  */
330 Con* workspace_prev() {
331     Con *current = con_get_workspace(focused);
332     Con *prev = NULL;
333     Con *output;
334
335     if (current->num == -1) {
336         /* If named workspace, find previous named workspace. */
337         prev = TAILQ_PREV(current, nodes_head, nodes);
338         if (prev && prev->num != -1)
339             prev = NULL;
340     } else {
341         /* If numbered workspace, find previous numbered workspace. */
342         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
343             NODES_FOREACH_REVERSE(output_get_content(output)) {
344                 if (child->type != CT_WORKSPACE || child->num == -1)
345                     continue;
346                 /* Need to check child against current and previous because we
347                  * are traversing multiple lists and thus are not guaranteed
348                  * the relative order between the list of workspaces. */
349                 if (current->num > child->num && (!prev || child->num > prev->num))
350                     prev = child;
351             }
352     }
353
354     /* Find previous named workspace. */
355     if (!prev) {
356         bool found_current = false;
357         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
358             NODES_FOREACH_REVERSE(output_get_content(output)) {
359                 if (child->type != CT_WORKSPACE)
360                     continue;
361                 if (child == current) {
362                     found_current = 1;
363                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
364                     prev = child;
365                     goto workspace_prev_end;
366                 }
367             }
368     }
369
370     /* Find last workspace. */
371     if (!prev) {
372         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
373             NODES_FOREACH_REVERSE(output_get_content(output)) {
374                 if (child->type != CT_WORKSPACE)
375                     continue;
376                 if (!prev || child->num > prev->num)
377                     prev = child;
378             }
379     }
380
381 workspace_prev_end:
382     return prev;
383 }
384
385 /*
386  * Focuses the previously focused workspace.
387  *
388  */
389 void workspace_back_and_forth() {
390     if (!previous_workspace_name) {
391         DLOG("No previous workspace name set. Not switching.");
392         return;
393     }
394
395     workspace_show_by_name(previous_workspace_name);
396 }
397
398 static bool get_urgency_flag(Con *con) {
399     Con *child;
400     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
401         if (child->urgent || get_urgency_flag(child))
402             return true;
403
404     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
405         if (child->urgent || get_urgency_flag(child))
406             return true;
407
408     return false;
409 }
410
411 /*
412  * Goes through all clients on the given workspace and updates the workspace’s
413  * urgent flag accordingly.
414  *
415  */
416 void workspace_update_urgent_flag(Con *ws) {
417     bool old_flag = ws->urgent;
418     ws->urgent = get_urgency_flag(ws);
419     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
420
421     if (old_flag != ws->urgent)
422         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
423 }
424
425 /*
426  * 'Forces' workspace orientation by moving all cons into a new split-con with
427  * the same orientation as the workspace and then changing the workspace
428  * orientation.
429  *
430  */
431 void ws_force_orientation(Con *ws, orientation_t orientation) {
432     /* 1: create a new split container */
433     Con *split = con_new(NULL, NULL);
434     split->parent = ws;
435
436     /* 2: copy layout and orientation from workspace */
437     split->layout = ws->layout;
438     split->orientation = ws->orientation;
439
440     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
441
442     /* 3: move the existing cons of this workspace below the new con */
443     DLOG("Moving cons\n");
444     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
445         Con *child = TAILQ_FIRST(&(ws->nodes_head));
446         con_detach(child);
447         con_attach(child, split, true);
448     }
449
450     /* 4: switch workspace orientation */
451     ws->orientation = orientation;
452
453     /* 5: attach the new split container to the workspace */
454     DLOG("Attaching new split to ws\n");
455     con_attach(split, ws, false);
456
457     /* 6: fix the percentages */
458     con_fix_percent(ws);
459
460     if (old_focused)
461         con_focus(old_focused);
462 }
463
464 /*
465  * Called when a new con (with a window, not an empty or split con) should be
466  * attached to the workspace (for example when managing a new window or when
467  * moving an existing window to the workspace level).
468  *
469  * Depending on the workspace_layout setting, this function either returns the
470  * workspace itself (default layout) or creates a new stacked/tabbed con and
471  * returns that.
472  *
473  */
474 Con *workspace_attach_to(Con *ws) {
475     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
476
477     if (config.default_layout == L_DEFAULT) {
478         DLOG("Default layout, just attaching it to the workspace itself.\n");
479         return ws;
480     }
481
482     DLOG("Non-default layout, creating a new split container\n");
483     /* 1: create a new split container */
484     Con *new = con_new(NULL, NULL);
485     new->parent = ws;
486
487     /* 2: set the requested layout on the split con */
488     new->layout = config.default_layout;
489
490     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
491      * to be set. Otherwise, this con will not be interpreted as a split
492      * container. */
493     if (config.default_orientation == NO_ORIENTATION) {
494         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
495     } else {
496         new->orientation = config.default_orientation;
497     }
498
499     /* 4: attach the new split container to the workspace */
500     DLOG("Attaching new split %p to workspace %p\n", new, ws);
501     con_attach(new, ws, false);
502
503     return new;
504 }