From f36a74759a291b63520eaabf853025ffbd221c80 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 2 Nov 2014 01:49:15 -0800 Subject: [PATCH] GBA Config: Begin refactor of separating out command line args from settings --- src/gba/gba-config.c | 9 +++++++-- src/gba/gba-config.h | 19 +++++++++++++++---- src/gba/gba-thread.c | 15 +++++++++------ src/gba/gba-thread.h | 3 ++- src/platform/commandline.c | 26 +++++++++++++------------- src/platform/commandline.h | 20 ++++---------------- src/platform/perf-main.c | 11 +++++++---- src/platform/qt/GBAApp.cpp | 5 ++++- src/platform/qt/GBAApp.h | 3 +++ src/platform/qt/Window.cpp | 21 +++++++++------------ src/platform/qt/Window.h | 2 ++ src/platform/sdl/gl-main.c | 20 +++++++++++--------- 12 files changed, 86 insertions(+), 68 deletions(-) diff --git a/src/gba/gba-config.c b/src/gba/gba-config.c index af3197797..be8338c31 100644 --- a/src/gba/gba-config.c +++ b/src/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 @@ void GBAConfigMapStartupOpts(const struct Configuration* config, const char* por _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; +} diff --git a/src/gba/gba-config.h b/src/gba/gba-config.h index 0edf67826..8f89e6b87 100644 --- a/src/gba/gba-config.h +++ b/src/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 diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 5e1b0cf16..82e853276 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -217,7 +217,15 @@ static THREAD_ENTRY _GBAThreadRun(void* context) { 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 @@ void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threa #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) { diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h index 142bc7a5e..cee2c5afb 100644 --- a/src/gba/gba-thread.h +++ b/src/gba/gba-thread.h @@ -94,7 +94,8 @@ struct GBAThread { 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); diff --git a/src/platform/commandline.c b/src/platform/commandline.c index 1e582eb93..152b113af 100644 --- a/src/platform/commandline.c +++ b/src/platform/commandline.c @@ -38,9 +38,9 @@ static const struct option _options[] = { { 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 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, 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 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, 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 @@ void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) 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 @@ bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) { 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; diff --git a/src/platform/commandline.h b/src/platform/commandline.h index ed86139ce..97cf2875d 100644 --- a/src/platform/commandline.h +++ b/src/platform/commandline.h @@ -3,6 +3,8 @@ #include "util/common.h" +#include "gba-config.h" + enum DebuggerType { DEBUGGER_NONE = 0, #ifdef USE_CLI_DEBUGGER @@ -15,40 +17,26 @@ enum DebuggerType { }; 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); diff --git a/src/platform/perf-main.c b/src/platform/perf-main.c index accc85e70..2a8015f9d 100644 --- a/src/platform/perf-main.c +++ b/src/platform/perf-main.c @@ -25,7 +25,7 @@ struct PerfOpts { 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 @@ int main(int argc, char** argv) { .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 @@ int main(int argc, char** argv) { 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 @@ static void _GBAPerfShutdown(int signal) { 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) { diff --git a/src/platform/qt/GBAApp.cpp b/src/platform/qt/GBAApp.cpp index daf58e59a..8862ebe0c 100644 --- a/src/platform/qt/GBAApp.cpp +++ b/src/platform/qt/GBAApp.cpp @@ -12,8 +12,11 @@ GBAApp::GBAApp(int& argc, char* argv[]) 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(); diff --git a/src/platform/qt/GBAApp.h b/src/platform/qt/GBAApp.h index a101b0ed5..f9dbd604a 100644 --- a/src/platform/qt/GBAApp.h +++ b/src/platform/qt/GBAApp.h @@ -7,6 +7,7 @@ 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; }; } diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index e27131f31..ed5730dc6 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -102,18 +102,6 @@ GBAKey Window::mapKey(int qtKey) { } void Window::optionsPassed(StartupOptions* opts) { - if (opts->logLevel) { - m_logView->setLevels(opts->logLevel); - } - - if (opts->frameskip) { - m_controller->setFrameskip(opts->frameskip); - } - - if (opts->bios) { - m_controller->loadBIOS(opts->bios); - } - if (opts->patch) { m_controller->loadPatch(opts->patch); } @@ -123,6 +111,15 @@ void Window::optionsPassed(StartupOptions* opts) { } } +void Window::setOptions(GBAOptions* opts) { + m_logView->setLevels(opts->logLevel); + m_controller->setFrameskip(opts->frameskip); + + if (opts->bios) { + m_controller->loadBIOS(opts->bios); + } +} + void Window::selectROM() { QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM")); if (!filename.isEmpty()) { diff --git a/src/platform/qt/Window.h b/src/platform/qt/Window.h index 2582f5631..731f22630 100644 --- a/src/platform/qt/Window.h +++ b/src/platform/qt/Window.h @@ -12,6 +12,7 @@ extern "C" { #include "Display.h" #include "LoadSaveState.h" +struct GBAOptions; struct StartupOptions; namespace QGBA { @@ -32,6 +33,7 @@ public: static GBAKey mapKey(int qtKey); + void setOptions(GBAOptions*); void optionsPassed(StartupOptions*); signals: diff --git a/src/platform/sdl/gl-main.c b/src/platform/sdl/gl-main.c index dc198315f..ad82b9c4e 100644 --- a/src/platform/sdl/gl-main.c +++ b/src/platform/sdl/gl-main.c @@ -69,25 +69,26 @@ int main(int argc, char** argv) { 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 @@ int main(int argc, char** argv) { context.debugger = createDebugger(&opts); - GBAMapOptionsToContext(&opts, &context); + GBAMapOptionsToContext(&gbaOpts, &context); + GBAMapStartupOptionsToContext(&opts, &context); renderer.audio.samples = context.audioBuffers; GBASDLInitAudio(&renderer.audio);