dinput: Keyboard should be acquired before calling GetDeviceState.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define DIRECTINPUT_VERSION 0x0700
20
21 #define NONAMELESSSTRUCT
22 #define NONAMELESSUNION
23 #include <windows.h>
24
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "wine/test.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "dinput.h"
33 #include "dxerr8.h"
34 #include "dinput_test.h"
35
36 const char * get_file_version(const char * file_name)
37 {
38     static char version[32];
39     DWORD size;
40     DWORD handle;
41
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, "\\", (LPVOID *)&pFixedVersionInfo, &len)) {
50                     sprintf(version, "%ld.%ld.%ld.%ld",
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");
59
60             HeapFree(GetProcessHeap(), 0, data);
61         } else
62             sprintf(version, "failed");
63     } else
64         sprintf(version, "not available");
65
66     return version;
67 }
68
69 static void keyboard_tests(DWORD version)
70 {
71     HRESULT hr;
72     LPDIRECTINPUT pDI;
73     LPDIRECTINPUTDEVICE pKeyboard;
74     HINSTANCE hInstance = GetModuleHandle(NULL);
75     BYTE kbd_state[256];
76     ULONG ref;
77
78     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
79     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
80     if (FAILED(hr)) return;
81     
82     
83     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
84     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
85     if (FAILED(hr))
86     {
87         IDirectInput_Release(pDI);
88         return;
89     }
90
91     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
92     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
93     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
94     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %s\n", DXGetErrorString8(hr));
95     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
96     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
97     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
98     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
99     hr = IDirectInputDevice_Acquire(pKeyboard);
100     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
101     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
102     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
103     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
104     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %s\n", DXGetErrorString8(hr));
105     
106     ref = IDirectInput_Release(pDI);
107     ok(!ref, "IDirectInput_Release() reference count = %ld\n", ref);
108 }
109
110 START_TEST(keyboard)
111 {
112     CoInitialize(NULL);
113
114     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
115
116     keyboard_tests(0x0700);
117
118     CoUninitialize();
119 }