From 7f872664738b45c982f3e95749f2136a0de4c19a Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 22 Apr 2022 12:19:49 -0700 Subject: til: add til_module_randomize_setup() from rtv This commit pulls the setup randomizer out of rtv into libtil proper, so other modules may make use of it. Other than adding an assert no functional changes occurred. It may make sense to split this into two functions; one which takes a til_module_t as-is, and a lower-level bare setup function callback based function that doesn't know about til_module_t the former would call into. That way generic setup randomization can occur (the same setup machinery is used in video contexts for example) without necessarily having a til_module_t on hand. --- src/modules/rtv/rtv.c | 43 +------------------------------------------ src/til.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/til.h | 1 + 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/modules/rtv/rtv.c b/src/modules/rtv/rtv.c index a236f96..19dcdca 100644 --- a/src/modules/rtv/rtv.c +++ b/src/modules/rtv/rtv.c @@ -110,47 +110,6 @@ static void randomize_channels(rtv_context_t *ctxt) } -static char * randomize_module_setup(const til_module_t *module, void **res_setup) -{ - til_settings_t *settings; - til_setting_t *setting; - const til_setting_desc_t *desc; - char *arg; - - if (!module->setup) - return NULL; - - settings = til_settings_new(NULL); - if (!settings) - return NULL; - - while (module->setup(settings, &setting, &desc, res_setup) > 0) { - if (desc->random) { - char *value; - - value = desc->random(); - til_settings_add_value(settings, desc->key, value, desc); - free(value); - } else if (desc->values) { - int n; - - for (n = 0; desc->values[n]; n++); - - n = rand() % n; - - til_settings_add_value(settings, desc->key, desc->values[n], desc); - } else { - til_settings_add_value(settings, desc->key, desc->preferred, desc); - } - } - - arg = til_settings_as_arg(settings); - til_settings_free(settings); - - return arg; -} - - static void setup_next_channel(rtv_context_t *ctxt, unsigned ticks) { time_t now = time(NULL); @@ -199,7 +158,7 @@ static void setup_next_channel(rtv_context_t *ctxt, unsigned ticks) char *settings; txt_t *caption; - settings = randomize_module_setup(ctxt->channel->module, &ctxt->channel->module_setup); + settings = til_module_randomize_setup(ctxt->channel->module, &ctxt->channel->module_setup); caption = txt_newf("Title: %s%s%s\nDescription: %s%s%s", ctxt->channel->module->name, ctxt->channel->module->author ? "\nAuthor: " : "", diff --git a/src/til.c b/src/til.c index 50df146..531c967 100644 --- a/src/til.c +++ b/src/til.c @@ -234,3 +234,47 @@ int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, cons return 0; } + + +/* originally taken from rtv, this randomizes a module's setup @res_setup, returning args form as well */ +char * til_module_randomize_setup(const til_module_t *module, void **res_setup) +{ + til_settings_t *settings; + til_setting_t *setting; + const til_setting_desc_t *desc; + char *arg; + + assert(module); + + if (!module->setup) + return NULL; + + settings = til_settings_new(NULL); + if (!settings) + return NULL; + + while (module->setup(settings, &setting, &desc, res_setup) > 0) { + if (desc->random) { + char *value; + + value = desc->random(); + til_settings_add_value(settings, desc->key, value, desc); + free(value); + } else if (desc->values) { + int n; + + for (n = 0; desc->values[n]; n++); + + n = rand() % n; + + til_settings_add_value(settings, desc->key, desc->values[n], desc); + } else { + til_settings_add_value(settings, desc->key, desc->preferred, desc); + } + } + + arg = til_settings_as_arg(settings); + til_settings_free(settings); + + return arg; +} diff --git a/src/til.h b/src/til.h index 29bbb2c..836907c 100644 --- a/src/til.h +++ b/src/til.h @@ -33,5 +33,6 @@ void til_module_render(const til_module_t *module, void *context, unsigned ticks int til_module_create_context(const til_module_t *module, unsigned ticks, void *setup, void **res_context); void * til_module_destroy_context(const til_module_t *module, void *context); int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, void **res_setup); +char * til_module_randomize_setup(const til_module_t *module, void **res_setup); #endif -- cgit v1.2.3