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, *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(child, &(output_get_content(output)->nodes_head), nodes) {
28 if (strcasecmp(child->name, num) != 0)
35 LOG("getting ws %s\n", num);
36 if (workspace == NULL) {
37 LOG("need to create this one\n");
38 output = con_get_output(focused);
39 Con *content = output_get_content(output);
40 LOG("got output %p with content %p\n", output, content);
41 /* We need to attach this container after setting its type. con_attach
42 * will handle CT_WORKSPACEs differently */
43 workspace = con_new(NULL);
45 asprintf(&name, "[i3 con] workspace %s", num);
46 x_set_name(workspace, name);
48 workspace->type = CT_WORKSPACE;
49 FREE(workspace->name);
50 workspace->name = sstrdup(num);
51 /* We set ->num to the number if this workspace’s name consists only of
52 * a positive number. Otherwise it’s a named ws and num will be -1. */
54 long parsed_num = strtol(num, &end, 10);
55 if (parsed_num == LONG_MIN ||
56 parsed_num == LONG_MAX ||
58 (end && *end != '\0'))
60 else workspace->num = parsed_num;
61 LOG("num = %d\n", workspace->num);
62 workspace->orientation = HORIZ;
63 con_attach(workspace, content, false);
65 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
68 //ewmh_update_workarea();
76 * Sets the name (or just its number) for the given workspace. This has to
77 * be called for every workspace as the rendering function
78 * (render_internal_bar) relies on workspace->name and workspace->name_len
82 void workspace_set_name(Workspace *ws, const char *name) {
87 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
88 else ret = asprintf(&label, "%d", ws->num + 1);
91 errx(1, "asprintf() failed");
96 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
97 if (config.font != NULL)
98 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
99 else ws->text_width = 0;
100 ws->utf8_name = label;
105 * Returns true if the workspace is currently visible. Especially important for
106 * multi-monitor environments, as they can have multiple currenlty active
110 bool workspace_is_visible(Con *ws) {
111 Con *output = con_get_output(ws);
114 Con *fs = con_get_fullscreen_con(output);
115 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
120 * XXX: we need to clean up all this recursive walking code.
123 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
126 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
127 if (current != exclude &&
128 current->sticky_group != NULL &&
129 current->window != NULL &&
130 strcmp(current->sticky_group, sticky_group) == 0)
133 Con *recurse = _get_sticky(current, sticky_group, exclude);
138 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
139 if (current != exclude &&
140 current->sticky_group != NULL &&
141 current->window != NULL &&
142 strcmp(current->sticky_group, sticky_group) == 0)
145 Con *recurse = _get_sticky(current, sticky_group, exclude);
154 * Reassigns all child windows in sticky containers. Called when the user
155 * changes workspaces.
157 * XXX: what about sticky containers which contain containers?
160 static void workspace_reassign_sticky(Con *con) {
162 /* 1: go through all containers */
164 /* handle all children and floating windows of this node */
165 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
166 if (current->sticky_group == NULL) {
167 workspace_reassign_sticky(current);
171 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
172 /* 2: find a window which we can re-assign */
173 Con *output = con_get_output(current);
174 Con *src = _get_sticky(output, current->sticky_group, current);
177 LOG("No window found for this sticky group\n");
178 workspace_reassign_sticky(current);
182 x_move_win(src, current);
183 current->window = src->window;
184 current->mapped = true;
188 x_reparent_child(current, src);
190 LOG("re-assigned window from src %p to dest %p\n", src, current);
193 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
194 workspace_reassign_sticky(current);
198 * Switches to the given workspace
201 void workspace_show(const char *num) {
202 Con *workspace, *current, *old = NULL;
204 workspace = workspace_get(num);
206 /* disable fullscreen for the other workspaces and get the workspace we are
208 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
209 if (current->fullscreen_mode == CF_OUTPUT)
211 current->fullscreen_mode = CF_NONE;
215 /* Check if the the currently focused con is on the same Output as the
216 * workspace we chose as 'old'. If not, use the workspace of the currently
218 Con *ws = con_get_workspace(focused);
219 if (ws && ws->parent != old->parent)
222 /* enable fullscreen for the target workspace. If it happens to be the
223 * same one we are currently on anyways, we can stop here. */
224 workspace->fullscreen_mode = CF_OUTPUT;
225 if (workspace == old)
228 workspace_reassign_sticky(workspace);
230 LOG("switching to %p\n", workspace);
231 Con *next = con_descend_focused(workspace);
233 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
234 /* check if this workspace is currently visible */
235 if (!workspace_is_visible(old)) {
236 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
237 tree_close(old, false, false);
238 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
243 workspace->fullscreen_mode = CF_OUTPUT;
244 LOG("focused now = %p / %s\n", focused, focused->name);
246 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
249 /* Check if the workspace has not been used yet */
250 workspace_initialize(t_ws, c_ws->output, false);
252 if (c_ws->output != t_ws->output) {
253 /* We need to switch to the other output first */
254 DLOG("moving over to other output.\n");
256 /* Store the old client */
257 Client *old_client = CUR_CELL->currently_focused;
259 c_ws = t_ws->output->current_workspace;
260 current_col = c_ws->current_col;
261 current_row = c_ws->current_row;
262 if (CUR_CELL->currently_focused != NULL)
265 Rect *dims = &(c_ws->output->rect);
266 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
267 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
270 /* Re-decorate the old client, it’s not focused anymore */
271 if ((old_client != NULL) && !old_client->dock)
272 redecorate_window(conn, old_client);
273 else xcb_flush(conn);
275 /* We need to check if a global fullscreen-client is blocking
276 * the t_ws and if necessary switch that to local fullscreen */
277 Client* client = c_ws->fullscreen_client;
278 if (client != NULL && client->workspace != c_ws) {
279 if (c_ws->fullscreen_client->workspace != c_ws)
280 c_ws->fullscreen_client = NULL;
281 client_enter_fullscreen(conn, client, false);
285 /* Check if we need to change something or if we’re already there */
286 if (c_ws->output->current_workspace->num == (workspace-1)) {
287 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
288 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
289 set_focus(conn, last_focused, true);
291 client_warp_pointer_into(conn, last_focused);
295 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
300 Workspace *old_workspace = c_ws;
301 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
303 /* Unmap all clients of the old workspace */
304 workspace_unmap_clients(conn, old_workspace);
306 current_row = c_ws->current_row;
307 current_col = c_ws->current_col;
308 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
310 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
312 workspace_map_clients(conn, c_ws);
314 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
315 * render_layout afterwards, there is a short flickering on the source
316 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
317 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
320 /* Restore focus on the new workspace */
321 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
322 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
323 set_focus(conn, last_focused, true);
324 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
328 /* We can warp the pointer only after the window has been
329 * reconfigured in render_layout, otherwise the pointer will
330 * be warped to the old position, which will not work when we
331 * moved it to another output. */
332 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
333 client_warp_pointer_into(conn, last_focused);
341 * Assigns the given workspace to the given output by correctly updating its
342 * state and reconfiguring all the clients on this workspace.
344 * This is called when initializing a output and when re-assigning it to a
345 * different output which just got available (if you configured it to be on
346 * output 1 and you just plugged in output 1).
349 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
352 bool visible = workspace_is_visible(ws);
356 /* Copy the dimensions from the virtual output */
357 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
359 ewmh_update_workarea();
361 /* Force reconfiguration for each client on that workspace */
362 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
363 client->force_reconfigure = true;
370 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
371 render_workspace(global_conn, output, ws);
373 /* …unless we want to see them at the moment, we should hide that workspace */
374 if (visible && !hide_it)
377 /* however, if this is the current workspace, we only need to adjust
378 * the output’s current_workspace pointer (and must not unmap the
381 DLOG("Need to adjust output->current_workspace...\n");
382 output->current_workspace = c_ws;
383 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
387 workspace_unmap_clients(global_conn, ws);
391 * Initializes the given workspace if it is not already initialized. The given
392 * screen is to be understood as a fallback, if the workspace itself either
393 * was not assigned to a particular screen or cannot be placed there because
394 * the screen is not attached at the moment.
397 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
400 if (ws->output != NULL && !recheck) {
401 DLOG("Workspace already initialized\n");
405 old_output = ws->output;
407 /* If this workspace has no preferred output or if the output it wants
408 * to be on is not available at the moment, we initialize it with
409 * the output which was given */
410 if (ws->preferred_output == NULL ||
411 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
414 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
415 /* If the assignment did not change, we do not need to update anything */
416 if (old_output != NULL && ws->output == old_output)
419 workspace_assign_to(ws, ws->output, false);
423 * Gets the first unused workspace for the given screen, taking into account
424 * the preferred_output setting of every workspace (workspace assignments).
427 Workspace *get_first_workspace_for_output(Output *output) {
428 Workspace *result = NULL;
431 TAILQ_FOREACH(ws, workspaces, workspaces) {
432 if (ws->preferred_output == NULL ||
433 get_output_by_name(ws->preferred_output) != output)
440 if (result == NULL) {
441 /* No assignment found, returning first unused workspace */
442 TAILQ_FOREACH(ws, workspaces, workspaces) {
443 if (ws->output != NULL)
451 if (result == NULL) {
452 DLOG("No existing free workspace found to assign, creating a new one\n");
455 TAILQ_FOREACH(ws, workspaces, workspaces)
457 result = workspace_get(last_ws + 1);
460 workspace_initialize(result, output, false);
466 static bool get_urgency_flag(Con *con) {
468 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
469 if (child->urgent || get_urgency_flag(child))
472 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
473 if (child->urgent || get_urgency_flag(child))
480 * Goes through all clients on the given workspace and updates the workspace’s
481 * urgent flag accordingly.
484 void workspace_update_urgent_flag(Con *ws) {
485 bool old_flag = ws->urgent;
486 ws->urgent = get_urgency_flag(ws);
487 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
489 if (old_flag != ws->urgent)
490 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
494 * 'Forces' workspace orientation by moving all cons into a new split-con with
495 * the same orientation as the workspace and then changing the workspace
499 void ws_force_orientation(Con *ws, orientation_t orientation) {
500 /* 1: create a new split container */
501 Con *split = con_new(NULL);
504 /* 2: copy layout and orientation from workspace */
505 split->layout = ws->layout;
506 split->orientation = ws->orientation;
508 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
510 /* 3: move the existing cons of this workspace below the new con */
511 DLOG("Moving cons\n");
512 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
513 Con *child = TAILQ_FIRST(&(ws->nodes_head));
515 con_attach(child, split, true);
518 /* 4: switch workspace orientation */
519 ws->orientation = orientation;
521 /* 5: attach the new split container to the workspace */
522 DLOG("Attaching new split to ws\n");
523 con_attach(split, ws, false);
525 /* 6: fix the percentages */
529 con_focus(old_focused);