FreeRDP
Loading...
Searching...
No Matches
sdl_prefs.cpp
1
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
79std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
80{
81 if (!item || !WINPR_JSON_IsString(item))
82 return fallback;
83 auto str = WINPR_JSON_GetStringValue(item);
84 if (!str)
85 return {};
86 return str;
87}
88
89std::string SdlPref::get_string(const std::string& key, const std::string& fallback,
90 bool systemConfigOnly) const
91{
92 auto item = get_item(key, systemConfigOnly);
93 return item_to_str(item, fallback);
94}
95
96bool SdlPref::get_bool(const std::string& key, bool fallback, bool systemConfigOnly) const
97{
98 auto& config = systemConfigOnly ? _system_config : _config;
99 return get_bool(config, key, fallback);
100}
101
102int64_t SdlPref::get_int(const std::string& key, int64_t fallback, bool systemConfigOnly) const
103{
104 auto item = get_item(key, systemConfigOnly);
105 if (!item || !WINPR_JSON_IsNumber(item))
106 return fallback;
107 auto val = WINPR_JSON_GetNumberValue(item);
108 return static_cast<int64_t>(val);
109}
110
111std::vector<std::string> SdlPref::get_array(const std::string& key,
112 const std::vector<std::string>& fallback,
113 bool systemConfigOnly) const
114{
115 auto item = get_item(key, systemConfigOnly);
116 if (!item || !WINPR_JSON_IsArray(item))
117 return fallback;
118
119 std::vector<std::string> values;
120 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
121 {
122 auto cur = WINPR_JSON_GetArrayItem(item, x);
123 values.push_back(item_to_str(cur));
124 }
125
126 return values;
127}
128
129void SdlPref::print_config_file_help(int version)
130{
131#if defined(WITH_WINPR_JSON)
132 const std::string url = "https://wiki.libsdl.org/SDL" + std::to_string(version);
133 std::cout << "GLOBAL CONFIGURATION FILE" << std::endl;
134 std::cout << std::endl;
135 std::cout << " The SDL client supports some system defined configuration options."
136 << std::endl;
137 std::cout << " Settings are stored in JSON format" << std::endl;
138 std::cout << " The location is a system configuration file. Location for current machine is "
139 << SdlPref::instance()->get_pref_file(true) << std::endl;
140 std::cout << std::endl;
141 std::cout << " The following configuration options are supported:" << std::endl;
142 std::cout << std::endl;
143 std::cout << " isUserConfigEnabled" << std::endl;
144 std::cout << " Allows to enable/disable user specific configuration files." << std::endl;
145 std::cout << " Default enabled" << std::endl;
146 std::cout << std::endl;
147 std::cout << " All options of the following user configuration file are also supported here."
148 << std::endl;
149 std::cout << std::endl;
150
151 std::cout << "CONFIGURATION FILE" << std::endl;
152 std::cout << std::endl;
153 std::cout << " The SDL client supports some user defined configuration options." << std::endl;
154 std::cout << " Settings are stored in JSON format" << std::endl;
155 std::cout << " The location is a per user file. Location for current user is "
156 << SdlPref::instance()->get_pref_file() << std::endl;
157 std::cout
158 << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
159 << std::endl;
160 std::cout << std::endl;
161 std::cout << " The following configuration options are supported:" << std::endl;
162 std::cout << std::endl;
163 std::cout << " SDL_KeyModMask" << std::endl;
164 std::cout << " Defines the key combination required for SDL client shortcuts."
165 << std::endl;
166 std::cout << " Default KMOD_RSHIFT" << std::endl;
167 std::cout << " An array of SDL_Keymod strings as defined at "
168 ""
169 << url << "/SDL_Keymod" << std::endl;
170 std::cout << std::endl;
171 std::cout << " SDL_Fullscreen" << std::endl;
172 std::cout << " Toggles client fullscreen state." << std::endl;
173 std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
174 std::cout << " A string as "
175 "defined at "
176 << url << "/SDLScancodeLookup" << std::endl;
177 std::cout << std::endl;
178 std::cout << " SDL_Minimize" << std::endl;
179 std::cout << " Minimizes client windows." << std::endl;
180 std::cout << " Default SDL_SCANCODE_M." << std::endl;
181 std::cout << " A string as "
182 "defined at "
183 << url << "/SDLScancodeLookup" << std::endl;
184 std::cout << std::endl;
185 std::cout << " SDL_Resizeable" << std::endl;
186 std::cout << " Toggles local window resizeable state." << std::endl;
187 std::cout << " Default SDL_SCANCODE_R." << std::endl;
188 std::cout << " A string as "
189 "defined at "
190 << url << "/SDLScancodeLookup" << std::endl;
191 std::cout << std::endl;
192 std::cout << " SDL_Grab" << std::endl;
193 std::cout << " Toggles keyboard and mouse grab state." << std::endl;
194 std::cout << " Default SDL_SCANCODE_G." << std::endl;
195 std::cout << " A string as "
196 "defined at "
197 << url << "/SDLScancodeLookup" << std::endl;
198 std::cout << std::endl;
199 std::cout << " SDL_Disconnect" << std::endl;
200 std::cout << " Disconnects from the RDP session." << std::endl;
201 std::cout << " Default SDL_SCANCODE_D." << std::endl;
202 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
203
204#endif
205}
206
207SdlPref::SdlPref(std::string file)
208 : _name(std::move(file)), _system_name(get_default_file(true)), _config(get(false)),
209 _system_config(get(true))
210{
211}
212
213std::string SdlPref::get_default_file(bool systemConfigOnly)
214{
215 CStringPtr name(freerdp_GetConfigFilePath(systemConfigOnly, "sdl-freerdp.json"), free);
216 fs::path config{ name.get() };
217 return config.string();
218}
219
220std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
221{
222 static std::shared_ptr<SdlPref> _instance;
223 if (!_instance || (_instance->get_pref_file() != name))
224 _instance.reset(new SdlPref(name));
225 return _instance;
226}
227
228std::string SdlPref::get_pref_file(bool systemConfigOnly) const
229{
230 if (systemConfigOnly)
231 return _system_name;
232
233 return _name;
234}
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:167
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:177
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:162
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 BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
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 double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition c-json.c:147
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:187
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