4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * workspace.c: Functions for modifying workspaces
27 #include "workspace.h"
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).
36 Workspace *workspace_get(int number) {
37 if (number > (num_workspaces-1)) {
38 int old_num_workspaces = num_workspaces;
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.
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);
55 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
56 if (current->workspace == NULL)
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);
64 c_ws = (Workspace*)(c_ws->num);
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);
79 c_ws = workspace_get((int)c_ws);
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);
89 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
90 if (current->workspace == NULL)
92 LOG("Handling client %p with (num %d)\n", current, current->workspace);
93 current->workspace = workspace_get((int)current->workspace - 1);
101 return &(workspaces[number]);
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.
111 void workspace_set_name(Workspace *ws, const char *name) {
116 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
117 else ret = asprintf(&label, "%d", ws->num + 1);
120 errx(1, "asprintf() failed");
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;
133 * Returns true if the workspace is currently visible. Especially important for
134 * multi-monitor environments, as they can have multiple currenlty active
138 bool workspace_is_visible(Workspace *ws) {
139 return (ws->screen->current_workspace == ws->num);
143 * Switches to the given workspace
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);
152 LOG("show_workspace(%d)\n", workspace);
154 /* Store current_row/current_col */
155 c_ws->current_row = current_row;
156 c_ws->current_col = current_col;
158 /* Check if the workspace has not been used yet */
159 workspace_initialize(t_ws, c_ws->screen);
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");
165 /* Store the old client */
166 Client *old_client = CUR_CELL->currently_focused;
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)
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));
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);
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);
191 client_warp_pointer_into(conn, last_focused);
198 t_ws->screen->current_workspace = workspace-1;
199 Workspace *old_workspace = c_ws;
200 c_ws = workspace_get(workspace-1);
202 /* Unmap all clients of the old workspace */
203 workspace_unmap_clients(conn, old_workspace);
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);
209 workspace_map_clients(conn, c_ws);
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
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);
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);
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
245 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
248 int preferred_screen = strtol(preference, &rest, 10);
250 LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
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);
259 x = atoi(preference);
260 y = atoi(strchr(preference, 'x') + 1);
263 x = atoi(preference);
266 LOG("Looking for screen at %d x %d\n", x, y);
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);
279 TAILQ_FOREACH(screen, slist, screens)
280 if (c++ == preferred_screen)
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.
294 void workspace_initialize(Workspace *ws, i3Screen *screen) {
295 if (ws->screen != NULL) {
296 LOG("Workspace already initialized\n");
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)
306 else { LOG("yay, found assignment\n"); }
308 /* Copy the dimensions from the virtual screen */
309 memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
313 * Gets the first unused workspace for the given screen, taking into account
314 * the preferred_screen setting of every workspace (workspace assignments).
317 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
318 Workspace *result = NULL;
320 for (int c = 0; c < num_workspaces; c++) {
321 Workspace *ws = workspace_get(c);
322 if (ws->preferred_screen == NULL ||
323 !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
330 if (result == NULL) {
331 /* No assignment found, returning first unused workspace */
332 for (int c = 0; c < num_workspaces; c++) {
333 if (workspaces[c].screen != NULL)
336 result = workspace_get(c);
341 if (result != NULL) {
342 workspace_initialize(result, screen);
346 LOG("WARNING: No free workspace found to assign!\n");
351 * Maps all clients (and stack windows) of the given workspace.
354 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
357 ignore_enter_notify_forall(conn, ws, true);
359 /* Map all clients on the new workspace */
361 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
362 client_map(conn, client);
364 /* Map all floating clients */
365 if (!ws->floating_hidden)
366 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
367 client_map(conn, client);
369 /* Map all stack windows, if any */
370 struct Stack_Window *stack_win;
371 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
372 if (stack_win->container->workspace == ws)
373 xcb_map_window(conn, stack_win->window);
375 ignore_enter_notify_forall(conn, ws, false);
379 * Unmaps all clients (and stack windows) of the given workspace.
381 * This needs to be called separately when temporarily rendering
382 * a workspace which is not the active workspace to force
383 * reconfiguration of all clients, like in src/xinerama.c when
384 * re-assigning a workspace to another screen.
387 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
389 struct Stack_Window *stack_win;
391 /* Ignore notify events because they would cause focus to be changed */
392 ignore_enter_notify_forall(conn, u_ws, true);
394 /* Unmap all clients of the given workspace */
395 int unmapped_clients = 0;
397 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
398 LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
399 client_unmap(conn, client);
403 /* To find floating clients, we traverse the focus stack */
404 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
405 if (!client_is_floating(client))
408 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
410 client_unmap(conn, client);
414 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
415 * if it is not the current workspace. */
416 if (unmapped_clients == 0 && u_ws != c_ws) {
417 /* Re-assign the workspace of all dock clients which use this workspace */
419 LOG("workspace %p is empty\n", u_ws);
420 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
421 if (dock->workspace != u_ws)
424 LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
425 dock->workspace = c_ws;
430 /* Unmap the stack windows on the given workspace, if any */
431 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
432 if (stack_win->container->workspace == u_ws)
433 xcb_unmap_window(conn, stack_win->window);
435 ignore_enter_notify_forall(conn, u_ws, false);
439 * Goes through all clients on the given workspace and updates the workspace’s
440 * urgent flag accordingly.
443 void workspace_update_urgent_flag(Workspace *ws) {
446 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
447 if (!current->urgent)