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