1 /*
2  * Copyright (c) 2005 Robert Reif
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #define DIRECTINPUT_VERSION 0x0700
20 
21 #define COBJMACROS
22 #include <windows.h>
23 
24 #include <math.h>
25 #include <stdio.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 void acquire_tests(IDirectInputA *pDI, HWND hwnd)
34 {
35     HRESULT hr;
36     IDirectInputDeviceA *pKeyboard;
37     BYTE kbd_state[256];
38     LONG custom_state[6];
39     int i;
40     DIOBJECTDATAFORMAT dodf[] =
41         {
42             { &GUID_Key, sizeof(LONG) * 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 },
43             { &GUID_Key, sizeof(LONG) * 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 },
44             { &GUID_Key, sizeof(LONG) * 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 },
45             { &GUID_Key, sizeof(LONG) * 4, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 },
46         };
47 
48     DIDATAFORMAT df;
49     df.dwSize = sizeof( df );
50     df.dwObjSize = sizeof( DIOBJECTDATAFORMAT );
51     df.dwFlags = DIDF_RELAXIS;
52     df.dwDataSize = sizeof( custom_state );
53     df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] );
54     df.rgodf = dodf;
55 
56     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
57     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
58     if (FAILED(hr)) return;
59 
60     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
61     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
62     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
63     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
64     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
65     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
66     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
67     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %08x\n", hr);
68     hr = IDirectInputDevice_Unacquire(pKeyboard);
69     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
70     hr = IDirectInputDevice_Acquire(pKeyboard);
71     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
72     hr = IDirectInputDevice_Acquire(pKeyboard);
73     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
74     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
75     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
76     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
77     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
78     hr = IDirectInputDevice_Unacquire(pKeyboard);
79     ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %08x\n", hr);
80     hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df );
81     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
82     hr = IDirectInputDevice_Acquire(pKeyboard);
83     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
84     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
85     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %08x\n", hr);
86     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
87     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %08x\n", hr);
88 
89     memset(custom_state, 0x56, sizeof(custom_state));
90     IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
91     for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++)
92         ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]);
93 
94     /* simulate some keyboard input */
95     SetFocus(hwnd);
96     keybd_event('Q', 0, 0, 0);
97     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
98     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
99     if (!custom_state[0])
100         win_skip("Keyboard event not processed, skipping test\n");
101     else
102     {
103         /* unacquiring should reset the device state */
104         hr = IDirectInputDevice_Unacquire(pKeyboard);
105         ok(SUCCEEDED(hr), "IDirectInputDevice_Unacquire() failed: %08x\n", hr);
106         hr = IDirectInputDevice_Acquire(pKeyboard);
107         ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
108         hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
109         ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState failed: %08x\n", hr);
110         for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++)
111             ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]);
112     }
113     keybd_event('Q', 0, KEYEVENTF_KEYUP, 0);
114 
115     if (pKeyboard) IUnknown_Release(pKeyboard);
116 }
117 
118 static const HRESULT SetCoop_null_window[16] =  {
119     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
120     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
121     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
122     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
123 
124 static const HRESULT SetCoop_invalid_window[16] =  {
125     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
126     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
127     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
128     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
129 
130 static const HRESULT SetCoop_real_window[16] =  {
131     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
132     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
133     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
134     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
135 
136 static const HRESULT SetCoop_child_window[16] =  {
137     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
138     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
139     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
140     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
141 
142 static void test_set_coop(IDirectInputA *pDI, HWND hwnd)
143 {
144     HRESULT hr;
145     IDirectInputDeviceA *pKeyboard = NULL;
146     int i;
147     HWND child;
148 
149     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
150     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
151     if (FAILED(hr)) return;
152 
153     for (i=0; i<16; i++)
154     {
155         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
156         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
157     }
158     for (i=0; i<16; i++)
159     {
160         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, (HWND)0x400000, i);
161         ok(hr == SetCoop_invalid_window[i], "SetCooperativeLevel(invalid, %d): %08x\n", i, hr);
162     }
163     for (i=0; i<16; i++)
164     {
165         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
166         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
167     }
168 
169     child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL,
170                           NULL, NULL);
171     ok(child != NULL, "err: %d\n", GetLastError());
172 
173     for (i=0; i<16; i++)
174     {
175         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, child, i);
176         ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
177     }
178 
179     DestroyWindow(child);
180     if (pKeyboard) IUnknown_Release(pKeyboard);
181 }
182 
183 static void test_get_prop(IDirectInputA *pDI, HWND hwnd)
184 {
185     HRESULT hr;
186     IDirectInputDeviceA *pKeyboard = NULL;
187     DIPROPRANGE diprg;
188 
189     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
190     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
191     if (FAILED(hr)) return;
192 
193     memset(&diprg, 0, sizeof(diprg));
194     diprg.diph.dwSize       = sizeof(DIPROPRANGE);
195     diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
196     diprg.diph.dwHow        = DIPH_DEVICE;
197     diprg.diph.dwObj        = 0;
198 
199     hr = IDirectInputDevice_GetProperty(pKeyboard, DIPROP_RANGE, &diprg.diph);
200     ok(hr == DIERR_UNSUPPORTED, "IDirectInputDevice_GetProperty() did not return DIPROP_RANGE but: %08x\n", hr);
201 
202     if (pKeyboard) IUnknown_Release(pKeyboard);
203 }
204 
205 static void test_capabilities(IDirectInputA *pDI, HWND hwnd)
206 {
207     HRESULT hr;
208     IDirectInputDeviceA *pKeyboard = NULL;
209     DIDEVCAPS caps;
210 
211     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
212     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
213     if (FAILED(hr)) return;
214 
215     caps.dwSize = sizeof(caps);
216     hr = IDirectInputDevice_GetCapabilities(pKeyboard, &caps);
217 
218     ok (SUCCEEDED(hr), "GetCapabilities failed: 0x%08x\n", hr);
219     ok (caps.dwFlags & DIDC_ATTACHED, "GetCapabilities dwFlags: 0x%08x\n", caps.dwFlags);
220     ok (LOWORD(LOBYTE(caps.dwDevType)) == DIDEVTYPE_KEYBOARD,
221         "GetCapabilities invalid device type for dwDevType: 0x%08x\n", caps.dwDevType);
222     ok (LOWORD(HIBYTE(caps.dwDevType)) != DIDEVTYPEKEYBOARD_UNKNOWN,
223         "GetCapabilities invalid device subtype for dwDevType: 0x%08x\n", caps.dwDevType);
224 
225     IUnknown_Release(pKeyboard);
226 }
227 
228 static void keyboard_tests(DWORD version)
229 {
230     HRESULT hr;
231     IDirectInputA *pDI = NULL;
232     HINSTANCE hInstance = GetModuleHandleW(NULL);
233     HWND hwnd;
234     ULONG ref = 0;
235 
236     hr = DirectInputCreateA(hInstance, version, &pDI, NULL);
237     if (hr == DIERR_OLDDIRECTINPUTVERSION)
238     {
239         skip("Tests require a newer dinput version\n");
240         return;
241     }
242     ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
243     if (FAILED(hr)) return;
244 
245     hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 10, 10, 200, 200,
246                          NULL, NULL, NULL, NULL);
247     ok(hwnd != NULL, "err: %d\n", GetLastError());
248 
249     if (hwnd)
250     {
251         acquire_tests(pDI, hwnd);
252         test_set_coop(pDI, hwnd);
253         test_get_prop(pDI, hwnd);
254         test_capabilities(pDI, hwnd);
255     }
256 
257     DestroyWindow(hwnd);
258     if (pDI) ref = IUnknown_Release(pDI);
259     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
260 }
261 
262 START_TEST(keyboard)
263 {
264     CoInitialize(NULL);
265 
266     keyboard_tests(0x0700);
267 
268     CoUninitialize();
269 }
270