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, *child;
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 (current->type != CT_CON)
31 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
32 if (strcasecmp(child->name, num) != 0)
41 LOG("getting ws %s\n", num);
42 if (workspace == NULL) {
43 LOG("need to create this one\n");
44 output = con_get_output(focused);
45 Con *child, *content = NULL;
46 TAILQ_FOREACH(child, &(output->nodes_head), nodes) {
47 if (child->type == CT_CON) {
52 assert(content != NULL);
53 LOG("got output %p with child %p\n", output, content);
54 /* We need to attach this container after setting its type. con_attach
55 * will handle CT_WORKSPACEs differently */
56 workspace = con_new(NULL);
58 asprintf(&name, "[i3 con] workspace %s", num);
59 x_set_name(workspace, name);
61 workspace->type = CT_WORKSPACE;
62 FREE(workspace->name);
63 workspace->name = sstrdup(num);
64 /* We set ->num to the number if this workspace’s name consists only of
65 * a positive number. Otherwise it’s a named ws and num will be -1. */
67 long parsed_num = strtol(num, &end, 10);
68 if (parsed_num == LONG_MIN ||
69 parsed_num == LONG_MAX ||
71 (end && *end != '\0'))
73 else workspace->num = parsed_num;
74 LOG("num = %d\n", workspace->num);
75 workspace->orientation = HORIZ;
76 con_attach(workspace, content, false);
78 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
81 //ewmh_update_workarea();
89 * Sets the name (or just its number) for the given workspace. This has to
90 * be called for every workspace as the rendering function
91 * (render_internal_bar) relies on workspace->name and workspace->name_len
95 void workspace_set_name(Workspace *ws, const char *name) {
100 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
101 else ret = asprintf(&label, "%d", ws->num + 1);
104 errx(1, "asprintf() failed");
109 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
110 if (config.font != NULL)
111 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
112 else ws->text_width = 0;
113 ws->utf8_name = label;
118 * Returns true if the workspace is currently visible. Especially important for
119 * multi-monitor environments, as they can have multiple currenlty active
123 bool workspace_is_visible(Con *ws) {
124 Con *output = con_get_output(ws);
127 Con *fs = con_get_fullscreen_con(output);
128 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
133 * XXX: we need to clean up all this recursive walking code.
136 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
139 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
140 if (current != exclude &&
141 current->sticky_group != NULL &&
142 current->window != NULL &&
143 strcmp(current->sticky_group, sticky_group) == 0)
146 Con *recurse = _get_sticky(current, sticky_group, exclude);
151 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
152 if (current != exclude &&
153 current->sticky_group != NULL &&
154 current->window != NULL &&
155 strcmp(current->sticky_group, sticky_group) == 0)
158 Con *recurse = _get_sticky(current, sticky_group, exclude);
167 * Reassigns all child windows in sticky containers. Called when the user
168 * changes workspaces.
170 * XXX: what about sticky containers which contain containers?
173 static void workspace_reassign_sticky(Con *con) {
175 /* 1: go through all containers */
177 /* handle all children and floating windows of this node */
178 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
179 if (current->sticky_group == NULL) {
180 workspace_reassign_sticky(current);
184 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
185 /* 2: find a window which we can re-assign */
186 Con *output = con_get_output(current);
187 Con *src = _get_sticky(output, current->sticky_group, current);
190 LOG("No window found for this sticky group\n");
191 workspace_reassign_sticky(current);
195 x_move_win(src, current);
196 current->window = src->window;
197 current->mapped = true;
201 x_reparent_child(current, src);
203 LOG("re-assigned window from src %p to dest %p\n", src, current);
206 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
207 workspace_reassign_sticky(current);
211 * Switches to the given workspace
214 void workspace_show(const char *num) {
215 Con *workspace, *current, *old;
217 workspace = workspace_get(num);
219 /* disable fullscreen for the other workspaces and get the workspace we are
221 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
222 if (current->fullscreen_mode == CF_OUTPUT)
224 current->fullscreen_mode = CF_NONE;
227 /* Check if the the currently focused con is on the same Output as the
228 * workspace we chose as 'old'. If not, use the workspace of the currently
230 Con *ws = con_get_workspace(focused);
231 if (ws && ws->parent != old->parent)
234 /* enable fullscreen for the target workspace. If it happens to be the
235 * same one we are currently on anyways, we can stop here. */
236 workspace->fullscreen_mode = CF_OUTPUT;
237 if (workspace == old)
240 workspace_reassign_sticky(workspace);
242 LOG("switching to %p\n", workspace);
243 Con *next = con_descend_focused(workspace);
245 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
246 /* check if this workspace is currently visible */
247 if (!workspace_is_visible(old)) {
248 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
249 tree_close(old, false, false);
250 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
255 workspace->fullscreen_mode = CF_OUTPUT;
256 LOG("focused now = %p / %s\n", focused, focused->name);
258 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
261 /* Check if the workspace has not been used yet */
262 workspace_initialize(t_ws, c_ws->output, false);
264 if (c_ws->output != t_ws->output) {
265 /* We need to switch to the other output first */
266 DLOG("moving over to other output.\n");
268 /* Store the old client */
269 Client *old_client = CUR_CELL->currently_focused;
271 c_ws = t_ws->output->current_workspace;
272 current_col = c_ws->current_col;
273 current_row = c_ws->current_row;
274 if (CUR_CELL->currently_focused != NULL)
277 Rect *dims = &(c_ws->output->rect);
278 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
279 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
282 /* Re-decorate the old client, it’s not focused anymore */
283 if ((old_client != NULL) && !old_client->dock)
284 redecorate_window(conn, old_client);
285 else xcb_flush(conn);
287 /* We need to check if a global fullscreen-client is blocking
288 * the t_ws and if necessary switch that to local fullscreen */
289 Client* client = c_ws->fullscreen_client;
290 if (client != NULL && client->workspace != c_ws) {
291 if (c_ws->fullscreen_client->workspace != c_ws)
292 c_ws->fullscreen_client = NULL;
293 client_enter_fullscreen(conn, client, false);
297 /* Check if we need to change something or if we’re already there */
298 if (c_ws->output->current_workspace->num == (workspace-1)) {
299 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
300 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
301 set_focus(conn, last_focused, true);
303 client_warp_pointer_into(conn, last_focused);
307 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
312 Workspace *old_workspace = c_ws;
313 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
315 /* Unmap all clients of the old workspace */
316 workspace_unmap_clients(conn, old_workspace);
318 current_row = c_ws->current_row;
319 current_col = c_ws->current_col;
320 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
322 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
324 workspace_map_clients(conn, c_ws);
326 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
327 * render_layout afterwards, there is a short flickering on the source
328 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
329 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
332 /* Restore focus on the new workspace */
333 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
334 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
335 set_focus(conn, last_focused, true);
336 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
340 /* We can warp the pointer only after the window has been
341 * reconfigured in render_layout, otherwise the pointer will
342 * be warped to the old position, which will not work when we
343 * moved it to another output. */
344 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
345 client_warp_pointer_into(conn, last_focused);
353 * Assigns the given workspace to the given output by correctly updating its
354 * state and reconfiguring all the clients on this workspace.
356 * This is called when initializing a output and when re-assigning it to a
357 * different output which just got available (if you configured it to be on
358 * output 1 and you just plugged in output 1).
361 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
364 bool visible = workspace_is_visible(ws);
368 /* Copy the dimensions from the virtual output */
369 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
371 ewmh_update_workarea();
373 /* Force reconfiguration for each client on that workspace */
374 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
375 client->force_reconfigure = true;
382 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
383 render_workspace(global_conn, output, ws);
385 /* …unless we want to see them at the moment, we should hide that workspace */
386 if (visible && !hide_it)
389 /* however, if this is the current workspace, we only need to adjust
390 * the output’s current_workspace pointer (and must not unmap the
393 DLOG("Need to adjust output->current_workspace...\n");
394 output->current_workspace = c_ws;
395 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
399 workspace_unmap_clients(global_conn, ws);
403 * Initializes the given workspace if it is not already initialized. The given
404 * screen is to be understood as a fallback, if the workspace itself either
405 * was not assigned to a particular screen or cannot be placed there because
406 * the screen is not attached at the moment.
409 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
412 if (ws->output != NULL && !recheck) {
413 DLOG("Workspace already initialized\n");
417 old_output = ws->output;
419 /* If this workspace has no preferred output or if the output it wants
420 * to be on is not available at the moment, we initialize it with
421 * the output which was given */
422 if (ws->preferred_output == NULL ||
423 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
426 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
427 /* If the assignment did not change, we do not need to update anything */
428 if (old_output != NULL && ws->output == old_output)
431 workspace_assign_to(ws, ws->output, false);
435 * Gets the first unused workspace for the given screen, taking into account
436 * the preferred_output setting of every workspace (workspace assignments).
439 Workspace *get_first_workspace_for_output(Output *output) {
440 Workspace *result = NULL;
443 TAILQ_FOREACH(ws, workspaces, workspaces) {
444 if (ws->preferred_output == NULL ||
445 get_output_by_name(ws->preferred_output) != output)
452 if (result == NULL) {
453 /* No assignment found, returning first unused workspace */
454 TAILQ_FOREACH(ws, workspaces, workspaces) {
455 if (ws->output != NULL)
463 if (result == NULL) {
464 DLOG("No existing free workspace found to assign, creating a new one\n");
467 TAILQ_FOREACH(ws, workspaces, workspaces)
469 result = workspace_get(last_ws + 1);
472 workspace_initialize(result, output, false);
478 static bool get_urgency_flag(Con *con) {
480 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
481 if (child->urgent || get_urgency_flag(child))
484 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
485 if (child->urgent || get_urgency_flag(child))
492 * Goes through all clients on the given workspace and updates the workspace’s
493 * urgent flag accordingly.
496 void workspace_update_urgent_flag(Con *ws) {
497 bool old_flag = ws->urgent;
498 ws->urgent = get_urgency_flag(ws);
499 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
501 if (old_flag != ws->urgent)
502 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
506 * 'Forces' workspace orientation by moving all cons into a new split-con with
507 * the same orientation as the workspace and then changing the workspace
511 void ws_force_orientation(Con *ws, orientation_t orientation) {
512 /* 1: create a new split container */
513 Con *split = con_new(NULL);
516 /* 2: copy layout and orientation from workspace */
517 split->layout = ws->layout;
518 split->orientation = ws->orientation;
520 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
522 /* 3: move the existing cons of this workspace below the new con */
523 DLOG("Moving cons\n");
524 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
525 Con *child = TAILQ_FIRST(&(ws->nodes_head));
527 con_attach(child, split, true);
530 /* 4: switch workspace orientation */
531 ws->orientation = orientation;
533 /* 5: attach the new split container to the workspace */
534 DLOG("Attaching new split to ws\n");
535 con_attach(split, ws, false);
537 /* 6: fix the percentages */
541 con_focus(old_focused);