Code

screen_search: fix crash when disconnected
[ncmpc.git] / src / command.c
index e6f2f1d3aae0034b0419efcb9168872c362ed63e..9495b79aa5ea62301478d9ee0e433c8315824194 100644 (file)
 #include <signal.h>
 #include <unistd.h>
 
-#undef DEBUG_KEYS
-
-#ifdef DEBUG_KEYS
-#define DK(x) x
-#else
-#define DK(x)
-#endif
-
 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
 
 #define BS   KEY_BACKSPACE
@@ -64,6 +56,7 @@
 #define F6   KEY_F(6)
 #define F7   KEY_F(7)
 #define F8   KEY_F(8)
+#define F9   KEY_F(9)
 #define C(x) KEY_CTL(x)
 
 static command_definition_t cmds[] = {
@@ -252,6 +245,10 @@ static command_definition_t cmds[] = {
          N_("Outputs screen") },
 #endif
 
+#ifdef ENABLE_CHAT_SCREEN
+       { {'9', F9, 0}, 0, CMD_SCREEN_CHAT, "screen-chat",
+         N_("Chat screen") },
+#endif
 
        { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
 };
@@ -262,6 +259,30 @@ get_command_definitions(void)
 {
        return cmds;
 }
+
+size_t
+get_cmds_max_name_width(command_definition_t *c)
+{
+       static size_t max = 0;
+
+       if (max != 0)
+               return max;
+
+       size_t len;
+       command_definition_t *p;
+
+       for (p = c; p->name != NULL; p++) {
+               /*
+                * width and length are considered the same here, as command
+                * names are not translated.
+                */
+               len = (size_t) strlen(p->name);
+               if (len > max)
+                       max = len;
+       }
+
+       return max;
+}
 #endif
 
 const char *
@@ -330,28 +351,28 @@ command_dump_keys(void)
 {
        for (int i = 0; cmds[i].description; i++)
                if (cmds[i].command != CMD_NONE)
-                       printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
+                       printf(" %20s : %s\n",
+                              get_key_names(cmds[i].command, true),
+                              cmds[i].name);
 }
 
 #ifndef NCMPC_MINI
 
-static int
+static void
 set_key_flags(command_definition_t *cp, command_t command, int flags)
 {
        for (int i = 0; cp[i].name; i++) {
                if (cp[i].command == command) {
                        cp[i].flags |= flags;
-                       return 0;
+                       break;
                }
        }
-
-       return 1;
 }
 
 #endif
 
 const char *
-get_key_names(command_t command, int all)
+get_key_names(command_t command, bool all)
 {
        for (int i = 0; cmds[i].description; i++) {
                if (cmds[i].command == command) {
@@ -429,7 +450,7 @@ get_keyboard_command(void)
        int key;
 
        key = wgetch(stdscr);
-       if (key == ERR)
+       if (key == ERR || key == '\0')
                return CMD_NONE;
 
 #ifdef HAVE_GETMOUSE