dinput8/tests: Debug device information during enumeration callback.
[wine.git] / dlls / dinput8 / tests / dinput.c
bloba6cbd34bb244a42424d8ab3c76e1bc83520bc7f3
1 /*
2 * Copyright (c) 2011 Andrew Nguyen
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define DIRECTINPUT_VERSION 0x0800
21 #define COBJMACROS
22 #include <initguid.h>
23 #include <windows.h>
24 #include <dinput.h>
25 #include <dinputd.h>
27 #include "wine/test.h"
29 HINSTANCE hInstance;
31 static BOOL CALLBACK dummy_callback(const DIDEVICEINSTANCEA *instance, void *context)
33 ok(0, "Callback was invoked with parameters (%p, %p)\n", instance, context);
34 return DIENUM_STOP;
37 static void test_preinitialization(void)
39 static const struct
41 REFGUID rguid;
42 BOOL pdev;
43 HRESULT expected_hr;
44 } create_device_tests[] =
46 {NULL, FALSE, E_POINTER},
47 {NULL, TRUE, E_POINTER},
48 {&GUID_Unknown, FALSE, E_POINTER},
49 {&GUID_Unknown, TRUE, DIERR_NOTINITIALIZED},
50 {&GUID_SysMouse, FALSE, E_POINTER},
51 {&GUID_SysMouse, TRUE, DIERR_NOTINITIALIZED},
54 static const struct
56 DWORD dwDevType;
57 LPDIENUMDEVICESCALLBACKA lpCallback;
58 DWORD dwFlags;
59 HRESULT expected_hr;
60 int todo;
61 } enum_devices_tests[] =
63 {0, NULL, 0, DIERR_INVALIDPARAM},
64 {0, NULL, ~0u, DIERR_INVALIDPARAM},
65 {0, dummy_callback, 0, DIERR_NOTINITIALIZED},
66 {0, dummy_callback, ~0u, DIERR_INVALIDPARAM},
67 {0xdeadbeef, NULL, 0, DIERR_INVALIDPARAM},
68 {0xdeadbeef, NULL, ~0u, DIERR_INVALIDPARAM},
69 {0xdeadbeef, dummy_callback, 0, DIERR_INVALIDPARAM},
70 {0xdeadbeef, dummy_callback, ~0u, DIERR_INVALIDPARAM},
73 IDirectInput8A *pDI;
74 HRESULT hr;
75 int i;
76 IDirectInputDevice8A *pDID;
78 hr = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInput8A, (void **)&pDI);
79 if (FAILED(hr))
81 skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
82 return;
85 for (i = 0; i < sizeof(create_device_tests)/sizeof(create_device_tests[0]); i++)
87 if (create_device_tests[i].pdev) pDID = (void *)0xdeadbeef;
88 hr = IDirectInput8_CreateDevice(pDI, create_device_tests[i].rguid,
89 create_device_tests[i].pdev ? &pDID : NULL,
90 NULL);
91 ok(hr == create_device_tests[i].expected_hr, "[%d] IDirectInput8_CreateDevice returned 0x%08x\n", i, hr);
92 if (create_device_tests[i].pdev)
93 ok(pDID == NULL, "[%d] Output interface pointer is %p\n", i, pDID);
96 for (i = 0; i < sizeof(enum_devices_tests)/sizeof(enum_devices_tests[0]); i++)
98 hr = IDirectInput8_EnumDevices(pDI, enum_devices_tests[i].dwDevType,
99 enum_devices_tests[i].lpCallback,
100 NULL,
101 enum_devices_tests[i].dwFlags);
102 todo_wine_if(enum_devices_tests[i].todo)
103 ok(hr == enum_devices_tests[i].expected_hr, "[%d] IDirectInput8_EnumDevice returned 0x%08x\n", i, hr);
106 hr = IDirectInput8_GetDeviceStatus(pDI, NULL);
107 ok(hr == E_POINTER, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
109 hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_Unknown);
110 ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
112 hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_SysMouse);
113 ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
115 hr = IDirectInput8_RunControlPanel(pDI, NULL, 0);
116 ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
118 hr = IDirectInput8_RunControlPanel(pDI, NULL, ~0u);
119 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
121 hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, 0);
122 ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
124 hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, ~0u);
125 ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
127 IDirectInput8_Release(pDI);
130 static void test_DirectInput8Create(void)
132 static const struct
134 BOOL hinst;
135 DWORD dwVersion;
136 REFIID riid;
137 BOOL ppdi;
138 HRESULT expected_hr;
139 } invalid_param_list[] =
141 {FALSE, 0, &IID_IDirectInputA, FALSE, E_POINTER},
142 {FALSE, 0, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
143 {FALSE, 0, &IID_IDirectInput8A, FALSE, E_POINTER},
144 {FALSE, 0, &IID_IDirectInput8A, TRUE, DIERR_INVALIDPARAM},
145 {FALSE, DIRECTINPUT_VERSION, &IID_IDirectInputA, FALSE, E_POINTER},
146 {FALSE, DIRECTINPUT_VERSION, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
147 {FALSE, DIRECTINPUT_VERSION, &IID_IDirectInput8A, FALSE, E_POINTER},
148 {FALSE, DIRECTINPUT_VERSION, &IID_IDirectInput8A, TRUE, DIERR_INVALIDPARAM},
149 {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, FALSE, E_POINTER},
150 {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
151 {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInput8A, FALSE, E_POINTER},
152 {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInput8A, TRUE, DIERR_INVALIDPARAM},
153 {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, FALSE, E_POINTER},
154 {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
155 {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInput8A, FALSE, E_POINTER},
156 {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInput8A, TRUE, DIERR_INVALIDPARAM},
157 {TRUE, 0, &IID_IDirectInputA, FALSE, E_POINTER},
158 {TRUE, 0, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
159 {TRUE, 0, &IID_IDirectInput8A, FALSE, E_POINTER},
160 {TRUE, 0, &IID_IDirectInput8A, TRUE, DIERR_NOTINITIALIZED},
161 {TRUE, DIRECTINPUT_VERSION, &IID_IDirectInputA, FALSE, E_POINTER},
162 {TRUE, DIRECTINPUT_VERSION, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
163 {TRUE, DIRECTINPUT_VERSION, &IID_IDirectInput8A, FALSE, E_POINTER},
164 {TRUE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, FALSE, E_POINTER},
165 {TRUE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
166 {TRUE, DIRECTINPUT_VERSION - 1, &IID_IDirectInput8A, FALSE, E_POINTER},
167 {TRUE, DIRECTINPUT_VERSION - 1, &IID_IDirectInput8A, TRUE, DIERR_BETADIRECTINPUTVERSION},
168 {TRUE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, FALSE, E_POINTER},
169 {TRUE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, TRUE, DIERR_NOINTERFACE},
170 {TRUE, DIRECTINPUT_VERSION + 1, &IID_IDirectInput8A, FALSE, E_POINTER},
171 {TRUE, DIRECTINPUT_VERSION + 1, &IID_IDirectInput8A, TRUE, DIERR_OLDDIRECTINPUTVERSION},
174 static REFIID no_interface_list[] = {&IID_IDirectInputA, &IID_IDirectInputW,
175 &IID_IDirectInput2A, &IID_IDirectInput2W,
176 &IID_IDirectInput7A, &IID_IDirectInput7W,
177 &IID_IDirectInputDeviceA, &IID_IDirectInputDeviceW,
178 &IID_IDirectInputDevice2A, &IID_IDirectInputDevice2W,
179 &IID_IDirectInputDevice7A, &IID_IDirectInputDevice7W,
180 &IID_IDirectInputDevice8A, &IID_IDirectInputDevice8W,
181 &IID_IDirectInputEffect};
183 static REFIID iid_list[] = {&IID_IUnknown, &IID_IDirectInput8A, &IID_IDirectInput8W};
185 int i;
186 IUnknown *pUnk;
187 HRESULT hr;
189 for (i = 0; i < sizeof(invalid_param_list)/sizeof(invalid_param_list[0]); i++)
191 if (invalid_param_list[i].ppdi) pUnk = (void *)0xdeadbeef;
192 hr = DirectInput8Create(invalid_param_list[i].hinst ? hInstance : NULL,
193 invalid_param_list[i].dwVersion,
194 invalid_param_list[i].riid,
195 invalid_param_list[i].ppdi ? (void **)&pUnk : NULL,
196 NULL);
197 ok(hr == invalid_param_list[i].expected_hr, "[%d] DirectInput8Create returned 0x%08x\n", i, hr);
198 if (invalid_param_list[i].ppdi)
199 ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
202 for (i = 0; i < sizeof(no_interface_list)/sizeof(no_interface_list[0]); i++)
204 pUnk = (void *)0xdeadbeef;
205 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, no_interface_list[i], (void **)&pUnk, NULL);
206 ok(hr == DIERR_NOINTERFACE, "[%d] DirectInput8Create returned 0x%08x\n", i, hr);
207 ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
210 for (i = 0; i < sizeof(iid_list)/sizeof(iid_list[0]); i++)
212 pUnk = NULL;
213 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, iid_list[i], (void **)&pUnk, NULL);
214 ok(hr == DI_OK, "[%d] DirectInput8Create returned 0x%08x\n", i, hr);
215 ok(pUnk != NULL, "[%d] Output interface pointer is NULL\n", i);
216 if (pUnk)
217 IUnknown_Release(pUnk);
221 static void test_QueryInterface(void)
223 static REFIID iid_list[] = {&IID_IUnknown, &IID_IDirectInput8A, &IID_IDirectInput8W, &IID_IDirectInputJoyConfig8};
225 static const struct
227 REFIID riid;
228 int test_todo;
229 } no_interface_list[] =
231 {&IID_IDirectInputA, 1},
232 {&IID_IDirectInputW, 1},
233 {&IID_IDirectInput2A, 1},
234 {&IID_IDirectInput2W, 1},
235 {&IID_IDirectInput7A, 1},
236 {&IID_IDirectInput7W, 1},
237 {&IID_IDirectInputDeviceA},
238 {&IID_IDirectInputDeviceW},
239 {&IID_IDirectInputDevice2A},
240 {&IID_IDirectInputDevice2W},
241 {&IID_IDirectInputDevice7A},
242 {&IID_IDirectInputDevice7W},
243 {&IID_IDirectInputDevice8A},
244 {&IID_IDirectInputDevice8W},
245 {&IID_IDirectInputEffect},
248 IDirectInput8A *pDI;
249 HRESULT hr;
250 IUnknown *pUnk;
251 int i;
253 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
254 if (FAILED(hr))
256 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
257 return;
260 hr = IDirectInput8_QueryInterface(pDI, NULL, NULL);
261 ok(hr == E_POINTER, "IDirectInput8_QueryInterface returned 0x%08x\n", hr);
263 pUnk = (void *)0xdeadbeef;
264 hr = IDirectInput8_QueryInterface(pDI, NULL, (void **)&pUnk);
265 ok(hr == E_POINTER, "IDirectInput8_QueryInterface returned 0x%08x\n", hr);
266 ok(pUnk == (void *)0xdeadbeef, "Output interface pointer is %p\n", pUnk);
268 hr = IDirectInput8_QueryInterface(pDI, &IID_IUnknown, NULL);
269 ok(hr == E_POINTER, "IDirectInput8_QueryInterface returned 0x%08x\n", hr);
271 for (i = 0; i < sizeof(iid_list)/sizeof(iid_list[0]); i++)
273 pUnk = NULL;
274 hr = IDirectInput8_QueryInterface(pDI, iid_list[i], (void **)&pUnk);
275 ok(hr == S_OK, "[%d] IDirectInput8_QueryInterface returned 0x%08x\n", i, hr);
276 ok(pUnk != NULL, "[%d] Output interface pointer is NULL\n", i);
277 if (pUnk)
279 int j;
280 for (j = 0; j < sizeof(iid_list)/sizeof(iid_list[0]); j++)
282 IUnknown *pUnk1 = NULL;
283 hr = IDirectInput8_QueryInterface(pUnk, iid_list[j], (void **)&pUnk1);
284 ok(hr == S_OK, "[%d] IDirectInput8_QueryInterface(pUnk) returned 0x%08x\n", j, hr);
285 ok(pUnk1 != NULL, "[%d] Output interface pointer is NULL\n", i);
286 if (pUnk1) IUnknown_Release(pUnk1);
288 IUnknown_Release(pUnk);
292 for (i = 0; i < sizeof(no_interface_list)/sizeof(no_interface_list[0]); i++)
294 pUnk = (void *)0xdeadbeef;
295 hr = IDirectInput8_QueryInterface(pDI, no_interface_list[i].riid, (void **)&pUnk);
296 if (no_interface_list[i].test_todo)
298 todo_wine
299 ok(hr == E_NOINTERFACE, "[%d] IDirectInput8_QueryInterface returned 0x%08x\n", i, hr);
300 todo_wine
301 ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
303 if (pUnk) IUnknown_Release(pUnk);
305 else
307 ok(hr == E_NOINTERFACE, "[%d] IDirectInput8_QueryInterface returned 0x%08x\n", i, hr);
308 ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
312 IDirectInput8_Release(pDI);
315 static void test_CreateDevice(void)
317 IDirectInput8A *pDI;
318 HRESULT hr;
319 IDirectInputDevice8A *pDID;
321 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
322 if (FAILED(hr))
324 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
325 return;
328 hr = IDirectInput8_CreateDevice(pDI, NULL, NULL, NULL);
329 ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
331 pDID = (void *)0xdeadbeef;
332 hr = IDirectInput8_CreateDevice(pDI, NULL, &pDID, NULL);
333 ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
334 ok(pDID == NULL, "Output interface pointer is %p\n", pDID);
336 hr = IDirectInput8_CreateDevice(pDI, &GUID_Unknown, NULL, NULL);
337 ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
339 pDID = (void *)0xdeadbeef;
340 hr = IDirectInput8_CreateDevice(pDI, &GUID_Unknown, &pDID, NULL);
341 ok(hr == DIERR_DEVICENOTREG, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
342 ok(pDID == NULL, "Output interface pointer is %p\n", pDID);
344 hr = IDirectInput8_CreateDevice(pDI, &GUID_SysMouse, NULL, NULL);
345 ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
347 hr = IDirectInput8_CreateDevice(pDI, &GUID_SysMouse, &pDID, NULL);
348 ok(hr == DI_OK, "IDirectInput8_CreateDevice returned 0x%08x\n", hr);
350 IDirectInputDevice_Release(pDID);
351 IDirectInput8_Release(pDI);
354 struct enum_devices_test
356 unsigned int device_count;
357 BOOL return_value;
360 static BOOL CALLBACK enum_devices_callback(const DIDEVICEINSTANCEA *instance, void *context)
362 struct enum_devices_test *enum_test = context;
364 trace("---- Device Information ----\n"
365 "Product Name : %s\n"
366 "Instance Name : %s\n"
367 "devType : 0x%08x\n"
368 "GUID Product : %s\n"
369 "GUID Instance : %s\n"
370 "HID Page : 0x%04x\n"
371 "HID Usage : 0x%04x\n",
372 instance->tszProductName,
373 instance->tszInstanceName,
374 instance->dwDevType,
375 wine_dbgstr_guid(&instance->guidProduct),
376 wine_dbgstr_guid(&instance->guidInstance),
377 instance->wUsagePage,
378 instance->wUsage);
380 enum_test->device_count++;
381 return enum_test->return_value;
384 static void test_EnumDevices(void)
386 IDirectInput8A *pDI;
387 HRESULT hr;
388 struct enum_devices_test enum_test, enum_test_return;
390 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
391 if (FAILED(hr))
393 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
394 return;
397 hr = IDirectInput8_EnumDevices(pDI, 0, NULL, NULL, 0);
398 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
400 hr = IDirectInput8_EnumDevices(pDI, 0, NULL, NULL, ~0u);
401 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
403 /* Test crashes on Wine. */
404 if (0)
406 hr = IDirectInput8_EnumDevices(pDI, 0, enum_devices_callback, NULL, ~0u);
407 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
410 hr = IDirectInput8_EnumDevices(pDI, 0xdeadbeef, NULL, NULL, 0);
411 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
413 hr = IDirectInput8_EnumDevices(pDI, 0xdeadbeef, NULL, NULL, ~0u);
414 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
416 hr = IDirectInput8_EnumDevices(pDI, 0xdeadbeef, enum_devices_callback, NULL, 0);
417 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
419 hr = IDirectInput8_EnumDevices(pDI, 0xdeadbeef, enum_devices_callback, NULL, ~0u);
420 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
422 enum_test.device_count = 0;
423 enum_test.return_value = DIENUM_CONTINUE;
424 hr = IDirectInput8_EnumDevices(pDI, 0, enum_devices_callback, &enum_test, 0);
425 ok(hr == DI_OK, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
426 ok(enum_test.device_count != 0, "Device count is %u\n", enum_test.device_count);
428 /* Enumeration only stops with an explicit DIENUM_STOP. */
429 enum_test_return.device_count = 0;
430 enum_test_return.return_value = 42;
431 hr = IDirectInput8_EnumDevices(pDI, 0, enum_devices_callback, &enum_test_return, 0);
432 ok(hr == DI_OK, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
433 ok(enum_test_return.device_count == enum_test.device_count,
434 "Device count is %u vs. %u\n", enum_test_return.device_count, enum_test.device_count);
436 enum_test.device_count = 0;
437 enum_test.return_value = DIENUM_STOP;
438 hr = IDirectInput8_EnumDevices(pDI, 0, enum_devices_callback, &enum_test, 0);
439 ok(hr == DI_OK, "IDirectInput8_EnumDevices returned 0x%08x\n", hr);
440 ok(enum_test.device_count == 1, "Device count is %u\n", enum_test.device_count);
442 IDirectInput8_Release(pDI);
445 struct enum_semantics_test
447 unsigned int device_count;
448 BOOL mouse;
449 BOOL keyboard;
450 DIACTIONFORMATA *lpdiaf;
451 const char* username;
454 static DIACTIONA actionMapping[]=
456 /* axis */
457 { 0, 0x01008A01 /* DIAXIS_DRIVINGR_STEER */, 0, { "Steer.\0" } },
458 /* button */
459 { 1, 0x01000C01 /* DIBUTTON_DRIVINGR_SHIFTUP */, 0, { "Upshift.\0" } },
460 /* keyboard key */
461 { 2, DIKEYBOARD_SPACE, 0, { "Missile.\0" } },
462 /* mouse button */
463 { 3, DIMOUSE_BUTTON0, 0, { "Select\0" } },
464 /* mouse axis */
465 { 4, DIMOUSE_YAXIS, 0, { "Y Axis\0" } }
467 /* By placing the memory pointed to by lptszActionName right before memory with PAGE_NOACCESS
468 * one can find out that the regular ansi string termination is not respected by EnumDevicesBySemantics.
469 * Adding a double termination, making it a valid wide string termination, made the test succeed.
470 * Therefore it looks like ansi version of EnumDevicesBySemantics forwards the string to
471 * the wide variant without conversation. */
473 static BOOL CALLBACK enum_semantics_callback(const DIDEVICEINSTANCEA *lpddi, IDirectInputDevice8A *lpdid, DWORD dwFlags, DWORD dwRemaining, void *context)
475 struct enum_semantics_test *data = context;
477 if (context == NULL) return DIENUM_STOP;
479 data->device_count++;
481 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard)) data->keyboard = TRUE;
483 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse)) data->mouse = TRUE;
485 return DIENUM_CONTINUE;
488 static BOOL CALLBACK set_action_map_callback(const DIDEVICEINSTANCEA *lpddi, IDirectInputDevice8A *lpdid, DWORD dwFlags, DWORD dwRemaining, void *context)
490 HRESULT hr;
491 struct enum_semantics_test *data = context;
493 /* Building and setting an action map */
494 /* It should not use any pre-stored mappings so we use DIDBAM_INITIALIZE */
495 hr = IDirectInputDevice8_BuildActionMap(lpdid, data->lpdiaf, NULL, DIDBAM_INITIALIZE);
496 ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
498 hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, data->username, 0);
499 ok (SUCCEEDED(hr), "SetActionMap failed hr=%08x\n", hr);
501 return DIENUM_CONTINUE;
504 static void test_EnumDevicesBySemantics(void)
506 IDirectInput8A *pDI;
507 HRESULT hr;
508 DIACTIONFORMATA diaf;
509 const GUID ACTION_MAPPING_GUID = { 0x1, 0x2, 0x3, { 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb } };
510 struct enum_semantics_test data = { 0, FALSE, FALSE, &diaf, NULL };
511 int device_total = 0;
513 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
514 if (FAILED(hr))
516 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
517 return;
520 memset (&diaf, 0, sizeof(diaf));
521 diaf.dwSize = sizeof(diaf);
522 diaf.dwActionSize = sizeof(DIACTIONA);
523 diaf.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
524 diaf.dwDataSize = 4 * diaf.dwNumActions;
525 diaf.rgoAction = actionMapping;
526 diaf.guidActionMap = ACTION_MAPPING_GUID;
527 diaf.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
528 diaf.dwBufferSize = 32;
530 /* Test enumerating all attached and installed devices */
531 data.keyboard = FALSE;
532 data.mouse = FALSE;
533 data.device_count = 0;
534 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, &data, DIEDBSFL_ATTACHEDONLY);
535 ok (data.device_count > 0, "EnumDevicesBySemantics did not call the callback hr=%08x\n", hr);
536 ok (data.keyboard, "EnumDevicesBySemantics should enumerate the keyboard\n");
537 ok (data.mouse, "EnumDevicesBySemantics should enumerate the mouse\n");
539 /* Enumerate Force feedback devices. We should get no mouse nor keyboard */
540 data.keyboard = FALSE;
541 data.mouse = FALSE;
542 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, &data, DIEDBSFL_FORCEFEEDBACK);
543 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
544 ok (!data.keyboard, "Keyboard should not be enumerated when asking for forcefeedback\n");
545 ok (!data.mouse, "Mouse should not be enumerated when asking for forcefeedback\n");
547 /* Enumerate available devices. That is devices not owned by any user.
548 Before setting the action map for all devices we still have them available. */
549 data.device_count = 0;
550 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, &data, DIEDBSFL_AVAILABLEDEVICES);
551 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
552 ok (data.device_count > 0, "There should be devices available before action mapping available=%d\n", data.device_count);
554 /* Keep the device total */
555 device_total = data.device_count;
557 /* This enumeration builds and sets the action map for all devices with a NULL username */
558 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, set_action_map_callback, &data, DIEDBSFL_ATTACHEDONLY);
559 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
561 /* After a successful action mapping we should have no devices available */
562 data.device_count = 0;
563 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, &data, DIEDBSFL_AVAILABLEDEVICES);
564 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
565 todo_wine ok (data.device_count == 0, "No device should be available after action mapping available=%d\n", data.device_count);
567 /* Now we'll give all the devices to a specific user */
568 data.username = "Sh4d0w M4g3";
569 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, set_action_map_callback, &data, DIEDBSFL_ATTACHEDONLY);
570 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
572 /* Testing with the default user, DIEDBSFL_THISUSER has no effect */
573 data.device_count = 0;
574 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, &data, DIEDBSFL_THISUSER);
575 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
576 ok (data.device_count == device_total, "THISUSER has no effect with NULL username owned=%d, expected=%d\n", data.device_count, device_total);
578 /* Using an empty user string is the same as passing NULL, DIEDBSFL_THISUSER has no effect */
579 data.device_count = 0;
580 hr = IDirectInput8_EnumDevicesBySemantics(pDI, "", &diaf, enum_semantics_callback, &data, DIEDBSFL_THISUSER);
581 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
582 ok (data.device_count == device_total, "THISUSER has no effect with \"\" as username owned=%d, expected=%d\n", data.device_count, device_total);
584 /* Testing with a user with no ownership of the devices */
585 data.device_count = 0;
586 hr = IDirectInput8_EnumDevicesBySemantics(pDI, "Ninja Brian", &diaf, enum_semantics_callback, &data, DIEDBSFL_THISUSER);
587 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
588 todo_wine ok (data.device_count == 0, "This user should own no devices owned=%d\n", data.device_count);
590 /* Sh4d0w M4g3 has ownership of all devices */
591 data.device_count = 0;
592 hr = IDirectInput8_EnumDevicesBySemantics(pDI, "Sh4d0w M4g3", &diaf, enum_semantics_callback, &data, DIEDBSFL_THISUSER);
593 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
594 ok (data.device_count == device_total, "This user should own %d devices owned=%d\n", device_total, data.device_count);
596 /* The call fails with a zeroed GUID */
597 memset(&diaf.guidActionMap, 0, sizeof(GUID));
598 hr = IDirectInput8_EnumDevicesBySemantics(pDI, NULL, &diaf, enum_semantics_callback, NULL, 0);
599 todo_wine ok(FAILED(hr), "EnumDevicesBySemantics succeeded with invalid GUID hr=%08x\n", hr);
601 IDirectInput8_Release(pDI);
604 static void test_GetDeviceStatus(void)
606 IDirectInput8A *pDI;
607 HRESULT hr;
609 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
610 if (FAILED(hr))
612 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
613 return;
616 hr = IDirectInput8_GetDeviceStatus(pDI, NULL);
617 ok(hr == E_POINTER, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
619 hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_Unknown);
620 todo_wine
621 ok(hr == DIERR_DEVICENOTREG, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
623 hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_SysMouse);
624 ok(hr == DI_OK, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr);
626 IDirectInput8_Release(pDI);
629 static void test_RunControlPanel(void)
631 IDirectInput8A *pDI;
632 HRESULT hr;
634 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
635 if (FAILED(hr))
637 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
638 return;
641 if (winetest_interactive)
643 hr = IDirectInput8_RunControlPanel(pDI, NULL, 0);
644 ok(hr == S_OK, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
646 hr = IDirectInput8_RunControlPanel(pDI, GetDesktopWindow(), 0);
647 ok(hr == S_OK, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
650 hr = IDirectInput8_RunControlPanel(pDI, NULL, ~0u);
651 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
653 hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, 0);
654 ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
656 hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, ~0u);
657 ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr);
659 IDirectInput8_Release(pDI);
662 static void test_Initialize(void)
664 IDirectInput8A *pDI;
665 HRESULT hr;
667 hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL);
668 if (FAILED(hr))
670 win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
671 return;
674 hr = IDirectInput8_Initialize(pDI, NULL, 0);
675 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_Initialize returned 0x%08x\n", hr);
677 hr = IDirectInput8_Initialize(pDI, NULL, DIRECTINPUT_VERSION);
678 ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_Initialize returned 0x%08x\n", hr);
680 hr = IDirectInput8_Initialize(pDI, hInstance, 0);
681 ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_Initialize returned 0x%08x\n", hr);
683 /* Invalid DirectInput versions less than DIRECTINPUT_VERSION yield DIERR_BETADIRECTINPUTVERSION. */
684 hr = IDirectInput8_Initialize(pDI, hInstance, DIRECTINPUT_VERSION - 1);
685 ok(hr == DIERR_BETADIRECTINPUTVERSION, "IDirectInput8_Initialize returned 0x%08x\n", hr);
687 /* Invalid DirectInput versions greater than DIRECTINPUT_VERSION yield DIERR_BETADIRECTINPUTVERSION. */
688 hr = IDirectInput8_Initialize(pDI, hInstance, DIRECTINPUT_VERSION + 1);
689 ok(hr == DIERR_OLDDIRECTINPUTVERSION, "IDirectInput8_Initialize returned 0x%08x\n", hr);
691 hr = IDirectInput8_Initialize(pDI, hInstance, DIRECTINPUT_VERSION);
692 ok(hr == DI_OK, "IDirectInput8_Initialize returned 0x%08x\n", hr);
694 /* Parameters are still validated after successful initialization. */
695 hr = IDirectInput8_Initialize(pDI, hInstance, 0);
696 ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_Initialize returned 0x%08x\n", hr);
698 IDirectInput8_Release(pDI);
701 START_TEST(dinput)
703 hInstance = GetModuleHandleA(NULL);
705 CoInitialize(NULL);
706 test_preinitialization();
707 test_DirectInput8Create();
708 test_QueryInterface();
709 test_CreateDevice();
710 test_EnumDevices();
711 test_EnumDevicesBySemantics();
712 test_GetDeviceStatus();
713 test_RunControlPanel();
714 test_Initialize();
715 CoUninitialize();