$OpenBSD: patch-src_commands_c,v 1.1 2012/05/28 13:37:50 dcoppa Exp $

From b88ab981fd0a5725ed886a9f9788a5b1e721534c Mon Sep 17 00:00:00 2001
From: Ondrej Grover <ondrej.grover@gmail.com>
Date: Wed, 02 May 2012 14:05:27 +0000
Subject: bugfix: less differentiation between named and numbered workspaces

calling workspace by number now also checks for switching back and forth
and creates a new workspace if no workspace starting with that number is
found

also removed the obsolete tree_render() in favor of setting
cmd_output->needs_tree_render to true

From 4eab046e8fa40535d1a2ad80533915983ef0ee7e Mon Sep 17 00:00:00 2001
From: Fernando Tarla Cardoso Lemos <fernandotcl@gmail.com>
Date: Sat, 21 Apr 2012 19:34:25 +0000
Subject: Allow focus w/ target when in fs in some cases.

If the target is in a different workspace, there's no reason why
we wouldn't allow the user to focus it. We already allow this when
focusing a workspace, for example.

--- src/commands.c.orig	Wed Apr 25 23:21:25 2012
+++ src/commands.c	Mon May 28 13:50:28 2012
@@ -65,6 +65,28 @@ static Output *get_output_from_string(Output *current_
     return output;
 }
 
+/*
+ * Checks whether we switched to a new workspace and returns false in that case,
+ * signaling that further workspace switching should be done by the calling function
+ * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
+ * and return true, signaling that no further workspace switching should occur in the calling function.
+ *
+ */
+static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
+    Con *ws = con_get_workspace(focused);
+
+    /* If we switched to a different workspace, do nothing */
+    if (strcmp(ws->name, name) != 0)
+        return false;
+
+    DLOG("This workspace is already focused.\n");
+    if (config.workspace_auto_back_and_forth) {
+        workspace_back_and_forth();
+        cmd_output->needs_tree_render = true;
+    }
+    return true;
+}
+
 // This code is commented out because we might recycle it for popping up error
 // messages on parser errors.
 #if 0
@@ -752,12 +774,19 @@ void cmd_workspace_number(I3_CMD, char *which) {
             child->num == parsed_num);
 
     if (!workspace) {
-        LOG("There is no workspace with number %d.\n", parsed_num);
+        LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
         cmd_output->json_output = sstrdup("{\"success\": false, "
                 "\"error\": \"No such workspace\"}");
+        /* terminate the which string after the endposition of the number */
+        *endptr = '\0';
+        if (maybe_back_and_forth(cmd_output, which))
+            return;
+        workspace_show_by_name(which);
+        cmd_output->needs_tree_render = true;
         return;
     }
-
+    if (maybe_back_and_forth(cmd_output, which))
+        return;
     workspace_show(workspace);
 
     cmd_output->needs_tree_render = true;
@@ -789,20 +818,8 @@ void cmd_workspace_name(I3_CMD, char *name) {
     }
 
     DLOG("should switch to workspace %s\n", name);
-
-    Con *ws = con_get_workspace(focused);
-
-    /* Check if the command wants to switch to the current workspace */
-    if (strcmp(ws->name, name) == 0) {
-        DLOG("This workspace is already focused.\n");
-        if (config.workspace_auto_back_and_forth) {
-            workspace_back_and_forth();
-            tree_render();
-        }
-        cmd_output->json_output = sstrdup("{\"sucess\": false}");
-        return;
-    }
-
+    if (maybe_back_and_forth(cmd_output, name))
+       return;
     workspace_show_by_name(name);
 
     cmd_output->needs_tree_render = true;
@@ -1196,16 +1213,7 @@ void cmd_focus_level(I3_CMD, char *level) {
  */
 void cmd_focus(I3_CMD) {
     DLOG("current_match = %p\n", current_match);
-    if (focused &&
-        focused->type != CT_WORKSPACE &&
-        focused->fullscreen_mode != CF_NONE) {
-        LOG("Cannot change focus while in fullscreen mode.\n");
-        cmd_output->json_output = sstrdup("{\"sucess\": false}");
-        return;
-    }
 
-    owindow *current;
-
     if (match_is_empty(current_match)) {
         ELOG("You have to specify which window/container should be focused.\n");
         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
@@ -1217,12 +1225,24 @@ void cmd_focus(I3_CMD) {
     }
 
     int count = 0;
+    owindow *current;
     TAILQ_FOREACH(current, &owindows, owindows) {
         Con *ws = con_get_workspace(current->con);
         /* If no workspace could be found, this was a dock window.
          * Just skip it, you cannot focus dock windows. */
         if (!ws)
             continue;
+
+        /* Don't allow the focus switch if the focused and current
+         * containers are in the same workspace. */
+        if (focused &&
+            focused->type != CT_WORKSPACE &&
+            focused->fullscreen_mode != CF_NONE &&
+            con_get_workspace(focused) == ws) {
+            LOG("Cannot change focus while in fullscreen mode (same workspace).\n");
+            cmd_output->json_output = sstrdup("{\"success\": false}");
+            return;
+        }
 
         /* If the container is not on the current workspace,
          * workspace_show() will switch to a different workspace and (if
