2013-04-04 11:58:30 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2016-01-10 03:06:50 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2013-04-04 11:58:30 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-21 00:45:18 +00:00
|
|
|
#include <string.h>
|
2015-09-05 17:24:21 +00:00
|
|
|
|
2014-10-22 01:35:04 +00:00
|
|
|
#include <file/config_file.h>
|
2016-03-20 13:53:54 +00:00
|
|
|
#include <lists/dir_list.h>
|
|
|
|
#include <lists/string_list.h>
|
2014-10-21 05:58:58 +00:00
|
|
|
#include <compat/posix_string.h>
|
|
|
|
#include <compat/strl.h>
|
2015-01-07 19:42:36 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
2016-01-20 03:07:24 +00:00
|
|
|
#include <string/stdstring.h>
|
2013-04-04 11:58:30 +00:00
|
|
|
|
2016-05-10 17:03:53 +00:00
|
|
|
#include <libretro.h>
|
2016-03-20 03:29:40 +00:00
|
|
|
|
2016-05-09 23:25:47 +00:00
|
|
|
#include "core_option_manager.h"
|
2015-09-05 17:24:21 +00:00
|
|
|
|
2013-04-04 11:58:30 +00:00
|
|
|
struct core_option
|
|
|
|
{
|
|
|
|
char *desc;
|
|
|
|
char *key;
|
|
|
|
struct string_list *vals;
|
|
|
|
size_t index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct core_option_manager
|
|
|
|
{
|
|
|
|
config_file_t *conf;
|
2015-01-07 19:42:36 +00:00
|
|
|
char conf_path[PATH_MAX_LENGTH];
|
2013-04-04 11:58:30 +00:00
|
|
|
|
|
|
|
struct core_option *opts;
|
|
|
|
size_t size;
|
|
|
|
bool updated;
|
|
|
|
};
|
|
|
|
|
2016-05-09 23:21:55 +00:00
|
|
|
static bool core_option_manager_parse_variable(core_option_manager_t *opt, size_t idx,
|
|
|
|
const struct retro_variable *var)
|
|
|
|
{
|
|
|
|
const char *val_start = NULL;
|
|
|
|
char *value = NULL;
|
|
|
|
char *desc_end = NULL;
|
|
|
|
char *config_val = NULL;
|
|
|
|
struct core_option *option = (struct core_option*)&opt->opts[idx];
|
|
|
|
|
|
|
|
if (!option)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
option->key = strdup(var->key);
|
|
|
|
value = strdup(var->value);
|
|
|
|
desc_end = strstr(value, "; ");
|
|
|
|
|
|
|
|
if (!desc_end)
|
|
|
|
{
|
|
|
|
free(value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*desc_end = '\0';
|
|
|
|
option->desc = strdup(value);
|
|
|
|
|
|
|
|
val_start = desc_end + 2;
|
|
|
|
option->vals = string_split(val_start, "|");
|
|
|
|
|
|
|
|
if (!option->vals)
|
|
|
|
{
|
|
|
|
free(value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config_get_string(opt->conf, option->key, &config_val))
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < option->vals->size; i++)
|
|
|
|
{
|
|
|
|
if (string_is_equal(option->vals->elems[i].data, config_val))
|
|
|
|
{
|
|
|
|
option->index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(config_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(value);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:10:17 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_free:
|
2015-01-16 01:10:17 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
*
|
|
|
|
* Frees core option manager handle.
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_free(core_option_manager_t *opt)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2015-01-15 21:05:07 +00:00
|
|
|
|
2013-04-04 11:58:30 +00:00
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < opt->size; i++)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2016-02-29 00:22:29 +00:00
|
|
|
if (opt->opts[i].desc)
|
|
|
|
free(opt->opts[i].desc);
|
|
|
|
if (opt->opts[i].key)
|
|
|
|
free(opt->opts[i].key);
|
|
|
|
|
|
|
|
if (opt->opts[i].vals)
|
|
|
|
string_list_free(opt->opts[i].vals);
|
|
|
|
|
|
|
|
opt->opts[i].desc = NULL;
|
|
|
|
opt->opts[i].key = NULL;
|
|
|
|
opt->opts[i].vals = NULL;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opt->conf)
|
|
|
|
config_file_free(opt->conf);
|
|
|
|
free(opt->opts);
|
|
|
|
free(opt);
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_get(core_option_manager_t *opt, void *data)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2016-03-20 03:29:40 +00:00
|
|
|
struct retro_variable *var = (struct retro_variable*)data;
|
2015-01-15 21:05:07 +00:00
|
|
|
|
2015-06-25 13:38:57 +00:00
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
2013-04-04 16:10:34 +00:00
|
|
|
opt->updated = false;
|
2015-01-15 21:05:07 +00:00
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < opt->size; i++)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2016-02-29 00:22:29 +00:00
|
|
|
if (string_is_empty(opt->opts[i].key))
|
|
|
|
continue;
|
|
|
|
|
2016-01-20 03:07:24 +00:00
|
|
|
if (string_is_equal(opt->opts[i].key, var->key))
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2016-05-09 23:21:55 +00:00
|
|
|
var->value = core_option_manager_get_val(opt, i);
|
2013-04-04 11:58:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var->value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 01:10:17 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_new:
|
2015-01-16 01:10:17 +00:00
|
|
|
* @conf_path : Filesystem path to write core option config file to.
|
|
|
|
* @vars : Pointer to variable array handle.
|
|
|
|
*
|
|
|
|
* Creates and initializes a core manager handle.
|
|
|
|
*
|
|
|
|
* Returns: handle to new core manager handle, otherwise NULL.
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
core_option_manager_t *core_option_manager_new(const char *conf_path,
|
2016-03-20 03:29:40 +00:00
|
|
|
const void *data)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
const struct retro_variable *var;
|
2016-03-20 03:29:40 +00:00
|
|
|
const struct retro_variable *vars = (const struct retro_variable*)data;
|
|
|
|
size_t size = 0;
|
|
|
|
core_option_manager_t *opt = (core_option_manager_t*)
|
2014-09-02 03:10:54 +00:00
|
|
|
calloc(1, sizeof(*opt));
|
2015-06-12 21:16:42 +00:00
|
|
|
|
2013-04-04 11:58:30 +00:00
|
|
|
if (!opt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (*conf_path)
|
|
|
|
opt->conf = config_file_new(conf_path);
|
|
|
|
if (!opt->conf)
|
|
|
|
opt->conf = config_file_new(NULL);
|
|
|
|
|
|
|
|
strlcpy(opt->conf_path, conf_path, sizeof(opt->conf_path));
|
|
|
|
|
|
|
|
if (!opt->conf)
|
|
|
|
goto error;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (var = vars; var->key && var->value; var++)
|
2013-04-04 11:58:30 +00:00
|
|
|
size++;
|
|
|
|
|
|
|
|
opt->opts = (struct core_option*)calloc(size, sizeof(*opt->opts));
|
|
|
|
if (!opt->opts)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
opt->size = size;
|
2015-06-12 21:16:42 +00:00
|
|
|
size = 0;
|
2013-04-04 11:58:30 +00:00
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (var = vars; var->key && var->value; size++, var++)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2016-05-09 23:21:55 +00:00
|
|
|
if (!core_option_manager_parse_variable(opt, size, var))
|
2013-04-04 11:58:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt;
|
|
|
|
|
|
|
|
error:
|
2016-05-09 23:21:55 +00:00
|
|
|
core_option_manager_free(opt);
|
2013-04-04 11:58:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:10:17 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_updated:
|
2015-01-16 01:10:17 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
*
|
|
|
|
* Has a core option been updated?
|
|
|
|
*
|
|
|
|
* Returns: true (1) if a core option has been updated,
|
|
|
|
* otherwise false (0).
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
bool core_option_manager_updated(core_option_manager_t *opt)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-01-09 01:06:41 +00:00
|
|
|
if (!opt)
|
|
|
|
return false;
|
|
|
|
return opt->updated;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 01:10:17 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_flush:
|
2015-01-16 01:10:17 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
*
|
|
|
|
* Writes core option key-pair values to file.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if core option values could be
|
|
|
|
* successfully saved to disk, otherwise false (0).
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
bool core_option_manager_flush(core_option_manager_t *opt)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2015-01-16 01:10:17 +00:00
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < opt->size; i++)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2014-08-02 14:15:40 +00:00
|
|
|
struct core_option *option = (struct core_option*)&opt->opts[i];
|
2015-01-16 01:10:17 +00:00
|
|
|
|
2015-01-08 02:58:14 +00:00
|
|
|
if (option)
|
2016-05-09 23:21:55 +00:00
|
|
|
config_set_string(opt->conf, option->key,
|
|
|
|
core_option_manager_get_val(opt, i));
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
2015-01-16 01:10:17 +00:00
|
|
|
|
|
|
|
return config_file_write(opt->conf, opt->conf_path);
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 03:09:39 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_flush_game_specific:
|
2015-11-16 03:09:39 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
* @path : path for the core options file
|
|
|
|
*
|
|
|
|
* Writes core option key-pair values to a custom file.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if core option values could be
|
|
|
|
* successfully saved to disk, otherwise false (0).
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
bool core_option_manager_flush_game_specific(core_option_manager_t *opt, char* path)
|
2015-11-16 03:09:39 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < opt->size; i++)
|
|
|
|
{
|
|
|
|
struct core_option *option = (struct core_option*)&opt->opts[i];
|
|
|
|
|
|
|
|
if (option)
|
2016-05-09 23:21:55 +00:00
|
|
|
config_set_string(opt->conf, option->key,
|
|
|
|
core_option_manager_get_val(opt, i));
|
2015-11-16 03:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config_file_write(opt->conf, path);
|
|
|
|
}
|
|
|
|
|
2015-01-09 21:01:19 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_size:
|
2015-01-09 21:01:19 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
*
|
|
|
|
* Gets total number of options.
|
|
|
|
*
|
|
|
|
* Returns: Total number of options.
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
size_t core_option_manager_size(core_option_manager_t *opt)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-01-08 02:58:14 +00:00
|
|
|
if (!opt)
|
|
|
|
return 0;
|
|
|
|
return opt->size;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 21:01:19 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_get_desc:
|
2015-01-09 21:01:19 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
* @index : index identifier of the option
|
|
|
|
*
|
|
|
|
* Gets description for an option.
|
|
|
|
*
|
|
|
|
* Returns: Description for an option.
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
const char *core_option_manager_get_desc(core_option_manager_t *opt, size_t idx)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-01-08 02:58:14 +00:00
|
|
|
if (!opt)
|
|
|
|
return NULL;
|
|
|
|
return opt->opts[idx].desc;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 21:01:19 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_get_val:
|
2015-01-09 21:01:19 +00:00
|
|
|
* @opt : options manager handle
|
|
|
|
* @index : index identifier of the option
|
|
|
|
*
|
|
|
|
* Gets value for an option.
|
|
|
|
*
|
|
|
|
* Returns: Value for an option.
|
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
const char *core_option_manager_get_val(core_option_manager_t *opt, size_t idx)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-06-25 13:18:51 +00:00
|
|
|
struct core_option *option = NULL;
|
|
|
|
if (!opt)
|
|
|
|
return NULL;
|
|
|
|
option = (struct core_option*)&opt->opts[idx];
|
2015-01-08 02:58:14 +00:00
|
|
|
if (!option)
|
|
|
|
return NULL;
|
|
|
|
return option->vals->elems[option->index].data;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_set_val(core_option_manager_t *opt,
|
2014-10-20 17:13:16 +00:00
|
|
|
size_t idx, size_t val_idx)
|
2013-11-22 14:30:02 +00:00
|
|
|
{
|
2015-06-25 13:38:57 +00:00
|
|
|
struct core_option *option= NULL;
|
|
|
|
|
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
option = (struct core_option*)&opt->opts[idx];
|
2014-10-08 16:37:09 +00:00
|
|
|
|
|
|
|
if (!option)
|
|
|
|
return;
|
|
|
|
|
2014-10-20 17:13:16 +00:00
|
|
|
option->index = val_idx % option->vals->size;
|
2015-01-15 21:05:07 +00:00
|
|
|
opt->updated = true;
|
2013-11-22 14:30:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:58:14 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_next:
|
2015-01-08 02:58:14 +00:00
|
|
|
* @opt : pointer to core option manager object.
|
|
|
|
* @idx : index of core option to be reset to defaults.
|
|
|
|
*
|
|
|
|
* Get next value for core option specified by @idx.
|
2015-01-09 21:01:19 +00:00
|
|
|
* Options wrap around.
|
2015-01-08 02:58:14 +00:00
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_next(core_option_manager_t *opt, size_t idx)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-06-25 13:38:57 +00:00
|
|
|
struct core_option *option = NULL;
|
|
|
|
|
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
option = (struct core_option*)&opt->opts[idx];
|
2014-10-08 16:37:09 +00:00
|
|
|
|
|
|
|
if (!option)
|
|
|
|
return;
|
|
|
|
|
2013-04-04 11:58:30 +00:00
|
|
|
option->index = (option->index + 1) % option->vals->size;
|
2015-01-15 21:05:07 +00:00
|
|
|
opt->updated = true;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:58:14 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_prev:
|
2015-01-08 02:58:14 +00:00
|
|
|
* @opt : pointer to core option manager object.
|
|
|
|
* @idx : index of core option to be reset to defaults.
|
2015-01-09 21:01:19 +00:00
|
|
|
* Options wrap around.
|
2015-01-08 02:58:14 +00:00
|
|
|
*
|
|
|
|
* Get previous value for core option specified by @idx.
|
2015-01-09 21:01:19 +00:00
|
|
|
* Options wrap around.
|
2015-01-08 02:58:14 +00:00
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_prev(core_option_manager_t *opt, size_t idx)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2015-06-25 13:38:57 +00:00
|
|
|
struct core_option *option = NULL;
|
|
|
|
|
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
option = (struct core_option*)&opt->opts[idx];
|
2014-10-08 16:37:09 +00:00
|
|
|
|
|
|
|
if (!option)
|
|
|
|
return;
|
|
|
|
|
2014-09-02 03:10:54 +00:00
|
|
|
option->index = (option->index + option->vals->size - 1) %
|
|
|
|
option->vals->size;
|
2015-01-15 21:05:07 +00:00
|
|
|
opt->updated = true;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:58:14 +00:00
|
|
|
/**
|
2016-05-09 23:21:55 +00:00
|
|
|
* core_option_manager_set_default:
|
2015-01-08 02:58:14 +00:00
|
|
|
* @opt : pointer to core option manager object.
|
|
|
|
* @idx : index of core option to be reset to defaults.
|
|
|
|
*
|
2015-01-09 21:01:19 +00:00
|
|
|
* Reset core option specified by @idx and sets default value for option.
|
2015-01-08 02:58:14 +00:00
|
|
|
**/
|
2016-05-09 23:21:55 +00:00
|
|
|
void core_option_manager_set_default(core_option_manager_t *opt, size_t idx)
|
2013-04-04 11:58:30 +00:00
|
|
|
{
|
2014-10-08 16:37:09 +00:00
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
2014-10-20 17:13:16 +00:00
|
|
|
opt->opts[idx].index = 0;
|
2015-01-15 21:05:07 +00:00
|
|
|
opt->updated = true;
|
2013-04-04 11:58:30 +00:00
|
|
|
}
|