diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 16 | ||||
| -rw-r--r-- | src/config.c | 225 | ||||
| -rw-r--r-- | src/config.h | 28 | ||||
| -rw-r--r-- | src/picow.c | 63 |
5 files changed, 272 insertions, 62 deletions
@@ -1,4 +1,2 @@ build/ -carts/*.p8.dir/ -carts/*.p8.png config.mk @@ -36,10 +36,8 @@ WAMR_CFLAGS := $(CFLAGS) -Wno-unused-parameter \ WAMR_SRC := $(shell find lib/wamr -name '*.c') WAMR_OBJ := $(patsubst %.c,$(OUT)/%.o,$(WAMR_SRC)) -CARTS := $(patsubst %,%.png,$(wildcard carts/*.p8)) - .PHONY: all -all: $(TARGET)/picow $(CARTS) ; +all: $(TARGET)/picow ; $(TARGET)/picow: $(PICOW_OBJ) $(TARGET)/libwamr.a @ echo "CC\t$@" @@ -80,10 +78,10 @@ $(SYSTEM)/%.html $(SYSTEM)/%.js $(SYSTEM)/%.wasm: carts/%.p8 @ mkdir -p $(dir $@) @ $(PICO8) $< -export "-w $(SYSTEM)/$*.html" >/dev/null -$(SYSTEM)/%.p8.png: carts/%.p8.png - @ echo "CP\t$@" +$(SYSTEM)/%.p8.png: carts/%.p8 + @ echo "PICO8\t$@" @ mkdir -p $(dir $@) - @ cp $< $@ + @ $(PICO8) $< -export $@ >/dev/null $(SYSTEM)/stub/%.wasm: stub/%.wat @ echo "WAT2WASM\t$@" @@ -95,10 +93,6 @@ $(TOOLS)/%: tools/%.c @ mkdir -p $(dir $@) @ $(HOSTCC) $(CFLAGS) $< -o $@ -carts/%.p8.png: carts/%.p8 - @ echo "PICO8\t$@" - @ $(PICO8) $< -export $@ >/dev/null - .PHONY: build/compile_commands.json build/compile_commands.json: $(TOOLS)/ccj @ rm -rf $(OUT) @@ -111,6 +105,6 @@ build/compile_commands.json: $(TOOLS)/ccj .PHONY: clean clean: - rm -rf $(CARTS) build/ + rm -rf build/ -include $(PICOW_OBJ:.o=.d) $(WAMR_OBJ:.o=.d) diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..56d6bac --- /dev/null +++ b/src/config.c @@ -0,0 +1,225 @@ +#include <stdio.h> + +// We don't have `getopt` on all platforms (notably, it is not available on +// Windows), so detect whether or not it is there and avoid command line +// parsing if it is not available. +#if !defined(PW_NO_CLI) && (defined(__unix__) || defined(__APPLE__)) +#include <unistd.h> +#endif + +#include <SDL3/SDL_filesystem.h> + +#include "config.h" +#include "path.h" + +static void usage(char const *name) { + printf("Usage: %s [-c <carts>] [-S <mode>] [-v[v[v]]]\n" + " -c <carts> Carts directory. Defaults to 'carts'.\n" + " -S <mode> Scale mode: 'pp' (default), 'fill' or 'stretch'.\n" + " -v[v[v]] Increasingly verbose logging.\n", + pw_basename(name)); +} + +static void print_err(char const *name, char const *err) { + if (err) { + fprintf(stderr, "%s: %s\n", pw_basename(name), err); + } +} + +struct named_value { + char const *k; + int v; +}; + +static bool parse_named_value(struct named_value *values, size_t size, + char const *s, int *v) { + for (size_t i = 0; i < size; ++i) { + if (!SDL_strcmp(s, values[i].k)) { + *v = values[i].v; + return true; + } + } + return false; +} + +static bool parse_scale_mode(char const *s, enum pw_scale_mode *m) { + struct named_value values[] = { + {"pp", PW_SCALE_MODE_PIXEL_PERFECT}, + {"fill", PW_SCALE_MODE_FILL}, + {"stretch", PW_SCALE_MODE_STRETCH}, + }; + int v; + if (!parse_named_value(values, SDL_arraysize(values), s, &v)) { + SDL_SetError("invalid scaling mode '%s'", s); + return false; + } + + *m = (enum pw_scale_mode)v; + return true; +} + +static bool parse_log_priority(char const *s, SDL_LogPriority *l) { + struct named_value values[] = { + {"trace", SDL_LOG_PRIORITY_TRACE}, + {"verbose", SDL_LOG_PRIORITY_VERBOSE}, + {"debug", SDL_LOG_PRIORITY_DEBUG}, + {"info", SDL_LOG_PRIORITY_INFO}, + {"warn", SDL_LOG_PRIORITY_WARN}, + {"error", SDL_LOG_PRIORITY_ERROR}, + {"critical", SDL_LOG_PRIORITY_CRITICAL}, + }; + int v; + if (!parse_named_value(values, SDL_arraysize(values), s, &v)) { + SDL_SetError("invalid verbosity '%s'", s); + return false; + } + + *l = (SDL_LogPriority)v; + return true; +} + +static void parse_config_ini(struct pw_config *config) { + char ini[1024]; + if (!pw_pathjoin(ini, sizeof(ini), config->carts, "config.ini", NULL)) { + goto error; + } + + size_t len; + char *data = SDL_LoadFile(ini, &len); + if (!data) { + goto error; + } + + char *pos = data; + char line[1024], key[32], value[992]; + int n; + while (pos < data + len) { + if (SDL_sscanf(pos++, "%1023[^\n]%n", line, &n) != 1) { + // Skip empty lines. + continue; + } + pos += n; + + if (SDL_sscanf(line, " %31[^= \t] = %991s", key, value) != 2) { + SDL_LogWarn(0, "invalid configuation line '%s'", line); + continue; + } + + if (!SDL_strcmp(key, "carts")) { + if (config->carts) { + SDL_free(config->carts); + } + config->carts = SDL_strdup(value); + } else if (!SDL_strcmp(key, "mode")) { + if (!parse_scale_mode(value, &config->mode)) { + SDL_LogWarn(0, "%s", SDL_GetError()); + SDL_ClearError(); + } + } else if (!SDL_strcmp(key, "verbosity")) { + if (!parse_log_priority(value, &config->logp)) { + SDL_LogWarn(0, "%s", SDL_GetError()); + SDL_ClearError(); + } + } + } + + SDL_free(data); + return; + +error: + // We are very permissive with configuration loading, where we just log a + // warning and continue without propogating an error. + SDL_LogWarn(0, "failed to read config.ini: %s", SDL_GetError()); + SDL_ClearError(); + return; +} + +struct pw_config *pw_config_load(int argc, char **argv) { + if (!argv) { + SDL_InvalidParamError("argv"); + return NULL; + } + + char const *name = "picow"; + if (argc) { + name = argv[0]; + } + + struct pw_config *config = SDL_malloc(sizeof(*config)); + if (!config) { + return NULL; + } + + config->carts = SDL_GetPrefPath("PICO-w", "picow"); + if (!config->carts) { + goto cleanup; + } + config->mode = PW_SCALE_MODE_PIXEL_PERFECT; + config->logp = SDL_GetLogPriority(0); + + // Read default values from the `config.ini` file before parsing command line + // arguments, as they take priority. + parse_config_ini(config); + +#if defined(_POSIX_VERSION) + int c; + while ((c = getopt(argc, argv, ":hc:S:v")) != -1) { + switch (c) { + case 'h': + usage(name); + SDL_ClearError(); + goto cleanup; + case 'c': + if (config->carts) { + SDL_free(config->carts); + } + config->carts = SDL_strdup(optarg); + break; + case 'S': + if (!parse_scale_mode(optarg, &config->mode)) { + goto cleanup; + } + break; + case 'v': + config->logp--; + if (config->logp == SDL_LOG_PRIORITY_INVALID) { + SDL_SetError("specified -v too many times"); + goto cleanup; + } + break; + case ':': + SDL_SetError("missing value for -%c", optopt); + goto cleanup; + case '?': + default: + SDL_SetError("unknown option -%c", optopt); + goto cleanup; + } + } +#else + (void)usage; +#endif + + if (!config->carts) { + goto cleanup; + } + + return config; + +cleanup: + print_err(name, SDL_GetError()); + pw_config_free(config); + return NULL; +} + +void pw_config_free(struct pw_config *config) { + if (!config) { + SDL_InvalidParamError("config"); + return; + } + + if (config->carts) { + SDL_free(config->carts); + } + SDL_free(config); +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..89e8e82 --- /dev/null +++ b/src/config.h @@ -0,0 +1,28 @@ +#ifndef PW_CONFIG_H +#define PW_CONFIG_H + +#include <SDL3/SDL_init.h> +#include <SDL3/SDL_log.h> + +#include "scale.h" + +/** + * The configuration for PICO-w. + */ +struct pw_config { + char *carts; + enum pw_scale_mode mode; + SDL_LogPriority logp; +}; + +/** + * Loads the configuration, reading overrides from the command line. + */ +struct pw_config *pw_config_load(int argc, char **argv); + +/** + * Frees the configuration. + */ +void pw_config_free(struct pw_config *config); + +#endif diff --git a/src/picow.c b/src/picow.c index 9b89447..79cd0ed 100644 --- a/src/picow.c +++ b/src/picow.c @@ -1,24 +1,12 @@ -#include <stdio.h> -#include <unistd.h> - #define SDL_MAIN_USE_CALLBACKS #include <SDL3/SDL.h> #include <SDL3/SDL_main.h> #include "cart.h" +#include "config.h" #include "game.h" #include "menu.h" -#include "path.h" #include "player.h" -#include "scale.h" - -static void usage(char const *name) { - printf("Usage: %s [-c <carts>] [-S <mode>] [-v[v[v]]]\n" - " -c <carts> Carts directory. Defaults to 'carts'.\n" - " -S <mode> Scale mode: 'pp' (default), 'fill' or 'stretch'.\n" - " -v[v[v]] Increasingly verbose logging.\n", - pw_basename(name)); -} static SDL_AppResult failf(char const *fmt, ...) { va_list ap; @@ -53,6 +41,7 @@ enum pw_scene { struct pw_appstate { SDL_Window *window; SDL_Renderer *renderer; + struct pw_config *config; struct pw_players *players; struct pw_menu *menu; struct pw_game *game; @@ -70,43 +59,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return failf("failed to initialize SDL"); } - char *carts = "carts"; - enum pw_scale_mode mode = PW_SCALE_MODE_PIXEL_PERFECT; - SDL_LogPriority logp = SDL_GetLogPriority(0); - - int c; - while ((c = getopt(argc, argv, "hc:S:v")) != -1) { - switch (c) { - case 'h': - usage(argv[0]); + state->config = pw_config_load(argc, argv); + if (!state->config) { + if (SDL_GetError()) { + return failf("failed to load configuration"); + } else { return SDL_APP_SUCCESS; - case 'c': - carts = optarg; - break; - case 'S': - if (!SDL_strcmp(optarg, "pp")) { - mode = PW_SCALE_MODE_PIXEL_PERFECT; - } else if (!SDL_strcmp(optarg, "fill")) { - mode = PW_SCALE_MODE_FILL; - } else if (!SDL_strcmp(optarg, "stretch")) { - mode = PW_SCALE_MODE_STRETCH; - } else { - fprintf(stderr, "ERROR: invalid scaling mode '%s'\n", optarg); - return SDL_APP_FAILURE; - } - break; - case 'v': - logp--; - if (logp == SDL_LOG_PRIORITY_INVALID) { - fprintf(stderr, "ERROR: specified -v too many times\n"); - return SDL_APP_FAILURE; - } - break; - default: - return SDL_APP_FAILURE; } } - SDL_SetLogPriority(0, logp); + + SDL_SetLogPriority(0, state->config->logp); + SDL_LogDebug(0, "using carts directory '%s'", state->config->carts); if (!SDL_CreateWindowAndRenderer("PICO-w", 512, 512, SDL_WINDOW_HIGH_PIXEL_DENSITY | @@ -124,12 +87,13 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return failf("failed to initialize player controllers"); } - state->menu = pw_menu_init(state->renderer, carts, mode); + state->menu = + pw_menu_init(state->renderer, state->config->carts, state->config->mode); if (!state->menu) { return failf("failed to initialize cart selection state"); } - state->game = pw_game_init(state->renderer, mode); + state->game = pw_game_init(state->renderer, state->config->mode); if (!state->game) { return failf("failed to initialize PICO-8 game state"); } @@ -201,6 +165,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { goto bye; } + pw_config_free(state->config); pw_players_free(state->players); pw_menu_free(state->menu); pw_game_free(state->game); |
