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"
34 * Returns a pointer to the workspace with the given number (starting at 0),
35 * creating the workspace if necessary (by allocating the necessary amount of
36 * memory and initializing the data structures correctly).
39 Workspace *workspace_get(int number) {
41 TAILQ_FOREACH(ws, workspaces, workspaces)
42 if (ws->num == number)
45 /* If we are still there, we could not find the requested workspace. */
46 int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
48 DLOG("We need to initialize that one, last ws = %d\n", last_ws);
50 for (int c = last_ws; c < number; c++) {
51 DLOG("Creating new ws\n");
53 ws = scalloc(sizeof(Workspace));
55 TAILQ_INIT(&(ws->floating_clients));
56 expand_table_cols(ws);
57 expand_table_rows(ws);
58 workspace_set_name(ws, NULL);
60 TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
62 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"create\"}");
66 ewmh_update_workarea();
72 * Sets the name (or just its number) for the given workspace. This has to
73 * be called for every workspace as the rendering function
74 * (render_internal_bar) relies on workspace->name and workspace->name_len
78 void workspace_set_name(Workspace *ws, const char *name) {
83 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
84 else ret = asprintf(&label, "%d", ws->num + 1);
87 errx(1, "asprintf() failed");
92 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
93 if (config.font != NULL)
94 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
95 else ws->text_width = 0;
96 ws->utf8_name = label;
100 * Returns true if the workspace is currently visible. Especially important for
101 * multi-monitor environments, as they can have multiple currenlty active
105 bool workspace_is_visible(Workspace *ws) {
106 return (ws->output != NULL && ws->output->current_workspace == ws);
110 * Switches to the given workspace
113 void workspace_show(xcb_connection_t *conn, int workspace) {
114 bool need_warp = false;
115 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
116 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
117 Workspace *t_ws = workspace_get(workspace-1);
119 DLOG("show_workspace(%d)\n", workspace);
121 /* Store current_row/current_col */
122 c_ws->current_row = current_row;
123 c_ws->current_col = current_col;
125 /* Check if the workspace has not been used yet */
126 workspace_initialize(t_ws, c_ws->output, false);
128 if (c_ws->output != t_ws->output) {
129 /* We need to switch to the other output first */
130 DLOG("moving over to other output.\n");
132 /* Store the old client */
133 Client *old_client = CUR_CELL->currently_focused;
135 c_ws = t_ws->output->current_workspace;
136 current_col = c_ws->current_col;
137 current_row = c_ws->current_row;
138 if (CUR_CELL->currently_focused != NULL)
141 Rect *dims = &(c_ws->output->rect);
142 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
143 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
146 /* Re-decorate the old client, it’s not focused anymore */
147 if ((old_client != NULL) && !old_client->dock)
148 redecorate_window(conn, old_client);
149 else xcb_flush(conn);
151 /* We need to check if a global fullscreen-client is blocking
152 * the t_ws and if necessary switch that to local fullscreen */
153 Client* client = c_ws->fullscreen_client;
154 if (client != NULL && client->workspace != c_ws) {
155 if (c_ws->fullscreen_client->workspace != c_ws)
156 c_ws->fullscreen_client = NULL;
157 client_enter_fullscreen(conn, client, false);
161 /* Check if we need to change something or if we’re already there */
162 if (c_ws->output->current_workspace->num == (workspace-1)) {
163 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
164 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
165 set_focus(conn, last_focused, true);
167 client_warp_pointer_into(conn, last_focused);
171 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
176 Workspace *old_workspace = c_ws;
177 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
179 /* Unmap all clients of the old workspace */
180 workspace_unmap_clients(conn, old_workspace);
182 current_row = c_ws->current_row;
183 current_col = c_ws->current_col;
184 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
186 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
188 workspace_map_clients(conn, c_ws);
190 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
191 * render_layout afterwards, there is a short flickering on the source
192 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
193 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
196 /* Restore focus on the new workspace */
197 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
198 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
199 set_focus(conn, last_focused, true);
200 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
204 /* We can warp the pointer only after the window has been
205 * reconfigured in render_layout, otherwise the pointer will
206 * be warped to the old position, which will not work when we
207 * moved it to another output. */
208 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
209 client_warp_pointer_into(conn, last_focused);
215 * Assigns the given workspace to the given output by correctly updating its
216 * state and reconfiguring all the clients on this workspace.
218 * This is called when initializing a output and when re-assigning it to a
219 * different output which just got available (if you configured it to be on
220 * output 1 and you just plugged in output 1).
223 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
226 bool visible = workspace_is_visible(ws);
230 /* Copy the dimensions from the virtual output */
231 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
233 ewmh_update_workarea();
235 /* Force reconfiguration for each client on that workspace */
236 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
237 client->force_reconfigure = true;
244 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
245 render_workspace(global_conn, output, ws);
247 /* …unless we want to see them at the moment, we should hide that workspace */
248 if (visible && !hide_it)
251 /* however, if this is the current workspace, we only need to adjust
252 * the output’s current_workspace pointer (and must not unmap the
255 DLOG("Need to adjust output->current_workspace...\n");
256 output->current_workspace = c_ws;
257 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
261 workspace_unmap_clients(global_conn, ws);
265 * Initializes the given workspace if it is not already initialized. The given
266 * screen is to be understood as a fallback, if the workspace itself either
267 * was not assigned to a particular screen or cannot be placed there because
268 * the screen is not attached at the moment.
271 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
274 if (ws->output != NULL && !recheck) {
275 DLOG("Workspace already initialized\n");
279 old_output = ws->output;
281 /* If this workspace has no preferred output or if the output it wants
282 * to be on is not available at the moment, we initialize it with
283 * the output which was given */
284 if (ws->preferred_output == NULL ||
285 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
288 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
289 /* If the assignment did not change, we do not need to update anything */
290 if (old_output != NULL && ws->output == old_output)
293 workspace_assign_to(ws, ws->output, false);
295 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
299 * Gets the first unused workspace for the given screen, taking into account
300 * the preferred_output setting of every workspace (workspace assignments).
303 Workspace *get_first_workspace_for_output(Output *output) {
304 Workspace *result = NULL;
307 TAILQ_FOREACH(ws, workspaces, workspaces) {
308 if (ws->preferred_output == NULL ||
309 get_output_by_name(ws->preferred_output) != output)
316 if (result == NULL) {
317 /* No assignment found, returning first unused workspace */
318 TAILQ_FOREACH(ws, workspaces, workspaces) {
319 if (ws->output != NULL)
327 if (result == NULL) {
328 DLOG("No existing free workspace found to assign, creating a new one\n");
331 TAILQ_FOREACH(ws, workspaces, workspaces)
333 result = workspace_get(last_ws + 1);
336 workspace_initialize(result, output, false);
341 * Maps all clients (and stack windows) of the given workspace.
344 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
347 ignore_enter_notify_forall(conn, ws, true);
349 /* Map all clients on the new workspace */
351 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
352 client_map(conn, client);
354 /* Map all floating clients */
355 if (!ws->floating_hidden)
356 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
357 client_map(conn, client);
359 /* Map all stack windows, if any */
360 struct Stack_Window *stack_win;
361 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
362 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
363 xcb_map_window(conn, stack_win->window);
365 ignore_enter_notify_forall(conn, ws, false);
369 * Unmaps all clients (and stack windows) of the given workspace.
371 * This needs to be called separately when temporarily rendering
372 * a workspace which is not the active workspace to force
373 * reconfiguration of all clients, like in src/xinerama.c when
374 * re-assigning a workspace to another screen.
377 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
379 struct Stack_Window *stack_win;
381 /* Ignore notify events because they would cause focus to be changed */
382 ignore_enter_notify_forall(conn, u_ws, true);
384 /* Unmap all clients of the given workspace */
385 int unmapped_clients = 0;
387 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
388 DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
389 client_unmap(conn, client);
393 /* To find floating clients, we traverse the focus stack */
394 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
395 if (!client_is_floating(client))
398 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
400 client_unmap(conn, client);
404 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
405 * if it is not the current workspace. */
406 if (unmapped_clients == 0 && u_ws != c_ws) {
407 /* Re-assign the workspace of all dock clients which use this workspace */
409 DLOG("workspace %p is empty\n", u_ws);
410 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
411 if (dock->workspace != u_ws)
414 DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
415 dock->workspace = c_ws;
420 /* Unmap the stack windows on the given workspace, if any */
421 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
422 if (stack_win->container->workspace == u_ws)
423 xcb_unmap_window(conn, stack_win->window);
425 ignore_enter_notify_forall(conn, u_ws, false);
429 * Goes through all clients on the given workspace and updates the workspace’s
430 * urgent flag accordingly.
433 void workspace_update_urgent_flag(Workspace *ws) {
435 bool old_flag = ws->urgent;
438 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
439 if (!current->urgent)
448 if (old_flag != urgent)
449 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
453 * Returns the width of the workspace.
456 int workspace_width(Workspace *ws) {
457 return ws->rect.width;
461 * Returns the effective height of the workspace (without the internal bar and
462 * without dock clients).
465 int workspace_height(Workspace *ws) {
466 int height = ws->rect.height;
467 i3Font *font = load_font(global_conn, config.font);
469 /* Reserve space for dock clients */
471 SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
472 height -= client->desired_height;
474 /* Space for the internal bar */
475 if (!config.disable_workspace_bar)
476 height -= (font->height + 6);