Code

release v0.29
[ncmpc.git] / src / command.c
index 85e499a00873ad2c985dbfbb1241c26b7b1006f2..f903165f57a113034df735373395a4a5a3d43a7f 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
  */
 
 #include "command.h"
-#include "config.h"
-#include "ncmpc.h"
 #include "i18n.h"
-#include "mpdclient.h"
-#include "screen.h"
+#include "ncmpc_curses.h"
 
+#include <assert.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #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 +54,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[] = {
@@ -96,9 +87,9 @@ static command_definition_t cmds[] = {
        { { 'v',  0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
          N_("Range selection") },
        { { C('N'),  0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
-         N_("Scroll up one line") },
-       { { C('B'),  0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
          N_("Scroll down one line") },
+       { { C('B'),  0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
+         N_("Scroll up one line") },
        { { 'N',  0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
          N_("Scroll up half a screen") },
        { { 'B',  0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
@@ -111,7 +102,7 @@ static command_definition_t cmds[] = {
        { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
          N_("Help screen") },
        { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
-         N_("Playlist screen") },
+         N_("Queue screen") },
        { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
          N_("Browse screen") },
 
@@ -138,15 +129,15 @@ static command_definition_t cmds[] = {
        { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
          N_("Decrease volume") },
        { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
-         N_("Select/deselect song in playlist") },
+         N_("Select/deselect song in queue") },
        { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
          N_("Select all listed items") },
        { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
-         N_("Delete song from playlist") },
+         N_("Delete song from queue") },
        { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
-         N_("Shuffle playlist") },
+         N_("Shuffle queue") },
        { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
-         N_("Clear playlist") },
+         N_("Clear queue") },
        { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
          N_("Toggle repeat mode") },
        { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
@@ -160,9 +151,9 @@ static command_definition_t cmds[] = {
        { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
          N_("Start a music database update") },
        { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
-         N_("Save playlist") },
+         N_("Save queue") },
        { { 'a', 0, 0 }, 0, CMD_ADD, "add",
-         N_("Add url/file to playlist") },
+         N_("Add url/file to queue") },
 
        { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
          N_("Go to root directory") },
@@ -252,6 +243,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 }
 };
@@ -271,15 +266,12 @@ get_cmds_max_name_width(command_definition_t *c)
        if (max != 0)
                return max;
 
-       size_t len;
-       command_definition_t *p;
-
-       for (p = c; p->name != NULL; p++) {
+       for (command_definition_t *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);
+               size_t len = strlen(p->name);
                if (len > max)
                        max = len;
        }
@@ -291,11 +283,9 @@ get_cmds_max_name_width(command_definition_t *c)
 const char *
 key2str(int key)
 {
-       static char buf[32];
-       int i;
-
-       buf[0] = 0;
        switch(key) {
+               static char buf[32];
+
        case 0:
                return _("Undefined");
        case ' ':
@@ -331,7 +321,7 @@ key2str(int key)
        case KEY_IC:
                return _("Insert");
        default:
-               for (i = 0; i <= 63; i++)
+               for (int i = 0; i <= 63; i++)
                        if (key == KEY_F(i)) {
                                g_snprintf(buf, 32, _("F%d"), i );
                                return buf;
@@ -344,15 +334,14 @@ key2str(int key)
                        g_snprintf(buf, 32, "%c", key);
                else
                        g_snprintf(buf, 32, "0x%03X", key);
+               return buf;
        }
-
-       return buf;
 }
 
 void
 command_dump_keys(void)
 {
-       for (int i = 0; cmds[i].description; i++)
+       for (size_t i = 0; cmds[i].description; i++)
                if (cmds[i].command != CMD_NONE)
                        printf(" %20s : %s\n",
                               get_key_names(cmds[i].command, true),
@@ -364,7 +353,7 @@ command_dump_keys(void)
 static void
 set_key_flags(command_definition_t *cp, command_t command, int flags)
 {
-       for (int i = 0; cp[i].name; i++) {
+       for (size_t i = 0; cp[i].name; i++) {
                if (cp[i].command == command) {
                        cp[i].flags |= flags;
                        break;
@@ -385,7 +374,7 @@ get_key_names(command_t command, bool all)
                        if (!all)
                                return keystr;
 
-                       for (int j = 1; j < MAX_COMMAND_KEYS &&
+                       for (unsigned j = 1; j < MAX_COMMAND_KEYS &&
                                        cmds[i].keys[j] > 0; j++) {
                                g_strlcat(keystr, " ", sizeof(keystr));
                                g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
@@ -399,7 +388,7 @@ get_key_names(command_t command, bool all)
 const char *
 get_key_description(command_t command)
 {
-       for (int i = 0; cmds[i].description; i++)
+       for (size_t i = 0; cmds[i].description; i++)
                if (cmds[i].command == command)
                        return _(cmds[i].description);
 
@@ -409,7 +398,7 @@ get_key_description(command_t command)
 const char *
 get_key_command_name(command_t command)
 {
-       for (int i = 0; cmds[i].name; i++)
+       for (size_t i = 0; cmds[i].name; i++)
                if (cmds[i].command == command)
                        return cmds[i].name;
 
@@ -417,9 +406,9 @@ get_key_command_name(command_t command)
 }
 
 command_t
-get_key_command_from_name(char *name)
+get_key_command_from_name(const char *name)
 {
-       for (int i = 0; cmds[i].name; i++)
+       for (size_t i = 0; cmds[i].name; i++)
                if (strcmp(name, cmds[i].name) == 0)
                        return cmds[i].command;
 
@@ -427,12 +416,12 @@ get_key_command_from_name(char *name)
 }
 
 command_t
-find_key_command(int key, command_definition_t *c)
+find_key_command(int key, const command_definition_t *c)
 {
        assert(key != 0);
        assert(c != NULL);
 
-       for (int i = 0; c[i].name; i++) {
+       for (size_t i = 0; c[i].name; i++) {
                for (int j = 0; j < MAX_COMMAND_KEYS; j++)
                        if (c[i].keys[j] == key)
                                return c[i].command;
@@ -447,54 +436,36 @@ get_key_command(int key)
        return find_key_command(key, cmds);
 }
 
-command_t
-get_keyboard_command(void)
-{
-       int key;
-
-       key = wgetch(stdscr);
-       if (key == ERR)
-               return CMD_NONE;
-
-#ifdef HAVE_GETMOUSE
-       if (key == KEY_MOUSE)
-               return CMD_MOUSE_EVENT;
-#endif
-
-       return get_key_command(key);
-}
-
-int
+bool
 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
 {
-       for (int i = 0; cmds[i].name; i++) {
+       for (size_t i = 0; cmds[i].name; i++) {
                if (cmds[i].command == command) {
                        memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
 #ifndef NCMPC_MINI
                        cmds[i].flags |= COMMAND_KEY_MODIFIED;
 #endif
-                       return 0;
+                       return true;
                }
        }
 
-       return -1;
+       return false;
 }
 
 #ifndef NCMPC_MINI
 
-int
+bool
 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
 {
-       int i;
-       int retval = 0;
+       bool success = true;
 
        if (cp == NULL)
                cp = cmds;
 
-       for (i = 0; cp[i].name; i++)
+       for (size_t i = 0; cp[i].name; i++)
                cp[i].flags &= ~COMMAND_KEY_CONFLICT;
 
-       for (i = 0; cp[i].name; i++) {
+       for (size_t i = 0; cp[i].name; i++) {
                int j;
                command_t cmd;
 
@@ -517,21 +488,21 @@ check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
                                }
                                cp[i].flags |= COMMAND_KEY_CONFLICT;
                                set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
-                               retval = -1;
+                               success = false;
                        }
                }
        }
 
-       return retval;
+       return success;
 }
 
-int
+bool
 write_key_bindings(FILE *f, int flags)
 {
        if (flags & KEYDEF_WRITE_HEADER)
                fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
 
-       for (int i = 0; cmds[i].name && !ferror(f); i++) {
+       for (size_t i = 0; cmds[i].name && !ferror(f); i++) {
                if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
                    flags & KEYDEF_WRITE_ALL) {
                        fprintf(f, "## %s\n", cmds[i].description);
@@ -553,7 +524,7 @@ write_key_bindings(FILE *f, int flags)
                }
        }
 
-       return ferror(f);
+       return ferror(f) == 0;
 }
 
 #endif /* NCMPC_MINI */