all repos — mgba @ f36a74759a291b63520eaabf853025ffbd221c80

mGBA Game Boy Advance Emulator

GBA Config: Begin refactor of separating out command line args from settings
Jeffrey Pfau jeffrey@endrift.com
Sun, 02 Nov 2014 01:49:15 -0800
commit

f36a74759a291b63520eaabf853025ffbd221c80

parent

5607a130398eae0691e2d3664555bc54c9c6fc99

M src/gba/gba-config.csrc/gba/gba-config.c

@@ -45,7 +45,7 @@ bool GBAConfigLoad(struct Configuration* config) {

return ConfigurationRead(config, BINARY_NAME ".ini"); } -void GBAConfigMapStartupOpts(const struct Configuration* config, const char* port, struct StartupOptions* opts) { +void GBAConfigMapGeneralOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) { _lookupCharValue(config, "bios", port, &opts->bios); _lookupIntValue(config, "logLevel", port, &opts->logLevel); _lookupIntValue(config, "frameskip", port, &opts->frameskip);

@@ -53,8 +53,13 @@ _lookupIntValue(config, "rewindBufferCapacity", port, &opts->rewindBufferCapacity);

_lookupIntValue(config, "rewindBufferInterval", port, &opts->rewindBufferInterval); } -void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GraphicsOpts* opts) { +void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) { _lookupIntValue(config, "fullscreen", port, &opts->fullscreen); _lookupIntValue(config, "width", port, &opts->width); _lookupIntValue(config, "height", port, &opts->height); } + +void GBAConfigFreeOpts(struct GBAOptions* opts) { + free(opts->bios); + opts->bios = 0; +}
M src/gba/gba-config.hsrc/gba/gba-config.h

@@ -4,12 +4,23 @@

#include "util/common.h" struct Configuration; -struct StartupOptions; -struct GraphicsOpts; + +struct GBAOptions { + char* bios; + int logLevel; + int frameskip; + int rewindBufferCapacity; + int rewindBufferInterval; + + int fullscreen; + int width; + int height; +}; bool GBAConfigLoad(struct Configuration*); -void GBAConfigMapStartupOpts(const struct Configuration*, const char* port, struct StartupOptions*); -void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GraphicsOpts*); +void GBAConfigMapGeneralOpts(const struct Configuration*, const char* port, struct GBAOptions*); +void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GBAOptions*); +void GBAConfigFreeOpts(struct GBAOptions*); #endif
M src/gba/gba-thread.csrc/gba/gba-thread.c

@@ -217,7 +217,15 @@

return 0; } -void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threadContext) { +void GBAMapOptionsToContext(struct GBAOptions* opts, struct GBAThread* threadContext) { + threadContext->bios = VFileOpen(opts->bios, O_RDONLY); + threadContext->frameskip = opts->frameskip; + threadContext->logLevel = opts->logLevel; + threadContext->rewindBufferCapacity = opts->rewindBufferCapacity; + threadContext->rewindBufferInterval = opts->rewindBufferInterval; +} + +void GBAMapStartupOptionsToContext(struct StartupOptions* opts, struct GBAThread* threadContext) { if (opts->dirmode) { threadContext->gameDir = VDirOpen(opts->fname); threadContext->stateDir = threadContext->gameDir;

@@ -228,12 +236,7 @@ threadContext->gameDir = VDirOpenZip(opts->fname, 0);

#endif } threadContext->fname = opts->fname; - threadContext->bios = VFileOpen(opts->bios, O_RDONLY); threadContext->patch = VFileOpen(opts->patch, O_RDONLY); - threadContext->frameskip = opts->frameskip; - threadContext->logLevel = opts->logLevel; - threadContext->rewindBufferCapacity = opts->rewindBufferCapacity; - threadContext->rewindBufferInterval = opts->rewindBufferInterval; } bool GBAThreadStart(struct GBAThread* threadContext) {
M src/gba/gba-thread.hsrc/gba/gba-thread.h

@@ -94,7 +94,8 @@ struct GBASerializedState** rewindBuffer;

int rewindBufferWriteOffset; }; -void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*); +void GBAMapOptionsToContext(struct GBAOptions*, struct GBAThread*); +void GBAMapStartupOptionsToContext(struct StartupOptions*, struct GBAThread*); bool GBAThreadStart(struct GBAThread* threadContext); bool GBAThreadHasStarted(struct GBAThread* threadContext);
M src/platform/commandline.csrc/platform/commandline.c

@@ -38,9 +38,9 @@ { "patch", required_argument, 0, 'p' },

{ 0, 0, 0, 0 } }; -bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg); +bool _parseGraphicsArg(struct SubParser* parser, struct GBAOptions* gbaOpts, int option, const char* arg); -bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) { +bool parseCommandArgs(struct StartupOptions* opts, struct GBAOptions* gbaOpts, int argc, char* const* argv, struct SubParser* subparser) { int ch; char options[64] = "b:Dl:p:s:"

@@ -58,7 +58,10 @@ }

while ((ch = getopt_long(argc, argv, options, _options, 0)) != -1) { switch (ch) { case 'b': - opts->bios = strdup(optarg); + if (gbaOpts->bios) { + free(gbaOpts->bios); + } + gbaOpts->bios = strdup(optarg); break; case 'D': opts->dirmode = true;

@@ -80,17 +83,17 @@ opts->debuggerType = DEBUGGER_GDB;

break; #endif case 'l': - opts->logLevel = atoi(optarg); + gbaOpts->logLevel = atoi(optarg); break; case 'p': opts->patch = strdup(optarg); break; case 's': - opts->frameskip = atoi(optarg); + gbaOpts->frameskip = atoi(optarg); break; default: if (subparser) { - if (!subparser->parse(subparser, ch, optarg)) { + if (!subparser->parse(subparser, gbaOpts, ch, optarg)) { return false; } }

@@ -110,9 +113,6 @@ void freeOptions(struct StartupOptions* opts) {

free(opts->fname); opts->fname = 0; - free(opts->bios); - opts->bios = 0; - free(opts->patch); opts->patch = 0; }

@@ -125,12 +125,12 @@ parser->extraOptions = GRAPHICS_OPTIONS;

opts->multiplier = 0; } -bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) { +bool _parseGraphicsArg(struct SubParser* parser, struct GBAOptions* gbaOpts, int option, const char* arg) { UNUSED(arg); struct GraphicsOpts* graphicsOpts = parser->opts; switch (option) { case 'f': - graphicsOpts->fullscreen = 1; + gbaOpts->fullscreen = 1; return true; case '1': case '2':

@@ -140,8 +140,8 @@ if (graphicsOpts->multiplier) {

return false; } graphicsOpts->multiplier = option - '0'; - graphicsOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier; - graphicsOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier; + gbaOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier; + gbaOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier; return true; default: return false;
M src/platform/commandline.hsrc/platform/commandline.h

@@ -3,6 +3,8 @@ #define COMMAND_LINE_H

#include "util/common.h" +#include "gba-config.h" + enum DebuggerType { DEBUGGER_NONE = 0, #ifdef USE_CLI_DEBUGGER

@@ -15,40 +17,26 @@ DEBUGGER_MAX

}; struct StartupOptions { - // Passed only char* fname; char* patch; bool dirmode; enum DebuggerType debuggerType; bool debugAtStart; - - // Configurable - char* bios; - int logLevel; - int frameskip; - int rewindBufferCapacity; - int rewindBufferInterval; }; struct SubParser { const char* usage; - bool (*parse)(struct SubParser* parser, int option, const char* arg); + bool (*parse)(struct SubParser* parser, struct GBAOptions* gbaOpts, int option, const char* arg); const char* extraOptions; void* opts; }; struct GraphicsOpts { - // Passed only int multiplier; - - // Configurable - int fullscreen; - int width; - int height; }; -bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser); +bool parseCommandArgs(struct StartupOptions* opts, struct GBAOptions* gbaOpts, int argc, char* const* argv, struct SubParser* subparser); void freeOptions(struct StartupOptions* opts); void usage(const char* arg0, const char* extraOptions);
M src/platform/perf-main.csrc/platform/perf-main.c

@@ -25,7 +25,7 @@ };

static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet); static void _GBAPerfShutdown(int signal); -static bool _parsePerfOpts(struct SubParser* parser, int option, const char* arg); +static bool _parsePerfOpts(struct SubParser* parser, struct GBAOptions* gbaOpts, int option, const char* arg); static struct GBAThread* _thread;

@@ -43,8 +43,9 @@ .extraOptions = PERF_OPTIONS,

.opts = &perfOpts }; + struct GBAOptions gbaOpts = {}; struct StartupOptions opts = {}; - if (!parseCommandArgs(&opts, argc, argv, &subparser)) { + if (!parseCommandArgs(&opts, &gbaOpts, argc, argv, &subparser)) { usage(argv[0], PERF_USAGE); return 1; }

@@ -65,7 +66,8 @@

context.debugger = createDebugger(&opts); char gameCode[5] = { 0 }; - GBAMapOptionsToContext(&opts, &context); + GBAMapStartupOptionsToContext(&opts, &context); + GBAMapOptionsToContext(&gbaOpts, &context); GBAThreadStart(&context); GBAGetGameCode(context.gba, gameCode);

@@ -147,7 +149,8 @@ _thread->state = THREAD_EXITING;

pthread_mutex_unlock(&_thread->stateMutex); } -static bool _parsePerfOpts(struct SubParser* parser, int option, const char* arg) { +static bool _parsePerfOpts(struct SubParser* parser, struct GBAOptions* gbaOpts, int option, const char* arg) { + UNUSED(gbaOpts); struct PerfOpts* opts = parser->opts; errno = 0; switch (option) {
M src/platform/qt/GBAApp.cppsrc/platform/qt/GBAApp.cpp

@@ -12,8 +12,11 @@ {

QApplication::setApplicationName(PROJECT_NAME); QApplication::setApplicationVersion(PROJECT_VERSION); - if (parseCommandArgs(&m_opts, argc, argv, 0)) { + if (parseCommandArgs(&m_opts, &m_gbaOpts, argc, argv, 0)) { + m_window.setOptions(&m_gbaOpts); m_window.optionsPassed(&m_opts); + } else { + m_window.setOptions(&m_gbaOpts); } m_window.show();
M src/platform/qt/GBAApp.hsrc/platform/qt/GBAApp.h

@@ -7,6 +7,7 @@ #include "Window.h"

extern "C" { #include "platform/commandline.h" +#include "util/configuration.h" } namespace QGBA {

@@ -27,6 +28,8 @@ private:

Window m_window; StartupOptions m_opts; + GBAOptions m_gbaOpts; + Configuration m_config; }; }
M src/platform/qt/Window.cppsrc/platform/qt/Window.cpp

@@ -102,24 +102,21 @@ }

} void Window::optionsPassed(StartupOptions* opts) { - if (opts->logLevel) { - m_logView->setLevels(opts->logLevel); + if (opts->patch) { + m_controller->loadPatch(opts->patch); } - if (opts->frameskip) { - m_controller->setFrameskip(opts->frameskip); + if (opts->fname) { + m_controller->loadGame(opts->fname, opts->dirmode); } +} + +void Window::setOptions(GBAOptions* opts) { + m_logView->setLevels(opts->logLevel); + m_controller->setFrameskip(opts->frameskip); if (opts->bios) { m_controller->loadBIOS(opts->bios); - } - - if (opts->patch) { - m_controller->loadPatch(opts->patch); - } - - if (opts->fname) { - m_controller->loadGame(opts->fname, opts->dirmode); } }
M src/platform/qt/Window.hsrc/platform/qt/Window.h

@@ -12,6 +12,7 @@ #include "GDBController.h"

#include "Display.h" #include "LoadSaveState.h" +struct GBAOptions; struct StartupOptions; namespace QGBA {

@@ -32,6 +33,7 @@ GameController* controller() { return m_controller; }

static GBAKey mapKey(int qtKey); + void setOptions(GBAOptions*); void optionsPassed(StartupOptions*); signals:
M src/platform/sdl/gl-main.csrc/platform/sdl/gl-main.c

@@ -69,25 +69,26 @@ struct Configuration config;

ConfigurationInit(&config); GBAConfigLoad(&config); - struct StartupOptions opts = { }; - struct GraphicsOpts graphicsOpts = { }; + struct GBAOptions gbaOpts = {}; + struct StartupOptions opts = {}; + struct GraphicsOpts graphicsOpts = {}; struct SubParser subparser; - GBAConfigMapStartupOpts(&config, PORT, &opts); - GBAConfigMapGraphicsOpts(&config, PORT, &graphicsOpts); + GBAConfigMapGeneralOpts(&config, PORT, &gbaOpts); + GBAConfigMapGraphicsOpts(&config, PORT, &gbaOpts); initParserForGraphics(&subparser, &graphicsOpts); - if (!parseCommandArgs(&opts, argc, argv, &subparser)) { + if (!parseCommandArgs(&opts, &gbaOpts, argc, argv, &subparser)) { usage(argv[0], subparser.usage); freeOptions(&opts); return 1; } - renderer.viewportWidth = graphicsOpts.width; - renderer.viewportHeight = graphicsOpts.height; + renderer.viewportWidth = gbaOpts.width; + renderer.viewportHeight = gbaOpts.height; #if SDL_VERSION_ATLEAST(2, 0, 0) - renderer.events.fullscreen = graphicsOpts.fullscreen; + renderer.events.fullscreen = gbaOpts.fullscreen; renderer.events.windowUpdated = 0; #endif

@@ -108,7 +109,8 @@ };

context.debugger = createDebugger(&opts); - GBAMapOptionsToContext(&opts, &context); + GBAMapOptionsToContext(&gbaOpts, &context); + GBAMapStartupOptionsToContext(&opts, &context); renderer.audio.samples = context.audioBuffers; GBASDLInitAudio(&renderer.audio);