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