X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Fconfig.c;h=2704aca5e95e39a6961f04205eec0b72db9effca;hb=595491c49949ad41ffe2c343f1484ae913b6de5f;hp=d4833f6f728da18b1dca190d1f36d8472a3dee05;hpb=960a8e2716349f9a7df6cca9b6e8e6b0724fe7ba;p=libpicofe.git diff --git a/common/config.c b/common/config.c index d4833f6..2704aca 100644 --- a/common/config.c +++ b/common/config.c @@ -1,22 +1,33 @@ /* * Human-readable config file management for PicoDrive - * (c) + * (c) notaz, 2008 */ +#include #include #include +#ifdef __EPOC32__ +#include +#endif #include "config.h" +#include "lprintf.h" + +static char *mystrip(char *str); + +#ifndef _MSC_VER + #include "menu.h" #include "emu.h" -#include "lprintf.h" -#include +#include 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 +38,7 @@ static menu_entry *cfg_opts[] = opt_entries, opt2_entries, cdopt_entries, + ctrlopt_entries, #ifdef PSP opt3_entries, #endif @@ -37,6 +49,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 +58,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; @@ -118,7 +113,6 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def) case 1: str = "hw horizontal"; break; case 2: str = "hw horiz. + vert."; break; case 3: str = "sw horizontal"; break; - case 1: str = "ON"; break; } fprintf(f, "Scaling = %s", str); #endif @@ -214,6 +208,7 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def) } +#if PLAT_HAVE_JOY static const char *joyKeyNames[32] = { "UP", "DOWN", "LEFT", "RIGHT", "b1", "b2", "b3", "b4", @@ -221,31 +216,40 @@ static const char *joyKeyNames[32] = "b13", "b14", "b15", "b16", "b17", "b19", "b19", "b20", "b21", "b22", "b23", "b24", "b25", "b26", "b27", "b28" }; +#endif 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) + const int def_binds[32], const char * const names[32], int key_count, int no_defaults) { int t, i; - char act[48]; + char act[48], name[32]; - for (t = 0; t < 32; t++) + for (t = 0; t < key_count; t++) { act[0] = act[31] = 0; if (no_defaults && binds[t] == def_binds[t]) continue; - if (strcmp(names[t], "???") == 0) continue; #ifdef __GP2X__ if (strcmp(names[t], "SELECT") == 0) continue; #endif + if (t >= 32 || names[t] == NULL || strcmp(names[t], "???") == 0) { + if ((t >= '0' && t <= '9') || (t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z')) + sprintf(name, "%c", t); + else + sprintf(name, "\\x%02x", t); + } + else + strcpy(name, names[t]); + 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); // no binds 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)); } } @@ -253,7 +257,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)); } } } @@ -290,6 +294,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; @@ -383,18 +391,20 @@ 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); + keys_write(fn, "bind", currentConfig.KeyBinds, defaultConfig.KeyBinds, keyNames, PLAT_MAX_KEYS, no_defaults); +#if PLAT_HAVE_JOY + keys_write(fn, "bind_joy0", currentConfig.JoyBinds[0], defaultConfig.JoyBinds[0], joyKeyNames, 32, 1); + keys_write(fn, "bind_joy1", currentConfig.JoyBinds[1], defaultConfig.JoyBinds[1], joyKeyNames, 32, 1); + keys_write(fn, "bind_joy2", currentConfig.JoyBinds[2], defaultConfig.JoyBinds[2], joyKeyNames, 32, 1); + keys_write(fn, "bind_joy3", currentConfig.JoyBinds[3], defaultConfig.JoyBinds[3], joyKeyNames, 32, 1); +#endif #ifndef PSP if (section == NULL) @@ -429,7 +439,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) @@ -462,7 +472,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; } @@ -493,9 +503,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; } @@ -692,21 +702,35 @@ 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 void keys_parse(const char *var, const char *val, int binds[32], + const char * const names[32], int max_keys) { int t, i; unsigned int player; for (t = 0; t < 32; t++) { - if (strcmp(names[t], var) == 0) break; + if (names[t] && strcmp(names[t], var) == 0) break; } - if (t == 32) { + if (t == 32) + { + int len = strlen(var); + if (len == 1) t = var[0]; + else if (len >= 4 && var[0] == '\\' && var[1] == 'x') { + char *p; + t = (int)strtoul(var + 2, &p, 16); + if (*p != 0) t = max_keys; // parse failed + } + else + t = max_keys; // invalid + } + if (t < 0 || t >= max_keys) { lprintf("unhandled bind \"%s\"\n", var); return; } - if (binds == currentConfig.KeyBinds && !(keys_encountered & (1< 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; +} +