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("getting 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);
43 asprintf(&name, "[i3 con] workspace %s", num);
44 x_set_name(workspace, name);
46 workspace->type = CT_WORKSPACE;
47 workspace->name = strdup(num);
48 workspace->orientation = HORIZ;
50 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
53 //ewmh_update_workarea();
61 * Sets the name (or just its number) for the given workspace. This has to
62 * be called for every workspace as the rendering function
63 * (render_internal_bar) relies on workspace->name and workspace->name_len
67 void workspace_set_name(Workspace *ws, const char *name) {
72 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
73 else ret = asprintf(&label, "%d", ws->num + 1);
76 errx(1, "asprintf() failed");
81 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
82 if (config.font != NULL)
83 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
84 else ws->text_width = 0;
85 ws->utf8_name = label;
90 * Returns true if the workspace is currently visible. Especially important for
91 * multi-monitor environments, as they can have multiple currenlty active
95 bool workspace_is_visible(Con *ws) {
96 Con *output = con_get_output(ws);
99 Con *fs = con_get_fullscreen_con(output);
100 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
105 * XXX: we need to clean up all this recursive walking code.
108 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
111 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
112 if (current != exclude &&
113 current->sticky_group != NULL &&
114 current->window != NULL &&
115 strcmp(current->sticky_group, sticky_group) == 0)
118 Con *recurse = _get_sticky(current, sticky_group, exclude);
123 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
124 if (current != exclude &&
125 current->sticky_group != NULL &&
126 current->window != NULL &&
127 strcmp(current->sticky_group, sticky_group) == 0)
130 Con *recurse = _get_sticky(current, sticky_group, exclude);
139 * Reassigns all child windows in sticky containers. Called when the user
140 * changes workspaces.
142 * XXX: what about sticky containers which contain containers?
145 static void workspace_reassign_sticky(Con *con) {
147 /* 1: go through all containers */
149 /* handle all children and floating windows of this node */
150 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
151 if (current->sticky_group == NULL) {
152 workspace_reassign_sticky(current);
156 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
157 /* 2: find a window which we can re-assign */
158 Con *output = con_get_output(current);
159 Con *src = _get_sticky(output, current->sticky_group, current);
162 LOG("No window found for this sticky group\n");
163 workspace_reassign_sticky(current);
167 x_move_win(src, current);
168 current->window = src->window;
169 current->mapped = true;
173 x_reparent_child(current, src);
175 LOG("re-assigned window from src %p to dest %p\n", src, current);
178 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
179 workspace_reassign_sticky(current);
183 * Switches to the given workspace
186 void workspace_show(const char *num) {
187 Con *workspace, *current, *old;
189 old = con_get_workspace(focused);
191 workspace = workspace_get(num);
192 if (workspace == old)
194 workspace->fullscreen_mode = CF_OUTPUT;
195 /* disable fullscreen */
196 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes)
197 current->fullscreen_mode = CF_NONE;
199 workspace_reassign_sticky(workspace);
201 LOG("switching to %p\n", workspace);
202 Con *next = workspace;
204 while (!TAILQ_EMPTY(&(next->focus_head)))
205 next = TAILQ_FIRST(&(next->focus_head));
208 if (TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
209 /* check if this workspace is currently visible */
210 if (!workspace_is_visible(old)) {
211 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
212 tree_close(old, false, false);
213 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
218 workspace->fullscreen_mode = CF_OUTPUT;
219 LOG("focused now = %p / %s\n", focused, focused->name);
221 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
224 /* Check if the workspace has not been used yet */
225 workspace_initialize(t_ws, c_ws->output, false);
227 if (c_ws->output != t_ws->output) {
228 /* We need to switch to the other output first */
229 DLOG("moving over to other output.\n");
231 /* Store the old client */
232 Client *old_client = CUR_CELL->currently_focused;
234 c_ws = t_ws->output->current_workspace;
235 current_col = c_ws->current_col;
236 current_row = c_ws->current_row;
237 if (CUR_CELL->currently_focused != NULL)
240 Rect *dims = &(c_ws->output->rect);
241 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
242 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
245 /* Re-decorate the old client, it’s not focused anymore */
246 if ((old_client != NULL) && !old_client->dock)
247 redecorate_window(conn, old_client);
248 else xcb_flush(conn);
250 /* We need to check if a global fullscreen-client is blocking
251 * the t_ws and if necessary switch that to local fullscreen */
252 Client* client = c_ws->fullscreen_client;
253 if (client != NULL && client->workspace != c_ws) {
254 if (c_ws->fullscreen_client->workspace != c_ws)
255 c_ws->fullscreen_client = NULL;
256 client_enter_fullscreen(conn, client, false);
260 /* Check if we need to change something or if we’re already there */
261 if (c_ws->output->current_workspace->num == (workspace-1)) {
262 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
263 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
264 set_focus(conn, last_focused, true);
266 client_warp_pointer_into(conn, last_focused);
270 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
275 Workspace *old_workspace = c_ws;
276 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
278 /* Unmap all clients of the old workspace */
279 workspace_unmap_clients(conn, old_workspace);
281 current_row = c_ws->current_row;
282 current_col = c_ws->current_col;
283 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
285 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
287 workspace_map_clients(conn, c_ws);
289 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
290 * render_layout afterwards, there is a short flickering on the source
291 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
292 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
295 /* Restore focus on the new workspace */
296 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
297 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
298 set_focus(conn, last_focused, true);
299 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
303 /* We can warp the pointer only after the window has been
304 * reconfigured in render_layout, otherwise the pointer will
305 * be warped to the old position, which will not work when we
306 * moved it to another output. */
307 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
308 client_warp_pointer_into(conn, last_focused);
316 * Assigns the given workspace to the given output by correctly updating its
317 * state and reconfiguring all the clients on this workspace.
319 * This is called when initializing a output and when re-assigning it to a
320 * different output which just got available (if you configured it to be on
321 * output 1 and you just plugged in output 1).
324 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
327 bool visible = workspace_is_visible(ws);
331 /* Copy the dimensions from the virtual output */
332 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
334 ewmh_update_workarea();
336 /* Force reconfiguration for each client on that workspace */
337 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
338 client->force_reconfigure = true;
345 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
346 render_workspace(global_conn, output, ws);
348 /* …unless we want to see them at the moment, we should hide that workspace */
349 if (visible && !hide_it)
352 /* however, if this is the current workspace, we only need to adjust
353 * the output’s current_workspace pointer (and must not unmap the
356 DLOG("Need to adjust output->current_workspace...\n");
357 output->current_workspace = c_ws;
358 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
362 workspace_unmap_clients(global_conn, ws);
366 * Initializes the given workspace if it is not already initialized. The given
367 * screen is to be understood as a fallback, if the workspace itself either
368 * was not assigned to a particular screen or cannot be placed there because
369 * the screen is not attached at the moment.
372 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
375 if (ws->output != NULL && !recheck) {
376 DLOG("Workspace already initialized\n");
380 old_output = ws->output;
382 /* If this workspace has no preferred output or if the output it wants
383 * to be on is not available at the moment, we initialize it with
384 * the output which was given */
385 if (ws->preferred_output == NULL ||
386 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
389 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
390 /* If the assignment did not change, we do not need to update anything */
391 if (old_output != NULL && ws->output == old_output)
394 workspace_assign_to(ws, ws->output, false);
398 * Gets the first unused workspace for the given screen, taking into account
399 * the preferred_output setting of every workspace (workspace assignments).
402 Workspace *get_first_workspace_for_output(Output *output) {
403 Workspace *result = NULL;
406 TAILQ_FOREACH(ws, workspaces, workspaces) {
407 if (ws->preferred_output == NULL ||
408 get_output_by_name(ws->preferred_output) != output)
415 if (result == NULL) {
416 /* No assignment found, returning first unused workspace */
417 TAILQ_FOREACH(ws, workspaces, workspaces) {
418 if (ws->output != NULL)
426 if (result == NULL) {
427 DLOG("No existing free workspace found to assign, creating a new one\n");
430 TAILQ_FOREACH(ws, workspaces, workspaces)
432 result = workspace_get(last_ws + 1);
435 workspace_initialize(result, output, false);
440 * Maps all clients (and stack windows) of the given workspace.
443 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
446 ignore_enter_notify_forall(conn, ws, true);
448 /* Map all clients on the new workspace */
450 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
451 client_map(conn, client);
453 /* Map all floating clients */
454 if (!ws->floating_hidden)
455 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
456 client_map(conn, client);
458 /* Map all stack windows, if any */
459 struct Stack_Window *stack_win;
460 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
461 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
462 xcb_map_window(conn, stack_win->window);
464 ignore_enter_notify_forall(conn, ws, false);
468 * Unmaps all clients (and stack windows) of the given workspace.
470 * This needs to be called separately when temporarily rendering
471 * a workspace which is not the active workspace to force
472 * reconfiguration of all clients, like in src/xinerama.c when
473 * re-assigning a workspace to another screen.
476 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
478 struct Stack_Window *stack_win;
480 /* Ignore notify events because they would cause focus to be changed */
481 ignore_enter_notify_forall(conn, u_ws, true);
483 /* Unmap all clients of the given workspace */
484 int unmapped_clients = 0;
486 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
487 DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
488 client_unmap(conn, client);
492 /* To find floating clients, we traverse the focus stack */
493 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
494 if (!client_is_floating(client))
497 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
499 client_unmap(conn, client);
503 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
504 * if it is not the current workspace. */
505 if (unmapped_clients == 0 && u_ws != c_ws) {
506 /* Re-assign the workspace of all dock clients which use this workspace */
508 DLOG("workspace %p is empty\n", u_ws);
509 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
510 if (dock->workspace != u_ws)
513 DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
514 dock->workspace = c_ws;
519 /* Unmap the stack windows on the given workspace, if any */
520 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
521 if (stack_win->container->workspace == u_ws)
522 xcb_unmap_window(conn, stack_win->window);
524 ignore_enter_notify_forall(conn, u_ws, false);
528 static bool get_urgency_flag(Con *con) {
530 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
531 if (child->urgent || get_urgency_flag(child))
534 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
535 if (child->urgent || get_urgency_flag(child))
542 * Goes through all clients on the given workspace and updates the workspace’s
543 * urgent flag accordingly.
546 void workspace_update_urgent_flag(Con *ws) {
547 bool old_flag = ws->urgent;
548 ws->urgent = get_urgency_flag(ws);
549 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
551 if (old_flag != ws->urgent)
552 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
558 * Returns the width of the workspace.
561 int workspace_width(Workspace *ws) {
562 return ws->rect.width;
566 * Returns the effective height of the workspace (without the internal bar and
567 * without dock clients).
570 int workspace_height(Workspace *ws) {
571 int height = ws->rect.height;
572 i3Font *font = load_font(global_conn, config.font);
574 /* Reserve space for dock clients */
576 SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
577 height -= client->desired_height;
579 /* Space for the internal bar */
580 if (!config.disable_workspace_bar)
581 height -= (font->height + 6);