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