Fix merge r65567.
[reactos.git] / dll / cpl / sysdm / hardprof.c
1 /*
2 * PROJECT: ReactOS System Control Panel Applet
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/sysdm/hardprof.c
5 * PURPOSE: Modify hardware profiles
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "precomp.h"
11
12 #include <debug.h>
13
14 typedef struct _PROFILE
15 {
16 WCHAR szFriendlyName[256];
17 } PROFILE, *PPROFILE;
18
19 typedef struct _PROFILEDATA
20 {
21 DWORD dwProfileCount;
22 PPROFILE pProfiles;
23 } PROFILEDATA, *PPROFILEDATA;
24
25
26 static
27 INT_PTR
28 CALLBACK
29 CopyProfileDlgProc(HWND hwndDlg,
30 UINT uMsg,
31 WPARAM wParam,
32 LPARAM lParam)
33 {
34 UNREFERENCED_PARAMETER(lParam);
35 UNREFERENCED_PARAMETER(wParam);
36 UNREFERENCED_PARAMETER(hwndDlg);
37
38 switch (uMsg)
39 {
40 case WM_COMMAND:
41 if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
42 {
43 EndDialog(hwndDlg,
44 LOWORD(wParam));
45 return TRUE;
46 }
47 break;
48 }
49 return FALSE;
50 }
51
52
53 static
54 INT_PTR
55 CALLBACK
56 RenameProfileDlgProc(HWND hwndDlg,
57 UINT uMsg,
58 WPARAM wParam,
59 LPARAM lParam)
60 {
61 UNREFERENCED_PARAMETER(lParam);
62 UNREFERENCED_PARAMETER(wParam);
63 UNREFERENCED_PARAMETER(hwndDlg);
64
65 switch (uMsg)
66 {
67 case WM_COMMAND:
68 if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
69 {
70 EndDialog(hwndDlg,
71 LOWORD(wParam));
72 return TRUE;
73 }
74 break;
75 }
76 return FALSE;
77 }
78
79
80 static
81 DWORD
82 GetUserWaitInterval(VOID)
83 {
84 DWORD dwWaitInterval = 30;
85 DWORD dwSize;
86 HKEY hKey;
87
88 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
89 L"System\\CurrentControlSet\\Control\\IDConfigDB",
90 0,
91 KEY_QUERY_VALUE,
92 &hKey))
93 return dwWaitInterval;
94
95 dwSize = sizeof(DWORD);
96 RegQueryValueExW(hKey,
97 L"UserWaitInterval",
98 NULL,
99 NULL,
100 (LPBYTE)&dwWaitInterval,
101 &dwSize);
102
103 RegCloseKey(hKey);
104
105 return dwWaitInterval;
106 }
107
108
109 static
110 VOID
111 SetUserWaitInterval(DWORD dwWaitInterval)
112 {
113 HKEY hKey;
114
115 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
116 L"System\\CurrentControlSet\\Control\\IDConfigDB",
117 0,
118 KEY_SET_VALUE,
119 &hKey))
120 return;
121
122 RegSetValueExW(hKey,
123 L"UserWaitInterval",
124 0,
125 REG_DWORD,
126 (LPBYTE)&dwWaitInterval,
127 sizeof(DWORD));
128
129 RegCloseKey(hKey);
130 }
131
132
133 static
134 BOOL
135 GetProfileCount(LPDWORD lpProfileCount)
136 {
137 HKEY hKey;
138 LONG lError;
139
140 *lpProfileCount = 0;
141
142 lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
143 L"System\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles",
144 0,
145 KEY_READ,
146 &hKey);
147 if (lError != ERROR_SUCCESS)
148 return FALSE;
149
150 lError = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, lpProfileCount,
151 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
152
153 RegCloseKey(hKey);
154
155 if (lError != ERROR_SUCCESS)
156 return FALSE;
157
158 return TRUE;
159 }
160
161
162 static
163 VOID
164 GetProfile(HWND hwndDlg, HKEY hKey, LPWSTR lpName, PPROFILE pProfile)
165 {
166 HKEY hProfileKey;
167 DWORD dwSize;
168 LONG lError;
169
170 lError = RegOpenKeyExW(hKey,
171 lpName,
172 0,
173 KEY_READ,
174 &hProfileKey);
175 if (lError != ERROR_SUCCESS)
176 return;
177
178 dwSize = 256 * sizeof(WCHAR);
179 lError = RegQueryValueExW(hProfileKey,
180 L"FriendlyName",
181 NULL,
182 NULL,
183 (LPBYTE)pProfile->szFriendlyName,
184 &dwSize);
185 if (lError == ERROR_SUCCESS)
186 {
187 DPRINT1("Profile: %S\n", pProfile->szFriendlyName);
188 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFLSTBOX, LB_ADDSTRING, 0, (LPARAM)pProfile->szFriendlyName);
189 }
190
191 RegCloseKey(hProfileKey);
192 }
193
194
195 static
196 BOOL
197 GetProfiles(HWND hwndDlg)
198 {
199 PPROFILEDATA pProfileData;
200 WCHAR szName[8];
201 DWORD dwNameLength;
202 DWORD dwIndex = 0;
203 HKEY hKey;
204 LONG lError;
205
206 pProfileData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROFILEDATA));
207 if (pProfileData == NULL)
208 return FALSE;
209
210 if (!GetProfileCount(&pProfileData->dwProfileCount))
211 {
212 HeapFree(GetProcessHeap(), 0, pProfileData);
213 return FALSE;
214 }
215
216 pProfileData->pProfiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
217 pProfileData->dwProfileCount * sizeof(PROFILE));
218 if (pProfileData->pProfiles == NULL)
219 {
220 HeapFree(GetProcessHeap(), 0, pProfileData);
221 return FALSE;
222 }
223
224 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pProfileData);
225
226 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
227 L"System\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles",
228 0,
229 KEY_READ,
230 &hKey) != ERROR_SUCCESS)
231 return FALSE;
232
233 for (dwIndex = 0; dwIndex < pProfileData->dwProfileCount; dwIndex++)
234 {
235 dwNameLength = 8;
236 lError = RegEnumKeyExW(hKey,
237 dwIndex,
238 szName,
239 &dwNameLength,
240 NULL,
241 NULL,
242 NULL,
243 NULL);
244 if (lError != ERROR_SUCCESS)
245 break;
246
247 DPRINT("Profile name: %S\n", szName);
248
249 GetProfile(hwndDlg, hKey, szName, &pProfileData->pProfiles[dwIndex]);
250 }
251
252 RegCloseKey(hKey);
253
254 return TRUE;
255 }
256
257
258 static
259 BOOL
260 OnInitDialog(HWND hwndDlg)
261 {
262 DWORD dwWaitInterval;
263
264 DPRINT("OnInitDialog()\n");
265
266 SendMessage(GetDlgItem(hwndDlg, IDC_HRDPROFUP),
267 BM_SETIMAGE,(WPARAM)IMAGE_ICON,
268 (LPARAM)(HANDLE)LoadIcon(hApplet, MAKEINTRESOURCE(IDI_UP)));
269 SendMessage(GetDlgItem(hwndDlg, IDC_HRDPROFDWN),
270 BM_SETIMAGE,(WPARAM)IMAGE_ICON,
271 (LPARAM)(HANDLE)LoadIcon(hApplet, MAKEINTRESOURCE(IDI_DOWN)));
272
273 if (!GetProfiles(hwndDlg))
274 return FALSE;
275
276 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG((SHORT)500, 0));
277
278 dwWaitInterval = GetUserWaitInterval();
279 if (dwWaitInterval == (DWORD)-1)
280 {
281 CheckDlgButton(hwndDlg, IDC_HRDPROFWAIT, BST_CHECKED);
282 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETPOS, 0, 30);
283 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), FALSE);
284 }
285 else
286 {
287 CheckDlgButton(hwndDlg, IDC_HRDPROFSELECT, BST_CHECKED);
288 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETPOS, 0, dwWaitInterval);
289 }
290
291 return TRUE;
292 }
293
294
295 static
296 VOID
297 OnOk(HWND hwndDlg)
298 {
299 DWORD dwWaitInterval;
300
301 if (IsDlgButtonChecked(hwndDlg, IDC_HRDPROFWAIT) == BST_CHECKED)
302 {
303 dwWaitInterval = (DWORD)-1;
304 }
305 else
306 {
307 dwWaitInterval = LOWORD(SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_GETPOS, 0, 0));
308 }
309
310 SetUserWaitInterval(dwWaitInterval);
311 }
312
313
314 /* Property page dialog callback */
315 INT_PTR
316 CALLBACK
317 HardProfDlgProc(HWND hwndDlg,
318 UINT uMsg,
319 WPARAM wParam,
320 LPARAM lParam)
321 {
322 PPROFILEDATA pProfileData;
323
324 UNREFERENCED_PARAMETER(lParam);
325 UNREFERENCED_PARAMETER(wParam);
326 UNREFERENCED_PARAMETER(hwndDlg);
327
328 pProfileData = (PPROFILEDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
329
330 switch (uMsg)
331 {
332 case WM_INITDIALOG:
333 return OnInitDialog(hwndDlg);
334
335 case WM_DESTROY:
336 if (pProfileData != NULL)
337 {
338 if (pProfileData->pProfiles != NULL)
339 HeapFree(GetProcessHeap(), 0, pProfileData->pProfiles);
340 HeapFree(GetProcessHeap(), 0, pProfileData);
341 }
342 break;
343
344 case WM_COMMAND:
345 switch (LOWORD(wParam))
346 {
347 case IDC_HRDPROFCOPY:
348 DialogBox(hApplet,
349 MAKEINTRESOURCE(IDD_COPYPROFILE),
350 hwndDlg,
351 (DLGPROC)CopyProfileDlgProc);
352 break;
353
354 case IDC_HRDPROFRENAME:
355 DialogBox(hApplet,
356 MAKEINTRESOURCE(IDD_RENAMEPROFILE),
357 hwndDlg,
358 (DLGPROC)RenameProfileDlgProc);
359 break;
360
361 case IDC_HRDPROFWAIT:
362 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), FALSE);
363 return TRUE;
364
365 case IDC_HRDPROFSELECT:
366 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), TRUE);
367 return TRUE;
368
369 case IDOK:
370 OnOk(hwndDlg);
371
372 case IDCANCEL:
373 EndDialog(hwndDlg,
374 LOWORD(wParam));
375 return TRUE;
376 }
377 break;
378 }
379
380 return FALSE;
381 }