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