bugfixes, new config system and messed code for it

git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@393 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
notaz 2008-03-24 23:11:47 +00:00
parent 3e85ebdd85
commit 367b6f1f93
10 changed files with 286 additions and 126 deletions

View file

@ -24,6 +24,21 @@ static const int *cfg_opt_counts[] = { &opt_entry_count, &opt2_entry_count, &cdo
#define NL "\n"
static void 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;
}
static int seek_sect(FILE *f, const char *section)
{
char line[128], *tmp;
@ -126,6 +141,52 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def)
fprintf(f, NL);
}
static const char *joyKeyNames[32] =
{
"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 *names[32], int no_defaults)
{
int t, i;
char act[48];
for (t = 0; t < 32; 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
for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
if (me_ctrl_actions[i].mask & binds[t]) {
sprintf(act, "player%i ", ((binds[t]>>16)&1)+1);
strncpy(act + 8, me_ctrl_actions[i].name, 31);
break;
}
}
if (act[0] == 0)
{
for (i = 0; emuctrl_actions[i].name != NULL; i++)
if (emuctrl_actions[i].mask & binds[t]) {
strncpy(act, emuctrl_actions[i].name, 31);
break;
}
}
mystrip(act);
fprintf(fn, "%s %s = %s" NL, bind_str, names[t], act);
}
}
static int default_var(const menu_entry *me)
{
switch (me->id)
@ -137,6 +198,7 @@ static int default_var(const menu_entry *me)
case MA_OPT2_ENABLE_Z80:
case MA_OPT2_ENABLE_YM2612:
case MA_OPT2_ENABLE_SN76496:
case MA_OPT2_SVP_DYNAREC:
case MA_CDOPT_CDDA:
case MA_CDOPT_PCM:
case MA_CDOPT_SAVERAM:
@ -253,6 +315,14 @@ write:
}
}
}
// 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);
fprintf(fn, NL);
if (fo != NULL)
@ -274,21 +344,6 @@ write:
}
static void 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;
}
int config_writelrom(const char *fname)
{
char line[128], *tmp, *optr = NULL;
@ -296,7 +351,7 @@ int config_writelrom(const char *fname)
int size;
FILE *f;
if (strlen(currentConfig.lastRomFile) == 0) return 0;
if (strlen(lastRomFile) == 0) return 0;
f = fopen(fname, "r");
if (f != NULL)
@ -329,11 +384,47 @@ int config_writelrom(const char *fname)
fwrite(old_data, 1, optr - old_data, f);
free(old_data);
}
fprintf(f, "LastUsedROM = %s" NL, currentConfig.lastRomFile);
fprintf(f, "LastUsedROM = %s" NL, lastRomFile);
fclose(f);
return 0;
}
/* --------------------------------------------------------------------------*/
int config_readlrom(const char *fname)
{
char line[128], *tmp;
int i, len, ret = -1;
FILE *f;
f = fopen(fname, "r");
if (f == NULL) return -1;
// seek to the section needed
while (!feof(f))
{
tmp = fgets(line, sizeof(line), f);
if (tmp == NULL) break;
if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
len = strlen(line);
for (i = 0; i < len; i++)
if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
tmp = strchr(line, '=');
if (tmp == NULL) break;
tmp++;
mystrip(tmp);
len = sizeof(lastRomFile);
strncpy(lastRomFile, tmp, len);
lastRomFile[len-1] = 0;
ret = 0;
break;
}
fclose(f);
return ret;
}
static int custom_read(menu_entry *me, const char *var, const char *val)
{
@ -432,16 +523,16 @@ static int custom_read(menu_entry *me, const char *var, const char *val)
case MA_OPT_CONFIRM_STATES:
if (strcasecmp(var, "Confirm savestate") != 0) return 0;
if (strcasecmp(val, "OFF") == 0) {
currentConfig.EmuOpt &= 5<<9;
currentConfig.EmuOpt &= ~(5<<9);
} else if (strcasecmp(val, "writes") == 0) {
currentConfig.EmuOpt &= 5<<9;
currentConfig.EmuOpt |= 1<<9;
currentConfig.EmuOpt &= ~(5<<9);
currentConfig.EmuOpt |= 1<<9;
} else if (strcasecmp(val, "loads") == 0) {
currentConfig.EmuOpt &= 5<<9;
currentConfig.EmuOpt |= 4<<9;
currentConfig.EmuOpt &= ~(5<<9);
currentConfig.EmuOpt |= 4<<9;
} else if (strcasecmp(val, "both") == 0) {
currentConfig.EmuOpt &= 5<<9;
currentConfig.EmuOpt |= 5<<9;
currentConfig.EmuOpt &= ~(5<<9);
currentConfig.EmuOpt |= 5<<9;
} else
return 0;
return 1;
@ -469,23 +560,82 @@ static int custom_read(menu_entry *me, const char *var, const char *val)
return 1;
default:
if (strcasecmp(var, "LastUsedROM") == 0) {
tmpi = sizeof(currentConfig.lastRomFile);
strncpy(currentConfig.lastRomFile, val, tmpi);
currentConfig.lastRomFile[tmpi-1] = 0;
return 1;
}
lprintf("unhandled custom_read: %i\n", me->id);
return 0;
}
}
static void keys_parse(const char *var, const char *val, int binds[32], const char *names[32])
{
int t, i, keys_encountered = 0;
unsigned int player;
for (t = 0; t < 32; t++)
{
if (strcmp(names[t], var) == 0) break;
}
if (t == 32) {
lprintf("unhandled bind \"%s\"\n", var);
return;
}
if (!(keys_encountered & (1<<t))) {
binds[t] = 0;
keys_encountered |= 1<<t;
}
if (val[0] == 0)
return;
if (strncasecmp(val, "player", 6) == 0)
{
player = atoi(val + 6) - 1;
if (player > 1) goto fail;
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;
}
}
}
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;
}
}
fail:
lprintf("unhandled action \"%s\"\n", val);
return;
}
#define try_joy_parse(num) { \
if (strncasecmp(var, "bind_joy"#num " ", 10) == 0) { \
keys_parse(var + 10, val, currentConfig.JoyBinds[num], joyKeyNames); \
return; \
} \
}
static void parse(const char *var, const char *val)
{
menu_entry *me;
int t, i, tlen, tmp, ret = 0;
if (strcasecmp(var, "LastUsedROM") == 0)
return; /* handled elsewhere */
// key binds
if (strncasecmp(var, "bind ", 5) == 0) {
keys_parse(var + 5, val, currentConfig.KeyBinds, keyNames);
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++)
{
me = cfg_opts[t];
@ -515,12 +665,27 @@ static void parse(const char *var, const char *val)
}
int config_havesect(const char *fname, const char *section)
{
FILE *f;
int ret;
f = fopen(fname, "r");
if (f == NULL) return 0;
ret = seek_sect(f, section);
fclose(f);
return ret;
}
int config_readsect(const char *fname, const char *section)
{
char line[128], *var, *val, *tmp;
FILE *f = fopen(fname, "r");
int len, i, ret;
FILE *f;
f = fopen(fname, "r");
if (f == NULL) return 0;
if (section != NULL)
@ -560,7 +725,7 @@ int config_readsect(const char *fname, const char *section)
val = &line[i+1];
mystrip(var);
mystrip(val);
if (strlen(var) == 0 || strlen(val) == 0) {
if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
continue;
}

View file

@ -1,6 +1,6 @@
#include <stdio.h>
int config_writesect(const char *fname, const char *section);
int config_writelrom(const char *fname);
int config_readsect(const char *fname, const char *section);
int config_readlrom(const char *fname);
int config_havesect(const char *fname, const char *section);

View file

@ -40,6 +40,7 @@ int rom_loaded = 0;
char noticeMsg[64];
int state_slot = 0;
int config_slot = 0, config_slot_current = 0;
char lastRomFile[512];
unsigned char *movie_data = NULL;
static int movie_size = 0;
@ -139,6 +140,7 @@ int emu_findBios(int region, char **bios_file)
}
/* check if the name begins with BIOS name */
/*
static int emu_isBios(const char *name)
{
int i;
@ -150,8 +152,9 @@ static int emu_isBios(const char *name)
if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
return 0;
}
*/
static unsigned char scd_id_header[0x100];
static unsigned char id_header[0x100];
/* checks if romFileName points to valid MegaCD image
* if so, checks for suitable BIOS */
@ -177,7 +180,7 @@ int emu_cdCheck(int *pregion)
}
pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);
pm_read(scd_id_header, sizeof(scd_id_header), cd_f);
pm_read(id_header, sizeof(id_header), cd_f);
/* it seems we have a CD image here. Try to detect region now.. */
pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
@ -229,21 +232,11 @@ static int extract_text(char *dest, unsigned char *src, int len, int swab)
char *emu_makeRomId(void)
{
static char id_string[3+0x11+0x11+0x30+16];
unsigned char *id_header;
int pos;
if (Pico.rom == NULL) {
id_string[0] = 0;
return id_string;
}
if (PicoMCD & 1) {
id_header = scd_id_header;
strcpy(id_string, "CD|");
} else {
id_header = Pico.rom + 0x100;
strcpy(id_string, "MD|");
}
if (PicoMCD & 1)
strcpy(id_string, "CD|");
else strcpy(id_string, "MD|");
pos = 3;
pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, 1); // serial
@ -253,7 +246,6 @@ char *emu_makeRomId(void)
pos += extract_text(id_string + pos, id_header + 0x50, 0x30, 1); // overseas name
id_string[pos] = 0;
printf("id_string: %s\n", id_string);
return id_string;
}
@ -391,6 +383,8 @@ int emu_ReloadRom(void)
}
// load config for this ROM (do this before insert to get correct region)
if (!(PicoMCD&1))
memcpy(id_header, rom_data + 0x100, sizeof(id_header));
if (!cfg_loaded) {
ret = emu_ReadConfig(1, 1);
if (!ret) emu_ReadConfig(0, 1);
@ -418,13 +412,6 @@ int emu_ReloadRom(void)
menu_romload_end();
if (!emu_isBios(romFileName))
{
// emu_ReadConfig() might have messed currentConfig.lastRomFile
strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
}
if (PicoPatches) {
PicoPatchPrepare();
PicoPatchApply();
@ -463,6 +450,8 @@ int emu_ReloadRom(void)
if (currentConfig.EmuOpt & 1)
emu_SaveLoadGame(1, 1);
strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1);
lastRomFile[sizeof(lastRomFile)-1] = 0;
rom_loaded = 1;
return 1;
}
@ -509,14 +498,18 @@ int emu_ReadConfig(int game, int no_defaults)
}
else
{
if (!no_defaults)
emu_setDefaultConfig();
char *sect = emu_makeRomId();
// try new .cfg way
if (config_slot != 0)
sprintf(cfg, "game.%i.cfg", config_slot);
else strcpy(cfg, "game.cfg");
ret = config_readsect(cfg, emu_makeRomId());
ret = -1;
if (config_havesect(cfg, sect)) {
emu_setDefaultConfig();
ret = config_readsect(cfg, sect);
}
if (ret != 0)
{
@ -532,7 +525,9 @@ int emu_ReadConfig(int game, int no_defaults)
f = fopen(cfg, "rb");
}
if (f) {
int bread = fread(&currentConfig, 1, sizeof(currentConfig), f);
int bread;
fseek(f, 512, SEEK_SET); // skip unused lrom buffer
bread = fread(&currentConfig, 1, sizeof(currentConfig), f);
lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");
fclose(f);
ret = 0;
@ -548,7 +543,7 @@ int emu_ReadConfig(int game, int no_defaults)
}
else
{
lprintf("loaded cf from game sect\n");
lprintf("loaded cfg from sect \"%s\"\n", sect);
}
}
@ -593,10 +588,12 @@ int emu_WriteConfig(int is_game)
sprintf(cfg, "game.%i.cfg", config_slot);
else strcpy(cfg, "game.cfg");
game_sect = emu_makeRomId();
lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect);
}
lprintf("emu_WriteConfig: %s ", cfg);
ret = config_writesect(cfg, game_sect);
if (write_lrom) config_writelrom(cfg);
#ifndef NO_SYNC
sync();
#endif

View file

@ -4,7 +4,7 @@
// For commercial use, separate licencing terms must be obtained.
typedef struct {
char lastRomFile[512];
// char lastRomFile[512];
int EmuOpt; // LSb->MSb: use_sram, show_fps, enable_sound, gzip_saves,
// squidgehack, no_save_cfg_on_exit, <unused>, 16_bit_mode
// craigix_ram, confirm_save, show_cd_leds, confirm_load
@ -33,6 +33,7 @@ extern char noticeMsg[64];
extern int state_slot;
extern int config_slot, config_slot_current;
extern unsigned char *movie_data;
extern char lastRomFile[512];
int emu_ReloadRom(void);
@ -49,4 +50,5 @@ void emu_textOut8 (int x, int y, const char *text);
void emu_textOut16(int x, int y, const char *text);
char *emu_makeRomId(void);
extern const char *keyNames[];
void emu_prepareDefaultConfig(void);

View file

@ -30,6 +30,24 @@
char menuErrorMsg[64] = { 0, };
// PicoPad[] format: MXYZ SACB RLDU
me_bind_action me_ctrl_actions[12] =
{
{ "UP ", 0x001 },
{ "DOWN ", 0x002 },
{ "LEFT ", 0x004 },
{ "RIGHT ", 0x008 },
{ "A ", 0x040 },
{ "B ", 0x010 },
{ "C ", 0x020 },
{ "START ", 0x080 },
{ "MODE ", 0x800 },
{ "X ", 0x400 },
{ "Y ", 0x200 },
{ "Z ", 0x100 }
};
static unsigned char menu_font_data[10240];
static int menu_text_color = 0xffff; // default to white
static int menu_sel_color = -1; // disabled

View file

@ -1,4 +1,4 @@
// (c) Copyright 2006,2007 notaz, All rights reserved.
// (c) Copyright 2006-2008 notaz, All rights reserved.
void menu_init(void);
@ -9,7 +9,6 @@ void menu_draw_selection(int x, int y, int w);
extern char menuErrorMsg[64];
typedef enum
{
MB_NONE = 1, /* no auto processing */
@ -65,6 +64,7 @@ typedef enum
MA_OPT2_SQUIDGEHACK, /* gp2x */
MA_OPT2_STATUS_LINE, /* psp */
MA_OPT2_NO_FRAME_LIMIT, /* psp */
MA_OPT2_SVP_DYNAREC,
MA_OPT2_DONE,
MA_OPT3_SCALE, /* psp (all OPT3) */
MA_OPT3_HSCALE32,
@ -102,6 +102,15 @@ typedef struct
char need_to_save;
} menu_entry;
typedef struct
{
char *name;
int mask;
} me_bind_action;
extern me_bind_action me_ctrl_actions[12];
extern me_bind_action emuctrl_actions[]; // platform code
typedef void (me_draw_custom_f)(const menu_entry *entry, int x, int y, void *param);