Reactos
1/*
2 * PROJECT: ReactOS devmgr.dll
3 * FILE: dll/win32/devmgr/hwresource.c
4 * PURPOSE: ReactOS Device Manager
5 * PROGRAMMER: Johannes Anderwald <johannes.anderwald@reactos.org>
6 * UPDATE HISTORY:
7 * 2005/11/24 Created
8 */
9
10#include "precomp.h"
11#include "restypes.h"
12#include "properties.h"
13
14#include "resource.h"
15
16
17typedef struct
18{
19 HWND hWnd;
20 HWND hWndDevList;
21
22
23}HARDWARE_RESOURCE_DATA, *PHARDWARE_RESOURCE_DATA;
24
25
26#define CX_TYPECOLUMN_WIDTH 120
27
28static VOID
29InitializeDevicesList(
30 IN HWND hWndDevList)
31{
32 LVCOLUMN lvc;
33 RECT rcClient;
34 WCHAR szColName[255];
35 int iCol = 0;
36
37 /* set the list view style */
38 (void)ListView_SetExtendedListViewStyle(hWndDevList,
39 LVS_EX_FULLROWSELECT);
40
41 GetClientRect(hWndDevList,
42 &rcClient);
43
44 /* add the list view columns */
45 lvc.mask = LVCF_TEXT | LVCF_WIDTH;
46 lvc.fmt = LVCFMT_LEFT;
47 lvc.pszText = szColName;
48
49 if (LoadString(hDllInstance,
50 IDS_RESOURCE_COLUMN,
51 szColName,
52 sizeof(szColName) / sizeof(szColName[0])))
53 {
54 lvc.cx = CX_TYPECOLUMN_WIDTH;
55 (void)ListView_InsertColumn(hWndDevList,
56 iCol++,
57 &lvc);
58 }
59 if (LoadString(hDllInstance,
60 IDS_SETTING_COLUMN,
61 szColName,
62 sizeof(szColName) / sizeof(szColName[0])))
63 {
64 lvc.cx = rcClient.right - CX_TYPECOLUMN_WIDTH -
65 GetSystemMetrics(SM_CXVSCROLL);
66
67 (void)ListView_InsertColumn(hWndDevList,
68 iCol++,
69 &lvc);
70 }
71}
72
73VOID
74InsertListItem(
75 IN HWND hWndDevList,
76 IN INT ItemCount,
77 IN LPWSTR ResourceType,
78 IN LPWSTR ResourceDescription)
79{
80 INT iItem;
81 LVITEM li = {0};
82
83 li.mask = LVIF_STATE | LVIF_TEXT;
84 li.iItem = ItemCount;
85 li.pszText = ResourceType;
86 //li.iImage = ClassDevInfo->ImageIndex;
87 iItem = ListView_InsertItem(hWndDevList, &li);
88
89 if (iItem != -1)
90 {
91 li.mask = LVIF_TEXT;
92 li.iItem = iItem;
93 li.iSubItem = 1;
94 li.pszText = ResourceDescription;
95 (void)ListView_SetItem(hWndDevList, &li);
96 }
97}
98
99VOID
100AddResourceItems(
101 IN PDEVADVPROP_INFO dap,
102 IN HWND hWndDevList)
103{
104 WCHAR szBuffer[100];
105 WCHAR szDetail[100];
106 PCM_RESOURCE_LIST ResourceList;
107 ULONG ItemCount = 0, Index;
108
109 ResourceList = (PCM_RESOURCE_LIST)dap->pResourceList;
110
111 for (Index = 0; Index < ResourceList->List[0].PartialResourceList.Count; Index++)
112 {
113 PCM_PARTIAL_RESOURCE_DESCRIPTOR Descriptor = &ResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
114 if (Descriptor->Type == CmResourceTypeInterrupt)
115 {
116 if (LoadString(hDllInstance, IDS_RESOURCE_INTERRUPT, szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])))
117 {
118 wsprintf(szDetail, L"0x%08x (%d)", Descriptor->u.Interrupt.Level, Descriptor->u.Interrupt.Vector);
119 InsertListItem(hWndDevList, ItemCount, szBuffer, szDetail);
120 ItemCount++;
121 }
122 }
123 else if (Descriptor->Type == CmResourceTypePort)
124 {
125 if (LoadString(hDllInstance, IDS_RESOURCE_PORT, szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])))
126 {
127 wsprintf(szDetail, L"%08lx - %08lx", Descriptor->u.Port.Start.LowPart, Descriptor->u.Port.Start.LowPart + Descriptor->u.Port.Length - 1);
128 InsertListItem(hWndDevList, ItemCount, szBuffer, szDetail);
129 ItemCount++;
130 }
131 }
132 else if (Descriptor->Type == CmResourceTypeMemory)
133 {
134 if (LoadString(hDllInstance, IDS_RESOURCE_MEMORY_RANGE, szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])))
135 {
136 wsprintf(szDetail, L"%08I64x - %08I64x", Descriptor->u.Memory.Start.QuadPart, Descriptor->u.Memory.Start.QuadPart + Descriptor->u.Memory.Length - 1);
137 InsertListItem(hWndDevList, ItemCount, szBuffer, szDetail);
138 ItemCount++;
139 }
140 }
141 else if (Descriptor->Type == CmResourceTypeDma)
142 {
143 if (LoadString(hDllInstance, IDS_RESOURCE_DMA, szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])))
144 {
145 wsprintf(szDetail, L"%08ld", Descriptor->u.Dma.Channel);
146 InsertListItem(hWndDevList, ItemCount, szBuffer, szDetail);
147 ItemCount++;
148 }
149 }
150 }
151}
152
153
154static VOID
155UpdateDriverResourceDlg(IN HWND hwndDlg,
156 IN PDEVADVPROP_INFO dap)
157{
158 /* set the device image */
159 SendDlgItemMessage(hwndDlg,
160 IDC_DEVICON,
161 STM_SETICON,
162 (WPARAM)dap->hDevIcon,
163 0);
164
165 /* set the device name edit control text */
166 SetDlgItemText(hwndDlg,
167 IDC_DEVNAME,
168 dap->szDevName);
169}
170
171INT_PTR
172CALLBACK
173ResourcesProcDriverDlgProc(IN HWND hwndDlg,
174 IN UINT uMsg,
175 IN WPARAM wParam,
176 IN LPARAM lParam)
177{
178 PDEVADVPROP_INFO hpd;
179 HWND hWndDevList;
180 INT_PTR Ret = FALSE;
181
182 hpd = (PDEVADVPROP_INFO)GetWindowLongPtr(hwndDlg, DWLP_USER);
183
184 if (hpd != NULL || uMsg == WM_INITDIALOG)
185 {
186 switch (uMsg)
187 {
188 case WM_INITDIALOG:
189 {
190 /* init list */
191 hWndDevList = GetDlgItem(hwndDlg, IDC_DRIVERRESOURCES);
192 InitializeDevicesList(hWndDevList);
193
194 hpd = (PDEVADVPROP_INFO)((LPPROPSHEETPAGE)lParam)->lParam;
195 if (hpd != NULL)
196 {
197 SetWindowLongPtr(hwndDlg, DWLP_USER, (DWORD_PTR)hpd);
198
199 UpdateDriverResourceDlg(hwndDlg, hpd);
200 AddResourceItems(hpd, hWndDevList);
201 }
202
203 Ret = TRUE;
204 break;
205 }
206 }
207 }
208
209 return Ret;
210}
211
212static
213PCM_RESOURCE_LIST
214GetAllocatedResourceList(
215 LPWSTR pszDeviceID)
216{
217 PCM_RESOURCE_LIST pResourceList = NULL;
218 HKEY hKey = NULL;
219 DWORD dwError, dwSize;
220
221 CStringW keyName = L"SYSTEM\\CurrentControlSet\\Enum\\";
222 keyName += pszDeviceID;
223 keyName += L"\\Control";
224
225 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &hKey);
226 if (dwError != ERROR_SUCCESS)
227 {
228 /* failed to open device instance log conf dir */
229 return NULL;
230 }
231
232 dwSize = 0;
233 RegQueryValueExW(hKey, L"AllocConfig", NULL, NULL, NULL, &dwSize);
234 if (dwSize == 0)
235 goto done;
236
237 pResourceList = static_cast<PCM_RESOURCE_LIST>(HeapAlloc(GetProcessHeap(), 0, dwSize));
238 if (pResourceList == NULL)
239 goto done;
240
241 dwError = RegQueryValueExW(hKey, L"AllocConfig", NULL, NULL, (LPBYTE)pResourceList, &dwSize);
242 if (dwError != ERROR_SUCCESS)
243 {
244 HeapFree(GetProcessHeap(), 0, pResourceList);
245 pResourceList = NULL;
246 }
247
248done:
249 if (hKey != NULL)
250 RegCloseKey(hKey);
251
252 return pResourceList;
253}
254
255static
256PCM_RESOURCE_LIST
257GetBootResourceList(
258 LPWSTR pszDeviceID)
259{
260 PCM_RESOURCE_LIST pResourceList = NULL;
261 HKEY hKey = NULL;
262 DWORD dwError, dwSize;
263
264 CStringW keyName = L"SYSTEM\\CurrentControlSet\\Enum\\";
265 keyName += pszDeviceID;
266 keyName += L"\\LogConf";
267
268 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &hKey);
269 if (dwError != ERROR_SUCCESS)
270 {
271 /* failed to open device instance log conf dir */
272 return NULL;
273 }
274
275 dwSize = 0;
276 RegQueryValueExW(hKey, L"BootConfig", NULL, NULL, NULL, &dwSize);
277 if (dwSize == 0)
278 goto done;
279
280 pResourceList = static_cast<PCM_RESOURCE_LIST>(HeapAlloc(GetProcessHeap(), 0, dwSize));
281 if (pResourceList == NULL)
282 goto done;
283
284 dwError = RegQueryValueExW(hKey, L"BootConfig", NULL, NULL, (LPBYTE)pResourceList, &dwSize);
285 if (dwError != ERROR_SUCCESS)
286 {
287 HeapFree(GetProcessHeap(), 0, pResourceList);
288 pResourceList = NULL;
289 }
290
291done:
292 if (hKey != NULL)
293 RegCloseKey(hKey);
294
295 return pResourceList;
296}
297
298PVOID
299GetResourceList(
300 LPWSTR pszDeviceID)
301{
302 PCM_RESOURCE_LIST pResourceList = NULL;
303
304 pResourceList = GetAllocatedResourceList(pszDeviceID);
305 if (pResourceList == NULL)
306 pResourceList = GetBootResourceList(pszDeviceID);
307
308 return (PVOID)pResourceList;
309}