winealsa: Add 'Front Mic' as alias for microphone.
[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 #include "dxerr8.h"
33 #include "dinput_test.h"
34
35 static const HRESULT SetCoop_null_window[16] =  {
36     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
37     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
38     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
39     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
40
41 static const HRESULT SetCoop_real_window[16] =  {
42     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
43     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
44     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
45     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
46
47 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
48 {
49     HRESULT hr;
50     LPDIRECTINPUTDEVICE pMouse = NULL;
51     int i;
52
53     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
54     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
55     if (FAILED(hr)) return;
56
57     for (i=0; i<16; i++)
58     {
59         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
60         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
61     }
62     for (i=0; i<16; i++)
63     {
64         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
65         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
66     }
67
68     if (pMouse) IUnknown_Release(pMouse);
69 }
70
71 static void test_acquire(LPDIRECTINPUT pDI, HWND hwnd)
72 {
73     HRESULT hr;
74     LPDIRECTINPUTDEVICE pMouse = NULL;
75     DIMOUSESTATE m_state;
76
77     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
78     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
79     if (FAILED(hr)) return;
80
81     hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
82     ok(hr == S_OK, "SetCooperativeLevel: %s\n", DXGetErrorString8(hr));
83
84     hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
85     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
86     hr = IDirectInputDevice_Unacquire(pMouse);
87     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %s\n", DXGetErrorString8(hr));
88     hr = IDirectInputDevice_Acquire(pMouse);
89     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
90     hr = IDirectInputDevice_Acquire(pMouse);
91     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %s\n", DXGetErrorString8(hr));
92
93     /* Foreground coop level requires window to have focus */
94     /* This should make dinput loose mouse input */
95     SetActiveWindow( 0 );
96
97     hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
98     ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
99     /* Workaround so we can test other things. Remove when Wine is fixed */
100     IDirectInputDevice_Unacquire(pMouse);
101
102     hr = IDirectInputDevice_Acquire(pMouse);
103     ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %s\n", DXGetErrorString8(hr));
104
105     SetActiveWindow( hwnd );
106     hr = IDirectInputDevice_Acquire(pMouse);
107     ok(hr == S_OK, "Acquire() failed: %s\n", DXGetErrorString8(hr));
108
109     if (pMouse) IUnknown_Release(pMouse);
110 }
111
112 static void mouse_tests(void)
113 {
114     HRESULT hr;
115     LPDIRECTINPUT pDI = NULL;
116     HINSTANCE hInstance = GetModuleHandle(NULL);
117     HWND hwnd;
118     ULONG ref = 0;
119
120     hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
121     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
122     if (FAILED(hr)) return;
123
124     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
125                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
126     ok(hwnd != NULL, "err: %d\n", GetLastError());
127     if (hwnd)
128     {
129         ShowWindow(hwnd, SW_SHOW);
130
131         test_set_coop(pDI, hwnd);
132         test_acquire(pDI, hwnd);
133
134         DestroyWindow(hwnd);
135     }
136     if (pDI) ref = IUnknown_Release(pDI);
137     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
138 }
139
140 START_TEST(mouse)
141 {
142     CoInitialize(NULL);
143
144     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
145
146     mouse_tests();
147
148     CoUninitialize();
149 }