wined3d: Initialize the state table before filling it.
[wine/multimedia.git] / dlls / dinput / tests / keyboard.c
blobd427e6550c964338903dd2005f42144211e1f490
1 /*
2 * Copyright (c) 2005 Robert Reif
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.
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.
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
19 #define DIRECTINPUT_VERSION 0x0700
21 #define COBJMACROS
22 #include <windows.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
32 #include "dinput_test.h"
34 const char * get_file_version(const char * file_name)
36 static char version[32];
37 static char backslash[] = "\\";
38 DWORD size;
39 DWORD handle;
41 size = GetFileVersionInfoSizeA(file_name, &handle);
42 if (size) {
43 char * data = HeapAlloc(GetProcessHeap(), 0, size);
44 if (data) {
45 if (GetFileVersionInfoA(file_name, handle, size, data)) {
46 VS_FIXEDFILEINFO *pFixedVersionInfo;
47 UINT len;
48 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
49 sprintf(version, "%d.%d.%d.%d",
50 pFixedVersionInfo->dwFileVersionMS >> 16,
51 pFixedVersionInfo->dwFileVersionMS & 0xffff,
52 pFixedVersionInfo->dwFileVersionLS >> 16,
53 pFixedVersionInfo->dwFileVersionLS & 0xffff);
54 } else
55 sprintf(version, "not available");
56 } else
57 sprintf(version, "failed");
59 HeapFree(GetProcessHeap(), 0, data);
60 } else
61 sprintf(version, "failed");
62 } else
63 sprintf(version, "not available");
65 return version;
68 static void acquire_tests(LPDIRECTINPUT pDI, HWND hwnd)
70 HRESULT hr;
71 LPDIRECTINPUTDEVICE pKeyboard;
72 BYTE kbd_state[256];
73 BYTE custom_state[4];
74 DIOBJECTDATAFORMAT dodf[] =
76 { &GUID_Key, 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 },
77 { &GUID_Key, 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 },
78 { &GUID_Key, 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 },
79 { &GUID_Key, 3, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 },
82 DIDATAFORMAT df;
83 df.dwSize = sizeof( df );
84 df.dwObjSize = sizeof( DIOBJECTDATAFORMAT );
85 df.dwFlags = DIDF_RELAXIS;
86 df.dwDataSize = sizeof( dodf )/sizeof( dodf[0] );
87 df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] );
88 df.rgodf = dodf;
90 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
91 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
92 if (FAILED(hr)) return;
94 hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
95 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
96 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
97 ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
98 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
99 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
100 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
101 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %08x\n", hr);
102 hr = IDirectInputDevice_Unacquire(pKeyboard);
103 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
104 hr = IDirectInputDevice_Acquire(pKeyboard);
105 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
106 hr = IDirectInputDevice_Acquire(pKeyboard);
107 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
108 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
109 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
110 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
111 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
112 hr = IDirectInputDevice_Unacquire(pKeyboard);
113 ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %08x\n", hr);
114 hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df );
115 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
116 hr = IDirectInputDevice_Acquire(pKeyboard);
117 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
118 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
119 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %08x\n", hr);
120 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
121 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %08x\n", hr);
123 if (pKeyboard) IUnknown_Release(pKeyboard);
126 static const HRESULT SetCoop_null_window[16] = {
127 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
128 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
129 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
130 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
132 static const HRESULT SetCoop_real_window[16] = {
133 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
134 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
135 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
136 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
138 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
140 HRESULT hr;
141 LPDIRECTINPUTDEVICE pKeyboard = NULL;
142 int i;
144 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
145 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
146 if (FAILED(hr)) return;
148 for (i=0; i<16; i++)
150 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
151 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
153 for (i=0; i<16; i++)
155 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
156 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
159 if (pKeyboard) IUnknown_Release(pKeyboard);
162 static void keyboard_tests(DWORD version)
164 HRESULT hr;
165 LPDIRECTINPUT pDI = NULL;
166 HINSTANCE hInstance = GetModuleHandle(NULL);
167 HWND hwnd;
168 ULONG ref = 0;
170 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
171 if (hr == DIERR_OLDDIRECTINPUTVERSION)
173 skip("Tests require a newer dinput version\n");
174 return;
176 ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
177 if (FAILED(hr)) return;
179 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
180 10, 10, 200, 200, NULL, NULL, NULL, NULL);
181 ok(hwnd != NULL, "err: %d\n", GetLastError());
183 if (hwnd)
185 acquire_tests(pDI, hwnd);
186 test_set_coop(pDI, hwnd);
189 DestroyWindow(hwnd);
190 if (pDI) ref = IUnknown_Release(pDI);
191 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
194 START_TEST(keyboard)
196 CoInitialize(NULL);
198 trace("DLL Version: %s\n", get_file_version("dinput.dll"));
200 keyboard_tests(0x0700);
202 CoUninitialize();