dsound: Always enumerate the default device first.
[wine] / dlls / dinput8 / tests / device.c
1 /*
2  * Copyright (c) 2011 Lucas Fialho Zawacki
3  * Copyright (c) 2006 Vitaliy Margolen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define DIRECTINPUT_VERSION 0x0800
21
22 #define COBJMACROS
23 #include <windows.h>
24
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "dinput.h"
28
29 struct enum_data {
30     LPDIRECTINPUT8 pDI;
31     LPDIACTIONFORMAT lpdiaf;
32     LPDIRECTINPUTDEVICE8 keyboard;
33     LPDIRECTINPUTDEVICE8 mouse;
34     int ndevices;
35 };
36
37 /* Dummy GUID */
38 static const GUID ACTION_MAPPING_GUID = { 0x1, 0x2, 0x3, { 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb } };
39
40 enum {
41     DITEST_AXIS,
42     DITEST_BUTTON,
43     DITEST_KEYBOARDSPACE,
44     DITEST_MOUSEBUTTON0,
45     DITEST_YAXIS
46 };
47
48 static DIACTION actionMapping[]=
49 {
50   /* axis */
51   { 0, 0x01008A01 /* DIAXIS_DRIVINGR_STEER */ , 0, { "Steer" } },
52   /* button */
53   { 1, 0x01000C01 /* DIBUTTON_DRIVINGR_SHIFTUP */ , 0, { "Upshift" } },
54   /* keyboard key */
55   { 2, DIKEYBOARD_SPACE , 0, { "Missile" } },
56   /* mouse button */
57   { 3, DIMOUSE_BUTTON0, 0, { "Select" } },
58   /* mouse axis */
59   { 4, DIMOUSE_YAXIS, 0, { "Y Axis" } }
60 };
61
62 static void test_device_input(
63     LPDIRECTINPUTDEVICE8 lpdid,
64     DWORD event_type,
65     DWORD event,
66     DWORD expected
67 )
68 {
69     HRESULT hr;
70     DIDEVICEOBJECTDATA obj_data;
71     DWORD data_size = 1;
72     int i;
73
74     hr = IDirectInputDevice8_Acquire(lpdid);
75     ok (SUCCEEDED(hr), "Failed to acquire device hr=%08x\n", hr);
76
77     if (event_type == INPUT_KEYBOARD)
78         keybd_event( event, DIK_SPACE, 0, 0);
79
80     if (event_type == INPUT_MOUSE)
81         mouse_event( event, 0, 0, 0, 0);
82
83     IDirectInputDevice8_Poll(lpdid);
84     hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
85
86     if (data_size != 1)
87     {
88         win_skip("We're not able to inject input into Windows dinput8 with events\n");
89         return;
90     }
91
92     ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data.uAppData, expected);
93
94     /* Check for buffer owerflow */
95     for (i = 0; i < 17; i++)
96         if (event_type == INPUT_KEYBOARD)
97         {
98             keybd_event( VK_SPACE, DIK_SPACE, 0, 0);
99             keybd_event( VK_SPACE, DIK_SPACE, KEYEVENTF_KEYUP, 0);
100         }
101         else if (event_type == INPUT_MOUSE)
102         {
103             mouse_event(MOUSEEVENTF_LEFTDOWN, 1, 1, 0, 0);
104             mouse_event(MOUSEEVENTF_LEFTUP, 1, 1, 0, 0);
105         }
106
107     IDirectInputDevice8_Poll(lpdid);
108
109     data_size = 1;
110     hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
111     ok(hr == DI_BUFFEROVERFLOW, "GetDeviceData() failed: %08x\n", hr);
112     data_size = 1;
113     hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
114     ok(hr == DI_OK && data_size == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, data_size);
115 }
116
117 static void test_build_action_map(
118     LPDIRECTINPUTDEVICE8 lpdid,
119     LPDIACTIONFORMAT lpdiaf,
120     int action_index,
121     DWORD expected_type,
122     DWORD expected_inst
123 )
124 {
125     HRESULT hr;
126     DIACTION *actions;
127     DWORD instance, type, how;
128     GUID assigned_to;
129     DIDEVICEINSTANCEA ddi;
130
131     ddi.dwSize = sizeof(ddi);
132     IDirectInputDevice_GetDeviceInfo(lpdid, &ddi);
133
134     hr = IDirectInputDevice8_BuildActionMap(lpdid, lpdiaf, NULL, DIDBAM_INITIALIZE);
135     ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
136
137     actions = lpdiaf->rgoAction;
138     instance = DIDFT_GETINSTANCE(actions[action_index].dwObjID);
139     type = DIDFT_GETTYPE(actions[action_index].dwObjID);
140     how = actions[action_index].dwHow;
141     assigned_to = actions[action_index].guidInstance;
142
143     ok (how == DIAH_USERCONFIG || how == DIAH_DEFAULT, "Action was not set dwHow=%08x\n", how);
144     ok (instance == expected_inst, "Action not mapped correctly instance=%08x expected=%08x\n", instance, expected_inst);
145     ok (type == expected_type, "Action type not mapped correctly type=%08x expected=%08x\n", type, expected_type);
146     ok (IsEqualGUID(&assigned_to, &ddi.guidInstance), "Action and device GUID do not match action=%d\n", action_index);
147 }
148
149 static BOOL CALLBACK enumeration_callback(
150     LPCDIDEVICEINSTANCE lpddi,
151     LPDIRECTINPUTDEVICE8 lpdid,
152     DWORD dwFlags,
153     DWORD dwRemaining,
154     LPVOID pvRef)
155 {
156     HRESULT hr;
157     DIPROPDWORD dp;
158     DIPROPRANGE dpr;
159     struct enum_data *data = pvRef;
160     DWORD cnt;
161     DIDEVICEOBJECTDATA buffer[5];
162
163     if (!data) return DIENUM_CONTINUE;
164
165     data->ndevices++;
166
167     /* collect the mouse and keyboard */
168     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
169     {
170         IDirectInputDevice_AddRef(lpdid);
171         data->keyboard = lpdid;
172
173         ok (dwFlags & DIEDBS_MAPPEDPRI1, "Keyboard should be mapped as pri1 dwFlags=%08x\n", dwFlags);
174     }
175
176     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
177     {
178         IDirectInputDevice_AddRef(lpdid);
179         data->mouse = lpdid;
180
181         ok (dwFlags & DIEDBS_MAPPEDPRI1, "Mouse should be mapped as pri1 dwFlags=%08x\n", dwFlags);
182     }
183
184     /* Building and setting an action map */
185     /* It should not use any pre-stored mappings so we use DIDBAM_INITIALIZE */
186     hr = IDirectInputDevice8_BuildActionMap(lpdid, data->lpdiaf, NULL, DIDBAM_INITIALIZE);
187     ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
188
189     /* Device has no data format and thus can't be acquired */
190     hr = IDirectInputDevice8_Acquire(lpdid);
191     ok (hr == DIERR_INVALIDPARAM, "Device was acquired before SetActionMap hr=%08x\n", hr);
192
193     hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
194     ok (SUCCEEDED(hr), "SetActionMap failed hr=%08x\n", hr);
195
196     /* Some joysticks may have no suitable actions and thus should not be tested */
197     if (hr == DI_NOEFFECT) return DIENUM_CONTINUE;
198
199     /* Test buffer size */
200     memset(&dp, 0, sizeof(dp));
201     dp.diph.dwSize = sizeof(dp);
202     dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
203     dp.diph.dwHow  = DIPH_DEVICE;
204
205     hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_BUFFERSIZE, &dp.diph);
206     ok (SUCCEEDED(hr), "GetProperty failed hr=%08x\n", hr);
207     ok (dp.dwData == data->lpdiaf->dwBufferSize, "SetActionMap must set the buffer, buffersize=%d\n", dp.dwData);
208
209     cnt = 1;
210     hr = IDirectInputDevice_GetDeviceData(lpdid, sizeof(buffer[0]), buffer, &cnt, 0);
211     ok(hr == DIERR_NOTACQUIRED, "GetDeviceData() failed hr=%08x\n", hr);
212
213     /* Test axis range */
214     memset(&dpr, 0, sizeof(dpr));
215     dpr.diph.dwSize = sizeof(dpr);
216     dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
217     dpr.diph.dwHow  = DIPH_DEVICE;
218
219     hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_RANGE, &dpr.diph);
220     /* Only test if device supports the range property */
221     if (SUCCEEDED(hr))
222     {
223         ok (dpr.lMin == data->lpdiaf->lAxisMin, "SetActionMap must set the min axis range expected=%d got=%d\n", data->lpdiaf->lAxisMin, dpr.lMin);
224         ok (dpr.lMax == data->lpdiaf->lAxisMax, "SetActionMap must set the max axis range expected=%d got=%d\n", data->lpdiaf->lAxisMax, dpr.lMax);
225     }
226
227     /* SetActionMap has set the data format so now it should work */
228     hr = IDirectInputDevice8_Acquire(lpdid);
229     ok (SUCCEEDED(hr), "Acquire failed hr=%08x\n", hr);
230
231     cnt = 1;
232     hr = IDirectInputDevice_GetDeviceData(lpdid, sizeof(buffer[0]), buffer, &cnt, 0);
233     ok(hr == DI_OK, "GetDeviceData() failed hr=%08x\n", hr);
234
235     /* SetActionMap should not work on an acquired device */
236     hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
237     ok (hr == DIERR_ACQUIRED, "SetActionMap succeeded with an acquired device hr=%08x\n", hr);
238
239     return DIENUM_CONTINUE;
240 }
241
242 /*  A simpler callback function used to count and check
243     the enumeration of devices.
244 */
245 static BOOL CALLBACK counting_callback(
246     LPCDIDEVICEINSTANCE lpddi,
247     LPDIRECTINPUTDEVICE8 lpdid,
248     DWORD dwFlags,
249     DWORD dwRemaining,
250     LPVOID pvRef)
251 {
252     struct enum_data *data = pvRef;
253     if (!data) return DIENUM_CONTINUE;
254
255     data->ndevices++;
256     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
257         data->keyboard = lpdid;
258
259     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
260         data->mouse = lpdid;
261
262     return DIENUM_CONTINUE;
263 }
264
265 static void test_action_mapping(void)
266 {
267     HRESULT hr;
268     HINSTANCE hinst = GetModuleHandle(NULL);
269     LPDIRECTINPUT8 pDI = NULL;
270     DIACTIONFORMAT af;
271     struct enum_data data =  {pDI, &af, NULL, NULL, 0};
272     struct enum_data count = {pDI, &af, NULL, NULL, 0};
273
274     hr = CoCreateInstance(&CLSID_DirectInput8, 0, 1, &IID_IDirectInput8A, (LPVOID*)&pDI);
275     if (hr == DIERR_OLDDIRECTINPUTVERSION ||
276         hr == DIERR_BETADIRECTINPUTVERSION ||
277         hr == REGDB_E_CLASSNOTREG)
278     {
279         win_skip("ActionMapping requires dinput8\n");
280         return;
281     }
282     ok(SUCCEEDED(hr), "DirectInput8 Create failed: hr=%08x\n", hr);
283     if (FAILED(hr)) return;
284
285     hr = IDirectInput8_Initialize(pDI,hinst, DIRECTINPUT_VERSION);
286     if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_BETADIRECTINPUTVERSION)
287     {
288         win_skip("ActionMapping requires dinput8\n");
289         return;
290     }
291     ok(SUCCEEDED(hr), "DirectInput8 Initialize failed: hr=%08x\n", hr);
292     if (FAILED(hr)) return;
293
294     memset (&af, 0, sizeof(af));
295     af.dwSize = sizeof(af);
296     af.dwActionSize = sizeof(DIACTION);
297     af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
298     af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
299     af.rgoAction = actionMapping;
300     af.guidActionMap = ACTION_MAPPING_GUID;
301     af.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
302     af.dwBufferSize = 32;
303
304     /* Test enumerating all attached and installed devices */
305     count.keyboard = NULL;
306     count.mouse = NULL;
307     count.ndevices = 0;
308     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_ATTACHEDONLY);
309     ok (count.ndevices > 0, "EnumDevicesBySemantics did not call the callback hr=%08x\n", hr);
310     ok (count.keyboard != NULL, "EnumDevicesBySemantics should enumerate the keyboard\n");
311     ok (count.mouse != NULL, "EnumDevicesBySemantics should enumerate the mouse\n");
312
313     /* Enumerate Force feedback devices. We should get no mouse nor keyboard */
314     count.keyboard = NULL;
315     count.mouse = NULL;
316     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_FORCEFEEDBACK);
317     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
318     ok (count.keyboard == NULL, "Keyboard should not be enumerated when asking for forcefeedback\n");
319     ok (count.mouse == NULL, "Mouse should not be enumerated when asking for forcefeedback\n");
320
321     /* Enumerate available devices. That is devices with not owned by any user.
322        Before setting the action map for all devices we still have them available.
323     */
324     count.ndevices = 0;
325     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
326     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
327     ok (count.ndevices > 0, "There should be devices available before action mapping available=%d\n", count.ndevices);
328
329     /* This enumeration builds and sets the action map for all devices */
330     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, &data, DIEDBSFL_ATTACHEDONLY);
331     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
332
333     /* After a successful action mapping we should have no devices available */
334     count.ndevices = 0;
335     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
336     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
337     todo_wine ok (count.ndevices == 0, "No device should be available after action mapping available=%d\n", count.ndevices);
338
339     /* Use the devices we collect for some tests */
340     if (data.keyboard != NULL)
341     {
342         /* Test keyboard BuildActionMap */
343         test_build_action_map(data.keyboard, data.lpdiaf, DITEST_KEYBOARDSPACE, DIDFT_PSHBUTTON, DIK_SPACE);
344         /* Test keyboard input */
345         test_device_input(data.keyboard, INPUT_KEYBOARD, VK_SPACE, 2);
346
347         /* Test BuildActionMap with no suitable actions for a device */
348         IDirectInputDevice_Unacquire(data.keyboard);
349         af.dwDataSize = 4 * DITEST_KEYBOARDSPACE;
350         af.dwNumActions = DITEST_KEYBOARDSPACE;
351
352         hr = IDirectInputDevice8_BuildActionMap(data.keyboard, data.lpdiaf, NULL, DIDBAM_INITIALIZE);
353         ok (hr == DI_NOEFFECT, "BuildActionMap should have no effect with no actions hr=%08x\n", hr);
354
355         hr = IDirectInputDevice8_SetActionMap(data.keyboard, data.lpdiaf, NULL, 0);
356         ok (hr == DI_NOEFFECT, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr);
357
358         af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
359         af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
360     }
361
362     if (data.mouse != NULL)
363     {
364         /* Test mouse BuildActionMap */
365         test_build_action_map(data.mouse, data.lpdiaf, DITEST_MOUSEBUTTON0, DIDFT_PSHBUTTON, 0x03);
366         test_build_action_map(data.mouse, data.lpdiaf, DITEST_YAXIS, DIDFT_RELAXIS, 0x01);
367
368         test_device_input(data.mouse, INPUT_MOUSE, MOUSEEVENTF_LEFTDOWN, 3);
369     }
370
371     /* The call fails with a zeroed GUID */
372     memset(&af.guidActionMap, 0, sizeof(GUID));
373     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, 0, 0);
374     todo_wine ok(FAILED(hr), "EnumDevicesBySemantics succeeded with invalid GUID hr=%08x\n", hr);
375 }
376
377 START_TEST(device)
378 {
379     CoInitialize(NULL);
380
381     test_action_mapping();
382
383     CoUninitialize();
384 }