]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
Migrate the resize command to use typed numbers.
[i3/i3] / src / commands.c
index 8189604722745a37810b21deac63fc27b1003dd4..ec657a645e6bbb0a4320c02f1de2c6f105c1ca33 100644 (file)
@@ -782,15 +782,11 @@ static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char
  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
  *
  */
-void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
-    /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
-    DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
-    // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
-    int px = atoi(resize_px);
-    int ppt = atoi(resize_ppt);
+void cmd_resize(I3_CMD, char *way, char *direction, long resize_px, long resize_ppt) {
+    DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
     if (strcmp(way, "shrink") == 0) {
-        px *= -1;
-        ppt *= -1;
+        resize_px *= -1;
+        resize_ppt *= -1;
     }
 
     HANDLE_EMPTY_MATCH;
@@ -805,14 +801,16 @@ void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resiz
 
         Con *floating_con;
         if ((floating_con = con_inside_floating(current->con))) {
-            cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
+            cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
         } else {
             if (strcmp(direction, "width") == 0 ||
                 strcmp(direction, "height") == 0) {
-                if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
+                if (!cmd_resize_tiling_width_height(current_match, cmd_output,
+                                                    current->con, way, direction, resize_ppt))
                     return;
             } else {
-                if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
+                if (!cmd_resize_tiling_direction(current_match, cmd_output,
+                                                 current->con, way, direction, resize_ppt))
                     return;
             }
         }
@@ -827,13 +825,10 @@ void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resiz
  * Implementation of 'resize set <px> [px] <px> [px]'.
  *
  */
-void cmd_size(I3_CMD, char *cwidth, char *cheight) {
-    DLOG("resizing to %sx%s px\n", cwidth, cheight);
-    // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
-    int x = atoi(cwidth);
-    int y = atoi(cheight);
-    if (x <= 0 || y <= 0) {
-        ELOG("Resize failed: dimensions cannot be negative (was %sx%s)\n", cwidth, cheight);
+void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
+    DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
+    if (cwidth <= 0 || cheight <= 0) {
+        ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
         return;
     }
 
@@ -843,7 +838,7 @@ void cmd_size(I3_CMD, char *cwidth, char *cheight) {
     TAILQ_FOREACH(current, &owindows, owindows) {
         Con *floating_con;
         if ((floating_con = con_inside_floating(current->con))) {
-            floating_resize(floating_con, x, y);
+            floating_resize(floating_con, cwidth, cheight);
         } else {
             ELOG("Resize failed: %p not a floating container\n", current->con);
         }
@@ -1120,27 +1115,10 @@ void cmd_mark(I3_CMD, char *mark, char *toggle) {
     }
 
     DLOG("matching: %p / %s\n", current->con, current->con->name);
-    current->con->mark_changed = true;
-    if (toggle != NULL && current->con->mark && strcmp(current->con->mark, mark) == 0) {
-        DLOG("removing window mark %s\n", mark);
-        FREE(current->con->mark);
+    if (toggle != NULL) {
+        con_mark_toggle(current->con, mark);
     } else {
-        DLOG("marking window with str %s\n", mark);
-        FREE(current->con->mark);
-        current->con->mark = sstrdup(mark);
-    }
-
-    DLOG("Clearing all non-matched windows with this mark\n");
-    Con *con;
-    TAILQ_FOREACH(con, &all_cons, all_cons) {
-        /* Skip matched window, we took care of it already. */
-        if (current->con == con)
-            continue;
-
-        if (con->mark && strcmp(con->mark, mark) == 0) {
-            FREE(con->mark);
-            con->mark_changed = true;
-        }
+        con_mark(current->con, mark);
     }
 
     cmd_output->needs_tree_render = true;
@@ -1153,24 +1131,7 @@ void cmd_mark(I3_CMD, char *mark, char *toggle) {
  *
  */
 void cmd_unmark(I3_CMD, char *mark) {
-    if (mark == NULL) {
-        Con *con;
-        TAILQ_FOREACH(con, &all_cons, all_cons) {
-            if (con->mark == NULL)
-                continue;
-
-            FREE(con->mark);
-            con->mark_changed = true;
-        }
-        DLOG("Removed all window marks.\n");
-    } else {
-        Con *con = con_by_mark(mark);
-        if (con != NULL) {
-            FREE(con->mark);
-            con->mark_changed = true;
-        }
-        DLOG("Removed window mark \"%s\".\n", mark);
-    }
+    con_unmark(mark);
 
     cmd_output->needs_tree_render = true;
     // XXX: default reply for now, make this a better reply
@@ -1194,41 +1155,37 @@ void cmd_mode(I3_CMD, char *mode) {
  *
  */
 void cmd_move_con_to_output(I3_CMD, char *name) {
-    owindow *current;
-
-    DLOG("should move window to output %s\n", name);
-
+    DLOG("Should move window to output \"%s\".\n", name);
     HANDLE_EMPTY_MATCH;
 
-    Output *current_output = NULL;
-    // TODO: fix the handling of criteria
-    TAILQ_FOREACH(current, &owindows, owindows)
-    current_output = get_output_of_con(current->con);
-    assert(current_output != NULL);
+    owindow *current;
+    bool had_error = false;
+    TAILQ_FOREACH(current, &owindows, owindows) {
+        DLOG("matching: %p / %s\n", current->con, current->con->name);
 
-    Output *output = get_output_from_string(current_output, name);
-    if (!output) {
-        LOG("No such output found.\n");
-        ysuccess(false);
-        return;
-    }
+        Output *current_output = get_output_of_con(current->con);
+        assert(current_output != NULL);
 
-    /* get visible workspace on output */
-    Con *ws = NULL;
-    GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
-    if (!ws) {
-        ysuccess(false);
-        return;
-    }
+        Output *output = get_output_from_string(current_output, name);
+        if (output == NULL) {
+            ELOG("Could not find output \"%s\", skipping.\n", name);
+            had_error = true;
+            continue;
+        }
+
+        Con *ws = NULL;
+        GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
+        if (ws == NULL) {
+            ELOG("Could not find a visible workspace on output %p.\n", output);
+            had_error = true;
+            continue;
+        }
 
-    TAILQ_FOREACH(current, &owindows, owindows) {
-        DLOG("matching: %p / %s\n", current->con, current->con->name);
         con_move_to_workspace(current->con, ws, true, false, false);
     }
 
     cmd_output->needs_tree_render = true;
-    // XXX: default reply for now, make this a better reply
-    ysuccess(true);
+    ysuccess(!had_error);
 }
 
 /*
@@ -1414,15 +1371,14 @@ void cmd_focus_window_mode(I3_CMD, char *window_mode) {
     DLOG("window_mode = %s\n", window_mode);
 
     Con *ws = con_get_workspace(focused);
-    Con *current;
     if (ws != NULL) {
         if (strcmp(window_mode, "mode_toggle") == 0) {
-            current = TAILQ_FIRST(&(ws->focus_head));
-            if (current != NULL && current->type == CT_FLOATING_CON)
+            if (con_inside_floating(focused))
                 window_mode = "tiling";
             else
                 window_mode = "floating";
         }
+        Con *current;
         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
@@ -1599,7 +1555,7 @@ void cmd_sticky(I3_CMD, char *action) {
 
     /* A window we made sticky might not be on a visible workspace right now, so we need to make
      * sure it gets pushed to the front now. */
-    output_push_sticky_windows();
+    output_push_sticky_windows(focused);
 
     cmd_output->needs_tree_render = true;
     ysuccess(true);