FreeRDP
Loading...
Searching...
No Matches
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#include "sdl_common_utils.hpp"
35
36#include <winpr/path.h>
37#include <winpr/config.h>
38#include <winpr/json.h>
39
40#include <freerdp/version.h>
41#include <freerdp/settings.h>
42#include <freerdp/utils/helpers.h>
43
44SdlPref::WINPR_JSONPtr SdlPref::get(bool systemConfigOnly) const
45{
46 auto config = get_pref_file(systemConfigOnly);
47 return { WINPR_JSON_ParseFromFile(config.c_str()), WINPR_JSON_Delete };
48}
49
50WINPR_JSON* SdlPref::get_item(const std::string& key, bool systemConfigOnly) const
51{
52 /* If we request a system setting or user settings are disabled */
53 if (systemConfigOnly || !is_user_config_enabled())
54 return get_item(_system_config, key);
55
56 /* Get the user setting */
57 auto res = get_item(_config, key);
58
59 /* User setting does not exist, fall back to system setting */
60 if (!res)
61 res = get_item(_system_config, key);
62 return res;
63}
64
65WINPR_JSON* SdlPref::get_item(const WINPR_JSONPtr& config, const std::string& key) const
66{
67 if (!config)
68 return nullptr;
69 return WINPR_JSON_GetObjectItemCaseSensitive(config.get(), key.c_str());
70}
71
72bool SdlPref::get_bool(const WINPR_JSONPtr& config, const std::string& key, bool fallback) const
73{
74 auto item = get_item(config, key);
75 if (!item || !WINPR_JSON_IsBool(item))
76 return fallback;
77 return WINPR_JSON_IsTrue(item);
78}
79
80bool SdlPref::is_user_config_enabled() const
81{
82 auto& config = _system_config;
83 if (!config)
84 return true;
85 return get_bool(config, "isUserConfigEnabled", true);
86}
87
88std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
89{
90 if (!item || !WINPR_JSON_IsString(item))
91 return fallback;
92 auto str = WINPR_JSON_GetStringValue(item);
93 if (!str)
94 return {};
95 return str;
96}
97
98std::string SdlPref::get_string(const std::string& key, const std::string& fallback,
99 bool systemConfigOnly) const
100{
101 auto item = get_item(key, systemConfigOnly);
102 return item_to_str(item, fallback);
103}
104
105bool SdlPref::get_bool(const std::string& key, bool fallback, bool systemConfigOnly) const
106{
107 auto& config = systemConfigOnly ? _system_config : _config;
108 return get_bool(config, key, fallback);
109}
110
111int64_t SdlPref::get_int(const std::string& key, int64_t fallback, bool systemConfigOnly) const
112{
113 auto item = get_item(key, systemConfigOnly);
114 if (!item || !WINPR_JSON_IsNumber(item))
115 return fallback;
116 auto val = WINPR_JSON_GetNumberValue(item);
117 return static_cast<int64_t>(val);
118}
119
120std::vector<std::string> SdlPref::get_array(const std::string& key,
121 const std::vector<std::string>& fallback,
122 bool systemConfigOnly) const
123{
124 auto item = get_item(key, systemConfigOnly);
125 if (!item || !WINPR_JSON_IsArray(item))
126 return fallback;
127
128 std::vector<std::string> values;
129 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
130 {
131 auto cur = WINPR_JSON_GetArrayItem(item, x);
132 values.push_back(item_to_str(cur));
133 }
134
135 return values;
136}
137
138void SdlPref::print_config_file_help(int version)
139{
140#if defined(WITH_WINPR_JSON)
141 const std::string url = "https://wiki.libsdl.org/SDL" + std::to_string(version);
142 std::cout << "GLOBAL CONFIGURATION FILE" << std::endl;
143 std::cout << std::endl;
144 std::cout << " The SDL client supports some system defined configuration options."
145 << std::endl;
146 std::cout << " Settings are stored in JSON format" << std::endl;
147 std::cout << " The location is a system configuration file. Location for current machine is "
148 << SdlPref::instance()->get_pref_file(true) << std::endl;
149 std::cout << std::endl;
150 std::cout << " The following configuration options are supported:" << std::endl;
151 std::cout << std::endl;
152 std::cout << " isUserConfigEnabled" << std::endl;
153 std::cout << " Allows to enable/disable user specific configuration files." << std::endl;
154 std::cout << " Default enabled" << std::endl;
155 std::cout << std::endl;
156 std::cout << " All options of the following user configuration file are also supported here."
157 << std::endl;
158 std::cout << std::endl;
159
160 std::cout << "CONFIGURATION FILE" << std::endl;
161 std::cout << std::endl;
162 std::cout << " The SDL client supports some user defined configuration options." << std::endl;
163 std::cout << " Settings are stored in JSON format" << std::endl;
164 std::cout << " The location is a per user file. Location for current user is "
165 << SdlPref::instance()->get_pref_file() << std::endl;
166 std::cout
167 << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
168 << std::endl;
169 std::cout << std::endl;
170 std::cout << " The following configuration options are supported:" << std::endl;
171 std::cout << std::endl;
172 std::cout << " SDL_KeyModMask" << std::endl;
173 std::cout << " Defines the key combination required for SDL client shortcuts."
174 << std::endl;
175 std::cout << " Default KMOD_RSHIFT" << std::endl;
176 std::cout << " An array of SDL_Keymod strings as defined at "
177 ""
178 << url << "/SDL_Keymod" << std::endl;
179 std::cout << std::endl;
180 std::cout << " SDL_Fullscreen" << std::endl;
181 std::cout << " Toggles client fullscreen state." << std::endl;
182 std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
183 std::cout << " A string as "
184 "defined at "
185 << url << "/SDLScancodeLookup" << std::endl;
186 std::cout << std::endl;
187 std::cout << " SDL_Minimize" << std::endl;
188 std::cout << " Minimizes client windows." << std::endl;
189 std::cout << " Default SDL_SCANCODE_M." << std::endl;
190 std::cout << " A string as "
191 "defined at "
192 << url << "/SDLScancodeLookup" << std::endl;
193 std::cout << std::endl;
194 std::cout << " SDL_Resizeable" << std::endl;
195 std::cout << " Toggles local window resizeable state." << std::endl;
196 std::cout << " Default SDL_SCANCODE_R." << std::endl;
197 std::cout << " A string as "
198 "defined at "
199 << url << "/SDLScancodeLookup" << std::endl;
200 std::cout << std::endl;
201 std::cout << " SDL_Grab" << std::endl;
202 std::cout << " Toggles keyboard and mouse grab state." << std::endl;
203 std::cout << " Default SDL_SCANCODE_G." << std::endl;
204 std::cout << " A string as "
205 "defined at "
206 << url << "/SDLScancodeLookup" << std::endl;
207 std::cout << std::endl;
208 std::cout << " SDL_Disconnect" << std::endl;
209 std::cout << " Disconnects from the RDP session." << std::endl;
210 std::cout << " Default SDL_SCANCODE_D." << std::endl;
211 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
212
213#endif
214}
215
216SdlPref::SdlPref(std::string file)
217 : _name(std::move(file)), _system_name(get_default_file(true)), _config(get(false)),
218 _system_config(get(true))
219{
220}
221
222std::string SdlPref::get_default_file(bool systemConfigOnly)
223{
224 CStringPtr name(freerdp_GetConfigFilePath(systemConfigOnly, "sdl-freerdp.json"), free);
225 fs::path config{ name.get() };
226 return config.string();
227}
228
229std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
230{
231 static std::shared_ptr<SdlPref> _instance;
232 if (!_instance || (_instance->get_pref_file() != name))
233 _instance.reset(new SdlPref(name));
234 return _instance;
235}
236
237std::string SdlPref::get_pref_file(bool systemConfigOnly) const
238{
239 if (systemConfigOnly)
240 return _system_name;
241
242 return _name;
243}
WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
WINPR_API BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition c-json.c:167
WINPR_API double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition c-json.c:147
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:27
WINPR_API BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition c-json.c:177
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition c-json.c:108
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition c-json.c:127
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:187
WINPR_API BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition c-json.c:162