dsound: Always enumerate the default device first.
[wine] / dlls / dinput / tests / mouse.c
1 /*
2  * Copyright (c) 2005 Robert Reif
3  * Copyright (c) 2006 Vitaliy Margolen
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #define DIRECTINPUT_VERSION 0x0700
21
22 #define COBJMACROS
23 #include <windows.h>
24
25 #include <math.h>
26 #include <stdlib.h>
27
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
32
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};
38
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};
44
45 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
46 {
47     HRESULT hr;
48     LPDIRECTINPUTDEVICE pMouse = NULL;
49     int i;
50
51     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
52     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
53     if (FAILED(hr)) return;
54
55     for (i=0; i<16; i++)
56     {
57         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
58         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
59     }
60     for (i=0; i<16; i++)
61     {
62         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
63         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
64     }
65
66     if (pMouse) IUnknown_Release(pMouse);
67 }
68
69 static void test_acquire(LPDIRECTINPUT pDI, HWND hwnd)
70 {
71     HRESULT hr;
72     LPDIRECTINPUTDEVICE pMouse = NULL;
73     DIMOUSESTATE m_state;
74     HWND hwnd2;
75     DIPROPDWORD di_op;
76     DIDEVICEOBJECTDATA mouse_state;
77     DWORD cnt;
78     int i;
79
80     if (! SetForegroundWindow(hwnd))
81     {
82         skip("Not running as foreground app, skipping acquire tests\n");
83         return;
84     }
85
86     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
87     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
88     if (FAILED(hr)) return;
89
90     hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
91     ok(hr == S_OK, "SetCooperativeLevel: %08x\n", hr);
92
93     memset(&di_op, 0, sizeof(di_op));
94     di_op.dwData = 5;
95     di_op.diph.dwHow = DIPH_DEVICE;
96     di_op.diph.dwSize = sizeof(DIPROPDWORD);
97     di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
98     hr = IDirectInputDevice_SetProperty(pMouse, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&di_op);
99     ok(hr == S_OK, "SetProperty() failed: %08x\n", hr);
100
101     hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
102     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
103     hr = IDirectInputDevice_Unacquire(pMouse);
104     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
105     hr = IDirectInputDevice_Acquire(pMouse);
106     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
107     hr = IDirectInputDevice_Acquire(pMouse);
108     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
109
110     /* Foreground coop level requires window to have focus */
111     /* Create a temporary window, this should make dinput
112      * loose mouse input */
113     hwnd2 = CreateWindow("static", "Temporary", WS_VISIBLE,
114                          10, 210, 200, 200, NULL, NULL, NULL, NULL);
115
116     hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
117     ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %08x\n", hr);
118     /* Workaround so we can test other things. Remove when Wine is fixed */
119     IDirectInputDevice_Unacquire(pMouse);
120
121     hr = IDirectInputDevice_Acquire(pMouse);
122     ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %08x\n", hr);
123
124     SetActiveWindow( hwnd );
125     hr = IDirectInputDevice_Acquire(pMouse);
126     ok(hr == S_OK, "Acquire() failed: %08x\n", hr);
127
128     mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
129     cnt = 1;
130     hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
131     ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
132
133     mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
134     IDirectInputDevice_Unacquire(pMouse);
135     cnt = 1;
136     hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
137     ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
138
139     IDirectInputDevice_Acquire(pMouse);
140     mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
141     IDirectInputDevice_Unacquire(pMouse);
142     IDirectInputDevice_Acquire(pMouse);
143     cnt = 1;
144     hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
145     ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
146
147     /* Check for buffer owerflow */
148     for (i = 0; i < 6; i++)
149         mouse_event(MOUSEEVENTF_MOVE, 10 + i, 10 + i, 0, 0);
150
151     cnt = 1;
152     hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
153     ok(hr == DI_OK, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
154     cnt = 1;
155     hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
156     ok(hr == DI_OK && cnt == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
157
158     if (pMouse) IUnknown_Release(pMouse);
159
160     DestroyWindow( hwnd2 );
161 }
162
163 static void mouse_tests(void)
164 {
165     HRESULT hr;
166     LPDIRECTINPUT pDI = NULL;
167     HINSTANCE hInstance = GetModuleHandle(NULL);
168     HWND hwnd;
169     ULONG ref = 0;
170
171     hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
172     if (hr == DIERR_OLDDIRECTINPUTVERSION)
173     {
174         skip("Tests require a newer dinput version\n");
175         return;
176     }
177     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
178     if (FAILED(hr)) return;
179
180     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
181                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
182     ok(hwnd != NULL, "err: %d\n", GetLastError());
183     if (hwnd)
184     {
185         ShowWindow(hwnd, SW_SHOW);
186
187         test_set_coop(pDI, hwnd);
188         test_acquire(pDI, hwnd);
189
190         DestroyWindow(hwnd);
191     }
192     if (pDI) ref = IUnknown_Release(pDI);
193     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
194 }
195
196 START_TEST(mouse)
197 {
198     CoInitialize(NULL);
199
200     mouse_tests();
201
202     CoUninitialize();
203 }