wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dinput / tests / device.c
blobfd212bf25144a7a26e7962a85fd62a482e2159b4
1 /*
2 * Copyright (c) 2006 Vitaliy Margolen
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 "wine/test.h"
25 #include "windef.h"
26 #include "dinput.h"
28 #include <limits.h>
30 static const DIOBJECTDATAFORMAT obj_data_format[] = {
31 { &GUID_YAxis, 16, DIDFT_OPTIONAL|DIDFT_AXIS |DIDFT_MAKEINSTANCE(1), 0},
32 { &GUID_Button,15, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(3), 0},
33 { &GUID_Key, 0, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(16),0},
34 { &GUID_Key, 1, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(17),0},
35 { &GUID_Key, 2, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(18),0},
36 { &GUID_Key, 3, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(19),0},
37 { &GUID_Key, 4, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(20),0},
38 { &GUID_Key, 5, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(21),0},
39 { &GUID_Key, 6, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(22),0},
40 { &GUID_Key, 7, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(23),0},
41 { &GUID_Key, 8, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(24),0},
42 { &GUID_Key, 9, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(25),0},
43 { &GUID_Key, 10, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(26),0},
44 { &GUID_Key, 11, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(27),0},
45 { &GUID_Key, 12, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(28),0},
46 { NULL, 13, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(5),0},
48 { &GUID_Button,14, DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_MAKEINSTANCE(32),0}
51 static const DIDATAFORMAT data_format = {
52 sizeof(DIDATAFORMAT),
53 sizeof(DIOBJECTDATAFORMAT),
54 DIDF_ABSAXIS,
55 32,
56 ARRAY_SIZE(obj_data_format),
57 (LPDIOBJECTDATAFORMAT)obj_data_format
60 static BOOL CALLBACK enum_callback(const DIDEVICEOBJECTINSTANCEA *oi, void *info)
62 if (winetest_debug > 1)
63 trace(" Type:%4x Ofs:%3d Flags:%08x Name:%s\n",
64 oi->dwType, oi->dwOfs, oi->dwFlags, oi->tszName);
65 (*(int*)info)++;
66 return DIENUM_CONTINUE;
69 static BOOL CALLBACK enum_type_callback(const DIDEVICEOBJECTINSTANCEA *oi, void *info)
71 DWORD expected = *(DWORD*)info;
72 ok (expected & DIDFT_GETTYPE(oi->dwType), "EnumObjects() enumerated wrong type for obj %s, expected: %08x got: %08x\n", oi->tszName, expected, oi->dwType);
73 return DIENUM_CONTINUE;
76 static void test_object_info(IDirectInputDeviceA *device, HWND hwnd)
78 HRESULT hr;
79 DIPROPDWORD dp;
80 DIDEVICEOBJECTINSTANCEA obj_info;
81 DWORD obj_types[] = {DIDFT_BUTTON, DIDFT_AXIS, DIDFT_POV};
82 int type_index;
83 int cnt1 = 0;
84 DWORD cnt = 0;
85 DIDEVICEOBJECTDATA buffer[5];
87 hr = IDirectInputDevice_EnumObjects(device, enum_callback, &cnt, DIDFT_ALL);
88 ok(SUCCEEDED(hr), "EnumObjects() failed: %08x\n", hr);
90 hr = IDirectInputDevice_SetDataFormat(device, &data_format);
91 ok(SUCCEEDED(hr), "SetDataFormat() failed: %08x\n", hr);
93 hr = IDirectInputDevice_EnumObjects(device, enum_callback, &cnt1, DIDFT_ALL);
94 ok(SUCCEEDED(hr), "EnumObjects() failed: %08x\n", hr);
95 if (0) /* fails for joystick only */
96 ok(cnt == cnt1, "Enum count changed from %d to %d\n", cnt, cnt1);
98 /* Testing EnumObjects with different types of device objects */
99 for (type_index=0; type_index < ARRAY_SIZE(obj_types); type_index++)
101 hr = IDirectInputDevice_EnumObjects(device, enum_type_callback, &obj_types[type_index], obj_types[type_index]);
102 ok(SUCCEEDED(hr), "EnumObjects() failed: %08x\n", hr);
105 /* Test buffered mode */
106 memset(&dp, 0, sizeof(dp));
107 dp.diph.dwSize = sizeof(DIPROPDWORD);
108 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
109 dp.diph.dwHow = DIPH_DEVICE;
110 dp.diph.dwObj = 0;
111 dp.dwData = UINT_MAX;
113 hr = IDirectInputDevice_GetProperty(device, DIPROP_BUFFERSIZE, &dp.diph);
114 ok(hr == DI_OK, "Failed: %08x\n", hr);
115 ok(dp.dwData == 0, "got %d\n", dp.dwData);
117 dp.dwData = UINT_MAX;
118 hr = IDirectInputDevice_SetProperty(device, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&dp.diph);
119 ok(hr == DI_OK, "SetProperty() failed: %08x\n", hr);
121 dp.dwData = 0;
122 hr = IDirectInputDevice_GetProperty(device, DIPROP_BUFFERSIZE, &dp.diph);
123 ok(hr == DI_OK, "Failed: %08x\n", hr);
124 ok(dp.dwData == UINT_MAX, "got %d\n", dp.dwData);
126 dp.dwData = 0;
127 hr = IDirectInputDevice_SetProperty(device, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&dp.diph);
128 ok(hr == DI_OK, "SetProperty() failed: %08x\n", hr);
129 cnt = 5;
130 hr = IDirectInputDevice_GetDeviceData(device, sizeof(buffer[0]), buffer, &cnt, 0);
131 ok(hr == DI_OK && cnt == 5, "GetDeviceData() failed: %08x cnt: %d\n", hr, cnt);
132 hr = IDirectInputDevice_GetDeviceData(device, sizeof(DIDEVICEOBJECTDATA_DX3), buffer, &cnt, 0);
133 ok(hr == DIERR_NOTBUFFERED, "GetDeviceData() should have failed: %08x\n", hr);
134 IDirectInputDevice_Acquire(device);
135 hr = IDirectInputDevice_GetDeviceData(device, sizeof(DIDEVICEOBJECTDATA_DX3), buffer, &cnt, 0);
136 ok(hr == DIERR_NOTBUFFERED, "GetDeviceData() should have failed: %08x\n", hr);
137 IDirectInputDevice_Unacquire(device);
139 dp.dwData = 20;
140 hr = IDirectInputDevice_SetProperty(device, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&dp.diph);
141 ok(hr == DI_OK, "SetProperty() failed: %08x\n", hr);
142 cnt = 5;
143 hr = IDirectInputDevice_GetDeviceData(device, sizeof(buffer[0]), buffer, &cnt, 0);
144 ok(hr == DI_OK, "GetDeviceData() failed: %08x\n", hr);
145 hr = IDirectInputDevice_GetDeviceData(device, sizeof(DIDEVICEOBJECTDATA_DX3), buffer, &cnt, 0);
146 ok(hr == DIERR_NOTACQUIRED, "GetDeviceData() should have failed: %08x\n", hr);
147 hr = IDirectInputDevice_Acquire(device);
148 ok(hr == DI_OK, "Acquire() failed: %08x\n", hr);
149 cnt = 1;
150 hr = IDirectInputDevice_GetDeviceData(device, sizeof(buffer[0]), buffer, &cnt, 0);
151 ok(hr == DI_OK, "GetDeviceData() failed: %08x\n", hr);
152 hr = IDirectInputDevice_Unacquire(device);
153 ok(hr == DI_OK, "Unacquire() failed: %08x\n", hr);
154 cnt = 1;
155 hr = IDirectInputDevice_GetDeviceData(device, sizeof(buffer[0]), buffer, &cnt, 0);
156 ok(hr == DI_OK, "GetDeviceData() failed: %08x\n", hr);
158 /* No need to test devices without axis */
159 obj_info.dwSize = sizeof(obj_info);
160 hr = IDirectInputDevice_GetObjectInfo(device, &obj_info, 16, DIPH_BYOFFSET);
161 if (SUCCEEDED(hr))
163 /* No device supports per axis relative/absolute mode */
164 dp.diph.dwHow = DIPH_BYOFFSET;
165 dp.diph.dwObj = 16;
166 dp.dwData = DIPROPAXISMODE_ABS;
167 hr = IDirectInputDevice_SetProperty(device, DIPROP_AXISMODE, &dp.diph);
168 ok(hr == DIERR_UNSUPPORTED, "SetProperty() returned: %08x\n", hr);
169 dp.diph.dwHow = DIPH_DEVICE;
170 hr = IDirectInputDevice_SetProperty(device, DIPROP_AXISMODE, &dp.diph);
171 ok(hr == DIERR_INVALIDPARAM, "SetProperty() returned: %08x\n", hr);
172 dp.diph.dwObj = 0;
173 hr = IDirectInputDevice_SetProperty(device, DIPROP_AXISMODE, &dp.diph);
174 ok(hr == DI_OK, "SetProperty() failed: %08x\n", hr);
176 /* Cannot change mode while acquired */
177 hr = IDirectInputDevice_Acquire(device);
178 ok(hr == DI_OK, "Acquire() failed: %08x\n", hr);
180 hr = IDirectInputDevice_SetProperty(device, DIPROP_AXISMODE, &dp.diph);
181 ok(hr == DIERR_ACQUIRED, "SetProperty() returned: %08x\n", hr);
182 hr = IDirectInputDevice_Unacquire(device);
183 ok(hr == DI_OK, "Unacquire() failed: %08x\n", hr);
186 /* Reset buffer size */
187 dp.diph.dwSize = sizeof(DIPROPDWORD);
188 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
189 dp.diph.dwHow = DIPH_DEVICE;
190 dp.diph.dwObj = 0;
191 dp.dwData = 0;
192 hr = IDirectInputDevice_SetProperty(device, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&dp.diph);
193 ok(hr == DI_OK, "SetProperty() failed: %08x\n", hr);
196 struct enum_data
198 IDirectInputA *pDI;
199 HWND hwnd;
200 BOOL tested_product_creation;
203 static BOOL CALLBACK enum_devices(const DIDEVICEINSTANCEA *lpddi, void *pvRef)
205 struct enum_data *data = pvRef;
206 IDirectInputDeviceA *device, *obj = NULL;
207 DIDEVICEINSTANCEA ddi2;
208 HRESULT hr;
210 hr = IDirectInput_GetDeviceStatus(data->pDI, &lpddi->guidInstance);
211 ok(hr == DI_OK, "IDirectInput_GetDeviceStatus() failed: %08x\n", hr);
213 if (hr == DI_OK)
215 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidInstance, &device, NULL);
216 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
217 trace("Testing device %p \"%s\"\n", device, lpddi->tszInstanceName);
219 hr = IUnknown_QueryInterface(device, &IID_IDirectInputDevice2A, (LPVOID*)&obj);
220 ok(SUCCEEDED(hr), "IUnknown_QueryInterface(IID_IDirectInputDevice2A) failed: %08x\n", hr);
221 test_object_info(obj, data->hwnd);
222 IUnknown_Release(obj);
223 obj = NULL;
225 hr = IUnknown_QueryInterface(device, &IID_IDirectInputDevice2W, (LPVOID*)&obj);
226 ok(SUCCEEDED(hr), "IUnknown_QueryInterface(IID_IDirectInputDevice2W) failed: %08x\n", hr);
227 test_object_info(obj, data->hwnd);
228 IUnknown_Release(obj);
230 IUnknown_Release(device);
232 if (!IsEqualGUID(&lpddi->guidInstance, &lpddi->guidProduct))
234 data->tested_product_creation = TRUE;
235 hr = IDirectInput_CreateDevice(data->pDI, &lpddi->guidProduct, &device, NULL);
236 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
238 ddi2.dwSize = sizeof(ddi2);
239 hr = IDirectInputDevice_GetDeviceInfo(device, &ddi2);
240 ok(SUCCEEDED(hr), "IDirectInput_GetDeviceInfo failed: %08x\n", hr);
242 ok(IsEqualGUID(&lpddi->guidProduct, &ddi2.guidProduct), "Product GUIDs do not match. Expected %s, got %s\n", debugstr_guid(&lpddi->guidProduct), debugstr_guid(&ddi2.guidProduct));
243 ok(IsEqualGUID(&ddi2.guidProduct, &ddi2.guidInstance), "Instance GUID should equal product GUID. Expected %s, got %s\n", debugstr_guid(&ddi2.guidProduct), debugstr_guid(&ddi2.guidInstance));
244 /* we cannot compare guidInstances as we may get a different device */
246 IUnknown_Release(device);
250 return DIENUM_CONTINUE;
253 static void device_tests(void)
255 HRESULT hr;
256 IDirectInputA *pDI = NULL, *obj = NULL;
257 HINSTANCE hInstance = GetModuleHandleW(NULL);
258 HWND hwnd;
259 struct enum_data data;
261 hr = CoCreateInstance(&CLSID_DirectInput, 0, 1, &IID_IDirectInput2A, (LPVOID*)&pDI);
262 if (hr == DIERR_OLDDIRECTINPUTVERSION || hr == DIERR_DEVICENOTREG)
264 skip("Tests require a newer dinput version\n");
265 return;
267 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
268 if (FAILED(hr)) return;
270 hr = IDirectInput_Initialize(pDI, hInstance, DIRECTINPUT_VERSION);
271 ok(SUCCEEDED(hr), "Initialize() failed: %08x\n", hr);
272 if (FAILED(hr)) return;
274 hr = IUnknown_QueryInterface(pDI, &IID_IDirectInput2W, (LPVOID*)&obj);
275 ok(SUCCEEDED(hr), "QueryInterface(IDirectInput7W) failed: %08x\n", hr);
277 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL,
278 NULL, NULL);
279 ok(hwnd != NULL, "err: %d\n", GetLastError());
280 if (hwnd)
282 ShowWindow(hwnd, SW_SHOW);
284 data.pDI = pDI;
285 data.hwnd = hwnd;
286 data.tested_product_creation = FALSE;
287 hr = IDirectInput_EnumDevices(pDI, 0, enum_devices, &data, DIEDFL_ALLDEVICES);
288 ok(SUCCEEDED(hr), "IDirectInput_EnumDevices() failed: %08x\n", hr);
290 if (!data.tested_product_creation) winetest_skip("Device creation using product GUID not tested\n");
292 /* If GetDeviceStatus returns DI_OK the device must exist */
293 hr = IDirectInput_GetDeviceStatus(pDI, &GUID_Joystick);
294 if (hr == DI_OK)
296 IDirectInputDeviceA *device = NULL;
298 hr = IDirectInput_CreateDevice(pDI, &GUID_Joystick, &device, NULL);
299 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
300 if (device) IUnknown_Release(device);
303 DestroyWindow(hwnd);
305 if (obj) IUnknown_Release(obj);
306 if (pDI) IUnknown_Release(pDI);
309 START_TEST(device)
311 CoInitialize(NULL);
313 device_tests();
315 CoUninitialize();