dinput: SetActionMap constructing a dataformat for keyboard and mouse.
[wine/multimedia.git] / dlls / dinput8 / tests / device.c
blob479ae34193e18c8bffe6ee0c05e7bda01642da93
1 /*
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
22 #define COBJMACROS
23 #include <windows.h>
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "initguid.h"
28 #include "dinput.h"
30 struct enum_data {
31 LPDIRECTINPUT8 pDI;
32 LPDIACTIONFORMAT lpdiaf;
33 LPDIRECTINPUTDEVICE8 keyboard;
34 LPDIRECTINPUTDEVICE8 mouse;
35 int ndevices;
38 /* Dummy GUID */
39 static const GUID ACTION_MAPPING_GUID = { 0x1, 0x2, 0x3, { 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb } };
41 enum {
42 DITEST_AXIS,
43 DITEST_BUTTON,
44 DITEST_KEYBOARDSPACE,
45 DITEST_MOUSEBUTTON0,
46 DITEST_YAXIS
49 static DIACTION actionMapping[]=
51 /* axis */
52 { 0, 0x01008A01 /* DIAXIS_DRIVINGR_STEER */ , 0, { "Steer" } },
53 /* button */
54 { 1, 0x01000C01 /* DIBUTTON_DRIVINGR_SHIFTUP */ , 0, { "Upshift" } },
55 /* keyboard key */
56 { 2, DIKEYBOARD_SPACE , 0, { "Missile" } },
57 /* mouse button */
58 { 3, DIMOUSE_BUTTON0, 0, { "Select" } },
59 /* mouse axis */
60 { 4, DIMOUSE_YAXIS, 0, { "Y Axis" } }
63 static void test_device_input(
64 LPDIRECTINPUTDEVICE8 lpdid,
65 DWORD event_type,
66 DWORD event,
67 DWORD expected
70 HRESULT hr;
71 DIDEVICEOBJECTDATA obj_data;
72 DWORD data_size = 1;
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, 0, 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);
86 if (data_size != 1)
88 win_skip("We're not able to inject input into Windows dinput8 with events\n");
89 return;
92 todo_wine ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%d\n", obj_data.uAppData, expected);
95 static void test_build_action_map(
96 LPDIRECTINPUTDEVICE8 lpdid,
97 LPDIACTIONFORMAT lpdiaf,
98 int action_index,
99 DWORD expected_type,
100 DWORD expected_inst
103 HRESULT hr;
104 DIACTION *actions;
105 DWORD instance, type, how;
106 GUID assigned_to;
107 DIDEVICEINSTANCEA ddi;
109 ddi.dwSize = sizeof(ddi);
110 IDirectInputDevice_GetDeviceInfo(lpdid, &ddi);
112 hr = IDirectInputDevice8_BuildActionMap(lpdid, lpdiaf, NULL, DIDBAM_INITIALIZE);
113 ok (SUCCEEDED(hr), "BuildActionMap failed hr=%08x\n", hr);
115 actions = lpdiaf->rgoAction;
116 instance = DIDFT_GETINSTANCE(actions[action_index].dwObjID);
117 type = DIDFT_GETTYPE(actions[action_index].dwObjID);
118 how = actions[action_index].dwHow;
119 assigned_to = actions[action_index].guidInstance;
121 ok (how == DIAH_USERCONFIG || how == DIAH_DEFAULT, "Action was not set dwHow=%08x\n", how);
122 ok (instance == expected_inst, "Action not mapped correctly instance=%08x expected=%08x\n", instance, expected_inst);
123 ok (type == expected_type, "Action type not mapped correctly type=%08x expected=%08x\n", type, expected_type);
124 ok (IsEqualGUID(&assigned_to, &ddi.guidInstance), "Action and device GUID do not match action=%d\n", action_index);
127 static BOOL CALLBACK enumeration_callback(
128 LPCDIDEVICEINSTANCE lpddi,
129 LPDIRECTINPUTDEVICE8 lpdid,
130 DWORD dwFlags,
131 DWORD dwRemaining,
132 LPVOID pvRef)
134 HRESULT hr;
135 DIPROPDWORD dp;
136 struct enum_data *data = pvRef;
137 if (!data) return DIENUM_CONTINUE;
139 data->ndevices++;
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);
153 data->mouse = 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 /* Test buffer size */
171 memset(&dp, 0, sizeof(dp));
172 dp.diph.dwSize = sizeof(dp);
173 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
174 dp.diph.dwHow = DIPH_DEVICE;
176 hr = IDirectInputDevice_GetProperty(lpdid, DIPROP_BUFFERSIZE, &dp.diph);
177 ok (SUCCEEDED(hr), "GetProperty failed hr=%08x\n", hr);
178 ok (dp.dwData == data->lpdiaf->dwBufferSize, "SetActionMap must set the buffer, buffersize=%d\n", dp.dwData);
180 /* SetActionMap has set the data format so now it should work */
181 hr = IDirectInputDevice8_Acquire(lpdid);
182 ok (SUCCEEDED(hr), "Acquire failed hr=%08x\n", hr);
184 /* SetActionMap should not work on an acquired device */
185 hr = IDirectInputDevice8_SetActionMap(lpdid, data->lpdiaf, NULL, 0);
186 ok (hr == DIERR_ACQUIRED, "SetActionMap succeeded with an acquired device hr=%08x\n", hr);
188 return DIENUM_CONTINUE;
192 static void test_action_mapping(void)
194 HRESULT hr;
195 HINSTANCE hinst = GetModuleHandle(NULL);
196 LPDIRECTINPUT8 pDI = NULL;
197 DIACTIONFORMAT af;
198 struct enum_data data = {pDI, &af, NULL, NULL, 0};
200 hr = CoCreateInstance(&CLSID_DirectInput8, 0, 1, &IID_IDirectInput8A, (LPVOID*)&pDI);
201 if (hr == DIERR_OLDDIRECTINPUTVERSION ||
202 hr == DIERR_BETADIRECTINPUTVERSION ||
203 hr == REGDB_E_CLASSNOTREG)
205 win_skip("ActionMapping requires dinput8\n");
206 return;
208 ok(SUCCEEDED(hr), "DirectInput8 Create failed: hr=%08x\n", hr);
209 if (FAILED(hr)) return;
211 hr = IDirectInput8_Initialize(pDI,hinst, DIRECTINPUT_VERSION);
212 if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_BETADIRECTINPUTVERSION)
214 win_skip("ActionMapping requires dinput8\n");
215 return;
217 ok(SUCCEEDED(hr), "DirectInput8 Initialize failed: hr=%08x\n", hr);
218 if (FAILED(hr)) return;
220 memset (&af, 0, sizeof(af));
221 af.dwSize = sizeof(af);
222 af.dwActionSize = sizeof(DIACTION);
223 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
224 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
225 af.rgoAction = actionMapping;
226 af.guidActionMap = ACTION_MAPPING_GUID;
227 af.dwGenre = 0x01000000; /* DIVIRTUAL_DRIVING_RACE */
228 af.dwBufferSize = 32;
230 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, &data, 0);
231 ok (SUCCEEDED(hr), "EnumDevicesBySemantics failed: hr=%08x\n", hr);
232 ok (data.ndevices > 0, "EnumDevicesBySemantics did not call the callback hr=%08x\n", hr);
233 ok (data.keyboard != NULL, "EnumDevicesBySemantics should enumerate the keyboard\n");
234 ok (data.mouse != NULL, "EnumDevicesBySemantics should enumerate the mouse\n");
236 if (data.keyboard != NULL)
238 /* Test keyboard BuildActionMap */
239 test_build_action_map(data.keyboard, data.lpdiaf, DITEST_KEYBOARDSPACE, DIDFT_PSHBUTTON, DIK_SPACE);
240 /* Test keyboard input */
241 test_device_input(data.keyboard, INPUT_KEYBOARD, VK_SPACE, 2);
243 /* Test BuildActionMap with no suitable actions for a device */
244 IDirectInputDevice_Unacquire(data.keyboard);
245 af.dwDataSize = 4 * DITEST_KEYBOARDSPACE;
246 af.dwNumActions = DITEST_KEYBOARDSPACE;
248 hr = IDirectInputDevice8_BuildActionMap(data.keyboard, data.lpdiaf, NULL, DIDBAM_INITIALIZE);
249 ok (hr == DI_NOEFFECT, "BuildActionMap should have no effect with no actions hr=%08x\n", hr);
251 hr = IDirectInputDevice8_SetActionMap(data.keyboard, data.lpdiaf, NULL, 0);
252 ok (hr == DI_NOEFFECT, "SetActionMap should have no effect with no actions to map hr=%08x\n", hr);
254 af.dwDataSize = 4 * sizeof(actionMapping) / sizeof(actionMapping[0]);
255 af.dwNumActions = sizeof(actionMapping) / sizeof(actionMapping[0]);
258 if (data.mouse != NULL)
260 /* Test mouse BuildActionMap */
261 test_build_action_map(data.mouse, data.lpdiaf, DITEST_MOUSEBUTTON0, DIDFT_PSHBUTTON, 0x03);
262 test_build_action_map(data.mouse, data.lpdiaf, DITEST_YAXIS, DIDFT_RELAXIS, 0x01);
264 test_device_input(data.mouse, INPUT_MOUSE, MOUSEEVENTF_LEFTDOWN, 3);
267 /* The call fails with a zeroed GUID */
268 memset(&af.guidActionMap, 0, sizeof(GUID));
269 hr = IDirectInput8_EnumDevicesBySemantics(pDI, 0, &af, enumeration_callback, 0, 0);
270 todo_wine ok(FAILED(hr), "EnumDevicesBySemantics succeeded with invalid GUID hr=%08x\n", hr);
273 START_TEST(device)
275 CoInitialize(NULL);
277 test_action_mapping();
279 CoUninitialize();