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
;
39 static const GUID ACTION_MAPPING_GUID
= { 0x1, 0x2, 0x3, { 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb } };
49 static DIACTION actionMapping
[]=
52 { 0, 0x01008A01 /* DIAXIS_DRIVINGR_STEER */ , 0, { "Steer" } },
54 { 1, 0x01000C01 /* DIBUTTON_DRIVINGR_SHIFTUP */ , 0, { "Upshift" } },
56 { 2, DIKEYBOARD_SPACE
, 0, { "Missile" } },
58 { 3, DIMOUSE_BUTTON0
, 0, { "Select" } },
60 { 4, DIMOUSE_YAXIS
, 0, { "Y Axis" } }
63 static void test_device_input(
64 LPDIRECTINPUTDEVICE8 lpdid
,
71 DIDEVICEOBJECTDATA obj_data
;
75 hr
= IDirectInputDevice8_Acquire(lpdid
);
76 ok (SUCCEEDED(hr
), "Failed to acquire device hr=%08x\n", hr
);
78 if (event_type
== INPUT_KEYBOARD
)
79 keybd_event( event
, DIK_SPACE
, 0, 0);
81 if (event_type
== INPUT_MOUSE
)
82 mouse_event( event
, 0, 0, 0, 0);
84 IDirectInputDevice8_Poll(lpdid
);
85 hr
= IDirectInputDevice8_GetDeviceData(lpdid
, sizeof(obj_data
), &obj_data
, &data_size
, 0);
89 win_skip("We're not able to inject input into Windows dinput8 with events\n");
93 ok (obj_data
.uAppData
== expected
, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data
.uAppData
, expected
);
95 /* Check for buffer owerflow */
96 for (i
= 0; i
< 17; i
++)
97 if (event_type
== INPUT_KEYBOARD
)
99 keybd_event( VK_SPACE
, DIK_SPACE
, 0, 0);
100 keybd_event( VK_SPACE
, DIK_SPACE
, KEYEVENTF_KEYUP
, 0);
102 else if (event_type
== INPUT_MOUSE
)
104 mouse_event(MOUSEEVENTF_LEFTDOWN
, 1, 1, 0, 0);
105 mouse_event(MOUSEEVENTF_LEFTUP
, 1, 1, 0, 0);
108 IDirectInputDevice8_Poll(lpdid
);
111 hr
= IDirectInputDevice8_GetDeviceData(lpdid
, sizeof(obj_data
), &obj_data
, &data_size
, 0);
112 ok(hr
== DI_BUFFEROVERFLOW
, "GetDeviceData() failed: %08x\n", hr
);
114 hr
= IDirectInputDevice8_GetDeviceData(lpdid
, sizeof(obj_data
), &obj_data
, &data_size
, 0);
115 ok(hr
== DI_OK
&& data_size
== 1, "GetDeviceData() failed: %08x cnt:%d\n", hr
, data_size
);
118 static void test_build_action_map(
119 LPDIRECTINPUTDEVICE8 lpdid
,
120 LPDIACTIONFORMAT lpdiaf
,
128 DWORD instance
, type
, how
;
130 DIDEVICEINSTANCEA ddi
;
132 ddi
.dwSize
= sizeof(ddi
);
133 IDirectInputDevice_GetDeviceInfo(lpdid
, &ddi
);
135 hr
= IDirectInputDevice8_BuildActionMap(lpdid
, lpdiaf
, NULL
, DIDBAM_INITIALIZE
);
136 ok (SUCCEEDED(hr
), "BuildActionMap failed hr=%08x\n", hr
);
138 actions
= lpdiaf
->rgoAction
;
139 instance
= DIDFT_GETINSTANCE(actions
[action_index
].dwObjID
);
140 type
= DIDFT_GETTYPE(actions
[action_index
].dwObjID
);
141 how
= actions
[action_index
].dwHow
;
142 assigned_to
= actions
[action_index
].guidInstance
;
144 ok (how
== DIAH_USERCONFIG
|| how
== DIAH_DEFAULT
, "Action was not set dwHow=%08x\n", how
);
145 ok (instance
== expected_inst
, "Action not mapped correctly instance=%08x expected=%08x\n", instance
, expected_inst
);
146 ok (type
== expected_type
, "Action type not mapped correctly type=%08x expected=%08x\n", type
, expected_type
);
147 ok (IsEqualGUID(&assigned_to
, &ddi
.guidInstance
), "Action and device GUID do not match action=%d\n", action_index
);
150 static BOOL CALLBACK
enumeration_callback(
151 LPCDIDEVICEINSTANCE lpddi
,
152 LPDIRECTINPUTDEVICE8 lpdid
,
161 WCHAR usernameW
[MAX_PATH
];
162 DWORD username_size
= MAX_PATH
;
163 struct enum_data
*data
= pvRef
;
165 DIDEVICEOBJECTDATA buffer
[5];
167 if (!data
) return DIENUM_CONTINUE
;
171 /* Convert username to WCHAR */
172 if (data
->username
!= NULL
)
174 username_size
= MultiByteToWideChar(CP_ACP
, 0, data
->username
, -1, usernameW
, 0);
175 MultiByteToWideChar(CP_ACP
, 0, data
->username
, -1, usernameW
, username_size
);
178 GetUserNameW(usernameW
, &username_size
);
180 /* collect the mouse and keyboard */
181 if (IsEqualGUID(&lpddi
->guidInstance
, &GUID_SysKeyboard
))
183 IDirectInputDevice_AddRef(lpdid
);
184 data
->keyboard
= lpdid
;
186 ok (dwFlags
& DIEDBS_MAPPEDPRI1
, "Keyboard should be mapped as pri1 dwFlags=%08x\n", dwFlags
);
189 if (IsEqualGUID(&lpddi
->guidInstance
, &GUID_SysMouse
))
191 IDirectInputDevice_AddRef(lpdid
);
194 ok (dwFlags
& DIEDBS_MAPPEDPRI1
, "Mouse should be mapped as pri1 dwFlags=%08x\n", dwFlags
);
197 /* Building and setting an action map */
198 /* It should not use any pre-stored mappings so we use DIDBAM_INITIALIZE */
199 hr
= IDirectInputDevice8_BuildActionMap(lpdid
, data
->lpdiaf
, NULL
, DIDBAM_INITIALIZE
);
200 ok (SUCCEEDED(hr
), "BuildActionMap failed hr=%08x\n", hr
);
202 /* Device has no data format and thus can't be acquired */
203 hr
= IDirectInputDevice8_Acquire(lpdid
);
204 ok (hr
== DIERR_INVALIDPARAM
, "Device was acquired before SetActionMap hr=%08x\n", hr
);
206 hr
= IDirectInputDevice8_SetActionMap(lpdid
, data
->lpdiaf
, data
->username
, 0);
207 ok (SUCCEEDED(hr
), "SetActionMap failed hr=%08x\n", hr
);
209 /* Some joysticks may have no suitable actions and thus should not be tested */
210 if (hr
== DI_NOEFFECT
) return DIENUM_CONTINUE
;
212 /* Test username after SetActionMap */
213 dps
.diph
.dwSize
= sizeof(dps
);
214 dps
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
216 dps
.diph
.dwHow
= DIPH_DEVICE
;
219 hr
= IDirectInputDevice_GetProperty(lpdid
, DIPROP_USERNAME
, &dps
.diph
);
220 todo_wine
ok (SUCCEEDED(hr
), "GetProperty failed hr=%08x\n", hr
);
221 todo_wine
ok (!lstrcmpW(usernameW
, dps
.wsz
), "Username not set correctly expected=%s, got=%s\n", wine_dbgstr_wn(usernameW
, -1), wine_dbgstr_wn(dps
.wsz
, -1));
223 /* Test buffer size */
224 memset(&dp
, 0, sizeof(dp
));
225 dp
.diph
.dwSize
= sizeof(dp
);
226 dp
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
227 dp
.diph
.dwHow
= DIPH_DEVICE
;
229 hr
= IDirectInputDevice_GetProperty(lpdid
, DIPROP_BUFFERSIZE
, &dp
.diph
);
230 ok (SUCCEEDED(hr
), "GetProperty failed hr=%08x\n", hr
);
231 ok (dp
.dwData
== data
->lpdiaf
->dwBufferSize
, "SetActionMap must set the buffer, buffersize=%d\n", dp
.dwData
);
234 hr
= IDirectInputDevice_GetDeviceData(lpdid
, sizeof(buffer
[0]), buffer
, &cnt
, 0);
235 ok(hr
== DIERR_NOTACQUIRED
, "GetDeviceData() failed hr=%08x\n", hr
);
237 /* Test axis range */
238 memset(&dpr
, 0, sizeof(dpr
));
239 dpr
.diph
.dwSize
= sizeof(dpr
);
240 dpr
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
241 dpr
.diph
.dwHow
= DIPH_DEVICE
;
243 hr
= IDirectInputDevice_GetProperty(lpdid
, DIPROP_RANGE
, &dpr
.diph
);
244 /* Only test if device supports the range property */
247 ok (dpr
.lMin
== data
->lpdiaf
->lAxisMin
, "SetActionMap must set the min axis range expected=%d got=%d\n", data
->lpdiaf
->lAxisMin
, dpr
.lMin
);
248 ok (dpr
.lMax
== data
->lpdiaf
->lAxisMax
, "SetActionMap must set the max axis range expected=%d got=%d\n", data
->lpdiaf
->lAxisMax
, dpr
.lMax
);
251 /* SetActionMap has set the data format so now it should work */
252 hr
= IDirectInputDevice8_Acquire(lpdid
);
253 ok (SUCCEEDED(hr
), "Acquire failed hr=%08x\n", hr
);
256 hr
= IDirectInputDevice_GetDeviceData(lpdid
, sizeof(buffer
[0]), buffer
, &cnt
, 0);
257 ok(hr
== DI_OK
, "GetDeviceData() failed hr=%08x\n", hr
);
259 /* SetActionMap should not work on an acquired device */
260 hr
= IDirectInputDevice8_SetActionMap(lpdid
, data
->lpdiaf
, NULL
, 0);
261 ok (hr
== DIERR_ACQUIRED
, "SetActionMap succeeded with an acquired device hr=%08x\n", hr
);
263 return DIENUM_CONTINUE
;
266 static void test_action_mapping(void)
269 HINSTANCE hinst
= GetModuleHandle(NULL
);
270 LPDIRECTINPUT8 pDI
= NULL
;
272 struct enum_data data
= {pDI
, &af
, NULL
, NULL
, NULL
, 0};
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
)
279 win_skip("ActionMapping requires dinput8\n");
282 ok(SUCCEEDED(hr
), "DirectInput8 Create failed: hr=%08x\n", hr
);
283 if (FAILED(hr
)) return;
285 hr
= IDirectInput8_Initialize(pDI
,hinst
, DIRECTINPUT_VERSION
);
286 if (hr
== DIERR_OLDDIRECTINPUTVERSION
|| hr
== DIERR_BETADIRECTINPUTVERSION
)
288 win_skip("ActionMapping requires dinput8\n");
291 ok(SUCCEEDED(hr
), "DirectInput8 Initialize failed: hr=%08x\n", hr
);
292 if (FAILED(hr
)) return;
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;
304 /* This enumeration builds and sets the action map for all devices */
305 hr
= IDirectInput8_EnumDevicesBySemantics(pDI
, 0, &af
, enumeration_callback
, &data
, DIEDBSFL_ATTACHEDONLY
);
306 ok (SUCCEEDED(hr
), "EnumDevicesBySemantics failed: hr=%08x\n", hr
);
308 /* Repeat tests with a non NULL user */
309 data
.username
= "Ninja Brian";
310 hr
= IDirectInput8_EnumDevicesBySemantics(pDI
, NULL
, &af
, enumeration_callback
, &data
, DIEDBSFL_ATTACHEDONLY
);
311 ok (SUCCEEDED(hr
), "EnumDevicesBySemantics failed: hr=%08x\n", hr
);
313 if (data
.keyboard
!= NULL
)
315 /* Test keyboard BuildActionMap */
316 test_build_action_map(data
.keyboard
, data
.lpdiaf
, DITEST_KEYBOARDSPACE
, DIDFT_PSHBUTTON
, DIK_SPACE
);
317 /* Test keyboard input */
318 test_device_input(data
.keyboard
, INPUT_KEYBOARD
, VK_SPACE
, 2);
320 /* Test BuildActionMap with no suitable actions for a device */
321 IDirectInputDevice_Unacquire(data
.keyboard
);
322 af
.dwDataSize
= 4 * DITEST_KEYBOARDSPACE
;
323 af
.dwNumActions
= DITEST_KEYBOARDSPACE
;
325 hr
= IDirectInputDevice8_BuildActionMap(data
.keyboard
, data
.lpdiaf
, NULL
, DIDBAM_INITIALIZE
);
326 ok (hr
== DI_NOEFFECT
, "BuildActionMap should have no effect with no actions hr=%08x\n", hr
);
328 hr
= IDirectInputDevice8_SetActionMap(data
.keyboard
, data
.lpdiaf
, NULL
, 0);
329 ok (hr
== DI_NOEFFECT
, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr
);
331 af
.dwDataSize
= 4 * sizeof(actionMapping
) / sizeof(actionMapping
[0]);
332 af
.dwNumActions
= sizeof(actionMapping
) / sizeof(actionMapping
[0]);
335 if (data
.mouse
!= NULL
)
337 /* Test mouse BuildActionMap */
338 test_build_action_map(data
.mouse
, data
.lpdiaf
, DITEST_MOUSEBUTTON0
, DIDFT_PSHBUTTON
, 0x03);
339 test_build_action_map(data
.mouse
, data
.lpdiaf
, DITEST_YAXIS
, DIDFT_RELAXIS
, 0x01);
341 test_device_input(data
.mouse
, INPUT_MOUSE
, MOUSEEVENTF_LEFTDOWN
, 3);
349 test_action_mapping();