diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2019-11-10 18:49:28 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2019-11-10 18:49:28 -0800 |
commit | 44ec797349713f28314bd329bee940cd55fad411 (patch) | |
tree | 2128c6ba385d4969b30b54259791811df315daf4 /src | |
parent | 4b82076cb381f91c1225eb39efa924e935043d8b (diff) |
settings: s/setting_desc_new/setting_desc_clone/
Slight refactor to make call sites less annoying.
Now takes a (setting_desc_t *) instead of the members as discrete
parameters, and returns an errno on error so callers can simply
propagate error codes out rather than having to get access to errno
defines, check for NULL and return -ENOMEM etc.
It also makes the call sites self documenting by employing designated
initializers in compound literals for the supplied setting_desc_t.
This is in prep for runtime-configurable module settings.
Diffstat (limited to 'src')
-rw-r--r-- | src/drm_fb.c | 50 | ||||
-rw-r--r-- | src/rototiller.c | 44 | ||||
-rw-r--r-- | src/sdl_fb.c | 49 | ||||
-rw-r--r-- | src/settings.c | 55 | ||||
-rw-r--r-- | src/settings.h | 2 |
5 files changed, 108 insertions, 92 deletions
diff --git a/src/drm_fb.c b/src/drm_fb.c index 6599d9d..3d0f6ba 100644 --- a/src/drm_fb.c +++ b/src/drm_fb.c @@ -70,12 +70,18 @@ static const char * connector_type_name(uint32_t type) { static setting_desc_t * dev_desc_generator(void *setup_context) { - return setting_desc_new("DRM Device Path", - "dev", - "/dev/dri/card[0-9]", - "/dev/dri/card0", - NULL, - NULL); + setting_desc_t *desc = NULL; + + (void) setting_desc_clone(&(setting_desc_t){ + .name = "DRM Device Path", + .key = "dev", + .regex = "/dev/dri/card[0-9]", + .preferred = "/dev/dri/card0", + .values = NULL, + .annotations = NULL + }, &desc); + + return desc; } @@ -145,18 +151,20 @@ static setting_desc_t * connector_desc_generator(void *setup_context) { drm_fb_setup_t *s = setup_context; const char **connectors; - setting_desc_t *desc; + setting_desc_t *desc = NULL; connectors = get_connectors(s->dev); if (!connectors) return NULL; - desc = setting_desc_new("DRM Connector", - "connector", - "[a-zA-Z0-9]+", - connectors[0], - connectors, - NULL); + (void) setting_desc_clone(&(setting_desc_t){ + .name = "DRM Connector", + .key = "connector", + .regex = "[a-zA-Z0-9]+", + .preferred = connectors[0], + .values = connectors, + .annotations = NULL + }, &desc); free_strv(connectors); @@ -239,19 +247,21 @@ _out: static setting_desc_t * mode_desc_generator(void *setup_context) { drm_fb_setup_t *s = setup_context; - setting_desc_t *desc; + setting_desc_t *desc = NULL; const char **modes; modes = get_modes(s->dev, s->connector); if (!modes) return NULL; - desc = setting_desc_new("DRM Video Mode", - "mode", - "[0-9]+[xX][0-9]+@[0-9]+", - modes[0], - modes, - NULL); + (void) setting_desc_clone(&(setting_desc_t){ + .name = "DRM Video Mode", + .key = "mode", + .regex = "[0-9]+[xX][0-9]+@[0-9]+", + .preferred = modes[0], + .values = modes, + .annotations = NULL + }, &desc); free_strv(modes); diff --git a/src/rototiller.c b/src/rototiller.c index 92821c2..7203ca2 100644 --- a/src/rototiller.c +++ b/src/rototiller.c @@ -162,17 +162,18 @@ static int setup_video(settings_t *settings, setting_desc_t **next_setting) "sdl", NULL, }; - - desc = setting_desc_new("Video Backend", - NULL, - "[a-z]+", - DEFAULT_VIDEO, - values, - NULL); - if (!desc) - return -ENOMEM; - - *next_setting = desc; + int r; + + r = setting_desc_clone(&(setting_desc_t){ + .name = "Video Backend", + .key = NULL, + .regex = "[a-z]+", + .preferred = DEFAULT_VIDEO, + .values = values, + .annotations = NULL + }, next_setting); + if (r < 0) + return r; return 1; } @@ -207,22 +208,23 @@ static int setup_module(settings_t *settings, setting_desc_t **next_setting) const char *annotations[nelems(modules) + 1] = {}; setting_desc_t *desc; unsigned i; + int r; for (i = 0; i < nelems(modules); i++) { values[i] = modules[i]->name; annotations[i] = modules[i]->description; } - desc = setting_desc_new("Renderer Module", - NULL, - "[a-zA-Z0-9]+", - DEFAULT_MODULE, - values, - annotations); - if (!desc) - return -ENOMEM; - - *next_setting = desc; + r = setting_desc_clone(&(setting_desc_t){ + .name = "Renderer Module", + .key = NULL, + .regex = "[a-zA-Z0-9]+", + .preferred = DEFAULT_MODULE, + .values = values, + .annotations = annotations + }, next_setting); + if (r < 0) + return r; return 1; } diff --git a/src/sdl_fb.c b/src/sdl_fb.c index 80a63dd..f85e9ca 100644 --- a/src/sdl_fb.c +++ b/src/sdl_fb.c @@ -36,18 +36,18 @@ int sdl_fb_setup(const settings_t *settings, setting_desc_t **next_setting) "on", NULL }; - setting_desc_t *desc; - - desc = setting_desc_new("SDL Fullscreen Mode", - "fullscreen", - NULL, - values[0], - values, - NULL); - if (!desc) - return -ENOMEM; - - *next_setting = desc; + int r; + + r = setting_desc_clone(&(setting_desc_t){ + .name = "SDL Fullscreen Mode", + .key = "fullscreen", + .regex = NULL, + .preferred = values[0], + .values = values, + .annotations = NULL + }, next_setting); + if (r < 0) + return r; return 1; } @@ -57,18 +57,19 @@ int sdl_fb_setup(const settings_t *settings, setting_desc_t **next_setting) size = settings_get_value(settings, "size"); if (!size) { - setting_desc_t *desc; - - desc = setting_desc_new("SDL Window size", - "size", - "[1-9][0-9]*[xX][1-9][0-9]*", - "640x480", - NULL, - NULL); - if (!desc) - return -ENOMEM; - - *next_setting = desc; + int r; + + r = setting_desc_clone(&(setting_desc_t){ + .name = "SDL Window size", + .key = "size", + .regex = "[1-9][0-9]*[xX][1-9][0-9]*", + .preferred = "640x480", + .values = NULL, + .annotations = NULL + }, + next_setting); + if (r < 0) + return r; return 1; } diff --git a/src/settings.c b/src/settings.c index d8be774..c24c280 100644 --- a/src/settings.c +++ b/src/settings.c @@ -214,50 +214,53 @@ int settings_apply_desc_generators(const settings_t *settings, const setting_des /* convenience helper for creating a new setting description */ -/* copies of everything supplied are made in newly allocated memory */ -setting_desc_t * setting_desc_new(const char *name, const char *key, const char *regex, const char *preferred, const char *values[], const char *annotations[]) +/* copies of everything supplied are made in newly allocated memory, stored @ res_desc */ +/* returns < 0 on error */ +int setting_desc_clone(const setting_desc_t *desc, setting_desc_t **res_desc) { - setting_desc_t *desc; + setting_desc_t *d; - assert(name); - assert(preferred); /* XXX: require a preferred default? */ - assert(!annotations || values); + assert(desc); + assert(desc->name); + assert(desc->preferred); /* XXX: require a preferred default? */ + assert(!desc->annotations || desc->values); - desc = calloc(1, sizeof(setting_desc_t)); - if (!desc) - return NULL; + d = calloc(1, sizeof(setting_desc_t)); + if (!d) + return -ENOMEM; - desc->name = strdup(name); - if (key) /* This is inappropriately subtle, but when key is NULL, the value will be the key, and there will be no value side at all. */ - desc->key = strdup(key); - if (regex) - desc->regex = strdup(regex); + d->name = strdup(desc->name); + if (desc->key) /* This is inappropriately subtle, but when key is NULL, the value will be the key, and there will be no value side at all. */ + d->key = strdup(desc->key); + if (desc->regex) + d->regex = strdup(desc->regex); - desc->preferred = strdup(preferred); + d->preferred = strdup(desc->preferred); - if (values) { + if (desc->values) { unsigned i; - for (i = 0; values[i]; i++); + for (i = 0; desc->values[i]; i++); - desc->values = calloc(i + 1, sizeof(*values)); + d->values = calloc(i + 1, sizeof(*desc->values)); - if (annotations) - desc->annotations = calloc(i + 1, sizeof(*annotations)); + if (desc->annotations) + d->annotations = calloc(i + 1, sizeof(*desc->annotations)); - for (i = 0; values[i]; i++) { - desc->values[i] = strdup(values[i]); + for (i = 0; desc->values[i]; i++) { + d->values[i] = strdup(desc->values[i]); - if (annotations) { - assert(annotations[i]); - desc->annotations[i] = strdup(annotations[i]); + if (desc->annotations) { + assert(desc->annotations[i]); + d->annotations[i] = strdup(desc->annotations[i]); } } } /* TODO: handle allocation errors above... */ + *res_desc = d; - return desc; + return 0; } diff --git a/src/settings.h b/src/settings.h index fe35b19..6803d09 100644 --- a/src/settings.h +++ b/src/settings.h @@ -30,7 +30,7 @@ int settings_add_value(settings_t *settings, const char *key, const char *value) char * settings_as_arg(const settings_t *settings); int settings_apply_desc_generators(const settings_t *settings, const setting_desc_generator_t generators[], unsigned n_generators, void *setup_context, setting_desc_t **next_setting); -setting_desc_t * setting_desc_new(const char *name, const char *key, const char *regex, const char *preferred, const char *values[], const char *annotations[]); +int setting_desc_clone(const setting_desc_t *desc, setting_desc_t **res_desc); void setting_desc_free(setting_desc_t *desc); #endif |