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 #include "precomp.h" 20 21 static void acquire_tests(IDirectInputA *pDI, HWND hwnd) 22 { 23 HRESULT hr; 24 IDirectInputDeviceA *pKeyboard; 25 BYTE kbd_state[256]; 26 LONG custom_state[6]; 27 int i; 28 DIOBJECTDATAFORMAT dodf[] = 29 { 30 { &GUID_Key, sizeof(LONG) * 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 }, 31 { &GUID_Key, sizeof(LONG) * 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 }, 32 { &GUID_Key, sizeof(LONG) * 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 }, 33 { &GUID_Key, sizeof(LONG) * 4, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 }, 34 }; 35 36 DIDATAFORMAT df; 37 df.dwSize = sizeof( df ); 38 df.dwObjSize = sizeof( DIOBJECTDATAFORMAT ); 39 df.dwFlags = DIDF_RELAXIS; 40 df.dwDataSize = sizeof( custom_state ); 41 df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] ); 42 df.rgodf = dodf; 43 44 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL); 45 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr); 46 if (FAILED(hr)) return; 47 48 hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard); 49 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr); 50 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); 51 ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr); 52 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state); 53 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr); 54 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state); 55 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %08x\n", hr); 56 hr = IDirectInputDevice_Unacquire(pKeyboard); 57 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr); 58 hr = IDirectInputDevice_Acquire(pKeyboard); 59 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr); 60 hr = IDirectInputDevice_Acquire(pKeyboard); 61 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr); 62 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state); 63 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr); 64 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state); 65 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr); 66 hr = IDirectInputDevice_Unacquire(pKeyboard); 67 ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %08x\n", hr); 68 hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df ); 69 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr); 70 hr = IDirectInputDevice_Acquire(pKeyboard); 71 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr); 72 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state); 73 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %08x\n", hr); 74 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state); 75 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %08x\n", hr); 76 77 memset(custom_state, 0x56, sizeof(custom_state)); 78 IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state); 79 for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++) 80 ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]); 81 82 /* simulate some keyboard input */ 83 SetFocus(hwnd); 84 keybd_event('Q', 0, 0, 0); 85 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state); 86 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr); 87 if (!custom_state[0]) 88 win_skip("Keyboard event not processed, skipping test\n"); 89 else 90 { 91 /* unacquiring should reset the device state */ 92 hr = IDirectInputDevice_Unacquire(pKeyboard); 93 ok(SUCCEEDED(hr), "IDirectInputDevice_Unacquire() failed: %08x\n", hr); 94 hr = IDirectInputDevice_Acquire(pKeyboard); 95 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr); 96 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state); 97 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState failed: %08x\n", hr); 98 for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++) 99 ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]); 100 } 101 keybd_event('Q', 0, KEYEVENTF_KEYUP, 0); 102 103 if (pKeyboard) IUnknown_Release(pKeyboard); 104 } 105 106 static const HRESULT SetCoop_null_window[16] = { 107 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, 108 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG, 109 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG, 110 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG}; 111 112 static const HRESULT SetCoop_invalid_window[16] = { 113 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, 114 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG, 115 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG, 116 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG}; 117 118 static const HRESULT SetCoop_real_window[16] = { 119 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, 120 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG, 121 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG, 122 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG}; 123 124 static const HRESULT SetCoop_child_window[16] = { 125 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, 126 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG, 127 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG, 128 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG}; 129 130 static void test_set_coop(IDirectInputA *pDI, HWND hwnd) 131 { 132 HRESULT hr; 133 IDirectInputDeviceA *pKeyboard = NULL; 134 int i; 135 HWND child; 136 137 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL); 138 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr); 139 if (FAILED(hr)) return; 140 141 for (i=0; i<16; i++) 142 { 143 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i); 144 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr); 145 } 146 for (i=0; i<16; i++) 147 { 148 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, (HWND)0x400000, i); 149 ok(hr == SetCoop_invalid_window[i], "SetCooperativeLevel(invalid, %d): %08x\n", i, hr); 150 } 151 for (i=0; i<16; i++) 152 { 153 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i); 154 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr); 155 } 156 157 child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL, 158 NULL, NULL); 159 ok(child != NULL, "err: %d\n", GetLastError()); 160 161 for (i=0; i<16; i++) 162 { 163 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, child, i); 164 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr); 165 } 166 167 DestroyWindow(child); 168 if (pKeyboard) IUnknown_Release(pKeyboard); 169 } 170 171 static void test_get_prop(IDirectInputA *pDI, HWND hwnd) 172 { 173 HRESULT hr; 174 IDirectInputDeviceA *pKeyboard = NULL; 175 DIPROPRANGE diprg; 176 177 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL); 178 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr); 179 if (FAILED(hr)) return; 180 181 memset(&diprg, 0, sizeof(diprg)); 182 diprg.diph.dwSize = sizeof(DIPROPRANGE); 183 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 184 diprg.diph.dwHow = DIPH_DEVICE; 185 diprg.diph.dwObj = 0; 186 187 hr = IDirectInputDevice_GetProperty(pKeyboard, DIPROP_RANGE, &diprg.diph); 188 ok(hr == DIERR_UNSUPPORTED, "IDirectInputDevice_GetProperty() did not return DIPROP_RANGE but: %08x\n", hr); 189 190 if (pKeyboard) IUnknown_Release(pKeyboard); 191 } 192 193 static void test_capabilities(IDirectInputA *pDI, HWND hwnd) 194 { 195 HRESULT hr; 196 IDirectInputDeviceA *pKeyboard = NULL; 197 DIDEVCAPS caps; 198 199 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL); 200 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr); 201 if (FAILED(hr)) return; 202 203 caps.dwSize = sizeof(caps); 204 hr = IDirectInputDevice_GetCapabilities(pKeyboard, &caps); 205 206 ok (SUCCEEDED(hr), "GetCapabilities failed: 0x%08x\n", hr); 207 ok (caps.dwFlags & DIDC_ATTACHED, "GetCapabilities dwFlags: 0x%08x\n", caps.dwFlags); 208 ok (LOWORD(LOBYTE(caps.dwDevType)) == DIDEVTYPE_KEYBOARD, 209 "GetCapabilities invalid device type for dwDevType: 0x%08x\n", caps.dwDevType); 210 ok (LOWORD(HIBYTE(caps.dwDevType)) != DIDEVTYPEKEYBOARD_UNKNOWN, 211 "GetCapabilities invalid device subtype for dwDevType: 0x%08x\n", caps.dwDevType); 212 213 IUnknown_Release(pKeyboard); 214 } 215 216 static void keyboard_tests(DWORD version) 217 { 218 HRESULT hr; 219 IDirectInputA *pDI = NULL; 220 HINSTANCE hInstance = GetModuleHandleW(NULL); 221 HWND hwnd; 222 ULONG ref = 0; 223 224 hr = DirectInputCreateA(hInstance, version, &pDI, NULL); 225 if (hr == DIERR_OLDDIRECTINPUTVERSION) 226 { 227 skip("Tests require a newer dinput version\n"); 228 return; 229 } 230 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr); 231 if (FAILED(hr)) return; 232 233 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 10, 10, 200, 200, 234 NULL, NULL, NULL, NULL); 235 ok(hwnd != NULL, "err: %d\n", GetLastError()); 236 237 if (hwnd) 238 { 239 acquire_tests(pDI, hwnd); 240 test_set_coop(pDI, hwnd); 241 test_get_prop(pDI, hwnd); 242 test_capabilities(pDI, hwnd); 243 } 244 245 DestroyWindow(hwnd); 246 if (pDI) ref = IUnknown_Release(pDI); 247 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref); 248 } 249 250 START_TEST(keyboard) 251 { 252 CoInitialize(NULL); 253 254 keyboard_tests(0x0700); 255 256 CoUninitialize(); 257 } 258