2 * Copyright (c) 2004-2005 Robert Reif
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define DIRECTINPUT_VERSION 0x0700
27 #include "wine/test.h"
32 #include "dinput_test.h"
34 #define numObjects(x) (sizeof(x) / sizeof(x[0]))
36 typedef struct tagUserData {
41 static const DIOBJECTDATAFORMAT dfDIJoystickTest[] = {
42 { &GUID_XAxis,DIJOFS_X,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
43 { &GUID_YAxis,DIJOFS_Y,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
44 { &GUID_ZAxis,DIJOFS_Z,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
45 { &GUID_RxAxis,DIJOFS_RX,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
46 { &GUID_RyAxis,DIJOFS_RY,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
47 { &GUID_RzAxis,DIJOFS_RZ,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
48 { &GUID_Button,DIJOFS_BUTTON(0),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
49 { &GUID_Button,DIJOFS_BUTTON(1),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
50 { &GUID_Button,DIJOFS_BUTTON(2),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
51 { &GUID_Button,DIJOFS_BUTTON(3),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
52 { &GUID_Button,DIJOFS_BUTTON(4),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
53 { &GUID_Button,DIJOFS_BUTTON(5),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
54 { &GUID_Button,DIJOFS_BUTTON(6),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
55 { &GUID_Button,DIJOFS_BUTTON(7),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
56 { &GUID_Button,DIJOFS_BUTTON(8),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
57 { &GUID_Button,DIJOFS_BUTTON(9),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
58 { &GUID_Button,DIJOFS_BUTTON(10),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
61 static const DIDATAFORMAT c_dfDIJoystickTest = {
63 sizeof(DIOBJECTDATAFORMAT),
66 numObjects(dfDIJoystickTest),
67 (LPDIOBJECTDATAFORMAT)dfDIJoystickTest
70 static HWND get_hwnd(void)
72 HWND hwnd=GetForegroundWindow();
74 hwnd=GetDesktopWindow();
78 typedef struct tagJoystickInfo
80 LPDIRECTINPUTDEVICE pJoystick;
88 static BOOL CALLBACK EnumAxes(
89 const DIDEVICEOBJECTINSTANCE* pdidoi,
93 JoystickInfo * info = (JoystickInfo *)pContext;
95 if (IsEqualIID(&pdidoi->guidType, &GUID_XAxis) ||
96 IsEqualIID(&pdidoi->guidType, &GUID_YAxis) ||
97 IsEqualIID(&pdidoi->guidType, &GUID_ZAxis) ||
98 IsEqualIID(&pdidoi->guidType, &GUID_RxAxis) ||
99 IsEqualIID(&pdidoi->guidType, &GUID_RyAxis) ||
100 IsEqualIID(&pdidoi->guidType, &GUID_RzAxis) ||
101 IsEqualIID(&pdidoi->guidType, &GUID_Slider))
106 diprg.diph.dwSize = sizeof(DIPROPRANGE);
107 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
108 diprg.diph.dwHow = DIPH_BYID;
109 diprg.diph.dwObj = pdidoi->dwType;
111 dipdw.diph.dwSize = sizeof(dipdw);
112 dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
113 dipdw.diph.dwHow = DIPH_BYID;
114 dipdw.diph.dwObj = pdidoi->dwType;
116 hr = IDirectInputDevice_GetProperty(info->pJoystick, DIPROP_RANGE, &diprg.diph);
117 ok(SUCCEEDED(hr), "IDirectInputDevice_GetProperty() failed: %s\n", DXGetErrorString8(hr));
118 ok(info->lMin == diprg.lMin && info->lMax == diprg.lMax, "Min/Max range invalid: "
119 "expected %d..%d got %d..%d\n", info->lMin, info->lMax, diprg.lMin, diprg.lMax);
124 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_RANGE, NULL);
125 ok(hr==E_INVALIDARG,"IDirectInputDevice_SetProperty() should have returned "
126 "E_INVALIDARG, returned: %s\n", DXGetErrorString8(hr));
128 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_RANGE, &diprg.diph);
129 ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %s\n", DXGetErrorString8(hr));
132 hr = IDirectInputDevice_GetProperty(info->pJoystick, DIPROP_DEADZONE, &dipdw.diph);
133 ok(SUCCEEDED(hr), "IDirectInputDevice_GetProperty() failed: %s\n", DXGetErrorString8(hr));
134 ok(info->dZone == dipdw.dwData, "deadzone invalid: expected %d got %d\n",
135 info->dZone, dipdw.dwData);
139 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_DEADZONE, &dipdw.diph);
140 ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %s\n", DXGetErrorString8(hr));
143 } else if (IsEqualIID(&pdidoi->guidType, &GUID_POV))
145 else if (IsEqualIID(&pdidoi->guidType, &GUID_Button))
148 return DIENUM_CONTINUE;
151 static const HRESULT SetCoop_null_window[16] = {
152 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
153 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
154 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
155 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
157 static const HRESULT SetCoop_real_window[16] = {
158 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
159 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
160 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
161 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
163 static BOOL CALLBACK EnumJoysticks(
164 LPCDIDEVICEINSTANCE lpddi,
168 UserData * data = (UserData *)pvRef;
169 LPDIRECTINPUTDEVICE pJoystick;
176 DIDEVICEINSTANCE inst;
177 DIDEVICEINSTANCE_DX3 inst3;
178 HWND hWnd = get_hwnd();
179 char oldstate[248], curstate[248];
181 ok(data->version > 0x0300, "Joysticks not supported in version 0x%04x\n", data->version);
183 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance, NULL, NULL);
184 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
185 "E_POINTER, returned: %s\n", DXGetErrorString8(hr));
187 hr = IDirectInput_CreateDevice(data->pDI, NULL, &pJoystick, NULL);
188 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
189 "E_POINTER, returned: %s\n", DXGetErrorString8(hr));
191 hr = IDirectInput_CreateDevice(data->pDI, NULL, NULL, NULL);
192 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
193 "E_POINTER, returned: %s\n", DXGetErrorString8(hr));
195 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance,
197 ok(hr==DI_OK,"IDirectInput_CreateDevice() failed: %s\n",
198 DXGetErrorString8(hr));
202 trace("---- %s ----\n", lpddi->tszProductName);
204 hr = IDirectInputDevice_SetDataFormat(pJoystick, NULL);
205 ok(hr==E_POINTER,"IDirectInputDevice_SetDataFormat() should have returned "
206 "E_POINTER, returned: %s\n", DXGetErrorString8(hr));
208 ZeroMemory(&format, sizeof(format));
209 hr = IDirectInputDevice_SetDataFormat(pJoystick, &format);
210 ok(hr==DIERR_INVALIDPARAM,"IDirectInputDevice_SetDataFormat() should have "
211 "returned DIERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(hr));
213 /* try the default formats */
214 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick);
215 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
216 DXGetErrorString8(hr));
218 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
219 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
220 DXGetErrorString8(hr));
222 /* try an alternate format */
223 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystickTest);
224 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
225 DXGetErrorString8(hr));
227 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
228 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
229 DXGetErrorString8(hr));
235 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, NULL, i);
236 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
240 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd, i);
241 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
244 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd,
245 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
246 ok(hr==DI_OK,"IDirectInputDevice_SetCooperativeLevel() failed: %s\n",
247 DXGetErrorString8(hr));
249 /* get capabilities */
250 hr = IDirectInputDevice_GetCapabilities(pJoystick, NULL);
251 ok(hr==E_POINTER,"IDirectInputDevice_GetCapabilities() "
252 "should have returned E_POINTER, returned: %s\n",
253 DXGetErrorString8(hr));
255 ZeroMemory(&caps, sizeof(caps));
256 hr = IDirectInputDevice_GetCapabilities(pJoystick, &caps);
257 ok(hr==DIERR_INVALIDPARAM,"IDirectInputDevice_GetCapabilities() "
258 "should have returned DIERR_INVALIDPARAM, returned: %s\n",
259 DXGetErrorString8(hr));
261 caps.dwSize = sizeof(caps);
262 hr = IDirectInputDevice_GetCapabilities(pJoystick, &caps);
263 ok(hr==DI_OK,"IDirectInputDevice_GetCapabilities() failed: %s\n",
264 DXGetErrorString8(hr));
266 ZeroMemory(&info, sizeof(info));
267 info.pJoystick = pJoystick;
269 /* default min/max limits */
272 /* enumerate objects */
273 hr = IDirectInputDevice_EnumObjects(pJoystick, EnumAxes, (VOID*)&info, DIDFT_ALL);
274 ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %s\n",
275 DXGetErrorString8(hr));
277 ok(caps.dwAxes == info.axis, "Number of enumerated axes doesn't match capabilities\n");
278 ok(caps.dwButtons == info.button, "Number of enumerated buttons doesn't match capabilities\n");
279 ok(caps.dwPOVs == info.pov, "Number of enumerated POVs doesn't match capabilities\n");
281 /* Set format and check limits again */
282 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
283 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
287 hr = IDirectInputDevice_EnumObjects(pJoystick, EnumAxes, (VOID*)&info, DIDFT_ALL);
288 ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %s\n", DXGetErrorString8(hr));
290 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, 0);
291 ok(hr==E_POINTER, "IDirectInputDevice_GetDeviceInfo() "
292 "should have returned E_POINTER, returned: %s\n",
293 DXGetErrorString8(hr));
295 ZeroMemory(&inst, sizeof(inst));
296 ZeroMemory(&inst3, sizeof(inst3));
298 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, &inst);
299 ok(hr==DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceInfo() "
300 "should have returned DIERR_INVALIDPARAM, returned: %s\n",
301 DXGetErrorString8(hr));
303 inst.dwSize = sizeof(inst);
304 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, &inst);
305 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceInfo() failed: %s\n",
306 DXGetErrorString8(hr));
308 inst3.dwSize = sizeof(inst3);
309 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, (LPDIDEVICEINSTANCE)&inst3);
310 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceInfo() failed: %s\n",
311 DXGetErrorString8(hr));
313 hr = IDirectInputDevice_Unacquire(pJoystick);
314 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have returned S_FALSE, got: %s\n",
315 DXGetErrorString8(hr));
317 hr = IDirectInputDevice_Acquire(pJoystick);
318 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %s\n",
319 DXGetErrorString8(hr));
323 hr = IDirectInputDevice_Acquire(pJoystick);
324 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have returned S_FALSE, got: %s\n",
325 DXGetErrorString8(hr));
327 if (winetest_interactive) {
328 trace("You have 30 seconds to test all axes, sliders, POVs and buttons\n");
335 for (i = 0; i < count; i++) {
336 hr = IDirectInputDevice_GetDeviceState(pJoystick, sizeof(DIJOYSTATE2), &js);
337 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceState() failed: %s\n",
338 DXGetErrorString8(hr));
341 sprintf(curstate, "X%5d Y%5d Z%5d Rx%5d Ry%5d Rz%5d "
342 "S0%5d S1%5d POV0%5d POV1%5d POV2%5d POV3%5d "
343 "B %d %d %d %d %d %d %d %d %d %d %d %d\n",
344 js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz,
345 js.rglSlider[0], js.rglSlider[1],
346 js.rgdwPOV[0], js.rgdwPOV[1], js.rgdwPOV[2], js.rgdwPOV[3],
347 js.rgbButtons[0]>>7, js.rgbButtons[1]>>7, js.rgbButtons[2]>>7,
348 js.rgbButtons[3]>>7, js.rgbButtons[4]>>7, js.rgbButtons[5]>>7,
349 js.rgbButtons[6]>>7, js.rgbButtons[7]>>7, js.rgbButtons[8]>>7,
350 js.rgbButtons[9]>>7, js.rgbButtons[10]>>7, js.rgbButtons[11]>>7);
351 if (strcmp(oldstate, curstate) != 0)
354 strcpy(oldstate, curstate);
360 hr = IDirectInputDevice_Unacquire(pJoystick);
361 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %s\n",
362 DXGetErrorString8(hr));
365 ref = IDirectInputDevice_Release(pJoystick);
366 ok(ref==0,"IDirectInputDevice_Release() reference count = %d\n", ref);
369 return DIENUM_CONTINUE;
372 static void joystick_tests(DWORD version)
377 HINSTANCE hInstance = GetModuleHandle(NULL);
379 trace("-- Testing Direct Input Version 0x%04x --\n", version);
380 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
381 ok(hr==DI_OK||hr==DIERR_OLDDIRECTINPUTVERSION,
382 "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
383 if (hr==DI_OK && pDI!=0) {
386 data.version = version;
387 hr = IDirectInput_EnumDevices(pDI, DIDEVTYPE_JOYSTICK, EnumJoysticks,
388 &data, DIEDFL_ALLDEVICES);
389 ok(hr==DI_OK,"IDirectInput_EnumDevices() failed: %s\n",
390 DXGetErrorString8(hr));
391 ref = IDirectInput_Release(pDI);
392 ok(ref==0,"IDirectInput_Release() reference count = %d\n", ref);
393 } else if (hr==DIERR_OLDDIRECTINPUTVERSION)
394 trace(" Version Not Supported\n");
401 trace("DLL Version: %s\n", get_file_version("dinput.dll"));
403 joystick_tests(0x0700);
404 joystick_tests(0x0500);
405 joystick_tests(0x0300);