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 WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
24 #define DIRECTINPUT_VERSION 0x0700
27 //#include <windows.h>
32 #include <wine/test.h>
37 static const HRESULT SetCoop_null_window
[16] = {
38 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
,
39 E_INVALIDARG
, E_HANDLE
, E_HANDLE
, E_INVALIDARG
,
40 E_INVALIDARG
, E_HANDLE
, S_OK
, E_INVALIDARG
,
41 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
};
43 static const HRESULT SetCoop_real_window
[16] = {
44 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
,
45 E_INVALIDARG
, S_OK
, S_OK
, E_INVALIDARG
,
46 E_INVALIDARG
, E_NOTIMPL
, S_OK
, E_INVALIDARG
,
47 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
};
49 static const HRESULT SetCoop_child_window
[16] = {
50 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
,
51 E_INVALIDARG
, E_HANDLE
, E_HANDLE
, E_INVALIDARG
,
52 E_INVALIDARG
, E_HANDLE
, E_HANDLE
, E_INVALIDARG
,
53 E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
, E_INVALIDARG
};
55 static void test_set_coop(IDirectInputA
*pDI
, HWND hwnd
)
58 IDirectInputDeviceA
*pMouse
= NULL
;
62 hr
= IDirectInput_CreateDevice(pDI
, &GUID_SysMouse
, &pMouse
, NULL
);
63 ok(SUCCEEDED(hr
), "IDirectInput_CreateDevice() failed: %08x\n", hr
);
64 if (FAILED(hr
)) return;
68 hr
= IDirectInputDevice_SetCooperativeLevel(pMouse
, NULL
, i
);
69 ok(hr
== SetCoop_null_window
[i
], "SetCooperativeLevel(NULL, %d): %08x\n", i
, hr
);
73 hr
= IDirectInputDevice_SetCooperativeLevel(pMouse
, hwnd
, i
);
74 ok(hr
== SetCoop_real_window
[i
], "SetCooperativeLevel(hwnd, %d): %08x\n", i
, hr
);
77 child
= CreateWindowA("static", "Title", WS_CHILD
| WS_VISIBLE
, 10, 10, 50, 50, hwnd
, NULL
,
79 ok(child
!= NULL
, "err: %d\n", GetLastError());
83 hr
= IDirectInputDevice_SetCooperativeLevel(pMouse
, child
, i
);
84 ok(hr
== SetCoop_child_window
[i
], "SetCooperativeLevel(child, %d): %08x\n", i
, hr
);
88 if (pMouse
) IUnknown_Release(pMouse
);
91 static void test_acquire(IDirectInputA
*pDI
, HWND hwnd
)
94 IDirectInputDeviceA
*pMouse
= NULL
;
98 DIDEVICEOBJECTDATA mouse_state
;
103 if (! SetForegroundWindow(hwnd
))
105 skip("Not running as foreground app, skipping acquire tests\n");
109 hr
= IDirectInput_CreateDevice(pDI
, &GUID_SysMouse
, &pMouse
, NULL
);
110 ok(SUCCEEDED(hr
), "IDirectInput_CreateDevice() failed: %08x\n", hr
);
111 if (FAILED(hr
)) return;
113 hr
= IDirectInputDevice_SetCooperativeLevel(pMouse
, hwnd
, DISCL_NONEXCLUSIVE
| DISCL_FOREGROUND
);
114 ok(hr
== S_OK
, "SetCooperativeLevel: %08x\n", hr
);
116 memset(&di_op
, 0, sizeof(di_op
));
118 di_op
.diph
.dwHow
= DIPH_DEVICE
;
119 di_op
.diph
.dwSize
= sizeof(DIPROPDWORD
);
120 di_op
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
121 hr
= IDirectInputDevice_SetProperty(pMouse
, DIPROP_BUFFERSIZE
, (LPCDIPROPHEADER
)&di_op
);
122 ok(hr
== S_OK
, "SetProperty() failed: %08x\n", hr
);
124 hr
= IDirectInputDevice_SetDataFormat(pMouse
, &c_dfDIMouse
);
125 ok(SUCCEEDED(hr
), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr
);
126 hr
= IDirectInputDevice_Unacquire(pMouse
);
127 ok(hr
== S_FALSE
, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr
);
128 hr
= IDirectInputDevice_Acquire(pMouse
);
129 ok(SUCCEEDED(hr
), "IDirectInputDevice_Acquire() failed: %08x\n", hr
);
130 hr
= IDirectInputDevice_Acquire(pMouse
);
131 ok(hr
== S_FALSE
, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr
);
133 /* Foreground coop level requires window to have focus */
134 /* Create a temporary window, this should make dinput
135 * lose mouse input */
136 hwnd2
= CreateWindowA("static", "Temporary", WS_VISIBLE
, 10, 210, 200, 200, NULL
, NULL
, NULL
,
138 ok(hwnd2
!= NULL
, "CreateWindowA failed with %u\n", GetLastError());
139 while (PeekMessageA(&msg
, 0, 0, 0, PM_REMOVE
)) DispatchMessageA(&msg
);
141 hr
= IDirectInputDevice_GetDeviceState(pMouse
, sizeof(m_state
), &m_state
);
142 ok(hr
== DIERR_NOTACQUIRED
, "GetDeviceState() should have failed: %08x\n", hr
);
143 /* Workaround so we can test other things. Remove when Wine is fixed */
144 IDirectInputDevice_Unacquire(pMouse
);
146 hr
= IDirectInputDevice_Acquire(pMouse
);
147 ok(hr
== DIERR_OTHERAPPHASPRIO
, "Acquire() should have failed: %08x\n", hr
);
149 SetActiveWindow( hwnd
);
150 hr
= IDirectInputDevice_Acquire(pMouse
);
151 ok(hr
== S_OK
, "Acquire() failed: %08x\n", hr
);
153 if (!winetest_interactive
)
154 skip("ROSTESTS-176/CORE-9710: Skipping randomly failing tests\n");
157 mouse_event(MOUSEEVENTF_MOVE
, 10, 10, 0, 0);
159 hr
= IDirectInputDevice_GetDeviceData(pMouse
, sizeof(mouse_state
), &mouse_state
, &cnt
, 0);
160 ok(hr
== S_OK
&& cnt
> 0, "GetDeviceData() failed: %08x cnt:%d\n", hr
, cnt
);
162 mouse_event(MOUSEEVENTF_MOVE
, 10, 10, 0, 0);
163 IDirectInputDevice_Unacquire(pMouse
);
165 hr
= IDirectInputDevice_GetDeviceData(pMouse
, sizeof(mouse_state
), &mouse_state
, &cnt
, 0);
166 ok(hr
== S_OK
&& cnt
> 0, "GetDeviceData() failed: %08x cnt:%d\n", hr
, cnt
);
168 IDirectInputDevice_Acquire(pMouse
);
169 mouse_event(MOUSEEVENTF_MOVE
, 10, 10, 0, 0);
170 IDirectInputDevice_Unacquire(pMouse
);
171 IDirectInputDevice_Acquire(pMouse
);
173 hr
= IDirectInputDevice_GetDeviceData(pMouse
, sizeof(mouse_state
), &mouse_state
, &cnt
, 0);
174 ok(hr
== S_OK
&& cnt
> 0, "GetDeviceData() failed: %08x cnt:%d\n", hr
, cnt
);
176 /* Check for buffer owerflow */
177 for (i
= 0; i
< 6; i
++)
178 mouse_event(MOUSEEVENTF_MOVE
, 10 + i
, 10 + i
, 0, 0);
181 hr
= IDirectInputDevice_GetDeviceData(pMouse
, sizeof(mouse_state
), &mouse_state
, &cnt
, 0);
182 ok(hr
== DI_OK
, "GetDeviceData() failed: %08x cnt:%d\n", hr
, cnt
);
184 hr
= IDirectInputDevice_GetDeviceData(pMouse
, sizeof(mouse_state
), &mouse_state
, &cnt
, 0);
185 ok(hr
== DI_OK
&& cnt
== 1, "GetDeviceData() failed: %08x cnt:%d\n", hr
, cnt
);
187 if (pMouse
) IUnknown_Release(pMouse
);
189 DestroyWindow( hwnd2
);
192 static void mouse_tests(void)
195 IDirectInputA
*pDI
= NULL
;
196 HINSTANCE hInstance
= GetModuleHandleW(NULL
);
200 hr
= DirectInputCreateA(hInstance
, DIRECTINPUT_VERSION
, &pDI
, NULL
);
201 if (hr
== DIERR_OLDDIRECTINPUTVERSION
)
203 skip("Tests require a newer dinput version\n");
206 ok(SUCCEEDED(hr
), "DirectInputCreateA() failed: %08x\n", hr
);
207 if (FAILED(hr
)) return;
209 hwnd
= CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW
, 10, 10, 200, 200, NULL
, NULL
,
211 ok(hwnd
!= NULL
, "err: %d\n", GetLastError());
214 ShowWindow(hwnd
, SW_SHOW
);
216 test_set_coop(pDI
, hwnd
);
217 test_acquire(pDI
, hwnd
);
221 if (pDI
) ref
= IUnknown_Release(pDI
);
222 ok(!ref
, "IDirectInput_Release() reference count = %d\n", ref
);