quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / dinput / tests / joystick.c
blob0251f35eef5baf4b5b8f88ce261c2fa3b89d16af
1 /*
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
21 #define COBJMACROS
22 #include <windows.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
33 #define numObjects(x) (sizeof(x) / sizeof(x[0]))
35 typedef struct tagUserData {
36 LPDIRECTINPUT pDI;
37 DWORD version;
38 } UserData;
40 static const DIOBJECTDATAFORMAT dfDIJoystickTest[] = {
41 { &GUID_XAxis,DIJOFS_X,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
42 { &GUID_YAxis,DIJOFS_Y,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
43 { &GUID_ZAxis,DIJOFS_Z,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
44 { &GUID_RxAxis,DIJOFS_RX,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
45 { &GUID_RyAxis,DIJOFS_RY,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
46 { &GUID_RzAxis,DIJOFS_RZ,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
47 { &GUID_Button,DIJOFS_BUTTON(0),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
48 { &GUID_Button,DIJOFS_BUTTON(1),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
49 { &GUID_Button,DIJOFS_BUTTON(2),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
50 { &GUID_Button,DIJOFS_BUTTON(3),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
51 { &GUID_Button,DIJOFS_BUTTON(4),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
52 { &GUID_Button,DIJOFS_BUTTON(5),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
53 { &GUID_Button,DIJOFS_BUTTON(6),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
54 { &GUID_Button,DIJOFS_BUTTON(7),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
55 { &GUID_Button,DIJOFS_BUTTON(8),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
56 { &GUID_Button,DIJOFS_BUTTON(9),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
57 { &GUID_Button,DIJOFS_BUTTON(10),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
60 static const DIDATAFORMAT c_dfDIJoystickTest = {
61 sizeof(DIDATAFORMAT),
62 sizeof(DIOBJECTDATAFORMAT),
63 DIDF_ABSAXIS,
64 sizeof(DIJOYSTATE2),
65 numObjects(dfDIJoystickTest),
66 (LPDIOBJECTDATAFORMAT)dfDIJoystickTest
69 static HWND get_hwnd(void)
71 HWND hwnd=GetForegroundWindow();
72 if (!hwnd)
73 hwnd=GetDesktopWindow();
74 return hwnd;
77 typedef struct tagJoystickInfo
79 LPDIRECTINPUTDEVICE pJoystick;
80 DWORD axis;
81 DWORD pov;
82 DWORD button;
83 LONG lMin, lMax;
84 DWORD dZone;
85 } JoystickInfo;
87 static int get_refcount(IUnknown *object)
89 IUnknown_AddRef( object );
90 return IUnknown_Release( object );
93 static BOOL CALLBACK EnumAxes(
94 const DIDEVICEOBJECTINSTANCE* pdidoi,
95 VOID* pContext)
97 HRESULT hr;
98 JoystickInfo * info = pContext;
100 if (IsEqualIID(&pdidoi->guidType, &GUID_XAxis) ||
101 IsEqualIID(&pdidoi->guidType, &GUID_YAxis) ||
102 IsEqualIID(&pdidoi->guidType, &GUID_ZAxis) ||
103 IsEqualIID(&pdidoi->guidType, &GUID_RxAxis) ||
104 IsEqualIID(&pdidoi->guidType, &GUID_RyAxis) ||
105 IsEqualIID(&pdidoi->guidType, &GUID_RzAxis) ||
106 IsEqualIID(&pdidoi->guidType, &GUID_Slider))
108 DIPROPRANGE diprg;
109 DIPROPDWORD dipdw;
111 diprg.diph.dwSize = sizeof(DIPROPRANGE);
112 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
113 diprg.diph.dwHow = DIPH_BYID;
114 diprg.diph.dwObj = pdidoi->dwType;
116 dipdw.diph.dwSize = sizeof(dipdw);
117 dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
118 dipdw.diph.dwHow = DIPH_BYID;
119 dipdw.diph.dwObj = pdidoi->dwType;
121 hr = IDirectInputDevice_GetProperty(info->pJoystick, DIPROP_RANGE, &diprg.diph);
122 ok(SUCCEEDED(hr), "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
123 ok(info->lMin == diprg.lMin && info->lMax == diprg.lMax, "Min/Max range invalid: "
124 "expected %d..%d got %d..%d\n", info->lMin, info->lMax, diprg.lMin, diprg.lMax);
126 diprg.lMin = -2000;
127 diprg.lMax = +2000;
129 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_RANGE, NULL);
130 ok(hr==E_INVALIDARG,"IDirectInputDevice_SetProperty() should have returned "
131 "E_INVALIDARG, returned: %08x\n", hr);
133 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_RANGE, &diprg.diph);
134 ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %08x\n", hr);
136 /* dead zone */
137 hr = IDirectInputDevice_GetProperty(info->pJoystick, DIPROP_DEADZONE, &dipdw.diph);
138 ok(SUCCEEDED(hr), "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
139 ok(info->dZone == dipdw.dwData, "deadzone invalid: expected %d got %d\n",
140 info->dZone, dipdw.dwData);
142 dipdw.dwData = 123;
144 hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_DEADZONE, &dipdw.diph);
145 ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %08x\n", hr);
147 info->axis++;
148 } else if (IsEqualIID(&pdidoi->guidType, &GUID_POV))
149 info->pov++;
150 else if (IsEqualIID(&pdidoi->guidType, &GUID_Button))
151 info->button++;
153 return DIENUM_CONTINUE;
156 static const HRESULT SetCoop_null_window[16] = {
157 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
158 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
159 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
160 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
162 static const HRESULT SetCoop_real_window[16] = {
163 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
164 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
165 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
166 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
168 static BOOL CALLBACK EnumJoysticks(
169 LPCDIDEVICEINSTANCE lpddi,
170 LPVOID pvRef)
172 HRESULT hr;
173 UserData * data = pvRef;
174 LPDIRECTINPUTDEVICE pJoystick;
175 DIDATAFORMAT format;
176 DIDEVCAPS caps;
177 DIJOYSTATE2 js;
178 JoystickInfo info;
179 int i, count;
180 ULONG ref;
181 DIDEVICEINSTANCE inst;
182 DIDEVICEINSTANCE_DX3 inst3;
183 HWND hWnd = get_hwnd();
184 char oldstate[248], curstate[248];
186 ok(data->version > 0x0300, "Joysticks not supported in version 0x%04x\n", data->version);
188 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance, NULL, NULL);
189 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
190 "E_POINTER, returned: %08x\n", hr);
192 hr = IDirectInput_CreateDevice(data->pDI, NULL, &pJoystick, NULL);
193 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
194 "E_POINTER, returned: %08x\n", hr);
196 hr = IDirectInput_CreateDevice(data->pDI, NULL, NULL, NULL);
197 ok(hr==E_POINTER,"IDirectInput_CreateDevice() should have returned "
198 "E_POINTER, returned: %08x\n", hr);
200 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance,
201 &pJoystick, NULL);
202 ok(hr==DI_OK,"IDirectInput_CreateDevice() failed: %08x\n", hr);
203 if (hr!=DI_OK)
204 goto DONE;
206 trace("---- %s ----\n", lpddi->tszProductName);
208 hr = IDirectInputDevice_SetDataFormat(pJoystick, NULL);
209 ok(hr==E_POINTER,"IDirectInputDevice_SetDataFormat() should have returned "
210 "E_POINTER, returned: %08x\n", hr);
212 ZeroMemory(&format, sizeof(format));
213 hr = IDirectInputDevice_SetDataFormat(pJoystick, &format);
214 ok(hr==DIERR_INVALIDPARAM,"IDirectInputDevice_SetDataFormat() should have "
215 "returned DIERR_INVALIDPARAM, returned: %08x\n", hr);
217 /* try the default formats */
218 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick);
219 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
221 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
222 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
224 /* try an alternate format */
225 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystickTest);
226 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
228 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
229 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
230 if (hr != DI_OK)
231 goto RELEASE;
233 for (i=0; i<16; i++)
235 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, NULL, i);
236 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
238 for (i=0; i<16; i++)
240 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd, i);
241 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
244 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd,
245 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
246 ok(hr==DI_OK,"IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
248 /* get capabilities */
249 hr = IDirectInputDevice_GetCapabilities(pJoystick, NULL);
250 ok(hr==E_POINTER,"IDirectInputDevice_GetCapabilities() "
251 "should have returned E_POINTER, returned: %08x\n", hr);
253 ZeroMemory(&caps, sizeof(caps));
254 hr = IDirectInputDevice_GetCapabilities(pJoystick, &caps);
255 ok(hr==DIERR_INVALIDPARAM,"IDirectInputDevice_GetCapabilities() "
256 "should have returned DIERR_INVALIDPARAM, returned: %08x\n", hr);
258 caps.dwSize = sizeof(caps);
259 hr = IDirectInputDevice_GetCapabilities(pJoystick, &caps);
260 ok(hr==DI_OK,"IDirectInputDevice_GetCapabilities() failed: %08x\n", hr);
262 ZeroMemory(&info, sizeof(info));
263 info.pJoystick = pJoystick;
265 /* default min/max limits */
266 info.lMin = 0;
267 info.lMax = 0xffff;
268 /* enumerate objects */
269 hr = IDirectInputDevice_EnumObjects(pJoystick, EnumAxes, &info, DIDFT_ALL);
270 ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %08x\n", hr);
272 ok(caps.dwAxes == info.axis, "Number of enumerated axes (%d) doesn't match capabilities (%d)\n", info.axis, caps.dwAxes);
273 ok(caps.dwButtons == info.button, "Number of enumerated buttons (%d) doesn't match capabilities (%d)\n", info.button, caps.dwButtons);
274 ok(caps.dwPOVs == info.pov, "Number of enumerated POVs (%d) doesn't match capabilities (%d)\n", info.pov, caps.dwPOVs);
276 /* Set format and check limits again */
277 hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
278 ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
279 info.lMin = -2000;
280 info.lMax = +2000;
281 info.dZone= 123;
282 hr = IDirectInputDevice_EnumObjects(pJoystick, EnumAxes, &info, DIDFT_ALL);
283 ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %08x\n", hr);
285 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, 0);
286 ok(hr==E_POINTER, "IDirectInputDevice_GetDeviceInfo() "
287 "should have returned E_POINTER, returned: %08x\n", hr);
289 ZeroMemory(&inst, sizeof(inst));
290 ZeroMemory(&inst3, sizeof(inst3));
292 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, &inst);
293 ok(hr==DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceInfo() "
294 "should have returned DIERR_INVALIDPARAM, returned: %08x\n", hr);
296 inst.dwSize = sizeof(inst);
297 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, &inst);
298 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceInfo() failed: %08x\n", hr);
300 inst3.dwSize = sizeof(inst3);
301 hr = IDirectInputDevice_GetDeviceInfo(pJoystick, (LPDIDEVICEINSTANCE)&inst3);
302 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceInfo() failed: %08x\n", hr);
304 hr = IDirectInputDevice_Unacquire(pJoystick);
305 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have returned S_FALSE, got: %08x\n", hr);
307 hr = IDirectInputDevice_Acquire(pJoystick);
308 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
309 if (hr != DI_OK)
310 goto RELEASE;
312 hr = IDirectInputDevice_Acquire(pJoystick);
313 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have returned S_FALSE, got: %08x\n", hr);
315 if (info.pov < 4)
317 hr = IDirectInputDevice_GetDeviceState(pJoystick, sizeof(DIJOYSTATE2), &js);
318 ok(hr == DI_OK, "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
319 ok(js.rgdwPOV[3] == -1, "Default for unassigned POV should be -1 not: %d\n", js.rgdwPOV[3]);
322 if (caps.dwFlags & DIDC_FORCEFEEDBACK)
324 DWORD axes[2] = {DIJOFS_X, DIJOFS_Y};
325 LONG direction[2] = {0, 0};
326 DICONSTANTFORCE force = {0};
327 DIEFFECT eff;
328 LPDIRECTINPUTEFFECT effect = NULL;
329 LONG cnt1, cnt2;
330 HWND real_hWnd;
331 HINSTANCE hInstance = GetModuleHandle(NULL);
332 DIPROPDWORD dip_gain_set, dip_gain_get;
334 trace("Testing force-feedback\n");
335 memset(&eff, 0, sizeof(eff));
336 eff.dwSize = sizeof(eff);
337 eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
338 eff.dwDuration = INFINITE;
339 eff.dwGain = DI_FFNOMINALMAX;
340 eff.dwTriggerButton = DIEB_NOTRIGGER;
341 eff.cAxes = sizeof(axes) / sizeof(axes[0]);
342 eff.rgdwAxes = axes;
343 eff.rglDirection = direction;
344 eff.cbTypeSpecificParams = sizeof(force);
345 eff.lpvTypeSpecificParams = &force;
347 /* Sending effects to joystick requires
348 * calling IDirectInputEffect_Initialize, which requires
349 * having exclusive access to the device, which requires
350 * - not having acquired the joystick when calling
351 * IDirectInputDevice_SetCooperativeLevel
352 * - a visible window
354 real_hWnd = CreateWindowEx(0, "EDIT", "Test text", 0, 10, 10, 300,
355 300, NULL, NULL, hInstance, NULL);
356 ok(real_hWnd!=0,"CreateWindowEx failed: %p\n", real_hWnd);
357 ShowWindow(real_hWnd, SW_SHOW);
358 hr = IDirectInputDevice_Unacquire(pJoystick);
359 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
360 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, real_hWnd,
361 DISCL_EXCLUSIVE | DISCL_FOREGROUND);
362 ok(hr==DI_OK,"IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
363 hr = IDirectInputDevice_Acquire(pJoystick);
364 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
366 cnt1 = get_refcount((IUnknown*)pJoystick);
368 hr = IDirectInputDevice2_CreateEffect((LPDIRECTINPUTDEVICE2)pJoystick, &GUID_ConstantForce,
369 &eff, &effect, NULL);
370 ok(hr == DI_OK, "IDirectInputDevice_CreateEffect() failed: %08x\n", hr);
371 cnt2 = get_refcount((IUnknown*)pJoystick);
372 ok(cnt1 == cnt2, "Ref count is wrong %d != %d\n", cnt1, cnt2);
374 if (effect)
376 DWORD effect_status;
377 struct DIPROPDWORD diprop_word;
378 GUID guid = {0};
380 hr = IDirectInputEffect_Initialize(effect, hInstance, data->version,
381 &GUID_ConstantForce);
382 ok(hr==DI_OK,"IDirectInputEffect_Initialize failed: %08x\n", hr);
383 hr = IDirectInputEffect_SetParameters(effect, &eff, DIEP_AXES | DIEP_DIRECTION |
384 DIEP_TYPESPECIFICPARAMS);
385 ok(hr==DI_OK,"IDirectInputEffect_SetParameters failed: %08x\n", hr);
386 if (hr==DI_OK) {
387 /* Test that upload, unacquire, acquire still permits updating
388 * uploaded effect. */
389 hr = IDirectInputDevice_Unacquire(pJoystick);
390 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
391 hr = IDirectInputDevice_Acquire(pJoystick);
392 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
393 hr = IDirectInputEffect_SetParameters(effect, &eff, DIEP_GAIN);
394 ok(hr==DI_OK,"IDirectInputEffect_SetParameters failed: %08x\n", hr);
397 /* Check effect status.
398 * State: initially stopped
399 * start
400 * State: started
401 * unacquire, acquire, download
402 * State: stopped
403 * start
404 * State: started
406 * Shows that:
407 * - effects are stopped after Unacquire + Acquire
408 * - effects are preserved (Download + Start doesn't complain
409 * about incomplete effect)
411 hr = IDirectInputEffect_GetEffectStatus(effect, &effect_status);
412 ok(hr==DI_OK,"IDirectInputEffect_GetEffectStatus() failed: %08x\n", hr);
413 ok(effect_status==0,"IDirectInputEffect_GetEffectStatus() reported effect as started\n");
414 hr = IDirectInputEffect_SetParameters(effect, &eff, DIEP_START);
415 ok(hr==DI_OK,"IDirectInputEffect_SetParameters failed: %08x\n", hr);
416 hr = IDirectInputEffect_GetEffectStatus(effect, &effect_status);
417 ok(hr==DI_OK,"IDirectInputEffect_GetEffectStatus() failed: %08x\n", hr);
418 todo_wine ok(effect_status!=0,"IDirectInputEffect_GetEffectStatus() reported effect as stopped\n");
419 hr = IDirectInputDevice_Unacquire(pJoystick);
420 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
421 hr = IDirectInputDevice_Acquire(pJoystick);
422 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
423 hr = IDirectInputEffect_Download(effect);
424 ok(hr==DI_OK,"IDirectInputEffect_Download() failed: %08x\n", hr);
425 hr = IDirectInputEffect_GetEffectStatus(effect, &effect_status);
426 ok(hr==DI_OK,"IDirectInputEffect_GetEffectStatus() failed: %08x\n", hr);
427 ok(effect_status==0,"IDirectInputEffect_GetEffectStatus() reported effect as started\n");
428 hr = IDirectInputEffect_Start(effect, 1, 0);
429 ok(hr==DI_OK,"IDirectInputEffect_Start() failed: %08x\n", hr);
430 hr = IDirectInputEffect_GetEffectStatus(effect, &effect_status);
431 ok(hr==DI_OK,"IDirectInputEffect_GetEffectStatus() failed: %08x\n", hr);
432 todo_wine ok(effect_status!=0,"IDirectInputEffect_GetEffectStatus() reported effect as stopped\n");
433 hr = IDirectInputEffect_GetEffectGuid(effect, &guid);
434 ok(hr==DI_OK,"IDirectInputEffect_GetEffectGuid() failed: %08x\n", hr);
435 ok(IsEqualGUID(&GUID_ConstantForce, &guid), "Wrong guid returned\n");
437 /* Check autocenter status
438 * State: initially stopped
439 * enable
440 * State: enabled
441 * acquire
442 * State: enabled
443 * unacquire
444 * State: enabled
446 * IDirectInputDevice2_SetProperty(DIPROP_AUTOCENTER) can only be
447 * executed when devide is released.
449 * If Executed interactively, user can feel that autocenter is
450 * only disabled when joystick is acquired.
452 diprop_word.diph.dwSize = sizeof(diprop_word);
453 diprop_word.diph.dwHeaderSize = sizeof(diprop_word.diph);
454 diprop_word.diph.dwObj = 0;
455 diprop_word.diph.dwHow = DIPH_DEVICE;
456 hr = IDirectInputDevice_Unacquire(pJoystick);
457 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
458 hr = IDirectInputDevice2_GetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
459 ok(hr==DI_OK,"IDirectInputDevice2_GetProperty() failed: %08x\n", hr);
460 ok(diprop_word.dwData==DIPROPAUTOCENTER_ON,"IDirectInputDevice2_GetProperty() reported autocenter as disabled\n");
461 diprop_word.dwData = DIPROPAUTOCENTER_OFF;
462 hr = IDirectInputDevice2_SetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
463 ok(hr==DI_OK,"IDirectInputDevice2_SetProperty() failed: %08x\n", hr);
464 hr = IDirectInputDevice2_GetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
465 ok(hr==DI_OK,"IDirectInputDevice2_GetProperty() failed: %08x\n", hr);
466 ok(diprop_word.dwData==DIPROPAUTOCENTER_OFF,"IDirectInputDevice2_GetProperty() reported autocenter as enabled\n");
467 if (winetest_interactive) {
468 trace("Acquiring in 2s, autocenter will be disabled.\n");
469 Sleep(2000);
471 hr = IDirectInputDevice_Acquire(pJoystick);
472 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
473 if (winetest_interactive)
474 trace("Acquired.\n");
475 hr = IDirectInputDevice2_GetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
476 ok(hr==DI_OK,"IDirectInputDevice2_GetProperty() failed: %08x\n", hr);
477 ok(diprop_word.dwData==DIPROPAUTOCENTER_OFF,"IDirectInputDevice2_GetProperty() reported autocenter as enabled\n");
478 if (winetest_interactive) {
479 trace("Releasing in 2s, autocenter will be re-enabled.\n");
480 Sleep(2000);
482 hr = IDirectInputDevice_Unacquire(pJoystick);
483 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
484 if (winetest_interactive)
485 trace("Released\n");
486 hr = IDirectInputDevice2_GetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
487 ok(hr==DI_OK,"IDirectInputDevice2_GetProperty() failed: %08x\n", hr);
488 ok(diprop_word.dwData==DIPROPAUTOCENTER_OFF,"IDirectInputDevice2_GetProperty() reported autocenter as enabled\n");
489 hr = IDirectInputDevice_Acquire(pJoystick);
490 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
491 hr = IDirectInputDevice2_GetProperty(pJoystick, DIPROP_AUTOCENTER, &diprop_word.diph);
492 ok(hr==DI_OK,"IDirectInputDevice2_GetProperty() failed: %08x\n", hr);
494 /* Device gain (DIPROP_FFGAIN).
495 * From MSDN:
496 * 0..10000 range, otherwise DIERR_INVALIDPARAM.
497 * Can be changed even if device is acquired.
498 * Difference found by tests:
499 * <0 is refused, >10000 is accepted
501 dip_gain_set.diph.dwSize = sizeof(DIPROPDWORD);
502 dip_gain_set.diph.dwHeaderSize = sizeof(DIPROPHEADER);
503 dip_gain_set.diph.dwObj = 0;
504 dip_gain_set.diph.dwHow = DIPH_DEVICE;
505 dip_gain_set.dwData = 10000;
506 dip_gain_get.diph.dwSize = sizeof(DIPROPDWORD);
507 dip_gain_get.diph.dwHeaderSize = sizeof(DIPROPHEADER);
508 dip_gain_get.diph.dwObj = 0;
509 dip_gain_get.diph.dwHow = DIPH_DEVICE;
510 dip_gain_get.dwData = 0;
512 /* Test device is acquisition (non)impact. */
513 hr = IDirectInputDevice_Unacquire(pJoystick);
514 ok(hr == DI_OK, "IDirectInputDevice_Unacquire() should have returned S_FALSE, got: %08x\n", hr);
515 dip_gain_set.dwData = 1;
516 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
517 ok(hr==DI_OK, "IDirectInputDevice_SetProperty() failed: %08x\n", hr);
518 hr = IDirectInputDevice_GetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_get.diph);
519 ok(hr==DI_OK, "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
520 ok(dip_gain_get.dwData==dip_gain_set.dwData, "Gain not udated: %i\n", dip_gain_get.dwData);
521 hr = IDirectInputDevice_Acquire(pJoystick);
522 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
523 dip_gain_set.dwData = 2;
524 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
525 ok(hr==DI_OK, "IDirectInputDevice_SetProperty() failed: %08x\n", hr);
526 hr = IDirectInputDevice_GetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_get.diph);
527 ok(hr==DI_OK, "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
528 ok(dip_gain_get.dwData==dip_gain_set.dwData, "Gain not udated: %i\n", dip_gain_get.dwData);
529 /* Test range and internal clamping. */
530 dip_gain_set.dwData = -1;
531 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
532 todo_wine ok(hr==DIERR_INVALIDPARAM, "IDirectInputDevice_SetProperty() should have returned %08x: %08x\n", DIERR_INVALIDPARAM, hr);
533 dip_gain_set.dwData = 0;
534 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
535 ok(hr==DI_OK, "IDirectInputDevice_SetProperty() failed: %08x\n", hr);
536 hr = IDirectInputDevice_GetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_get.diph);
537 ok(hr==DI_OK, "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
538 ok(dip_gain_get.dwData==dip_gain_set.dwData, "Gain not updated: %i\n", dip_gain_get.dwData);
539 dip_gain_set.dwData = 10000;
540 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
541 ok(hr==DI_OK, "IDirectInputDevice_SetProperty() failed: %08x\n", hr);
542 hr = IDirectInputDevice_GetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_get.diph);
543 ok(hr==DI_OK, "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
544 ok(dip_gain_get.dwData==dip_gain_set.dwData, "Gain not updated: %i\n", dip_gain_get.dwData);
545 /* WARNING: This call succeeds, on the contrary of what is stated on MSDN. */
546 dip_gain_set.dwData = 10001;
547 hr = IDirectInputDevice_SetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_set.diph);
548 ok(hr==DI_OK, "IDirectInputDevice_SetProperty() failed: %08x\n", hr);
549 hr = IDirectInputDevice_GetProperty(pJoystick, DIPROP_FFGAIN, &dip_gain_get.diph);
550 ok(hr==DI_OK, "IDirectInputDevice_GetProperty() failed: %08x\n", hr);
551 ok(dip_gain_get.dwData==dip_gain_set.dwData, "Gain not updated: %i\n", dip_gain_get.dwData);
553 ref = IUnknown_Release(effect);
554 ok(ref == 0, "IDirectInputDevice_Release() reference count = %d\n", ref);
556 cnt1 = get_refcount((IUnknown*)pJoystick);
557 ok(cnt1 == cnt2, "Ref count is wrong %d != %d\n", cnt1, cnt2);
559 /* Before destroying the window, release joystick to revert to
560 * non-exclusive, background cooperative level. */
561 hr = IDirectInputDevice_Unacquire(pJoystick);
562 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
563 hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd,
564 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
565 ok(hr==DI_OK,"IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
566 DestroyWindow (real_hWnd);
567 hr = IDirectInputDevice_Acquire(pJoystick);
568 ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %08x\n", hr);
571 if (winetest_interactive) {
572 trace("You have 30 seconds to test all axes, sliders, POVs and buttons\n");
573 count = 300;
574 } else
575 count = 1;
577 trace("\n");
578 oldstate[0]='\0';
579 for (i = 0; i < count; i++) {
580 hr = IDirectInputDevice_GetDeviceState(pJoystick, sizeof(DIJOYSTATE2), &js);
581 ok(hr==DI_OK,"IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
582 if (hr != DI_OK)
583 break;
584 sprintf(curstate, "X%5d Y%5d Z%5d Rx%5d Ry%5d Rz%5d "
585 "S0%5d S1%5d POV0%5d POV1%5d POV2%5d POV3%5d "
586 "B %d %d %d %d %d %d %d %d %d %d %d %d\n",
587 js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz,
588 js.rglSlider[0], js.rglSlider[1],
589 js.rgdwPOV[0], js.rgdwPOV[1], js.rgdwPOV[2], js.rgdwPOV[3],
590 js.rgbButtons[0]>>7, js.rgbButtons[1]>>7, js.rgbButtons[2]>>7,
591 js.rgbButtons[3]>>7, js.rgbButtons[4]>>7, js.rgbButtons[5]>>7,
592 js.rgbButtons[6]>>7, js.rgbButtons[7]>>7, js.rgbButtons[8]>>7,
593 js.rgbButtons[9]>>7, js.rgbButtons[10]>>7, js.rgbButtons[11]>>7);
594 if (strcmp(oldstate, curstate) != 0)
596 trace("%s\n", curstate);
597 strcpy(oldstate, curstate);
599 Sleep(100);
601 trace("\n");
603 hr = IDirectInputDevice_Unacquire(pJoystick);
604 ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %08x\n", hr);
606 RELEASE:
607 ref = IDirectInputDevice_Release(pJoystick);
608 ok(ref==0,"IDirectInputDevice_Release() reference count = %d\n", ref);
610 DONE:
611 return DIENUM_CONTINUE;
614 static void joystick_tests(DWORD version)
616 HRESULT hr;
617 LPDIRECTINPUT pDI;
618 ULONG ref;
619 HINSTANCE hInstance = GetModuleHandle(NULL);
621 trace("-- Testing Direct Input Version 0x%04x --\n", version);
622 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
623 ok(hr==DI_OK||hr==DIERR_OLDDIRECTINPUTVERSION,
624 "DirectInputCreate() failed: %08x\n", hr);
625 if (hr==DI_OK && pDI!=0) {
626 UserData data;
627 data.pDI = pDI;
628 data.version = version;
629 hr = IDirectInput_EnumDevices(pDI, DIDEVTYPE_JOYSTICK, EnumJoysticks,
630 &data, DIEDFL_ALLDEVICES);
631 ok(hr==DI_OK,"IDirectInput_EnumDevices() failed: %08x\n", hr);
632 ref = IDirectInput_Release(pDI);
633 ok(ref==0,"IDirectInput_Release() reference count = %d\n", ref);
634 } else if (hr==DIERR_OLDDIRECTINPUTVERSION)
635 trace(" Version Not Supported\n");
638 START_TEST(joystick)
640 CoInitialize(NULL);
642 joystick_tests(0x0700);
643 joystick_tests(0x0500);
644 joystick_tests(0x0300);
646 CoUninitialize();