opengl32: Allocate a separate context structure to store generic information.
[wine] / dlls / dinput / tests / keyboard.c
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(LPDIRECTINPUT pDI, HWND hwnd)
34 {
35     HRESULT hr;
36     LPDIRECTINPUTDEVICE 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     if (pKeyboard) IUnknown_Release(pKeyboard);
95 }
96
97 static const HRESULT SetCoop_null_window[16] =  {
98     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
99     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
100     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
101     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
102
103 static const HRESULT SetCoop_real_window[16] =  {
104     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
105     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
106     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
107     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
108
109 static const HRESULT SetCoop_child_window[16] =  {
110     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
111     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
112     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
113     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
114
115 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
116 {
117     HRESULT hr;
118     LPDIRECTINPUTDEVICE pKeyboard = NULL;
119     int i;
120     HWND child;
121
122     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
123     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
124     if (FAILED(hr)) return;
125
126     for (i=0; i<16; i++)
127     {
128         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
129         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
130     }
131     for (i=0; i<16; i++)
132     {
133         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
134         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
135     }
136
137     child = CreateWindow("static", "Title", WS_CHILD | WS_VISIBLE,
138                          10, 10, 50, 50, hwnd, NULL, NULL, NULL);
139     ok(child != NULL, "err: %d\n", GetLastError());
140
141     for (i=0; i<16; i++)
142     {
143         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, child, i);
144         ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
145     }
146
147     DestroyWindow(child);
148     if (pKeyboard) IUnknown_Release(pKeyboard);
149 }
150
151 static void test_get_prop(LPDIRECTINPUT pDI, HWND hwnd)
152 {
153     HRESULT hr;
154     LPDIRECTINPUTDEVICE pKeyboard = NULL;
155     DIPROPRANGE diprg;
156
157     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
158     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
159     if (FAILED(hr)) return;
160
161     memset(&diprg, 0, sizeof(diprg));
162     diprg.diph.dwSize       = sizeof(DIPROPRANGE);
163     diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
164     diprg.diph.dwHow        = DIPH_DEVICE;
165     diprg.diph.dwObj        = 0;
166
167     hr = IDirectInputDevice_GetProperty(pKeyboard, DIPROP_RANGE, &diprg.diph);
168     ok(hr == DIERR_UNSUPPORTED, "IDirectInputDevice_GetProperty() did not return DIPROP_RANGE but: %08x\n", hr);
169
170     if (pKeyboard) IUnknown_Release(pKeyboard);
171 }
172
173 static void keyboard_tests(DWORD version)
174 {
175     HRESULT hr;
176     LPDIRECTINPUT pDI = NULL;
177     HINSTANCE hInstance = GetModuleHandle(NULL);
178     HWND hwnd;
179     ULONG ref = 0;
180
181     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
182     if (hr == DIERR_OLDDIRECTINPUTVERSION)
183     {
184         skip("Tests require a newer dinput version\n");
185         return;
186     }
187     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
188     if (FAILED(hr)) return;
189
190     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
191                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
192     ok(hwnd != NULL, "err: %d\n", GetLastError());
193
194     if (hwnd)
195     {
196         acquire_tests(pDI, hwnd);
197         test_set_coop(pDI, hwnd);
198         test_get_prop(pDI, hwnd);
199     }
200
201     DestroyWindow(hwnd);
202     if (pDI) ref = IUnknown_Release(pDI);
203     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
204 }
205
206 START_TEST(keyboard)
207 {
208     CoInitialize(NULL);
209
210     keyboard_tests(0x0700);
211
212     CoUninitialize();
213 }