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