]> git.sur5r.net Git - i3/i3/commitdiff
Introduce get_existing_workspace_by_name
authorOrestis Floros <orestisf1993@gmail.com>
Tue, 27 Mar 2018 19:18:17 +0000 (22:18 +0300)
committerOrestis Floros <orestisf1993@gmail.com>
Wed, 28 Mar 2018 12:18:43 +0000 (15:18 +0300)
include/workspace.h
src/commands.c
src/load_layout.c
src/randr.c
src/workspace.c

index 8d109e9e31726fe04ada3915501e64809c2dbadd..3e7183b9f7b79cc068099adde5d2a14dc9dfb81e 100644 (file)
 #define NET_WM_DESKTOP_NONE 0xFFFFFFF0
 #define NET_WM_DESKTOP_ALL 0xFFFFFFFF
 
+/**
+ * Returns the workspace with the given name or NULL if such a workspace does
+ * not exist.
+ *
+ */
+Con *get_existing_workspace_by_name(const char *name);
+
 /**
  * Returns a pointer to the workspace with the given number (starting at 0),
  * creating the workspace if necessary (by allocating the necessary amount of
index 500a697eed194aec622d9b82e9f1394edd2169bf..e6f6174b719ed94806901ed50f907d2dcb427fcd 100644 (file)
@@ -1955,11 +1955,9 @@ void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
         LOG("Renaming current workspace to \"%s\"\n", new_name);
     }
 
-    Con *output, *workspace = NULL;
+    Con *workspace;
     if (old_name) {
-        TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
-        GREP_FIRST(workspace, output_get_content(output),
-                   !strcasecmp(child->name, old_name));
+        workspace = get_existing_workspace_by_name(old_name);
     } else {
         workspace = con_get_workspace(focused);
         old_name = workspace->name;
@@ -1970,10 +1968,7 @@ void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
         return;
     }
 
-    Con *check_dest = NULL;
-    TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
-    GREP_FIRST(check_dest, output_get_content(output),
-               !strcasecmp(child->name, new_name));
+    Con *check_dest = get_existing_workspace_by_name(new_name);
 
     /* If check_dest == workspace, the user might be changing the case of the
      * workspace, or it might just be a no-op. */
index aa7ac03c452f20c66505215602be441cc61ae3bd..add78875bdcc387b6d8258c42b68a8a4eb389d61 100644 (file)
@@ -108,18 +108,11 @@ static int json_end_map(void *ctx) {
             /* Prevent name clashes when appending a workspace, e.g. when the
              * user tries to restore a workspace called “1” but already has a
              * workspace called “1”. */
-            Con *output;
-            Con *workspace = NULL;
-            TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
-            GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
             char *base = sstrdup(json_node->name);
             int cnt = 1;
-            while (workspace != NULL) {
+            while (get_existing_workspace_by_name(json_node->name) != NULL) {
                 FREE(json_node->name);
                 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
-                workspace = NULL;
-                TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
-                GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
             }
             free(base);
 
index c43c645517c215b99a2bd7d306dfc5a058ad1a87..d7e1accab360fd688de099f1e6a734de66b69826 100644 (file)
@@ -427,11 +427,7 @@ void init_ws_for_output(Output *output, Con *content) {
         if (strcmp(assignment->output, output_primary_name(output)) != 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));
+        Con *workspace = get_existing_workspace_by_name(assignment->name);
         if (workspace == NULL)
             continue;
 
index edd3ee6f0306f69f96041f8e30d76b658aa01bfe..bdd6f6f4dacf41af58c442f9a771d8898f3457ba 100644 (file)
@@ -19,6 +19,20 @@ static char *previous_workspace_name = NULL;
  * keybindings. */
 static char **binding_workspace_names = NULL;
 
+/*
+ * Returns the workspace with the given name or NULL if such a workspace does
+ * not exist.
+ *
+ */
+Con *get_existing_workspace_by_name(const char *name) {
+    Con *output, *workspace = NULL;
+    TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
+        GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
+    }
+
+    return workspace;
+}
+
 /*
  * Sets ws->layout to splith/splitv if default_orientation was specified in the
  * configfile. Otherwise, it uses splith/splitv depending on whether the output
@@ -46,15 +60,12 @@ static void _workspace_apply_default_orientation(Con *ws) {
  *
  */
 Con *workspace_get(const char *num, bool *created) {
-    Con *output, *workspace = NULL;
-
-    TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
-    GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
+    Con *workspace = get_existing_workspace_by_name(num);
 
     if (workspace == NULL) {
         LOG("Creating new workspace \"%s\"\n", num);
         /* unless an assignment is found, we will create this workspace on the current output */
-        output = con_get_output(focused);
+        Con *output = con_get_output(focused);
         /* look for assignments */
         struct Workspace_Assignment *assignment;
 
@@ -172,7 +183,6 @@ void extract_workspace_names_from_bindings(void) {
  */
 Con *create_workspace_on_output(Output *output, Con *content) {
     /* add a workspace to this output */
-    Con *out, *current;
     char *name;
     bool exists = true;
     Con *ws = con_new(NULL, NULL);
@@ -198,10 +208,7 @@ Con *create_workspace_on_output(Output *output, Con *content) {
         if (assigned)
             continue;
 
-        current = NULL;
-        TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
-        GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
-        exists = (current != NULL);
+        exists = (get_existing_workspace_by_name(target_name) != NULL);
         if (!exists) {
             ws->name = sstrdup(target_name);
             /* Set ->num to the number of the workspace, if the name actually
@@ -222,7 +229,7 @@ Con *create_workspace_on_output(Output *output, Con *content) {
 
             ws->num = c;
 
-            current = NULL;
+            Con *out, *current = NULL;
             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
             GREP_FIRST(current, output_get_content(out), child->num == ws->num);
             exists = (current != NULL);
@@ -940,14 +947,10 @@ bool workspace_move_to_output(Con *ws, const char *name) {
         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
             if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
                 continue;
-
             /* check if this workspace is already attached to the tree */
-            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)
+            if (get_existing_workspace_by_name(assignment->name) != NULL) {
                 continue;
+            }
 
             /* so create the workspace referenced to by this assignment */
             LOG("Creating workspace from assignment %s.\n", assignment->name);