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 /* 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 workspace->name = strdup(num);
50 /* We set ->num to the number if this workspace’s name consists only of
51 * a positive number. Otherwise it’s a named ws and num will be -1. */
53 long parsed_num = strtol(num, &end, 10);
54 if (parsed_num == LONG_MIN ||
55 parsed_num == LONG_MAX ||
57 (end && *end != '\0'))
59 else workspace->num = parsed_num;
60 LOG("num = %d\n", workspace->num);
61 workspace->orientation = HORIZ;
62 con_attach(workspace, output, false);
64 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
67 //ewmh_update_workarea();
75 * Sets the name (or just its number) for the given workspace. This has to
76 * be called for every workspace as the rendering function
77 * (render_internal_bar) relies on workspace->name and workspace->name_len
81 void workspace_set_name(Workspace *ws, const char *name) {
86 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
87 else ret = asprintf(&label, "%d", ws->num + 1);
90 errx(1, "asprintf() failed");
95 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
96 if (config.font != NULL)
97 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
98 else ws->text_width = 0;
99 ws->utf8_name = label;
104 * Returns true if the workspace is currently visible. Especially important for
105 * multi-monitor environments, as they can have multiple currenlty active
109 bool workspace_is_visible(Con *ws) {
110 Con *output = con_get_output(ws);
113 Con *fs = con_get_fullscreen_con(output);
114 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
119 * XXX: we need to clean up all this recursive walking code.
122 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
125 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
126 if (current != exclude &&
127 current->sticky_group != NULL &&
128 current->window != NULL &&
129 strcmp(current->sticky_group, sticky_group) == 0)
132 Con *recurse = _get_sticky(current, sticky_group, exclude);
137 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
138 if (current != exclude &&
139 current->sticky_group != NULL &&
140 current->window != NULL &&
141 strcmp(current->sticky_group, sticky_group) == 0)
144 Con *recurse = _get_sticky(current, sticky_group, exclude);
153 * Reassigns all child windows in sticky containers. Called when the user
154 * changes workspaces.
156 * XXX: what about sticky containers which contain containers?
159 static void workspace_reassign_sticky(Con *con) {
161 /* 1: go through all containers */
163 /* handle all children and floating windows of this node */
164 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
165 if (current->sticky_group == NULL) {
166 workspace_reassign_sticky(current);
170 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
171 /* 2: find a window which we can re-assign */
172 Con *output = con_get_output(current);
173 Con *src = _get_sticky(output, current->sticky_group, current);
176 LOG("No window found for this sticky group\n");
177 workspace_reassign_sticky(current);
181 x_move_win(src, current);
182 current->window = src->window;
183 current->mapped = true;
187 x_reparent_child(current, src);
189 LOG("re-assigned window from src %p to dest %p\n", src, current);
192 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
193 workspace_reassign_sticky(current);
197 * Switches to the given workspace
200 void workspace_show(const char *num) {
201 Con *workspace, *current, *old;
203 old = con_get_workspace(focused);
205 workspace = workspace_get(num);
206 if (workspace == old)
208 workspace->fullscreen_mode = CF_OUTPUT;
209 /* disable fullscreen */
210 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes)
211 current->fullscreen_mode = CF_NONE;
213 workspace_reassign_sticky(workspace);
215 LOG("switching to %p\n", workspace);
216 Con *next = workspace;
218 while (!TAILQ_EMPTY(&(next->focus_head)))
219 next = TAILQ_FIRST(&(next->focus_head));
222 if (TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
223 /* check if this workspace is currently visible */
224 if (!workspace_is_visible(old)) {
225 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
226 tree_close(old, false, false);
227 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
232 workspace->fullscreen_mode = CF_OUTPUT;
233 LOG("focused now = %p / %s\n", focused, focused->name);
235 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
238 /* Check if the workspace has not been used yet */
239 workspace_initialize(t_ws, c_ws->output, false);
241 if (c_ws->output != t_ws->output) {
242 /* We need to switch to the other output first */
243 DLOG("moving over to other output.\n");
245 /* Store the old client */
246 Client *old_client = CUR_CELL->currently_focused;
248 c_ws = t_ws->output->current_workspace;
249 current_col = c_ws->current_col;
250 current_row = c_ws->current_row;
251 if (CUR_CELL->currently_focused != NULL)
254 Rect *dims = &(c_ws->output->rect);
255 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
256 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
259 /* Re-decorate the old client, it’s not focused anymore */
260 if ((old_client != NULL) && !old_client->dock)
261 redecorate_window(conn, old_client);
262 else xcb_flush(conn);
264 /* We need to check if a global fullscreen-client is blocking
265 * the t_ws and if necessary switch that to local fullscreen */
266 Client* client = c_ws->fullscreen_client;
267 if (client != NULL && client->workspace != c_ws) {
268 if (c_ws->fullscreen_client->workspace != c_ws)
269 c_ws->fullscreen_client = NULL;
270 client_enter_fullscreen(conn, client, false);
274 /* Check if we need to change something or if we’re already there */
275 if (c_ws->output->current_workspace->num == (workspace-1)) {
276 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
277 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
278 set_focus(conn, last_focused, true);
280 client_warp_pointer_into(conn, last_focused);
284 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
289 Workspace *old_workspace = c_ws;
290 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
292 /* Unmap all clients of the old workspace */
293 workspace_unmap_clients(conn, old_workspace);
295 current_row = c_ws->current_row;
296 current_col = c_ws->current_col;
297 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
299 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
301 workspace_map_clients(conn, c_ws);
303 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
304 * render_layout afterwards, there is a short flickering on the source
305 * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
306 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
309 /* Restore focus on the new workspace */
310 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
311 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
312 set_focus(conn, last_focused, true);
313 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
317 /* We can warp the pointer only after the window has been
318 * reconfigured in render_layout, otherwise the pointer will
319 * be warped to the old position, which will not work when we
320 * moved it to another output. */
321 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
322 client_warp_pointer_into(conn, last_focused);
330 * Assigns the given workspace to the given output by correctly updating its
331 * state and reconfiguring all the clients on this workspace.
333 * This is called when initializing a output and when re-assigning it to a
334 * different output which just got available (if you configured it to be on
335 * output 1 and you just plugged in output 1).
338 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
341 bool visible = workspace_is_visible(ws);
345 /* Copy the dimensions from the virtual output */
346 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
348 ewmh_update_workarea();
350 /* Force reconfiguration for each client on that workspace */
351 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
352 client->force_reconfigure = true;
359 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
360 render_workspace(global_conn, output, ws);
362 /* …unless we want to see them at the moment, we should hide that workspace */
363 if (visible && !hide_it)
366 /* however, if this is the current workspace, we only need to adjust
367 * the output’s current_workspace pointer (and must not unmap the
370 DLOG("Need to adjust output->current_workspace...\n");
371 output->current_workspace = c_ws;
372 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
376 workspace_unmap_clients(global_conn, ws);
380 * Initializes the given workspace if it is not already initialized. The given
381 * screen is to be understood as a fallback, if the workspace itself either
382 * was not assigned to a particular screen or cannot be placed there because
383 * the screen is not attached at the moment.
386 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
389 if (ws->output != NULL && !recheck) {
390 DLOG("Workspace already initialized\n");
394 old_output = ws->output;
396 /* If this workspace has no preferred output or if the output it wants
397 * to be on is not available at the moment, we initialize it with
398 * the output which was given */
399 if (ws->preferred_output == NULL ||
400 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
403 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
404 /* If the assignment did not change, we do not need to update anything */
405 if (old_output != NULL && ws->output == old_output)
408 workspace_assign_to(ws, ws->output, false);
412 * Gets the first unused workspace for the given screen, taking into account
413 * the preferred_output setting of every workspace (workspace assignments).
416 Workspace *get_first_workspace_for_output(Output *output) {
417 Workspace *result = NULL;
420 TAILQ_FOREACH(ws, workspaces, workspaces) {
421 if (ws->preferred_output == NULL ||
422 get_output_by_name(ws->preferred_output) != output)
429 if (result == NULL) {
430 /* No assignment found, returning first unused workspace */
431 TAILQ_FOREACH(ws, workspaces, workspaces) {
432 if (ws->output != NULL)
440 if (result == NULL) {
441 DLOG("No existing free workspace found to assign, creating a new one\n");
444 TAILQ_FOREACH(ws, workspaces, workspaces)
446 result = workspace_get(last_ws + 1);
449 workspace_initialize(result, output, false);
455 static bool get_urgency_flag(Con *con) {
457 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
458 if (child->urgent || get_urgency_flag(child))
461 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
462 if (child->urgent || get_urgency_flag(child))
469 * Goes through all clients on the given workspace and updates the workspace’s
470 * urgent flag accordingly.
473 void workspace_update_urgent_flag(Con *ws) {
474 bool old_flag = ws->urgent;
475 ws->urgent = get_urgency_flag(ws);
476 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
478 if (old_flag != ws->urgent)
479 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");