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