1 /* 2 * The Wine project - Xinput Joystick Library 3 * Copyright 2008 Andrew Fenn 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 18 */ 19 20 #include <windows.h> 21 #include <stdio.h> 22 23 #include "xinput.h" 24 #include "wine/test.h" 25 26 static DWORD (WINAPI *pXInputGetState)(DWORD, XINPUT_STATE*); 27 static DWORD (WINAPI *pXInputGetStateEx)(DWORD, XINPUT_STATE*); 28 static DWORD (WINAPI *pXInputGetCapabilities)(DWORD,DWORD,XINPUT_CAPABILITIES*); 29 static DWORD (WINAPI *pXInputSetState)(DWORD, XINPUT_VIBRATION*); 30 static void (WINAPI *pXInputEnable)(BOOL); 31 static DWORD (WINAPI *pXInputGetKeystroke)(DWORD, DWORD, PXINPUT_KEYSTROKE); 32 static DWORD (WINAPI *pXInputGetDSoundAudioDeviceGuids)(DWORD, GUID*, GUID*); 33 static DWORD (WINAPI *pXInputGetBatteryInformation)(DWORD, BYTE, XINPUT_BATTERY_INFORMATION*); 34 35 static void dump_gamepad(XINPUT_GAMEPAD *data) 36 { 37 trace("-- Gamepad Variables --\n"); 38 trace("Gamepad.wButtons: %#x\n", data->wButtons); 39 trace("Gamepad.bLeftTrigger: %d\n", data->bLeftTrigger); 40 trace("Gamepad.bRightTrigger: %d\n", data->bRightTrigger); 41 trace("Gamepad.sThumbLX: %d\n", data->sThumbLX); 42 trace("Gamepad.sThumbLY: %d\n", data->sThumbLY); 43 trace("Gamepad.sThumbRX: %d\n", data->sThumbRX); 44 trace("Gamepad.sThumbRY: %d\n\n", data->sThumbRY); 45 } 46 47 static void test_set_state(void) 48 { 49 XINPUT_VIBRATION vibrator; 50 DWORD controllerNum; 51 DWORD result; 52 53 for(controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 54 { 55 ZeroMemory(&vibrator, sizeof(XINPUT_VIBRATION)); 56 57 vibrator.wLeftMotorSpeed = 32767; 58 vibrator.wRightMotorSpeed = 32767; 59 result = pXInputSetState(controllerNum, &vibrator); 60 if (result == ERROR_DEVICE_NOT_CONNECTED) continue; 61 62 Sleep(250); 63 vibrator.wLeftMotorSpeed = 0; 64 vibrator.wRightMotorSpeed = 0; 65 result = pXInputSetState(controllerNum, &vibrator); 66 ok(result == ERROR_SUCCESS, "XInputSetState failed with (%d)\n", result); 67 68 /* Disabling XInput here, queueing a vibration and then re-enabling XInput 69 * is used to prove that vibrations are auto enabled when resuming XInput. 70 * If XInputEnable(1) is removed below the vibration will never play. */ 71 if (pXInputEnable) pXInputEnable(0); 72 73 Sleep(250); 74 vibrator.wLeftMotorSpeed = 65535; 75 vibrator.wRightMotorSpeed = 65535; 76 result = pXInputSetState(controllerNum, &vibrator); 77 ok(result == ERROR_SUCCESS, "XInputSetState failed with (%d)\n", result); 78 79 if (pXInputEnable) pXInputEnable(1); 80 Sleep(250); 81 82 vibrator.wLeftMotorSpeed = 0; 83 vibrator.wRightMotorSpeed = 0; 84 result = pXInputSetState(controllerNum, &vibrator); 85 ok(result == ERROR_SUCCESS, "XInputSetState failed with (%d)\n", result); 86 } 87 88 result = pXInputSetState(XUSER_MAX_COUNT+1, &vibrator); 89 ok(result == ERROR_BAD_ARGUMENTS, "XInputSetState returned (%d)\n", result); 90 } 91 92 static void test_get_state(void) 93 { 94 XINPUT_STATE state; 95 DWORD controllerNum, i, result, good = XUSER_MAX_COUNT; 96 97 for (i = 0; i < (pXInputGetStateEx ? 2 : 1); i++) 98 { 99 for (controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 100 { 101 ZeroMemory(&state, sizeof(state)); 102 103 if (i == 0) 104 result = pXInputGetState(controllerNum, &state); 105 else 106 result = pXInputGetStateEx(controllerNum, &state); 107 ok(result == ERROR_SUCCESS || result == ERROR_DEVICE_NOT_CONNECTED, 108 "%s failed with (%d)\n", i == 0 ? "XInputGetState" : "XInputGetStateEx", result); 109 110 if (ERROR_DEVICE_NOT_CONNECTED == result) 111 { 112 skip("Controller %d is not connected\n", controllerNum); 113 continue; 114 } 115 116 trace("-- Results for controller %d --\n", controllerNum); 117 if (i == 0) 118 { 119 good = controllerNum; 120 trace("XInputGetState: %d\n", result); 121 } 122 else 123 trace("XInputGetStateEx: %d\n", result); 124 trace("State->dwPacketNumber: %d\n", state.dwPacketNumber); 125 dump_gamepad(&state.Gamepad); 126 } 127 } 128 129 result = pXInputGetState(XUSER_MAX_COUNT, &state); 130 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result); 131 132 result = pXInputGetState(XUSER_MAX_COUNT+1, &state); 133 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result); 134 if (pXInputGetStateEx) 135 { 136 result = pXInputGetStateEx(XUSER_MAX_COUNT, &state); 137 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result); 138 139 result = pXInputGetStateEx(XUSER_MAX_COUNT+1, &state); 140 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result); 141 } 142 143 if (winetest_interactive && good < XUSER_MAX_COUNT) 144 { 145 DWORD now = GetTickCount(), packet = 0; 146 XINPUT_GAMEPAD *game = &state.Gamepad; 147 148 trace("You have 20 seconds to test the joystick freely\n"); 149 do 150 { 151 Sleep(100); 152 pXInputGetState(good, &state); 153 if (state.dwPacketNumber == packet) 154 continue; 155 156 packet = state.dwPacketNumber; 157 trace("Buttons 0x%04X Triggers %3d/%3d LT %6d/%6d RT %6d/%6d\n", 158 game->wButtons, game->bLeftTrigger, game->bRightTrigger, 159 game->sThumbLX, game->sThumbLY, game->sThumbRX, game->sThumbRY); 160 } 161 while(GetTickCount() - now < 20000); 162 trace("Test over...\n"); 163 } 164 } 165 166 static void test_get_keystroke(void) 167 { 168 XINPUT_KEYSTROKE keystroke; 169 DWORD controllerNum; 170 DWORD result; 171 172 for(controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 173 { 174 ZeroMemory(&keystroke, sizeof(XINPUT_KEYSTROKE)); 175 176 result = pXInputGetKeystroke(controllerNum, XINPUT_FLAG_GAMEPAD, &keystroke); 177 ok(result == ERROR_EMPTY || result == ERROR_SUCCESS || result == ERROR_DEVICE_NOT_CONNECTED, 178 "XInputGetKeystroke failed with (%d)\n", result); 179 180 if (ERROR_DEVICE_NOT_CONNECTED == result) 181 { 182 skip("Controller %d is not connected\n", controllerNum); 183 } 184 } 185 186 ZeroMemory(&keystroke, sizeof(XINPUT_KEYSTROKE)); 187 result = pXInputGetKeystroke(XUSER_MAX_COUNT+1, XINPUT_FLAG_GAMEPAD, &keystroke); 188 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetKeystroke returned (%d)\n", result); 189 } 190 191 static void test_get_capabilities(void) 192 { 193 XINPUT_CAPABILITIES capabilities; 194 DWORD controllerNum; 195 DWORD result; 196 197 for(controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 198 { 199 ZeroMemory(&capabilities, sizeof(XINPUT_CAPABILITIES)); 200 201 result = pXInputGetCapabilities(controllerNum, XINPUT_FLAG_GAMEPAD, &capabilities); 202 ok(result == ERROR_SUCCESS || result == ERROR_DEVICE_NOT_CONNECTED, "XInputGetCapabilities failed with (%d)\n", result); 203 204 if (ERROR_DEVICE_NOT_CONNECTED == result) 205 { 206 skip("Controller %d is not connected\n", controllerNum); 207 continue; 208 } 209 210 /* Important to show that the results changed between 1.3 and 1.4 XInput version */ 211 dump_gamepad(&capabilities.Gamepad); 212 } 213 214 ZeroMemory(&capabilities, sizeof(XINPUT_CAPABILITIES)); 215 result = pXInputGetCapabilities(XUSER_MAX_COUNT+1, XINPUT_FLAG_GAMEPAD, &capabilities); 216 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetCapabilities returned (%d)\n", result); 217 } 218 219 static void test_get_dsoundaudiodevice(void) 220 { 221 DWORD controllerNum; 222 DWORD result; 223 GUID soundRender, soundCapture; 224 GUID testGuid = {0xFFFFFFFF, 0xFFFF, 0xFFFF, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}}; 225 GUID emptyGuid = {0x0, 0x0, 0x0, {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}}; 226 227 for(controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 228 { 229 soundRender = soundCapture = testGuid; 230 result = pXInputGetDSoundAudioDeviceGuids(controllerNum, &soundRender, &soundCapture); 231 ok(result == ERROR_SUCCESS || result == ERROR_DEVICE_NOT_CONNECTED, "XInputGetDSoundAudioDeviceGuids failed with (%d)\n", result); 232 233 if (ERROR_DEVICE_NOT_CONNECTED == result) 234 { 235 skip("Controller %d is not connected\n", controllerNum); 236 continue; 237 } 238 239 if (!IsEqualGUID(&soundRender, &emptyGuid)) 240 ok(!IsEqualGUID(&soundRender, &testGuid), "Broken GUID returned for sound render device\n"); 241 else 242 trace("Headset phone not attached\n"); 243 244 if (!IsEqualGUID(&soundCapture, &emptyGuid)) 245 ok(!IsEqualGUID(&soundCapture, &testGuid), "Broken GUID returned for sound capture device\n"); 246 else 247 trace("Headset microphone not attached\n"); 248 } 249 250 result = pXInputGetDSoundAudioDeviceGuids(XUSER_MAX_COUNT+1, &soundRender, &soundCapture); 251 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetDSoundAudioDeviceGuids returned (%d)\n", result); 252 } 253 254 static void test_get_batteryinformation(void) 255 { 256 DWORD controllerNum; 257 DWORD result; 258 XINPUT_BATTERY_INFORMATION batteryInfo; 259 260 for(controllerNum = 0; controllerNum < XUSER_MAX_COUNT; controllerNum++) 261 { 262 ZeroMemory(&batteryInfo, sizeof(XINPUT_BATTERY_INFORMATION)); 263 264 result = pXInputGetBatteryInformation(controllerNum, BATTERY_DEVTYPE_GAMEPAD, &batteryInfo); 265 ok(result == ERROR_SUCCESS || result == ERROR_DEVICE_NOT_CONNECTED, "XInputGetBatteryInformation failed with (%d)\n", result); 266 267 if (ERROR_DEVICE_NOT_CONNECTED == result) 268 { 269 ok(batteryInfo.BatteryLevel == BATTERY_TYPE_DISCONNECTED, "Failed to report device as being disconnected.\n"); 270 skip("Controller %d is not connected\n", controllerNum); 271 } 272 } 273 274 result = pXInputGetBatteryInformation(XUSER_MAX_COUNT+1, BATTERY_DEVTYPE_GAMEPAD, &batteryInfo); 275 ok(result == ERROR_BAD_ARGUMENTS, "XInputGetBatteryInformation returned (%d)\n", result); 276 } 277 278 START_TEST(xinput) 279 { 280 struct 281 { 282 const char *name; 283 int version; 284 } libs[] = { 285 { "xinput1_1.dll", 1 }, 286 { "xinput1_2.dll", 2 }, 287 { "xinput1_3.dll", 3 }, 288 { "xinput1_4.dll", 4 }, 289 { "xinput9_1_0.dll", 0 } /* legacy for XP/Vista */ 290 }; 291 HMODULE hXinput; 292 void *pXInputGetStateEx_Ordinal; 293 int i; 294 295 for (i = 0; i < sizeof(libs) / sizeof(libs[0]); i++) 296 { 297 hXinput = LoadLibraryA( libs[i].name ); 298 299 if (!hXinput) 300 { 301 win_skip("Could not load %s\n", libs[i].name); 302 continue; 303 } 304 trace("Testing %s\n", libs[i].name); 305 306 pXInputEnable = (void*)GetProcAddress(hXinput, "XInputEnable"); 307 pXInputSetState = (void*)GetProcAddress(hXinput, "XInputSetState"); 308 pXInputGetState = (void*)GetProcAddress(hXinput, "XInputGetState"); 309 pXInputGetStateEx = (void*)GetProcAddress(hXinput, "XInputGetStateEx"); /* Win >= 8 */ 310 pXInputGetStateEx_Ordinal = (void*)GetProcAddress(hXinput, (LPCSTR) 100); 311 pXInputGetKeystroke = (void*)GetProcAddress(hXinput, "XInputGetKeystroke"); 312 pXInputGetCapabilities = (void*)GetProcAddress(hXinput, "XInputGetCapabilities"); 313 pXInputGetDSoundAudioDeviceGuids = (void*)GetProcAddress(hXinput, "XInputGetDSoundAudioDeviceGuids"); 314 pXInputGetBatteryInformation = (void*)GetProcAddress(hXinput, "XInputGetBatteryInformation"); 315 316 /* XInputGetStateEx may not be present by name, use ordinal in this case */ 317 if (!pXInputGetStateEx) 318 pXInputGetStateEx = pXInputGetStateEx_Ordinal; 319 320 test_set_state(); 321 test_get_state(); 322 test_get_capabilities(); 323 324 if (libs[i].version != 4) 325 test_get_dsoundaudiodevice(); 326 else 327 ok(!pXInputGetDSoundAudioDeviceGuids, "XInputGetDSoundAudioDeviceGuids exists in %s\n", libs[i].name); 328 329 if (libs[i].version > 2) 330 { 331 test_get_keystroke(); 332 test_get_batteryinformation(); 333 ok(pXInputGetStateEx != NULL, "XInputGetStateEx not found in %s\n", libs[i].name); 334 } 335 else 336 { 337 ok(!pXInputGetKeystroke, "XInputGetKeystroke exists in %s\n", libs[i].name); 338 ok(!pXInputGetStateEx, "XInputGetStateEx exists in %s\n", libs[i].name); 339 ok(!pXInputGetBatteryInformation, "XInputGetBatteryInformation exists in %s\n", libs[i].name); 340 if (libs[i].version == 0) 341 ok(!pXInputEnable, "XInputEnable exists in %s\n", libs[i].name); 342 } 343 344 FreeLibrary(hXinput); 345 } 346 } 347