]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
4fcdd7473ec46958a74287bc393867b749d222e8
[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 <limits.h>
11
12 #include "all.h"
13
14 /*
15  * Returns a pointer to the workspace with the given number (starting at 0),
16  * creating the workspace if necessary (by allocating the necessary amount of
17  * memory and initializing the data structures correctly).
18  *
19  */
20 Con *workspace_get(const char *num) {
21     Con *output, *workspace = NULL, *current;
22
23     /* TODO: could that look like this in the future?
24     GET_MATCHING_NODE(workspace, croot, strcasecmp(current->name, num) != 0);
25     */
26     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
27         TAILQ_FOREACH(current, &(output->nodes_head), nodes) {
28             if (strcasecmp(current->name, num) != 0)
29                 continue;
30
31             workspace = current;
32             break;
33         }
34     }
35
36     LOG("getting ws %s\n", num);
37     if (workspace == NULL) {
38         LOG("need to create this one\n");
39         output = con_get_output(focused);
40         LOG("got output %p\n", output);
41         /* We need to attach this container after setting its type. con_attach
42          * will handle CT_WORKSPACEs differently */
43         workspace = con_new(NULL);
44         char *name;
45         asprintf(&name, "[i3 con] workspace %s", num);
46         x_set_name(workspace, name);
47         free(name);
48         workspace->type = CT_WORKSPACE;
49         FREE(workspace->name);
50         workspace->name = sstrdup(num);
51         /* We set ->num to the number if this workspace’s name consists only of
52          * a positive number. Otherwise it’s a named ws and num will be -1. */
53         char *end;
54         long parsed_num = strtol(num, &end, 10);
55         if (parsed_num == LONG_MIN ||
56             parsed_num == LONG_MAX ||
57             parsed_num < 0 ||
58             (end && *end != '\0'))
59             workspace->num = -1;
60         else workspace->num = parsed_num;
61         LOG("num = %d\n", workspace->num);
62         workspace->orientation = HORIZ;
63         con_attach(workspace, output, false);
64
65         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
66     }
67
68     //ewmh_update_workarea();
69
70     return workspace;
71 }
72
73 #if 0
74
75 /*
76  * Sets the name (or just its number) for the given workspace. This has to
77  * be called for every workspace as the rendering function
78  * (render_internal_bar) relies on workspace->name and workspace->name_len
79  * being ready-to-use.
80  *
81  */
82 void workspace_set_name(Workspace *ws, const char *name) {
83         char *label;
84         int ret;
85
86         if (name != NULL)
87                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
88         else ret = asprintf(&label, "%d", ws->num + 1);
89
90         if (ret == -1)
91                 errx(1, "asprintf() failed");
92
93         FREE(ws->name);
94         FREE(ws->utf8_name);
95
96         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
97         if (config.font != NULL)
98                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
99         else ws->text_width = 0;
100         ws->utf8_name = label;
101 }
102 #endif
103
104 /*
105  * Returns true if the workspace is currently visible. Especially important for
106  * multi-monitor environments, as they can have multiple currenlty active
107  * workspaces.
108  *
109  */
110 bool workspace_is_visible(Con *ws) {
111     Con *output = con_get_output(ws);
112     if (output == NULL)
113         return false;
114     Con *fs = con_get_fullscreen_con(output);
115     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
116     return (fs == ws);
117 }
118
119 /*
120  * XXX: we need to clean up all this recursive walking code.
121  *
122  */
123 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
124     Con *current;
125
126     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
127         if (current != exclude &&
128             current->sticky_group != NULL &&
129             current->window != NULL &&
130             strcmp(current->sticky_group, sticky_group) == 0)
131             return current;
132
133         Con *recurse = _get_sticky(current, sticky_group, exclude);
134         if (recurse != NULL)
135             return recurse;
136     }
137
138     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
139         if (current != exclude &&
140             current->sticky_group != NULL &&
141             current->window != NULL &&
142             strcmp(current->sticky_group, sticky_group) == 0)
143             return current;
144
145         Con *recurse = _get_sticky(current, sticky_group, exclude);
146         if (recurse != NULL)
147             return recurse;
148     }
149
150     return NULL;
151 }
152
153 /*
154  * Reassigns all child windows in sticky containers. Called when the user
155  * changes workspaces.
156  *
157  * XXX: what about sticky containers which contain containers?
158  *
159  */
160 static void workspace_reassign_sticky(Con *con) {
161     Con *current;
162     /* 1: go through all containers */
163
164     /* handle all children and floating windows of this node */
165     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
166         if (current->sticky_group == NULL) {
167             workspace_reassign_sticky(current);
168             continue;
169         }
170
171         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
172         /* 2: find a window which we can re-assign */
173         Con *output = con_get_output(current);
174         Con *src = _get_sticky(output, current->sticky_group, current);
175
176         if (src == NULL) {
177             LOG("No window found for this sticky group\n");
178             workspace_reassign_sticky(current);
179             continue;
180         }
181
182         x_move_win(src, current);
183         current->window = src->window;
184         current->mapped = true;
185         src->window = NULL;
186         src->mapped = false;
187
188         x_reparent_child(current, src);
189
190         LOG("re-assigned window from src %p to dest %p\n", src, current);
191     }
192
193     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
194         workspace_reassign_sticky(current);
195 }
196
197 /*
198  * Switches to the given workspace
199  *
200  */
201 void workspace_show(const char *num) {
202     Con *workspace, *current, *old;
203
204     workspace = workspace_get(num);
205
206     /* disable fullscreen for the other workspaces and get the workspace we are
207      * currently on. */
208     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
209         if (current->fullscreen_mode == CF_OUTPUT)
210             old = current;
211         current->fullscreen_mode = CF_NONE;
212     }
213
214     /* Check if the the currently focused con is on the same Output as the
215      * workspace we chose as 'old'. If not, use the workspace of the currently
216      * focused con */
217     if (con_get_workspace(focused)->parent != old->parent)
218         old = con_get_workspace(focused);
219
220     /* enable fullscreen for the target workspace. If it happens to be the
221      * same one we are currently on anyways, we can stop here. */
222     workspace->fullscreen_mode = CF_OUTPUT;
223     if (workspace == old)
224         return;
225
226     workspace_reassign_sticky(workspace);
227
228     LOG("switching to %p\n", workspace);
229     Con *next = con_descend_focused(workspace);
230
231     if (TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
232         /* check if this workspace is currently visible */
233         if (!workspace_is_visible(old)) {
234             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
235             tree_close(old, false, false);
236             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
237         }
238     }
239
240     con_focus(next);
241     workspace->fullscreen_mode = CF_OUTPUT;
242     LOG("focused now = %p / %s\n", focused, focused->name);
243
244     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
245 #if 0
246
247         /* Check if the workspace has not been used yet */
248         workspace_initialize(t_ws, c_ws->output, false);
249
250         if (c_ws->output != t_ws->output) {
251                 /* We need to switch to the other output first */
252                 DLOG("moving over to other output.\n");
253
254                 /* Store the old client */
255                 Client *old_client = CUR_CELL->currently_focused;
256
257                 c_ws = t_ws->output->current_workspace;
258                 current_col = c_ws->current_col;
259                 current_row = c_ws->current_row;
260                 if (CUR_CELL->currently_focused != NULL)
261                         need_warp = true;
262                 else {
263                         Rect *dims = &(c_ws->output->rect);
264                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
265                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
266                 }
267
268                 /* Re-decorate the old client, it’s not focused anymore */
269                 if ((old_client != NULL) && !old_client->dock)
270                         redecorate_window(conn, old_client);
271                 else xcb_flush(conn);
272
273                 /* We need to check if a global fullscreen-client is blocking
274                  * the t_ws and if necessary switch that to local fullscreen */
275                 Client* client = c_ws->fullscreen_client;
276                 if (client != NULL && client->workspace != c_ws) {
277                         if (c_ws->fullscreen_client->workspace != c_ws)
278                                 c_ws->fullscreen_client = NULL;
279                         client_enter_fullscreen(conn, client, false);
280                 }
281         }
282
283         /* Check if we need to change something or if we’re already there */
284         if (c_ws->output->current_workspace->num == (workspace-1)) {
285                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
286                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
287                         set_focus(conn, last_focused, true);
288                 if (need_warp) {
289                         client_warp_pointer_into(conn, last_focused);
290                         xcb_flush(conn);
291                 }
292
293                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
294
295                 return;
296         }
297
298         Workspace *old_workspace = c_ws;
299         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
300
301         /* Unmap all clients of the old workspace */
302         workspace_unmap_clients(conn, old_workspace);
303
304         current_row = c_ws->current_row;
305         current_col = c_ws->current_col;
306         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
307
308         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
309
310         workspace_map_clients(conn, c_ws);
311
312         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
313          * render_layout afterwards, there is a short flickering on the source
314          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
315          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
316          * flickering). */
317
318         /* Restore focus on the new workspace */
319         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
320         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
321                 set_focus(conn, last_focused, true);
322         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
323
324         render_layout(conn);
325
326         /* We can warp the pointer only after the window has been
327          * reconfigured in render_layout, otherwise the pointer will
328          * be warped to the old position, which will not work when we
329          * moved it to another output. */
330         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
331                 client_warp_pointer_into(conn, last_focused);
332                 xcb_flush(conn);
333         }
334 #endif
335 }
336
337 #if 0
338 /*
339  * Assigns the given workspace to the given output by correctly updating its
340  * state and reconfiguring all the clients on this workspace.
341  *
342  * This is called when initializing a output and when re-assigning it to a
343  * different output which just got available (if you configured it to be on
344  * output 1 and you just plugged in output 1).
345  *
346  */
347 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
348         Client *client;
349         bool empty = true;
350         bool visible = workspace_is_visible(ws);
351
352         ws->output = output;
353
354         /* Copy the dimensions from the virtual output */
355         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
356
357         ewmh_update_workarea();
358
359         /* Force reconfiguration for each client on that workspace */
360         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
361                 client->force_reconfigure = true;
362                 empty = false;
363         }
364
365         if (empty)
366                 return;
367
368         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
369         render_workspace(global_conn, output, ws);
370
371         /* …unless we want to see them at the moment, we should hide that workspace */
372         if (visible && !hide_it)
373                 return;
374
375         /* however, if this is the current workspace, we only need to adjust
376          * the output’s current_workspace pointer (and must not unmap the
377          * windows) */
378         if (c_ws == ws) {
379                 DLOG("Need to adjust output->current_workspace...\n");
380                 output->current_workspace = c_ws;
381                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
382                 return;
383         }
384
385         workspace_unmap_clients(global_conn, ws);
386 }
387
388 /*
389  * Initializes the given workspace if it is not already initialized. The given
390  * screen is to be understood as a fallback, if the workspace itself either
391  * was not assigned to a particular screen or cannot be placed there because
392  * the screen is not attached at the moment.
393  *
394  */
395 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
396         Output *old_output;
397
398         if (ws->output != NULL && !recheck) {
399                 DLOG("Workspace already initialized\n");
400                 return;
401         }
402
403         old_output = ws->output;
404
405         /* If this workspace has no preferred output or if the output it wants
406          * to be on is not available at the moment, we initialize it with
407          * the output which was given */
408         if (ws->preferred_output == NULL ||
409             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
410                 ws->output = output;
411
412         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
413         /* If the assignment did not change, we do not need to update anything */
414         if (old_output != NULL && ws->output == old_output)
415                 return;
416
417         workspace_assign_to(ws, ws->output, false);
418 }
419
420 /*
421  * Gets the first unused workspace for the given screen, taking into account
422  * the preferred_output setting of every workspace (workspace assignments).
423  *
424  */
425 Workspace *get_first_workspace_for_output(Output *output) {
426         Workspace *result = NULL;
427
428         Workspace *ws;
429         TAILQ_FOREACH(ws, workspaces, workspaces) {
430                 if (ws->preferred_output == NULL ||
431                     get_output_by_name(ws->preferred_output) != output)
432                         continue;
433
434                 result = ws;
435                 break;
436         }
437
438         if (result == NULL) {
439                 /* No assignment found, returning first unused workspace */
440                 TAILQ_FOREACH(ws, workspaces, workspaces) {
441                         if (ws->output != NULL)
442                                 continue;
443
444                         result = ws;
445                         break;
446                 }
447         }
448
449         if (result == NULL) {
450                 DLOG("No existing free workspace found to assign, creating a new one\n");
451
452                 int last_ws = 0;
453                 TAILQ_FOREACH(ws, workspaces, workspaces)
454                         last_ws = ws->num;
455                 result = workspace_get(last_ws + 1);
456         }
457
458         workspace_initialize(result, output, false);
459         return result;
460 }
461
462 #endif
463
464 static bool get_urgency_flag(Con *con) {
465     Con *child;
466     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
467         if (child->urgent || get_urgency_flag(child))
468             return true;
469
470     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
471         if (child->urgent || get_urgency_flag(child))
472             return true;
473
474     return false;
475 }
476
477 /*
478  * Goes through all clients on the given workspace and updates the workspace’s
479  * urgent flag accordingly.
480  *
481  */
482 void workspace_update_urgent_flag(Con *ws) {
483     bool old_flag = ws->urgent;
484     ws->urgent = get_urgency_flag(ws);
485     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
486
487     if (old_flag != ws->urgent)
488         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
489 }
490
491 /*
492  * 'Forces' workspace orientation by moving all cons into a new split-con with
493  * the same orientation as the workspace and then changing the workspace
494  * orientation.
495  *
496  */
497 void ws_force_orientation(Con *ws, orientation_t orientation) {
498     /* 1: create a new split container */
499     Con *split = con_new(NULL);
500     split->parent = ws;
501
502     /* 2: copy layout and orientation from workspace */
503     split->layout = ws->layout;
504     split->orientation = ws->orientation;
505
506     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
507
508     /* 3: move the existing cons of this workspace below the new con */
509     DLOG("Moving cons\n");
510     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
511         Con *child = TAILQ_FIRST(&(ws->nodes_head));
512         con_detach(child);
513         con_attach(child, split, true);
514     }
515
516     /* 4: switch workspace orientation */
517     ws->orientation = orientation;
518
519     /* 5: attach the new split container to the workspace */
520     DLOG("Attaching new split to ws\n");
521     con_attach(split, ws, false);
522
523     /* 6: fix the percentages */
524     con_fix_percent(ws);
525
526     if (old_focused)
527         con_focus(old_focused);
528 }