comctl32: Switch to a Nx4 tiling.
[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 #define NONAMELESSSTRUCT
23 #define NONAMELESSUNION
24 #include <windows.h>
25
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "wine/test.h"
31 #include "windef.h"
32 #include "wingdi.h"
33 #include "dinput.h"
34 #include "dxerr8.h"
35 #include "dinput_test.h"
36
37 const char * get_file_version(const char * file_name)
38 {
39     static char version[32];
40     static char backslash[] = "\\";
41     DWORD size;
42     DWORD handle;
43
44     size = GetFileVersionInfoSizeA(file_name, &handle);
45     if (size) {
46         char * data = HeapAlloc(GetProcessHeap(), 0, size);
47         if (data) {
48             if (GetFileVersionInfoA(file_name, handle, size, data)) {
49                 VS_FIXEDFILEINFO *pFixedVersionInfo;
50                 UINT len;
51                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
52                     sprintf(version, "%d.%d.%d.%d",
53                             pFixedVersionInfo->dwFileVersionMS >> 16,
54                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
55                             pFixedVersionInfo->dwFileVersionLS >> 16,
56                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
57                 } else
58                     sprintf(version, "not available");
59             } else
60                 sprintf(version, "failed");
61
62             HeapFree(GetProcessHeap(), 0, data);
63         } else
64             sprintf(version, "failed");
65     } else
66         sprintf(version, "not available");
67
68     return version;
69 }
70
71 static void acquire_tests(LPDIRECTINPUT pDI, HWND hwnd)
72 {
73     HRESULT hr;
74     LPDIRECTINPUTDEVICE pKeyboard;
75     BYTE kbd_state[256];
76
77     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
78     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
79     if (FAILED(hr)) return;
80
81     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
82     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
83     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
84     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %s\n", DXGetErrorString8(hr));
85     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
86     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
87     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
88     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
89     hr = IDirectInputDevice_Acquire(pKeyboard);
90     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
91     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
92     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
93     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
94     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %s\n", DXGetErrorString8(hr));
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: %s\n", DXGetErrorString8(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): %s\n", i, DXGetErrorString8(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): %s\n", i, DXGetErrorString8(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     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
143     if (FAILED(hr)) return;
144
145     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
146                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
147     ok(hwnd != NULL, "err: %d\n", GetLastError());
148
149     if (hwnd)
150     {
151         acquire_tests(pDI, hwnd);
152         test_set_coop(pDI, hwnd);
153     }
154
155     DestroyWindow(hwnd);
156     if (pDI) ref = IUnknown_Release(pDI);
157     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
158 }
159
160 START_TEST(keyboard)
161 {
162     CoInitialize(NULL);
163
164     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
165
166     keyboard_tests(0x0700);
167
168     CoUninitialize();
169 }