+void output_init_con(Output *output) {
+ Con *con = NULL, *current;
+ bool reused = false;
+
+ DLOG("init_con for output %s\n", output->name);
+
+ /* Search for a Con with that name directly below the root node. There
+ * might be one from a restored layout. */
+ TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
+ if (strcmp(current->name, output->name) != 0)
+ continue;
+
+ con = current;
+ reused = true;
+ DLOG("Using existing con %p / %s\n", con, con->name);
+ break;
+ }
+
+ if (con == NULL) {
+ con = con_new(croot, NULL);
+ FREE(con->name);
+ con->name = sstrdup(output->name);
+ con->type = CT_OUTPUT;
+ con->layout = L_OUTPUT;
+ con_fix_percent(croot);
+ }
+ con->rect = output->rect;
+ output->con = con;
+
+ char *name;
+ asprintf(&name, "[i3 con] output %s", con->name);
+ x_set_name(con, name);
+ FREE(name);
+
+ if (reused) {
+ DLOG("Not adding workspace, this was a reused con\n");
+ return;
+ }
+
+ DLOG("Changing layout, adding top/bottom dockarea\n");
+ Con *topdock = con_new(NULL, NULL);
+ topdock->type = CT_DOCKAREA;
+ topdock->layout = L_DOCKAREA;
+ topdock->orientation = VERT;
+ /* this container swallows dock clients */
+ Match *match = scalloc(sizeof(Match));
+ match_init(match);
+ match->dock = M_DOCK_TOP;
+ match->insert_where = M_BELOW;
+ TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
+
+ FREE(topdock->name);
+ topdock->name = sstrdup("topdock");
+
+ asprintf(&name, "[i3 con] top dockarea %s", con->name);
+ x_set_name(topdock, name);
+ FREE(name);
+ DLOG("attaching\n");
+ con_attach(topdock, con, false);
+
+ /* content container */
+
+ DLOG("adding main content container\n");
+ Con *content = con_new(NULL, NULL);
+ content->type = CT_CON;
+ FREE(content->name);
+ content->name = sstrdup("content");
+
+ asprintf(&name, "[i3 con] content %s", con->name);
+ x_set_name(content, name);
+ FREE(name);
+ con_attach(content, con, false);
+
+ /* bottom dock container */
+ Con *bottomdock = con_new(NULL, NULL);
+ bottomdock->type = CT_DOCKAREA;
+ bottomdock->layout = L_DOCKAREA;
+ bottomdock->orientation = VERT;
+ /* this container swallows dock clients */
+ match = scalloc(sizeof(Match));
+ match_init(match);
+ match->dock = M_DOCK_BOTTOM;
+ match->insert_where = M_BELOW;
+ TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
+
+ FREE(bottomdock->name);
+ bottomdock->name = sstrdup("bottomdock");
+
+ asprintf(&name, "[i3 con] bottom dockarea %s", con->name);
+ x_set_name(bottomdock, name);
+ FREE(name);
+ DLOG("attaching\n");
+ con_attach(bottomdock, con, false);
+}
+
+/*
+ * Initializes at least one workspace for this output, trying the following
+ * steps until there is at least one workspace:
+ *
+ * • Move existing workspaces, which are assigned to be on the given output, to
+ * the output.
+ * • Create the first assigned workspace for this output.
+ * • Create the first unused workspace.
+ *
+ */
+void init_ws_for_output(Output *output, Con *content) {
+ char *name;
+
+ /* go through all assignments and move the existing workspaces to this output */
+ struct Workspace_Assignment *assignment;
+ TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
+ if (strcmp(assignment->output, output->name) != 0)
+ continue;
+
+ /* check if this workspace actually exists */
+ Con *workspace = NULL, *out;
+ TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
+ GREP_FIRST(workspace, output_get_content(out),
+ !strcasecmp(child->name, assignment->name));
+ if (workspace == NULL)
+ continue;
+
+ /* check that this workspace is not already attached (that means the
+ * user configured this assignment twice) */
+ Con *workspace_out = con_get_output(workspace);
+ if (workspace_out == output->con) {
+ LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
+ "there. Do you have two assignment directives for the same "
+ "workspace in your configuration file?\n",
+ workspace->name, output->name);
+ continue;
+ }
+
+ /* if so, move it over */
+ LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
+ workspace->name, workspace_out->name, output->name);
+
+ /* if the workspace is currently visible on that output, we need to
+ * switch to a different workspace - otherwise the output would end up
+ * with no active workspace */
+ bool visible = workspace_is_visible(workspace);
+ Con *previous = NULL;
+ if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
+ LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
+ previous->name, workspace_out->name);
+ workspace_show(previous->name);
+ }
+
+ con_detach(workspace);
+ con_attach(workspace, content, false);
+
+ /* In case the workspace we just moved was visible but there was no
+ * other workspace to switch to, we need to initialize the source
+ * output aswell */
+ if (visible && previous == NULL) {
+ LOG("There is no workspace left on \"%s\", re-initializing\n",
+ workspace_out->name);
+ init_ws_for_output(get_output_by_name(workspace_out->name),
+ output_get_content(workspace_out));
+ DLOG("Done re-initializing, continuing with \"%s\"\n", output->name);
+ }
+ }
+
+ /* if a workspace exists, we are done now */
+ if (!TAILQ_EMPTY(&(content->nodes_head))) {
+ /* ensure that one of the workspaces is actually visible (in fullscreen
+ * mode), if they were invisible before, this might not be the case. */
+ Con *visible = NULL;
+ GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
+ if (!visible) {
+ visible = TAILQ_FIRST(&(content->nodes_head));
+ focused = content;
+ workspace_show(visible->name);
+ }
+ return;
+ }
+
+ /* otherwise, we create the first assigned ws for this output */
+ TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
+ if (strcmp(assignment->output, output->name) != 0)
+ continue;
+
+ LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
+ assignment->name, assignment->output);
+ focused = content;
+ workspace_show(assignment->name);
+ return;
+ }
+
+ /* if there is still no workspace, we create the first free workspace */
+ DLOG("Now adding a workspace\n");
+
+ /* add a workspace to this output */
+ Con *out, *current;
+ bool exists = true;
+ Con *ws = con_new(NULL, NULL);
+ ws->type = CT_WORKSPACE;
+
+ /* try the configured workspace bindings first to find a free name */
+ Binding *bind;
+ TAILQ_FOREACH(bind, bindings, bindings) {
+ DLOG("binding with command %s\n", bind->command);
+ if (strlen(bind->command) < strlen("workspace ") ||
+ strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
+ continue;
+ DLOG("relevant command = %s\n", bind->command);
+ char *target = bind->command + strlen("workspace ");
+ /* We check if this is the workspace next/prev command. Beware: The
+ * workspace names "next" and "prev" are OK, so we check before
+ * stripping the double quotes */
+ if (strncasecmp(target, "next", strlen("next")) == 0 ||
+ strncasecmp(target, "prev", strlen("prev")) == 0)
+ continue;
+ if (*target == '"')
+ target++;
+ FREE(ws->name);
+ ws->name = strdup(target);
+ if (ws->name[strlen(ws->name)-1] == '"')
+ ws->name[strlen(ws->name)-1] = '\0';
+ DLOG("trying name *%s*\n", ws->name);
+
+ current = NULL;
+ TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
+ GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
+
+ exists = (current != NULL);
+ if (!exists) {
+ /* Set ->num to the number of the workspace, if the name actually
+ * is a number or starts with a number */
+ char *endptr = NULL;
+ long parsed_num = strtol(ws->name, &endptr, 10);
+ if (parsed_num == LONG_MIN ||
+ parsed_num == LONG_MAX ||
+ parsed_num < 0 ||
+ endptr == ws->name)
+ ws->num = -1;
+ else ws->num = parsed_num;
+ LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
+
+ break;
+ }
+ }