server: Avoid refreshing areas of a window that have already been copied by the X...
[wine] / dlls / dinput / tests / 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 DIRECTINPUT_VERSION 0x0700
20
21 #include <windows.h>
22
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "wine/test.h"
28 #include "windef.h"
29 #include "wingdi.h"
30 #include "dinput.h"
31 #include "dxerr8.h"
32 #include "dinput_test.h"
33
34 #define numObjects(x) (sizeof(x) / sizeof(x[0]))
35
36 typedef struct tagUserData {
37     LPDIRECTINPUT pDI;
38     DWORD version;
39 } UserData;
40
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},
59 };
60
61 static const DIDATAFORMAT c_dfDIJoystickTest = {
62     sizeof(DIDATAFORMAT),
63     sizeof(DIOBJECTDATAFORMAT),
64     DIDF_ABSAXIS,
65     sizeof(DIJOYSTATE2),
66     numObjects(dfDIJoystickTest),
67     (LPDIOBJECTDATAFORMAT)dfDIJoystickTest
68 };
69
70 static HWND get_hwnd(void)
71 {
72     HWND hwnd=GetForegroundWindow();
73     if (!hwnd)
74         hwnd=GetDesktopWindow();
75     return hwnd;
76 }
77
78 typedef struct tagJoystickInfo
79 {
80     LPDIRECTINPUTDEVICE pJoystick;
81     DWORD axis;
82     DWORD pov;
83     DWORD button;
84     LONG  lMin, lMax;
85     DWORD dZone;
86 } JoystickInfo;
87
88 static BOOL CALLBACK EnumAxes(
89     const DIDEVICEOBJECTINSTANCE* pdidoi,
90     VOID* pContext)
91 {
92     HRESULT hr;
93     JoystickInfo * info = (JoystickInfo *)pContext;
94
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))
102     {
103         DIPROPRANGE diprg;
104         DIPROPDWORD dipdw;
105
106         diprg.diph.dwSize       = sizeof(DIPROPRANGE);
107         diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
108         diprg.diph.dwHow        = DIPH_BYID;
109         diprg.diph.dwObj        = pdidoi->dwType;
110
111         dipdw.diph.dwSize       = sizeof(dipdw);
112         dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
113         dipdw.diph.dwHow        = DIPH_BYID;
114         dipdw.diph.dwObj        = pdidoi->dwType;
115
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);
120
121         diprg.lMin = -2000;
122         diprg.lMax = +2000;
123
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));
127
128         hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_RANGE, &diprg.diph);
129         ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %s\n", DXGetErrorString8(hr));
130
131         /* dead zone */
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);
136
137         dipdw.dwData = 123;
138
139         hr = IDirectInputDevice_SetProperty(info->pJoystick, DIPROP_DEADZONE, &dipdw.diph);
140         ok(hr==DI_OK,"IDirectInputDevice_SetProperty() failed: %s\n", DXGetErrorString8(hr));
141
142         info->axis++;
143     } else if (IsEqualIID(&pdidoi->guidType, &GUID_POV))
144         info->pov++;
145     else if (IsEqualIID(&pdidoi->guidType, &GUID_Button))
146         info->button++;
147
148     return DIENUM_CONTINUE;
149 }
150
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};
156
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};
162
163 static BOOL CALLBACK EnumJoysticks(
164     LPCDIDEVICEINSTANCE lpddi,
165     LPVOID pvRef)
166 {
167     HRESULT hr;
168     UserData * data = (UserData *)pvRef;
169     LPDIRECTINPUTDEVICE pJoystick;
170     DIDATAFORMAT format;
171     DIDEVCAPS caps;
172     DIJOYSTATE2 js;
173     JoystickInfo info;
174     int i, count;
175     ULONG ref;
176     DIDEVICEINSTANCE inst;
177     DIDEVICEINSTANCE_DX3 inst3;
178     HWND hWnd = get_hwnd();
179     char oldstate[248], curstate[248];
180
181     ok(data->version > 0x0300, "Joysticks not supported in version 0x%04x\n", data->version);
182  
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));
186
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));
190
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));
194
195     hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance,
196                                    &pJoystick, NULL);
197     ok(hr==DI_OK,"IDirectInput_CreateDevice() failed: %s\n",
198        DXGetErrorString8(hr));
199     if (hr!=DI_OK)
200         goto DONE;
201
202     trace("---- %s ----\n", lpddi->tszProductName);
203
204     hr = IDirectInputDevice_SetDataFormat(pJoystick, NULL);
205     ok(hr==E_POINTER,"IDirectInputDevice_SetDataFormat() should have returned "
206        "E_POINTER, returned: %s\n", DXGetErrorString8(hr));
207
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));
212
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));
217
218     hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
219     ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
220        DXGetErrorString8(hr));
221
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));
226
227     hr = IDirectInputDevice_SetDataFormat(pJoystick, &c_dfDIJoystick2);
228     ok(hr==DI_OK,"IDirectInputDevice_SetDataFormat() failed: %s\n",
229        DXGetErrorString8(hr));
230     if (hr != DI_OK)
231         goto RELEASE;
232
233     for (i=0; i<16; i++)
234     {
235         hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, NULL, i);
236         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
237     }
238     for (i=0; i<16; i++)
239     {
240         hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd, i);
241         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
242     }
243
244     hr = IDirectInputDevice_SetCooperativeLevel(pJoystick, hWnd,
245                                                 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
246     ok(hr==DI_OK,"IDirectInputDevice_SetCooperativeLevel() failed: %s\n",
247        DXGetErrorString8(hr));
248
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));
254
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));
260
261     caps.dwSize = sizeof(caps);
262     hr = IDirectInputDevice_GetCapabilities(pJoystick, &caps);
263     ok(hr==DI_OK,"IDirectInputDevice_GetCapabilities() failed: %s\n",
264        DXGetErrorString8(hr));
265
266     ZeroMemory(&info, sizeof(info));
267     info.pJoystick = pJoystick;
268
269     /* default min/max limits */
270     info.lMin = 0;
271     info.lMax = 0xffff;
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));
276
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");
280
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));
284     info.lMin = -2000;
285     info.lMax = +2000;
286     info.dZone= 123;
287     hr = IDirectInputDevice_EnumObjects(pJoystick, EnumAxes, (VOID*)&info, DIDFT_ALL);
288     ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %s\n", DXGetErrorString8(hr));
289
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));
294
295     ZeroMemory(&inst, sizeof(inst));
296     ZeroMemory(&inst3, sizeof(inst3));
297
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));
302
303     inst.dwSize = sizeof(inst);
304     hr = IDirectInputDevice_GetDeviceInfo(pJoystick, &inst);
305     ok(hr==DI_OK,"IDirectInputDevice_GetDeviceInfo() failed: %s\n",
306        DXGetErrorString8(hr));
307
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));
312
313     hr = IDirectInputDevice_Unacquire(pJoystick);
314     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have returned S_FALSE, got: %s\n",
315        DXGetErrorString8(hr));
316
317     hr = IDirectInputDevice_Acquire(pJoystick);
318     ok(hr==DI_OK,"IDirectInputDevice_Acquire() failed: %s\n",
319        DXGetErrorString8(hr));
320     if (hr != DI_OK)
321         goto RELEASE;
322
323     hr = IDirectInputDevice_Acquire(pJoystick);
324     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have returned S_FALSE, got: %s\n",
325        DXGetErrorString8(hr));
326
327     if (winetest_interactive) {
328         trace("You have 30 seconds to test all axes, sliders, POVs and buttons\n");
329         count = 300;
330     } else
331         count = 1;
332
333     trace("\n");
334     oldstate[0]='\0';
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));
339         if (hr != DI_OK)
340             break;
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)
352         {
353             trace(curstate);
354             strcpy(oldstate, curstate);
355         }
356         Sleep(100);
357     }
358     trace("\n");
359
360     hr = IDirectInputDevice_Unacquire(pJoystick);
361     ok(hr==DI_OK,"IDirectInputDevice_Unacquire() failed: %s\n",
362        DXGetErrorString8(hr));
363
364 RELEASE:
365     ref = IDirectInputDevice_Release(pJoystick);
366     ok(ref==0,"IDirectInputDevice_Release() reference count = %d\n", ref);
367
368 DONE:
369     return DIENUM_CONTINUE;
370 }
371
372 static void joystick_tests(DWORD version)
373 {
374     HRESULT hr;
375     LPDIRECTINPUT pDI;
376     ULONG ref;
377     HINSTANCE hInstance = GetModuleHandle(NULL);
378
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) {
384         UserData data;
385         data.pDI = pDI;
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");
395 }
396
397 START_TEST(joystick)
398 {
399     CoInitialize(NULL);
400
401     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
402
403     joystick_tests(0x0700);
404     joystick_tests(0x0500);
405     joystick_tests(0x0300);
406
407     CoUninitialize();
408 }