continuing input framework integration
[libpicofe.git] / common / config.c
index f90ab0b..63c8f00 100644 (file)
@@ -1,22 +1,34 @@
 /*
  * Human-readable config file management for PicoDrive
- * (c)
+ * (c) notaz, 2008
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#ifdef __EPOC32__
+#include <unistd.h>
+#endif
 #include "config.h"
+#include "input.h"
+#include "lprintf.h"
+
+static char *mystrip(char *str);
+
+#ifndef _MSC_VER
+
 #include "menu.h"
 #include "emu.h"
-#include "lprintf.h"
-#include <Pico/Pico.h>
+#include <pico/pico.h>
 
 extern menu_entry opt_entries[];
 extern menu_entry opt2_entries[];
 extern menu_entry cdopt_entries[];
+extern menu_entry ctrlopt_entries[];
 extern const int opt_entry_count;
 extern const int opt2_entry_count;
 extern const int cdopt_entry_count;
+extern const int ctrlopt_entry_count;
 #ifdef PSP
 extern menu_entry opt3_entries[];
 extern const int opt3_entry_count;
@@ -27,6 +39,7 @@ static menu_entry *cfg_opts[] =
        opt_entries,
        opt2_entries,
        cdopt_entries,
+       ctrlopt_entries,
 #ifdef PSP
        opt3_entries,
 #endif
@@ -37,6 +50,7 @@ static const int *cfg_opt_counts[] =
        &opt_entry_count,
        &opt2_entry_count,
        &cdopt_entry_count,
+       &ctrlopt_entry_count,
 #ifdef PSP
        &opt3_entry_count,
 #endif
@@ -45,24 +59,6 @@ static const int *cfg_opt_counts[] =
 #define NL "\r\n"
 
 
-static char *mystrip(char *str)
-{
-       int i, len;
-
-       len = strlen(str);
-       for (i = 0; i < len; i++)
-               if (str[i] != ' ') break;
-       if (i > 0) memmove(str, str + i, len - i + 1);
-
-       len = strlen(str);
-       for (i = len - 1; i >= 0; i--)
-               if (str[i] != ' ') break;
-       str[i+1] = 0;
-
-       return str;
-}
-
-
 static int seek_sect(FILE *f, const char *section)
 {
        char line[128], *tmp;
@@ -213,38 +209,37 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def)
 }
 
 
-static const char *joyKeyNames[32] =
+static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
 {
-       "UP", "DOWN", "LEFT", "RIGHT", "b1", "b2", "b3", "b4",
-       "b5",  "b6",  "b7",  "b8",  "b9",  "b10", "b11", "b12",
-       "b13", "b14", "b15", "b16", "b17", "b19", "b19", "b20",
-       "b21", "b22", "b23", "b24", "b25", "b26", "b27", "b28"
-};
-
-static void keys_write(FILE *fn, const char *bind_str, const int binds[32],
-               const int def_binds[32], const char * const names[32], int no_defaults)
-{
-       int t, i;
        char act[48];
+       int key_count, t, i;
+       const int *def_binds;
+
+       key_count = in_get_dev_bind_count(dev_id);
+       def_binds = in_get_dev_def_binds(dev_id);
 
-       for (t = 0; t < 32; t++)
+       for (t = 0; t < key_count; t++)
        {
+               const char *name;
                act[0] = act[31] = 0;
+
                if (no_defaults && binds[t] == def_binds[t])
                        continue;
-               if (strcmp(names[t], "???") == 0) continue;
+
+               name = in_get_key_name(dev_id, t);
 #ifdef __GP2X__
-               if (strcmp(names[t], "SELECT") == 0) continue;
+               if (strcmp(name, "SELECT") == 0) continue;
 #endif
+
                if (binds[t] == 0 && def_binds[t] != 0) {
-                       fprintf(fn, "%s %s =" NL, bind_str, names[t]); // no binds
+                       fprintf(fn, "%s %s =" NL, bind_str, name);
                        continue;
                }
 
                for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
                        if (me_ctrl_actions[i].mask & binds[t]) {
                                strncpy(act, me_ctrl_actions[i].name, 31);
-                               fprintf(fn, "%s %s = player%i %s" NL, bind_str, names[t],
+                               fprintf(fn, "%s %s = player%i %s" NL, bind_str, name,
                                        ((binds[t]>>16)&1)+1, mystrip(act));
                        }
                }
@@ -252,7 +247,7 @@ static void keys_write(FILE *fn, const char *bind_str, const int binds[32],
                for (i = 0; emuctrl_actions[i].name != NULL; i++) {
                        if (emuctrl_actions[i].mask & binds[t]) {
                                strncpy(act, emuctrl_actions[i].name, 31);
-                               fprintf(fn, "%s %s = %s" NL, bind_str, names[t], mystrip(act));
+                               fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
                        }
                }
        }
@@ -289,6 +284,10 @@ static int default_var(const menu_entry *me)
                case MA_CDOPT_LEDS:
                        return defaultConfig.EmuOpt;
 
+               case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
+               case MA_OPT_SCALING:     return defaultConfig.scaling;
+               case MA_OPT_ROTATION:    return defaultConfig.rotation;
+
                case MA_OPT_SAVE_SLOT:
                default:
                        return 0;
@@ -382,18 +381,40 @@ write:
                                if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
                                        fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
                        } else if (me->beh == MB_RANGE) {
-                               if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
+                               if (!no_defaults || (*(int *)me->var ^ default_var(me)))
                                        fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
                        }
                }
        }
 
-       // save key config
-       keys_write(fn, "bind", currentConfig.KeyBinds, defaultConfig.KeyBinds, keyNames, no_defaults);
-       keys_write(fn, "bind_joy0", currentConfig.JoyBinds[0], defaultConfig.JoyBinds[0], joyKeyNames, 1);
-       keys_write(fn, "bind_joy1", currentConfig.JoyBinds[1], defaultConfig.JoyBinds[1], joyKeyNames, 1);
-       keys_write(fn, "bind_joy2", currentConfig.JoyBinds[2], defaultConfig.JoyBinds[2], joyKeyNames, 1);
-       keys_write(fn, "bind_joy3", currentConfig.JoyBinds[3], defaultConfig.JoyBinds[3], joyKeyNames, 1);
+       /* input: save device names */
+       for (t = 0; t < IN_MAX_DEVS; t++)
+       {
+               const int  *binds = in_get_dev_binds(t);
+               const char *name =  in_get_dev_name(t, 0);
+               if (binds == NULL || name == NULL)
+                       continue;
+
+               fprintf(fn, "input%d = %s" NL, t, name);
+       }
+
+       /* input: save binds */
+       for (t = 0; t < IN_MAX_DEVS; t++)
+       {
+               const int *binds = in_get_dev_binds(t);
+               const char *name = in_get_dev_name(t, 0);
+               char strbind[16];
+               int count;
+
+               if (binds == NULL || name == NULL)
+                       continue;
+
+               sprintf(strbind, "bind%d", t);
+               if (t == 0) strbind[4] = 0;
+
+               count = in_get_dev_bind_count(t);
+               keys_write(fn, strbind, t, binds, no_defaults);
+       }
 
 #ifndef PSP
        if (section == NULL)
@@ -428,7 +449,7 @@ int config_writelrom(const char *fname)
        int size;
        FILE *f;
 
-       if (strlen(lastRomFile) == 0) return -1;
+       if (strlen(loadedRomFName) == 0) return -1;
 
        f = fopen(fname, "r");
        if (f != NULL)
@@ -461,7 +482,7 @@ int config_writelrom(const char *fname)
                fwrite(old_data, 1, optr - old_data, f);
                free(old_data);
        }
-       fprintf(f, "LastUsedROM = %s" NL, lastRomFile);
+       fprintf(f, "LastUsedROM = %s" NL, loadedRomFName);
        fclose(f);
        return 0;
 }
@@ -492,9 +513,9 @@ int config_readlrom(const char *fname)
                tmp++;
                mystrip(tmp);
 
-               len = sizeof(lastRomFile);
-               strncpy(lastRomFile, tmp, len);
-               lastRomFile[len-1] = 0;
+               len = sizeof(loadedRomFName);
+               strncpy(loadedRomFName, tmp, len);
+               loadedRomFName[len-1] = 0;
                ret = 0;
                break;
        }
@@ -691,57 +712,64 @@ static int custom_read(menu_entry *me, const char *var, const char *val)
 
 static unsigned int keys_encountered = 0;
 
-static void keys_parse(const char *var, const char *val, int binds[32], const char * const names[32])
+static int parse_bind_val(const char *val)
 {
-       int t, i;
-       unsigned int player;
+       int i;
 
-       for (t = 0; t < 32; t++)
-       {
-               if (strcmp(names[t], var) == 0) break;
-       }
-       if (t == 32) {
-               lprintf("unhandled bind \"%s\"\n", var);
-               return;
-       }
-
-       if (binds == currentConfig.KeyBinds && !(keys_encountered & (1<<t))) { // hack
-               binds[t] = 0;
-               keys_encountered |= 1<<t;
-       }
        if (val[0] == 0)
-               return;
+               return 0;
+       
        if (strncasecmp(val, "player", 6) == 0)
        {
+               unsigned int player;
                player = atoi(val + 6) - 1;
-               if (player > 1) goto fail;
+               if (player > 1)
+                       return -1;
+
                for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
-                       if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0) {
-                               binds[t] |= me_ctrl_actions[i].mask | (player<<16);
-                               return;
-                       }
+                       if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
+                               return me_ctrl_actions[i].mask | (player<<16);
                }
        }
        for (i = 0; emuctrl_actions[i].name != NULL; i++) {
-               if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
-                       binds[t] |= emuctrl_actions[i].mask;
-                       return;
-               }
+               if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0)
+                       return emuctrl_actions[i].mask;
+       }
+
+       return -1;
+}
+
+static void keys_parse(const char *key, const char *val, int dev_id)
+{
+       int binds;
+
+       binds = parse_bind_val(val);
+       if (binds == -1) {
+               lprintf("config: unhandled action \"%s\"\n", val);
+               return;
        }
 
-fail:
-       lprintf("unhandled action \"%s\"\n", val);
-       return;
+       in_config_bind_key(dev_id, key, binds);
 }
 
+static int get_numvar_num(const char *var)
+{
+       char *p = NULL;
+       int num;
+       
+       if (var[0] == ' ')
+               return 0;
+
+       num = strtoul(var, &p, 10);
+       if (*p == 0 || *p == ' ')
+               return num;
 
-#define try_joy_parse(num) { \
-       if (strncasecmp(var, "bind_joy"#num " ", 10) == 0) { \
-               keys_parse(var + 10, val, currentConfig.JoyBinds[num], joyKeyNames); \
-               return; \
-       } \
+       return -1;
 }
 
+/* map dev number in confing to input dev number */
+static unsigned char input_dev_map[IN_MAX_DEVS];
+
 static void parse(const char *var, const char *val)
 {
        menu_entry *me;
@@ -755,15 +783,36 @@ static void parse(const char *var, const char *val)
                return;
        }
 
+       /* input: device name */
+       if (strncasecmp(var, "input", 5) == 0) {
+               int num = get_numvar_num(var + 5);
+               if (num >= 0 && num < IN_MAX_DEVS)
+                       input_dev_map[num] = in_config_parse_dev(val);
+               else
+                       lprintf("config: failed to parse: %s\n", var);
+               return;
+       }
+
        // key binds
-       if (strncasecmp(var, "bind ", 5) == 0) {
-               keys_parse(var + 5, val, currentConfig.KeyBinds, keyNames);
+       if (strncasecmp(var, "bind", 4) == 0) {
+               const char *p = var + 4;
+               int num = get_numvar_num(p);
+               if (num < 0 || num >= IN_MAX_DEVS) {
+                       lprintf("config: failed to parse: %s\n", var);
+                       return;
+               }
+
+               num = input_dev_map[num];
+               if (num < 0 || num >= IN_MAX_DEVS) {
+                       lprintf("config: invalid device id: %s\n", var);
+                       return;
+               }
+
+               while (*p && *p != ' ') p++;
+               while (*p && *p == ' ') p++;
+               keys_parse(p, val, num);
                return;
        }
-       try_joy_parse(0)
-       try_joy_parse(1)
-       try_joy_parse(2)
-       try_joy_parse(3)
 
        for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]) && ret == 0; t++)
        {
@@ -790,7 +839,7 @@ static void parse(const char *var, const char *val)
                        ret = custom_read(me, var, val);
                }
        }
-       if (!ret) lprintf("config_readsect: unhandled var: %s\n", var);
+       if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
 }
 
 
@@ -807,12 +856,11 @@ int config_havesect(const char *fname, const char *section)
        return ret;
 }
 
-
 int config_readsect(const char *fname, const char *section)
 {
-       char line[128], *var, *val, *tmp;
-       int len, i, ret;
+       char line[128], *var, *val;
        FILE *f;
+       int ret;
 
        f = fopen(fname, "r");
        if (f == NULL) return -1;
@@ -828,43 +876,88 @@ int config_readsect(const char *fname, const char *section)
        }
 
        keys_encountered = 0;
+       memset(input_dev_map, 0xff, sizeof(input_dev_map));
 
+       in_config_start();
        while (!feof(f))
        {
-               tmp = fgets(line, sizeof(line), f);
-               if (tmp == NULL) break;
-
-               if (line[0] == '[') break; // other section
-
-               // strip comments, linefeed, spaces..
-               len = strlen(line);
-               for (i = 0; i < len; i++)
-                       if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
-               mystrip(line);
-               len = strlen(line);
-               if (len <= 0) continue;
-
-               // get var and val
-               for (i = 0; i < len; i++)
-                       if (line[i] == '=') break;
-               if (i >= len || strchr(&line[i+1], '=') != NULL) {
-                       lprintf("config_readsect: can't parse: %s\n", line);
-                       continue;
-               }
-               line[i] = 0;
-               var = line;
-               val = &line[i+1];
-               mystrip(var);
-               mystrip(val);
-               if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
-                       lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
-                       continue;
-               }
+               ret = config_get_var_val(f, line, sizeof(line), &var, &val);
+               if (ret ==  0) break;
+               if (ret == -1) continue;
 
                parse(var, val);
        }
+       in_config_end();
 
        fclose(f);
        return 0;
 }
 
+#endif // _MSC_VER
+
+static char *mystrip(char *str)
+{
+       int i, len;
+
+       len = strlen(str);
+       for (i = 0; i < len; i++)
+               if (str[i] != ' ') break;
+       if (i > 0) memmove(str, str + i, len - i + 1);
+
+       len = strlen(str);
+       for (i = len - 1; i >= 0; i--)
+               if (str[i] != ' ') break;
+       str[i+1] = 0;
+
+       return str;
+}
+
+/* returns:
+ *  0 - EOF, end
+ *  1 - parsed ok
+ * -1 - failed to parse line
+ */
+int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
+{
+       char *var, *val, *tmp;
+       FILE *f = file;
+       int len, i;
+
+       tmp = fgets(line, lsize, f);
+       if (tmp == NULL) return 0;
+
+       if (line[0] == '[') return 0; // other section
+
+       // strip comments, linefeed, spaces..
+       len = strlen(line);
+       for (i = 0; i < len; i++)
+               if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
+       mystrip(line);
+       len = strlen(line);
+       if (len <= 0) return -1;;
+
+       // get var and val
+       for (i = 0; i < len; i++)
+               if (line[i] == '=') break;
+       if (i >= len || strchr(&line[i+1], '=') != NULL) {
+               lprintf("config_readsect: can't parse: %s\n", line);
+               return -1;
+       }
+       line[i] = 0;
+       var = line;
+       val = &line[i+1];
+       mystrip(var);
+       mystrip(val);
+
+#ifndef _MSC_VER
+       if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
+               lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
+               return -1;;
+       }
+#endif
+
+       *rvar = var;
+       *rval = val;
+       return 1;
+}
+