xref: /reactos/base/applications/osk/settings.c (revision 7115d7ba)
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 LONG LoadDataFromRegistry(IN LPCWSTR lpValueDataName,
15                           OUT PDWORD pdwValueData)
16 {
17     HKEY hKey;
18     LONG lResult;
19     DWORD dwValue;
20     DWORD cbData = sizeof(dwValue);
21 
22     /* Initialize the pointer parameter to default */
23     *pdwValueData = 0;
24 
25     /* Open our application's key in order to load its configuration data */
26     lResult = RegOpenKeyExW(HKEY_CURRENT_USER,
27                             L"Software\\Microsoft\\Osk",
28                             0,
29                             KEY_READ,
30                             &hKey);
31 
32     if (lResult != ERROR_SUCCESS)
33     {
34         /* Bail out */
35         DPRINT("LoadDataFromRegistry(): Failed to open the application's key! (Error - %li)\n", lResult);
36         return lResult;
37     }
38 
39     /* Load the specific value based on the parameter caller, lpValueDataName */
40     lResult = RegQueryValueExW(hKey,
41                                lpValueDataName,
42                                0,
43                                0,
44                                (BYTE *)&dwValue,
45                                &cbData);
46 
47     if (lResult != ERROR_SUCCESS)
48     {
49 
50         /* Bail out */
51         DPRINT("LoadDataFromRegistry(): Failed to load the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
52         RegCloseKey(hKey);
53         return lResult;
54     }
55 
56     /* Is the buffer's size too small to query the required data? */
57     if (cbData != sizeof(dwValue))
58     {
59         /* It is therefore bail out */
60         DPRINT("LoadDataFromRegistry(): The buffer is too small to hold the data!\n");
61         RegCloseKey(hKey);
62         return ERROR_MORE_DATA;
63     }
64 
65     *pdwValueData = dwValue;
66     RegCloseKey(hKey);
67     return lResult;
68 }
69 
70 LONG SaveDataToRegistry(IN LPCWSTR lpValueDataName,
71                         IN DWORD dwValueData)
72 {
73     HKEY hKey;
74     LONG lResult;
75 
76     /* Set up the application's key in case it has not been made yet */
77     lResult = RegCreateKeyExW(HKEY_CURRENT_USER,
78                               L"Software\\Microsoft\\Osk",
79                               0,
80                               NULL,
81                               0,
82                               KEY_WRITE,
83                               NULL,
84                               &hKey,
85                               NULL);
86 
87     if (lResult != ERROR_SUCCESS)
88     {
89         /* Bail out */
90         DPRINT("SaveDataToRegistry(): Failed to create the application's key! (Error - %li)\n", lResult);
91         return lResult;
92     }
93 
94     /* Save the data into the registry value */
95     lResult = RegSetValueExW(hKey,
96                              lpValueDataName,
97                              0,
98                              REG_DWORD,
99                              (BYTE *)&dwValueData,
100                              sizeof(dwValueData));
101 
102     if (lResult != ERROR_SUCCESS)
103     {
104         /* Bail out */
105         DPRINT("SaveDataToRegistry(): Failed to save the following value - \"%S\". (Error - %li)\n", lpValueDataName, lResult);
106         RegCloseKey(hKey);
107         return lResult;
108     }
109 
110     RegCloseKey(hKey);
111     return lResult;
112 }
113 
114 VOID LoadSettings(VOID)
115 {
116     DWORD dwValue;
117     LONG lResult;
118 
119     /* Initialize the registry application settings */
120     Globals.bShowWarning = TRUE;
121     Globals.bIsEnhancedKeyboard = TRUE;
122     Globals.bAlwaysOnTop = TRUE;
123     Globals.bSoundClick = FALSE;
124 
125     /* Set the coordinate values to default */
126     Globals.PosX = CW_USEDEFAULT;
127     Globals.PosY = CW_USEDEFAULT;
128 
129     /* Warning dialog registry setting */
130     lResult = LoadDataFromRegistry(L"ShowWarning", &dwValue);
131     if (lResult == NO_ERROR)
132         Globals.bShowWarning = (dwValue != 0);
133 
134     /* Enhanced keyboard switch dialog registry setting */
135     lResult = LoadDataFromRegistry(L"IsEnhancedKeyboard", &dwValue);
136     if (lResult == NO_ERROR)
137         Globals.bIsEnhancedKeyboard = (dwValue != 0);
138 
139     /* Sound on click event registry setting */
140     lResult = LoadDataFromRegistry(L"ClickSound", &dwValue);
141     if (lResult == NO_ERROR)
142         Globals.bSoundClick = (dwValue != 0);
143 
144     /* X coordinate dialog placement registry setting */
145     lResult = LoadDataFromRegistry(L"WindowLeft", &dwValue);
146     if (lResult == NO_ERROR)
147         Globals.PosX = dwValue;
148 
149     /* Y coordinate dialog placement registry setting */
150     lResult = LoadDataFromRegistry(L"WindowTop", &dwValue);
151     if (lResult == NO_ERROR)
152         Globals.PosY = dwValue;
153 
154     /* Top window state registry setting */
155     lResult = LoadDataFromRegistry(L"AlwaysOnTop", &dwValue);
156     if (lResult == NO_ERROR)
157         Globals.bAlwaysOnTop = (dwValue != 0);
158 }
159 
160 VOID SaveSettings(VOID)
161 {
162     WINDOWPLACEMENT wp;
163 
164     /* Initialize the window placement structure */
165     wp.length = sizeof(WINDOWPLACEMENT);
166     GetWindowPlacement(Globals.hMainWnd, &wp);
167 
168     /* Warning dialog registry setting */
169     SaveDataToRegistry(L"ShowWarning", Globals.bShowWarning);
170 
171     /* Enhanced keyboard switch dialog registry setting */
172     SaveDataToRegistry(L"IsEnhancedKeyboard", Globals.bIsEnhancedKeyboard);
173 
174     /* Sound on click event registry setting */
175     SaveDataToRegistry(L"ClickSound", Globals.bSoundClick);
176 
177     /* X coordinate dialog placement registry setting */
178     SaveDataToRegistry(L"WindowLeft", wp.rcNormalPosition.left);
179 
180     /* Y coordinate dialog placement registry setting */
181     SaveDataToRegistry(L"WindowTop", wp.rcNormalPosition.top);
182 
183     /* Top window state registry setting */
184     SaveDataToRegistry(L"AlwaysOnTop", Globals.bAlwaysOnTop);
185 }
186