2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
7 * workspace.c: Functions for modifying workspaces
15 * Returns a pointer to the workspace with the given number (starting at 0),
16 * creating the workspace if necessary (by allocating the necessary amount of
17 * memory and initializing the data structures correctly).
20 Con *workspace_get(const char *num) {
21 Con *output, *workspace = NULL, *current;
23 /* TODO: could that look like this in the future?
24 GET_MATCHING_NODE(workspace, croot, strcasecmp(current->name, num) != 0);
26 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
27 TAILQ_FOREACH(current, &(output->nodes_head), nodes) {
28 if (strcasecmp(current->name, num) != 0)
36 LOG("should switch to ws %s\n", num);
37 if (workspace == NULL) {
38 LOG("need to create this one\n");
39 output = con_get_output(focused);
40 LOG("got output %p\n", output);
41 workspace = con_new(output);
42 workspace->name = strdup(num);
44 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
47 //ewmh_update_workarea();
55 * Sets the name (or just its number) for the given workspace. This has to
56 * be called for every workspace as the rendering function
57 * (render_internal_bar) relies on workspace->name and workspace->name_len
61 void workspace_set_name(Workspace *ws, const char *name) {
66 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
67 else ret = asprintf(&label, "%d", ws->num + 1);
70 errx(1, "asprintf() failed");
75 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
76 if (config.font != NULL)
77 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
78 else ws->text_width = 0;
79 ws->utf8_name = label;
83 * Returns true if the workspace is currently visible. Especially important for
84 * multi-monitor environments, as they can have multiple currenlty active
88 bool workspace_is_visible(Workspace *ws) {
89 return (ws->output != NULL && ws->output->current_workspace == ws);
95 * Switches to the given workspace
98 void workspace_show(const char *num) {
99 Con *workspace, *current;
101 workspace = workspace_get(num);
102 workspace->fullscreen_mode = CF_OUTPUT;
103 /* disable fullscreen */
104 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes)
105 current->fullscreen_mode = CF_NONE;
107 LOG("switching to %p\n", workspace);
108 Con *next = workspace;
110 while (!TAILQ_EMPTY(&(next->focus_head)))
111 next = TAILQ_FIRST(&(next->focus_head));
114 workspace->fullscreen_mode = CF_OUTPUT;
115 LOG("focused now = %p / %s\n", focused, focused->name);
118 /* Check if the workspace has not been used yet */
119 workspace_initialize(t_ws, c_ws->output, false);
121 if (c_ws->output != t_ws->output) {
122 /* We need to switch to the other output first */
123 DLOG("moving over to other output.\n");
125 /* Store the old client */
126 Client *old_client = CUR_CELL->currently_focused;
128 c_ws = t_ws->output->current_workspace;
129 current_col = c_ws->current_col;
130 current_row = c_ws->current_row;
131 if (CUR_CELL->currently_focused != NULL)
134 Rect *dims = &(c_ws->output->rect);
135 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
136 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
139 /* Re-decorate the old client, it’s not focused anymore */
140 if ((old_client != NULL) && !old_client->dock)
141 redecorate_window(conn, old_client);
142 else xcb_flush(conn);
144 /* We need to check if a global fullscreen-client is blocking
145 * the t_ws and if necessary switch that to local fullscreen */
146 Client* client = c_ws->fullscreen_client;
147 if (client != NULL && client->workspace != c_ws) {
148 if (c_ws->fullscreen_client->workspace != c_ws)
149 c_ws->fullscreen_client = NULL;
150 client_enter_fullscreen(conn, client, false);
154 /* Check if we need to change something or if we’re already there */
155 if (c_ws->output->current_workspace->num == (workspace-1)) {
156 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
157 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
158 set_focus(conn, last_focused, true);
160 client_warp_pointer_into(conn, last_focused);
164 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
169 Workspace *old_workspace = c_ws;
170 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
172 /* Unmap all clients of the old workspace */
173 workspace_unmap_clients(conn, old_workspace);
175 current_row = c_ws->current_row;
176 current_col = c_ws->current_col;
177 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
179 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
181 workspace_map_clients(conn, c_ws);
183 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
184 * render_layout afterwards, there is a short flickering on the source
185 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
186 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
189 /* Restore focus on the new workspace */
190 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
191 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
192 set_focus(conn, last_focused, true);
193 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
197 /* We can warp the pointer only after the window has been
198 * reconfigured in render_layout, otherwise the pointer will
199 * be warped to the old position, which will not work when we
200 * moved it to another output. */
201 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
202 client_warp_pointer_into(conn, last_focused);
210 * Assigns the given workspace to the given output by correctly updating its
211 * state and reconfiguring all the clients on this workspace.
213 * This is called when initializing a output and when re-assigning it to a
214 * different output which just got available (if you configured it to be on
215 * output 1 and you just plugged in output 1).
218 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
221 bool visible = workspace_is_visible(ws);
225 /* Copy the dimensions from the virtual output */
226 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
228 ewmh_update_workarea();
230 /* Force reconfiguration for each client on that workspace */
231 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
232 client->force_reconfigure = true;
239 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
240 render_workspace(global_conn, output, ws);
242 /* …unless we want to see them at the moment, we should hide that workspace */
243 if (visible && !hide_it)
246 /* however, if this is the current workspace, we only need to adjust
247 * the output’s current_workspace pointer (and must not unmap the
250 DLOG("Need to adjust output->current_workspace...\n");
251 output->current_workspace = c_ws;
252 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
256 workspace_unmap_clients(global_conn, ws);
260 * Initializes the given workspace if it is not already initialized. The given
261 * screen is to be understood as a fallback, if the workspace itself either
262 * was not assigned to a particular screen or cannot be placed there because
263 * the screen is not attached at the moment.
266 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
269 if (ws->output != NULL && !recheck) {
270 DLOG("Workspace already initialized\n");
274 old_output = ws->output;
276 /* If this workspace has no preferred output or if the output it wants
277 * to be on is not available at the moment, we initialize it with
278 * the output which was given */
279 if (ws->preferred_output == NULL ||
280 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
283 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
284 /* If the assignment did not change, we do not need to update anything */
285 if (old_output != NULL && ws->output == old_output)
288 workspace_assign_to(ws, ws->output, false);
292 * Gets the first unused workspace for the given screen, taking into account
293 * the preferred_output setting of every workspace (workspace assignments).
296 Workspace *get_first_workspace_for_output(Output *output) {
297 Workspace *result = NULL;
300 TAILQ_FOREACH(ws, workspaces, workspaces) {
301 if (ws->preferred_output == NULL ||
302 get_output_by_name(ws->preferred_output) != output)
309 if (result == NULL) {
310 /* No assignment found, returning first unused workspace */
311 TAILQ_FOREACH(ws, workspaces, workspaces) {
312 if (ws->output != NULL)
320 if (result == NULL) {
321 DLOG("No existing free workspace found to assign, creating a new one\n");
324 TAILQ_FOREACH(ws, workspaces, workspaces)
326 result = workspace_get(last_ws + 1);
329 workspace_initialize(result, output, false);
334 * Maps all clients (and stack windows) of the given workspace.
337 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
340 ignore_enter_notify_forall(conn, ws, true);
342 /* Map all clients on the new workspace */
344 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
345 client_map(conn, client);
347 /* Map all floating clients */
348 if (!ws->floating_hidden)
349 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
350 client_map(conn, client);
352 /* Map all stack windows, if any */
353 struct Stack_Window *stack_win;
354 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
355 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
356 xcb_map_window(conn, stack_win->window);
358 ignore_enter_notify_forall(conn, ws, false);
362 * Unmaps all clients (and stack windows) of the given workspace.
364 * This needs to be called separately when temporarily rendering
365 * a workspace which is not the active workspace to force
366 * reconfiguration of all clients, like in src/xinerama.c when
367 * re-assigning a workspace to another screen.
370 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
372 struct Stack_Window *stack_win;
374 /* Ignore notify events because they would cause focus to be changed */
375 ignore_enter_notify_forall(conn, u_ws, true);
377 /* Unmap all clients of the given workspace */
378 int unmapped_clients = 0;
380 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
381 DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
382 client_unmap(conn, client);
386 /* To find floating clients, we traverse the focus stack */
387 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
388 if (!client_is_floating(client))
391 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
393 client_unmap(conn, client);
397 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
398 * if it is not the current workspace. */
399 if (unmapped_clients == 0 && u_ws != c_ws) {
400 /* Re-assign the workspace of all dock clients which use this workspace */
402 DLOG("workspace %p is empty\n", u_ws);
403 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
404 if (dock->workspace != u_ws)
407 DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
408 dock->workspace = c_ws;
413 /* Unmap the stack windows on the given workspace, if any */
414 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
415 if (stack_win->container->workspace == u_ws)
416 xcb_unmap_window(conn, stack_win->window);
418 ignore_enter_notify_forall(conn, u_ws, false);
422 * Goes through all clients on the given workspace and updates the workspace’s
423 * urgent flag accordingly.
426 void workspace_update_urgent_flag(Workspace *ws) {
428 bool old_flag = ws->urgent;
431 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
432 if (!current->urgent)
441 if (old_flag != urgent)
442 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
446 * Returns the width of the workspace.
449 int workspace_width(Workspace *ws) {
450 return ws->rect.width;
454 * Returns the effective height of the workspace (without the internal bar and
455 * without dock clients).
458 int workspace_height(Workspace *ws) {
459 int height = ws->rect.height;
460 i3Font *font = load_font(global_conn, config.font);
462 /* Reserve space for dock clients */
464 SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
465 height -= client->desired_height;
467 /* Space for the internal bar */
468 if (!config.disable_workspace_bar)
469 height -= (font->height + 6);