[OSK] General refactoring
[reactos.git] / base / applications / osk / settings.c
1 /*
2 * PROJECT: ReactOS On-Screen Keyboard
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Configuration settings of the application
5 * COPYRIGHT: Copyright 2018-2019 Bișoc George (fraizeraust99 at gmail dot com)
6 */
7
8 /* INCLUDES *******************************************************************/
9
10 #include "precomp.h"
11
12 /* FUNCTIONS *******************************************************************/
13
14 BOOL LoadDataFromRegistry(VOID)
15 {
16 HKEY hKey;
17 LONG lResult;
18 DWORD dwValue;
19 DWORD cbData = sizeof(DWORD);
20
21 /* Initialize the registry application settings */
22 Globals.bShowWarning = TRUE;
23 Globals.bIsEnhancedKeyboard = TRUE;
24 Globals.bSoundClick = FALSE;
25 Globals.bAlwaysOnTop = TRUE;
26
27 /* Set the coordinate values to default */
28 Globals.PosX = CW_USEDEFAULT;
29 Globals.PosY = CW_USEDEFAULT;
30
31 /* Open the key, so that we can query it */
32 lResult = RegOpenKeyExW(HKEY_CURRENT_USER,
33 L"Software\\Microsoft\\Osk",
34 0,
35 KEY_READ,
36 &hKey);
37
38 if (lResult != ERROR_SUCCESS)
39 {
40 /* Bail out and return FALSE if we fail */
41 return FALSE;
42 }
43
44 /* Query the key */
45 lResult = RegQueryValueExW(hKey,
46 L"ShowWarning",
47 0,
48 0,
49 (BYTE *)&dwValue,
50 &cbData);
51
52 if (lResult != ERROR_SUCCESS)
53 {
54 /* Bail out and return FALSE if we fail */
55 RegCloseKey(hKey);
56 return FALSE;
57 }
58
59 /* Load the data value (it can be either FALSE or TRUE depending on the data itself) */
60 Globals.bShowWarning = (dwValue != 0);
61
62 /* Query the key */
63 lResult = RegQueryValueExW(hKey,
64 L"IsEnhancedKeyboard",
65 0,
66 0,
67 (BYTE *)&dwValue,
68 &cbData);
69
70 if (lResult != ERROR_SUCCESS)
71 {
72 /* Bail out and return FALSE if we fail */
73 RegCloseKey(hKey);
74 return FALSE;
75 }
76
77 /* Load the dialog layout value */
78 Globals.bIsEnhancedKeyboard = (dwValue != 0);
79
80 /* Query the key */
81 lResult = RegQueryValueExW(hKey,
82 L"ClickSound",
83 0,
84 0,
85 (BYTE *)&dwValue,
86 &cbData);
87
88 if (lResult != ERROR_SUCCESS)
89 {
90 /* Bail out and return FALSE if we fail */
91 RegCloseKey(hKey);
92 return FALSE;
93 }
94
95 /* Load the sound on click value event */
96 Globals.bSoundClick = (dwValue != 0);
97
98 /* Query the key */
99 lResult = RegQueryValueExW(hKey,
100 L"WindowLeft",
101 0,
102 0,
103 (BYTE *)&dwValue,
104 &cbData);
105
106 if (lResult != ERROR_SUCCESS)
107 {
108 /* Bail out and return FALSE if we fail */
109 RegCloseKey(hKey);
110 return FALSE;
111 }
112
113 /* Load the X value data of the dialog's coordinate */
114 Globals.PosX = dwValue;
115
116 lResult = RegQueryValueExW(hKey,
117 L"WindowTop",
118 0,
119 0,
120 (BYTE *)&dwValue,
121 &cbData);
122
123 if (lResult != ERROR_SUCCESS)
124 {
125 /* Bail out and return FALSE if we fail */
126 RegCloseKey(hKey);
127 return FALSE;
128 }
129
130 /* Load the Y value data of the dialog's coordinate */
131 Globals.PosY = dwValue;
132
133 lResult = RegQueryValueExW(hKey,
134 L"AlwaysOnTop",
135 0,
136 0,
137 (BYTE *)&dwValue,
138 &cbData);
139
140 if (lResult != ERROR_SUCCESS)
141 {
142 /* Bail out and return FALSE if we fail */
143 RegCloseKey(hKey);
144 return FALSE;
145 }
146
147 /* Load the window state value data */
148 Globals.bAlwaysOnTop = (dwValue != 0);
149
150 /* If we're here then we succeed, close the key and return TRUE */
151 RegCloseKey(hKey);
152 return TRUE;
153 }
154
155 BOOL SaveDataToRegistry(VOID)
156 {
157 HKEY hKey;
158 LONG lResult;
159 DWORD dwValue;
160 WINDOWPLACEMENT wp;
161
162 /* Set the structure length and retrieve the dialog's placement */
163 wp.length = sizeof(WINDOWPLACEMENT);
164 GetWindowPlacement(Globals.hMainWnd, &wp);
165
166 /* If no key has been made, create one */
167 lResult = RegCreateKeyExW(HKEY_CURRENT_USER,
168 L"Software\\Microsoft\\Osk",
169 0,
170 NULL,
171 0,
172 KEY_WRITE,
173 NULL,
174 &hKey,
175 NULL);
176
177 if (lResult != ERROR_SUCCESS)
178 {
179 /* Bail out and return FALSE if we fail */
180 return FALSE;
181 }
182
183 /* The data value of the subkey will be appended to the warning dialog switch */
184 dwValue = Globals.bShowWarning;
185
186 /* Welcome warning box value key */
187 lResult = RegSetValueExW(hKey,
188 L"ShowWarning",
189 0,
190 REG_DWORD,
191 (BYTE *)&dwValue,
192 sizeof(dwValue));
193
194 if (lResult != ERROR_SUCCESS)
195 {
196 /* Bail out and return FALSE if we fail */
197 RegCloseKey(hKey);
198 return FALSE;
199 }
200
201 /* The value will be appended to the layout dialog */
202 dwValue = Globals.bIsEnhancedKeyboard;
203
204 /* Keyboard dialog switcher */
205 lResult = RegSetValueExW(hKey,
206 L"IsEnhancedKeyboard",
207 0,
208 REG_DWORD,
209 (BYTE *)&dwValue,
210 sizeof(dwValue));
211
212 if (lResult != ERROR_SUCCESS)
213 {
214 /* Bail out and return FALSE if we fail */
215 RegCloseKey(hKey);
216 return FALSE;
217 }
218
219 /* The value will be appended to the sound on click event */
220 dwValue = Globals.bSoundClick;
221
222 /* "Sound on Click" switcher value key */
223 lResult = RegSetValueExW(hKey,
224 L"ClickSound",
225 0,
226 REG_DWORD,
227 (BYTE *)&dwValue,
228 sizeof(dwValue));
229
230 if (lResult != ERROR_SUCCESS)
231 {
232 /* Bail out and return FALSE if we fail */
233 RegCloseKey(hKey);
234 return FALSE;
235 }
236
237 /* The value will be appended to the X coordination dialog's placement */
238 dwValue = wp.rcNormalPosition.left;
239
240 /* Position X coordination of dialog's placement value key */
241 lResult = RegSetValueExW(hKey,
242 L"WindowLeft",
243 0,
244 REG_DWORD,
245 (BYTE *)&dwValue,
246 sizeof(dwValue));
247
248 if (lResult != ERROR_SUCCESS)
249 {
250 /* Bail out and return FALSE if we fail */
251 RegCloseKey(hKey);
252 return FALSE;
253 }
254
255 /* The value will be appended to the Y coordination dialog's placement */
256 dwValue = wp.rcNormalPosition.top;
257
258 /* Position Y coordination of dialog's placement value key */
259 lResult = RegSetValueExW(hKey,
260 L"WindowTop",
261 0,
262 REG_DWORD,
263 (BYTE *)&dwValue,
264 sizeof(dwValue));
265
266 if (lResult != ERROR_SUCCESS)
267 {
268 /* Bail out and return FALSE if we fail */
269 RegCloseKey(hKey);
270 return FALSE;
271 }
272
273 /* Window top state value */
274 dwValue = Globals.bAlwaysOnTop;
275
276 /* "Always on Top" state value key */
277 lResult = RegSetValueExW(hKey,
278 L"AlwaysOnTop",
279 0,
280 REG_DWORD,
281 (BYTE *)&dwValue,
282 sizeof(dwValue));
283
284 if (lResult != ERROR_SUCCESS)
285 {
286 /* Bail out and return FALSE if we fail */
287 RegCloseKey(hKey);
288 return FALSE;
289 }
290
291 /* If we're here then we succeed, close the key and return TRUE */
292 RegCloseKey(hKey);
293 return TRUE;
294 }