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