dmsynth: Mark internal symbols with hidden visibility.
[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 void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
110 {
111     HRESULT hr;
112     LPDIRECTINPUTDEVICE pKeyboard = NULL;
113     int i;
114
115     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
116     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
117     if (FAILED(hr)) return;
118
119     for (i=0; i<16; i++)
120     {
121         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
122         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
123     }
124     for (i=0; i<16; i++)
125     {
126         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
127         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
128     }
129
130     if (pKeyboard) IUnknown_Release(pKeyboard);
131 }
132
133 static void keyboard_tests(DWORD version)
134 {
135     HRESULT hr;
136     LPDIRECTINPUT pDI = NULL;
137     HINSTANCE hInstance = GetModuleHandle(NULL);
138     HWND hwnd;
139     ULONG ref = 0;
140
141     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
142     if (hr == DIERR_OLDDIRECTINPUTVERSION)
143     {
144         skip("Tests require a newer dinput version\n");
145         return;
146     }
147     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
148     if (FAILED(hr)) return;
149
150     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
151                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
152     ok(hwnd != NULL, "err: %d\n", GetLastError());
153
154     if (hwnd)
155     {
156         acquire_tests(pDI, hwnd);
157         test_set_coop(pDI, hwnd);
158     }
159
160     DestroyWindow(hwnd);
161     if (pDI) ref = IUnknown_Release(pDI);
162     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
163 }
164
165 START_TEST(keyboard)
166 {
167     CoInitialize(NULL);
168
169     keyboard_tests(0x0700);
170
171     CoUninitialize();
172 }