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 <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 return { WINPR_JSON_ParseFromFile(config.c_str()), WINPR_JSON_Delete };
45}
46
47WINPR_JSON* SdlPref::get_item(const std::string& key)
48{
49 if (!_config)
50 return nullptr;
51 return WINPR_JSON_GetObjectItem(_config.get(), key.c_str());
52}
53
54std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
55{
56 if (!item || !WINPR_JSON_IsString(item))
57 return fallback;
58 auto str = WINPR_JSON_GetStringValue(item);
59 if (!str)
60 return {};
61 return str;
62}
63
64std::string SdlPref::get_string(const std::string& key, const std::string& fallback)
65{
66 auto item = get_item(key);
67 return item_to_str(item, fallback);
68}
69
70bool SdlPref::get_bool(const std::string& key, bool fallback)
71{
72 auto item = get_item(key);
73 if (!item || !WINPR_JSON_IsBool(item))
74 return fallback;
75 return WINPR_JSON_IsTrue(item);
76}
77
78int64_t SdlPref::get_int(const std::string& key, int64_t fallback)
79{
80 auto item = get_item(key);
81 if (!item || !WINPR_JSON_IsNumber(item))
82 return fallback;
83 auto val = WINPR_JSON_GetNumberValue(item);
84 return static_cast<int64_t>(val);
85}
86
87std::vector<std::string> SdlPref::get_array(const std::string& key,
88 const std::vector<std::string>& fallback)
89{
90 auto item = get_item(key);
91 if (!item || !WINPR_JSON_IsArray(item))
92 return fallback;
93
94 std::vector<std::string> values;
95 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
96 {
97 auto cur = WINPR_JSON_GetArrayItem(item, x);
98 values.push_back(item_to_str(cur));
99 }
100
101 return values;
102}
103
104void SdlPref::print_config_file_help(int version)
105{
106#if defined(WITH_WINPR_JSON)
107 const std::string url = "https://wiki.libsdl.org/SDL" + std::to_string(version);
108 std::cout << "CONFIGURATION FILE" << std::endl;
109 std::cout << std::endl;
110 std::cout << " The SDL client supports some user defined configuration options." << std::endl;
111 std::cout << " Settings are stored in JSON format" << std::endl;
112 std::cout << " The location is a per user file. Location for current user is "
113 << SdlPref::instance()->get_pref_file() << std::endl;
114 std::cout
115 << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
116 << std::endl;
117 std::cout << std::endl;
118 std::cout << " The following configuration options are supported:" << std::endl;
119 std::cout << std::endl;
120 std::cout << " SDL_KeyModMask" << std::endl;
121 std::cout << " Defines the key combination required for SDL client shortcuts."
122 << std::endl;
123 std::cout << " Default KMOD_RSHIFT" << std::endl;
124 std::cout << " An array of SDL_Keymod strings as defined at "
125 ""
126 << url << "/SDL_Keymod" << std::endl;
127 std::cout << std::endl;
128 std::cout << " SDL_Fullscreen" << std::endl;
129 std::cout << " Toggles client fullscreen state." << std::endl;
130 std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
131 std::cout << " A string as "
132 "defined at "
133 << url << "/SDLScancodeLookup" << std::endl;
134 std::cout << std::endl;
135 std::cout << " SDL_Minimize" << std::endl;
136 std::cout << " Minimizes client windows." << std::endl;
137 std::cout << " Default SDL_SCANCODE_M." << std::endl;
138 std::cout << " A string as "
139 "defined at "
140 << url << "/SDLScancodeLookup" << std::endl;
141 std::cout << std::endl;
142 std::cout << " SDL_Resizeable" << std::endl;
143 std::cout << " Toggles local window resizeable state." << std::endl;
144 std::cout << " Default SDL_SCANCODE_R." << std::endl;
145 std::cout << " A string as "
146 "defined at "
147 << url << "/SDLScancodeLookup" << std::endl;
148 std::cout << std::endl;
149 std::cout << " SDL_Grab" << std::endl;
150 std::cout << " Toggles keyboard and mouse grab state." << std::endl;
151 std::cout << " Default SDL_SCANCODE_G." << std::endl;
152 std::cout << " A string as "
153 "defined at "
154 << url << "/SDLScancodeLookup" << std::endl;
155 std::cout << std::endl;
156 std::cout << " SDL_Disconnect" << std::endl;
157 std::cout << " Disconnects from the RDP session." << std::endl;
158 std::cout << " Default SDL_SCANCODE_D." << std::endl;
159 std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
160#endif
161}
162
163SdlPref::SdlPref(std::string file) : _name(std::move(file)), _config(get())
164{
165}
166
167std::string SdlPref::get_pref_dir()
168{
169 using CStringPtr = std::unique_ptr<char, decltype(&free)>;
170 CStringPtr path(freerdp_settings_get_config_path(), free);
171 if (!path)
172 return {};
173
174 fs::path config{ path.get() };
175 return config.string();
176}
177
178std::string SdlPref::get_default_file()
179{
180 fs::path config{ SdlPref::get_pref_dir() };
181 config /= "sdl-freerdp.json";
182 return config.string();
183}
184
185std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
186{
187 static std::shared_ptr<SdlPref> _instance;
188 if (!_instance || (_instance->get_pref_file() != name))
189 _instance.reset(new SdlPref(name));
190 return _instance;
191}
192
193std::string SdlPref::get_pref_file()
194{
195 return _name;
196}
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
FREERDP_API char * freerdp_settings_get_config_path(void)
return the configuration directory for the library