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