push 9fcdc0d7acf404e32ebbdf911b92222c6b0dee73
[wine/hacks.git] / dlls / dinput / tests / keyboard.c
blobbaac4db7bc20502859609769026e98abef7cc4e7
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 "dxerr8.h"
33 #include "dinput_test.h"
35 const char * get_file_version(const char * file_name)
37 static char version[32];
38 static char backslash[] = "\\";
39 DWORD size;
40 DWORD handle;
42 size = GetFileVersionInfoSizeA(file_name, &handle);
43 if (size) {
44 char * data = HeapAlloc(GetProcessHeap(), 0, size);
45 if (data) {
46 if (GetFileVersionInfoA(file_name, handle, size, data)) {
47 VS_FIXEDFILEINFO *pFixedVersionInfo;
48 UINT len;
49 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
50 sprintf(version, "%d.%d.%d.%d",
51 pFixedVersionInfo->dwFileVersionMS >> 16,
52 pFixedVersionInfo->dwFileVersionMS & 0xffff,
53 pFixedVersionInfo->dwFileVersionLS >> 16,
54 pFixedVersionInfo->dwFileVersionLS & 0xffff);
55 } else
56 sprintf(version, "not available");
57 } else
58 sprintf(version, "failed");
60 HeapFree(GetProcessHeap(), 0, data);
61 } else
62 sprintf(version, "failed");
63 } else
64 sprintf(version, "not available");
66 return version;
69 static void acquire_tests(LPDIRECTINPUT pDI, HWND hwnd)
71 HRESULT hr;
72 LPDIRECTINPUTDEVICE pKeyboard;
73 BYTE kbd_state[256];
74 BYTE custom_state[4];
75 DIOBJECTDATAFORMAT dodf[] =
77 { &GUID_Key, 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 },
78 { &GUID_Key, 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 },
79 { &GUID_Key, 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 },
80 { &GUID_Key, 3, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 },
83 DIDATAFORMAT df;
84 df.dwSize = sizeof( df );
85 df.dwObjSize = sizeof( DIOBJECTDATAFORMAT );
86 df.dwFlags = DIDF_RELAXIS;
87 df.dwDataSize = sizeof( dodf )/sizeof( dodf[0] );
88 df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] );
89 df.rgodf = dodf;
91 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
92 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
93 if (FAILED(hr)) return;
95 hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
96 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
97 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
98 ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %s\n", DXGetErrorString8(hr));
99 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
100 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
101 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
102 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
103 hr = IDirectInputDevice_Unacquire(pKeyboard);
104 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %s\n", DXGetErrorString8(hr));
105 hr = IDirectInputDevice_Acquire(pKeyboard);
106 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
107 hr = IDirectInputDevice_Acquire(pKeyboard);
108 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %s\n", DXGetErrorString8(hr));
109 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
110 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
111 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
112 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %s\n", DXGetErrorString8(hr));
113 hr = IDirectInputDevice_Unacquire(pKeyboard);
114 ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %s\n", DXGetErrorString8(hr));
115 hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df );
116 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
117 hr = IDirectInputDevice_Acquire(pKeyboard);
118 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
119 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
120 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %s\n", DXGetErrorString8(hr));
121 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
122 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %s\n", DXGetErrorString8(hr));
124 if (pKeyboard) IUnknown_Release(pKeyboard);
127 static const HRESULT SetCoop_null_window[16] = {
128 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
129 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
130 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
131 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
133 static const HRESULT SetCoop_real_window[16] = {
134 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
135 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
136 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
137 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
139 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
141 HRESULT hr;
142 LPDIRECTINPUTDEVICE pKeyboard = NULL;
143 int i;
145 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
146 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
147 if (FAILED(hr)) return;
149 for (i=0; i<16; i++)
151 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
152 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
154 for (i=0; i<16; i++)
156 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
157 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
160 if (pKeyboard) IUnknown_Release(pKeyboard);
163 static void keyboard_tests(DWORD version)
165 HRESULT hr;
166 LPDIRECTINPUT pDI = NULL;
167 HINSTANCE hInstance = GetModuleHandle(NULL);
168 HWND hwnd;
169 ULONG ref = 0;
171 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
172 if (hr == DIERR_OLDDIRECTINPUTVERSION)
174 skip("Tests require a newer dinput version\n");
175 return;
177 ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
178 if (FAILED(hr)) return;
180 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
181 10, 10, 200, 200, NULL, NULL, NULL, NULL);
182 ok(hwnd != NULL, "err: %d\n", GetLastError());
184 if (hwnd)
186 acquire_tests(pDI, hwnd);
187 test_set_coop(pDI, hwnd);
190 DestroyWindow(hwnd);
191 if (pDI) ref = IUnknown_Release(pDI);
192 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
195 START_TEST(keyboard)
197 CoInitialize(NULL);
199 trace("DLL Version: %s\n", get_file_version("dinput.dll"));
201 keyboard_tests(0x0700);
203 CoUninitialize();