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