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"
30 Workspace *workspace_get(int number) {
31 if (number > (num_workspaces-1)) {
32 int old_num_workspaces = num_workspaces;
34 /* Convert all container->workspace and client->workspace
35 * pointers to numbers representing their workspace. Necessary
36 * because the realloc() may make all the pointers invalid, so
37 * we need to preserve them this way and restore them later.
39 * To distinguish between the first workspace and a NULL
40 * pointer, we store <workspace number> + 1. */
41 for (int c = 0; c < num_workspaces; c++)
42 FOR_TABLE(&(workspaces[c])) {
43 Container *con = workspaces[c].table[cols][rows];
44 if (con->workspace != NULL) {
45 LOG("Handling con %p with pointer %p (num %d)\n", con, con->workspace, con->workspace->num);
46 con->workspace = (Workspace*)(con->workspace->num + 1);
49 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
50 if (current->workspace == NULL)
52 LOG("Handling client %p with pointer %p (num %d)\n", current, current->workspace, current->workspace->num);
53 current->workspace = (Workspace*)(current->workspace->num + 1);
58 c_ws = (Workspace*)(c_ws->num);
60 LOG("We need to initialize that one\n");
61 num_workspaces = number+1;
62 workspaces = realloc(workspaces, num_workspaces * sizeof(Workspace));
63 for (int c = old_num_workspaces; c < num_workspaces; c++) {
64 memset(&workspaces[c], 0, sizeof(Workspace));
65 workspaces[c].screen = NULL;
66 workspaces[c].num = c;
67 TAILQ_INIT(&(workspaces[c].floating_clients));
68 expand_table_cols(&(workspaces[c]));
69 expand_table_rows(&(workspaces[c]));
70 workspace_set_name(&(workspaces[c]), NULL);
73 c_ws = workspace_get((int)c_ws);
75 for (int c = 0; c < old_num_workspaces; c++)
76 FOR_TABLE(&(workspaces[c])) {
77 Container *con = workspaces[c].table[cols][rows];
78 if (con->workspace != NULL) {
79 LOG("Handling con %p with (num %d)\n", con, con->workspace);
80 con->workspace = workspace_get((int)con->workspace - 1);
83 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
84 if (current->workspace == NULL)
86 LOG("Handling client %p with (num %d)\n", current, current->workspace);
87 current->workspace = workspace_get((int)current->workspace - 1);
95 return &(workspaces[number]);
99 * Sets the name (or just its number) for the given workspace. This has to
100 * be called for every workspace as the rendering function
101 * (render_internal_bar) relies on workspace->name and workspace->name_len
102 * being ready-to-use.
105 void workspace_set_name(Workspace *ws, const char *name) {
110 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
111 else ret = asprintf(&label, "%d", ws->num + 1);
114 errx(1, "asprintf() failed");
118 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
119 if (config.font != NULL)
120 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
121 else ws->text_width = 0;
127 * Returns true if the workspace is currently visible. Especially important for
128 * multi-monitor environments, as they can have multiple currenlty active
132 bool workspace_is_visible(Workspace *ws) {
133 return (ws->screen->current_workspace == ws->num);
137 * Switches to the given workspace
140 void workspace_show(xcb_connection_t *conn, int workspace) {
141 bool need_warp = false;
142 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
143 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
144 Workspace *t_ws = workspace_get(workspace-1);
146 LOG("show_workspace(%d)\n", workspace);
148 /* Store current_row/current_col */
149 c_ws->current_row = current_row;
150 c_ws->current_col = current_col;
152 /* Check if the workspace has not been used yet */
153 workspace_initialize(t_ws, c_ws->screen);
155 if (c_ws->screen != t_ws->screen) {
156 /* We need to switch to the other screen first */
157 LOG("moving over to other screen.\n");
159 /* Store the old client */
160 Client *old_client = CUR_CELL->currently_focused;
162 c_ws = workspace_get(t_ws->screen->current_workspace);
163 current_col = c_ws->current_col;
164 current_row = c_ws->current_row;
165 if (CUR_CELL->currently_focused != NULL)
168 Rect *dims = &(c_ws->screen->rect);
169 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
170 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
173 /* Re-decorate the old client, it’s not focused anymore */
174 if ((old_client != NULL) && !old_client->dock)
175 redecorate_window(conn, old_client);
176 else xcb_flush(conn);
179 /* Check if we need to change something or if we’re already there */
180 if (c_ws->screen->current_workspace == (workspace-1)) {
181 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
182 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
183 set_focus(conn, last_focused, true);
185 client_warp_pointer_into(conn, last_focused);
192 t_ws->screen->current_workspace = workspace-1;
193 Workspace *old_workspace = c_ws;
194 c_ws = workspace_get(workspace-1);
196 /* Unmap all clients of the old workspace */
197 workspace_unmap_clients(conn, old_workspace);
199 current_row = c_ws->current_row;
200 current_col = c_ws->current_col;
201 LOG("new current row = %d, current col = %d\n", current_row, current_col);
203 workspace_map_clients(conn, c_ws);
205 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
206 * render_layout afterwards, there is a short flickering on the source
207 * workspace (assign ws 3 to screen 0, ws 4 to screen 1, create single
208 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
211 /* Restore focus on the new workspace */
212 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
213 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
214 set_focus(conn, last_focused, true);
215 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
219 /* We can warp the pointer only after the window has been
220 * reconfigured in render_layout, otherwise the pointer will
221 * be warped to the old position, which will not work when we
222 * moved it to another screen. */
223 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
224 client_warp_pointer_into(conn, last_focused);
231 * Parses the preferred_screen property of a workspace. You can either specify
232 * the screen number (it is not given that the screen numbering always stays
233 * the same) or the screen coordinates (exact coordinates, e.g. 1280 will match
234 * the screen starting at x=1280, but 1281 will not). For coordinates, you can
235 * either specify an x coordinate ("1280") or an y coordinate ("x800") or both
239 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
242 int preferred_screen = strtol(preference, &rest, 10);
244 LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
246 if ((rest == preference) || (preferred_screen >= num_screens)) {
247 int x = INT_MAX, y = INT_MAX;
248 if (strchr(preference, 'x') != NULL) {
249 /* Check if only the y coordinate was specified */
250 if (*preference == 'x')
251 y = atoi(preference+1);
253 x = atoi(preference);
254 y = atoi(strchr(preference, 'x') + 1);
257 x = atoi(preference);
260 LOG("Looking for screen at %d x %d\n", x, y);
262 TAILQ_FOREACH(screen, slist, screens)
263 if ((x == INT_MAX || screen->rect.x == x) &&
264 (y == INT_MAX || screen->rect.y == y)) {
265 LOG("found %p\n", screen);
273 TAILQ_FOREACH(screen, slist, screens)
274 if (c++ == preferred_screen)
282 * Initializes the given workspace if it is not already initialized. The given
283 * screen is to be understood as a fallback, if the workspace itself either
284 * was not assigned to a particular screen or cannot be placed there because
285 * the screen is not attached at the moment.
288 void workspace_initialize(Workspace *ws, i3Screen *screen) {
289 if (ws->screen != NULL) {
290 LOG("Workspace already initialized\n");
294 /* If this workspace has no preferred screen or if the screen it wants
295 * to be on is not available at the moment, we initialize it with
296 * the screen which was given */
297 if (ws->preferred_screen == NULL ||
298 (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
300 else { LOG("yay, found assignment\n"); }
302 /* Copy the dimensions from the virtual screen */
303 memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
307 * Gets the first unused workspace for the given screen, taking into account
308 * the preferred_screen setting of every workspace (workspace assignments).
311 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
312 Workspace *result = NULL;
314 for (int c = 0; c < num_workspaces; c++) {
315 Workspace *ws = workspace_get(c);
316 if (ws->preferred_screen == NULL ||
317 !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
324 if (result == NULL) {
325 /* No assignment found, returning first unused workspace */
326 for (int c = 0; c < num_workspaces; c++) {
327 if (workspaces[c].screen != NULL)
330 result = workspace_get(c);
335 if (result != NULL) {
336 workspace_initialize(result, screen);
340 LOG("WARNING: No free workspace found to assign!\n");
345 * Maps all clients (and stack windows) of the given workspace.
348 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
351 ignore_enter_notify_forall(conn, ws, true);
353 /* Map all clients on the new workspace */
355 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
356 client_map(conn, client);
358 /* Map all floating clients */
359 if (!ws->floating_hidden)
360 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
361 client_map(conn, client);
363 /* Map all stack windows, if any */
364 struct Stack_Window *stack_win;
365 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
366 if (stack_win->container->workspace == ws)
367 xcb_map_window(conn, stack_win->window);
369 ignore_enter_notify_forall(conn, ws, false);
373 * Unmaps all clients (and stack windows) of the given workspace.
375 * This needs to be called separately when temporarily rendering
376 * a workspace which is not the active workspace to force
377 * reconfiguration of all clients, like in src/xinerama.c when
378 * re-assigning a workspace to another screen.
381 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
383 struct Stack_Window *stack_win;
385 /* Ignore notify events because they would cause focus to be changed */
386 ignore_enter_notify_forall(conn, u_ws, true);
388 /* Unmap all clients of the given workspace */
389 int unmapped_clients = 0;
391 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
392 LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
393 client_unmap(conn, client);
397 /* To find floating clients, we traverse the focus stack */
398 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
399 if (!client_is_floating(client))
402 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
404 client_unmap(conn, client);
408 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
409 * if it is not the current workspace. */
410 if (unmapped_clients == 0 && u_ws != c_ws) {
411 /* Re-assign the workspace of all dock clients which use this workspace */
413 LOG("workspace %p is empty\n", u_ws);
414 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
415 if (dock->workspace != u_ws)
418 LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
419 dock->workspace = c_ws;
424 /* Unmap the stack windows on the given workspace, if any */
425 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
426 if (stack_win->container->workspace == u_ws)
427 xcb_unmap_window(conn, stack_win->window);
429 ignore_enter_notify_forall(conn, u_ws, false);
433 * Goes through all clients on the given workspace and updates the workspace’s
434 * urgent flag accordingly.
437 void workspace_update_urgent_flag(Workspace *ws) {
440 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
441 if (!current->urgent)