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