server: Support unbound console input device.
[wine.git] / dlls / dinput / tests / mouse.c
blob2e0b8cdf2e9da000af1824b1ad6e8038cc054950
1 /*
2 * Copyright (c) 2005 Robert Reif
3 * Copyright (c) 2006 Vitaliy Margolen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define DIRECTINPUT_VERSION 0x0700
22 #define COBJMACROS
23 #include <windows.h>
25 #include <math.h>
26 #include <stdlib.h>
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
33 static const HRESULT SetCoop_null_window[16] = {
34 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
35 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
36 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
37 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
39 static const HRESULT SetCoop_real_window[16] = {
40 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
41 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
42 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
43 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
45 static const HRESULT SetCoop_child_window[16] = {
46 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
47 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
48 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
49 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
51 static void test_set_coop(IDirectInputA *pDI, HWND hwnd)
53 HRESULT hr;
54 IDirectInputDeviceA *pMouse = NULL;
55 int i;
56 HWND child;
58 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
59 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
60 if (FAILED(hr)) return;
62 for (i=0; i<16; i++)
64 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
65 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
67 for (i=0; i<16; i++)
69 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
70 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
73 child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL,
74 NULL, NULL);
75 ok(child != NULL, "err: %d\n", GetLastError());
77 for (i=0; i<16; i++)
79 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, child, i);
80 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
83 DestroyWindow(child);
84 if (pMouse) IUnknown_Release(pMouse);
87 static void test_acquire(IDirectInputA *pDI, HWND hwnd)
89 HRESULT hr;
90 IDirectInputDeviceA *pMouse = NULL;
91 DIMOUSESTATE m_state;
92 HWND hwnd2;
93 DIPROPDWORD di_op;
94 DIDEVICEOBJECTDATA mouse_state;
95 DWORD cnt;
96 int i;
98 if (! SetForegroundWindow(hwnd))
100 skip("Not running as foreground app, skipping acquire tests\n");
101 return;
104 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
105 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
106 if (FAILED(hr)) return;
108 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
109 ok(hr == S_OK, "SetCooperativeLevel: %08x\n", hr);
111 memset(&di_op, 0, sizeof(di_op));
112 di_op.dwData = 5;
113 di_op.diph.dwHow = DIPH_DEVICE;
114 di_op.diph.dwSize = sizeof(DIPROPDWORD);
115 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
116 hr = IDirectInputDevice_SetProperty(pMouse, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&di_op);
117 ok(hr == S_OK, "SetProperty() failed: %08x\n", hr);
119 hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
120 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
121 hr = IDirectInputDevice_Unacquire(pMouse);
122 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
123 hr = IDirectInputDevice_Acquire(pMouse);
124 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
125 hr = IDirectInputDevice_Acquire(pMouse);
126 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
128 /* Foreground coop level requires window to have focus */
129 /* Create a temporary window, this should make dinput
130 * lose mouse input */
131 hwnd2 = CreateWindowA("static", "Temporary", WS_VISIBLE, 10, 210, 200, 200, NULL, NULL, NULL,
132 NULL);
133 ok(hwnd2 != NULL, "CreateWindowA failed with %u\n", GetLastError());
135 hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
136 ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %08x\n", hr);
138 hr = IDirectInputDevice_Acquire(pMouse);
139 ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %08x\n", hr);
141 SetActiveWindow( hwnd );
142 hr = IDirectInputDevice_Acquire(pMouse);
143 ok(hr == S_OK, "Acquire() failed: %08x\n", hr);
145 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
146 cnt = 1;
147 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
148 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
150 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
151 hr = IDirectInputDevice_Unacquire(pMouse);
152 ok(hr == S_OK, "Failed: %08x\n", hr);
153 cnt = 1;
154 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
155 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
157 hr = IDirectInputDevice_Acquire(pMouse);
158 ok(hr == S_OK, "Failed: %08x\n", hr);
159 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
160 hr = IDirectInputDevice_Unacquire(pMouse);
161 ok(hr == S_OK, "Failed: %08x\n", hr);
163 hr = IDirectInputDevice_Acquire(pMouse);
164 ok(hr == S_OK, "Failed: %08x\n", hr);
165 cnt = 1;
166 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
167 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
169 /* Check for buffer overflow */
170 for (i = 0; i < 6; i++)
171 mouse_event(MOUSEEVENTF_MOVE, 10 + i, 10 + i, 0, 0);
173 cnt = 1;
174 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
175 ok(hr == DI_OK, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
176 cnt = 1;
177 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
178 ok(hr == DI_OK && cnt == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
180 /* Check for granularity property using BYOFFSET */
181 memset(&di_op, 0, sizeof(di_op));
182 di_op.diph.dwHow = DIPH_BYOFFSET;
183 di_op.diph.dwObj = DIMOFS_Y;
184 di_op.diph.dwSize = sizeof(DIPROPDWORD);
185 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
186 hr = IDirectInputDevice_GetProperty(pMouse, DIPROP_GRANULARITY, &di_op.diph);
187 /* Granularity of Y axis should be 1! */
188 ok(hr == S_OK && di_op.dwData == 1, "GetProperty(): %08x, dwData: %i but should be 1.\n", hr, di_op.dwData);
190 /* Check for granularity property using BYID */
191 memset(&di_op, 0, sizeof(di_op));
192 di_op.diph.dwHow = DIPH_BYID;
193 /* WINE_MOUSE_Y_AXIS_INSTANCE := 1 */
194 di_op.diph.dwObj = (DIDFT_MAKEINSTANCE(1) | DIDFT_RELAXIS);
195 di_op.diph.dwSize = sizeof(DIPROPDWORD);
196 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
197 hr = IDirectInputDevice_GetProperty(pMouse, DIPROP_GRANULARITY, &di_op.diph);
198 /* Granularity of Y axis should be 1! */
199 ok(hr == S_OK && di_op.dwData == 1, "GetProperty(): %08x, dwData: %i but should be 1.\n", hr, di_op.dwData);
201 memset(&di_op, 0, sizeof(di_op));
202 di_op.diph.dwSize = sizeof(DIPROPDWORD);
203 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
204 di_op.diph.dwHow = DIPH_DEVICE;
205 di_op.diph.dwObj = 0;
206 hr = IDirectInputDevice_GetProperty(pMouse, DIPROP_VIDPID, &di_op.diph);
207 ok(hr == DIERR_UNSUPPORTED, "got %08x\n", hr);
209 IUnknown_Release(pMouse);
211 DestroyWindow( hwnd2 );
214 static void test_GetDeviceInfo(IDirectInputA *pDI)
216 HRESULT hr;
217 IDirectInputDeviceA *pMouse = NULL;
218 DIDEVICEINSTANCEA instA;
219 DIDEVICEINSTANCE_DX3A inst3A;
221 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
222 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
223 if (FAILED(hr)) return;
225 instA.dwSize = sizeof(instA);
226 hr = IDirectInputDevice_GetDeviceInfo(pMouse, &instA);
227 ok(SUCCEEDED(hr), "got %08x\n", hr);
229 inst3A.dwSize = sizeof(inst3A);
230 hr = IDirectInputDevice_GetDeviceInfo(pMouse, (DIDEVICEINSTANCEA *)&inst3A);
231 ok(SUCCEEDED(hr), "got %08x\n", hr);
233 ok(instA.dwSize != inst3A.dwSize, "got %d, %d \n", instA.dwSize, inst3A.dwSize);
234 ok(IsEqualGUID(&instA.guidInstance, &inst3A.guidInstance), "got %s, %s\n",
235 wine_dbgstr_guid(&instA.guidInstance), wine_dbgstr_guid(&inst3A.guidInstance) );
236 ok(IsEqualGUID(&instA.guidProduct, &inst3A.guidProduct), "got %s, %s\n",
237 wine_dbgstr_guid(&instA.guidProduct), wine_dbgstr_guid(&inst3A.guidProduct) );
238 ok(instA.dwDevType == inst3A.dwDevType, "got %d, %d\n", instA.dwDevType, inst3A.dwDevType);
240 IUnknown_Release(pMouse);
243 static BOOL CALLBACK EnumAxes(const DIDEVICEOBJECTINSTANCEA *pdidoi, void *pContext)
245 if (IsEqualIID(&pdidoi->guidType, &GUID_XAxis) ||
246 IsEqualIID(&pdidoi->guidType, &GUID_YAxis) ||
247 IsEqualIID(&pdidoi->guidType, &GUID_ZAxis))
249 ok(pdidoi->dwFlags & DIDOI_ASPECTPOSITION, "Missing DIDOI_ASPECTPOSITION, flags are 0x%x\n",
250 pdidoi->dwFlags);
252 else
253 ok(pdidoi->dwFlags == 0, "Flags are 0x%x\n", pdidoi->dwFlags);
255 return DIENUM_CONTINUE;
258 static void test_mouse_EnumObjects(IDirectInputA *pDI)
260 HRESULT hr;
261 IDirectInputDeviceA *pMouse = NULL;
263 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
264 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
265 if (FAILED(hr)) return;
267 hr = IDirectInputDevice_EnumObjects(pMouse, EnumAxes, NULL, DIDFT_ALL);
268 ok(hr==DI_OK,"IDirectInputDevice_EnumObjects() failed: %08x\n", hr);
270 IUnknown_Release(pMouse);
273 static void mouse_tests(void)
275 HRESULT hr;
276 IDirectInputA *pDI = NULL;
277 HINSTANCE hInstance = GetModuleHandleW(NULL);
278 HWND hwnd;
279 ULONG ref = 0;
281 hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
282 if (hr == DIERR_OLDDIRECTINPUTVERSION)
284 skip("Tests require a newer dinput version\n");
285 return;
287 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
288 if (FAILED(hr)) return;
290 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL,
291 NULL, NULL);
292 ok(hwnd != NULL, "err: %d\n", GetLastError());
293 if (hwnd)
295 ShowWindow(hwnd, SW_SHOW);
297 test_set_coop(pDI, hwnd);
298 test_acquire(pDI, hwnd);
299 test_GetDeviceInfo(pDI);
300 test_mouse_EnumObjects(pDI);
302 DestroyWindow(hwnd);
304 if (pDI) ref = IUnknown_Release(pDI);
305 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
308 START_TEST(mouse)
310 CoInitialize(NULL);
312 mouse_tests();
314 CoUninitialize();