]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
Improve error messages on failing commands
[i3/i3] / src / commands.c
index 7a6502684948a0012fd251805a5e4226decb6ae1..13e0fa7e88df6104d17a311205249b44e49bf794 100644 (file)
             y(map_close);                   \
         }                                   \
     } while (0)
-#define yerror(message)                     \
-    do {                                    \
-        if (cmd_output->json_gen != NULL) { \
-            y(map_open);                    \
-            ystr("success");                \
-            y(bool, false);                 \
-            ystr("error");                  \
-            ystr(message);                  \
-            y(map_close);                   \
-        }                                   \
+#define yerror(format, ...)                             \
+    do {                                                \
+        if (cmd_output->json_gen != NULL) {             \
+            char *message;                              \
+            sasprintf(&message, format, ##__VA_ARGS__); \
+            y(map_open);                                \
+            ystr("success");                            \
+            y(bool, false);                             \
+            ystr("error");                              \
+            ystr(message);                              \
+            y(map_close);                               \
+            free(message);                              \
+        }                                               \
     } while (0)
 
 /** When the command did not include match criteria (!), we use the currently
@@ -574,8 +577,7 @@ void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
 
     if (parsed_num == -1) {
         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
-        // TODO: better error message
-        yerror("Could not parse number");
+        yerror("Could not parse number \"%s\"", which);
         return;
     }
 
@@ -899,15 +901,27 @@ void cmd_nop(I3_CMD, char *comment) {
  */
 void cmd_append_layout(I3_CMD, char *path) {
     LOG("Appending layout \"%s\"\n", path);
+
+    json_content_t content = json_determine_content(path);
+    LOG("JSON content = %d\n", content);
+    if (content == JSON_CONTENT_UNKNOWN) {
+        ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
+        ysuccess(false);
+        return;
+    }
+
     Con *parent = focused;
-    /* We need to append the layout to a split container, since a leaf
-     * container must not have any children (by definition).
-     * Note that we explicitly check for workspaces, since they are okay for
-     * this purpose, but con_accepts_window() returns false for workspaces. */
-    while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
-        parent = parent->parent;
-    DLOG("Appending to parent=%p instead of focused=%p\n",
-         parent, focused);
+    if (content == JSON_CONTENT_WORKSPACE) {
+        parent = output_get_content(con_get_output(parent));
+    } else {
+        /* We need to append the layout to a split container, since a leaf
+         * container must not have any children (by definition).
+         * Note that we explicitly check for workspaces, since they are okay for
+         * this purpose, but con_accepts_window() returns false for workspaces. */
+        while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
+            parent = parent->parent;
+    }
+    DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
     char *errormsg = NULL;
     tree_append_json(parent, path, &errormsg);
     if (errormsg != NULL) {
@@ -931,6 +945,9 @@ void cmd_append_layout(I3_CMD, char *path) {
 
     restore_open_placeholder_windows(parent);
 
+    if (content == JSON_CONTENT_WORKSPACE)
+        ipc_send_workspace_event("restored", parent, NULL);
+
     cmd_output->needs_tree_render = true;
 }
 
@@ -943,6 +960,12 @@ void cmd_workspace(I3_CMD, char *which) {
 
     DLOG("which=%s\n", which);
 
+    if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
+        LOG("Cannot switch workspace while in global fullscreen\n");
+        ysuccess(false);
+        return;
+    }
+
     if (strcmp(which, "next") == 0)
         ws = workspace_next();
     else if (strcmp(which, "prev") == 0)
@@ -971,12 +994,17 @@ void cmd_workspace(I3_CMD, char *which) {
 void cmd_workspace_number(I3_CMD, char *which) {
     Con *output, *workspace = NULL;
 
+    if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
+        LOG("Cannot switch workspace while in global fullscreen\n");
+        ysuccess(false);
+        return;
+    }
+
     long parsed_num = ws_name_to_number(which);
 
     if (parsed_num == -1) {
         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
-        // TODO: better error message
-        yerror("Could not parse number");
+        yerror("Could not parse number \"%s\"", which);
         return;
     }
 
@@ -1005,6 +1033,12 @@ void cmd_workspace_number(I3_CMD, char *which) {
  *
  */
 void cmd_workspace_back_and_forth(I3_CMD) {
+    if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
+        LOG("Cannot switch workspace while in global fullscreen\n");
+        ysuccess(false);
+        return;
+    }
+
     workspace_back_and_forth();
 
     cmd_output->needs_tree_render = true;
@@ -1023,6 +1057,12 @@ void cmd_workspace_name(I3_CMD, char *name) {
         return;
     }
 
+    if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
+        LOG("Cannot switch workspace while in global fullscreen\n");
+        ysuccess(false);
+        return;
+    }
+
     DLOG("should switch to workspace %s\n", name);
     if (maybe_back_and_forth(cmd_output, name))
         return;
@@ -1274,7 +1314,7 @@ void cmd_move_workspace_to_output(I3_CMD, char *name) {
                 create_workspace_on_output(current_output, ws->parent);
 
             /* notify the IPC listeners */
-            ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
+            ipc_send_workspace_event("init", ws, NULL);
         }
         DLOG("Detaching\n");
 
@@ -1295,7 +1335,7 @@ void cmd_move_workspace_to_output(I3_CMD, char *name) {
         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
         floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
 
-        ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
+        ipc_send_workspace_event("move", ws, NULL);
         if (workspace_was_visible) {
             /* Focus the moved workspace on the destination output. */
             workspace_show(ws);
@@ -1559,20 +1599,26 @@ void cmd_focus(I3_CMD) {
 }
 
 /*
- * Implementation of 'fullscreen [global]'.
+ * Implementation of 'fullscreen enable|toggle [global]' and
+ *                   'fullscreen disable'
  *
  */
-void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
-    if (fullscreen_mode == NULL)
-        fullscreen_mode = "output";
-    DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
+void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
+    fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
+    DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
     owindow *current;
 
     HANDLE_EMPTY_MATCH;
 
     TAILQ_FOREACH(current, &owindows, owindows) {
         DLOG("matching: %p / %s\n", current->con, current->con->name);
-        con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
+        if (strcmp(action, "toggle") == 0) {
+            con_toggle_fullscreen(current->con, mode);
+        } else if (strcmp(action, "enable") == 0) {
+            con_enable_fullscreen(current->con, mode);
+        } else if (strcmp(action, "disable") == 0) {
+            con_disable_fullscreen(current->con);
+        }
     }
 
     cmd_output->needs_tree_render = true;
@@ -1716,7 +1762,7 @@ void cmd_reload(I3_CMD) {
     load_configuration(conn, NULL, true);
     x_set_i3_atoms();
     /* Send an IPC event just in case the ws names have changed */
-    ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
+    ipc_send_workspace_event("reload", NULL, NULL);
     /* Send an update event for the barconfig just in case it has changed */
     update_barconfig();
 
@@ -1957,10 +2003,7 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
     }
 
     if (!workspace) {
-        // TODO: we should include the old workspace name here and use yajl for
-        // generating the reply.
-        // TODO: better error message
-        yerror("Old workspace not found");
+        yerror("Old workspace \"%s\" not found", old_name);
         return;
     }
 
@@ -1970,10 +2013,7 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
                !strcasecmp(child->name, new_name));
 
     if (check_dest != NULL) {
-        // TODO: we should include the new workspace name here and use yajl for
-        // generating the reply.
-        // TODO: better error message
-        yerror("New workspace already exists");
+        yerror("New workspace \"%s\" already exists", new_name);
         return;
     }
 
@@ -1995,7 +2035,7 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
     cmd_output->needs_tree_render = true;
     ysuccess(true);
 
-    ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
+    ipc_send_workspace_event("rename", workspace, NULL);
     ewmh_update_desktop_names();
     ewmh_update_desktop_viewport();
     ewmh_update_current_desktop();