]> git.sur5r.net Git - i3/i3/blobdiff - src/workspace.c
replace remaining printf()s with D?LOG
[i3/i3] / src / workspace.c
index 5a0913bfcc77852c1084fe2f2908097000714b8c..256b0804768eceb2f9ff4310f0aefcd927b990b6 100644 (file)
@@ -11,9 +11,6 @@
  *
  */
 #include "all.h"
-#include "yajl_utils.h"
-
-#include <yajl/yajl_gen.h>
 
 /* Stores a copy of the name of the last used workspace for the workspace
  * back-and-forth switching. */
@@ -56,14 +53,25 @@ Con *workspace_get(const char *num, bool *created) {
         output = con_get_output(focused);
         /* look for assignments */
         struct Workspace_Assignment *assignment;
-        TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
-            if (strcmp(assignment->name, num) != 0)
-                continue;
 
-            LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
-            GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
-            break;
+        /* We set workspace->num to the number if this workspace’s name begins
+         * with a positive number. Otherwise it’s a named ws and num will be
+         * -1. */
+        long parsed_num = ws_name_to_number(num);
+
+        TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
+            if (strcmp(assignment->name, num) == 0) {
+                DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
+                GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
+                break;
+            } else if (parsed_num != -1
+                    && name_is_digits(assignment->name)
+                    && ws_name_to_number(assignment->name) == parsed_num) {
+                DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
+                GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
+            }
         }
+
         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
@@ -77,16 +85,7 @@ Con *workspace_get(const char *num, bool *created) {
         FREE(workspace->name);
         workspace->name = sstrdup(num);
         workspace->workspace_layout = config.default_layout;
-        /* We set ->num to the number if this workspace’s name begins with a
-         * positive number. Otherwise it’s a named ws and num will be -1. */
-        char *endptr = NULL;
-        long parsed_num = strtol(num, &endptr, 10);
-        if (parsed_num == LONG_MIN ||
-            parsed_num == LONG_MAX ||
-            parsed_num < 0 ||
-            endptr == num)
-            workspace->num = -1;
-        else workspace->num = parsed_num;
+        workspace->num = parsed_num;
         LOG("num = %d\n", workspace->num);
 
         workspace->parent = content;
@@ -128,6 +127,8 @@ Con *create_workspace_on_output(Output *output, Con *content) {
             continue;
         DLOG("relevant command = %s\n", bind->command);
         char *target = bind->command + strlen("workspace ");
+        while((*target == ' ' || *target == '\t') && target != '\0')
+            target++;
         /* We check if this is the workspace
          * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
          * Beware: The workspace names "next", "prev", "next_on_output",
@@ -143,6 +144,10 @@ Con *create_workspace_on_output(Output *output, Con *content) {
             continue;
         if (*target == '"')
             target++;
+        if (strncasecmp(target, "__", strlen("__")) == 0) {
+            LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
+            continue;
+        }
         FREE(ws->name);
         ws->name = strdup(target);
         if (ws->name[strlen(ws->name)-1] == '"')
@@ -195,17 +200,16 @@ Con *create_workspace_on_output(Output *output, Con *content) {
         while (exists) {
             c++;
 
-            FREE(ws->name);
-            sasprintf(&(ws->name), "%d", c);
+            ws->num = c;
 
             current = NULL;
             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
-                GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
+                GREP_FIRST(current, output_get_content(out), child->num == ws->num);
             exists = (current != NULL);
 
-            DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
+            DLOG("result for ws %d: exists = %d\n", c, exists);
         }
-        ws->num = c;
+        sasprintf(&(ws->name), "%d", c);
     }
     con_attach(ws, content, false);
 
@@ -334,39 +338,6 @@ static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents
     FREE(con->urgency_timer);
 }
 
-/*
- * For the "focus" event we send, along the usual "change" field, also the
- * current and previous workspace, in "current" and "old" respectively.
- */
-static void ipc_send_workspace_focus_event(Con *current, Con *old) {
-    setlocale(LC_NUMERIC, "C");
-    yajl_gen gen = ygenalloc();
-
-    y(map_open);
-
-    ystr("change");
-    ystr("focus");
-
-    ystr("current");
-    dump_node(gen, current, false);
-
-    ystr("old");
-    if (old == NULL)
-        y(null);
-    else
-        dump_node(gen, old, false);
-
-    y(map_close);
-
-    const unsigned char *payload;
-    ylength length;
-    y(get_buf, &payload, &length);
-
-    ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
-    y(free);
-    setlocale(LC_NUMERIC, "");
-}
-
 static void _workspace_show(Con *workspace) {
     Con *current, *old = NULL;
 
@@ -442,7 +413,7 @@ static void _workspace_show(Con *workspace) {
     } else
         con_focus(next);
 
-    ipc_send_workspace_focus_event(workspace, old);
+    ipc_send_workspace_focus_event(workspace, current);
 
     DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
     /* Close old workspace if necessary. This must be done *after* doing