GBA: Add configuration loader

This commit is contained in:
Jeffrey Pfau 2014-11-01 03:04:10 -07:00
parent 5f440b6a02
commit 3ed2993e8c
6 changed files with 100 additions and 31 deletions

50
src/gba/gba-config.c Normal file
View File

@ -0,0 +1,50 @@
#include "gba-config.h"
#include "platform/commandline.h"
#include "util/configuration.h"
#define SECTION_NAME_MAX 128
static const char* _lookupValue(const struct Configuration* config, const char* key, const char* port) {
if (port) {
char sectionName[SECTION_NAME_MAX];
snprintf(sectionName, SECTION_NAME_MAX, "ports.%s", port);
sectionName[SECTION_NAME_MAX - 1] = '\0';
const char* value = ConfigurationGetValue(config, sectionName, key);
if (value) {
return value;
}
}
return ConfigurationGetValue(config, 0, key);
}
static bool _lookupIntValue(const struct Configuration* config, const char* key, const char* port, int* out) {
const char* charValue = _lookupValue(config, key, port);
if (!charValue) {
return false;
}
char* end;
long value = strtol(charValue, &end, 10);
if (*end) {
return false;
}
*out = value;
return true;
}
bool GBAConfigLoad(struct Configuration* config) {
return ConfigurationRead(config, BINARY_NAME ".ini");
}
void GBAConfigMapStartupOpts(const struct Configuration* config, const char* port, struct StartupOptions* opts) {
_lookupIntValue(config, "logLevel", port, &opts->logLevel);
_lookupIntValue(config, "frameskip", port, &opts->frameskip);
_lookupIntValue(config, "rewindBufferCapacity", port, &opts->rewindBufferCapacity);
_lookupIntValue(config, "rewindBufferInterval", port, &opts->rewindBufferInterval);
}
void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GraphicsOpts* opts) {
_lookupIntValue(config, "fullscreen", port, &opts->fullscreen);
_lookupIntValue(config, "width", port, &opts->width);
_lookupIntValue(config, "height", port, &opts->height);
}

15
src/gba/gba-config.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef GBA_CONFIG_H
#define GBA_CONFIG_H
#include "util/common.h"
struct Configuration;
struct StartupOptions;
struct GraphicsOpts;
bool GBAConfigLoad(struct Configuration*);
void GBAConfigMapStartupOpts(const struct Configuration*, const char* port, struct StartupOptions*);
void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GraphicsOpts*);
#endif

View File

@ -10,12 +10,15 @@
#include "debugger/gdb-stub.h"
#endif
#include "gba/gba-video.h"
#include <fcntl.h>
#include <getopt.h>
#define GRAPHICS_OPTIONS "234f"
#define GRAPHICS_OPTIONS "1234f"
#define GRAPHICS_USAGE \
"\nGraphics options:\n" \
" -1 1x viewport\n" \
" -2 2x viewport\n" \
" -3 3x viewport\n" \
" -4 4x viewport\n" \
@ -38,8 +41,6 @@ static const struct option _options[] = {
bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg);
bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) {
memset(opts, 0, sizeof(*opts));
int ch;
char options[64] =
"b:Dl:p:s:"
@ -121,10 +122,7 @@ void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts)
parser->opts = opts;
parser->parse = _parseGraphicsArg;
parser->extraOptions = GRAPHICS_OPTIONS;
opts->multiplier = 1;
opts->fullscreen = 0;
opts->width = 240;
opts->height = 160;
opts->multiplier = 0;
}
bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) {
@ -134,29 +132,16 @@ bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) {
case 'f':
graphicsOpts->fullscreen = 1;
return true;
case '1':
case '2':
if (graphicsOpts->multiplier != 1) {
return false;
}
graphicsOpts->multiplier = 2;
graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier;
return true;
case '3':
if (graphicsOpts->multiplier != 1) {
return false;
}
graphicsOpts->multiplier = 3;
graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier;
return true;
case '4':
if (graphicsOpts->multiplier != 1) {
if (graphicsOpts->multiplier) {
return false;
}
graphicsOpts->multiplier = 4;
graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier;
graphicsOpts->multiplier = option - '0';
graphicsOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier;
graphicsOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier;
return true;
default:
return false;

View File

@ -15,17 +15,20 @@ enum DebuggerType {
};
struct StartupOptions {
// Passed only
char* fname;
char* bios;
char* patch;
bool dirmode;
enum DebuggerType debuggerType;
bool debugAtStart;
// Configurable
int logLevel;
int frameskip;
int rewindBufferCapacity;
int rewindBufferInterval;
enum DebuggerType debuggerType;
int debugAtStart;
};
struct SubParser {
@ -36,7 +39,10 @@ struct SubParser {
};
struct GraphicsOpts {
// Passed only
int multiplier;
// Configurable
int fullscreen;
int width;
int height;

View File

@ -43,7 +43,7 @@ int main(int argc, char** argv) {
.opts = &perfOpts
};
struct StartupOptions opts;
struct StartupOptions opts = {};
if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
usage(argv[0], PERF_USAGE);
return 1;

View File

@ -8,10 +8,12 @@
#include "gba-thread.h"
#include "gba.h"
#include "gba-config.h"
#include "sdl-audio.h"
#include "sdl-events.h"
#include "renderers/video-software.h"
#include "platform/commandline.h"
#include "util/configuration.h"
#include <SDL.h>
#ifdef __APPLE__
@ -24,6 +26,8 @@
#include <signal.h>
#include <sys/time.h>
#define PORT "sdl-gl"
struct GLSoftwareRenderer {
struct GBAVideoSoftwareRenderer d;
struct GBASDLAudio audio;
@ -61,9 +65,18 @@ int main(int argc, char** argv) {
struct GLSoftwareRenderer renderer;
GBAVideoSoftwareRendererCreate(&renderer.d);
struct StartupOptions opts;
struct Configuration config;
ConfigurationInit(&config);
GBAConfigLoad(&config);
struct StartupOptions opts = { };
struct GraphicsOpts graphicsOpts = { };
struct SubParser subparser;
struct GraphicsOpts graphicsOpts;
GBAConfigMapStartupOpts(&config, PORT, &opts);
GBAConfigMapGraphicsOpts(&config, PORT, &graphicsOpts);
initParserForGraphics(&subparser, &graphicsOpts);
if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
usage(argv[0], subparser.usage);