FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
sdl_prefs.cpp
1
20#include <iostream>
21#include <fstream>
22#if __has_include(<filesystem>)
23#include <filesystem>
24#include <utility>
25namespace fs = std::filesystem;
26#elif __has_include(<experimental/filesystem>)
27#include <experimental/filesystem>
28namespace fs = std::experimental::filesystem;
29#else
30#error Could not find system header "<filesystem>" or "<experimental/filesystem>"
31#endif
32
33#include "sdl_prefs.hpp"
34
35#include <winpr/path.h>
36#include <winpr/config.h>
37#include <freerdp/version.h>
38#include <winpr/json.h>
39#include <freerdp/settings.h>
40
41SdlPref::WINPR_JSONPtr SdlPref::get()
42{
43 auto config = get_pref_file();
44
45 std::ifstream ifs(config);
46 std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
47 return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
48}
49
50WINPR_JSON* SdlPref::get_item(const std::string& key)
51{
52 if (!_config)
53 return nullptr;
54 return WINPR_JSON_GetObjectItem(_config.get(), key.c_str());
55}
56
57std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
58{
59 if (!item || !WINPR_JSON_IsString(item))
60 return fallback;
61 auto str = WINPR_JSON_GetStringValue(item);
62 if (!str)
63 return {};
64 return str;
65}
66
67std::string SdlPref::get_string(const std::string& key, const std::string& fallback)
68{
69 auto item = get_item(key);
70 return item_to_str(item, fallback);
71}
72
73bool SdlPref::get_bool(const std::string& key, bool fallback)
74{
75 auto item = get_item(key);
76 if (!item || !WINPR_JSON_IsBool(item))
77 return fallback;
78 return WINPR_JSON_IsTrue(item);
79}
80
81int64_t SdlPref::get_int(const std::string& key, int64_t fallback)
82{
83 auto item = get_item(key);
84 if (!item || !WINPR_JSON_IsNumber(item))
85 return fallback;
86 auto val = WINPR_JSON_GetNumberValue(item);
87 return static_cast<int64_t>(val);
88}
89
90std::vector<std::string> SdlPref::get_array(const std::string& key,
91 const std::vector<std::string>& fallback)
92{
93 auto item = get_item(key);
94 if (!item || !WINPR_JSON_IsArray(item))
95 return fallback;
96
97 std::vector<std::string> values;
98 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
99 {
100 auto cur = WINPR_JSON_GetArrayItem(item, x);
101 values.push_back(item_to_str(cur));
102 }
103
104 return values;
105}
106
107void SdlPref::print_config_file_help(int version)
108{
109#if defined(WITH_WINPR_JSON)
110 const std::string url = "https://wiki.libsdl.org/SDL" + std::to_string(version);
111 std::cout << "CONFIGURATION FILE" << std::endl;
112 std::cout << std::endl;
113 std::cout << " The SDL client supports some user defined configuration options." << std::endl;
114 std::cout << " Settings are stored in JSON format" << std::endl;
115 std::cout << " The location is a per user file. Location for current user is "
116 << SdlPref::instance()->get_pref_file() << std::endl;
117 std::cout
118 << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
119 << std::endl;
120 std::cout << std::endl;
121 std::cout << " The following configuration options are supported:" << std::endl;
122 std::cout << std::endl;
123 std::cout << " SDL_KeyModMask" << std::endl;
124 std::cout << " Defines the key combination required for SDL client shortcuts."
125 << std::endl;
126 std::cout << " Default KMOD_RSHIFT" << std::endl;
127 std::cout << " An array of SDL_Keymod strings as defined at "
128 ""
129 << url << "/SDL_Keymod" << std::endl;
130 std::cout << std::endl;
131 std::cout << " SDL_Fullscreen" << std::endl;
132 std::cout << " Toggles client fullscreen state." << std::endl;
133 std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
134 std::cout << " A string as "
135 "defined at "
136 << url << "/SDLScancodeLookup" << std::endl;
137 std::cout << std::endl;
138 std::cout << " SDL_Minimize" << std::endl;
139 std::cout << " Minimizes client windows." << std::endl;
140 std::cout << " Default SDL_SCANCODE_M." << std::endl;
141 std::cout << " A string as "
142 "defined at "
143 << url << "/SDLScancodeLookup" << std::endl;
144 std::cout << std::endl;
145 std::cout << " SDL_Resizeable" << std::endl;
146 std::cout << " Toggles local window resizeable state." << std::endl;
147 std::cout << " Default SDL_SCANCODE_R." << std::endl;
148 std::cout << " A string as "
149 "defined at "
150 << url << "/SDLScancodeLookup" << std::endl;
151 std::cout << std::endl;
152 std::cout << " SDL_Grab" << std::endl;
153 std::cout << " Toggles keyboard and mouse grab state." << std::endl;
154 std::cout << " Default SDL_SCANCODE_G." << std::endl;
155 std::cout << " A string as "
156 "defined at "
157 << url << "/SDLScancodeLookup" << std::endl;
158 std::cout << std::endl;
159 std::cout << " SDL_Disconnect" << std::endl;
160 std::cout << " Disconnects from the RDP session." << std::endl;
161 std::cout << " Default SDL_SCANCODE_D." << std::endl;
162 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
163#endif
164}
165
166SdlPref::SdlPref(std::string file) : _name(std::move(file)), _config(get())
167{
168}
169
170std::string SdlPref::get_pref_dir()
171{
172 using CStringPtr = std::unique_ptr<char, decltype(&free)>;
173 CStringPtr path(freerdp_settings_get_config_path(), free);
174 if (!path)
175 return {};
176
177 fs::path config{ path.get() };
178 return config.string();
179}
180
181std::string SdlPref::get_default_file()
182{
183 fs::path config{ SdlPref::get_pref_dir() };
184 config /= "sdl-freerdp.json";
185 return config.string();
186}
187
188std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
189{
190 static std::shared_ptr<SdlPref> _instance;
191 if (!_instance || (_instance->get_pref_file() != name))
192 _instance.reset(new SdlPref(name));
193 return _instance;
194}
195
196std::string SdlPref::get_pref_file()
197{
198 return _name;
199}
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition json.c:182
WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition json.c:347
WINPR_API BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition json.c:310
WINPR_API double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition json.c:244
WINPR_API BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition json.c:334
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition json.c:153
WINPR_API WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition json.c:123
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition json.c:232
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition json.c:142
WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition json.c:167
WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition json.c:359
WINPR_API BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition json.c:295
FREERDP_API char * freerdp_settings_get_config_path(void)
return the configuration directory for the library