]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
63e079f90f1ff463861d2ab118878003a6bf496e
[i3/i3] / src / workspace.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * workspace.c: Functions for modifying workspaces
11  *
12  */
13 #include <limits.h>
14
15 #include "all.h"
16
17 extern Con *focused;
18
19 /*
20  * Returns a pointer to the workspace with the given number (starting at 0),
21  * creating the workspace if necessary (by allocating the necessary amount of
22  * memory and initializing the data structures correctly).
23  *
24  */
25 Con *workspace_get(const char *num) {
26         Con *output, *workspace = NULL, *current;
27
28         /* TODO: could that look like this in the future?
29         GET_MATCHING_NODE(workspace, croot, strcasecmp(current->name, num) != 0);
30         */
31         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
32                 TAILQ_FOREACH(current, &(output->nodes_head), nodes) {
33                         if (strcasecmp(current->name, num) != 0)
34                                 continue;
35
36                         workspace = current;
37                         break;
38                 }
39         }
40
41         LOG("should switch to ws %s\n", num);
42         if (workspace == NULL) {
43                 LOG("need to create this one\n");
44                 output = con_get_output(focused);
45                 LOG("got output %p\n", output);
46                 workspace = con_new(output);
47                 workspace->name = strdup(num);
48
49                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
50         }
51
52         //ewmh_update_workarea();
53
54         return workspace;
55 }
56
57 #if 0
58
59 /*
60  * Sets the name (or just its number) for the given workspace. This has to
61  * be called for every workspace as the rendering function
62  * (render_internal_bar) relies on workspace->name and workspace->name_len
63  * being ready-to-use.
64  *
65  */
66 void workspace_set_name(Workspace *ws, const char *name) {
67         char *label;
68         int ret;
69
70         if (name != NULL)
71                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
72         else ret = asprintf(&label, "%d", ws->num + 1);
73
74         if (ret == -1)
75                 errx(1, "asprintf() failed");
76
77         FREE(ws->name);
78         FREE(ws->utf8_name);
79
80         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
81         if (config.font != NULL)
82                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
83         else ws->text_width = 0;
84         ws->utf8_name = label;
85 }
86
87 /*
88  * Returns true if the workspace is currently visible. Especially important for
89  * multi-monitor environments, as they can have multiple currenlty active
90  * workspaces.
91  *
92  */
93 bool workspace_is_visible(Workspace *ws) {
94         return (ws->output != NULL && ws->output->current_workspace == ws);
95 }
96 #endif
97
98
99 /*
100  * Switches to the given workspace
101  *
102  */
103 void workspace_show(const char *num) {
104         Con *workspace, *current;
105
106         workspace = workspace_get(num);
107         workspace->fullscreen_mode = CF_OUTPUT;
108         /* disable fullscreen */
109         TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes)
110                 current->fullscreen_mode = CF_NONE;
111
112         LOG("switching to %p\n", workspace);
113         Con *next = workspace;
114
115     while (!TAILQ_EMPTY(&(next->focus_head)))
116         next = TAILQ_FIRST(&(next->focus_head));
117
118         con_focus(next);
119         workspace->fullscreen_mode = CF_OUTPUT;
120         LOG("focused now = %p / %s\n", focused, focused->name);
121 #if 0
122
123         /* Check if the workspace has not been used yet */
124         workspace_initialize(t_ws, c_ws->output, false);
125
126         if (c_ws->output != t_ws->output) {
127                 /* We need to switch to the other output first */
128                 DLOG("moving over to other output.\n");
129
130                 /* Store the old client */
131                 Client *old_client = CUR_CELL->currently_focused;
132
133                 c_ws = t_ws->output->current_workspace;
134                 current_col = c_ws->current_col;
135                 current_row = c_ws->current_row;
136                 if (CUR_CELL->currently_focused != NULL)
137                         need_warp = true;
138                 else {
139                         Rect *dims = &(c_ws->output->rect);
140                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
141                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
142                 }
143
144                 /* Re-decorate the old client, it’s not focused anymore */
145                 if ((old_client != NULL) && !old_client->dock)
146                         redecorate_window(conn, old_client);
147                 else xcb_flush(conn);
148
149                 /* We need to check if a global fullscreen-client is blocking
150                  * the t_ws and if necessary switch that to local fullscreen */
151                 Client* client = c_ws->fullscreen_client;
152                 if (client != NULL && client->workspace != c_ws) {
153                         if (c_ws->fullscreen_client->workspace != c_ws)
154                                 c_ws->fullscreen_client = NULL;
155                         client_enter_fullscreen(conn, client, false);
156                 }
157         }
158
159         /* Check if we need to change something or if we’re already there */
160         if (c_ws->output->current_workspace->num == (workspace-1)) {
161                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
162                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
163                         set_focus(conn, last_focused, true);
164                 if (need_warp) {
165                         client_warp_pointer_into(conn, last_focused);
166                         xcb_flush(conn);
167                 }
168
169                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
170
171                 return;
172         }
173
174         Workspace *old_workspace = c_ws;
175         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
176
177         /* Unmap all clients of the old workspace */
178         workspace_unmap_clients(conn, old_workspace);
179
180         current_row = c_ws->current_row;
181         current_col = c_ws->current_col;
182         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
183
184         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
185
186         workspace_map_clients(conn, c_ws);
187
188         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
189          * render_layout afterwards, there is a short flickering on the source
190          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
191          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
192          * flickering). */
193
194         /* Restore focus on the new workspace */
195         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
196         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
197                 set_focus(conn, last_focused, true);
198         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
199
200         render_layout(conn);
201
202         /* We can warp the pointer only after the window has been
203          * reconfigured in render_layout, otherwise the pointer will
204          * be warped to the old position, which will not work when we
205          * moved it to another output. */
206         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
207                 client_warp_pointer_into(conn, last_focused);
208                 xcb_flush(conn);
209         }
210 #endif
211 }
212
213 #if 0
214 /*
215  * Assigns the given workspace to the given output by correctly updating its
216  * state and reconfiguring all the clients on this workspace.
217  *
218  * This is called when initializing a output and when re-assigning it to a
219  * different output which just got available (if you configured it to be on
220  * output 1 and you just plugged in output 1).
221  *
222  */
223 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
224         Client *client;
225         bool empty = true;
226         bool visible = workspace_is_visible(ws);
227
228         ws->output = output;
229
230         /* Copy the dimensions from the virtual output */
231         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
232
233         ewmh_update_workarea();
234
235         /* Force reconfiguration for each client on that workspace */
236         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
237                 client->force_reconfigure = true;
238                 empty = false;
239         }
240
241         if (empty)
242                 return;
243
244         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
245         render_workspace(global_conn, output, ws);
246
247         /* …unless we want to see them at the moment, we should hide that workspace */
248         if (visible && !hide_it)
249                 return;
250
251         /* however, if this is the current workspace, we only need to adjust
252          * the output’s current_workspace pointer (and must not unmap the
253          * windows) */
254         if (c_ws == ws) {
255                 DLOG("Need to adjust output->current_workspace...\n");
256                 output->current_workspace = c_ws;
257                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
258                 return;
259         }
260
261         workspace_unmap_clients(global_conn, ws);
262 }
263
264 /*
265  * Initializes the given workspace if it is not already initialized. The given
266  * screen is to be understood as a fallback, if the workspace itself either
267  * was not assigned to a particular screen or cannot be placed there because
268  * the screen is not attached at the moment.
269  *
270  */
271 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
272         Output *old_output;
273
274         if (ws->output != NULL && !recheck) {
275                 DLOG("Workspace already initialized\n");
276                 return;
277         }
278
279         old_output = ws->output;
280
281         /* If this workspace has no preferred output or if the output it wants
282          * to be on is not available at the moment, we initialize it with
283          * the output which was given */
284         if (ws->preferred_output == NULL ||
285             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
286                 ws->output = output;
287
288         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
289         /* If the assignment did not change, we do not need to update anything */
290         if (old_output != NULL && ws->output == old_output)
291                 return;
292
293         workspace_assign_to(ws, ws->output, false);
294 }
295
296 /*
297  * Gets the first unused workspace for the given screen, taking into account
298  * the preferred_output setting of every workspace (workspace assignments).
299  *
300  */
301 Workspace *get_first_workspace_for_output(Output *output) {
302         Workspace *result = NULL;
303
304         Workspace *ws;
305         TAILQ_FOREACH(ws, workspaces, workspaces) {
306                 if (ws->preferred_output == NULL ||
307                     get_output_by_name(ws->preferred_output) != output)
308                         continue;
309
310                 result = ws;
311                 break;
312         }
313
314         if (result == NULL) {
315                 /* No assignment found, returning first unused workspace */
316                 TAILQ_FOREACH(ws, workspaces, workspaces) {
317                         if (ws->output != NULL)
318                                 continue;
319
320                         result = ws;
321                         break;
322                 }
323         }
324
325         if (result == NULL) {
326                 DLOG("No existing free workspace found to assign, creating a new one\n");
327
328                 int last_ws = 0;
329                 TAILQ_FOREACH(ws, workspaces, workspaces)
330                         last_ws = ws->num;
331                 result = workspace_get(last_ws + 1);
332         }
333
334         workspace_initialize(result, output, false);
335         return result;
336 }
337
338 /*
339  * Maps all clients (and stack windows) of the given workspace.
340  *
341  */
342 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
343         Client *client;
344
345         ignore_enter_notify_forall(conn, ws, true);
346
347         /* Map all clients on the new workspace */
348         FOR_TABLE(ws)
349                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
350                         client_map(conn, client);
351
352         /* Map all floating clients */
353         if (!ws->floating_hidden)
354                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
355                         client_map(conn, client);
356
357         /* Map all stack windows, if any */
358         struct Stack_Window *stack_win;
359         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
360                 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
361                         xcb_map_window(conn, stack_win->window);
362
363         ignore_enter_notify_forall(conn, ws, false);
364 }
365
366 /*
367  * Unmaps all clients (and stack windows) of the given workspace.
368  *
369  * This needs to be called separately when temporarily rendering
370  * a workspace which is not the active workspace to force
371  * reconfiguration of all clients, like in src/xinerama.c when
372  * re-assigning a workspace to another screen.
373  *
374  */
375 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
376         Client *client;
377         struct Stack_Window *stack_win;
378
379         /* Ignore notify events because they would cause focus to be changed */
380         ignore_enter_notify_forall(conn, u_ws, true);
381
382         /* Unmap all clients of the given workspace */
383         int unmapped_clients = 0;
384         FOR_TABLE(u_ws)
385                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
386                         DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
387                         client_unmap(conn, client);
388                         unmapped_clients++;
389                 }
390
391         /* To find floating clients, we traverse the focus stack */
392         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
393                 if (!client_is_floating(client))
394                         continue;
395
396                 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
397
398                 client_unmap(conn, client);
399                 unmapped_clients++;
400         }
401
402         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
403          * if it is not the current workspace. */
404         if (unmapped_clients == 0 && u_ws != c_ws) {
405                 /* Re-assign the workspace of all dock clients which use this workspace */
406                 Client *dock;
407                 DLOG("workspace %p is empty\n", u_ws);
408                 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
409                         if (dock->workspace != u_ws)
410                                 continue;
411
412                         DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
413                         dock->workspace = c_ws;
414                 }
415                 u_ws->output = NULL;
416         }
417
418         /* Unmap the stack windows on the given workspace, if any */
419         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
420                 if (stack_win->container->workspace == u_ws)
421                         xcb_unmap_window(conn, stack_win->window);
422
423         ignore_enter_notify_forall(conn, u_ws, false);
424 }
425
426 /*
427  * Goes through all clients on the given workspace and updates the workspace’s
428  * urgent flag accordingly.
429  *
430  */
431 void workspace_update_urgent_flag(Workspace *ws) {
432         Client *current;
433         bool old_flag = ws->urgent;
434         bool urgent = false;
435
436         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
437                 if (!current->urgent)
438                         continue;
439
440                 urgent = true;
441                 break;
442         }
443
444         ws->urgent = urgent;
445
446         if (old_flag != urgent)
447                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
448 }
449
450 /*
451  * Returns the width of the workspace.
452  *
453  */
454 int workspace_width(Workspace *ws) {
455         return ws->rect.width;
456 }
457
458 /*
459  * Returns the effective height of the workspace (without the internal bar and
460  * without dock clients).
461  *
462  */
463 int workspace_height(Workspace *ws) {
464         int height = ws->rect.height;
465         i3Font *font = load_font(global_conn, config.font);
466
467         /* Reserve space for dock clients */
468         Client *client;
469         SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
470                 height -= client->desired_height;
471
472         /* Space for the internal bar */
473         if (!config.disable_workspace_bar)
474                 height -= (font->height + 6);
475
476         return height;
477 }
478 #endif