2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* Simple program to test the SDL gamepad routines */
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2022-04-12 06:07:18 -06:00
|
|
|
#include "testutils.h"
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
#define SCREEN_WIDTH 512
|
|
|
|
#define SCREEN_HEIGHT 320
|
2022-05-15 21:01:12 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
#define BUTTON_SIZE 50
|
|
|
|
#define AXIS_SIZE 50
|
2022-05-15 21:01:12 -06:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* This is indexed by SDL_GamepadButton. */
|
2022-11-30 13:51:59 -07:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} button_positions[] = {
|
2022-12-27 10:46:24 -07:00
|
|
|
{ 387, 167 }, /* SDL_GAMEPAD_BUTTON_A */
|
|
|
|
{ 431, 132 }, /* SDL_GAMEPAD_BUTTON_B */
|
|
|
|
{ 342, 132 }, /* SDL_GAMEPAD_BUTTON_X */
|
|
|
|
{ 389, 101 }, /* SDL_GAMEPAD_BUTTON_Y */
|
|
|
|
{ 174, 132 }, /* SDL_GAMEPAD_BUTTON_BACK */
|
|
|
|
{ 232, 128 }, /* SDL_GAMEPAD_BUTTON_GUIDE */
|
|
|
|
{ 289, 132 }, /* SDL_GAMEPAD_BUTTON_START */
|
|
|
|
{ 75, 154 }, /* SDL_GAMEPAD_BUTTON_LEFT_STICK */
|
|
|
|
{ 305, 230 }, /* SDL_GAMEPAD_BUTTON_RIGHT_STICK */
|
|
|
|
{ 77, 40 }, /* SDL_GAMEPAD_BUTTON_LEFT_SHOULDER */
|
|
|
|
{ 396, 36 }, /* SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER */
|
|
|
|
{ 154, 188 }, /* SDL_GAMEPAD_BUTTON_DPAD_UP */
|
|
|
|
{ 154, 249 }, /* SDL_GAMEPAD_BUTTON_DPAD_DOWN */
|
|
|
|
{ 116, 217 }, /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
|
|
|
|
{ 186, 217 }, /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
|
|
|
|
{ 232, 174 }, /* SDL_GAMEPAD_BUTTON_MISC1 */
|
|
|
|
{ 132, 135 }, /* SDL_GAMEPAD_BUTTON_PADDLE1 */
|
|
|
|
{ 330, 135 }, /* SDL_GAMEPAD_BUTTON_PADDLE2 */
|
|
|
|
{ 132, 175 }, /* SDL_GAMEPAD_BUTTON_PADDLE3 */
|
|
|
|
{ 330, 175 }, /* SDL_GAMEPAD_BUTTON_PADDLE4 */
|
|
|
|
{ 0, 0 }, /* SDL_GAMEPAD_BUTTON_TOUCHPAD */
|
2015-06-21 09:33:46 -06:00
|
|
|
};
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_COMPILE_TIME_ASSERT(button_positions, SDL_arraysize(button_positions) == SDL_GAMEPAD_BUTTON_MAX);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* This is indexed by SDL_GamepadAxis. */
|
2022-11-30 13:51:59 -07:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
double angle;
|
|
|
|
} axis_positions[] = {
|
|
|
|
{ 74, 153, 270.0 }, /* LEFTX */
|
|
|
|
{ 74, 153, 0.0 }, /* LEFTY */
|
|
|
|
{ 306, 231, 270.0 }, /* RIGHTX */
|
|
|
|
{ 306, 231, 0.0 }, /* RIGHTY */
|
|
|
|
{ 91, -20, 0.0 }, /* TRIGGERLEFT */
|
|
|
|
{ 375, -20, 0.0 }, /* TRIGGERRIGHT */
|
2015-06-21 09:33:46 -06:00
|
|
|
};
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_COMPILE_TIME_ASSERT(axis_positions, SDL_arraysize(axis_positions) == SDL_GAMEPAD_AXIS_MAX);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-07-24 14:19:02 -06:00
|
|
|
/* This is indexed by SDL_JoystickPowerLevel + 1. */
|
2022-11-30 13:51:59 -07:00
|
|
|
static const char *power_level_strings[] = {
|
2022-07-24 14:19:02 -06:00
|
|
|
"unknown", /* SDL_JOYSTICK_POWER_UNKNOWN */
|
|
|
|
"empty", /* SDL_JOYSTICK_POWER_EMPTY */
|
|
|
|
"low", /* SDL_JOYSTICK_POWER_LOW */
|
|
|
|
"medium", /* SDL_JOYSTICK_POWER_MEDIUM */
|
|
|
|
"full", /* SDL_JOYSTICK_POWER_FULL */
|
|
|
|
"wired", /* SDL_JOYSTICK_POWER_WIRED */
|
|
|
|
};
|
|
|
|
SDL_COMPILE_TIME_ASSERT(power_level_strings, SDL_arraysize(power_level_strings) == SDL_JOYSTICK_POWER_MAX + 1);
|
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
static SDL_Window *window = NULL;
|
|
|
|
static SDL_Renderer *screen = NULL;
|
|
|
|
static SDL_bool retval = SDL_FALSE;
|
|
|
|
static SDL_bool done = SDL_FALSE;
|
|
|
|
static SDL_bool set_LED = SDL_FALSE;
|
|
|
|
static int trigger_effect = 0;
|
2022-10-05 15:56:27 -06:00
|
|
|
static SDL_Texture *background_front, *background_back, *button_texture, *axis_texture;
|
2022-12-27 10:46:24 -07:00
|
|
|
static SDL_Gamepad *gamepad;
|
|
|
|
static SDL_Gamepad **gamepads;
|
|
|
|
static int num_gamepads = 0;
|
2022-05-15 21:01:12 -06:00
|
|
|
static SDL_Joystick *virtual_joystick = NULL;
|
2022-12-27 10:46:24 -07:00
|
|
|
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
|
2022-12-29 20:31:12 -07:00
|
|
|
static float virtual_axis_start_x;
|
|
|
|
static float virtual_axis_start_y;
|
2022-12-27 10:46:24 -07:00
|
|
|
static SDL_GamepadButton virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-02-23 12:58:14 -07:00
|
|
|
|
|
|
|
static void PrintJoystickInfo(SDL_JoystickID instance_id)
|
|
|
|
{
|
|
|
|
char guid[64];
|
|
|
|
const char *name;
|
|
|
|
const char *path;
|
|
|
|
const char *description;
|
2023-03-01 17:37:56 -07:00
|
|
|
const char *mapping = NULL;
|
2023-02-23 12:58:14 -07:00
|
|
|
|
|
|
|
SDL_GetJoystickGUIDString(SDL_GetJoystickInstanceGUID(instance_id), guid, sizeof(guid));
|
|
|
|
|
|
|
|
if (SDL_IsGamepad(instance_id)) {
|
|
|
|
name = SDL_GetGamepadInstanceName(instance_id);
|
|
|
|
path = SDL_GetGamepadInstancePath(instance_id);
|
|
|
|
switch (SDL_GetGamepadInstanceType(instance_id)) {
|
|
|
|
case SDL_GAMEPAD_TYPE_AMAZON_LUNA:
|
|
|
|
description = "Amazon Luna Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_GOOGLE_STADIA:
|
|
|
|
description = "Google Stadia Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
|
|
|
|
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
|
|
|
|
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
|
|
|
|
description = "Nintendo Switch Joy-Con";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
|
|
|
|
description = "Nintendo Switch Pro Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_PS3:
|
|
|
|
description = "PS3 Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_PS4:
|
|
|
|
description = "PS4 Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_PS5:
|
|
|
|
description = "PS5 Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_XBOX360:
|
|
|
|
description = "XBox 360 Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_XBOXONE:
|
|
|
|
description = "XBox One Controller";
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_TYPE_VIRTUAL:
|
|
|
|
description = "Virtual Gamepad";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
description = "Gamepad";
|
|
|
|
break;
|
|
|
|
}
|
2023-03-01 17:37:56 -07:00
|
|
|
mapping = SDL_GetGamepadInstanceMapping(instance_id);
|
2023-02-23 12:58:14 -07:00
|
|
|
} else {
|
|
|
|
name = SDL_GetJoystickInstanceName(instance_id);
|
|
|
|
path = SDL_GetJoystickInstancePath(instance_id);
|
|
|
|
description = "Joystick";
|
|
|
|
}
|
|
|
|
SDL_Log("%s: %s%s%s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n",
|
|
|
|
description, name ? name : "Unknown", path ? ", " : "", path ? path : "", guid,
|
|
|
|
SDL_GetJoystickInstanceVendor(instance_id),
|
|
|
|
SDL_GetJoystickInstanceProduct(instance_id),
|
|
|
|
SDL_GetJoystickInstancePlayerIndex(instance_id));
|
2023-03-01 17:37:56 -07:00
|
|
|
if (mapping) {
|
|
|
|
SDL_Log("Mapping: %s\n", mapping);
|
|
|
|
}
|
2023-02-23 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
2020-11-27 19:57:38 -07:00
|
|
|
static void UpdateWindowTitle()
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2022-11-27 09:38:43 -07:00
|
|
|
if (window == NULL) {
|
2020-11-27 19:57:38 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad) {
|
|
|
|
const char *name = SDL_GetGamepadName(gamepad);
|
|
|
|
const char *serial = SDL_GetGamepadSerial(gamepad);
|
|
|
|
const char *basetitle = "Gamepad Test: ";
|
2022-09-22 23:50:28 -06:00
|
|
|
const size_t titlelen = SDL_strlen(basetitle) + (name ? SDL_strlen(name) : 0) + (serial ? 3 + SDL_strlen(serial) : 0) + 1;
|
2020-11-27 19:57:38 -07:00
|
|
|
char *title = (char *)SDL_malloc(titlelen);
|
|
|
|
|
|
|
|
retval = SDL_FALSE;
|
|
|
|
done = SDL_FALSE;
|
|
|
|
|
|
|
|
if (title) {
|
2022-09-22 23:50:28 -06:00
|
|
|
SDL_strlcpy(title, basetitle, titlelen);
|
|
|
|
if (name) {
|
|
|
|
SDL_strlcat(title, name, titlelen);
|
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
if (serial) {
|
|
|
|
SDL_strlcat(title, " (", titlelen);
|
|
|
|
SDL_strlcat(title, serial, titlelen);
|
|
|
|
SDL_strlcat(title, ")", titlelen);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
SDL_SetWindowTitle(window, title);
|
|
|
|
SDL_free(title);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
} else {
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_SetWindowTitle(window, "Waiting for gamepad...");
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-09-07 01:00:27 -06:00
|
|
|
static const char *GetSensorName(SDL_SensorType sensor)
|
|
|
|
{
|
|
|
|
switch (sensor) {
|
|
|
|
case SDL_SENSOR_ACCEL:
|
|
|
|
return "accelerometer";
|
|
|
|
case SDL_SENSOR_GYRO:
|
|
|
|
return "gyro";
|
|
|
|
case SDL_SENSOR_ACCEL_L:
|
|
|
|
return "accelerometer (L)";
|
|
|
|
case SDL_SENSOR_GYRO_L:
|
|
|
|
return "gyro (L)";
|
|
|
|
case SDL_SENSOR_ACCEL_R:
|
|
|
|
return "accelerometer (R)";
|
|
|
|
case SDL_SENSOR_GYRO_R:
|
|
|
|
return "gyro (R)";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static int FindGamepad(SDL_JoystickID gamepad_id)
|
2020-11-27 19:57:38 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
for (i = 0; i < num_gamepads; ++i) {
|
|
|
|
if (gamepad_id == SDL_GetJoystickInstanceID(SDL_GetGamepadJoystick(gamepads[i]))) {
|
2020-11-27 19:57:38 -07:00
|
|
|
return i;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
return -1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2022-12-27 19:10:06 -07:00
|
|
|
static void AddGamepad(SDL_JoystickID gamepad_id, SDL_bool verbose)
|
2020-04-18 22:41:37 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Gamepad **new_gamepads;
|
|
|
|
SDL_Gamepad *new_gamepad;
|
2022-06-06 07:48:54 -06:00
|
|
|
Uint16 firmware_version;
|
2022-09-07 01:00:27 -06:00
|
|
|
SDL_SensorType sensors[] = {
|
|
|
|
SDL_SENSOR_ACCEL,
|
|
|
|
SDL_SENSOR_GYRO,
|
|
|
|
SDL_SENSOR_ACCEL_L,
|
|
|
|
SDL_SENSOR_GYRO_L,
|
|
|
|
SDL_SENSOR_ACCEL_R,
|
|
|
|
SDL_SENSOR_GYRO_R
|
|
|
|
};
|
|
|
|
unsigned int i;
|
2020-11-27 19:57:38 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (FindGamepad(gamepad_id) >= 0) {
|
|
|
|
/* We already have this gamepad */
|
2020-11-27 19:57:38 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 19:10:06 -07:00
|
|
|
new_gamepad = SDL_OpenGamepad(gamepad_id);
|
2022-12-27 10:46:24 -07:00
|
|
|
if (new_gamepad == NULL) {
|
|
|
|
SDL_Log("Couldn't open gamepad: %s\n", SDL_GetError());
|
2020-11-27 19:57:38 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
new_gamepads = (SDL_Gamepad **)SDL_realloc(gamepads, (num_gamepads + 1) * sizeof(*gamepads));
|
|
|
|
if (new_gamepads == NULL) {
|
|
|
|
SDL_CloseGamepad(gamepad);
|
2020-11-27 19:57:38 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
new_gamepads[num_gamepads++] = new_gamepad;
|
|
|
|
gamepads = new_gamepads;
|
|
|
|
gamepad = new_gamepad;
|
2021-07-08 14:22:41 -06:00
|
|
|
trigger_effect = 0;
|
2020-11-27 19:57:38 -07:00
|
|
|
|
|
|
|
if (verbose) {
|
2022-12-27 10:46:24 -07:00
|
|
|
const char *name = SDL_GetGamepadName(gamepad);
|
|
|
|
const char *path = SDL_GetGamepadPath(gamepad);
|
|
|
|
SDL_Log("Opened gamepad %s%s%s\n", name, path ? ", " : "", path ? path : "");
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
2020-11-17 11:30:20 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
firmware_version = SDL_GetGamepadFirmwareVersion(gamepad);
|
2022-06-06 07:48:54 -06:00
|
|
|
if (firmware_version) {
|
|
|
|
if (verbose) {
|
|
|
|
SDL_Log("Firmware version: 0x%x (%d)\n", firmware_version, firmware_version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 01:00:27 -06:00
|
|
|
for (i = 0; i < SDL_arraysize(sensors); ++i) {
|
|
|
|
SDL_SensorType sensor = sensors[i];
|
2020-11-17 11:30:20 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (SDL_GamepadHasSensor(gamepad, sensor)) {
|
2022-09-07 01:00:27 -06:00
|
|
|
if (verbose) {
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Log("Enabling %s at %.2f Hz\n", GetSensorName(sensor), SDL_GetGamepadSensorDataRate(gamepad, sensor));
|
2022-09-07 01:00:27 -06:00
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_SetGamepadSensorEnabled(gamepad, sensor, SDL_TRUE);
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
2020-11-17 11:30:20 -07:00
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (SDL_GamepadHasRumble(gamepad)) {
|
2021-11-11 14:53:11 -07:00
|
|
|
SDL_Log("Rumble supported");
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (SDL_GamepadHasRumbleTriggers(gamepad)) {
|
2021-11-11 14:53:11 -07:00
|
|
|
SDL_Log("Trigger rumble supported");
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:57:38 -07:00
|
|
|
UpdateWindowTitle();
|
2020-11-17 11:30:20 -07:00
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static void SetGamepad(SDL_JoystickID gamepad_id)
|
2020-11-17 11:30:20 -07:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
int i = FindGamepad(gamepad_id);
|
2020-11-17 11:30:20 -07:00
|
|
|
|
2020-11-27 19:57:38 -07:00
|
|
|
if (i < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-17 11:30:20 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad != gamepads[i]) {
|
|
|
|
gamepad = gamepads[i];
|
2020-11-27 19:57:38 -07:00
|
|
|
UpdateWindowTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static void DelGamepad(SDL_JoystickID gamepad_id)
|
2020-11-27 19:57:38 -07:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
int i = FindGamepad(gamepad_id);
|
2020-11-27 19:57:38 -07:00
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_CloseGamepad(gamepads[i]);
|
2020-11-27 19:57:42 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
--num_gamepads;
|
|
|
|
if (i < num_gamepads) {
|
|
|
|
SDL_memcpy(&gamepads[i], &gamepads[i + 1], (num_gamepads - i) * sizeof(*gamepads));
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (num_gamepads > 0) {
|
|
|
|
gamepad = gamepads[0];
|
2020-11-27 19:57:38 -07:00
|
|
|
} else {
|
2022-12-27 10:46:24 -07:00
|
|
|
gamepad = NULL;
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
|
|
|
UpdateWindowTitle();
|
|
|
|
}
|
|
|
|
|
2021-11-23 04:22:02 -07:00
|
|
|
static Uint16 ConvertAxisToRumble(Sint16 axisval)
|
2020-11-11 19:57:37 -07:00
|
|
|
{
|
|
|
|
/* Only start rumbling if the axis is past the halfway point */
|
2020-11-12 08:53:01 -07:00
|
|
|
const Sint16 half_axis = (Sint16)SDL_ceil(SDL_JOYSTICK_AXIS_MAX / 2.0f);
|
2021-11-23 04:22:02 -07:00
|
|
|
if (axisval > half_axis) {
|
|
|
|
return (Uint16)(axisval - half_axis) * 4;
|
2020-11-11 19:57:37 -07:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
/* PS5 trigger effect documentation:
|
|
|
|
https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2022-11-30 13:51:59 -07:00
|
|
|
Uint8 ucEnableBits1; /* 0 */
|
|
|
|
Uint8 ucEnableBits2; /* 1 */
|
|
|
|
Uint8 ucRumbleRight; /* 2 */
|
|
|
|
Uint8 ucRumbleLeft; /* 3 */
|
|
|
|
Uint8 ucHeadphoneVolume; /* 4 */
|
|
|
|
Uint8 ucSpeakerVolume; /* 5 */
|
|
|
|
Uint8 ucMicrophoneVolume; /* 6 */
|
|
|
|
Uint8 ucAudioEnableBits; /* 7 */
|
|
|
|
Uint8 ucMicLightMode; /* 8 */
|
|
|
|
Uint8 ucAudioMuteBits; /* 9 */
|
|
|
|
Uint8 rgucRightTriggerEffect[11]; /* 10 */
|
|
|
|
Uint8 rgucLeftTriggerEffect[11]; /* 21 */
|
|
|
|
Uint8 rgucUnknown1[6]; /* 32 */
|
|
|
|
Uint8 ucLedFlags; /* 38 */
|
|
|
|
Uint8 rgucUnknown2[2]; /* 39 */
|
|
|
|
Uint8 ucLedAnim; /* 41 */
|
|
|
|
Uint8 ucLedBrightness; /* 42 */
|
|
|
|
Uint8 ucPadLights; /* 43 */
|
|
|
|
Uint8 ucLedRed; /* 44 */
|
|
|
|
Uint8 ucLedGreen; /* 45 */
|
|
|
|
Uint8 ucLedBlue; /* 46 */
|
2021-07-08 14:22:41 -06:00
|
|
|
} DS5EffectsState_t;
|
|
|
|
|
|
|
|
static void CyclePS5TriggerEffect()
|
|
|
|
{
|
|
|
|
DS5EffectsState_t state;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
Uint8 effects[3][11] = {
|
2021-07-08 14:22:41 -06:00
|
|
|
/* Clear trigger effect */
|
|
|
|
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
/* Constant resistance across entire trigger pull */
|
|
|
|
{ 0x01, 0, 110, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
/* Resistance and vibration when trigger is pulled */
|
|
|
|
{ 0x06, 15, 63, 128, 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
trigger_effect = (trigger_effect + 1) % SDL_arraysize(effects);
|
|
|
|
|
|
|
|
SDL_zero(state);
|
|
|
|
state.ucEnableBits1 |= (0x04 | 0x08); /* Modify right and left trigger effect respectively */
|
|
|
|
SDL_memcpy(state.rgucRightTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect]));
|
|
|
|
SDL_memcpy(state.rgucLeftTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect]));
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_SendGamepadEffect(gamepad, &state, sizeof(state));
|
2021-07-08 14:22:41 -06:00
|
|
|
}
|
|
|
|
|
2022-05-15 21:01:12 -06:00
|
|
|
static SDL_bool ShowingFront()
|
|
|
|
{
|
|
|
|
SDL_bool showing_front = SDL_TRUE;
|
|
|
|
int i;
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad) {
|
|
|
|
/* Show the back of the gamepad if the paddles are being held */
|
|
|
|
for (i = SDL_GAMEPAD_BUTTON_PADDLE1; i <= SDL_GAMEPAD_BUTTON_PADDLE4; ++i) {
|
|
|
|
if (SDL_GetGamepadButton(gamepad, (SDL_GamepadButton)i) == SDL_PRESSED) {
|
2022-05-15 21:01:12 -06:00
|
|
|
showing_front = SDL_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 14:08:42 -07:00
|
|
|
if (SDL_GetModState() & SDL_KMOD_SHIFT) {
|
2022-05-15 21:01:12 -06:00
|
|
|
showing_front = SDL_FALSE;
|
|
|
|
}
|
|
|
|
return showing_front;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static void SDLCALL VirtualGamepadSetPlayerIndex(void *userdata, int player_index)
|
2022-05-16 08:21:28 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Log("Virtual Gamepad: player index set to %d\n", player_index);
|
2022-05-16 08:21:28 -06:00
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static int SDLCALL VirtualGamepadRumble(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
|
2022-05-16 08:21:28 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Log("Virtual Gamepad: rumble set to %d/%d\n", low_frequency_rumble, high_frequency_rumble);
|
2022-05-16 08:21:28 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static int SDLCALL VirtualGamepadRumbleTriggers(void *userdata, Uint16 left_rumble, Uint16 right_rumble)
|
2022-05-16 08:21:28 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Log("Virtual Gamepad: trigger rumble set to %d/%d\n", left_rumble, right_rumble);
|
2022-05-16 08:21:28 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static int SDLCALL VirtualGamepadSetLED(void *userdata, Uint8 red, Uint8 green, Uint8 blue)
|
2022-05-16 08:21:28 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_Log("Virtual Gamepad: LED set to RGB %d,%d,%d\n", red, green, blue);
|
2022-05-16 08:21:28 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static void OpenVirtualGamepad()
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
|
|
|
SDL_VirtualJoystickDesc desc;
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_JoystickID virtual_id;
|
2022-05-15 21:01:12 -06:00
|
|
|
|
|
|
|
SDL_zero(desc);
|
|
|
|
desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
|
2022-12-27 12:31:54 -07:00
|
|
|
desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
|
2022-12-27 10:46:24 -07:00
|
|
|
desc.naxes = SDL_GAMEPAD_AXIS_MAX;
|
|
|
|
desc.nbuttons = SDL_GAMEPAD_BUTTON_MAX;
|
|
|
|
desc.SetPlayerIndex = VirtualGamepadSetPlayerIndex;
|
|
|
|
desc.Rumble = VirtualGamepadRumble;
|
|
|
|
desc.RumbleTriggers = VirtualGamepadRumbleTriggers;
|
|
|
|
desc.SetLED = VirtualGamepadSetLED;
|
2022-05-16 09:55:54 -06:00
|
|
|
|
2022-12-27 19:10:06 -07:00
|
|
|
virtual_id = SDL_AttachVirtualJoystickEx(&desc);
|
|
|
|
if (virtual_id == 0) {
|
|
|
|
SDL_Log("Couldn't attach virtual device: %s\n", SDL_GetError());
|
2022-05-16 09:55:54 -06:00
|
|
|
} else {
|
2022-12-27 19:10:06 -07:00
|
|
|
virtual_joystick = SDL_OpenJoystick(virtual_id);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (virtual_joystick == NULL) {
|
2022-05-16 09:55:54 -06:00
|
|
|
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
static void CloseVirtualGamepad()
|
2022-05-16 09:55:54 -06:00
|
|
|
{
|
|
|
|
int i;
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
|
|
|
|
if (joysticks) {
|
|
|
|
for (i = 0; joysticks[i]; ++i) {
|
|
|
|
SDL_JoystickID instance_id = joysticks[i];
|
|
|
|
if (SDL_IsJoystickVirtual(instance_id)) {
|
|
|
|
SDL_DetachVirtualJoystick(instance_id);
|
|
|
|
}
|
2022-05-16 09:55:54 -06:00
|
|
|
}
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_free(joysticks);
|
2022-05-16 09:55:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virtual_joystick) {
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_CloseJoystick(virtual_joystick);
|
2022-05-16 09:55:54 -06:00
|
|
|
virtual_joystick = NULL;
|
|
|
|
}
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
static SDL_GamepadButton FindButtonAtPosition(float x, float y)
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
2022-12-29 20:31:12 -07:00
|
|
|
SDL_FPoint point;
|
2022-05-15 21:01:12 -06:00
|
|
|
int i;
|
|
|
|
SDL_bool showing_front = ShowingFront();
|
|
|
|
|
|
|
|
point.x = x;
|
|
|
|
point.y = y;
|
2022-12-27 10:46:24 -07:00
|
|
|
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
|
|
|
|
SDL_bool on_front = (i < SDL_GAMEPAD_BUTTON_PADDLE1 || i > SDL_GAMEPAD_BUTTON_PADDLE4);
|
2022-05-15 21:01:12 -06:00
|
|
|
if (on_front == showing_front) {
|
2022-12-29 20:31:12 -07:00
|
|
|
SDL_FRect rect;
|
|
|
|
rect.x = (float)button_positions[i].x;
|
|
|
|
rect.y = (float)button_positions[i].y;
|
|
|
|
rect.w = (float)BUTTON_SIZE;
|
|
|
|
rect.h = (float)BUTTON_SIZE;
|
|
|
|
if (SDL_PointInRectFloat(&point, &rect)) {
|
2022-12-27 10:46:24 -07:00
|
|
|
return (SDL_GamepadButton)i;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
return SDL_GAMEPAD_BUTTON_INVALID;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
static SDL_GamepadAxis FindAxisAtPosition(float x, float y)
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
2022-12-29 20:31:12 -07:00
|
|
|
SDL_FPoint point;
|
2022-05-15 21:01:12 -06:00
|
|
|
int i;
|
|
|
|
SDL_bool showing_front = ShowingFront();
|
|
|
|
|
|
|
|
point.x = x;
|
|
|
|
point.y = y;
|
2022-12-27 10:46:24 -07:00
|
|
|
for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
|
2022-05-15 21:01:12 -06:00
|
|
|
if (showing_front) {
|
2022-12-29 20:31:12 -07:00
|
|
|
SDL_FRect rect;
|
|
|
|
rect.x = (float)axis_positions[i].x;
|
|
|
|
rect.y = (float)axis_positions[i].y;
|
|
|
|
rect.w = (float)AXIS_SIZE;
|
|
|
|
rect.h = (float)AXIS_SIZE;
|
|
|
|
if (SDL_PointInRectFloat(&point, &rect)) {
|
2022-12-27 10:46:24 -07:00
|
|
|
return (SDL_GamepadAxis)i;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
return SDL_GAMEPAD_AXIS_INVALID;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
static void VirtualGamepadMouseMotion(float x, float y)
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
|
|
|
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
|
2022-12-29 20:31:12 -07:00
|
|
|
const float MOVING_DISTANCE = 2.0f;
|
|
|
|
if (SDL_fabs(x - virtual_axis_start_x) >= MOVING_DISTANCE ||
|
|
|
|
SDL_fabs(y - virtual_axis_start_y) >= MOVING_DISTANCE) {
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
2022-12-27 10:46:24 -07:00
|
|
|
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
|
|
|
|
if (virtual_axis_active == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
|
|
|
|
virtual_axis_active == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
|
2022-05-15 21:01:12 -06:00
|
|
|
int range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
|
2022-12-29 20:31:12 -07:00
|
|
|
float distance = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f);
|
2022-05-15 21:01:12 -06:00
|
|
|
Sint16 value = (Sint16)(SDL_JOYSTICK_AXIS_MIN + (distance * range));
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, value);
|
2022-05-15 21:01:12 -06:00
|
|
|
} else {
|
2022-12-29 20:31:12 -07:00
|
|
|
float distanceX = SDL_clamp((x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f);
|
|
|
|
float distanceY = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f);
|
2022-05-15 21:01:12 -06:00
|
|
|
Sint16 valueX, valueY;
|
|
|
|
|
|
|
|
if (distanceX >= 0) {
|
|
|
|
valueX = (Sint16)(distanceX * SDL_JOYSTICK_AXIS_MAX);
|
|
|
|
} else {
|
|
|
|
valueX = (Sint16)(distanceX * -SDL_JOYSTICK_AXIS_MIN);
|
|
|
|
}
|
|
|
|
if (distanceY >= 0) {
|
|
|
|
valueY = (Sint16)(distanceY * SDL_JOYSTICK_AXIS_MAX);
|
|
|
|
} else {
|
|
|
|
valueY = (Sint16)(distanceY * -SDL_JOYSTICK_AXIS_MIN);
|
|
|
|
}
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, valueX);
|
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active + 1, valueY);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
static void VirtualGamepadMouseDown(float x, float y)
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_GamepadButton button;
|
|
|
|
SDL_GamepadAxis axis;
|
2022-05-15 21:01:12 -06:00
|
|
|
|
|
|
|
button = FindButtonAtPosition(x, y);
|
2022-12-27 10:46:24 -07:00
|
|
|
if (button != SDL_GAMEPAD_BUTTON_INVALID) {
|
2022-05-15 21:01:12 -06:00
|
|
|
virtual_button_active = button;
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_PRESSED);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
axis = FindAxisAtPosition(x, y);
|
2022-12-27 10:46:24 -07:00
|
|
|
if (axis != SDL_GAMEPAD_AXIS_INVALID) {
|
2022-05-15 21:01:12 -06:00
|
|
|
virtual_axis_active = axis;
|
|
|
|
virtual_axis_start_x = x;
|
|
|
|
virtual_axis_start_y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
static void VirtualGamepadMouseUp(float x, float y)
|
2022-05-15 21:01:12 -06:00
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
2022-12-27 10:46:24 -07:00
|
|
|
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
|
|
|
|
if (virtual_axis_active == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
|
|
|
|
virtual_axis_active == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, SDL_JOYSTICK_AXIS_MIN);
|
2022-05-15 21:01:12 -06:00
|
|
|
} else {
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, 0);
|
|
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active + 1, 0);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
void loop(void *arg)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
int i;
|
2022-05-15 21:01:12 -06:00
|
|
|
SDL_bool showing_front;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2021-07-29 07:36:20 -06:00
|
|
|
/* Update to get the current event state */
|
|
|
|
SDL_PumpEvents();
|
|
|
|
|
|
|
|
/* Process all currently pending events */
|
2023-01-23 18:54:09 -07:00
|
|
|
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) == 1) {
|
2015-06-21 09:33:46 -06:00
|
|
|
switch (event.type) {
|
2023-02-23 12:58:14 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_ADDED:
|
|
|
|
PrintJoystickInfo(event.jdevice.which);
|
|
|
|
break;
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_ADDED:
|
2023-03-01 10:13:01 -07:00
|
|
|
SDL_Log("Gamepad device %" SDL_PRIu32 " added.\n",
|
|
|
|
event.gdevice.which);
|
|
|
|
AddGamepad(event.gdevice.which, SDL_TRUE);
|
2020-04-18 22:41:37 -06:00
|
|
|
break;
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
2023-03-01 10:13:01 -07:00
|
|
|
SDL_Log("Gamepad device %" SDL_PRIu32 " removed.\n",
|
|
|
|
event.gdevice.which);
|
|
|
|
DelGamepad(event.gdevice.which);
|
2020-04-18 22:41:37 -06:00
|
|
|
break;
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
|
|
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
|
|
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " touchpad %" SDL_PRIs32 " finger %" SDL_PRIs32 " %s %.2f, %.2f, %.2f\n",
|
2023-03-01 10:13:01 -07:00
|
|
|
event.gtouchpad.which,
|
|
|
|
event.gtouchpad.touchpad,
|
|
|
|
event.gtouchpad.finger,
|
2023-01-23 18:54:09 -07:00
|
|
|
(event.type == SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN ? "pressed at" : (event.type == SDL_EVENT_GAMEPAD_TOUCHPAD_UP ? "released at" : "moved to")),
|
2023-03-01 10:13:01 -07:00
|
|
|
event.gtouchpad.x,
|
|
|
|
event.gtouchpad.y,
|
|
|
|
event.gtouchpad.pressure);
|
2020-11-13 19:01:29 -07:00
|
|
|
break;
|
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
#define VERBOSE_SENSORS
|
|
|
|
#ifdef VERBOSE_SENSORS
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " sensor %s: %.2f, %.2f, %.2f (%" SDL_PRIu64 ")\n",
|
2023-03-01 10:13:01 -07:00
|
|
|
event.gsensor.which,
|
|
|
|
GetSensorName((SDL_SensorType) event.gsensor.sensor),
|
|
|
|
event.gsensor.data[0],
|
|
|
|
event.gsensor.data[1],
|
|
|
|
event.gsensor.data[2],
|
|
|
|
event.gsensor.sensor_timestamp);
|
2020-11-17 11:30:20 -07:00
|
|
|
break;
|
2021-07-08 14:22:41 -06:00
|
|
|
#endif /* VERBOSE_SENSORS */
|
2020-11-17 11:30:20 -07:00
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
#define VERBOSE_AXES
|
|
|
|
#ifdef VERBOSE_AXES
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
|
2023-03-01 10:13:01 -07:00
|
|
|
if (event.gaxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.gaxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
|
|
|
|
SetGamepad(event.gaxis.which);
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
2023-03-01 10:13:01 -07:00
|
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " axis %s changed to %d\n",
|
|
|
|
event.gaxis.which,
|
|
|
|
SDL_GetGamepadStringForAxis((SDL_GamepadAxis) event.gaxis.axis),
|
|
|
|
event.gaxis.value);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2021-07-08 14:22:41 -06:00
|
|
|
#endif /* VERBOSE_AXES */
|
2020-11-13 19:01:29 -07:00
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
|
|
|
case SDL_EVENT_GAMEPAD_BUTTON_UP:
|
|
|
|
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
2023-03-01 10:13:01 -07:00
|
|
|
SetGamepad(event.gbutton.which);
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
2023-03-01 10:13:01 -07:00
|
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " button %s %s\n",
|
|
|
|
event.gbutton.which,
|
|
|
|
SDL_GetGamepadStringForButton((SDL_GamepadButton) event.gbutton.button),
|
|
|
|
event.gbutton.state ? "pressed" : "released");
|
2021-07-08 14:22:41 -06:00
|
|
|
|
|
|
|
/* Cycle PS5 trigger effects when the microphone button is pressed */
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN &&
|
2023-03-01 10:13:01 -07:00
|
|
|
event.gbutton.button == SDL_GAMEPAD_BUTTON_MISC1 &&
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_GetGamepadType(gamepad) == SDL_GAMEPAD_TYPE_PS5) {
|
2021-07-08 14:22:41 -06:00
|
|
|
CyclePS5TriggerEffect();
|
|
|
|
}
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2020-11-13 19:01:29 -07:00
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_BATTERY_UPDATED:
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " battery state changed to %s\n", event.jbattery.which, power_level_strings[event.jbattery.level + 1]);
|
2022-07-24 14:19:02 -06:00
|
|
|
break;
|
|
|
|
|
2023-01-29 20:06:08 -07:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
2022-05-15 21:01:12 -06:00
|
|
|
if (virtual_joystick) {
|
2022-12-27 10:46:24 -07:00
|
|
|
VirtualGamepadMouseDown(event.button.x, event.button.y);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-01-29 20:06:08 -07:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
2022-05-15 21:01:12 -06:00
|
|
|
if (virtual_joystick) {
|
2022-12-27 10:46:24 -07:00
|
|
|
VirtualGamepadMouseUp(event.button.x, event.button.y);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
2022-05-15 21:01:12 -06:00
|
|
|
if (virtual_joystick) {
|
2022-12-27 10:46:24 -07:00
|
|
|
VirtualGamepadMouseMotion(event.motion.x, event.motion.y);
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_KEY_DOWN:
|
2020-12-22 15:38:32 -07:00
|
|
|
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad) {
|
2020-12-22 15:38:32 -07:00
|
|
|
int player_index = (event.key.keysym.sym - SDLK_0);
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_SetGamepadPlayerIndex(gamepad, player_index);
|
2020-12-22 15:38:32 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-05-16 09:55:54 -06:00
|
|
|
if (event.key.keysym.sym == SDLK_a) {
|
2022-12-27 10:46:24 -07:00
|
|
|
OpenVirtualGamepad();
|
2022-05-16 09:55:54 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (event.key.keysym.sym == SDLK_d) {
|
2022-12-27 10:46:24 -07:00
|
|
|
CloseVirtualGamepad();
|
2022-05-16 09:55:54 -06:00
|
|
|
break;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
if (event.key.keysym.sym != SDLK_ESCAPE) {
|
|
|
|
break;
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 15:38:12 -06:00
|
|
|
SDL_FALLTHROUGH;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_QUIT:
|
2015-06-21 09:33:46 -06:00
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:01:12 -06:00
|
|
|
showing_front = ShowingFront();
|
2020-11-07 03:22:15 -07:00
|
|
|
|
|
|
|
/* blank screen, set up for drawing this frame. */
|
|
|
|
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(screen);
|
2022-12-27 07:21:13 -07:00
|
|
|
SDL_RenderTexture(screen, showing_front ? background_front : background_back, NULL, NULL);
|
2020-11-07 03:22:15 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad) {
|
|
|
|
/* Update visual gamepad state */
|
|
|
|
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
|
|
|
|
if (SDL_GetGamepadButton(gamepad, (SDL_GamepadButton)i) == SDL_PRESSED) {
|
|
|
|
SDL_bool on_front = (i < SDL_GAMEPAD_BUTTON_PADDLE1 || i > SDL_GAMEPAD_BUTTON_PADDLE4);
|
2020-11-07 03:22:15 -07:00
|
|
|
if (on_front == showing_front) {
|
2022-12-31 12:19:32 -07:00
|
|
|
SDL_FRect dst;
|
|
|
|
dst.x = (float)button_positions[i].x;
|
|
|
|
dst.y = (float)button_positions[i].y;
|
|
|
|
dst.w = (float)BUTTON_SIZE;
|
|
|
|
dst.h = (float)BUTTON_SIZE;
|
2022-12-27 07:21:13 -07:00
|
|
|
SDL_RenderTextureRotated(screen, button_texture, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
|
2020-11-07 03:22:15 -07:00
|
|
|
}
|
2020-04-18 22:41:37 -06:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2020-11-07 03:22:15 -07:00
|
|
|
if (showing_front) {
|
2022-12-27 10:46:24 -07:00
|
|
|
for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
|
2022-11-30 13:51:59 -07:00
|
|
|
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
|
2022-12-27 10:46:24 -07:00
|
|
|
const Sint16 value = SDL_GetGamepadAxis(gamepad, (SDL_GamepadAxis)(i));
|
2020-11-07 03:22:15 -07:00
|
|
|
if (value < -deadzone) {
|
|
|
|
const double angle = axis_positions[i].angle;
|
2022-12-31 12:19:32 -07:00
|
|
|
SDL_FRect dst;
|
|
|
|
dst.x = (float)axis_positions[i].x;
|
|
|
|
dst.y = (float)axis_positions[i].y;
|
|
|
|
dst.w = (float)AXIS_SIZE;
|
|
|
|
dst.h = (float)AXIS_SIZE;
|
2022-12-27 07:21:13 -07:00
|
|
|
SDL_RenderTextureRotated(screen, axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
2020-11-07 03:22:15 -07:00
|
|
|
} else if (value > deadzone) {
|
|
|
|
const double angle = axis_positions[i].angle + 180.0;
|
2022-12-31 12:19:32 -07:00
|
|
|
SDL_FRect dst;
|
|
|
|
dst.x = (float)axis_positions[i].x;
|
|
|
|
dst.y = (float)axis_positions[i].y;
|
|
|
|
dst.w = (float)AXIS_SIZE;
|
|
|
|
dst.h = (float)AXIS_SIZE;
|
2022-12-27 07:21:13 -07:00
|
|
|
SDL_RenderTextureRotated(screen, axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
2020-11-07 03:22:15 -07:00
|
|
|
}
|
2020-04-18 22:41:37 -06:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:07:54 -07:00
|
|
|
/* Update LED based on left thumbstick position */
|
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
Sint16 x = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTX);
|
|
|
|
Sint16 y = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTY);
|
2020-11-05 12:07:54 -07:00
|
|
|
|
2020-11-16 11:39:44 -07:00
|
|
|
if (!set_LED) {
|
|
|
|
set_LED = (x < -8000 || x > 8000 || y > 8000);
|
|
|
|
}
|
|
|
|
if (set_LED) {
|
|
|
|
Uint8 r, g, b;
|
|
|
|
|
|
|
|
if (x < 0) {
|
2022-12-01 14:07:03 -07:00
|
|
|
r = (Uint8)(((~x) * 255) / 32767);
|
2020-11-16 11:39:44 -07:00
|
|
|
b = 0;
|
|
|
|
} else {
|
|
|
|
r = 0;
|
2022-11-30 13:51:59 -07:00
|
|
|
b = (Uint8)(((int)(x)*255) / 32767);
|
2020-11-16 11:39:44 -07:00
|
|
|
}
|
|
|
|
if (y > 0) {
|
2022-11-30 13:51:59 -07:00
|
|
|
g = (Uint8)(((int)(y)*255) / 32767);
|
2020-11-16 11:39:44 -07:00
|
|
|
} else {
|
|
|
|
g = 0;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_SetGamepadLED(gamepad, r, g, b);
|
2020-11-16 11:39:44 -07:00
|
|
|
}
|
2020-11-05 12:07:54 -07:00
|
|
|
}
|
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
if (trigger_effect == 0) {
|
|
|
|
/* Update rumble based on trigger state */
|
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
Sint16 left = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER);
|
|
|
|
Sint16 right = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
|
2021-07-08 14:22:41 -06:00
|
|
|
Uint16 low_frequency_rumble = ConvertAxisToRumble(left);
|
|
|
|
Uint16 high_frequency_rumble = ConvertAxisToRumble(right);
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_RumbleGamepad(gamepad, low_frequency_rumble, high_frequency_rumble, 250);
|
2021-07-08 14:22:41 -06:00
|
|
|
}
|
2020-11-11 19:57:37 -07:00
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
/* Update trigger rumble based on thumbstick state */
|
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
Sint16 left = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTY);
|
|
|
|
Sint16 right = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY);
|
2021-07-08 14:22:41 -06:00
|
|
|
Uint16 left_rumble = ConvertAxisToRumble(~left);
|
|
|
|
Uint16 right_rumble = ConvertAxisToRumble(~right);
|
2020-11-11 19:57:37 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_RumbleGamepadTriggers(gamepad, left_rumble, right_rumble, 250);
|
2021-07-08 14:22:41 -06:00
|
|
|
}
|
2020-11-11 19:57:37 -07:00
|
|
|
}
|
2020-02-07 12:45:32 -07:00
|
|
|
}
|
2022-04-29 21:57:00 -06:00
|
|
|
SDL_Delay(16);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_RenderPresent(screen);
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
int i;
|
2022-12-27 10:46:24 -07:00
|
|
|
int gamepad_index = 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-11-06 16:54:18 -07:00
|
|
|
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
2020-11-21 19:01:23 -07:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
|
2020-12-22 21:53:27 -07:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
|
2022-11-10 20:16:53 -07:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_STEAM, "1");
|
2022-02-15 14:07:51 -07:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1");
|
2020-12-04 16:47:28 -07:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
|
2021-11-27 12:01:18 -07:00
|
|
|
SDL_SetHint(SDL_HINT_LINUX_JOYSTICK_DEADZONES, "1");
|
2020-11-06 16:54:18 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Enable standard application logging */
|
2015-11-25 13:39:28 -07:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* Initialize SDL (Note: video is required to start event loop) */
|
2022-12-27 10:46:24 -07:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD) < 0) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
2021-11-23 04:22:02 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_AddGamepadMappingsFromFile("gamecontrollerdb.txt");
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2016-11-29 07:36:57 -07:00
|
|
|
/* Print information about the mappings */
|
2020-04-18 22:41:37 -06:00
|
|
|
if (argv[1] && SDL_strcmp(argv[1], "--mappings") == 0) {
|
2016-12-09 05:17:10 -07:00
|
|
|
SDL_Log("Supported mappings:\n");
|
2022-12-27 10:46:24 -07:00
|
|
|
for (i = 0; i < SDL_GetNumGamepadMappings(); ++i) {
|
|
|
|
char *mapping = SDL_GetGamepadMappingForIndex(i);
|
2016-12-09 05:17:10 -07:00
|
|
|
if (mapping) {
|
|
|
|
SDL_Log("\t%s\n", mapping);
|
|
|
|
SDL_free(mapping);
|
|
|
|
}
|
2016-11-29 07:36:57 -07:00
|
|
|
}
|
2016-12-09 05:17:10 -07:00
|
|
|
SDL_Log("\n");
|
2016-11-29 07:36:57 -07:00
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* Create a window to display gamepad state */
|
2023-03-05 15:44:38 -07:00
|
|
|
window = SDL_CreateWindow("Gamepad Test", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
2020-04-18 22:41:37 -06:00
|
|
|
if (window == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
|
|
|
return 2;
|
|
|
|
}
|
2015-11-14 10:35:45 -07:00
|
|
|
|
2022-12-13 21:26:49 -07:00
|
|
|
screen = SDL_CreateRenderer(window, NULL, 0);
|
2020-04-18 22:41:37 -06:00
|
|
|
if (screen == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
return 2;
|
|
|
|
}
|
2015-11-14 10:35:45 -07:00
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(screen);
|
|
|
|
SDL_RenderPresent(screen);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
/* scale for platforms that don't give you the window size you asked for. */
|
2023-02-03 13:25:46 -07:00
|
|
|
SDL_SetRenderLogicalPresentation(screen, SCREEN_WIDTH, SCREEN_HEIGHT,
|
|
|
|
SDL_LOGICAL_PRESENTATION_LETTERBOX,
|
2023-02-03 14:23:02 -07:00
|
|
|
SDL_SCALEMODE_LINEAR);
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
background_front = LoadTexture(screen, "gamepadmap.bmp", SDL_FALSE, NULL, NULL);
|
|
|
|
background_back = LoadTexture(screen, "gamepadmap_back.bmp", SDL_FALSE, NULL, NULL);
|
2022-10-05 15:56:27 -06:00
|
|
|
button_texture = LoadTexture(screen, "button.bmp", SDL_TRUE, NULL, NULL);
|
|
|
|
axis_texture = LoadTexture(screen, "axis.bmp", SDL_TRUE, NULL, NULL);
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
if (background_front == NULL || background_back == NULL || button_texture == NULL || axis_texture == NULL) {
|
2020-04-18 22:41:37 -06:00
|
|
|
SDL_DestroyRenderer(screen);
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
return 2;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-10-05 15:56:27 -06:00
|
|
|
SDL_SetTextureColorMod(button_texture, 10, 255, 21);
|
|
|
|
SDL_SetTextureColorMod(axis_texture, 10, 255, 21);
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2023-02-23 12:58:14 -07:00
|
|
|
/* Process the initial gamepad list */
|
|
|
|
loop(NULL);
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2022-05-15 21:01:12 -06:00
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (SDL_strcmp(argv[i], "--virtual") == 0) {
|
2022-12-27 10:46:24 -07:00
|
|
|
OpenVirtualGamepad();
|
2022-05-15 21:01:12 -06:00
|
|
|
}
|
|
|
|
if (argv[i] && *argv[i] != '-') {
|
2022-12-27 10:46:24 -07:00
|
|
|
gamepad_index = SDL_atoi(argv[i]);
|
2022-05-15 21:01:12 -06:00
|
|
|
break;
|
|
|
|
}
|
2020-11-27 19:57:38 -07:00
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
if (gamepad_index < num_gamepads) {
|
|
|
|
gamepad = gamepads[gamepad_index];
|
2020-11-27 19:57:38 -07:00
|
|
|
} else {
|
2022-12-27 10:46:24 -07:00
|
|
|
gamepad = NULL;
|
2020-11-23 15:28:30 -07:00
|
|
|
}
|
2020-11-17 11:30:20 -07:00
|
|
|
UpdateWindowTitle();
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* Loop, getting gamepad events! */
|
2020-04-18 22:41:37 -06:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop_arg(loop, NULL, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop(NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2021-07-08 14:22:41 -06:00
|
|
|
/* Reset trigger state */
|
|
|
|
if (trigger_effect != 0) {
|
|
|
|
trigger_effect = -1;
|
|
|
|
CyclePS5TriggerEffect();
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
CloseVirtualGamepad();
|
2020-04-18 22:41:37 -06:00
|
|
|
SDL_DestroyRenderer(screen);
|
|
|
|
SDL_DestroyWindow(window);
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
return 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|