[client,sdl] refactor SdlPref
* Wrap in class * Allow overriding default configuration file for tests
This commit is contained in:
parent
c073c76871
commit
632f500f3e
@ -1561,7 +1561,7 @@ static void print_config_file_help()
|
|||||||
std::cout << " The SDL client supports some user defined configuration options." << std::endl;
|
std::cout << " The SDL client supports some user defined configuration options." << std::endl;
|
||||||
std::cout << " Settings are stored in JSON format" << std::endl;
|
std::cout << " Settings are stored in JSON format" << std::endl;
|
||||||
std::cout << " The location is a per user file. Location for current user is "
|
std::cout << " The location is a per user file. Location for current user is "
|
||||||
<< sdl_get_pref_file() << std::endl;
|
<< SdlPref::instance()->get_pref_file() << std::endl;
|
||||||
std::cout
|
std::cout
|
||||||
<< " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
|
<< " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
@ -402,7 +402,7 @@ uint32_t sdlInput::prefToMask()
|
|||||||
{ "KMOD_GUI", KMOD_GUI }
|
{ "KMOD_GUI", KMOD_GUI }
|
||||||
};
|
};
|
||||||
uint32_t mod = KMOD_NONE;
|
uint32_t mod = KMOD_NONE;
|
||||||
for (const auto& val : sdl_get_pref_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
|
for (const auto& val : SdlPref::instance()->get_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
|
||||||
{
|
{
|
||||||
auto it = mapping.find(val);
|
auto it = mapping.find(val);
|
||||||
if (it != mapping.end())
|
if (it != mapping.end())
|
||||||
@ -480,7 +480,7 @@ static UINT32 sdl_scancode_to_rdp(Uint32 scancode)
|
|||||||
|
|
||||||
uint32_t sdlInput::prefKeyValue(const std::string& key, uint32_t fallback)
|
uint32_t sdlInput::prefKeyValue(const std::string& key, uint32_t fallback)
|
||||||
{
|
{
|
||||||
auto item = sdl_get_pref_string(key);
|
auto item = SdlPref::instance()->get_string(key);
|
||||||
if (item.empty())
|
if (item.empty())
|
||||||
return fallback;
|
return fallback;
|
||||||
auto val = sdl_scancode_val(item.c_str());
|
auto val = sdl_scancode_val(item.c_str());
|
||||||
|
@ -35,29 +35,23 @@ namespace fs = std::experimental::filesystem;
|
|||||||
#include <freerdp/version.h>
|
#include <freerdp/version.h>
|
||||||
#include <winpr/json.h>
|
#include <winpr/json.h>
|
||||||
|
|
||||||
#if defined(WITH_WINPR_JSON)
|
SdlPref::WINPR_JSONPtr SdlPref::get()
|
||||||
using WINPR_JSONPtr = std::unique_ptr<WINPR_JSON, decltype(&WINPR_JSON_Delete)>;
|
|
||||||
|
|
||||||
static WINPR_JSONPtr get()
|
|
||||||
{
|
{
|
||||||
auto config = sdl_get_pref_file();
|
auto config = get_pref_file();
|
||||||
|
|
||||||
std::ifstream ifs(config);
|
std::ifstream ifs(config);
|
||||||
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||||
return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
|
return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
|
||||||
}
|
}
|
||||||
|
|
||||||
static WINPR_JSON* get_item(const std::string& key)
|
WINPR_JSON* SdlPref::get_item(const std::string& key)
|
||||||
{
|
{
|
||||||
static WINPR_JSONPtr config{ nullptr, WINPR_JSON_Delete };
|
if (!_config)
|
||||||
if (!config)
|
|
||||||
config = get();
|
|
||||||
if (!config)
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return WINPR_JSON_GetObjectItem(config.get(), key.c_str());
|
return WINPR_JSON_GetObjectItem(_config.get(), key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string item_to_str(WINPR_JSON* item, const std::string& fallback = "")
|
std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
|
||||||
{
|
{
|
||||||
if (!item || !WINPR_JSON_IsString(item))
|
if (!item || !WINPR_JSON_IsString(item))
|
||||||
return fallback;
|
return fallback;
|
||||||
@ -66,47 +60,33 @@ static std::string item_to_str(WINPR_JSON* item, const std::string& fallback = "
|
|||||||
return {};
|
return {};
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string sdl_get_pref_string(const std::string& key, const std::string& fallback)
|
std::string SdlPref::get_string(const std::string& key, const std::string& fallback)
|
||||||
{
|
{
|
||||||
#if defined(WITH_WINPR_JSON)
|
|
||||||
auto item = get_item(key);
|
auto item = get_item(key);
|
||||||
return item_to_str(item, fallback);
|
return item_to_str(item, fallback);
|
||||||
#else
|
|
||||||
return fallback;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sdl_get_pref_bool(const std::string& key, bool fallback)
|
bool SdlPref::get_bool(const std::string& key, bool fallback)
|
||||||
{
|
{
|
||||||
#if defined(WITH_WINPR_JSON)
|
|
||||||
auto item = get_item(key);
|
auto item = get_item(key);
|
||||||
if (!item || !WINPR_JSON_IsBool(item))
|
if (!item || !WINPR_JSON_IsBool(item))
|
||||||
return fallback;
|
return fallback;
|
||||||
return WINPR_JSON_IsTrue(item);
|
return WINPR_JSON_IsTrue(item);
|
||||||
#else
|
|
||||||
return fallback;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t sdl_get_pref_int(const std::string& key, int64_t fallback)
|
int64_t SdlPref::get_int(const std::string& key, int64_t fallback)
|
||||||
{
|
{
|
||||||
#if defined(WITH_WINPR_JSON)
|
|
||||||
auto item = get_item(key);
|
auto item = get_item(key);
|
||||||
if (!item || !WINPR_JSON_IsNumber(item))
|
if (!item || !WINPR_JSON_IsNumber(item))
|
||||||
return fallback;
|
return fallback;
|
||||||
auto val = WINPR_JSON_GetNumberValue(item);
|
auto val = WINPR_JSON_GetNumberValue(item);
|
||||||
return static_cast<int64_t>(val);
|
return static_cast<int64_t>(val);
|
||||||
#else
|
|
||||||
return fallback;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> sdl_get_pref_array(const std::string& key,
|
std::vector<std::string> SdlPref::get_array(const std::string& key,
|
||||||
const std::vector<std::string>& fallback)
|
const std::vector<std::string>& fallback)
|
||||||
{
|
{
|
||||||
#if defined(WITH_WINPR_JSON)
|
|
||||||
auto item = get_item(key);
|
auto item = get_item(key);
|
||||||
if (!item || !WINPR_JSON_IsArray(item))
|
if (!item || !WINPR_JSON_IsArray(item))
|
||||||
return fallback;
|
return fallback;
|
||||||
@ -119,12 +99,13 @@ std::vector<std::string> sdl_get_pref_array(const std::string& key,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
#else
|
|
||||||
return fallback;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sdl_get_pref_dir()
|
SdlPref::SdlPref(const std::string& file) : _name(file), _config(get())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SdlPref::get_pref_dir()
|
||||||
{
|
{
|
||||||
using CStringPtr = std::unique_ptr<char, decltype(&free)>;
|
using CStringPtr = std::unique_ptr<char, decltype(&free)>;
|
||||||
CStringPtr path(GetKnownPath(KNOWN_PATH_XDG_CONFIG_HOME), free);
|
CStringPtr path(GetKnownPath(KNOWN_PATH_XDG_CONFIG_HOME), free);
|
||||||
@ -137,9 +118,22 @@ std::string sdl_get_pref_dir()
|
|||||||
return config.string();
|
return config.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sdl_get_pref_file()
|
std::string SdlPref::get_default_file()
|
||||||
{
|
{
|
||||||
fs::path config{ sdl_get_pref_dir() };
|
fs::path config{ SdlPref::get_pref_dir() };
|
||||||
config /= "sdl-freerdp.json";
|
config /= "sdl-freerdp.json";
|
||||||
return config.string();
|
return config.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
|
||||||
|
{
|
||||||
|
static std::shared_ptr<SdlPref> _instance;
|
||||||
|
if (!_instance || (_instance->get_pref_file() != name))
|
||||||
|
_instance.reset(new SdlPref(name));
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SdlPref::get_pref_file()
|
||||||
|
{
|
||||||
|
return _name;
|
||||||
|
}
|
||||||
|
@ -21,12 +21,34 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include <winpr/json.h>
|
||||||
|
|
||||||
std::string sdl_get_pref_dir();
|
class SdlPref
|
||||||
std::string sdl_get_pref_file();
|
{
|
||||||
|
public:
|
||||||
|
static std::shared_ptr<SdlPref> instance(const std::string& name = SdlPref::get_default_file());
|
||||||
|
|
||||||
std::string sdl_get_pref_string(const std::string& key, const std::string& fallback = "");
|
std::string get_pref_file();
|
||||||
int64_t sdl_get_pref_int(const std::string& key, int64_t fallback = 0);
|
|
||||||
bool sdl_get_pref_bool(const std::string& key, bool fallback = false);
|
std::string get_string(const std::string& key, const std::string& fallback = "");
|
||||||
std::vector<std::string> sdl_get_pref_array(const std::string& key,
|
int64_t get_int(const std::string& key, int64_t fallback = 0);
|
||||||
const std::vector<std::string>& fallback = {});
|
bool get_bool(const std::string& key, bool fallback = false);
|
||||||
|
std::vector<std::string> get_array(const std::string& key,
|
||||||
|
const std::vector<std::string>& fallback = {});
|
||||||
|
|
||||||
|
private:
|
||||||
|
using WINPR_JSONPtr = std::unique_ptr<WINPR_JSON, decltype(&WINPR_JSON_Delete)>;
|
||||||
|
|
||||||
|
std::string _name;
|
||||||
|
WINPR_JSONPtr _config;
|
||||||
|
|
||||||
|
SdlPref(const std::string& file);
|
||||||
|
|
||||||
|
WINPR_JSON* get_item(const std::string& key);
|
||||||
|
WINPR_JSONPtr get();
|
||||||
|
|
||||||
|
static std::string get_pref_dir();
|
||||||
|
static std::string get_default_file();
|
||||||
|
static std::string item_to_str(WINPR_JSON* item, const std::string& fallback = "");
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user