xref: /reactos/base/applications/osk/settings.c (revision 84344399)
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 George Bișoc (george.bisoc@reactos.org)
6  *                  Baruch Rutman (peterooch at gmail dot com)
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "precomp.h"
12 
13 /* FUNCTIONS *******************************************************************/
14 
15 LONG LoadDWORDFromRegistry(IN LPCWSTR lpValueDataName,
16                            OUT PDWORD pdwValueData)
17 {
18     HKEY hKey;
19     LONG lResult;
20     DWORD dwValue;
21     DWORD cbData = sizeof(dwValue);
22 
23     /* Initialize the pointer parameter to default */
24     *pdwValueData = 0;
25 
26     /* Open our application's key in order to load its configuration data */
27     lResult = RegOpenKeyExW(HKEY_CURRENT_USER,
28                             L"Software\\Microsoft\\Osk",
29                             0,
30                             KEY_READ,
31                             &hKey);
32 
33     if (lResult != ERROR_SUCCESS)
34     {
35         /* Bail out */
36         DPRINT("LoadDWORDFromRegistry(): Failed to open the application's key! (Error - %li)\n", lResult);
37         return lResult;
38     }
39 
40     /* Load the specific value based on the parameter caller, lpValueDataName */
41     lResult = RegQueryValueExW(hKey,
42                                lpValueDataName,
43                                0,
44                                0,
45                                (BYTE *)&dwValue,
46                                &cbData);
47 
48     if (lResult != ERROR_SUCCESS)
49     {
50 
51         /* Bail out */
52         DPRINT("LoadDWORDFromRegistry(): Failed to load the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
53         RegCloseKey(hKey);
54         return lResult;
55     }
56 
57     /* Is the buffer's size too small to query the required data? */
58     if (cbData != sizeof(dwValue))
59     {
60         /* It is therefore bail out */
61         DPRINT("LoadDWORDFromRegistry(): The buffer is too small to hold the data!\n");
62         RegCloseKey(hKey);
63         return ERROR_MORE_DATA;
64     }
65 
66     *pdwValueData = dwValue;
67     RegCloseKey(hKey);
68     return lResult;
69 }
70 
71 /* IN: cchCount is how many characters fit in lpValueData,
72    OUT: cchCount is how many characters were written into lpValueData */
73 LONG LoadStringFromRegistry(IN LPCWSTR lpValueDataName,
74                             OUT LPWSTR lpValueData,
75                             IN OUT LPUINT cchCount)
76 {
77     HKEY hKey;
78     LONG lResult;
79     UINT cbCount;
80 
81     cbCount = (*cchCount) * sizeof(WCHAR);
82 
83     /* Open our application's key in order to load its configuration data */
84     lResult = RegOpenKeyExW(HKEY_CURRENT_USER,
85                             L"Software\\Microsoft\\Osk",
86                             0,
87                             KEY_READ,
88                             &hKey);
89 
90     if (lResult != ERROR_SUCCESS)
91     {
92         /* Bail out */
93         DPRINT("LoadStringFromRegistry(): Failed to open the application's key! (Error - %li)\n", lResult);
94         return lResult;
95     }
96 
97     /* Load the specific value based on the parameter caller, lpValueDataName */
98     lResult = RegQueryValueExW(hKey,
99                                lpValueDataName,
100                                0,
101                                0,
102                                (BYTE *)lpValueData,
103                                (LPDWORD)&cbCount);
104 
105 
106     if (lResult != ERROR_SUCCESS)
107     {
108 
109         /* Bail out */
110         DPRINT("LoadStringFromRegistry(): Failed to load the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
111         RegCloseKey(hKey);
112         return lResult;
113     }
114 
115     *cchCount = cbCount / sizeof(WCHAR);
116 
117     RegCloseKey(hKey);
118     return lResult;
119 }
120 
121 LONG SaveDWORDToRegistry(IN LPCWSTR lpValueDataName,
122                          IN DWORD dwValueData)
123 {
124     HKEY hKey;
125     LONG lResult;
126 
127     /* Set up the application's key in case it has not been made yet */
128     lResult = RegCreateKeyExW(HKEY_CURRENT_USER,
129                               L"Software\\Microsoft\\Osk",
130                               0,
131                               NULL,
132                               0,
133                               KEY_WRITE,
134                               NULL,
135                               &hKey,
136                               NULL);
137 
138     if (lResult != ERROR_SUCCESS)
139     {
140         /* Bail out */
141         DPRINT("SaveDWORDToRegistry(): Failed to create the application's key! (Error - %li)\n", lResult);
142         return lResult;
143     }
144 
145     /* Save the data into the registry value */
146     lResult = RegSetValueExW(hKey,
147                              lpValueDataName,
148                              0,
149                              REG_DWORD,
150                              (BYTE *)&dwValueData,
151                              sizeof(dwValueData));
152 
153     if (lResult != ERROR_SUCCESS)
154     {
155         /* Bail out */
156         DPRINT("SaveDWORDToRegistry(): Failed to save the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
157         RegCloseKey(hKey);
158         return lResult;
159     }
160 
161     RegCloseKey(hKey);
162     return lResult;
163 }
164 
165 LONG SaveStringToRegistry(IN LPCWSTR lpValueDataName,
166                           IN LPCWSTR lpValueData,
167                           IN UINT cchCount)
168 {
169     HKEY hKey;
170     LONG lResult;
171 
172     /* Set up the application's key in case it has not been made yet */
173     lResult = RegCreateKeyExW(HKEY_CURRENT_USER,
174                               L"Software\\Microsoft\\Osk",
175                               0,
176                               NULL,
177                               0,
178                               KEY_WRITE,
179                               NULL,
180                               &hKey,
181                               NULL);
182 
183     if (lResult != ERROR_SUCCESS)
184     {
185         /* Bail out */
186         DPRINT("SaveStringToRegistry(): Failed to create the application's key! (Error - %li)\n", lResult);
187         return lResult;
188     }
189 
190     /* Save the data into the registry value */
191     lResult = RegSetValueExW(hKey,
192                              lpValueDataName,
193                              0,
194                              REG_SZ,
195                              (BYTE *)lpValueData,
196                              cchCount * sizeof(WCHAR));
197 
198     if (lResult != ERROR_SUCCESS)
199     {
200         /* Bail out */
201         DPRINT("SaveStringToRegistry(): Failed to save the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
202         RegCloseKey(hKey);
203         return lResult;
204     }
205 
206     RegCloseKey(hKey);
207     return lResult;
208 }
209 
210 VOID LoadSettings(VOID)
211 {
212     DWORD dwValue;
213     LONG lResult;
214 
215     /* Initialize the registry application settings */
216     Globals.bShowWarning = TRUE;
217     Globals.bIsEnhancedKeyboard = TRUE;
218     Globals.bAlwaysOnTop = TRUE;
219     Globals.bSoundClick = FALSE;
220 
221     /* Set the coordinate values to default */
222     Globals.PosX = CW_USEDEFAULT;
223     Globals.PosY = CW_USEDEFAULT;
224 
225     /* Set font value defaults */
226     Globals.FontHeight = DEFAULT_FONTSIZE;
227 
228     /* Warning dialog registry setting */
229     lResult = LoadDWORDFromRegistry(L"ShowWarning", &dwValue);
230     if (lResult == NO_ERROR)
231         Globals.bShowWarning = (dwValue != 0);
232 
233     /* Enhanced keyboard switch dialog registry setting */
234     lResult = LoadDWORDFromRegistry(L"IsEnhancedKeyboard", &dwValue);
235     if (lResult == NO_ERROR)
236         Globals.bIsEnhancedKeyboard = (dwValue != 0);
237 
238     /* Sound on click event registry setting */
239     lResult = LoadDWORDFromRegistry(L"ClickSound", &dwValue);
240     if (lResult == NO_ERROR)
241         Globals.bSoundClick = (dwValue != 0);
242 
243     /* X coordinate dialog placement registry setting */
244     lResult = LoadDWORDFromRegistry(L"WindowLeft", &dwValue);
245     if (lResult == NO_ERROR)
246         Globals.PosX = dwValue;
247 
248     /* Y coordinate dialog placement registry setting */
249     lResult = LoadDWORDFromRegistry(L"WindowTop", &dwValue);
250     if (lResult == NO_ERROR)
251         Globals.PosY = dwValue;
252 
253     /* Top window state registry setting */
254     lResult = LoadDWORDFromRegistry(L"AlwaysOnTop", &dwValue);
255     if (lResult == NO_ERROR)
256         Globals.bAlwaysOnTop = (dwValue != 0);
257 
258     /* Font information */
259     UINT cchCount = _countof(Globals.FontFaceName);
260     lResult = LoadStringFromRegistry(L"FontFaceName", Globals.FontFaceName, &cchCount);
261 
262     if (lResult != NO_ERROR) /* Copy default on failure */
263         StringCchCopyW(Globals.FontFaceName, _countof(Globals.FontFaceName), L"MS Shell Dlg");
264 
265     lResult = LoadDWORDFromRegistry(L"FontHeight", &dwValue);
266     if (lResult == NO_ERROR)
267         Globals.FontHeight = dwValue;
268 }
269 
270 VOID SaveSettings(VOID)
271 {
272     WINDOWPLACEMENT wp;
273 
274     /* Initialize the window placement structure */
275     wp.length = sizeof(WINDOWPLACEMENT);
276     GetWindowPlacement(Globals.hMainWnd, &wp);
277 
278     /* Warning dialog registry setting */
279     SaveDWORDToRegistry(L"ShowWarning", Globals.bShowWarning);
280 
281     /* Enhanced keyboard switch dialog registry setting */
282     SaveDWORDToRegistry(L"IsEnhancedKeyboard", Globals.bIsEnhancedKeyboard);
283 
284     /* Sound on click event registry setting */
285     SaveDWORDToRegistry(L"ClickSound", Globals.bSoundClick);
286 
287     /* X coordinate dialog placement registry setting */
288     SaveDWORDToRegistry(L"WindowLeft", wp.rcNormalPosition.left);
289 
290     /* Y coordinate dialog placement registry setting */
291     SaveDWORDToRegistry(L"WindowTop", wp.rcNormalPosition.top);
292 
293     /* Top window state registry setting */
294     SaveDWORDToRegistry(L"AlwaysOnTop", Globals.bAlwaysOnTop);
295 
296     /* Font information */
297     SaveStringToRegistry(L"FontFaceName", Globals.FontFaceName, _countof(Globals.FontFaceName));
298     SaveDWORDToRegistry(L"FontHeight", Globals.FontHeight);
299 }
300