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;
74 hr = IDirectInputDevice8_Acquire(lpdid);
75 ok (SUCCEEDED(hr), "Failed to acquire device hr=%08x\n", hr);
77 if (event_type == INPUT_KEYBOARD)
78 keybd_event( event, DIK_SPACE, 0, 0);
80 if (event_type == INPUT_MOUSE)
81 mouse_event( event, 0, 0, 0, 0);
83 IDirectInputDevice8_Poll(lpdid);
84 hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
88 win_skip("We're not able to inject input into Windows dinput8 with events\n");
92 ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data.uAppData, expected);
94 /* Check for buffer owerflow */
95 for (i = 0; i < 17; i++)
96 if (event_type == INPUT_KEYBOARD)
98 keybd_event( VK_SPACE, DIK_SPACE, 0, 0);
99 keybd_event( VK_SPACE, DIK_SPACE, KEYEVENTF_KEYUP, 0);
101 else if (event_type == INPUT_MOUSE)
103 mouse_event(MOUSEEVENTF_LEFTDOWN, 1, 1, 0, 0);
104 mouse_event(MOUSEEVENTF_LEFTUP, 1, 1, 0, 0);
107 IDirectInputDevice8_Poll(lpdid);
110 hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0);
111 ok(hr == DI_BUFFEROVERFLOW, "GetDeviceData() failed: %08x\n", hr);
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);
117 static void test_build_action_map(
118 LPDIRECTINPUTDEVICE8 lpdid,
119 LPDIACTIONFORMAT lpdiaf,
127 DWORD instance, type, how;
129 DIDEVICEINSTANCEA ddi;
131 ddi.dwSize = sizeof(ddi);
132 IDirectInputDevice_GetDeviceInfo(lpdid, &ddi);
134 hr = IDirectInputDevice8_BuildActionMap(lpdid, lpdiaf, NULL, DIDBAM_INITIALIZE);
135 ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
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;
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);
149 static BOOL CALLBACK enumeration_callback(
150 LPCDIDEVICEINSTANCE lpddi,
151 LPDIRECTINPUTDEVICE8 lpdid,
159 struct enum_data *data = pvRef;
161 DIDEVICEOBJECTDATA buffer[5];
163 if (!data) return DIENUM_CONTINUE;
167 /* collect the mouse and keyboard */
168 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysKeyboard))
170 IDirectInputDevice_AddRef(lpdid);
171 data->keyboard = lpdid;
173 ok (dwFlags & DIEDBS_MAPPEDPRI1, "Keyboard should be mapped as pri1 dwFlags=%08x\n", dwFlags);
176 if (IsEqualGUID(&lpddi->guidInstance, &GUID_SysMouse))
178 IDirectInputDevice_AddRef(lpdid);
181 ok (dwFlags & DIEDBS_MAPPEDPRI1, "Mouse should be mapped as pri1 dwFlags=%08x\n", dwFlags);
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);
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);
193 hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
194 ok (SUCCEEDED(hr), "SetActionMap failed hr=%08x\n", hr);
196 /* Some joysticks may have no suitable actions and thus should not be tested */
197 if (hr == DI_NOEFFECT) return DIENUM_CONTINUE;
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;
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);
210 hr = IDirectInputDevice_GetDeviceData(lpdid, sizeof(buffer[0]), buffer, &cnt, 0);
211 ok(hr == DIERR_NOTACQUIRED, "GetDeviceData() failed hr=%08x\n", hr);
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;
219 hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_RANGE, &dpr.diph);
220 /* Only test if device supports the range property */
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);
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);
232 hr = IDirectInputDevice_GetDeviceData(lpdid, sizeof(buffer[0]), buffer, &cnt, 0);
233 ok(hr == DI_OK, "GetDeviceData() failed hr=%08x\n", hr);
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);
239 return DIENUM_CONTINUE;
242 static void test_action_mapping(void)
245 HINSTANCE hinst = GetModuleHandle(NULL);
246 LPDIRECTINPUT8 pDI = NULL;
248 struct enum_data data = {pDI, &af, NULL, NULL, 0};
250 hr = CoCreateInstance(&CLSID_DirectInput8, 0, 1, &IID_IDirectInput8A, (LPVOID*)&pDI);
251 if (hr == DIERR_OLDDIRECTINPUTVERSION ||
252 hr == DIERR_BETADIRECTINPUTVERSION ||
253 hr == REGDB_E_CLASSNOTREG)
255 win_skip("ActionMapping requires dinput8\n");
258 ok(SUCCEEDED(hr), "DirectInput8 Create failed: hr=%08x\n", hr);
259 if (FAILED(hr)) return;
261 hr = IDirectInput8_Initialize(pDI,hinst, DIRECTINPUT_VERSION);
262 if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_BETADIRECTINPUTVERSION)
264 win_skip("ActionMapping requires dinput8\n");
267 ok(SUCCEEDED(hr), "DirectInput8 Initialize failed: hr=%08x\n", hr);
268 if (FAILED(hr)) return;
270 memset (&af, 0, sizeof(af));
271 af.dwSize = sizeof(af);
272 af.dwActionSize = sizeof(DIACTION);
273 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
274 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
275 af.rgoAction = actionMapping;
276 af.guidActionMap = ACTION_MAPPING_GUID;
277 af.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
278 af.dwBufferSize = 32;
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);
284 if (data.keyboard != NULL)
286 /* Test keyboard BuildActionMap */
287 test_build_action_map(data.keyboard, data.lpdiaf, DITEST_KEYBOARDSPACE, DIDFT_PSHBUTTON, DIK_SPACE);
288 /* Test keyboard input */
289 test_device_input(data.keyboard, INPUT_KEYBOARD, VK_SPACE, 2);
291 /* Test BuildActionMap with no suitable actions for a device */
292 IDirectInputDevice_Unacquire(data.keyboard);
293 af.dwDataSize = 4 * DITEST_KEYBOARDSPACE;
294 af.dwNumActions = DITEST_KEYBOARDSPACE;
296 hr = IDirectInputDevice8_BuildActionMap(data.keyboard, data.lpdiaf, NULL, DIDBAM_INITIALIZE);
297 ok (hr == DI_NOEFFECT, "BuildActionMap should have no effect with no actions hr=%08x\n", hr);
299 hr = IDirectInputDevice8_SetActionMap(data.keyboard, data.lpdiaf, NULL, 0);
300 ok (hr == DI_NOEFFECT, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr);
302 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
303 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
306 if (data.mouse != NULL)
308 /* Test mouse BuildActionMap */
309 test_build_action_map(data.mouse, data.lpdiaf, DITEST_MOUSEBUTTON0, DIDFT_PSHBUTTON, 0x03);
310 test_build_action_map(data.mouse, data.lpdiaf, DITEST_YAXIS, DIDFT_RELAXIS, 0x01);
312 test_device_input(data.mouse, INPUT_MOUSE, MOUSEEVENTF_LEFTDOWN, 3);
320 test_action_mapping();