Code

release v0.29
[ncmpc.git] / src / screen_keydef.c
index 32791b8c0e364a2eb32eab2006e2c7d098c8442f..9a76c1bf1baf4c2a5a6d8e18eef802a4b1d6248e 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
@@ -25,6 +25,8 @@
 #include "conf.h"
 #include "screen.h"
 #include "screen_utils.h"
+#include "options.h"
+#include "Compiler.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -42,21 +44,24 @@ static unsigned command_n_commands = 0;
  * the position of the "apply" item. It's the same as command_n_commands,
  * because array subscripts start at 0, while numbers of items start at 1.
  */
-static G_GNUC_PURE inline unsigned
+gcc_pure
+static inline unsigned
 command_item_apply(void)
 {
        return command_n_commands;
 }
 
 /** the position of the "apply and save" item */
-static G_GNUC_PURE inline unsigned
+gcc_pure
+static inline unsigned
 command_item_save(void)
 {
        return command_item_apply() + 1;
 }
 
 /** the number of items in the "command" view */
-static G_GNUC_PURE inline unsigned
+gcc_pure
+static inline unsigned
 command_length(void)
 {
        return command_item_save() + 1;
@@ -73,28 +78,32 @@ static int subcmd = -1;
 static unsigned subcmd_n_keys = 0;
 
 /** The position of the up ("[..]") item */
-static G_GNUC_CONST inline unsigned
+gcc_const
+static inline unsigned
 subcmd_item_up(void)
 {
        return 0;
 }
 
 /** The position of the "add a key" item */
-static G_GNUC_PURE inline unsigned
+gcc_pure
+static inline unsigned
 subcmd_item_add(void)
 {
        return subcmd_n_keys + 1;
 }
 
 /** The number of items in the list_window, if there's a command being edited */
-static G_GNUC_PURE inline unsigned
+gcc_pure
+static inline unsigned
 subcmd_length(void)
 {
        return subcmd_item_add() + 1;
 }
 
 /** Check whether a given item is a key */
-static G_GNUC_PURE inline bool
+gcc_pure
+static inline bool
 subcmd_item_is_key(unsigned i)
 {
        return (i > subcmd_item_up() && i < subcmd_item_add());
@@ -104,7 +113,8 @@ subcmd_item_is_key(unsigned i)
  * Convert an item id (as in lw->selected) into a "key id", which is an array
  * subscript to cmds[subcmd].keys.
  */
-static G_GNUC_CONST inline unsigned
+gcc_const
+static inline unsigned
 subcmd_item_to_key_id(unsigned i)
 {
        return i - 1;
@@ -136,31 +146,33 @@ apply_keys(void)
 static int
 save_keys(void)
 {
-       FILE *f;
-       char *filename;
+       char *allocated = NULL;
+       const char *filename = options.key_file;
+       if (filename == NULL) {
+               if (!check_user_conf_dir()) {
+                       screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
+                                            strerror(errno));
+                       screen_bell();
+                       return -1;
+               }
 
-       if (check_user_conf_dir()) {
-               screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
-                                    strerror(errno));
-               screen_bell();
-               return -1;
+               filename = allocated = build_user_key_binding_filename();
        }
 
-       filename = build_user_key_binding_filename();
-
-       if ((f = fopen(filename,"w")) == NULL) {
+       FILE *f = fopen(filename, "w");
+       if (f == NULL) {
                screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
                screen_bell();
-               g_free(filename);
+               g_free(allocated);
                return -1;
        }
 
        if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
-               screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
-       else
                screen_status_printf(_("Wrote %s"), filename);
+       else
+               screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
 
-       g_free(filename);
+       g_free(allocated);
        return fclose(f);
 }
 
@@ -183,13 +195,6 @@ check_subcmd_length(void)
 static void
 keydef_paint(void);
 
-static void
-keydef_repaint(void)
-{
-       keydef_paint();
-       wrefresh(lw->w);
-}
-
 /** lw->start the last time switch_to_subcmd_mode() was called */
 static unsigned saved_start = 0;
 
@@ -204,7 +209,7 @@ switch_to_subcmd_mode(int cmd)
        list_window_reset(lw);
        check_subcmd_length();
 
-       keydef_repaint();
+       keydef_paint();
 }
 
 static void
@@ -218,7 +223,7 @@ switch_to_command_mode(void)
 
        lw->start = saved_start;
 
-       keydef_repaint();
+       keydef_paint();
 }
 
 /**
@@ -245,7 +250,7 @@ delete_key(int cmd_index, int key_index)
        screen_status_printf(_("Deleted"));
 
        /* repaint */
-       keydef_repaint();
+       keydef_paint();
 
        /* update key conflict flags */
        check_key_bindings(cmds, NULL, 0);
@@ -255,14 +260,11 @@ delete_key(int cmd_index, int key_index)
 static void
 overwrite_key(int cmd_index, int key_index)
 {
-       int key;
-       char *buf;
-       command_t cmd;
-
        assert(key_index < MAX_COMMAND_KEYS);
 
-       buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
-       key = screen_getch(buf);
+       char *buf = g_strdup_printf(_("Enter new key for %s: "),
+                                   cmds[cmd_index].name);
+       const int key = screen_getch(buf);
        g_free(buf);
 
        if (key == ERR) {
@@ -275,7 +277,7 @@ overwrite_key(int cmd_index, int key_index)
                return;
        }
 
-       cmd = find_key_command(key, cmds);
+       const command_t cmd = find_key_command(key, cmds);
        if (cmd != CMD_NONE) {
                screen_status_printf(_("Error: key %s is already used for %s"),
                                     key2str(key), get_key_command_name(cmd));
@@ -291,7 +293,7 @@ overwrite_key(int cmd_index, int key_index)
        check_subcmd_length();
 
        /* repaint */
-       keydef_repaint();
+       keydef_paint();
 
        /* update key conflict flags */
        check_key_bindings(cmds, NULL, 0);
@@ -306,7 +308,7 @@ add_key(int cmd_index)
 }
 
 static const char *
-list_callback(unsigned idx, G_GNUC_UNUSED void *data)
+list_callback(unsigned idx, gcc_unused void *data)
 {
        static char buf[256];
 
@@ -357,13 +359,13 @@ list_callback(unsigned idx, G_GNUC_UNUSED void *data)
 }
 
 static void
-keydef_init(WINDOW *w, int cols, int rows)
+keydef_init(WINDOW *w, unsigned cols, unsigned rows)
 {
        lw = list_window_init(w, cols, rows);
 }
 
 static void
-keydef_resize(int cols, int rows)
+keydef_resize(unsigned cols, unsigned rows)
 {
        list_window_resize(lw, cols, rows);
 }
@@ -379,18 +381,17 @@ keydef_exit(void)
 }
 
 static void
-keydef_open(G_GNUC_UNUSED struct mpdclient *c)
+keydef_open(gcc_unused struct mpdclient *c)
 {
        if (cmds == NULL) {
                command_definition_t *current_cmds = get_command_definitions();
-               size_t cmds_size;
-
                command_n_commands = 0;
                while (current_cmds[command_n_commands].name)
                        command_n_commands++;
 
                /* +1 for the terminator element */
-               cmds_size = (command_n_commands + 1) * sizeof(command_definition_t);
+               size_t cmds_size = (command_n_commands + 1)
+                       * sizeof(command_definition_t);
                cmds = g_malloc0(cmds_size);
                memcpy(cmds, current_cmds, cmds_size);
        }
@@ -426,13 +427,13 @@ keydef_paint(void)
 }
 
 static bool
-keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
+keydef_cmd(gcc_unused struct mpdclient *c, command_t cmd)
 {
        if (cmd == CMD_LIST_RANGE_SELECT)
                return false;
 
        if (list_window_cmd(lw, cmd)) {
-               keydef_repaint();
+               keydef_paint();
                return true;
        }
 
@@ -482,7 +483,7 @@ keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
                screen_find(lw, cmd, list_callback, NULL);
-               keydef_repaint();
+               keydef_paint();
                return true;
 
        default: