GBA Config: Begin refactor of separating out command line args from settings

This commit is contained in:
Jeffrey Pfau 2014-11-02 01:49:15 -08:00
parent 5607a13039
commit f36a74759a
12 changed files with 86 additions and 68 deletions

View File

@ -45,7 +45,7 @@ bool GBAConfigLoad(struct Configuration* config) {
return ConfigurationRead(config, BINARY_NAME ".ini"); 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); _lookupCharValue(config, "bios", port, &opts->bios);
_lookupIntValue(config, "logLevel", port, &opts->logLevel); _lookupIntValue(config, "logLevel", port, &opts->logLevel);
_lookupIntValue(config, "frameskip", port, &opts->frameskip); _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); _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, "fullscreen", port, &opts->fullscreen);
_lookupIntValue(config, "width", port, &opts->width); _lookupIntValue(config, "width", port, &opts->width);
_lookupIntValue(config, "height", port, &opts->height); _lookupIntValue(config, "height", port, &opts->height);
} }
void GBAConfigFreeOpts(struct GBAOptions* opts) {
free(opts->bios);
opts->bios = 0;
}

View File

@ -4,12 +4,23 @@
#include "util/common.h" #include "util/common.h"
struct Configuration; 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*); bool GBAConfigLoad(struct Configuration*);
void GBAConfigMapStartupOpts(const struct Configuration*, const char* port, struct StartupOptions*); void GBAConfigMapGeneralOpts(const struct Configuration*, const char* port, struct GBAOptions*);
void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GraphicsOpts*); void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GBAOptions*);
void GBAConfigFreeOpts(struct GBAOptions*);
#endif #endif

View File

@ -217,7 +217,15 @@ static THREAD_ENTRY _GBAThreadRun(void* context) {
return 0; 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) { if (opts->dirmode) {
threadContext->gameDir = VDirOpen(opts->fname); threadContext->gameDir = VDirOpen(opts->fname);
threadContext->stateDir = threadContext->gameDir; threadContext->stateDir = threadContext->gameDir;
@ -228,12 +236,7 @@ void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threa
#endif #endif
} }
threadContext->fname = opts->fname; threadContext->fname = opts->fname;
threadContext->bios = VFileOpen(opts->bios, O_RDONLY);
threadContext->patch = VFileOpen(opts->patch, 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) { bool GBAThreadStart(struct GBAThread* threadContext) {

View File

@ -94,7 +94,8 @@ struct GBAThread {
int rewindBufferWriteOffset; 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 GBAThreadStart(struct GBAThread* threadContext);
bool GBAThreadHasStarted(struct GBAThread* threadContext); bool GBAThreadHasStarted(struct GBAThread* threadContext);

View File

@ -38,9 +38,9 @@ static const struct option _options[] = {
{ 0, 0, 0, 0 } { 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; int ch;
char options[64] = char options[64] =
"b:Dl:p:s:" "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) { while ((ch = getopt_long(argc, argv, options, _options, 0)) != -1) {
switch (ch) { switch (ch) {
case 'b': case 'b':
opts->bios = strdup(optarg); if (gbaOpts->bios) {
free(gbaOpts->bios);
}
gbaOpts->bios = strdup(optarg);
break; break;
case 'D': case 'D':
opts->dirmode = true; opts->dirmode = true;
@ -80,17 +83,17 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv,
break; break;
#endif #endif
case 'l': case 'l':
opts->logLevel = atoi(optarg); gbaOpts->logLevel = atoi(optarg);
break; break;
case 'p': case 'p':
opts->patch = strdup(optarg); opts->patch = strdup(optarg);
break; break;
case 's': case 's':
opts->frameskip = atoi(optarg); gbaOpts->frameskip = atoi(optarg);
break; break;
default: default:
if (subparser) { if (subparser) {
if (!subparser->parse(subparser, ch, optarg)) { if (!subparser->parse(subparser, gbaOpts, ch, optarg)) {
return false; return false;
} }
} }
@ -110,9 +113,6 @@ void freeOptions(struct StartupOptions* opts) {
free(opts->fname); free(opts->fname);
opts->fname = 0; opts->fname = 0;
free(opts->bios);
opts->bios = 0;
free(opts->patch); free(opts->patch);
opts->patch = 0; opts->patch = 0;
} }
@ -125,12 +125,12 @@ void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts)
opts->multiplier = 0; 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); UNUSED(arg);
struct GraphicsOpts* graphicsOpts = parser->opts; struct GraphicsOpts* graphicsOpts = parser->opts;
switch (option) { switch (option) {
case 'f': case 'f':
graphicsOpts->fullscreen = 1; gbaOpts->fullscreen = 1;
return true; return true;
case '1': case '1':
case '2': case '2':
@ -140,8 +140,8 @@ bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) {
return false; return false;
} }
graphicsOpts->multiplier = option - '0'; graphicsOpts->multiplier = option - '0';
graphicsOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier; gbaOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier;
graphicsOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier; gbaOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier;
return true; return true;
default: default:
return false; return false;

View File

@ -3,6 +3,8 @@
#include "util/common.h" #include "util/common.h"
#include "gba-config.h"
enum DebuggerType { enum DebuggerType {
DEBUGGER_NONE = 0, DEBUGGER_NONE = 0,
#ifdef USE_CLI_DEBUGGER #ifdef USE_CLI_DEBUGGER
@ -15,40 +17,26 @@ enum DebuggerType {
}; };
struct StartupOptions { struct StartupOptions {
// Passed only
char* fname; char* fname;
char* patch; char* patch;
bool dirmode; bool dirmode;
enum DebuggerType debuggerType; enum DebuggerType debuggerType;
bool debugAtStart; bool debugAtStart;
// Configurable
char* bios;
int logLevel;
int frameskip;
int rewindBufferCapacity;
int rewindBufferInterval;
}; };
struct SubParser { struct SubParser {
const char* usage; 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; const char* extraOptions;
void* opts; void* opts;
}; };
struct GraphicsOpts { struct GraphicsOpts {
// Passed only
int multiplier; 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 freeOptions(struct StartupOptions* opts);
void usage(const char* arg0, const char* extraOptions); void usage(const char* arg0, const char* extraOptions);

View File

@ -25,7 +25,7 @@ struct PerfOpts {
static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet); static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet);
static void _GBAPerfShutdown(int signal); 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; static struct GBAThread* _thread;
@ -43,8 +43,9 @@ int main(int argc, char** argv) {
.opts = &perfOpts .opts = &perfOpts
}; };
struct GBAOptions gbaOpts = {};
struct StartupOptions opts = {}; struct StartupOptions opts = {};
if (!parseCommandArgs(&opts, argc, argv, &subparser)) { if (!parseCommandArgs(&opts, &gbaOpts, argc, argv, &subparser)) {
usage(argv[0], PERF_USAGE); usage(argv[0], PERF_USAGE);
return 1; return 1;
} }
@ -65,7 +66,8 @@ int main(int argc, char** argv) {
context.debugger = createDebugger(&opts); context.debugger = createDebugger(&opts);
char gameCode[5] = { 0 }; char gameCode[5] = { 0 };
GBAMapOptionsToContext(&opts, &context); GBAMapStartupOptionsToContext(&opts, &context);
GBAMapOptionsToContext(&gbaOpts, &context);
GBAThreadStart(&context); GBAThreadStart(&context);
GBAGetGameCode(context.gba, gameCode); GBAGetGameCode(context.gba, gameCode);
@ -147,7 +149,8 @@ static void _GBAPerfShutdown(int signal) {
pthread_mutex_unlock(&_thread->stateMutex); 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; struct PerfOpts* opts = parser->opts;
errno = 0; errno = 0;
switch (option) { switch (option) {

View File

@ -12,8 +12,11 @@ GBAApp::GBAApp(int& argc, char* argv[])
QApplication::setApplicationName(PROJECT_NAME); QApplication::setApplicationName(PROJECT_NAME);
QApplication::setApplicationVersion(PROJECT_VERSION); 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); m_window.optionsPassed(&m_opts);
} else {
m_window.setOptions(&m_gbaOpts);
} }
m_window.show(); m_window.show();

View File

@ -7,6 +7,7 @@
extern "C" { extern "C" {
#include "platform/commandline.h" #include "platform/commandline.h"
#include "util/configuration.h"
} }
namespace QGBA { namespace QGBA {
@ -27,6 +28,8 @@ private:
Window m_window; Window m_window;
StartupOptions m_opts; StartupOptions m_opts;
GBAOptions m_gbaOpts;
Configuration m_config;
}; };
} }

View File

@ -102,18 +102,6 @@ GBAKey Window::mapKey(int qtKey) {
} }
void Window::optionsPassed(StartupOptions* opts) { 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) { if (opts->patch) {
m_controller->loadPatch(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() { void Window::selectROM() {
QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM")); QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
if (!filename.isEmpty()) { if (!filename.isEmpty()) {

View File

@ -12,6 +12,7 @@ extern "C" {
#include "Display.h" #include "Display.h"
#include "LoadSaveState.h" #include "LoadSaveState.h"
struct GBAOptions;
struct StartupOptions; struct StartupOptions;
namespace QGBA { namespace QGBA {
@ -32,6 +33,7 @@ public:
static GBAKey mapKey(int qtKey); static GBAKey mapKey(int qtKey);
void setOptions(GBAOptions*);
void optionsPassed(StartupOptions*); void optionsPassed(StartupOptions*);
signals: signals:

View File

@ -69,25 +69,26 @@ int main(int argc, char** argv) {
ConfigurationInit(&config); ConfigurationInit(&config);
GBAConfigLoad(&config); GBAConfigLoad(&config);
struct StartupOptions opts = { }; struct GBAOptions gbaOpts = {};
struct GraphicsOpts graphicsOpts = { }; struct StartupOptions opts = {};
struct GraphicsOpts graphicsOpts = {};
struct SubParser subparser; struct SubParser subparser;
GBAConfigMapStartupOpts(&config, PORT, &opts); GBAConfigMapGeneralOpts(&config, PORT, &gbaOpts);
GBAConfigMapGraphicsOpts(&config, PORT, &graphicsOpts); GBAConfigMapGraphicsOpts(&config, PORT, &gbaOpts);
initParserForGraphics(&subparser, &graphicsOpts); initParserForGraphics(&subparser, &graphicsOpts);
if (!parseCommandArgs(&opts, argc, argv, &subparser)) { if (!parseCommandArgs(&opts, &gbaOpts, argc, argv, &subparser)) {
usage(argv[0], subparser.usage); usage(argv[0], subparser.usage);
freeOptions(&opts); freeOptions(&opts);
return 1; return 1;
} }
renderer.viewportWidth = graphicsOpts.width; renderer.viewportWidth = gbaOpts.width;
renderer.viewportHeight = graphicsOpts.height; renderer.viewportHeight = gbaOpts.height;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
renderer.events.fullscreen = graphicsOpts.fullscreen; renderer.events.fullscreen = gbaOpts.fullscreen;
renderer.events.windowUpdated = 0; renderer.events.windowUpdated = 0;
#endif #endif
@ -108,7 +109,8 @@ int main(int argc, char** argv) {
context.debugger = createDebugger(&opts); context.debugger = createDebugger(&opts);
GBAMapOptionsToContext(&opts, &context); GBAMapOptionsToContext(&gbaOpts, &context);
GBAMapStartupOptionsToContext(&opts, &context);
renderer.audio.samples = context.audioBuffers; renderer.audio.samples = context.audioBuffers;
GBASDLInitAudio(&renderer.audio); GBASDLInitAudio(&renderer.audio);