FreeRDP
Loading...
Searching...
No Matches
sdl_prefs.cpp
1
19
20#include <iostream>
21#include <fstream>
22#include "filesystem.hpp"
23
24#include "sdl_prefs.hpp"
25#include "sdl_common_utils.hpp"
26
27#include <winpr/path.h>
28#include <winpr/config.h>
29#include <winpr/json.h>
30
31#include <freerdp/version.h>
32#include <freerdp/settings.h>
33#include <freerdp/utils/helpers.h>
34
35SdlPref::WINPR_JSONPtr SdlPref::get(bool systemConfigOnly) const
36{
37 auto config = get_pref_file(systemConfigOnly);
38 return { WINPR_JSON_ParseFromFile(config.c_str()), WINPR_JSON_Delete };
39}
40
41WINPR_JSON* SdlPref::get_item(const std::string& key, bool systemConfigOnly) const
42{
43 /* If we request a system setting or user settings are disabled */
44 if (systemConfigOnly || !is_user_config_enabled())
45 return get_item(_system_config, key);
46
47 /* Get the user setting */
48 auto res = get_item(_config, key);
49
50 /* User setting does not exist, fall back to system setting */
51 if (!res)
52 res = get_item(_system_config, key);
53 return res;
54}
55
56WINPR_JSON* SdlPref::get_item(const WINPR_JSONPtr& config, const std::string& key) const
57{
58 if (!config)
59 return nullptr;
60 return WINPR_JSON_GetObjectItemCaseSensitive(config.get(), key.c_str());
61}
62
63bool SdlPref::get_bool(const WINPR_JSONPtr& config, const std::string& key, bool fallback) const
64{
65 auto item = get_item(config, key);
66 if (!item || !WINPR_JSON_IsBool(item))
67 return fallback;
68 return WINPR_JSON_IsTrue(item);
69}
70
71bool SdlPref::is_user_config_enabled() const
72{
73 auto& config = _system_config;
74 if (!config)
75 return true;
76 return get_bool(config, "isUserConfigEnabled", true);
77}
78
79bool SdlPref::is_commandline_override_allowed() const
80{
81 auto& config = _system_config;
82 if (!config)
83 return true;
84 return get_bool(config, "allowCommandlineToOverride", true);
85}
86
87std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
88{
89 if (!item || !WINPR_JSON_IsString(item))
90 return fallback;
91 auto str = WINPR_JSON_GetStringValue(item);
92 if (!str)
93 return {};
94 return str;
95}
96
97std::string SdlPref::get_string(const std::string& key, const std::string& fallback,
98 bool systemConfigOnly) const
99{
100 auto item = get_item(key, systemConfigOnly);
101 return item_to_str(item, fallback);
102}
103
104bool SdlPref::get_bool(const std::string& key, bool fallback, bool systemConfigOnly) const
105{
106 auto item = get_item(key, systemConfigOnly);
107 if (!item || !WINPR_JSON_IsBool(item))
108 return fallback;
109 return WINPR_JSON_IsTrue(item);
110}
111
112int64_t SdlPref::get_int(const std::string& key, int64_t fallback, bool systemConfigOnly) const
113{
114 auto item = get_item(key, systemConfigOnly);
115 if (!item || !WINPR_JSON_IsNumber(item))
116 return fallback;
117 return WINPR_JSON_GetIntegerValue(item);
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 << " allowCommandlineToOverride" << std::endl;
157 std::cout
158 << " Allows command line options to override values from the configuration files."
159 << std::endl;
160 std::cout << " Default enabled" << std::endl;
161 std::cout << std::endl;
162 std::cout << " All options of the following user configuration file are also supported here."
163 << std::endl;
164 std::cout << std::endl;
165
166 std::cout << "CONFIGURATION FILE" << std::endl;
167 std::cout << std::endl;
168 std::cout << " The SDL client supports some user defined configuration options." << std::endl;
169 std::cout << " Settings are stored in JSON format" << std::endl;
170 std::cout << " The location is a per user file. Location for current user is "
171 << SdlPref::instance()->get_pref_file() << std::endl;
172 std::cout
173 << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
174 << std::endl;
175 std::cout << std::endl;
176 std::cout << " The following configuration options are supported:" << std::endl;
177 std::cout << std::endl;
178 std::cout << " SDL_KeyModMask" << std::endl;
179 std::cout << " Defines the key combination required for SDL client shortcuts."
180 << std::endl;
181 std::cout << " Default KMOD_RSHIFT" << std::endl;
182 std::cout << " An array of SDL_Keymod strings as defined at "
183 ""
184 << url << "/SDL_Keymod" << std::endl;
185 std::cout << std::endl;
186 std::cout << " SDL_Fullscreen" << std::endl;
187 std::cout << " Toggles client fullscreen state." << std::endl;
188 std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
189 std::cout << " A string as "
190 "defined at "
191 << url << "/SDLScancodeLookup" << std::endl;
192 std::cout << std::endl;
193 std::cout << " SDL_Minimize" << std::endl;
194 std::cout << " Minimizes client windows." << std::endl;
195 std::cout << " Default SDL_SCANCODE_M." << std::endl;
196 std::cout << " A string as "
197 "defined at "
198 << url << "/SDLScancodeLookup" << std::endl;
199 std::cout << std::endl;
200 std::cout << " SDL_Resizeable" << std::endl;
201 std::cout << " Toggles local window resizeable state." << std::endl;
202 std::cout << " Default SDL_SCANCODE_R." << std::endl;
203 std::cout << " A string as "
204 "defined at "
205 << url << "/SDLScancodeLookup" << std::endl;
206 std::cout << std::endl;
207 std::cout << " SDL_Grab" << std::endl;
208 std::cout << " Toggles keyboard and mouse grab state." << std::endl;
209 std::cout << " Default SDL_SCANCODE_G." << std::endl;
210 std::cout << " A string as "
211 "defined at "
212 << url << "/SDLScancodeLookup" << std::endl;
213 std::cout << std::endl;
214 std::cout << " SDL_Disconnect" << std::endl;
215 std::cout << " Disconnects from the RDP session." << std::endl;
216 std::cout << " Default SDL_SCANCODE_D." << std::endl;
217 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
218 if (version >= 3)
219 {
220 std::cout << std::endl;
221 std::cout << " SDL_ConnectionDialog" << std::endl;
222 std::cout << " Shows the connection dialog with progress and errors." << std::endl;
223 std::cout << " Default true. +/-sdl-connection-dialog overrides it unless "
224 "allowCommandlineToOverride is disabled."
225 << std::endl;
226 std::cout << std::endl;
227 std::cout << " SDL_Info" << std::endl;
228 std::cout << " Shows and hides the stats and info overlay (+sdl-info shows it from "
229 "the start)."
230 << std::endl;
231 std::cout << " Default SDL_SCANCODE_I. Like every shortcut, the modifier and the key "
232 "no longer reach the session: with the default right Shift, a capital I "
233 "has to be typed with the left one."
234 << std::endl;
235 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
236 }
237
238#endif
239}
240
241SdlPref::SdlPref(std::string file, std::string systemFile)
242 : _name(std::move(file)), _system_name(std::move(systemFile)), _config(get(false)),
243 _system_config(get(true))
244{
245}
246
247std::string SdlPref::get_default_file(bool systemConfigOnly)
248{
249 CStringPtr name(freerdp_GetConfigFilePath(systemConfigOnly, "sdl-freerdp.json"), free);
250 fs::path config{ name.get() };
251 return config.string();
252}
253
254std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name, const std::string& systemName)
255{
256 static std::shared_ptr<SdlPref> _instance;
257 if (!_instance || (_instance->get_pref_file() != name) ||
258 (_instance->get_pref_file(true) != systemName))
259 _instance.reset(new SdlPref(name, systemName));
260 return _instance;
261}
262
263std::string SdlPref::get_pref_file(bool systemConfigOnly) const
264{
265 if (systemConfigOnly)
266 return _system_name;
267
268 return _name;
269}
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition c-json.c:179
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition c-json.c:189
WINPR_ATTR_NODISCARD 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_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition c-json.c:174
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:27
WINPR_ATTR_NODISCARD 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_ATTR_NODISCARD WINPR_API INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON *item)
Return the Integer value of a JSON item.
Definition c-json.c:152
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:194
WINPR_ATTR_NODISCARD 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_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:199
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142