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