dinput: EnumDevicesBySemantics enumerating all kinds of joysticks.
[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
73     hr = IDirectInputDevice8_Acquire(lpdid);
74     ok (SUCCEEDED(hr), "Failed to acquire device hr=%08x\n", hr);
75
76     if (event_type == INPUT_KEYBOARD)
77         keybd_event( event, 0, 0, 0);
78
79     if (event_type == INPUT_MOUSE)
80         mouse_event( event, 0, 0, 0, 0);
81
82     IDirectInputDevice8_Poll(lpdid);
83     hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
84
85     if (data_size != 1)
86     {
87         win_skip("We're not able to inject input into Windows dinput8 with events\n");
88         return;
89     }
90
91     ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data.uAppData, expected);
92 }
93
94 static void test_build_action_map(
95     LPDIRECTINPUTDEVICE8 lpdid,
96     LPDIACTIONFORMAT lpdiaf,
97     int action_index,
98     DWORD expected_type,
99     DWORD expected_inst
100 )
101 {
102     HRESULT hr;
103     DIACTION *actions;
104     DWORD instance, type, how;
105     GUID assigned_to;
106     DIDEVICEINSTANCEA ddi;
107
108     ddi.dwSize = sizeof(ddi);
109     IDirectInputDevice_GetDeviceInfo(lpdid, &ddi);
110
111     hr = IDirectInputDevice8_BuildActionMap(lpdid, lpdiaf, NULL, DIDBAM_INITIALIZE);
112     ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
113
114     actions = lpdiaf->rgoAction;
115     instance = DIDFT_GETINSTANCE(actions[action_index].dwObjID);
116     type = DIDFT_GETTYPE(actions[action_index].dwObjID);
117     how = actions[action_index].dwHow;
118     assigned_to = actions[action_index].guidInstance;
119
120     ok (how == DIAH_USERCONFIG || how == DIAH_DEFAULT, "Action was not set dwHow=%08x\n", how);
121     ok (instance == expected_inst, "Action not mapped correctly instance=%08x expected=%08x\n", instance, expected_inst);
122     ok (type == expected_type, "Action type not mapped correctly type=%08x expected=%08x\n", type, expected_type);
123     ok (IsEqualGUID(&assigned_to, &ddi.guidInstance), "Action and device GUID do not match action=%d\n", action_index);
124 }
125
126 static BOOL CALLBACK enumeration_callback(
127     LPCDIDEVICEINSTANCE lpddi,
128     LPDIRECTINPUTDEVICE8 lpdid,
129     DWORD dwFlags,
130     DWORD dwRemaining,
131     LPVOID pvRef)
132 {
133     HRESULT hr;
134     DIPROPDWORD dp;
135     struct enum_data *data = pvRef;
136     if (!data) return DIENUM_CONTINUE;
137
138     data->ndevices++;
139
140     /* collect the mouse and keyboard */
141     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
142     {
143         IDirectInputDevice_AddRef(lpdid);
144         data->keyboard = lpdid;
145
146         ok (dwFlags & DIEDBS_MAPPEDPRI1, "Keyboard should be mapped as pri1 dwFlags=%08x\n", dwFlags);
147     }
148
149     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
150     {
151         IDirectInputDevice_AddRef(lpdid);
152         data->mouse = lpdid;
153
154         ok (dwFlags & DIEDBS_MAPPEDPRI1, "Mouse should be mapped as pri1 dwFlags=%08x\n", dwFlags);
155     }
156
157     /* Building and setting an action map */
158     /* It should not use any pre-stored mappings so we use DIDBAM_INITIALIZE */
159     hr = IDirectInputDevice8_BuildActionMap(lpdid, data->lpdiaf, NULL, DIDBAM_INITIALIZE);
160     ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
161
162     /* Device has no data format and thus can't be acquired */
163     hr = IDirectInputDevice8_Acquire(lpdid);
164     ok (hr == DIERR_INVALIDPARAM, "Device was acquired before SetActionMap hr=%08x\n", hr);
165
166     hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
167     ok (SUCCEEDED(hr), "SetActionMap failed hr=%08x\n", hr);
168
169     /* Some joysticks may have no suitable actions and thus should not be tested */
170     if (hr == DI_NOEFFECT) return DIENUM_CONTINUE;
171
172     /* Test buffer size */
173     memset(&dp, 0, sizeof(dp));
174     dp.diph.dwSize = sizeof(dp);
175     dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
176     dp.diph.dwHow  = DIPH_DEVICE;
177
178     hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_BUFFERSIZE, &dp.diph);
179     ok (SUCCEEDED(hr), "GetProperty failed hr=%08x\n", hr);
180     ok (dp.dwData == data->lpdiaf->dwBufferSize, "SetActionMap must set the buffer, buffersize=%d\n", dp.dwData);
181
182     /* SetActionMap has set the data format so now it should work */
183     hr = IDirectInputDevice8_Acquire(lpdid);
184     ok (SUCCEEDED(hr), "Acquire failed hr=%08x\n", hr);
185
186     /* SetActionMap should not work on an acquired device */
187     hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
188     ok (hr == DIERR_ACQUIRED, "SetActionMap succeeded with an acquired device hr=%08x\n", hr);
189
190     return DIENUM_CONTINUE;
191 }
192
193 /*  A simpler callback function used to count and check
194     the enumeration of devices.
195 */
196 static BOOL CALLBACK counting_callback(
197     LPCDIDEVICEINSTANCE lpddi,
198     LPDIRECTINPUTDEVICE8 lpdid,
199     DWORD dwFlags,
200     DWORD dwRemaining,
201     LPVOID pvRef)
202 {
203     struct enum_data *data = pvRef;
204     if (!data) return DIENUM_CONTINUE;
205
206     data->ndevices++;
207     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
208         data->keyboard = lpdid;
209
210     if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
211         data->mouse = lpdid;
212
213     return DIENUM_CONTINUE;
214 }
215
216 static void test_action_mapping(void)
217 {
218     HRESULT hr;
219     HINSTANCE hinst = GetModuleHandle(NULL);
220     LPDIRECTINPUT8 pDI = NULL;
221     DIACTIONFORMAT af;
222     struct enum_data data =  {pDI, &af, NULL, NULL, 0};
223     struct enum_data count = {pDI, &af, NULL, NULL, 0};
224
225     hr = CoCreateInstance(&CLSID_DirectInput8, 0, 1, &IID_IDirectInput8A, (LPVOID*)&pDI);
226     if (hr == DIERR_OLDDIRECTINPUTVERSION ||
227         hr == DIERR_BETADIRECTINPUTVERSION ||
228         hr == REGDB_E_CLASSNOTREG)
229     {
230         win_skip("ActionMapping requires dinput8\n");
231         return;
232     }
233     ok(SUCCEEDED(hr), "DirectInput8 Create failed: hr=%08x\n", hr);
234     if (FAILED(hr)) return;
235
236     hr = IDirectInput8_Initialize(pDI,hinst, DIRECTINPUT_VERSION);
237     if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_BETADIRECTINPUTVERSION)
238     {
239         win_skip("ActionMapping requires dinput8\n");
240         return;
241     }
242     ok(SUCCEEDED(hr), "DirectInput8 Initialize failed: hr=%08x\n", hr);
243     if (FAILED(hr)) return;
244
245     memset (&af, 0, sizeof(af));
246     af.dwSize = sizeof(af);
247     af.dwActionSize = sizeof(DIACTION);
248     af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
249     af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
250     af.rgoAction = actionMapping;
251     af.guidActionMap = ACTION_MAPPING_GUID;
252     af.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
253     af.dwBufferSize = 32;
254
255     /* Test enumerating all attached and installed devices */
256     count.keyboard = NULL;
257     count.mouse = NULL;
258     count.ndevices = 0;
259     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_ATTACHEDONLY);
260     ok (count.ndevices > 0, "EnumDevicesBySemantics did not call the callback hr=%08x\n", hr);
261     ok (count.keyboard != NULL, "EnumDevicesBySemantics should enumerate the keyboard\n");
262     ok (count.mouse != NULL, "EnumDevicesBySemantics should enumerate the mouse\n");
263
264     /* Enumerate Force feedback devices. We should get no mouse nor keyboard */
265     count.keyboard = NULL;
266     count.mouse = NULL;
267     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_FORCEFEEDBACK);
268     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
269     ok (count.keyboard == NULL, "Keyboard should not be enumerated when asking for forcefeedback\n");
270     ok (count.mouse == NULL, "Mouse should not be enumerated when asking for forcefeedback\n");
271
272     /* Enumerate available devices. That is devices with not owned by any user.
273        Before setting the action map for all devices we still have them available.
274     */
275     count.ndevices = 0;
276     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
277     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
278     ok (count.ndevices > 0, "There should be devices available before action mapping available=%d\n", count.ndevices);
279
280     /* This enumeration builds and sets the action map for all devices */
281     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, &data, DIEDBSFL_ATTACHEDONLY);
282     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
283
284     /* After a succesfull action mapping we should have no devices available */
285     count.ndevices = 0;
286     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
287     ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
288     todo_wine ok (count.ndevices == 0, "No device should be available after action mapping available=%d\n", count.ndevices);
289
290     /* Use the devices we collect for some tests */
291     if (data.keyboard != NULL)
292     {
293         /* Test keyboard BuildActionMap */
294         test_build_action_map(data.keyboard, data.lpdiaf, DITEST_KEYBOARDSPACE, DIDFT_PSHBUTTON, DIK_SPACE);
295         /* Test keyboard input */
296         test_device_input(data.keyboard, INPUT_KEYBOARD, VK_SPACE, 2);
297
298         /* Test BuildActionMap with no suitable actions for a device */
299         IDirectInputDevice_Unacquire(data.keyboard);
300         af.dwDataSize = 4 * DITEST_KEYBOARDSPACE;
301         af.dwNumActions = DITEST_KEYBOARDSPACE;
302
303         hr = IDirectInputDevice8_BuildActionMap(data.keyboard, data.lpdiaf, NULL, DIDBAM_INITIALIZE);
304         ok (hr == DI_NOEFFECT, "BuildActionMap should have no effect with no actions hr=%08x\n", hr);
305
306         hr = IDirectInputDevice8_SetActionMap(data.keyboard, data.lpdiaf, NULL, 0);
307         ok (hr == DI_NOEFFECT, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr);
308
309         af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
310         af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
311     }
312
313     if (data.mouse != NULL)
314     {
315         /* Test mouse BuildActionMap */
316         test_build_action_map(data.mouse, data.lpdiaf, DITEST_MOUSEBUTTON0, DIDFT_PSHBUTTON, 0x03);
317         test_build_action_map(data.mouse, data.lpdiaf, DITEST_YAXIS, DIDFT_RELAXIS, 0x01);
318
319         test_device_input(data.mouse, INPUT_MOUSE, MOUSEEVENTF_LEFTDOWN, 3);
320     }
321
322     /* The call fails with a zeroed GUID */
323     memset(&af.guidActionMap, 0, sizeof(GUID));
324     hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, 0, 0);
325     todo_wine ok(FAILED(hr), "EnumDevicesBySemantics succeeded with invalid GUID hr=%08x\n", hr);
326 }
327
328 START_TEST(device)
329 {
330     CoInitialize(NULL);
331
332     test_action_mapping();
333
334     CoUninitialize();
335 }