4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * workspace.c: Functions for modifying workspaces
27 #include "workspace.h"
33 * Returns a pointer to the workspace with the given number (starting at 0),
34 * creating the workspace if necessary (by allocating the necessary amount of
35 * memory and initializing the data structures correctly).
38 Workspace *workspace_get(int number) {
40 TAILQ_FOREACH(ws, workspaces, workspaces)
41 if (ws->num == number)
44 /* If we are still there, we could not find the requested workspace. */
45 int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
47 DLOG("We need to initialize that one, last ws = %d\n", last_ws);
49 for (int c = last_ws; c < number; c++) {
50 DLOG("Creating new ws\n");
52 ws = scalloc(sizeof(Workspace));
54 TAILQ_INIT(&(ws->floating_clients));
55 expand_table_cols(ws);
56 expand_table_rows(ws);
57 workspace_set_name(ws, NULL);
59 TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
63 ewmh_update_workarea();
69 * Sets the name (or just its number) for the given workspace. This has to
70 * be called for every workspace as the rendering function
71 * (render_internal_bar) relies on workspace->name and workspace->name_len
75 void workspace_set_name(Workspace *ws, const char *name) {
80 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
81 else ret = asprintf(&label, "%d", ws->num + 1);
84 errx(1, "asprintf() failed");
88 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
89 if (config.font != NULL)
90 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
91 else ws->text_width = 0;
97 * Returns true if the workspace is currently visible. Especially important for
98 * multi-monitor environments, as they can have multiple currenlty active
102 bool workspace_is_visible(Workspace *ws) {
103 return (ws->output->current_workspace == ws);
107 * Switches to the given workspace
110 void workspace_show(xcb_connection_t *conn, int workspace) {
111 bool need_warp = false;
112 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
113 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
114 Workspace *t_ws = workspace_get(workspace-1);
116 DLOG("show_workspace(%d)\n", workspace);
118 /* Store current_row/current_col */
119 c_ws->current_row = current_row;
120 c_ws->current_col = current_col;
122 /* Check if the workspace has not been used yet */
123 workspace_initialize(t_ws, c_ws->output, false);
125 if (c_ws->output != t_ws->output) {
126 /* We need to switch to the other output first */
127 DLOG("moving over to other output.\n");
129 /* Store the old client */
130 Client *old_client = CUR_CELL->currently_focused;
132 c_ws = t_ws->output->current_workspace;
133 current_col = c_ws->current_col;
134 current_row = c_ws->current_row;
135 if (CUR_CELL->currently_focused != NULL)
138 Rect *dims = &(c_ws->output->rect);
139 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
140 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
143 /* Re-decorate the old client, it’s not focused anymore */
144 if ((old_client != NULL) && !old_client->dock)
145 redecorate_window(conn, old_client);
146 else xcb_flush(conn);
149 /* Check if we need to change something or if we’re already there */
150 if (c_ws->output->current_workspace->num == (workspace-1)) {
151 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
152 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
153 set_focus(conn, last_focused, true);
155 client_warp_pointer_into(conn, last_focused);
162 Workspace *old_workspace = c_ws;
163 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
165 /* Unmap all clients of the old workspace */
166 workspace_unmap_clients(conn, old_workspace);
168 current_row = c_ws->current_row;
169 current_col = c_ws->current_col;
170 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
172 workspace_map_clients(conn, c_ws);
174 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
175 * render_layout afterwards, there is a short flickering on the source
176 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
177 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
180 /* Restore focus on the new workspace */
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);
184 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
188 /* We can warp the pointer only after the window has been
189 * reconfigured in render_layout, otherwise the pointer will
190 * be warped to the old position, which will not work when we
191 * moved it to another output. */
192 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
193 client_warp_pointer_into(conn, last_focused);
199 * Assigns the given workspace to the given output by correctly updating its
200 * state and reconfiguring all the clients on this workspace.
202 * This is called when initializing a output and when re-assigning it to a
203 * different output which just got available (if you configured it to be on
204 * output 1 and you just plugged in output 1).
207 void workspace_assign_to(Workspace *ws, Output *output) {
213 /* Copy the dimensions from the virtual output */
214 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
216 ewmh_update_workarea();
218 /* Force reconfiguration for each client on that workspace */
219 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
220 client->force_reconfigure = true;
227 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
228 render_workspace(global_conn, output, ws);
230 /* …unless we want to see them at the moment, we should hide that workspace */
231 if (workspace_is_visible(ws))
234 workspace_unmap_clients(global_conn, ws);
237 DLOG("Need to adjust c_ws...\n");
238 c_ws = output->current_workspace;
243 * Initializes the given workspace if it is not already initialized. The given
244 * screen is to be understood as a fallback, if the workspace itself either
245 * was not assigned to a particular screen or cannot be placed there because
246 * the screen is not attached at the moment.
249 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
252 if (ws->output != NULL && !recheck) {
253 DLOG("Workspace already initialized\n");
257 old_output = ws->output;
259 /* If this workspace has no preferred output or if the output it wants
260 * to be on is not available at the moment, we initialize it with
261 * the output which was given */
262 if (ws->preferred_output == NULL ||
263 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
266 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
267 /* If the assignment did not change, we do not need to update anything */
268 if (old_output != NULL && ws->output == old_output)
271 workspace_assign_to(ws, ws->output);
275 * Gets the first unused workspace for the given screen, taking into account
276 * the preferred_output setting of every workspace (workspace assignments).
279 Workspace *get_first_workspace_for_screen(Output *output) {
280 Workspace *result = NULL;
283 TAILQ_FOREACH(ws, workspaces, workspaces) {
284 if (ws->preferred_output == NULL ||
285 get_output_by_name(ws->preferred_output) != output)
292 if (result == NULL) {
293 /* No assignment found, returning first unused workspace */
294 TAILQ_FOREACH(ws, workspaces, workspaces) {
295 if (ws->output != NULL)
303 if (result == NULL) {
304 DLOG("No existing free workspace found to assign, creating a new one\n");
307 TAILQ_FOREACH(ws, workspaces, workspaces)
309 result = workspace_get(last_ws + 1);
312 workspace_initialize(result, output, false);
317 * Maps all clients (and stack windows) of the given workspace.
320 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
323 ignore_enter_notify_forall(conn, ws, true);
325 /* Map all clients on the new workspace */
327 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
328 client_map(conn, client);
330 /* Map all floating clients */
331 if (!ws->floating_hidden)
332 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
333 client_map(conn, client);
335 /* Map all stack windows, if any */
336 struct Stack_Window *stack_win;
337 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
338 if (stack_win->container->workspace == ws)
339 xcb_map_window(conn, stack_win->window);
341 ignore_enter_notify_forall(conn, ws, false);
345 * Unmaps all clients (and stack windows) of the given workspace.
347 * This needs to be called separately when temporarily rendering
348 * a workspace which is not the active workspace to force
349 * reconfiguration of all clients, like in src/xinerama.c when
350 * re-assigning a workspace to another screen.
353 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
355 struct Stack_Window *stack_win;
357 /* Ignore notify events because they would cause focus to be changed */
358 ignore_enter_notify_forall(conn, u_ws, true);
360 /* Unmap all clients of the given workspace */
361 int unmapped_clients = 0;
363 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
364 DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
365 client_unmap(conn, client);
369 /* To find floating clients, we traverse the focus stack */
370 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
371 if (!client_is_floating(client))
374 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
376 client_unmap(conn, client);
380 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
381 * if it is not the current workspace. */
382 if (unmapped_clients == 0 && u_ws != c_ws) {
383 /* Re-assign the workspace of all dock clients which use this workspace */
385 DLOG("workspace %p is empty\n", u_ws);
386 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
387 if (dock->workspace != u_ws)
390 DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
391 dock->workspace = c_ws;
396 /* Unmap the stack windows on the given workspace, if any */
397 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
398 if (stack_win->container->workspace == u_ws)
399 xcb_unmap_window(conn, stack_win->window);
401 ignore_enter_notify_forall(conn, u_ws, false);
405 * Goes through all clients on the given workspace and updates the workspace’s
406 * urgent flag accordingly.
409 void workspace_update_urgent_flag(Workspace *ws) {
412 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
413 if (!current->urgent)
424 * Returns the width of the workspace.
427 int workspace_width(Workspace *ws) {
428 return ws->rect.width;
432 * Returns the effective height of the workspace (without the internal bar and
433 * without dock clients).
436 int workspace_height(Workspace *ws) {
437 int height = ws->rect.height;
438 i3Font *font = load_font(global_conn, config.font);
440 /* Reserve space for dock clients */
442 SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
443 height -= client->desired_height;
445 /* Space for the internal bar */
446 height -= (font->height + 6);