Code

fix shadow warnings
[ncmpc.git] / src / screen_keydef.c
index b85c094c557412f35b6d859d679e9513395e54ea..bc37f1af3edc2958452d73d8711cd5eb16c2b40a 100644 (file)
  *
  */
 
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <glib.h>
-#include <ncurses.h>
-
 #include "config.h"
 
-#ifdef  ENABLE_KEYDEF_SCREEN
+#ifndef  DISABLE_KEYDEF_SCREEN
 #include "ncmpc.h"
 #include "mpdclient.h"
 #include "options.h"
 #include "screen.h"
 #include "screen_utils.h"
 
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <ncurses.h>
+
 #define STATIC_ITEMS      0
 #define STATIC_SUB_ITEMS  1
 #define BUFSIZE 256
@@ -102,7 +102,7 @@ save_keys(void)
       g_free(filename);
       return -1;
     }
-  if( write_key_bindings(f) )
+  if( write_key_bindings(f, KEYDEF_WRITE_HEADER) )
     screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
   else
     screen_status_printf(_("Wrote %s"), filename);
@@ -137,21 +137,24 @@ delete_key(int cmd_index, int key_index)
   while( i<MAX_COMMAND_KEYS && cmds[cmd_index].keys[i] )
     cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
   cmds[cmd_index].keys[key_index] = 0;
-
+  cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
   check_subcmd_length();
   lw->clear = 1;
   lw->repaint = 1;
+  /* update key conflict flags */
+  check_key_bindings(cmds, NULL, 0);
 }
 
 static void
 assign_new_key(WINDOW *w, int cmd_index, int key_index)
 {
   int key;
-  char buf[BUFSIZE];
+  char *buf;
   command_t cmd;
 
-  snprintf(buf, BUFSIZE, _("Enter new key for %s: "), cmds[cmd_index].name);
+  buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
   key = screen_getch(w, buf);
+  g_free(buf);
   if( key==KEY_RESIZE )
     screen_resize();
   if( key==ERR )
@@ -169,48 +172,49 @@ assign_new_key(WINDOW *w, int cmd_index, int key_index)
       return;
     }
   cmds[cmd_index].keys[key_index] = key;
+  cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
+  
   screen_status_printf(_("Assigned %s to %s"), 
                       key2str(key),cmds[cmd_index].name);
   check_subcmd_length();
   lw->repaint = 1;
+  /* update key conflict flags */
+  check_key_bindings(cmds, NULL, 0);
 }
 
-static char *
-list_callback(int index, int *highlight, void *data)
+static const char *
+list_callback(int idx, int *highlight, void *data)
 {
-  static char buf[BUFSIZE];
+       static char buf[BUFSIZE];
+
+       *highlight = 0;
+       if (subcmd < 0) {
+               if (idx < command_list_length) {
+                       if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
+                               *highlight = 1;
+                       return cmds[idx].name;
+               } else if (idx == LIST_ITEM_APPLY())
+                       return LIST_ITEM_APPLY_LABEL;
+               else if (idx == LIST_ITEM_SAVE())
+                       return LIST_ITEM_SAVE_LABEL;
+       } else {
+               if (idx == 0)
+                       return "[..]";
+               idx--;
+               if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
+                       g_snprintf(buf,
+                                  BUFSIZE, "%d. %-20s   (%d) ",
+                                  idx + 1,
+                                  key2str(cmds[subcmd].keys[idx]),
+                                  cmds[subcmd].keys[idx]);
+                       return buf;
+               } else if (idx == subcmd_addpos) {
+                       g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
+                       return buf;
+               }
+       }
 
-  if( subcmd <0 )
-    {
-      if( index<command_list_length )
-       return cmds[index].name;
-      else if( index==LIST_ITEM_APPLY() )
-       return LIST_ITEM_APPLY_LABEL;
-      else if( index==LIST_ITEM_SAVE() )
-       return LIST_ITEM_SAVE_LABEL;
-    }
-  else
-  {
-    if( index== 0 )
-      return "[..]";
-    index--;
-    if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 )
-      {
-       snprintf(buf, 
-                BUFSIZE, "%d. %-20s   (%d) ", 
-                index+1, 
-                key2str(cmds[subcmd].keys[index]),
-                cmds[subcmd].keys[index]);
-       return buf;
-      } 
-    else if ( index==subcmd_addpos )
-      {
-       snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 );
-       return buf;
-      }
-  }
-  
-  return NULL;
+       return NULL;
 }
 
 static void 
@@ -271,13 +275,13 @@ keydef_close(void)
     screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
 }
 
-static char *
+static const char *
 keydef_title(char *str, size_t size)
 {
   if( subcmd<0 )
     return _("Edit key bindings");
   
-  snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
+  g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
   return str;
 }
 
@@ -300,66 +304,61 @@ keydef_update(screen_t *screen, mpdclient_t *c)
     }
 }
 
-static int 
+static int
 keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
 {
-  int length = LIST_LENGTH();
-
-  if( subcmd>=0 )
-    length = subcmd_length;
-
-  switch(cmd)
-    {
-    case CMD_PLAY:
-      if( subcmd<0 )
-       {
-         if( lw->selected == LIST_ITEM_APPLY() )
-           apply_keys();
-         else if( lw->selected == LIST_ITEM_SAVE() )
-           {
-             apply_keys();
-             save_keys();
-           }
-         else
-           {
-             subcmd = lw->selected;
-             lw->selected=0;
-             check_subcmd_length();
-           }
-       }
-      else
-       {
-         if( lw->selected == 0 ) /* up */
-           {
-             lw->selected = subcmd;
-             subcmd = -1;
-           }
-         else
-           assign_new_key(screen->status_window.w, 
-                          subcmd,
-                          lw->selected-STATIC_SUB_ITEMS);
+       int length = LIST_LENGTH();
+
+       if (subcmd >= 0)
+               length = subcmd_length;
+
+       switch(cmd) {
+       case CMD_PLAY:
+               if( subcmd<0 ) {
+                       if( lw->selected == LIST_ITEM_APPLY() )
+                               apply_keys();
+                       else if( lw->selected == LIST_ITEM_SAVE() ) {
+                               apply_keys();
+                               save_keys();
+                       } else {
+                               subcmd = lw->selected;
+                               lw->selected=0;
+                               check_subcmd_length();
+                       }
+               } else {
+                       if (lw->selected == 0) { /* up */
+                               lw->selected = subcmd;
+                               subcmd = -1;
+                       } else
+                               assign_new_key(screen->status_window.w,
+                                              subcmd,
+                                              lw->selected-STATIC_SUB_ITEMS);
+               }
+               lw->repaint = 1;
+               lw->clear = 1;
+               return 1;
+       case CMD_DELETE:
+               if (subcmd >= 0 && lw->selected - STATIC_SUB_ITEMS >= 0)
+                       delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
+               return 1;
+               break;
+       case CMD_SAVE_PLAYLIST:
+               apply_keys();
+               save_keys();
+               break;
+       case CMD_LIST_FIND:
+       case CMD_LIST_RFIND:
+       case CMD_LIST_FIND_NEXT:
+       case CMD_LIST_RFIND_NEXT:
+               return screen_find(screen,
+                                  lw,  length,
+                                  cmd, list_callback, NULL);
+
+       default:
+               break;
        }
-      lw->repaint = 1;
-      lw->clear = 1;
-      return 1;
-    case CMD_DELETE:
-      if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 )
-       delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS);
-      return 1;
-      break;
-    case CMD_LIST_FIND:
-    case CMD_LIST_RFIND:
-    case CMD_LIST_FIND_NEXT:
-    case CMD_LIST_RFIND_NEXT:
-      return screen_find(screen, c, 
-                        lw,  length,
-                        cmd, list_callback);
-
-    default:
-      break;
-    }
 
-  return list_window_cmd(lw, length, cmd);
+       return list_window_cmd(lw, length, cmd);
 }
 
 static list_window_t *