+
+#include "all.h"
+
+/*
+ * Returns a pointer to the workspace with the given number (starting at 0),
+ * creating the workspace if necessary (by allocating the necessary amount of
+ * memory and initializing the data structures correctly).
+ *
+ */
+Con *workspace_get(const char *num, bool *created) {
+ Con *output, *workspace = NULL, *child;
+
+ /* TODO: could that look like this in the future?
+ GET_MATCHING_NODE(workspace, croot, strcasecmp(current->name, num) != 0);
+ */
+ TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
+ TAILQ_FOREACH(child, &(output_get_content(output)->nodes_head), nodes) {
+ if (strcasecmp(child->name, num) != 0)
+ continue;
+
+ workspace = child;
+ break;
+ }
+
+ LOG("getting ws %s\n", num);
+ if (workspace == NULL) {
+ LOG("need to create this one\n");
+ output = con_get_output(focused);
+ Con *content = output_get_content(output);
+ LOG("got output %p with content %p\n", output, content);
+ /* We need to attach this container after setting its type. con_attach
+ * will handle CT_WORKSPACEs differently */
+ workspace = con_new(NULL);
+ char *name;
+ asprintf(&name, "[i3 con] workspace %s", num);
+ x_set_name(workspace, name);
+ free(name);
+ workspace->type = CT_WORKSPACE;
+ FREE(workspace->name);
+ workspace->name = sstrdup(num);
+ /* We set ->num to the number if this workspace’s name consists only of
+ * a positive number. Otherwise it’s a named ws and num will be -1. */
+ char *end;
+ long parsed_num = strtol(num, &end, 10);
+ if (parsed_num == LONG_MIN ||
+ parsed_num == LONG_MAX ||
+ parsed_num < 0 ||
+ (end && *end != '\0'))
+ workspace->num = -1;
+ else workspace->num = parsed_num;
+ LOG("num = %d\n", workspace->num);
+
+ /* If default_orientation is set to NO_ORIENTATION we
+ * determine workspace orientation from workspace size.
+ * Otherwise we just set the orientation to default_orientation. */
+ if (config.default_orientation == NO_ORIENTATION) {
+ workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
+ DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
+ workspace->rect.width, workspace->rect.height, workspace->orientation);
+ } else {
+ workspace->orientation = config.default_orientation;
+ }
+
+ con_attach(workspace, content, false);
+
+ ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
+ if (created != NULL)
+ *created = true;
+ }
+ else if (created != NULL) {
+ *created = false;
+ }
+
+ return workspace;
+}
+
+#if 0