i3
src/workspace.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * workspace.c: Modifying workspaces, accessing them, moving containers to
00008  *              workspaces.
00009  *
00010  */
00011 #include "all.h"
00012 
00013 /* Stores a copy of the name of the last used workspace for the workspace
00014  * back-and-forth switching. */
00015 static char *previous_workspace_name = NULL;
00016 
00017 /*
00018  * Returns a pointer to the workspace with the given number (starting at 0),
00019  * creating the workspace if necessary (by allocating the necessary amount of
00020  * memory and initializing the data structures correctly).
00021  *
00022  */
00023 Con *workspace_get(const char *num, bool *created) {
00024     Con *output, *workspace = NULL;
00025 
00026     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
00027         GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
00028 
00029     if (workspace == NULL) {
00030         LOG("Creating new workspace \"%s\"\n", num);
00031         /* unless an assignment is found, we will create this workspace on the current output */
00032         output = con_get_output(focused);
00033         /* look for assignments */
00034         struct Workspace_Assignment *assignment;
00035         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
00036             if (strcmp(assignment->name, num) != 0)
00037                 continue;
00038 
00039             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
00040             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
00041             break;
00042         }
00043         Con *content = output_get_content(output);
00044         LOG("got output %p with content %p\n", output, content);
00045         /* We need to attach this container after setting its type. con_attach
00046          * will handle CT_WORKSPACEs differently */
00047         workspace = con_new(NULL, NULL);
00048         char *name;
00049         sasprintf(&name, "[i3 con] workspace %s", num);
00050         x_set_name(workspace, name);
00051         free(name);
00052         workspace->type = CT_WORKSPACE;
00053         FREE(workspace->name);
00054         workspace->name = sstrdup(num);
00055         /* We set ->num to the number if this workspace’s name begins with a
00056          * positive number. Otherwise it’s a named ws and num will be -1. */
00057         char *endptr = NULL;
00058         long parsed_num = strtol(num, &endptr, 10);
00059         if (parsed_num == LONG_MIN ||
00060             parsed_num == LONG_MAX ||
00061             parsed_num < 0 ||
00062             endptr == num)
00063             workspace->num = -1;
00064         else workspace->num = parsed_num;
00065         LOG("num = %d\n", workspace->num);
00066 
00067         /* If default_orientation is set to NO_ORIENTATION we
00068          * determine workspace orientation from workspace size.
00069          * Otherwise we just set the orientation to default_orientation. */
00070         if (config.default_orientation == NO_ORIENTATION) {
00071             workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
00072             DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
00073                  workspace->rect.width, workspace->rect.height, workspace->orientation);
00074         } else {
00075             workspace->orientation = config.default_orientation;
00076         }
00077 
00078         con_attach(workspace, content, false);
00079 
00080         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
00081         if (created != NULL)
00082             *created = true;
00083     }
00084     else if (created != NULL) {
00085         *created = false;
00086     }
00087 
00088     return workspace;
00089 }
00090 
00091 /*
00092  * Returns a pointer to a new workspace in the given output. The workspace
00093  * is created attached to the tree hierarchy through the given content
00094  * container.
00095  *
00096  */
00097 Con *create_workspace_on_output(Output *output, Con *content) {
00098     /* add a workspace to this output */
00099     Con *out, *current;
00100     char *name;
00101     bool exists = true;
00102     Con *ws = con_new(NULL, NULL);
00103     ws->type = CT_WORKSPACE;
00104 
00105     /* try the configured workspace bindings first to find a free name */
00106     Binding *bind;
00107     TAILQ_FOREACH(bind, bindings, bindings) {
00108         DLOG("binding with command %s\n", bind->command);
00109         if (strlen(bind->command) < strlen("workspace ") ||
00110             strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
00111             continue;
00112         DLOG("relevant command = %s\n", bind->command);
00113         char *target = bind->command + strlen("workspace ");
00114         /* We check if this is the workspace
00115          * next/prev/next_on_output/prev_on_output/back_and_forth command.
00116          * Beware: The workspace names "next", "prev", "next_on_output",
00117          * "prev_on_output" and "back_and_forth" are OK, so we check before
00118          * stripping the double quotes */
00119         if (strncasecmp(target, "next", strlen("next")) == 0 ||
00120             strncasecmp(target, "prev", strlen("prev")) == 0 ||
00121             strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
00122             strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
00123             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0)
00124             continue;
00125         if (*target == '"')
00126             target++;
00127         FREE(ws->name);
00128         ws->name = strdup(target);
00129         if (ws->name[strlen(ws->name)-1] == '"')
00130             ws->name[strlen(ws->name)-1] = '\0';
00131         DLOG("trying name *%s*\n", ws->name);
00132 
00133         /* Ensure that this workspace is not assigned to a different output —
00134          * otherwise we would create it, then move it over to its output, then
00135          * find a new workspace, etc… */
00136         bool assigned = false;
00137         struct Workspace_Assignment *assignment;
00138         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
00139             if (strcmp(assignment->name, ws->name) != 0 ||
00140                 strcmp(assignment->output, output->name) == 0)
00141                 continue;
00142 
00143             assigned = true;
00144             break;
00145         }
00146 
00147         if (assigned)
00148             continue;
00149 
00150         current = NULL;
00151         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
00152             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
00153 
00154         exists = (current != NULL);
00155         if (!exists) {
00156             /* Set ->num to the number of the workspace, if the name actually
00157              * is a number or starts with a number */
00158             char *endptr = NULL;
00159             long parsed_num = strtol(ws->name, &endptr, 10);
00160             if (parsed_num == LONG_MIN ||
00161                 parsed_num == LONG_MAX ||
00162                 parsed_num < 0 ||
00163                 endptr == ws->name)
00164                 ws->num = -1;
00165             else ws->num = parsed_num;
00166             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
00167 
00168             break;
00169         }
00170     }
00171 
00172     if (exists) {
00173         /* get the next unused workspace number */
00174         DLOG("Getting next unused workspace by number\n");
00175         int c = 0;
00176         while (exists) {
00177             c++;
00178 
00179             FREE(ws->name);
00180             sasprintf(&(ws->name), "%d", c);
00181 
00182             current = NULL;
00183             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
00184                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
00185             exists = (current != NULL);
00186 
00187             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
00188         }
00189         ws->num = c;
00190     }
00191     con_attach(ws, content, false);
00192 
00193     sasprintf(&name, "[i3 con] workspace %s", ws->name);
00194     x_set_name(ws, name);
00195     free(name);
00196 
00197     ws->fullscreen_mode = CF_OUTPUT;
00198 
00199     /* If default_orientation is set to NO_ORIENTATION we determine
00200      * orientation depending on output resolution. */
00201     if (config.default_orientation == NO_ORIENTATION) {
00202         ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
00203         DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
00204              output->rect.width, output->rect.height, ws->orientation);
00205     } else {
00206         ws->orientation = config.default_orientation;
00207     }
00208 
00209     return ws;
00210 }
00211 
00212 /*
00213  * Returns true if the workspace is currently visible. Especially important for
00214  * multi-monitor environments, as they can have multiple currenlty active
00215  * workspaces.
00216  *
00217  */
00218 bool workspace_is_visible(Con *ws) {
00219     Con *output = con_get_output(ws);
00220     if (output == NULL)
00221         return false;
00222     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
00223     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
00224     return (fs == ws);
00225 }
00226 
00227 /*
00228  * XXX: we need to clean up all this recursive walking code.
00229  *
00230  */
00231 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
00232     Con *current;
00233 
00234     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
00235         if (current != exclude &&
00236             current->sticky_group != NULL &&
00237             current->window != NULL &&
00238             strcmp(current->sticky_group, sticky_group) == 0)
00239             return current;
00240 
00241         Con *recurse = _get_sticky(current, sticky_group, exclude);
00242         if (recurse != NULL)
00243             return recurse;
00244     }
00245 
00246     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
00247         if (current != exclude &&
00248             current->sticky_group != NULL &&
00249             current->window != NULL &&
00250             strcmp(current->sticky_group, sticky_group) == 0)
00251             return current;
00252 
00253         Con *recurse = _get_sticky(current, sticky_group, exclude);
00254         if (recurse != NULL)
00255             return recurse;
00256     }
00257 
00258     return NULL;
00259 }
00260 
00261 /*
00262  * Reassigns all child windows in sticky containers. Called when the user
00263  * changes workspaces.
00264  *
00265  * XXX: what about sticky containers which contain containers?
00266  *
00267  */
00268 static void workspace_reassign_sticky(Con *con) {
00269     Con *current;
00270     /* 1: go through all containers */
00271 
00272     /* handle all children and floating windows of this node */
00273     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
00274         if (current->sticky_group == NULL) {
00275             workspace_reassign_sticky(current);
00276             continue;
00277         }
00278 
00279         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
00280         /* 2: find a window which we can re-assign */
00281         Con *output = con_get_output(current);
00282         Con *src = _get_sticky(output, current->sticky_group, current);
00283 
00284         if (src == NULL) {
00285             LOG("No window found for this sticky group\n");
00286             workspace_reassign_sticky(current);
00287             continue;
00288         }
00289 
00290         x_move_win(src, current);
00291         current->window = src->window;
00292         current->mapped = true;
00293         src->window = NULL;
00294         src->mapped = false;
00295 
00296         x_reparent_child(current, src);
00297 
00298         LOG("re-assigned window from src %p to dest %p\n", src, current);
00299     }
00300 
00301     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
00302         workspace_reassign_sticky(current);
00303 }
00304 
00305 
00306 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
00307     Con *current, *old = NULL;
00308 
00309     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
00310     if (workspace->name[0] == '_' && workspace->name[1] == '_')
00311         return;
00312 
00313     /* disable fullscreen for the other workspaces and get the workspace we are
00314      * currently on. */
00315     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
00316         if (current->fullscreen_mode == CF_OUTPUT)
00317             old = current;
00318         current->fullscreen_mode = CF_NONE;
00319     }
00320 
00321     /* enable fullscreen for the target workspace. If it happens to be the
00322      * same one we are currently on anyways, we can stop here. */
00323     workspace->fullscreen_mode = CF_OUTPUT;
00324     current = con_get_workspace(focused);
00325     if (workspace == current) {
00326         DLOG("Not switching, already there.\n");
00327         return;
00328     }
00329 
00330     /* Remember currently focused workspace for switching back to it later with
00331      * the 'workspace back_and_forth' command.
00332      * NOTE: We have to duplicate the name as the original will be freed when
00333      * the corresponding workspace is cleaned up. */
00334 
00335     FREE(previous_workspace_name);
00336     if (current)
00337         previous_workspace_name = sstrdup(current->name);
00338 
00339     workspace_reassign_sticky(workspace);
00340 
00341     LOG("switching to %p\n", workspace);
00342     Con *next = con_descend_focused(workspace);
00343 
00344     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
00345         /* check if this workspace is currently visible */
00346         if (!workspace_is_visible(old)) {
00347             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
00348             tree_close(old, DONT_KILL_WINDOW, false, false);
00349             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
00350             changed_num_workspaces = true;
00351         }
00352     }
00353 
00354     /* Memorize current output */
00355     Con *old_output = con_get_output(focused);
00356 
00357     con_focus(next);
00358     workspace->fullscreen_mode = CF_OUTPUT;
00359     LOG("focused now = %p / %s\n", focused, focused->name);
00360 
00361     /* Set mouse pointer */
00362     Con *new_output = con_get_output(focused);
00363     if (old_output != new_output) {
00364         x_set_warp_to(&next->rect);
00365     }
00366 
00367     /* Update the EWMH hints */
00368     ewmh_update_current_desktop();
00369 
00370     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00371 }
00372 
00373 /*
00374  * Switches to the given workspace
00375  *
00376  */
00377 void workspace_show(Con *workspace) {
00378     _workspace_show(workspace, false);
00379 }
00380 
00381 /*
00382  * Looks up the workspace by name and switches to it.
00383  *
00384  */
00385 void workspace_show_by_name(const char *num) {
00386     Con *workspace;
00387     bool changed_num_workspaces;
00388     workspace = workspace_get(num, &changed_num_workspaces);
00389     _workspace_show(workspace, changed_num_workspaces);
00390 }
00391 
00392 /*
00393  * Focuses the next workspace.
00394  *
00395  */
00396 Con* workspace_next(void) {
00397     Con *current = con_get_workspace(focused);
00398     Con *next = NULL;
00399     Con *output;
00400 
00401     if (current->num == -1) {
00402         /* If currently a named workspace, find next named workspace. */
00403         next = TAILQ_NEXT(current, nodes);
00404     } else {
00405         /* If currently a numbered workspace, find next numbered workspace. */
00406         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
00407             /* Skip outputs starting with __, they are internal. */
00408             if (output->name[0] == '_' && output->name[1] == '_')
00409                 continue;
00410             NODES_FOREACH(output_get_content(output)) {
00411                 if (child->type != CT_WORKSPACE)
00412                     continue;
00413                 if (child->num == -1)
00414                     break;
00415                 /* Need to check child against current and next because we are
00416                  * traversing multiple lists and thus are not guaranteed the
00417                  * relative order between the list of workspaces. */
00418                 if (current->num < child->num && (!next || child->num < next->num))
00419                     next = child;
00420             }
00421         }
00422     }
00423 
00424     /* Find next named workspace. */
00425     if (!next) {
00426         bool found_current = false;
00427         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
00428             /* Skip outputs starting with __, they are internal. */
00429             if (output->name[0] == '_' && output->name[1] == '_')
00430                 continue;
00431             NODES_FOREACH(output_get_content(output)) {
00432                 if (child->type != CT_WORKSPACE)
00433                     continue;
00434                 if (child == current) {
00435                     found_current = 1;
00436                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
00437                     next = child;
00438                     goto workspace_next_end;
00439                 }
00440             }
00441         }
00442     }
00443 
00444     /* Find first workspace. */
00445     if (!next) {
00446         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
00447             /* Skip outputs starting with __, they are internal. */
00448             if (output->name[0] == '_' && output->name[1] == '_')
00449                 continue;
00450             NODES_FOREACH(output_get_content(output)) {
00451                 if (child->type != CT_WORKSPACE)
00452                     continue;
00453                 if (!next || (child->num != -1 && child->num < next->num))
00454                     next = child;
00455             }
00456         }
00457     }
00458 workspace_next_end:
00459     return next;
00460 }
00461 
00462 /*
00463  * Focuses the previous workspace.
00464  *
00465  */
00466 Con* workspace_prev(void) {
00467     Con *current = con_get_workspace(focused);
00468     Con *prev = NULL;
00469     Con *output;
00470 
00471     if (current->num == -1) {
00472         /* If named workspace, find previous named workspace. */
00473         prev = TAILQ_PREV(current, nodes_head, nodes);
00474         if (prev && prev->num != -1)
00475             prev = NULL;
00476     } else {
00477         /* If numbered workspace, find previous numbered workspace. */
00478         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
00479             /* Skip outputs starting with __, they are internal. */
00480             if (output->name[0] == '_' && output->name[1] == '_')
00481                 continue;
00482             NODES_FOREACH_REVERSE(output_get_content(output)) {
00483                 if (child->type != CT_WORKSPACE || child->num == -1)
00484                     continue;
00485                 /* Need to check child against current and previous because we
00486                  * are traversing multiple lists and thus are not guaranteed
00487                  * the relative order between the list of workspaces. */
00488                 if (current->num > child->num && (!prev || child->num > prev->num))
00489                     prev = child;
00490             }
00491         }
00492     }
00493 
00494     /* Find previous named workspace. */
00495     if (!prev) {
00496         bool found_current = false;
00497         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
00498             /* Skip outputs starting with __, they are internal. */
00499             if (output->name[0] == '_' && output->name[1] == '_')
00500                 continue;
00501             NODES_FOREACH_REVERSE(output_get_content(output)) {
00502                 if (child->type != CT_WORKSPACE)
00503                     continue;
00504                 if (child == current) {
00505                     found_current = true;
00506                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
00507                     prev = child;
00508                     goto workspace_prev_end;
00509                 }
00510             }
00511         }
00512     }
00513 
00514     /* Find last workspace. */
00515     if (!prev) {
00516         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
00517             /* Skip outputs starting with __, they are internal. */
00518             if (output->name[0] == '_' && output->name[1] == '_')
00519                 continue;
00520             NODES_FOREACH_REVERSE(output_get_content(output)) {
00521                 if (child->type != CT_WORKSPACE)
00522                     continue;
00523                 if (!prev || child->num > prev->num)
00524                     prev = child;
00525             }
00526         }
00527     }
00528 
00529 workspace_prev_end:
00530     return prev;
00531 }
00532 
00533 
00534 /*
00535  * Focuses the next workspace on the same output.
00536  *
00537  */
00538 Con* workspace_next_on_output(void) {
00539     Con *current = con_get_workspace(focused);
00540     Con *next = NULL;
00541     Con *output  = con_get_output(focused);
00542 
00543     if (current->num == -1) {
00544         /* If currently a named workspace, find next named workspace. */
00545         next = TAILQ_NEXT(current, nodes);
00546     } else {
00547         /* If currently a numbered workspace, find next numbered workspace. */
00548         NODES_FOREACH(output_get_content(output)) {
00549             if (child->type != CT_WORKSPACE)
00550                 continue;
00551             if (child->num == -1)
00552                 break;
00553             /* Need to check child against current and next because we are
00554              * traversing multiple lists and thus are not guaranteed the
00555              * relative order between the list of workspaces. */
00556             if (current->num < child->num && (!next || child->num < next->num))
00557                 next = child;
00558             }
00559         }
00560 
00561     /* Find next named workspace. */
00562     if (!next) {
00563         bool found_current = false;
00564         NODES_FOREACH(output_get_content(output)) {
00565             if (child->type != CT_WORKSPACE)
00566                 continue;
00567             if (child == current) {
00568                 found_current = 1;
00569             } else if (child->num == -1 && (current->num != -1 || found_current)) {
00570                 next = child;
00571                 goto workspace_next_on_output_end;
00572             }
00573         }
00574     }
00575 
00576     /* Find first workspace. */
00577     if (!next) {
00578         NODES_FOREACH(output_get_content(output)) {
00579             if (child->type != CT_WORKSPACE)
00580                 continue;
00581             if (!next || (child->num != -1 && child->num < next->num))
00582                 next = child;
00583         }
00584     }
00585 workspace_next_on_output_end:
00586     return next;
00587 }
00588 
00589 /*
00590  * Focuses the previous workspace on same output.
00591  *
00592  */
00593 Con* workspace_prev_on_output(void) {
00594     Con *current = con_get_workspace(focused);
00595     Con *prev = NULL;
00596     Con *output  = con_get_output(focused);
00597     DLOG("output = %s\n", output->name);
00598 
00599     if (current->num == -1) {
00600         /* If named workspace, find previous named workspace. */
00601         prev = TAILQ_PREV(current, nodes_head, nodes);
00602         if (prev && prev->num != -1)
00603             prev = NULL;
00604     } else {
00605         /* If numbered workspace, find previous numbered workspace. */
00606         NODES_FOREACH_REVERSE(output_get_content(output)) {
00607             if (child->type != CT_WORKSPACE || child->num == -1)
00608                 continue;
00609              /* Need to check child against current and previous because we
00610              * are traversing multiple lists and thus are not guaranteed
00611              * the relative order between the list of workspaces. */
00612             if (current->num > child->num && (!prev || child->num > prev->num))
00613                 prev = child;
00614         }
00615     }
00616 
00617     /* Find previous named workspace. */
00618     if (!prev) {
00619         bool found_current = false;
00620         NODES_FOREACH_REVERSE(output_get_content(output)) {
00621             if (child->type != CT_WORKSPACE)
00622                 continue;
00623             if (child == current) {
00624                 found_current = true;
00625             } else if (child->num == -1 && (current->num != -1 || found_current)) {
00626                 prev = child;
00627                 goto workspace_prev_on_output_end;
00628             }
00629         }
00630     }
00631 
00632     /* Find last workspace. */
00633     if (!prev) {
00634         NODES_FOREACH_REVERSE(output_get_content(output)) {
00635             if (child->type != CT_WORKSPACE)
00636                 continue;
00637             if (!prev || child->num > prev->num)
00638                 prev = child;
00639         }
00640     }
00641 
00642 workspace_prev_on_output_end:
00643     return prev;
00644 }
00645 
00646 /*
00647  * Focuses the previously focused workspace.
00648  *
00649  */
00650 void workspace_back_and_forth(void) {
00651     if (!previous_workspace_name) {
00652         DLOG("No previous workspace name set. Not switching.");
00653         return;
00654     }
00655 
00656     workspace_show_by_name(previous_workspace_name);
00657 }
00658 
00659 static bool get_urgency_flag(Con *con) {
00660     Con *child;
00661     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
00662         if (child->urgent || get_urgency_flag(child))
00663             return true;
00664 
00665     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
00666         if (child->urgent || get_urgency_flag(child))
00667             return true;
00668 
00669     return false;
00670 }
00671 
00672 /*
00673  * Goes through all clients on the given workspace and updates the workspace’s
00674  * urgent flag accordingly.
00675  *
00676  */
00677 void workspace_update_urgent_flag(Con *ws) {
00678     bool old_flag = ws->urgent;
00679     ws->urgent = get_urgency_flag(ws);
00680     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
00681 
00682     if (old_flag != ws->urgent)
00683         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
00684 }
00685 
00686 /*
00687  * 'Forces' workspace orientation by moving all cons into a new split-con with
00688  * the same orientation as the workspace and then changing the workspace
00689  * orientation.
00690  *
00691  */
00692 void ws_force_orientation(Con *ws, orientation_t orientation) {
00693     /* 1: create a new split container */
00694     Con *split = con_new(NULL, NULL);
00695     split->parent = ws;
00696 
00697     /* 2: copy layout and orientation from workspace */
00698     split->layout = ws->layout;
00699     split->orientation = ws->orientation;
00700 
00701     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
00702 
00703     /* 3: move the existing cons of this workspace below the new con */
00704     DLOG("Moving cons\n");
00705     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
00706         Con *child = TAILQ_FIRST(&(ws->nodes_head));
00707         con_detach(child);
00708         con_attach(child, split, true);
00709     }
00710 
00711     /* 4: switch workspace orientation */
00712     ws->orientation = orientation;
00713 
00714     /* 5: attach the new split container to the workspace */
00715     DLOG("Attaching new split to ws\n");
00716     con_attach(split, ws, false);
00717 
00718     /* 6: fix the percentages */
00719     con_fix_percent(ws);
00720 
00721     if (old_focused)
00722         con_focus(old_focused);
00723 }
00724 
00725 /*
00726  * Called when a new con (with a window, not an empty or split con) should be
00727  * attached to the workspace (for example when managing a new window or when
00728  * moving an existing window to the workspace level).
00729  *
00730  * Depending on the workspace_layout setting, this function either returns the
00731  * workspace itself (default layout) or creates a new stacked/tabbed con and
00732  * returns that.
00733  *
00734  */
00735 Con *workspace_attach_to(Con *ws) {
00736     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
00737 
00738     if (config.default_layout == L_DEFAULT) {
00739         DLOG("Default layout, just attaching it to the workspace itself.\n");
00740         return ws;
00741     }
00742 
00743     DLOG("Non-default layout, creating a new split container\n");
00744     /* 1: create a new split container */
00745     Con *new = con_new(NULL, NULL);
00746     new->parent = ws;
00747 
00748     /* 2: set the requested layout on the split con */
00749     new->layout = config.default_layout;
00750 
00751     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
00752      * to be set. Otherwise, this con will not be interpreted as a split
00753      * container. */
00754     if (config.default_orientation == NO_ORIENTATION) {
00755         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
00756     } else {
00757         new->orientation = config.default_orientation;
00758     }
00759 
00760     /* 4: attach the new split container to the workspace */
00761     DLOG("Attaching new split %p to workspace %p\n", new, ws);
00762     con_attach(new, ws, false);
00763 
00764     return new;
00765 }