| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Simple program to test the SDL game controller routines */ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "SDL.h" |
| 20 |
#include "testutils.h" |
| 21 |
|
| 22 |
#ifdef __EMSCRIPTEN__ |
| 23 |
#include <emscripten/emscripten.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifndef SDL_JOYSTICK_DISABLED |
| 27 |
|
| 28 |
#define SCREEN_WIDTH 512 |
| 29 |
#define SCREEN_HEIGHT 320 |
| 30 |
|
| 31 |
#define BUTTON_SIZE 50 |
| 32 |
#define AXIS_SIZE 50 |
| 33 |
|
| 34 |
#define BUTTON_SIZE 50 |
| 35 |
#define AXIS_SIZE 50 |
| 36 |
|
| 37 |
/* This is indexed by SDL_GameControllerButton. */ |
| 38 |
static const struct |
| 39 |
{ |
| 40 |
int x; |
| 41 |
int y; |
| 42 |
} button_positions[] = { |
| 43 |
{ 387, 167 }, /* SDL_CONTROLLER_BUTTON_A */ |
| 44 |
{ 431, 132 }, /* SDL_CONTROLLER_BUTTON_B */ |
| 45 |
{ 342, 132 }, /* SDL_CONTROLLER_BUTTON_X */ |
| 46 |
{ 389, 101 }, /* SDL_CONTROLLER_BUTTON_Y */ |
| 47 |
{ 174, 132 }, /* SDL_CONTROLLER_BUTTON_BACK */ |
| 48 |
{ 232, 128 }, /* SDL_CONTROLLER_BUTTON_GUIDE */ |
| 49 |
{ 289, 132 }, /* SDL_CONTROLLER_BUTTON_START */ |
| 50 |
{ 75, 154 }, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */ |
| 51 |
{ 305, 230 }, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */ |
| 52 |
{ 77, 40 }, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */ |
| 53 |
{ 396, 36 }, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */ |
| 54 |
{ 154, 188 }, /* SDL_CONTROLLER_BUTTON_DPAD_UP */ |
| 55 |
{ 154, 249 }, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */ |
| 56 |
{ 116, 217 }, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */ |
| 57 |
{ 186, 217 }, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */ |
| 58 |
{ 232, 174 }, /* SDL_CONTROLLER_BUTTON_MISC1 */ |
| 59 |
{ 132, 135 }, /* SDL_CONTROLLER_BUTTON_PADDLE1 */ |
| 60 |
{ 330, 135 }, /* SDL_CONTROLLER_BUTTON_PADDLE2 */ |
| 61 |
{ 132, 175 }, /* SDL_CONTROLLER_BUTTON_PADDLE3 */ |
| 62 |
{ 330, 175 }, /* SDL_CONTROLLER_BUTTON_PADDLE4 */ |
| 63 |
{ 0, 0 }, /* SDL_CONTROLLER_BUTTON_TOUCHPAD */ |
| 64 |
}; |
| 65 |
SDL_COMPILE_TIME_ASSERT(button_positions, SDL_arraysize(button_positions) == SDL_CONTROLLER_BUTTON_MAX); |
| 66 |
|
| 67 |
/* This is indexed by SDL_GameControllerAxis. */ |
| 68 |
static const struct |
| 69 |
{ |
| 70 |
int x; |
| 71 |
int y; |
| 72 |
double angle; |
| 73 |
} axis_positions[] = { |
| 74 |
{ 74, 153, 270.0 }, /* LEFTX */ |
| 75 |
{ 74, 153, 0.0 }, /* LEFTY */ |
| 76 |
{ 306, 231, 270.0 }, /* RIGHTX */ |
| 77 |
{ 306, 231, 0.0 }, /* RIGHTY */ |
| 78 |
{ 91, -20, 0.0 }, /* TRIGGERLEFT */ |
| 79 |
{ 375, -20, 0.0 }, /* TRIGGERRIGHT */ |
| 80 |
}; |
| 81 |
SDL_COMPILE_TIME_ASSERT(axis_positions, SDL_arraysize(axis_positions) == SDL_CONTROLLER_AXIS_MAX); |
| 82 |
|
| 83 |
/* This is indexed by SDL_JoystickPowerLevel + 1. */ |
| 84 |
static const char *power_level_strings[] = { |
| 85 |
"unknown", /* SDL_JOYSTICK_POWER_UNKNOWN */ |
| 86 |
"empty", /* SDL_JOYSTICK_POWER_EMPTY */ |
| 87 |
"low", /* SDL_JOYSTICK_POWER_LOW */ |
| 88 |
"medium", /* SDL_JOYSTICK_POWER_MEDIUM */ |
| 89 |
"full", /* SDL_JOYSTICK_POWER_FULL */ |
| 90 |
"wired", /* SDL_JOYSTICK_POWER_WIRED */ |
| 91 |
}; |
| 92 |
SDL_COMPILE_TIME_ASSERT(power_level_strings, SDL_arraysize(power_level_strings) == SDL_JOYSTICK_POWER_MAX + 1); |
| 93 |
|
| 94 |
static SDL_Window *window = NULL; |
| 95 |
static SDL_Renderer *screen = NULL; |
| 96 |
static SDL_bool retval = SDL_FALSE; |
| 97 |
static SDL_bool done = SDL_FALSE; |
| 98 |
static SDL_bool set_LED = SDL_FALSE; |
| 99 |
static int trigger_effect = 0; |
| 100 |
static SDL_Texture *background_front, *background_back, *button_texture, *axis_texture; |
| 101 |
static SDL_GameController *gamecontroller; |
| 102 |
static SDL_GameController **gamecontrollers; |
| 103 |
static int num_controllers = 0; |
| 104 |
static SDL_Joystick *virtual_joystick = NULL; |
| 105 |
static SDL_GameControllerAxis virtual_axis_active = SDL_CONTROLLER_AXIS_INVALID; |
| 106 |
static int virtual_axis_start_x; |
| 107 |
static int virtual_axis_start_y; |
| 108 |
static SDL_GameControllerButton virtual_button_active = SDL_CONTROLLER_BUTTON_INVALID; |
| 109 |
|
| 110 |
static void UpdateWindowTitle(void) |
| 111 |
{ |
| 112 |
if (!window) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
if (gamecontroller) { |
| 117 |
const char *name = SDL_GameControllerName(gamecontroller); |
| 118 |
const char *serial = SDL_GameControllerGetSerial(gamecontroller); |
| 119 |
const char *basetitle = "Game Controller Test: "; |
| 120 |
const size_t titlelen = SDL_strlen(basetitle) + (name ? SDL_strlen(name) : 0) + (serial ? 3 + SDL_strlen(serial) : 0) + 1; |
| 121 |
char *title = (char *)SDL_malloc(titlelen); |
| 122 |
|
| 123 |
retval = SDL_FALSE; |
| 124 |
done = SDL_FALSE; |
| 125 |
|
| 126 |
if (title) { |
| 127 |
SDL_strlcpy(title, basetitle, titlelen); |
| 128 |
if (name) { |
| 129 |
SDL_strlcat(title, name, titlelen); |
| 130 |
} |
| 131 |
if (serial) { |
| 132 |
SDL_strlcat(title, " (", titlelen); |
| 133 |
SDL_strlcat(title, serial, titlelen); |
| 134 |
SDL_strlcat(title, ")", titlelen); |
| 135 |
} |
| 136 |
SDL_SetWindowTitle(window, title); |
| 137 |
SDL_free(title); |
| 138 |
} |
| 139 |
} else { |
| 140 |
SDL_SetWindowTitle(window, "Waiting for controller..."); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static const char *GetSensorName(SDL_SensorType sensor) |
| 145 |
{ |
| 146 |
switch (sensor) { |
| 147 |
case SDL_SENSOR_ACCEL: |
| 148 |
return "accelerometer"; |
| 149 |
case SDL_SENSOR_GYRO: |
| 150 |
return "gyro"; |
| 151 |
case SDL_SENSOR_ACCEL_L: |
| 152 |
return "accelerometer (L)"; |
| 153 |
case SDL_SENSOR_GYRO_L: |
| 154 |
return "gyro (L)"; |
| 155 |
case SDL_SENSOR_ACCEL_R: |
| 156 |
return "accelerometer (R)"; |
| 157 |
case SDL_SENSOR_GYRO_R: |
| 158 |
return "gyro (R)"; |
| 159 |
default: |
| 160 |
return "UNKNOWN"; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
static int FindController(SDL_JoystickID controller_id) |
| 165 |
{ |
| 166 |
int i; |
| 167 |
|
| 168 |
for (i = 0; i < num_controllers; ++i) { |
| 169 |
if (controller_id == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontrollers[i]))) { |
| 170 |
return i; |
| 171 |
} |
| 172 |
} |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
static void AddController(int device_index, SDL_bool verbose) |
| 177 |
{ |
| 178 |
SDL_JoystickID controller_id = SDL_JoystickGetDeviceInstanceID(device_index); |
| 179 |
SDL_GameController *controller; |
| 180 |
SDL_GameController **controllers; |
| 181 |
Uint16 firmware_version; |
| 182 |
SDL_SensorType sensors[] = { |
| 183 |
SDL_SENSOR_ACCEL, |
| 184 |
SDL_SENSOR_GYRO, |
| 185 |
SDL_SENSOR_ACCEL_L, |
| 186 |
SDL_SENSOR_GYRO_L, |
| 187 |
SDL_SENSOR_ACCEL_R, |
| 188 |
SDL_SENSOR_GYRO_R |
| 189 |
}; |
| 190 |
unsigned int i; |
| 191 |
|
| 192 |
controller_id = SDL_JoystickGetDeviceInstanceID(device_index); |
| 193 |
if (controller_id < 0) { |
| 194 |
SDL_Log("Couldn't get controller ID: %s\n", SDL_GetError()); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
if (FindController(controller_id) >= 0) { |
| 199 |
/* We already have this controller */ |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
controller = SDL_GameControllerOpen(device_index); |
| 204 |
if (!controller) { |
| 205 |
SDL_Log("Couldn't open controller: %s\n", SDL_GetError()); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
controllers = (SDL_GameController **)SDL_realloc(gamecontrollers, (num_controllers + 1) * sizeof(*controllers)); |
| 210 |
if (!controllers) { |
| 211 |
SDL_GameControllerClose(controller); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
controllers[num_controllers++] = controller; |
| 216 |
gamecontrollers = controllers; |
| 217 |
gamecontroller = controller; |
| 218 |
trigger_effect = 0; |
| 219 |
|
| 220 |
if (verbose) { |
| 221 |
const char *name = SDL_GameControllerName(gamecontroller); |
| 222 |
const char *path = SDL_GameControllerPath(gamecontroller); |
| 223 |
SDL_Log("Opened game controller %s%s%s\n", name, path ? ", " : "", path ? path : ""); |
| 224 |
SDL_Log("Mapping: %s\n", SDL_GameControllerMapping(gamecontroller)); |
| 225 |
} |
| 226 |
|
| 227 |
firmware_version = SDL_GameControllerGetFirmwareVersion(gamecontroller); |
| 228 |
if (firmware_version) { |
| 229 |
if (verbose) { |
| 230 |
SDL_Log("Firmware version: 0x%x (%d)\n", firmware_version, firmware_version); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
for (i = 0; i < SDL_arraysize(sensors); ++i) { |
| 235 |
SDL_SensorType sensor = sensors[i]; |
| 236 |
|
| 237 |
if (SDL_GameControllerHasSensor(gamecontroller, sensor)) { |
| 238 |
if (verbose) { |
| 239 |
SDL_Log("Enabling %s at %.2f Hz\n", GetSensorName(sensor), SDL_GameControllerGetSensorDataRate(gamecontroller, sensor)); |
| 240 |
} |
| 241 |
SDL_GameControllerSetSensorEnabled(gamecontroller, sensor, SDL_TRUE); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (SDL_GameControllerHasRumble(gamecontroller)) { |
| 246 |
SDL_Log("Rumble supported"); |
| 247 |
} |
| 248 |
|
| 249 |
if (SDL_GameControllerHasRumbleTriggers(gamecontroller)) { |
| 250 |
SDL_Log("Trigger rumble supported"); |
| 251 |
} |
| 252 |
|
| 253 |
UpdateWindowTitle(); |
| 254 |
} |
| 255 |
|
| 256 |
static void SetController(SDL_JoystickID controller) |
| 257 |
{ |
| 258 |
int i = FindController(controller); |
| 259 |
|
| 260 |
if (i < 0) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if (gamecontroller != gamecontrollers[i]) { |
| 265 |
gamecontroller = gamecontrollers[i]; |
| 266 |
UpdateWindowTitle(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
static void DelController(SDL_JoystickID controller) |
| 271 |
{ |
| 272 |
int i = FindController(controller); |
| 273 |
|
| 274 |
if (i < 0) { |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
SDL_GameControllerClose(gamecontrollers[i]); |
| 279 |
|
| 280 |
--num_controllers; |
| 281 |
if (i < num_controllers) { |
| 282 |
SDL_memcpy(&gamecontrollers[i], &gamecontrollers[i + 1], (num_controllers - i) * sizeof(*gamecontrollers)); |
| 283 |
} |
| 284 |
|
| 285 |
if (num_controllers > 0) { |
| 286 |
gamecontroller = gamecontrollers[0]; |
| 287 |
} else { |
| 288 |
gamecontroller = NULL; |
| 289 |
} |
| 290 |
UpdateWindowTitle(); |
| 291 |
} |
| 292 |
|
| 293 |
static Uint16 ConvertAxisToRumble(Sint16 axisval) |
| 294 |
{ |
| 295 |
/* Only start rumbling if the axis is past the halfway point */ |
| 296 |
const Sint16 half_axis = (Sint16)SDL_ceil(SDL_JOYSTICK_AXIS_MAX / 2.0f); |
| 297 |
if (axisval > half_axis) { |
| 298 |
return (Uint16)(axisval - half_axis) * 4; |
| 299 |
} else { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/* PS5 trigger effect documentation: |
| 305 |
https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes |
| 306 |
*/ |
| 307 |
typedef struct |
| 308 |
{ |
| 309 |
Uint8 ucEnableBits1; /* 0 */ |
| 310 |
Uint8 ucEnableBits2; /* 1 */ |
| 311 |
Uint8 ucRumbleRight; /* 2 */ |
| 312 |
Uint8 ucRumbleLeft; /* 3 */ |
| 313 |
Uint8 ucHeadphoneVolume; /* 4 */ |
| 314 |
Uint8 ucSpeakerVolume; /* 5 */ |
| 315 |
Uint8 ucMicrophoneVolume; /* 6 */ |
| 316 |
Uint8 ucAudioEnableBits; /* 7 */ |
| 317 |
Uint8 ucMicLightMode; /* 8 */ |
| 318 |
Uint8 ucAudioMuteBits; /* 9 */ |
| 319 |
Uint8 rgucRightTriggerEffect[11]; /* 10 */ |
| 320 |
Uint8 rgucLeftTriggerEffect[11]; /* 21 */ |
| 321 |
Uint8 rgucUnknown1[6]; /* 32 */ |
| 322 |
Uint8 ucLedFlags; /* 38 */ |
| 323 |
Uint8 rgucUnknown2[2]; /* 39 */ |
| 324 |
Uint8 ucLedAnim; /* 41 */ |
| 325 |
Uint8 ucLedBrightness; /* 42 */ |
| 326 |
Uint8 ucPadLights; /* 43 */ |
| 327 |
Uint8 ucLedRed; /* 44 */ |
| 328 |
Uint8 ucLedGreen; /* 45 */ |
| 329 |
Uint8 ucLedBlue; /* 46 */ |
| 330 |
} DS5EffectsState_t; |
| 331 |
|
| 332 |
static void CyclePS5TriggerEffect(void) |
| 333 |
{ |
| 334 |
DS5EffectsState_t state; |
| 335 |
|
| 336 |
Uint8 effects[3][11] = { |
| 337 |
/* Clear trigger effect */ |
| 338 |
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 339 |
/* Constant resistance across entire trigger pull */ |
| 340 |
{ 0x01, 0, 110, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 341 |
/* Resistance and vibration when trigger is pulled */ |
| 342 |
{ 0x06, 15, 63, 128, 0, 0, 0, 0, 0, 0, 0 }, |
| 343 |
}; |
| 344 |
|
| 345 |
trigger_effect = (trigger_effect + 1) % SDL_arraysize(effects); |
| 346 |
|
| 347 |
SDL_zero(state); |
| 348 |
state.ucEnableBits1 |= (0x04 | 0x08); /* Modify right and left trigger effect respectively */ |
| 349 |
SDL_memcpy(state.rgucRightTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect])); |
| 350 |
SDL_memcpy(state.rgucLeftTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect])); |
| 351 |
SDL_GameControllerSendEffect(gamecontroller, &state, sizeof(state)); |
| 352 |
} |
| 353 |
|
| 354 |
static SDL_bool ShowingFront(void) |
| 355 |
{ |
| 356 |
SDL_bool showing_front = SDL_TRUE; |
| 357 |
int i; |
| 358 |
|
| 359 |
if (gamecontroller) { |
| 360 |
/* Show the back of the controller if the paddles are being held */ |
| 361 |
for (i = SDL_CONTROLLER_BUTTON_PADDLE1; i <= SDL_CONTROLLER_BUTTON_PADDLE4; ++i) { |
| 362 |
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) { |
| 363 |
showing_front = SDL_FALSE; |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
if ((SDL_GetModState() & KMOD_SHIFT) != 0) { |
| 369 |
showing_front = SDL_FALSE; |
| 370 |
} |
| 371 |
return showing_front; |
| 372 |
} |
| 373 |
|
| 374 |
static void SDLCALL VirtualControllerSetPlayerIndex(void *userdata, int player_index) |
| 375 |
{ |
| 376 |
SDL_Log("Virtual Controller: player index set to %d\n", player_index); |
| 377 |
} |
| 378 |
|
| 379 |
static int SDLCALL VirtualControllerRumble(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) |
| 380 |
{ |
| 381 |
SDL_Log("Virtual Controller: rumble set to %d/%d\n", low_frequency_rumble, high_frequency_rumble); |
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
static int SDLCALL VirtualControllerRumbleTriggers(void *userdata, Uint16 left_rumble, Uint16 right_rumble) |
| 386 |
{ |
| 387 |
SDL_Log("Virtual Controller: trigger rumble set to %d/%d\n", left_rumble, right_rumble); |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
static int SDLCALL VirtualControllerSetLED(void *userdata, Uint8 red, Uint8 green, Uint8 blue) |
| 392 |
{ |
| 393 |
SDL_Log("Virtual Controller: LED set to RGB %d,%d,%d\n", red, green, blue); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
static void OpenVirtualController(void) |
| 398 |
{ |
| 399 |
SDL_VirtualJoystickDesc desc; |
| 400 |
int virtual_index; |
| 401 |
|
| 402 |
SDL_zero(desc); |
| 403 |
desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION; |
| 404 |
desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
| 405 |
desc.naxes = SDL_CONTROLLER_AXIS_MAX; |
| 406 |
desc.nbuttons = SDL_CONTROLLER_BUTTON_MAX; |
| 407 |
desc.SetPlayerIndex = VirtualControllerSetPlayerIndex; |
| 408 |
desc.Rumble = VirtualControllerRumble; |
| 409 |
desc.RumbleTriggers = VirtualControllerRumbleTriggers; |
| 410 |
desc.SetLED = VirtualControllerSetLED; |
| 411 |
|
| 412 |
virtual_index = SDL_JoystickAttachVirtualEx(&desc); |
| 413 |
if (virtual_index < 0) { |
| 414 |
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError()); |
| 415 |
} else { |
| 416 |
virtual_joystick = SDL_JoystickOpen(virtual_index); |
| 417 |
if (!virtual_joystick) { |
| 418 |
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError()); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
static void CloseVirtualController(void) |
| 424 |
{ |
| 425 |
int i; |
| 426 |
|
| 427 |
for (i = SDL_NumJoysticks(); i--;) { |
| 428 |
if (SDL_JoystickIsVirtual(i)) { |
| 429 |
SDL_JoystickDetachVirtual(i); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if (virtual_joystick) { |
| 434 |
SDL_JoystickClose(virtual_joystick); |
| 435 |
virtual_joystick = NULL; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
static SDL_GameControllerButton FindButtonAtPosition(int x, int y) |
| 440 |
{ |
| 441 |
SDL_Point point; |
| 442 |
int i; |
| 443 |
SDL_bool showing_front = ShowingFront(); |
| 444 |
|
| 445 |
point.x = x; |
| 446 |
point.y = y; |
| 447 |
for (i = 0; i < SDL_CONTROLLER_BUTTON_TOUCHPAD; ++i) { |
| 448 |
SDL_bool on_front = (i < SDL_CONTROLLER_BUTTON_PADDLE1 || i > SDL_CONTROLLER_BUTTON_PADDLE4); |
| 449 |
if (on_front == showing_front) { |
| 450 |
SDL_Rect rect; |
| 451 |
rect.x = button_positions[i].x; |
| 452 |
rect.y = button_positions[i].y; |
| 453 |
rect.w = BUTTON_SIZE; |
| 454 |
rect.h = BUTTON_SIZE; |
| 455 |
if (SDL_PointInRect(&point, &rect)) { |
| 456 |
return (SDL_GameControllerButton)i; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
return SDL_CONTROLLER_BUTTON_INVALID; |
| 461 |
} |
| 462 |
|
| 463 |
static SDL_GameControllerAxis FindAxisAtPosition(int x, int y) |
| 464 |
{ |
| 465 |
SDL_Point point; |
| 466 |
int i; |
| 467 |
SDL_bool showing_front = ShowingFront(); |
| 468 |
|
| 469 |
point.x = x; |
| 470 |
point.y = y; |
| 471 |
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) { |
| 472 |
if (showing_front) { |
| 473 |
SDL_Rect rect; |
| 474 |
rect.x = axis_positions[i].x; |
| 475 |
rect.y = axis_positions[i].y; |
| 476 |
rect.w = AXIS_SIZE; |
| 477 |
rect.h = AXIS_SIZE; |
| 478 |
if (SDL_PointInRect(&point, &rect)) { |
| 479 |
return (SDL_GameControllerAxis)i; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
return SDL_CONTROLLER_AXIS_INVALID; |
| 484 |
} |
| 485 |
|
| 486 |
static void VirtualControllerMouseMotion(int x, int y) |
| 487 |
{ |
| 488 |
if (virtual_button_active != SDL_CONTROLLER_BUTTON_INVALID) { |
| 489 |
if (virtual_axis_active != SDL_CONTROLLER_AXIS_INVALID) { |
| 490 |
const int MOVING_DISTANCE = 2; |
| 491 |
if (SDL_abs(x - virtual_axis_start_x) >= MOVING_DISTANCE || |
| 492 |
SDL_abs(y - virtual_axis_start_y) >= MOVING_DISTANCE) { |
| 493 |
SDL_JoystickSetVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED); |
| 494 |
virtual_button_active = SDL_CONTROLLER_BUTTON_INVALID; |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
if (virtual_axis_active != SDL_CONTROLLER_AXIS_INVALID) { |
| 500 |
if (virtual_axis_active == SDL_CONTROLLER_AXIS_TRIGGERLEFT || |
| 501 |
virtual_axis_active == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { |
| 502 |
int range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN); |
| 503 |
float distance = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f); |
| 504 |
Sint16 value = (Sint16)(SDL_JOYSTICK_AXIS_MIN + (distance * range)); |
| 505 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active, value); |
| 506 |
} else { |
| 507 |
float distanceX = SDL_clamp(((float)x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f); |
| 508 |
float distanceY = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f); |
| 509 |
Sint16 valueX, valueY; |
| 510 |
|
| 511 |
if (distanceX >= 0) { |
| 512 |
valueX = (Sint16)(distanceX * SDL_JOYSTICK_AXIS_MAX); |
| 513 |
} else { |
| 514 |
valueX = (Sint16)(distanceX * -SDL_JOYSTICK_AXIS_MIN); |
| 515 |
} |
| 516 |
if (distanceY >= 0) { |
| 517 |
valueY = (Sint16)(distanceY * SDL_JOYSTICK_AXIS_MAX); |
| 518 |
} else { |
| 519 |
valueY = (Sint16)(distanceY * -SDL_JOYSTICK_AXIS_MIN); |
| 520 |
} |
| 521 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active, valueX); |
| 522 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active + 1, valueY); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
static void VirtualControllerMouseDown(int x, int y) |
| 528 |
{ |
| 529 |
SDL_GameControllerButton button; |
| 530 |
SDL_GameControllerAxis axis; |
| 531 |
|
| 532 |
button = FindButtonAtPosition(x, y); |
| 533 |
if (button != SDL_CONTROLLER_BUTTON_INVALID) { |
| 534 |
virtual_button_active = button; |
| 535 |
SDL_JoystickSetVirtualButton(virtual_joystick, virtual_button_active, SDL_PRESSED); |
| 536 |
} |
| 537 |
|
| 538 |
axis = FindAxisAtPosition(x, y); |
| 539 |
if (axis != SDL_CONTROLLER_AXIS_INVALID) { |
| 540 |
virtual_axis_active = axis; |
| 541 |
virtual_axis_start_x = x; |
| 542 |
virtual_axis_start_y = y; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
static void VirtualControllerMouseUp(int x, int y) |
| 547 |
{ |
| 548 |
if (virtual_button_active != SDL_CONTROLLER_BUTTON_INVALID) { |
| 549 |
SDL_JoystickSetVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED); |
| 550 |
virtual_button_active = SDL_CONTROLLER_BUTTON_INVALID; |
| 551 |
} |
| 552 |
|
| 553 |
if (virtual_axis_active != SDL_CONTROLLER_AXIS_INVALID) { |
| 554 |
if (virtual_axis_active == SDL_CONTROLLER_AXIS_TRIGGERLEFT || |
| 555 |
virtual_axis_active == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { |
| 556 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active, SDL_JOYSTICK_AXIS_MIN); |
| 557 |
} else { |
| 558 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active, 0); |
| 559 |
SDL_JoystickSetVirtualAxis(virtual_joystick, virtual_axis_active + 1, 0); |
| 560 |
} |
| 561 |
virtual_axis_active = SDL_CONTROLLER_AXIS_INVALID; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
void loop(void *arg) |
| 566 |
{ |
| 567 |
SDL_Event event; |
| 568 |
int i; |
| 569 |
SDL_bool showing_front; |
| 570 |
|
| 571 |
/* Update to get the current event state */ |
| 572 |
SDL_PumpEvents(); |
| 573 |
|
| 574 |
/* Process all currently pending events */ |
| 575 |
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) { |
| 576 |
switch (event.type) { |
| 577 |
case SDL_CONTROLLERDEVICEADDED: |
| 578 |
SDL_Log("Game controller device %d added.\n", (int)SDL_JoystickGetDeviceInstanceID(event.cdevice.which)); |
| 579 |
AddController(event.cdevice.which, SDL_TRUE); |
| 580 |
break; |
| 581 |
|
| 582 |
case SDL_CONTROLLERDEVICEREMOVED: |
| 583 |
SDL_Log("Game controller device %d removed.\n", (int)event.cdevice.which); |
| 584 |
DelController(event.cdevice.which); |
| 585 |
break; |
| 586 |
|
| 587 |
case SDL_CONTROLLERTOUCHPADDOWN: |
| 588 |
case SDL_CONTROLLERTOUCHPADMOTION: |
| 589 |
case SDL_CONTROLLERTOUCHPADUP: |
| 590 |
SDL_Log("Controller %" SDL_PRIs32 " touchpad %" SDL_PRIs32 " finger %" SDL_PRIs32 " %s %.2f, %.2f, %.2f\n", |
| 591 |
event.ctouchpad.which, |
| 592 |
event.ctouchpad.touchpad, |
| 593 |
event.ctouchpad.finger, |
| 594 |
(event.type == SDL_CONTROLLERTOUCHPADDOWN ? "pressed at" : (event.type == SDL_CONTROLLERTOUCHPADUP ? "released at" : "moved to")), |
| 595 |
event.ctouchpad.x, |
| 596 |
event.ctouchpad.y, |
| 597 |
event.ctouchpad.pressure); |
| 598 |
break; |
| 599 |
|
| 600 |
#define VERBOSE_SENSORS |
| 601 |
#ifdef VERBOSE_SENSORS |
| 602 |
case SDL_CONTROLLERSENSORUPDATE: |
| 603 |
SDL_Log("Controller %" SDL_PRIs32 " sensor %s: %.2f, %.2f, %.2f (%" SDL_PRIu64 ")\n", |
| 604 |
event.csensor.which, |
| 605 |
GetSensorName((SDL_SensorType)event.csensor.sensor), |
| 606 |
event.csensor.data[0], |
| 607 |
event.csensor.data[1], |
| 608 |
event.csensor.data[2], |
| 609 |
event.csensor.timestamp_us); |
| 610 |
break; |
| 611 |
#endif /* VERBOSE_SENSORS */ |
| 612 |
|
| 613 |
#define VERBOSE_AXES |
| 614 |
#ifdef VERBOSE_AXES |
| 615 |
case SDL_CONTROLLERAXISMOTION: |
| 616 |
if (event.caxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.caxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) { |
| 617 |
SetController(event.caxis.which); |
| 618 |
} |
| 619 |
SDL_Log("Controller %" SDL_PRIs32 " axis %s changed to %d\n", event.caxis.which, SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value); |
| 620 |
break; |
| 621 |
#endif /* VERBOSE_AXES */ |
| 622 |
|
| 623 |
case SDL_CONTROLLERBUTTONDOWN: |
| 624 |
case SDL_CONTROLLERBUTTONUP: |
| 625 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| 626 |
SetController(event.cbutton.which); |
| 627 |
} |
| 628 |
SDL_Log("Controller %" SDL_PRIs32 " button %s %s\n", event.cbutton.which, SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), |
| 629 |
event.cbutton.state ? "pressed" : "released"); |
| 630 |
|
| 631 |
/* Cycle PS5 trigger effects when the microphone button is pressed */ |
| 632 |
if (event.type == SDL_CONTROLLERBUTTONDOWN && |
| 633 |
event.cbutton.button == SDL_CONTROLLER_BUTTON_MISC1 && |
| 634 |
SDL_GameControllerGetType(gamecontroller) == SDL_CONTROLLER_TYPE_PS5) { |
| 635 |
CyclePS5TriggerEffect(); |
| 636 |
} |
| 637 |
break; |
| 638 |
|
| 639 |
case SDL_JOYBATTERYUPDATED: |
| 640 |
SDL_Log("Controller %" SDL_PRIs32 " battery state changed to %s\n", event.jbattery.which, power_level_strings[event.jbattery.level + 1]); |
| 641 |
break; |
| 642 |
|
| 643 |
case SDL_MOUSEBUTTONDOWN: |
| 644 |
if (virtual_joystick) { |
| 645 |
VirtualControllerMouseDown(event.button.x, event.button.y); |
| 646 |
} |
| 647 |
break; |
| 648 |
|
| 649 |
case SDL_MOUSEBUTTONUP: |
| 650 |
if (virtual_joystick) { |
| 651 |
VirtualControllerMouseUp(event.button.x, event.button.y); |
| 652 |
} |
| 653 |
break; |
| 654 |
|
| 655 |
case SDL_MOUSEMOTION: |
| 656 |
if (virtual_joystick) { |
| 657 |
VirtualControllerMouseMotion(event.motion.x, event.motion.y); |
| 658 |
} |
| 659 |
break; |
| 660 |
|
| 661 |
case SDL_KEYDOWN: |
| 662 |
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) { |
| 663 |
if (gamecontroller) { |
| 664 |
int player_index = (event.key.keysym.sym - SDLK_0); |
| 665 |
|
| 666 |
SDL_GameControllerSetPlayerIndex(gamecontroller, player_index); |
| 667 |
} |
| 668 |
break; |
| 669 |
} |
| 670 |
if (event.key.keysym.sym == SDLK_a) { |
| 671 |
OpenVirtualController(); |
| 672 |
break; |
| 673 |
} |
| 674 |
if (event.key.keysym.sym == SDLK_d) { |
| 675 |
CloseVirtualController(); |
| 676 |
break; |
| 677 |
} |
| 678 |
if (event.key.keysym.sym != SDLK_ESCAPE) { |
| 679 |
break; |
| 680 |
} |
| 681 |
SDL_FALLTHROUGH; |
| 682 |
case SDL_QUIT: |
| 683 |
done = SDL_TRUE; |
| 684 |
break; |
| 685 |
default: |
| 686 |
break; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
showing_front = ShowingFront(); |
| 691 |
|
| 692 |
/* blank screen, set up for drawing this frame. */ |
| 693 |
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
| 694 |
SDL_RenderClear(screen); |
| 695 |
SDL_RenderCopy(screen, showing_front ? background_front : background_back, NULL, NULL); |
| 696 |
|
| 697 |
if (gamecontroller) { |
| 698 |
/* Update visual controller state */ |
| 699 |
for (i = 0; i < SDL_CONTROLLER_BUTTON_TOUCHPAD; ++i) { |
| 700 |
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) { |
| 701 |
SDL_bool on_front = (i < SDL_CONTROLLER_BUTTON_PADDLE1 || i > SDL_CONTROLLER_BUTTON_PADDLE4); |
| 702 |
if (on_front == showing_front) { |
| 703 |
SDL_Rect dst; |
| 704 |
dst.x = button_positions[i].x; |
| 705 |
dst.y = button_positions[i].y; |
| 706 |
dst.w = BUTTON_SIZE; |
| 707 |
dst.h = BUTTON_SIZE; |
| 708 |
SDL_RenderCopyEx(screen, button_texture, NULL, &dst, 0, NULL, SDL_FLIP_NONE); |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
if (showing_front) { |
| 714 |
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) { |
| 715 |
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */ |
| 716 |
const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i)); |
| 717 |
if (value < -deadzone) { |
| 718 |
const double angle = axis_positions[i].angle; |
| 719 |
SDL_Rect dst; |
| 720 |
dst.x = axis_positions[i].x; |
| 721 |
dst.y = axis_positions[i].y; |
| 722 |
dst.w = AXIS_SIZE; |
| 723 |
dst.h = AXIS_SIZE; |
| 724 |
SDL_RenderCopyEx(screen, axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE); |
| 725 |
} else if (value > deadzone) { |
| 726 |
const double angle = axis_positions[i].angle + 180.0; |
| 727 |
SDL_Rect dst; |
| 728 |
dst.x = axis_positions[i].x; |
| 729 |
dst.y = axis_positions[i].y; |
| 730 |
dst.w = AXIS_SIZE; |
| 731 |
dst.h = AXIS_SIZE; |
| 732 |
SDL_RenderCopyEx(screen, axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE); |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
/* Update LED based on left thumbstick position */ |
| 738 |
{ |
| 739 |
Sint16 x = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_LEFTX); |
| 740 |
Sint16 y = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_LEFTY); |
| 741 |
|
| 742 |
if (!set_LED) { |
| 743 |
set_LED = (x < -8000 || x > 8000 || y > 8000); |
| 744 |
} |
| 745 |
if (set_LED) { |
| 746 |
Uint8 r, g, b; |
| 747 |
|
| 748 |
if (x < 0) { |
| 749 |
r = (Uint8)(((~x) * 255) / 32767); |
| 750 |
b = 0; |
| 751 |
} else { |
| 752 |
r = 0; |
| 753 |
b = (Uint8)(((int)(x)*255) / 32767); |
| 754 |
} |
| 755 |
if (y > 0) { |
| 756 |
g = (Uint8)(((int)(y)*255) / 32767); |
| 757 |
} else { |
| 758 |
g = 0; |
| 759 |
} |
| 760 |
|
| 761 |
SDL_GameControllerSetLED(gamecontroller, r, g, b); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
if (trigger_effect == 0) { |
| 766 |
/* Update rumble based on trigger state */ |
| 767 |
{ |
| 768 |
Sint16 left = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT); |
| 769 |
Sint16 right = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT); |
| 770 |
Uint16 low_frequency_rumble = ConvertAxisToRumble(left); |
| 771 |
Uint16 high_frequency_rumble = ConvertAxisToRumble(right); |
| 772 |
SDL_GameControllerRumble(gamecontroller, low_frequency_rumble, high_frequency_rumble, 250); |
| 773 |
} |
| 774 |
|
| 775 |
/* Update trigger rumble based on thumbstick state */ |
| 776 |
{ |
| 777 |
Sint16 left = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_LEFTY); |
| 778 |
Sint16 right = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_RIGHTY); |
| 779 |
Uint16 left_rumble = ConvertAxisToRumble(~left); |
| 780 |
Uint16 right_rumble = ConvertAxisToRumble(~right); |
| 781 |
|
| 782 |
SDL_GameControllerRumbleTriggers(gamecontroller, left_rumble, right_rumble, 250); |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
SDL_Delay(16); |
| 787 |
SDL_RenderPresent(screen); |
| 788 |
|
| 789 |
#ifdef __EMSCRIPTEN__ |
| 790 |
if (done) { |
| 791 |
emscripten_cancel_main_loop(); |
| 792 |
} |
| 793 |
#endif |
| 794 |
} |
| 795 |
|
| 796 |
int main(int argc, char *argv[]) |
| 797 |
{ |
| 798 |
int i; |
| 799 |
int controller_count = 0; |
| 800 |
int controller_index = 0; |
| 801 |
char guid[64]; |
| 802 |
|
| 803 |
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); |
| 804 |
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1"); |
| 805 |
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); |
| 806 |
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_STEAM, "1"); |
| 807 |
SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1"); |
| 808 |
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); |
| 809 |
SDL_SetHint(SDL_HINT_LINUX_JOYSTICK_DEADZONES, "1"); |
| 810 |
|
| 811 |
/* Enable standard application logging */ |
| 812 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 813 |
|
| 814 |
/* Initialize SDL (Note: video is required to start event loop) */ |
| 815 |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) { |
| 816 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 817 |
return 1; |
| 818 |
} |
| 819 |
|
| 820 |
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt"); |
| 821 |
|
| 822 |
/* Print information about the mappings */ |
| 823 |
if (argv[1] && SDL_strcmp(argv[1], "--mappings") == 0) { |
| 824 |
SDL_Log("Supported mappings:\n"); |
| 825 |
for (i = 0; i < SDL_GameControllerNumMappings(); ++i) { |
| 826 |
char *mapping = SDL_GameControllerMappingForIndex(i); |
| 827 |
if (mapping) { |
| 828 |
SDL_Log("\t%s\n", mapping); |
| 829 |
SDL_free(mapping); |
| 830 |
} |
| 831 |
} |
| 832 |
SDL_Log("\n"); |
| 833 |
} |
| 834 |
|
| 835 |
/* Print information about the controller */ |
| 836 |
for (i = 0; i < SDL_NumJoysticks(); ++i) { |
| 837 |
const char *name; |
| 838 |
const char *path; |
| 839 |
const char *description; |
| 840 |
|
| 841 |
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i), |
| 842 |
guid, sizeof(guid)); |
| 843 |
|
| 844 |
if (SDL_IsGameController(i)) { |
| 845 |
controller_count++; |
| 846 |
name = SDL_GameControllerNameForIndex(i); |
| 847 |
path = SDL_GameControllerPathForIndex(i); |
| 848 |
switch (SDL_GameControllerTypeForIndex(i)) { |
| 849 |
case SDL_CONTROLLER_TYPE_AMAZON_LUNA: |
| 850 |
description = "Amazon Luna Controller"; |
| 851 |
break; |
| 852 |
case SDL_CONTROLLER_TYPE_GOOGLE_STADIA: |
| 853 |
description = "Google Stadia Controller"; |
| 854 |
break; |
| 855 |
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT: |
| 856 |
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT: |
| 857 |
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: |
| 858 |
description = "Nintendo Switch Joy-Con"; |
| 859 |
break; |
| 860 |
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO: |
| 861 |
description = "Nintendo Switch Pro Controller"; |
| 862 |
break; |
| 863 |
case SDL_CONTROLLER_TYPE_PS3: |
| 864 |
description = "PS3 Controller"; |
| 865 |
break; |
| 866 |
case SDL_CONTROLLER_TYPE_PS4: |
| 867 |
description = "PS4 Controller"; |
| 868 |
break; |
| 869 |
case SDL_CONTROLLER_TYPE_PS5: |
| 870 |
description = "PS5 Controller"; |
| 871 |
break; |
| 872 |
case SDL_CONTROLLER_TYPE_XBOX360: |
| 873 |
description = "XBox 360 Controller"; |
| 874 |
break; |
| 875 |
case SDL_CONTROLLER_TYPE_XBOXONE: |
| 876 |
description = "XBox One Controller"; |
| 877 |
break; |
| 878 |
case SDL_CONTROLLER_TYPE_VIRTUAL: |
| 879 |
description = "Virtual Game Controller"; |
| 880 |
break; |
| 881 |
default: |
| 882 |
description = "Game Controller"; |
| 883 |
break; |
| 884 |
} |
| 885 |
AddController(i, SDL_FALSE); |
| 886 |
} else { |
| 887 |
name = SDL_JoystickNameForIndex(i); |
| 888 |
path = SDL_JoystickPathForIndex(i); |
| 889 |
description = "Joystick"; |
| 890 |
} |
| 891 |
SDL_Log("%s %d: %s%s%s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n", |
| 892 |
description, i, name ? name : "Unknown", path ? ", " : "", path ? path : "", guid, |
| 893 |
SDL_JoystickGetDeviceVendor(i), SDL_JoystickGetDeviceProduct(i), SDL_JoystickGetDevicePlayerIndex(i)); |
| 894 |
} |
| 895 |
SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", controller_count, SDL_NumJoysticks()); |
| 896 |
|
| 897 |
/* Create a window to display controller state */ |
| 898 |
window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED, |
| 899 |
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, |
| 900 |
SCREEN_HEIGHT, 0); |
| 901 |
if (!window) { |
| 902 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError()); |
| 903 |
return 2; |
| 904 |
} |
| 905 |
|
| 906 |
screen = SDL_CreateRenderer(window, -1, 0); |
| 907 |
if (!screen) { |
| 908 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); |
| 909 |
SDL_DestroyWindow(window); |
| 910 |
return 2; |
| 911 |
} |
| 912 |
|
| 913 |
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
| 914 |
SDL_RenderClear(screen); |
| 915 |
SDL_RenderPresent(screen); |
| 916 |
|
| 917 |
/* scale for platforms that don't give you the window size you asked for. */ |
| 918 |
SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT); |
| 919 |
|
| 920 |
background_front = LoadTexture(screen, "controllermap.bmp", SDL_FALSE, NULL, NULL); |
| 921 |
background_back = LoadTexture(screen, "controllermap_back.bmp", SDL_FALSE, NULL, NULL); |
| 922 |
button_texture = LoadTexture(screen, "button.bmp", SDL_TRUE, NULL, NULL); |
| 923 |
axis_texture = LoadTexture(screen, "axis.bmp", SDL_TRUE, NULL, NULL); |
| 924 |
|
| 925 |
if (!background_front || !background_back || !button_texture || !axis_texture) { |
| 926 |
SDL_DestroyRenderer(screen); |
| 927 |
SDL_DestroyWindow(window); |
| 928 |
return 2; |
| 929 |
} |
| 930 |
SDL_SetTextureColorMod(button_texture, 10, 255, 21); |
| 931 |
SDL_SetTextureColorMod(axis_texture, 10, 255, 21); |
| 932 |
|
| 933 |
/* !!! FIXME: */ |
| 934 |
/*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/ |
| 935 |
|
| 936 |
for (i = 1; i < argc; ++i) { |
| 937 |
if (SDL_strcmp(argv[i], "--virtual") == 0) { |
| 938 |
OpenVirtualController(); |
| 939 |
} |
| 940 |
if (argv[i] && *argv[i] != '-') { |
| 941 |
controller_index = SDL_atoi(argv[i]); |
| 942 |
break; |
| 943 |
} |
| 944 |
} |
| 945 |
if (controller_index < num_controllers) { |
| 946 |
gamecontroller = gamecontrollers[controller_index]; |
| 947 |
} else { |
| 948 |
gamecontroller = NULL; |
| 949 |
} |
| 950 |
UpdateWindowTitle(); |
| 951 |
|
| 952 |
/* Loop, getting controller events! */ |
| 953 |
#ifdef __EMSCRIPTEN__ |
| 954 |
emscripten_set_main_loop_arg(loop, NULL, 0, 1); |
| 955 |
#else |
| 956 |
while (!done) { |
| 957 |
loop(NULL); |
| 958 |
} |
| 959 |
#endif |
| 960 |
|
| 961 |
/* Reset trigger state */ |
| 962 |
if (trigger_effect != 0) { |
| 963 |
trigger_effect = -1; |
| 964 |
CyclePS5TriggerEffect(); |
| 965 |
} |
| 966 |
|
| 967 |
CloseVirtualController(); |
| 968 |
SDL_DestroyRenderer(screen); |
| 969 |
SDL_DestroyWindow(window); |
| 970 |
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER); |
| 971 |
|
| 972 |
return 0; |
| 973 |
} |
| 974 |
|
| 975 |
#else |
| 976 |
|
| 977 |
int |
| 978 |
main(int argc, char *argv[]) |
| 979 |
{ |
| 980 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); |
| 981 |
return 1; |
| 982 |
} |
| 983 |
|
| 984 |
#endif |
| 985 |
|
| 986 |
/* vi: set ts=4 sw=4 expandtab: */ |