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