2 * Copyright (c) 2011 Lucas Fialho Zawacki
3 * Copyright (c) 2006 Vitaliy Margolen
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.
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.
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
20 #define DIRECTINPUT_VERSION 0x0800
25 #include "wine/test.h"
31 LPDIACTIONFORMAT lpdiaf;
32 LPDIRECTINPUTDEVICE8 keyboard;
33 LPDIRECTINPUTDEVICE8 mouse;
38 static const GUID ACTION_MAPPING_GUID = { 0x1, 0x2, 0x3, { 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb } };
48 static DIACTION actionMapping[]=
51 { 0, 0x01008A01 /* DIAXIS_DRIVINGR_STEER */ , 0, { "Steer" } },
53 { 1, 0x01000C01 /* DIBUTTON_DRIVINGR_SHIFTUP */ , 0, { "Upshift" } },
55 { 2, DIKEYBOARD_SPACE , 0, { "Missile" } },
57 { 3, DIMOUSE_BUTTON0, 0, { "Select" } },
59 { 4, DIMOUSE_YAXIS, 0, { "Y Axis" } }
62 static void test_device_input(
63 LPDIRECTINPUTDEVICE8 lpdid,
70 DIDEVICEOBJECTDATA obj_data;
73 hr = IDirectInputDevice8_Acquire(lpdid);
74 ok (SUCCEEDED(hr), "Failed to acquire device hr=%08x\n", hr);
76 if (event_type == INPUT_KEYBOARD)
77 keybd_event( event, 0, 0, 0);
79 if (event_type == INPUT_MOUSE)
80 mouse_event( event, 0, 0, 0, 0);
82 IDirectInputDevice8_Poll(lpdid);
83 hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
87 win_skip("We're not able to inject input into Windows dinput8 with events\n");
91 ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data.uAppData, expected);
94 static void test_build_action_map(
95 LPDIRECTINPUTDEVICE8 lpdid,
96 LPDIACTIONFORMAT lpdiaf,
104 DWORD instance, type, how;
106 DIDEVICEINSTANCEA ddi;
108 ddi.dwSize = sizeof(ddi);
109 IDirectInputDevice_GetDeviceInfo(lpdid, &ddi);
111 hr = IDirectInputDevice8_BuildActionMap(lpdid, lpdiaf, NULL, DIDBAM_INITIALIZE);
112 ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
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;
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);
126 static BOOL CALLBACK enumeration_callback(
127 LPCDIDEVICEINSTANCE lpddi,
128 LPDIRECTINPUTDEVICE8 lpdid,
136 struct enum_data *data = pvRef;
137 if (!data) return DIENUM_CONTINUE;
141 /* collect the mouse and keyboard */
142 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
144 IDirectInputDevice_AddRef(lpdid);
145 data->keyboard = lpdid;
147 ok (dwFlags & DIEDBS_MAPPEDPRI1, "Keyboard should be mapped as pri1 dwFlags=%08x\n", dwFlags);
150 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
152 IDirectInputDevice_AddRef(lpdid);
155 ok (dwFlags & DIEDBS_MAPPEDPRI1, "Mouse should be mapped as pri1 dwFlags=%08x\n", dwFlags);
158 /* Building and setting an action map */
159 /* It should not use any pre-stored mappings so we use DIDBAM_INITIALIZE */
160 hr = IDirectInputDevice8_BuildActionMap(lpdid, data->lpdiaf, NULL, DIDBAM_INITIALIZE);
161 ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
163 /* Device has no data format and thus can't be acquired */
164 hr = IDirectInputDevice8_Acquire(lpdid);
165 ok (hr == DIERR_INVALIDPARAM, "Device was acquired before SetActionMap hr=%08x\n", hr);
167 hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
168 ok (SUCCEEDED(hr), "SetActionMap failed hr=%08x\n", hr);
170 /* Some joysticks may have no suitable actions and thus should not be tested */
171 if (hr == DI_NOEFFECT) return DIENUM_CONTINUE;
173 /* Test buffer size */
174 memset(&dp, 0, sizeof(dp));
175 dp.diph.dwSize = sizeof(dp);
176 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
177 dp.diph.dwHow = DIPH_DEVICE;
179 hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_BUFFERSIZE, &dp.diph);
180 ok (SUCCEEDED(hr), "GetProperty failed hr=%08x\n", hr);
181 ok (dp.dwData == data->lpdiaf->dwBufferSize, "SetActionMap must set the buffer, buffersize=%d\n", dp.dwData);
183 /* Test axis range */
184 memset(&dpr, 0, sizeof(dpr));
185 dpr.diph.dwSize = sizeof(dpr);
186 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
187 dpr.diph.dwHow = DIPH_DEVICE;
189 hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_RANGE, &dpr.diph);
190 /* Only test if device supports the range property */
193 ok (dpr.lMin == data->lpdiaf->lAxisMin, "SetActionMap must set the min axis range expected=%d got=%d\n", data->lpdiaf->lAxisMin, dpr.lMin);
194 ok (dpr.lMax == data->lpdiaf->lAxisMax, "SetActionMap must set the max axis range expected=%d got=%d\n", data->lpdiaf->lAxisMax, dpr.lMax);
197 /* SetActionMap has set the data format so now it should work */
198 hr = IDirectInputDevice8_Acquire(lpdid);
199 ok (SUCCEEDED(hr), "Acquire failed hr=%08x\n", hr);
201 /* SetActionMap should not work on an acquired device */
202 hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
203 ok (hr == DIERR_ACQUIRED, "SetActionMap succeeded with an acquired device hr=%08x\n", hr);
205 return DIENUM_CONTINUE;
208 /* A simpler callback function used to count and check
209 the enumeration of devices.
211 static BOOL CALLBACK counting_callback(
212 LPCDIDEVICEINSTANCE lpddi,
213 LPDIRECTINPUTDEVICE8 lpdid,
218 struct enum_data *data = pvRef;
219 if (!data) return DIENUM_CONTINUE;
222 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
223 data->keyboard = lpdid;
225 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
228 return DIENUM_CONTINUE;
231 static void test_action_mapping(void)
234 HINSTANCE hinst = GetModuleHandle(NULL);
235 LPDIRECTINPUT8 pDI = NULL;
237 struct enum_data data = {pDI, &af, NULL, NULL, 0};
238 struct enum_data count = {pDI, &af, NULL, NULL, 0};
240 hr = CoCreateInstance(&CLSID_DirectInput8, 0, 1, &IID_IDirectInput8A, (LPVOID*)&pDI);
241 if (hr == DIERR_OLDDIRECTINPUTVERSION ||
242 hr == DIERR_BETADIRECTINPUTVERSION ||
243 hr == REGDB_E_CLASSNOTREG)
245 win_skip("ActionMapping requires dinput8\n");
248 ok(SUCCEEDED(hr), "DirectInput8 Create failed: hr=%08x\n", hr);
249 if (FAILED(hr)) return;
251 hr = IDirectInput8_Initialize(pDI,hinst, DIRECTINPUT_VERSION);
252 if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_BETADIRECTINPUTVERSION)
254 win_skip("ActionMapping requires dinput8\n");
257 ok(SUCCEEDED(hr), "DirectInput8 Initialize failed: hr=%08x\n", hr);
258 if (FAILED(hr)) return;
260 memset (&af, 0, sizeof(af));
261 af.dwSize = sizeof(af);
262 af.dwActionSize = sizeof(DIACTION);
263 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
264 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
265 af.rgoAction = actionMapping;
266 af.guidActionMap = ACTION_MAPPING_GUID;
267 af.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
268 af.dwBufferSize = 32;
270 /* Test enumerating all attached and installed devices */
271 count.keyboard = NULL;
274 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_ATTACHEDONLY);
275 ok (count.ndevices > 0, "EnumDevicesBySemantics did not call the callback hr=%08x\n", hr);
276 ok (count.keyboard != NULL, "EnumDevicesBySemantics should enumerate the keyboard\n");
277 ok (count.mouse != NULL, "EnumDevicesBySemantics should enumerate the mouse\n");
279 /* Enumerate Force feedback devices. We should get no mouse nor keyboard */
280 count.keyboard = NULL;
282 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_FORCEFEEDBACK);
283 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
284 ok (count.keyboard == NULL, "Keyboard should not be enumerated when asking for forcefeedback\n");
285 ok (count.mouse == NULL, "Mouse should not be enumerated when asking for forcefeedback\n");
287 /* Enumerate available devices. That is devices with not owned by any user.
288 Before setting the action map for all devices we still have them available.
291 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
292 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
293 ok (count.ndevices > 0, "There should be devices available before action mapping available=%d\n", count.ndevices);
295 /* This enumeration builds and sets the action map for all devices */
296 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, &data, DIEDBSFL_ATTACHEDONLY);
297 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
299 /* After a successful action mapping we should have no devices available */
301 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, counting_callback, &count, DIEDBSFL_AVAILABLEDEVICES);
302 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed hr=%08x\n", hr);
303 todo_wine ok (count.ndevices == 0, "No device should be available after action mapping available=%d\n", count.ndevices);
305 /* Use the devices we collect for some tests */
306 if (data.keyboard != NULL)
308 /* Test keyboard BuildActionMap */
309 test_build_action_map(data.keyboard, data.lpdiaf, DITEST_KEYBOARDSPACE, DIDFT_PSHBUTTON, DIK_SPACE);
310 /* Test keyboard input */
311 test_device_input(data.keyboard, INPUT_KEYBOARD, VK_SPACE, 2);
313 /* Test BuildActionMap with no suitable actions for a device */
314 IDirectInputDevice_Unacquire(data.keyboard);
315 af.dwDataSize = 4 * DITEST_KEYBOARDSPACE;
316 af.dwNumActions = DITEST_KEYBOARDSPACE;
318 hr = IDirectInputDevice8_BuildActionMap(data.keyboard, data.lpdiaf, NULL, DIDBAM_INITIALIZE);
319 ok (hr == DI_NOEFFECT, "BuildActionMap should have no effect with no actions hr=%08x\n", hr);
321 hr = IDirectInputDevice8_SetActionMap(data.keyboard, data.lpdiaf, NULL, 0);
322 ok (hr == DI_NOEFFECT, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr);
324 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
325 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
328 if (data.mouse != NULL)
330 /* Test mouse BuildActionMap */
331 test_build_action_map(data.mouse, data.lpdiaf, DITEST_MOUSEBUTTON0, DIDFT_PSHBUTTON, 0x03);
332 test_build_action_map(data.mouse, data.lpdiaf, DITEST_YAXIS, DIDFT_RELAXIS, 0x01);
334 test_device_input(data.mouse, INPUT_MOUSE, MOUSEEVENTF_LEFTDOWN, 3);
337 /* The call fails with a zeroed GUID */
338 memset(&af.guidActionMap, 0, sizeof(GUID));
339 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, 0, 0);
340 todo_wine ok(FAILED(hr), "EnumDevicesBySemantics succeeded with invalid GUID hr=%08x\n", hr);
347 test_action_mapping();