From 2be9749940f3a5b2e568aa56613c90c5ea66ef92 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 16 Mar 2014 06:51:15 +0000
Subject: [PATCH] [Add] Some nice additions to keybinds menu, showing bindings
 in both naviation/information pane.

---
 src/input.c   |  2 +-
 src/input.h   |  2 +-
 src/options.c | 54 +++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/src/input.c b/src/input.c
index 11a16b7..f10a479 100644
--- a/src/input.c
+++ b/src/input.c
@@ -212,7 +212,7 @@ void input_setKeybind(char* keybind, KeybindType type, int key,
 /**
  * @brief Get the value of a keybind.
  */
-SDLKey input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, int* reverse) {
+SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse) {
   int i;
   for(i = 0; strcmp(keybindNames[i], "end"); i++)
     if(strcmp(keybind, input_keybinds[i]->name)==0) {
diff --git a/src/input.h b/src/input.h
index ae6f282..640bcb7 100644
--- a/src/input.h
+++ b/src/input.h
@@ -14,7 +14,7 @@ typedef enum {
 /* Set input. */
 void input_setDefault(void);
 void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse);
-SDLKey input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod,  int* reverse);
+SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod,  int* reverse);
 const char* input_getKeybindDescription(char* keybind);
 
 /* Handle the events. */
diff --git a/src/options.c b/src/options.c
index 7b39271..1e57c83 100644
--- a/src/options.c
+++ b/src/options.c
@@ -32,6 +32,10 @@ void opt_menuKeybinds(void) {
   int i, j;
   unsigned int wid;
   char** str;
+  SDLKey key;
+  KeybindType type;
+  SDLMod mod;
+  int reverse;
 
   /* Create the window. */
   wid = window_create("Keybindings", -1, -1, KEYBINDS_WIDTH, KEYBINDS_HEIGHT);
@@ -48,8 +52,27 @@ void opt_menuKeybinds(void) {
   /* Create the list. */
   for(i = 0; strcmp(keybindNames[i], "end"); i++);
   str = malloc(sizeof(char*) * (i-1));
-  for(j = 0; j < i; j++)
-    str[j] = strdup(keybindNames[j]);
+  for(j = 0; j < i; j++) {
+    str[j] = malloc(sizeof(char) * 64);
+    key = input_getKeybind(keybindNames[j], &type, &mod, &reverse);
+    switch(type) {
+      case KEYBIND_KEYBOARD:
+        if(isalpha(key))
+          snprintf(str[j], 64, "%s <%c>", keybindNames[j], toupper(key));
+        else
+          snprintf(str[j], 64, "%s <%s>", keybindNames[j], SDL_GetKeyName(key));
+        break;
+      case KEYBIND_JAXIS:
+        snprintf(str[j], 64, "%s <jb%d>", keybindNames[j], key);
+        break;
+      case KEYBIND_JBUTTON:
+        snprintf(str[j], 64, "%s <ja%d>", keybindNames[j], key);
+        break;
+      default:
+        snprintf(str[j], 64, keybindNames[j]);
+        break;
+    }
+  }
   window_addList(wid, 20, -40, 160, KEYBINDS_HEIGHT-60, "lstKeybinds",
       str, i-1, 0, menuKeybinds_update);
 
@@ -80,7 +103,8 @@ static const char* modToText(SDLMod mod) {
  */
 static void menuKeybinds_update(unsigned int wid, char* name) {
   (void) name;
-  char* keybind;
+  int i;
+  char* selected,  keybind[32];
   const char* desc;
 
   SDLKey key;
@@ -92,7 +116,12 @@ static void menuKeybinds_update(unsigned int wid, char* name) {
   char bind[32];
 
   /* Get the keybind. */
-  keybind = toolkit_getList(wid, "lstKeybinds");
+  selected = toolkit_getList(wid, "lstKeybinds");
+
+  /* Remove the axcess. */
+  for(i = 0; (selected[i] != '\0') && (selected[i] != ' '); i++)
+    keybind[i] = selected[i];
+  keybind[i] = '\0';
   window_modifyText(wid, "txtName", keybind);
 
   /* Get information. */
@@ -105,15 +134,16 @@ static void menuKeybinds_update(unsigned int wid, char* name) {
       snprintf(bind, 64, "Not bound");
       break;
     case KEYBIND_KEYBOARD:
-      snprintf(pre, 32, "keyboard:  %s%s",
-          (mod != KMOD_NONE) ? modToText(mod) : "",
-          (mod != KMOD_NONE) ? " + " : "");
-
-      /* Is key. */
-      if(isalnum(key))
-        snprintf(bind, 32, "%s%c", pre, toupper((char)key));
+      if(isalpha(key))
+        snprintf(bind, 32, "keyboard:   %s%s%c",
+            (mod != KMOD_NONE) ? modToText(mod) : "",
+            (mod != KMOD_NONE) ? " + " : "",
+            toupper(key));
       else
-        snprintf(bind, 32, "%s<%d>", pre, key);
+        snprintf(bind, 32, "keyboard:   %s%s%s",
+            (mod != KMOD_NONE) ? modToText(mod) : "",
+            (mod != KMOD_NONE) ? " + " : "",
+            SDL_GetKeyName(key));
       break;
     case KEYBIND_JAXIS:
       snprintf(bind, 64, "joy axis:   <%d>%s", key, (reverse) ? " rev" : "");