ddb72c5a6b3647ba85a7dd8c3714ed3a61c60a8c
[reactos.git] / dll / win32 / msports / parallel.c
1 /*
2 * PROJECT: Ports installer library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll\win32\msports\parallel.c
5 * PURPOSE: Parallel Port property functions
6 * COPYRIGHT: Copyright 2013 Eric Kohl
7 */
8
9 #include "precomp.h"
10
11 typedef struct _PORT_DATA
12 {
13 HDEVINFO DeviceInfoSet;
14 PSP_DEVINFO_DATA DeviceInfoData;
15
16 WCHAR szPortName[16];
17 DWORD dwFilterResourceMethod;
18 DWORD dwLegacy;
19
20 } PORT_DATA, *PPORT_DATA;
21
22
23 static
24 VOID
25 ReadPortSettings(
26 PPORT_DATA pPortData)
27 {
28 DWORD dwSize;
29 HKEY hKey;
30 DWORD dwError;
31
32 TRACE("ReadPortSettings(%p)\n", pPortData);
33
34 pPortData->dwFilterResourceMethod = 1; /* Never use an interrupt */
35 pPortData->dwLegacy = 0; /* Disabled */
36
37 hKey = SetupDiOpenDevRegKey(pPortData->DeviceInfoSet,
38 pPortData->DeviceInfoData,
39 DICS_FLAG_GLOBAL,
40 0,
41 DIREG_DEV,
42 KEY_READ);
43 if (hKey != INVALID_HANDLE_VALUE)
44 {
45 dwSize = sizeof(pPortData->szPortName);
46 dwError = RegQueryValueExW(hKey,
47 L"PortName",
48 NULL,
49 NULL,
50 (PBYTE)pPortData->szPortName,
51 &dwSize);
52 if (dwError != ERROR_SUCCESS)
53 {
54 ERR("RegQueryValueExW failed (Error %lu)\n", dwError);
55 }
56
57 dwSize = sizeof(pPortData->dwFilterResourceMethod);
58 dwError = RegQueryValueExW(hKey,
59 L"FilterResourceMethod",
60 NULL,
61 NULL,
62 (PBYTE)&pPortData->dwFilterResourceMethod,
63 &dwSize);
64 if (dwError != ERROR_SUCCESS)
65 {
66 ERR("RegQueryValueExW failed (Error %lu)\n", dwError);
67 }
68
69 RegCloseKey(hKey);
70 }
71
72 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
73 L"SYSTEM\\CurrentControlSet\\Services\\Parport\\Parameters",
74 0,
75 KEY_READ,
76 &hKey);
77 if (dwError == ERROR_SUCCESS)
78 {
79 dwSize = sizeof(pPortData->dwLegacy);
80 dwError = RegQueryValueExW(hKey,
81 L"ParEnableLegacyZip",
82 NULL,
83 NULL,
84 (PBYTE)&pPortData->dwLegacy,
85 &dwSize);
86 if (dwError != ERROR_SUCCESS)
87 {
88 ERR("RegQueryValueExW failed (Error %lu)\n", dwError);
89 }
90
91 RegCloseKey(hKey);
92 }
93 }
94
95
96 static
97 VOID
98 WritePortSettings(
99 HWND hwnd,
100 PPORT_DATA pPortData)
101 {
102 DWORD dwDisposition;
103 DWORD dwFilterResourceMethod;
104 DWORD dwLegacy;
105 HKEY hKey;
106 DWORD dwError;
107
108 TRACE("WritePortSettings(%p)\n", pPortData);
109
110 dwFilterResourceMethod = 1;
111 if (Button_GetCheck(GetDlgItem(hwnd, IDC_TRY_INTERRUPT)) == BST_CHECKED)
112 dwFilterResourceMethod = 0;
113 else if (Button_GetCheck(GetDlgItem(hwnd, IDC_NEVER_INTERRUPT)) == BST_CHECKED)
114 dwFilterResourceMethod = 1;
115 else if (Button_GetCheck(GetDlgItem(hwnd, IDC_ANY_INTERRUPT)) == BST_CHECKED)
116 dwFilterResourceMethod = 2;
117
118 if (dwFilterResourceMethod != pPortData->dwFilterResourceMethod)
119 {
120 hKey = SetupDiOpenDevRegKey(pPortData->DeviceInfoSet,
121 pPortData->DeviceInfoData,
122 DICS_FLAG_GLOBAL,
123 0,
124 DIREG_DEV,
125 KEY_WRITE);
126 if (hKey != INVALID_HANDLE_VALUE)
127 {
128 dwError = RegSetValueExW(hKey,
129 L"FilterResourceMethod",
130 0,
131 REG_DWORD,
132 (PBYTE)&dwFilterResourceMethod,
133 sizeof(dwFilterResourceMethod));
134 if (dwError != ERROR_SUCCESS)
135 {
136 ERR("RegSetValueExW failed (Error %lu)\n", dwError);
137 }
138
139 RegCloseKey(hKey);
140 pPortData->dwFilterResourceMethod = dwFilterResourceMethod;
141 }
142 }
143
144 dwLegacy = 0;
145 if (Button_GetCheck(GetDlgItem(hwnd, IDC_PARALLEL_LEGACY)) == BST_CHECKED)
146 dwLegacy = 1;
147
148 if (dwLegacy != pPortData->dwLegacy)
149 {
150 dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
151 L"SYSTEM\\CurrentControlSet\\Services\\Parport\\Parameters",
152 0,
153 NULL,
154 REG_OPTION_NON_VOLATILE,
155 KEY_WRITE,
156 NULL,
157 &hKey,
158 &dwDisposition);
159 if (dwError == ERROR_SUCCESS)
160 {
161 dwError = RegSetValueExW(hKey,
162 L"ParEnableLegacyZip",
163 0,
164 REG_DWORD,
165 (LPBYTE)&dwLegacy,
166 sizeof(dwLegacy));
167 RegCloseKey(hKey);
168
169 if (dwError == ERROR_SUCCESS)
170 {
171 FIXME("Notify the driver!\n");
172 }
173 }
174 }
175 }
176
177
178 static
179 BOOL
180 OnInitDialog(HWND hwnd,
181 WPARAM wParam,
182 LPARAM lParam)
183 {
184 PPORT_DATA pPortData;
185 HWND hwndControl;
186 WCHAR szBuffer[256];
187 UINT i;
188
189 TRACE("OnInitDialog()\n");
190
191 pPortData = (PPORT_DATA)((LPPROPSHEETPAGEW)lParam)->lParam;
192 if (pPortData == NULL)
193 {
194 ERR("pPortData is NULL\n");
195 return FALSE;
196 }
197
198 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)pPortData);
199
200 /* Read and parse the port settings */
201 ReadPortSettings(pPortData);
202
203 /* Set the 'Filter Resource Method' radiobuttons */
204 hwndControl = GetDlgItem(hwnd,
205 IDC_TRY_INTERRUPT + pPortData->dwFilterResourceMethod);
206 if (hwndControl)
207 Button_SetCheck(hwndControl, BST_CHECKED);
208
209 /* Disable the 'Enable legacy PNP detection' checkbox */
210 hwndControl = GetDlgItem(hwnd, IDC_PARALLEL_LEGACY);
211 if (hwndControl)
212 {
213 Button_SetCheck(GetDlgItem(hwnd, IDC_PARALLEL_LEGACY),
214 pPortData->dwLegacy ? BST_CHECKED : BST_UNCHECKED);
215 }
216
217 /* Fill the 'LPT Port Number' combobox */
218 hwndControl = GetDlgItem(hwnd, IDC_PARALLEL_NAME);
219 if (hwndControl)
220 {
221 for (i = 0; i < 3; i++)
222 {
223 swprintf(szBuffer, L"LPT%lu", i + 1);
224 ComboBox_AddString(hwndControl, szBuffer);
225 }
226
227 /* Disable it */
228 EnableWindow(hwndControl, FALSE);
229 }
230
231 return TRUE;
232 }
233
234
235 static
236 VOID
237 OnNotify(
238 HWND hwnd,
239 WPARAM wParam,
240 LPARAM lParam)
241 {
242 PPORT_DATA pPortData;
243
244 TRACE("OnNotify()\n");
245
246 pPortData = (PPORT_DATA)GetWindowLongPtr(hwnd, DWLP_USER);
247 if (pPortData == NULL)
248 {
249 ERR("pPortData is NULL\n");
250 return;
251 }
252
253 if (((LPNMHDR)lParam)->code == (UINT)PSN_APPLY)
254 {
255 TRACE("PSN_APPLY!\n");
256 WritePortSettings(hwnd, pPortData);
257 }
258 }
259
260
261 static
262 VOID
263 OnDestroy(
264 HWND hwnd)
265 {
266 PPORT_DATA pPortData;
267
268 TRACE("OnDestroy()\n");
269
270 pPortData = (PPORT_DATA)GetWindowLongPtr(hwnd, DWLP_USER);
271 if (pPortData == NULL)
272 {
273 ERR("pPortData is NULL\n");
274 return;
275 }
276
277 HeapFree(GetProcessHeap(), 0, pPortData);
278 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)NULL);
279 }
280
281
282 static
283 INT_PTR
284 CALLBACK
285 ParallelSettingsDlgProc(HWND hwnd,
286 UINT uMsg,
287 WPARAM wParam,
288 LPARAM lParam)
289 {
290 TRACE("ParallelSettingsDlgProc()\n");
291
292 switch (uMsg)
293 {
294 case WM_INITDIALOG:
295 return OnInitDialog(hwnd, wParam, lParam);
296
297 case WM_NOTIFY:
298 OnNotify(hwnd, wParam, lParam);
299 break;
300
301 case WM_DESTROY:
302 OnDestroy(hwnd);
303 break;
304 }
305
306 return FALSE;
307 }
308
309
310 BOOL
311 WINAPI
312 ParallelPortPropPageProvider(PSP_PROPSHEETPAGE_REQUEST lpPropSheetPageRequest,
313 LPFNADDPROPSHEETPAGE lpfnAddPropSheetPageProc,
314 LPARAM lParam)
315 {
316 PROPSHEETPAGEW PropSheetPage;
317 HPROPSHEETPAGE hPropSheetPage;
318 PPORT_DATA pPortData;
319
320 TRACE("ParallelPortPropPageProvider(%p %p %lx)\n",
321 lpPropSheetPageRequest, lpfnAddPropSheetPageProc, lParam);
322
323 pPortData = HeapAlloc(GetProcessHeap(),
324 HEAP_ZERO_MEMORY,
325 sizeof(PORT_DATA));
326 if (pPortData == NULL)
327 {
328 ERR("Port data allocation failed!\n");
329 return FALSE;
330 }
331
332 pPortData->DeviceInfoSet = lpPropSheetPageRequest->DeviceInfoSet;
333 pPortData->DeviceInfoData = lpPropSheetPageRequest->DeviceInfoData;
334
335 if (lpPropSheetPageRequest->PageRequested == SPPSR_ENUM_ADV_DEVICE_PROPERTIES)
336 {
337 TRACE("SPPSR_ENUM_ADV_DEVICE_PROPERTIES\n");
338
339 PropSheetPage.dwSize = sizeof(PROPSHEETPAGEW);
340 PropSheetPage.dwFlags = 0;
341 PropSheetPage.hInstance = hInstance;
342 PropSheetPage.pszTemplate = MAKEINTRESOURCE(IDD_PARALLELSETTINGS);
343 PropSheetPage.pfnDlgProc = ParallelSettingsDlgProc;
344 PropSheetPage.lParam = (LPARAM)pPortData;
345 PropSheetPage.pfnCallback = NULL;
346
347 hPropSheetPage = CreatePropertySheetPageW(&PropSheetPage);
348 if (hPropSheetPage == NULL)
349 {
350 ERR("CreatePropertySheetPageW() failed!\n");
351 return FALSE;
352 }
353
354 if (!(*lpfnAddPropSheetPageProc)(hPropSheetPage, lParam))
355 {
356 ERR("lpfnAddPropSheetPageProc() failed!\n");
357 DestroyPropertySheetPage(hPropSheetPage);
358 return FALSE;
359 }
360 }
361
362 TRACE("Done!\n");
363
364 return TRUE;
365 }
366
367 /* EOF */