]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/d3dframe/ddenum.cpp
Fix mismatched arg types in the win32 code (d1x r1.7)
[btb/d2x.git] / arch / win32 / d3dframe / ddenum.cpp
1 //-----------------------------------------------------------------------------
2 // File: ddenum.cpp
3 //
4 // Desc: This sample demonstrates how to enumerate all of the devices and show
5 //       the driver information about each.
6 //
7 //
8 // Copyright (c) 1998 Microsoft Corporation. All rights reserved.
9 //-----------------------------------------------------------------------------
10
11 #ifndef WIN32_LEAN_AND_MEAN
12 #define WIN32_LEAN_AND_MEAN
13 #endif
14 //-----------------------------------------------------------------------------
15 // Include files
16 //-----------------------------------------------------------------------------
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <windows.h>
20 #include <ddraw.h>
21 #include "resource.h"
22
23 //-----------------------------------------------------------------------------
24 // Local definitions
25 //-----------------------------------------------------------------------------
26 #define NAME            "DDEnum"
27 #define TITLE           "DirectDraw Enumeration Example"
28
29 //-----------------------------------------------------------------------------
30 // Default settings
31 //-----------------------------------------------------------------------------
32 #define MAX_DEVICES     16
33
34 //-----------------------------------------------------------------------------
35 // Global Variables
36 //-----------------------------------------------------------------------------
37 int                     g_iMaxDevices = 0;
38 struct{
39     DDDEVICEIDENTIFIER  DeviceInfo;
40     DDDEVICEIDENTIFIER  DeviceInfoHost;
41 } g_DeviceInfo[MAX_DEVICES];
42
43
44
45
46 //-----------------------------------------------------------------------------
47 // Name: InitFail()
48 // Desc: This function is called if an initialization function fails
49 //-----------------------------------------------------------------------------
50 void
51 InitFail(LPCTSTR szError, ...)
52 {
53     char            szBuff[128];
54     va_list         vl;
55
56     va_start(vl, szError);
57     vsprintf(szBuff, szError, vl);
58     MessageBox(NULL, szBuff, TITLE, MB_OK);
59     va_end(vl);
60 }
61
62
63
64
65 //-----------------------------------------------------------------------------
66 // Name: SetInfoDlgText()
67 // Desc: Update all of the text and buttons in the dialog
68 //-----------------------------------------------------------------------------
69 void
70 SetInfoDlgText(HWND hDlg, int iCurrent, DWORD dwHost)
71 {
72     char        szBuff[128];
73     GUID        *pGUID;
74     LPDDDEVICEIDENTIFIER pDI;
75
76     if (dwHost == DDGDI_GETHOSTIDENTIFIER)
77         CheckRadioButton( hDlg, IDC_RADIO_DEVICE, IDC_RADIO_HOST, IDC_RADIO_HOST );
78     else
79         CheckRadioButton( hDlg, IDC_RADIO_DEVICE, IDC_RADIO_DEVICE, IDC_RADIO_DEVICE );
80
81     pDI = &g_DeviceInfo[iCurrent].DeviceInfo;
82     if (dwHost == DDGDI_GETHOSTIDENTIFIER)
83         pDI = &g_DeviceInfo[iCurrent].DeviceInfoHost;
84
85     wsprintf(szBuff, "Device information for device %d of %d",iCurrent + 1,g_iMaxDevices);
86     SetDlgItemText( hDlg, IDC_RADIO_DEVICE, szBuff);
87
88     //Device ID stuff:
89     wsprintf(szBuff,"%08X",pDI->dwVendorId);
90     SetDlgItemText( hDlg, IDC_DWVENDORID, szBuff);
91     wsprintf(szBuff,"%08X",pDI->dwDeviceId);
92     SetDlgItemText( hDlg, IDC_DWDEVICEID, szBuff);
93     wsprintf(szBuff,"%08X",pDI->dwSubSysId);
94     SetDlgItemText( hDlg, IDC_DWSUBSYS, szBuff);
95     wsprintf(szBuff,"%08X",pDI->dwRevision);
96     SetDlgItemText( hDlg, IDC_DWREVISION, szBuff);
97
98     //Driver version:
99     wsprintf(szBuff,"%d.%02d.%02d.%04d",
100         HIWORD(pDI->liDriverVersion.u.HighPart),
101         LOWORD(pDI->liDriverVersion.u.HighPart),
102         HIWORD(pDI->liDriverVersion.u.LowPart),
103         LOWORD(pDI->liDriverVersion.u.LowPart) );
104     SetDlgItemText( hDlg, IDC_VERSION, szBuff);
105
106     //Device description and HAL filename
107     SetDlgItemText( hDlg, IDC_DESCRIPTION, pDI->szDescription);
108     SetDlgItemText( hDlg, IDC_FILENAME, pDI->szDriver);
109
110     //Unique driver/device identifier:
111     pGUID = &pDI->guidDeviceIdentifier;
112     wsprintf(szBuff, "%08X-%04X-%04X-%02X%02X%02X%02X%02X%02X%02X%02X",
113              pGUID->Data1, pGUID->Data2, pGUID->Data3,
114              pGUID->Data4[0], pGUID->Data4[1], pGUID->Data4[2], pGUID->Data4[3],
115              pGUID->Data4[4], pGUID->Data4[5], pGUID->Data4[6], pGUID->Data4[7]);
116     SetDlgItemText( hDlg, IDC_GUID, szBuff);
117
118     // Change the state and style of the Prev and Next buttons if needed
119     if (0 == iCurrent)
120     {
121         // The Prev button should be disabled
122         SetFocus(GetDlgItem(hDlg, IDOK));
123         SendDlgItemMessage( hDlg, IDC_PREV, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
124         SendMessage(hDlg, DM_SETDEFID, IDOK, 0);
125         EnableWindow(GetDlgItem(hDlg, IDC_PREV), FALSE);
126     }
127     else
128         if (IsWindowEnabled(GetDlgItem(hDlg, IDC_PREV)) == FALSE)
129             EnableWindow(GetDlgItem(hDlg, IDC_PREV), TRUE);
130
131     if (iCurrent >= (g_iMaxDevices - 1))
132     {
133         // The Next button should be disabled
134         SetFocus(GetDlgItem(hDlg, IDOK));
135         SendDlgItemMessage( hDlg, IDC_NEXT, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
136         SendMessage(hDlg, DM_SETDEFID, IDOK, 0);
137         EnableWindow(GetDlgItem(hDlg, IDC_NEXT), FALSE);
138     }
139     else
140         if (IsWindowEnabled(GetDlgItem(hDlg, IDC_NEXT)) == FALSE)
141             EnableWindow(GetDlgItem(hDlg, IDC_NEXT), TRUE);
142 }
143
144
145
146
147 //-----------------------------------------------------------------------------
148 // Name: InfoDlgProc()
149 // Desc: The dialog window proc
150 //-----------------------------------------------------------------------------
151 LRESULT CALLBACK
152 InfoDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
153 {
154 //    RECT            rc;
155     static int      iCurrent = 0;
156     static DWORD    dwHost = 0;
157
158     switch (uMsg)
159     {
160         case WM_INITDIALOG:
161             // Setup the first devices text
162             SetInfoDlgText(hDlg, iCurrent, dwHost);
163             return TRUE;
164         case WM_COMMAND:
165             switch (HIWORD(wParam))
166             {
167                 case BN_CLICKED:
168                     switch (LOWORD(wParam))
169                     {
170                         case IDOK:
171                         case IDCANCEL:
172                             EndDialog(hDlg, TRUE);
173                             return TRUE;
174                         case IDC_PREV:
175                             // Show the previous device
176                             if (iCurrent)
177                                 iCurrent--;
178                             SetInfoDlgText(hDlg, iCurrent, dwHost);
179                             break;
180                         case IDC_NEXT:
181                             // Show the next device
182                             if (iCurrent < g_iMaxDevices)
183                                 iCurrent++;
184                             SetInfoDlgText(hDlg, iCurrent, dwHost);
185                             break;
186                         case IDC_RADIO_HOST:
187                         case IDC_RADIO_DEVICE:
188                             if (dwHost == DDGDI_GETHOSTIDENTIFIER )
189                                 dwHost = 0;
190                             else
191                                 dwHost = DDGDI_GETHOSTIDENTIFIER;
192                             SetInfoDlgText(hDlg, iCurrent, dwHost);
193                             break;
194                     }
195                     break;
196             }
197             break;
198     }
199
200     return FALSE;
201 }
202
203
204
205
206 //-----------------------------------------------------------------------------
207 // Name: DDEnumCallbackEx()
208 // Desc: This callback gets the information for each device enumerated
209 //-----------------------------------------------------------------------------
210 BOOL WINAPI 
211 DDEnumCallbackEx(GUID *pGUID, LPSTR pDescription, LPSTR pName,
212                  LPVOID pContext, HMONITOR hm)
213 {
214     LPDIRECTDRAW            pDD;            // DD1 interface, used to get DD4 interface
215     LPDIRECTDRAW4           pDD4 = NULL;        // DirectDraw4 interface
216     HRESULT                 hRet;
217
218     // Create the main DirectDraw object
219     hRet = DirectDrawCreate(pGUID, &pDD, NULL);
220     if (hRet != DD_OK)
221     {
222         InitFail("DirectDrawCreate FAILED");
223         return DDENUMRET_CANCEL;
224     }
225
226     // Fetch DirectDraw4 interface
227     hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *)&pDD4);
228     if (hRet != DD_OK)
229     {
230         InitFail("QueryInterface FAILED");
231         return DDENUMRET_CANCEL;
232     }
233
234     // Get the device information and save it
235     hRet = pDD4->GetDeviceIdentifier(&g_DeviceInfo[g_iMaxDevices].DeviceInfo,0);
236     hRet = pDD4->GetDeviceIdentifier(&g_DeviceInfo[g_iMaxDevices].DeviceInfoHost,DDGDI_GETHOSTIDENTIFIER);
237
238     // Finished with the DirectDraw object, so release it
239     if (pDD4)
240         pDD4->Release();
241
242     // Bump to the next open slot or finish the callbacks if full
243     if (g_iMaxDevices < MAX_DEVICES)
244         g_iMaxDevices++;
245     else
246         return DDENUMRET_CANCEL;
247     return DDENUMRET_OK;
248 }
249
250
251
252
253 //-----------------------------------------------------------------------------
254 // Name: DDEnumCallback()
255 // Desc: Old style callback retained for backwards compatibility
256 //-----------------------------------------------------------------------------
257 BOOL WINAPI 
258 DDEnumCallback(GUID *pGUID, LPSTR pDescription, LPSTR pName, LPVOID context)
259 {
260     return (DDEnumCallbackEx(pGUID, pDescription, pName, context, NULL));
261 }
262
263
264
265
266 //-----------------------------------------------------------------------------
267 // Name: WinMain()
268 // Desc: Entry point to the program. Initializes everything and calls
269 //       DirectDrawEnumerateEx() to get all of the device info.
270 //-----------------------------------------------------------------------------
271 int PASCAL
272 WinMain(HINSTANCE   hInstance,
273         HINSTANCE   hPrevInstance,
274         LPSTR       lpszCmdLine,
275         int         nCmdShow)
276 {
277     LPDIRECTDRAWENUMERATEEX pDirectDrawEnumerateEx;
278     HINSTANCE               hDDrawDLL;
279
280     // Do a GetModuleHandle and GetProcAddress in order to get the
281     // DirectDrawEnumerateEx function
282     hDDrawDLL = GetModuleHandle("DDRAW");
283     if (!hDDrawDLL)
284     {
285         InitFail("LoadLibrary() FAILED");
286         return -1;
287     }
288     pDirectDrawEnumerateEx = (LPDIRECTDRAWENUMERATEEX )GetProcAddress(hDDrawDLL,"DirectDrawEnumerateExA");
289     if (pDirectDrawEnumerateEx)
290     {
291         pDirectDrawEnumerateEx(DDEnumCallbackEx, NULL,
292                                 DDENUM_ATTACHEDSECONDARYDEVICES |
293                                 DDENUM_DETACHEDSECONDARYDEVICES |
294                                 DDENUM_NONDISPLAYDEVICES);
295     }
296     else
297     {
298         // Old DirectDraw, so do it the old way
299         DirectDrawEnumerate(DDEnumCallback, NULL);
300     }
301
302     if (0 == g_iMaxDevices)
303     {
304         InitFail("No devices to enumerate.");
305         return -1;
306     }
307
308     // Bring up the dialog to show all the devices
309     DialogBox(hInstance, MAKEINTRESOURCE(IDD_DRIVERINFO), GetDesktopWindow(), (DLGPROC) InfoDlgProc);
310
311     return 0;
312 }
313
314