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