]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
rename the internal function to _workspace_show, add a comment to workspace_show_by_name
[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 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
181     Con *current, *old = NULL;
182
183     /* disable fullscreen for the other workspaces and get the workspace we are
184      * currently on. */
185     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
186         if (current->fullscreen_mode == CF_OUTPUT)
187             old = current;
188         current->fullscreen_mode = CF_NONE;
189     }
190
191     /* enable fullscreen for the target workspace. If it happens to be the
192      * same one we are currently on anyways, we can stop here. */
193     workspace->fullscreen_mode = CF_OUTPUT;
194     if (workspace == con_get_workspace(focused)) {
195         DLOG("Not switching, already there.\n");
196         return;
197     }
198
199     workspace_reassign_sticky(workspace);
200
201     LOG("switching to %p\n", workspace);
202     Con *next = con_descend_focused(workspace);
203
204     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
205         /* check if this workspace is currently visible */
206         if (!workspace_is_visible(old)) {
207             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
208             tree_close(old, DONT_KILL_WINDOW, false, false);
209             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
210             changed_num_workspaces = true;
211         }
212     }
213
214     /* Memorize current output */
215     Con *old_output = con_get_output(focused);
216
217     con_focus(next);
218     workspace->fullscreen_mode = CF_OUTPUT;
219     LOG("focused now = %p / %s\n", focused, focused->name);
220
221     /* Set mouse pointer */
222     Con *new_output = con_get_output(focused);
223     if (old_output != new_output) {
224         x_set_warp_to(&next->rect);
225     }
226
227     /* Update the EWMH hints */
228     if (changed_num_workspaces)
229         ewmh_update_workarea();
230     ewmh_update_current_desktop();
231
232     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
233 }
234
235 /*
236  * Switches to the given workspace
237  *
238  */
239 void workspace_show(Con *workspace) {
240     _workspace_show(workspace, false);
241 }
242
243 /*
244  * Looks up the workspace by name and switches to it.
245  *
246  */
247 void workspace_show_by_name(const char *num) {
248     Con *workspace;
249     bool changed_num_workspaces;
250     workspace = workspace_get(num, &changed_num_workspaces);
251     _workspace_show(workspace, changed_num_workspaces);
252 }
253
254 /*
255  * Focuses the next workspace.
256  *
257  */
258 Con* workspace_next() {
259     Con *current = con_get_workspace(focused);
260     Con *next = NULL;
261     Con *output;
262
263     if (current->num == -1) {
264         /* If currently a named workspace, find next named workspace. */
265         next = TAILQ_NEXT(current, nodes);
266     } else {
267         /* If currently a numbered workspace, find next numbered workspace. */
268         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
269             NODES_FOREACH(output_get_content(output)) {
270                 if (child->type != CT_WORKSPACE)
271                     continue;
272                 if (child->num == -1)
273                     break;
274                 /* Need to check child against current and next because we are
275                  * traversing multiple lists and thus are not guaranteed the
276                  * relative order between the list of workspaces. */
277                 if (current->num < child->num && (!next || child->num < next->num))
278                     next = child;
279             }
280     }
281
282     /* Find next named workspace. */
283     if (!next) {
284         bool found_current = false;
285         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
286             NODES_FOREACH(output_get_content(output)) {
287                 if (child->type != CT_WORKSPACE)
288                     continue;
289                 if (child == current) {
290                     found_current = 1;
291                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
292                     next = child;
293                     goto workspace_next_end;
294                 }
295             }
296     }
297
298     /* Find first workspace. */
299     if (!next) {
300         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
301             NODES_FOREACH(output_get_content(output)) {
302                 if (child->type != CT_WORKSPACE)
303                     continue;
304                 if (!next || (child->num != -1 && child->num < next->num))
305                     next = child;
306             }
307     }
308 workspace_next_end:
309     return next;
310 }
311
312 /*
313  * Focuses the previous workspace.
314  *
315  */
316 Con* workspace_prev() {
317     Con *current = con_get_workspace(focused);
318     Con *prev = NULL;
319     Con *output;
320
321     if (current->num == -1) {
322         /* If named workspace, find previous named workspace. */
323         prev = TAILQ_PREV(current, nodes_head, nodes);
324         if (prev && prev->num != -1)
325             prev = NULL;
326     } else {
327         /* If numbered workspace, find previous numbered workspace. */
328         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
329             NODES_FOREACH_REVERSE(output_get_content(output)) {
330                 if (child->type != CT_WORKSPACE || child->num == -1)
331                     continue;
332                 /* Need to check child against current and previous because we
333                  * are traversing multiple lists and thus are not guaranteed
334                  * the relative order between the list of workspaces. */
335                 if (current->num > child->num && (!prev || child->num > prev->num))
336                     prev = child;
337             }
338     }
339
340     /* Find previous named workspace. */
341     if (!prev) {
342         bool found_current = false;
343         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
344             NODES_FOREACH_REVERSE(output_get_content(output)) {
345                 if (child->type != CT_WORKSPACE)
346                     continue;
347                 if (child == current) {
348                     found_current = 1;
349                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
350                     prev = child;
351                     goto workspace_prev_end;
352                 }
353             }
354     }
355
356     /* Find last workspace. */
357     if (!prev) {
358         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
359             NODES_FOREACH_REVERSE(output_get_content(output)) {
360                 if (child->type != CT_WORKSPACE)
361                     continue;
362                 if (!prev || child->num > prev->num)
363                     prev = child;
364             }
365     }
366
367 workspace_prev_end:
368     return prev;
369 }
370
371 static bool get_urgency_flag(Con *con) {
372     Con *child;
373     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
374         if (child->urgent || get_urgency_flag(child))
375             return true;
376
377     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
378         if (child->urgent || get_urgency_flag(child))
379             return true;
380
381     return false;
382 }
383
384 /*
385  * Goes through all clients on the given workspace and updates the workspace’s
386  * urgent flag accordingly.
387  *
388  */
389 void workspace_update_urgent_flag(Con *ws) {
390     bool old_flag = ws->urgent;
391     ws->urgent = get_urgency_flag(ws);
392     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
393
394     if (old_flag != ws->urgent)
395         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
396 }
397
398 /*
399  * 'Forces' workspace orientation by moving all cons into a new split-con with
400  * the same orientation as the workspace and then changing the workspace
401  * orientation.
402  *
403  */
404 void ws_force_orientation(Con *ws, orientation_t orientation) {
405     /* 1: create a new split container */
406     Con *split = con_new(NULL, NULL);
407     split->parent = ws;
408
409     /* 2: copy layout and orientation from workspace */
410     split->layout = ws->layout;
411     split->orientation = ws->orientation;
412
413     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
414
415     /* 3: move the existing cons of this workspace below the new con */
416     DLOG("Moving cons\n");
417     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
418         Con *child = TAILQ_FIRST(&(ws->nodes_head));
419         con_detach(child);
420         con_attach(child, split, true);
421     }
422
423     /* 4: switch workspace orientation */
424     ws->orientation = orientation;
425
426     /* 5: attach the new split container to the workspace */
427     DLOG("Attaching new split to ws\n");
428     con_attach(split, ws, false);
429
430     /* 6: fix the percentages */
431     con_fix_percent(ws);
432
433     if (old_focused)
434         con_focus(old_focused);
435 }
436
437 /*
438  * Called when a new con (with a window, not an empty or split con) should be
439  * attached to the workspace (for example when managing a new window or when
440  * moving an existing window to the workspace level).
441  *
442  * Depending on the workspace_layout setting, this function either returns the
443  * workspace itself (default layout) or creates a new stacked/tabbed con and
444  * returns that.
445  *
446  */
447 Con *workspace_attach_to(Con *ws) {
448     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
449
450     if (config.default_layout == L_DEFAULT) {
451         DLOG("Default layout, just attaching it to the workspace itself.\n");
452         return ws;
453     }
454
455     DLOG("Non-default layout, creating a new split container\n");
456     /* 1: create a new split container */
457     Con *new = con_new(NULL, NULL);
458     new->parent = ws;
459
460     /* 2: set the requested layout on the split con */
461     new->layout = config.default_layout;
462
463     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
464      * to be set. Otherwise, this con will not be interpreted as a split
465      * container. */
466     if (config.default_orientation == NO_ORIENTATION) {
467         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
468     } else {
469         new->orientation = config.default_orientation;
470     }
471
472     /* 4: attach the new split container to the workspace */
473     DLOG("Attaching new split %p to workspace %p\n", new, ws);
474     con_attach(new, ws, false);
475
476     return new;
477 }