janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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 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     static char backslash[] = "\\";
40     DWORD size;
41     DWORD handle;
42
43     size = GetFileVersionInfoSizeA(file_name, &handle);
44     if (size) {
45         char * data = HeapAlloc(GetProcessHeap(), 0, size);
46         if (data) {
47             if (GetFileVersionInfoA(file_name, handle, size, data)) {
48                 VS_FIXEDFILEINFO *pFixedVersionInfo;
49                 UINT len;
50                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
51                     sprintf(version, "%ld.%ld.%ld.%ld",
52                             pFixedVersionInfo->dwFileVersionMS >> 16,
53                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
54                             pFixedVersionInfo->dwFileVersionLS >> 16,
55                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
56                 } else
57                     sprintf(version, "not available");
58             } else
59                 sprintf(version, "failed");
60
61             HeapFree(GetProcessHeap(), 0, data);
62         } else
63             sprintf(version, "failed");
64     } else
65         sprintf(version, "not available");
66
67     return version;
68 }
69
70 static void keyboard_tests(DWORD version)
71 {
72     HRESULT hr;
73     LPDIRECTINPUT pDI;
74     LPDIRECTINPUTDEVICE pKeyboard;
75     HINSTANCE hInstance = GetModuleHandle(NULL);
76     BYTE kbd_state[256];
77     ULONG ref;
78
79     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
80     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
81     if (FAILED(hr)) return;
82     
83     
84     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
85     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
86     if (FAILED(hr))
87     {
88         IDirectInput_Release(pDI);
89         return;
90     }
91
92     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
93     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
94     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
95     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %s\n", DXGetErrorString8(hr));
96     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
97     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
98     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
99     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
100     hr = IDirectInputDevice_Acquire(pKeyboard);
101     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
102     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
103     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
104     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
105     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %s\n", DXGetErrorString8(hr));
106     
107     ref = IDirectInput_Release(pDI);
108     ok(!ref, "IDirectInput_Release() reference count = %ld\n", ref);
109 }
110
111 START_TEST(keyboard)
112 {
113     CoInitialize(NULL);
114
115     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
116
117     keyboard_tests(0x0700);
118
119     CoUninitialize();
120 }