forked from Green-Sky/tomato
Green Sky
644725478f
399bc709b build-scripts.pl: Added add-source-to-projects.pl ac6827187 Visual-WinRT: dos2unix the project files to match other Visual Studio targets. 34719cba9 Fixed crash in hid_init() if the HIDDeviceManager isn't available 2e92e94eb Make sure we update device->sample_frames in SDL_AudioDeviceFormatChangedAlreadyLocked() 9964e5c5b wayland: Don't retrieve the drag offer strings with every pointer motion event bac7eeaaa Added missing include a541e2ac1 audio: Change a few SDL_memcpy calls to SDL_copyp. 54125c140 audio: Only update bound audiostreams' formats when necessary. e0b0f9a36 testaudio: Fix mouseover testing. 2f3deec24 wayland: Don't process drag & drop events from surfaces not owned by SDL 42bdced05 events: Log file drop position events and print the pointer coordinates c10d93d3a wayland: Replace magic constant with define 500852153 emscripten: Restore compatibility with existing emsdk releases. 953b55dd6 Use EM_ASM_PTR when the return value is a pointer a4541a255 audio: SDL_GetAudioStreamQueued now returns bytes, not frames. 703aefbce Sync SDL3 wiki -> header 99421b64d linux: Add portal drag and drop 952c5059b Remove stray  eebd5d18a linux: Handle upower's UP_DEVICE_STATE_PENDING_CHARGE, PENDING_DISCHARGE f8fdb20d8 audio: Destroy all existing SDL_AudioStreams on shutdown. 62d445997 audio: Removed declarations of functions that don't exist anymore. b656720bc loopwave: Use SDL_GetAudioStreamQueued() for more accurate results. 34b931f7e audio: Added SDL_GetAudioStreamQueued 23206b9e3 audio: Added SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED c7e6d7a1f audio: Changed debug logging output. 87ec6acf2 audio: Added a FIXME ac88ffb7e audio: don't allocate buffer in SDL_SetAudioPostmixCallback for NULL callback. 2a950f6ae audio: Replace some SDL_memcpy calls with SDL_copyp. 0dc0434a3 audio: Fixed race condition in subsystem shutdown. 23f60203a audio: precalculate if we can use simple copies instead of the full mixer. 36b0f1141 audio: Optimize setting device formats during audio thread iteration. 4c3e84897 testspriteminimal: make standalone by embedding icon.bmp 2a01f9dcb tests: plug leaks when running with --trackmem f42bbeca2 SDL_test: track stack frames of allocations on Windows 12c0be028 SDL_test: clear text cache on exit event b4bfb1831 SDL_test: free state before logging allocations 248b1edd3 SDL_test: destroy windows in SDL_CommonQuit 98da2dd30 SDL_test: don't warn about expected allocations when running with --trackmem 6a381567b Support audio rate conversion up to 384KHz b2b548a1f Don't hang if IAudioRenderClient_GetBuffer() fails indefinitely a3a5e1728 Fixed build warning '=': conversion from 'Uint32' to 'Uint16', possible loss of data 6d3e21c27 Fixed android build warnings fca2f5318 Fixed warning: this function declaration is not a prototype a72dfa6a5 Fixed sensor timestamp units for third-party PS5 controllers f6756047a Fixed error: array subscript 2 is above array bounds of ‘const Uint8[2]’ 7059a55cc Fixed sensor timestamp calculation for third-party PS5 controllers c0443e5d1 Fixed crash in SDL_IMMDevice_FindByDevID() fde8499f6 Use around 20ms for the audio buffer size e5739d7d1 video: Remove SDL_GetFocusWindow() 39c2f9737 Fix NULL dereference in SDL_OpenAudio 9a23d0e3f Added new audio files to the Xcode project a62e62f97 Refactored SDL_audiocvt.c 31229fd47 include: Added a note about SDL's iOS app delegate functions. 65aaf3a9a x11: Always update clipboard owner f622f21e6 Fixed build 5774c9638 Prefer hidraw over libusb when libusb whitelisting is not enabled 9301f7ace hidapi/libusb: only enumerate each interface once 859dc14ad Replaced SDL_GetGamepadBindForAxis() and SDL_GetGamepadBindForButton() with SDL_GetGamepadBindings() 9e50048ab Revert "Removed SDL_GamepadBinding from the API" 9f17d1a9d Don't reference the same function in "see also" 86505ea63 fix SDL_AudioStreamCallback documentation d885d5c31 Sync SDL3 wiki -> header 2f43f7bc5 audio: Allow querying of device buffer size. cf9572113 audio: Added a hint to let apps force device buffer size. 47d8c77c6 audio: Choose better default sample frame counts. 8b26e95f9 audio: Change SDL_AudioStreamCallback 9da34e8fb docs: Updated README-emscripten.md. fd1c54a00 detect fanatec steering wheels cb4414608 docs: Whoops, this got added by the wiki bridge by accident! cd633b9a8 Renamed SDL_IsAudioDevicePaused() to SDL_AudioDevicePaused() c6cad07fa Sync SDL3 wiki -> header a6e52f9e4 Sync SDL3 wiki -> header 2de2e9d03 Fix flickering of window when using desktop-fullscreen and borderless window on multiple monitors on Linux. Closes #8186. 723835d16 Windows: fix for client rect resizing larger each time we came from exclusive fullscreen -> windowed on a monitor with HiDPI set. The problem was we were using the monitor DPI rather than the window DPI so AdjustWindowRectExForDpi was giving us an incorrect size which would be too large for the client rect. Closes #8237. ce27363df wikiheaders: Sort undocumented functions. e22282b09 Added README about transparent windows in Win32 1d1c6e630 Turn off COREAUDIO debug logging by default 52efefca0 wayland: Fix drag offer leak 3a992af44 audio: Added a postmix callback to logical devices. 7207bdce5 render: Enable clipping for zero-sized rectangles 22d81fb3e cmake: use MSVC_RUNTIME_LIBRARY to force MT a2e17852d cmake: make sure SDL_GetPrefPath is run before testfilesystem 2fb266e0a ci: run tests in parallel ad1313e75 testaudio: Patched to compile. 5747ddc01 testaudio: Clean up some messy memory management. fafbea1ce audio: Move internal float32 mixing to a simplified function 116b0ec97 include: minor tweak to audio API documentation fb1377035 include: Replaced old Bugzilla URL. 38c8fc05c audio: Remove ChooseMixStrategy. b00cbd76a wikiheaders.pl: create Unsupported.md file with list of functions undocumented in either the headers or the wiki 37e1fc3b5 wayland: Ensure that the toplevel window is recreated when switching decoration modes f2ca9a615 Added SDL_AUDIO_FRAMESIZE 53122593f Added SDL_AUDIO_BYTESIZE 544351c98 Sync SDL3 wiki -> header 2e7d2b94e Clarify that SDL_BlitSurface() ignores the width and height in dstrect a2c1984d3 Detect Simagic wheel bases as wheels (#8198) 1d8dfbb22 avoid type redefinition errors after PR/8181 266b91d2f Detect Logitech G923 Playstation as wheel G923 have two different versions - Xbox version is already present in the wheel list, but not the PS version. cde67ea49 Detect Logitech PRO Racing Wheel for Xbox (PC mode) as wheel Logitech PRO Racing Wheel have two different versions - for Playstation and Xbox. Vendor + Product ID for Playstation version already present in SDL sources, but not an Xbox version 3a932141e Restore audio format binary compatibility with SDL 2.0 e85206ffd wikiheaders.pl: add --rev= option to pass revision string 233789b0d Audio types have the same naming convention as other SDL endian types, e.g. [S|U][BITS][LE|BE] 36b5f3e35 Sync SDL3 wiki -> header 0e552761b Renamed AudioStreamSpeed to AudioStreamFrequencyRatio 47bcb078f Fixed some incorrect SDL_AUDIO_F32 uses 2833f2e7b Fixed OOB access in audio_convertAccuracy test 8387fae69 Sync SDL3 wiki -> header 832181345 docs: Add note about Wayland application icons 825d34475 Make sure that the same timestamp is used for all PS5 events from the same packet 9c1430324 Removed SDL_dataqueue 28b28bd8f Added audio_formatChange test a59152688 Try and avoid overflow when handling very large audio streams 5394a805f Improved testaudiostreamdynamicresample e55844274 Added SDL_(Get|Set)AudioStreamSpeed 43c3c5736 Track the formats of data in an SDL_AudioStream 337fed3df Tweaked ResampleFrame_SSE Use _mm_unpack(lo|hi)_ps instead of _mm_shuffle_ps fd7cd91dc audio: Mix multiple streams in float32 to prevent clipping. 9097573e3 audio: Choose a mixing strategy on each iteration. bbe2e012a Don't provide the SDL3 header path c17a35f09 Fixed typo 4f72255eb Fixed README.md link e0ab59754 Simplified SDL_main.h migration notes d44bde61e Added SDL migration information to the top level README.md 6ff31e10c metal: Add hint to select low power device instead of the default one (#8182) 8a8aed477 Make sure that we process touch events that position the mouse f84c87f20 Sync SDL3 wiki -> header a7eea9997 macOS: Don't raise the parent top-level window when raising a child window, only raise the child window to the top of the parent a5e721479 Add SDL_WINDOW_NOT_FOCUSABLE flag to set that the window should not be able to gain key focus b385dc3b6 n3dsaudio: Patched to compile. 4e0c7c91f audio: PlayDevice() should return an error code. a94d724f1 wayland: add SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL da5d93d3d wayland: don't define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_* macro's f002f7d12 ci: build emscripten with Debug buid type 3699b12ed audio: Fixed some "is_*" variables to be cleaner and/or more specific. 2471d8cc2 audio: Fixed logic error in SDL_OpenAudioDeviceStream. 1b03a2430 testsurround: fix order of arguments of callback 82db2b58f Renamed audio stream callback and moved the userdata parameter first 5bdad5210 Sync SDL3 wiki -> header 58c859f64 audio: Rename SDL_GetAudioStreamBinding to SDL_GetAudioStreamDevice. efd2023a7 audio: Fixed documentation. 1e775e0ee audio: Replace SDL_CreateAndBindAudioStream with SDL_OpenAudioDeviceStream. bd088c2f9 Revert "Clarify whether an audio function expects a physical or logical device ID" 82e481b52 Added --randmem test parameter ea68bb802 Add some additional checks to audio_convertAudio f8286df16 Fixed ResampleFrame_SSE doing unnecessary work b1d63be53 Fixed audio_resampleLoss test c191d6c30 Better Win32 transparent window support 923d612ca hidapi: sync macOS code with mainstream. 363f4fa9c avoid type redefinition errors after commit ee806597b9. 615824a80 Updated documentation now that SDL_GetAudioDevices() has been split into separate functions for output and capture devices 506a133d8 Clarify whether an audio function expects a physical or logical device ID 3b1d1e4e3 hidapi: sync the hidraw changes with mainstream f617918e0 cmake: check linkage to libusb too, instead of libusb.h presence only. 041dbd6b5 Fixed GetResamplerAvailableOutputFrames Non-euclidean division is a pain b49d0a607 x11: Avoid including full Vulkan headers. 4d2f9f3a3 yuv_rgb: Comment out unused code. 3c3486e2a wayland: Don't include full Vulkan headers when not necessary. f066bbe98 x11: Don't include system headers twice. d86d02bbb updated dynapi after SDL_GDKGetDefaultUser addition 4355f9cec Fixed warning C4389: '!=': signed/unsigned mismatch 5755de07a Fixed build warnings 0f80d47bb Fixed thread-safety warning ee806597b Removed SDL_vulkan_internal.h from SDL_sysvideo.h 34860b932 Fixed testautomation --filter pixels_allocFreeFormat 6f8a6a31c gdk: GetBasePath should be a UTF8 version of Win32 GetBasePath e30e5c77e Sync SDL3 wiki -> header c0cd8c814 gdk: Add SDL_GDKGetDefaultUser, SDL_GetPrefPath implementation 106abce69 Refactored GetAudioStreamDataInternal buffer handling The final conversion step should now always go straight into the output buffer. e44f54ec5 Avoid using hex-floats 5b696996c Added ResampleFrame_SSE 958b3cfae Tweaked and enabled audio_convertAudio test 7dbb9b65b audio_convertAccuracy: Shuffle the data in case of a bad SIMD implementation f6a4080ff audio_resampleLoss: Add support for multiple channels 4f894e748 audio_resampleLoss: SDL_GetAudioStreamData now returns the correct length ab83f75bb Make sure GetAudioStreamDataInternal is called with a valid length 6a73f74b6 Rebuild full ResamplerFilter (left wing + right wing) at runtime 0c15ce006 Add a missing int cast b74ee86b1 Optimized ResampleAudio, with special cases for 1 and 2 channels This would also benefit from some SIMD, since it's just a bunch of multiply-adds fba6e1e3d Removed ResamplerFilterDifference It takes 1 extra multiply to calculate the correct interpolation, but I think the improvement in cache locality (and binary size) outweighs that. 9f7a22fa4 Removed 64-bit handling from AudioConvertByteswap 1f5327a9f Removed future_buffer, left_padding, and right_padding from SDL_AudioStream 71ad52d6d Lowered SDL_GetAudioStreamData to 32 KB No particular reason for this number, but 1 MB was a bit silly 69aec8c91 Fixed the report format for the Razer Wolverine V2 Pro 7c2669c9d Accept key events from any source 1e9d31448 Updated to Android minSdkVersion 19 and targetSdkVersion 34 8924d0d92 Added missing function prototype for SDL_WriteS64BE() 845f3c745 Fixed mismatch between stdlib calloc() and SDL free() fb7921173 emscriptenaudio: Fire the capture silence_callback at an interval. 5191b2054 emscriptenaudio: Don't bother undefining things about to be unreachable. fd75a4ca0 emscriptenaudio: Deal with blocked audio devices better. 981b8a337 emscriptenaudio: Remove unnecessary functions. c7588e426 Transparent window for Win32 + OpenGL (#8143) f9581178d cmake: fixed a typo. e6c878824 Fixed ResampleAudio interpolation factor calculation 498363863 Misc audio tweaks/cleanup 72d9d53de Invert the inner ResampleAudio loops to avoid doing unnecessary work 88123a510 The history buffer should always have the maximum possible padding frames 96e47f165 Clamp results of GetResampler(AvailableOutput|NeededInput)Frames d2b9c8b80 Fixed maths in testaudiostreamdynamicresample (and just show the actual scale) 14e38b17d Removed assertions from inner ResampleAudio loop 9d413dfdc The history buffer doesn't need to be so large 2788e848f Allow resampling less than 1 frame of input 383084e0a Pre-calculate resampling rate, and use it instead of .freq in most places 40a6a445c Update resample_offset inside ResampleAudio 47fea7f06 Used fixed-point arithmetic in ResampleAudio 7bb4e806e Clear resample_offset in SDL_ClearAudioStream, not SetAudioStreamFormat Not entirely sure if ClearAudioStream is the right place, but SetAudioStreamFormat was the wrong place b9541b9ea Improved ResampleAudio * filterindex2 was off-by-one * Generate ResamplerFilter using doubles * Transpose ResamplerFilter to improve access patterns cdaa19869 Track offset within the current sample when resampling d60ebb06d mouse: Ensure that the dummy default cursor is removed from the cursor list e58c2731f mouse: Free the default cursor when destroyed 789ce17e1 audio: Don't resample in chunks for now. cbab33482 audio: Don't call SDL_AudioStream callbacks for empty data sets. 3e1ae0c86 Clearified the libusb whitelist default logic f4520821e Removed some unnecessary integer casts 0989b7e86 Avoid using designated initializers c6c1e673c Optimized SDL_Convert_*_to_*_Scalar f97b920b3 Optimized SDL_Convert_*_to_*_SSE2 Some of the SDL_Convert_F32_to_*_SSE2 do not explicitly clamp the input, but instead rely on saturating casts. Inputs very far outside the valid [-1.0, 1.0] range may produce an incorrect result, but I believe that is an acceptable trade-off. 300d1ec3e Added audio_convertAccuracy test 32cecc2ea Fixed assertion in audio_convertAudio 33f11e21e Removed assertions in AudioConvert(To|From)Float c2f388fd8 cmake: add SDL_HIDAPI_LIBUSB_SHARED option + test on ci 371cc2d17 wayland: Remove unnecessary flag and state settings fe85e6e75 cocoa: Send a maximized event instead of restored if a deminiaturized window is zoomed ddddcb78c cocoa: Use the close method to hide a miniaturized window be8c42cfd Clarify that a window being 'hidden' means that it is unmapped/ordered out a44338cbc Fix typo in SDL_audiocvt.c f464eb2c5 SDL_hidapi.c: change 'use_libusb_whitelist_default' into a macro. 6607a3cfa Disable cache in python http server 181d5d285 hidapi: Enable libusb support by default. f0f15e365 hidapi: Use a whitelist for libusb when other backends are available c3f7a7dc4 Convert audio using SDL_AUDIO_F32SYS format instead of SDL_AUDIO_F32 796713b9d xxd.py: always write \n line endings 723bcd0a8 SDL_TriggerBreakppoint for riscv arch (both 32/64) version. git-subtree-dir: external/sdl/SDL git-subtree-split: 399bc709b7485bab57880f8261f826f29dc0d7b2
2030 lines
72 KiB
C
2030 lines
72 KiB
C
/*
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
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.
|
|
*/
|
|
|
|
/* Simple program to test the SDL controller routines */
|
|
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_main.h>
|
|
#include <SDL3/SDL_test.h>
|
|
#include <SDL3/SDL_test_font.h>
|
|
|
|
#include "gamepadutils.h"
|
|
#include "testutils.h"
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
#include <emscripten/emscripten.h>
|
|
#endif
|
|
|
|
#if 0
|
|
#define DEBUG_AXIS_MAPPING
|
|
#endif
|
|
|
|
#define TITLE_HEIGHT 48
|
|
#define PANEL_SPACING 25
|
|
#define PANEL_WIDTH 250
|
|
#define MINIMUM_BUTTON_WIDTH 96
|
|
#define BUTTON_MARGIN 16
|
|
#define BUTTON_PADDING 12
|
|
#define GAMEPAD_WIDTH 512
|
|
#define GAMEPAD_HEIGHT 480
|
|
|
|
#define SCREEN_WIDTH (PANEL_WIDTH + PANEL_SPACING + GAMEPAD_WIDTH + PANEL_SPACING + PANEL_WIDTH)
|
|
#define SCREEN_HEIGHT (TITLE_HEIGHT + GAMEPAD_HEIGHT)
|
|
|
|
typedef struct
|
|
{
|
|
SDL_bool m_bMoving;
|
|
int m_nLastValue;
|
|
int m_nStartingValue;
|
|
int m_nFarthestValue;
|
|
} AxisState;
|
|
|
|
typedef struct
|
|
{
|
|
SDL_JoystickID id;
|
|
|
|
SDL_Joystick *joystick;
|
|
int num_axes;
|
|
AxisState *axis_state;
|
|
|
|
SDL_Gamepad *gamepad;
|
|
char *mapping;
|
|
SDL_bool has_bindings;
|
|
|
|
int trigger_effect;
|
|
} Controller;
|
|
|
|
static SDL_Window *window = NULL;
|
|
static SDL_Renderer *screen = NULL;
|
|
static ControllerDisplayMode display_mode = CONTROLLER_MODE_TESTING;
|
|
static GamepadImage *image = NULL;
|
|
static GamepadDisplay *gamepad_elements = NULL;
|
|
static GamepadTypeDisplay *gamepad_type = NULL;
|
|
static JoystickDisplay *joystick_elements = NULL;
|
|
static GamepadButton *setup_mapping_button = NULL;
|
|
static GamepadButton *done_mapping_button = NULL;
|
|
static GamepadButton *cancel_button = NULL;
|
|
static GamepadButton *clear_button = NULL;
|
|
static GamepadButton *copy_button = NULL;
|
|
static GamepadButton *paste_button = NULL;
|
|
static char *backup_mapping = NULL;
|
|
static SDL_bool done = SDL_FALSE;
|
|
static SDL_bool set_LED = SDL_FALSE;
|
|
static int num_controllers = 0;
|
|
static Controller *controllers;
|
|
static Controller *controller;
|
|
static SDL_JoystickID mapping_controller = 0;
|
|
static int binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
|
|
static int last_binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
|
|
static SDL_bool binding_flow = SDL_FALSE;
|
|
static Uint64 binding_advance_time = 0;
|
|
static SDL_FRect title_area;
|
|
static SDL_bool title_highlighted;
|
|
static SDL_bool title_pressed;
|
|
static SDL_FRect type_area;
|
|
static SDL_bool type_highlighted;
|
|
static SDL_bool type_pressed;
|
|
static char *controller_name;
|
|
static SDL_Joystick *virtual_joystick = NULL;
|
|
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
|
|
static float virtual_axis_start_x;
|
|
static float virtual_axis_start_y;
|
|
static SDL_GamepadButton virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
|
|
|
static int s_arrBindingOrder[] = {
|
|
/* Standard sequence */
|
|
SDL_GAMEPAD_BUTTON_A,
|
|
SDL_GAMEPAD_BUTTON_B,
|
|
SDL_GAMEPAD_BUTTON_X,
|
|
SDL_GAMEPAD_BUTTON_Y,
|
|
SDL_GAMEPAD_BUTTON_DPAD_LEFT,
|
|
SDL_GAMEPAD_BUTTON_DPAD_RIGHT,
|
|
SDL_GAMEPAD_BUTTON_DPAD_UP,
|
|
SDL_GAMEPAD_BUTTON_DPAD_DOWN,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE,
|
|
SDL_GAMEPAD_BUTTON_LEFT_STICK,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE,
|
|
SDL_GAMEPAD_BUTTON_RIGHT_STICK,
|
|
SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER,
|
|
SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
|
|
SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER,
|
|
SDL_GAMEPAD_BUTTON_BACK,
|
|
SDL_GAMEPAD_BUTTON_START,
|
|
SDL_GAMEPAD_BUTTON_GUIDE,
|
|
SDL_GAMEPAD_BUTTON_MISC1,
|
|
SDL_GAMEPAD_ELEMENT_INVALID,
|
|
|
|
/* Paddle sequence */
|
|
SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1,
|
|
SDL_GAMEPAD_BUTTON_LEFT_PADDLE1,
|
|
SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2,
|
|
SDL_GAMEPAD_BUTTON_LEFT_PADDLE2,
|
|
SDL_GAMEPAD_ELEMENT_INVALID,
|
|
};
|
|
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
/* PS5 trigger effect documentation:
|
|
https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes
|
|
*/
|
|
typedef struct
|
|
{
|
|
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 */
|
|
} DS5EffectsState_t;
|
|
|
|
static void CyclePS5TriggerEffect(Controller *device)
|
|
{
|
|
DS5EffectsState_t state;
|
|
|
|
Uint8 effects[3][11] = {
|
|
/* 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 },
|
|
};
|
|
|
|
device->trigger_effect = (device->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[device->trigger_effect], sizeof(effects[0]));
|
|
SDL_memcpy(state.rgucLeftTriggerEffect, effects[device->trigger_effect], sizeof(effects[0]));
|
|
SDL_SendGamepadEffect(device->gamepad, &state, sizeof(state));
|
|
}
|
|
|
|
static void ClearButtonHighlights(void)
|
|
{
|
|
title_highlighted = SDL_FALSE;
|
|
title_pressed = SDL_FALSE;
|
|
|
|
type_highlighted = SDL_FALSE;
|
|
type_pressed = SDL_FALSE;
|
|
|
|
ClearGamepadImage(image);
|
|
SetGamepadDisplayHighlight(gamepad_elements, SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
SetGamepadTypeDisplayHighlight(gamepad_type, SDL_GAMEPAD_TYPE_UNSELECTED, SDL_FALSE);
|
|
SetGamepadButtonHighlight(setup_mapping_button, SDL_FALSE, SDL_FALSE);
|
|
SetGamepadButtonHighlight(done_mapping_button, SDL_FALSE, SDL_FALSE);
|
|
SetGamepadButtonHighlight(cancel_button, SDL_FALSE, SDL_FALSE);
|
|
SetGamepadButtonHighlight(clear_button, SDL_FALSE, SDL_FALSE);
|
|
SetGamepadButtonHighlight(copy_button, SDL_FALSE, SDL_FALSE);
|
|
SetGamepadButtonHighlight(paste_button, SDL_FALSE, SDL_FALSE);
|
|
}
|
|
|
|
static void UpdateButtonHighlights(float x, float y, SDL_bool button_down)
|
|
{
|
|
ClearButtonHighlights();
|
|
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
SetGamepadButtonHighlight(setup_mapping_button, GamepadButtonContains(setup_mapping_button, x, y), button_down);
|
|
} else if (display_mode == CONTROLLER_MODE_BINDING) {
|
|
SDL_FPoint point;
|
|
int gamepad_highlight_element = SDL_GAMEPAD_ELEMENT_INVALID;
|
|
char *joystick_highlight_element;
|
|
|
|
point.x = x;
|
|
point.y = y;
|
|
if (SDL_PointInRectFloat(&point, &title_area)) {
|
|
title_highlighted = SDL_TRUE;
|
|
title_pressed = button_down;
|
|
} else {
|
|
title_highlighted = SDL_FALSE;
|
|
title_pressed = SDL_FALSE;
|
|
}
|
|
|
|
if (SDL_PointInRectFloat(&point, &type_area)) {
|
|
type_highlighted = SDL_TRUE;
|
|
type_pressed = button_down;
|
|
} else {
|
|
type_highlighted = SDL_FALSE;
|
|
type_pressed = SDL_FALSE;
|
|
}
|
|
|
|
if (controller->joystick != virtual_joystick) {
|
|
gamepad_highlight_element = GetGamepadImageElementAt(image, x, y);
|
|
}
|
|
if (gamepad_highlight_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
gamepad_highlight_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, x, y);
|
|
}
|
|
SetGamepadDisplayHighlight(gamepad_elements, gamepad_highlight_element, button_down);
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
|
|
int gamepad_highlight_type = GetGamepadTypeDisplayAt(gamepad_type, x, y);
|
|
SetGamepadTypeDisplayHighlight(gamepad_type, gamepad_highlight_type, button_down);
|
|
}
|
|
|
|
joystick_highlight_element = GetJoystickDisplayElementAt(joystick_elements, controller->joystick, x, y);
|
|
SetJoystickDisplayHighlight(joystick_elements, joystick_highlight_element, button_down);
|
|
SDL_free(joystick_highlight_element);
|
|
|
|
SetGamepadButtonHighlight(done_mapping_button, GamepadButtonContains(done_mapping_button, x, y), button_down);
|
|
SetGamepadButtonHighlight(cancel_button, GamepadButtonContains(cancel_button, x, y), button_down);
|
|
SetGamepadButtonHighlight(clear_button, GamepadButtonContains(clear_button, x, y), button_down);
|
|
SetGamepadButtonHighlight(copy_button, GamepadButtonContains(copy_button, x, y), button_down);
|
|
SetGamepadButtonHighlight(paste_button, GamepadButtonContains(paste_button, x, y), button_down);
|
|
}
|
|
}
|
|
|
|
static int StandardizeAxisValue(int nValue)
|
|
{
|
|
if (nValue > SDL_JOYSTICK_AXIS_MAX / 2) {
|
|
return SDL_JOYSTICK_AXIS_MAX;
|
|
} else if (nValue < SDL_JOYSTICK_AXIS_MIN / 2) {
|
|
return SDL_JOYSTICK_AXIS_MIN;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static void RefreshControllerName(void)
|
|
{
|
|
const char *name = NULL;
|
|
|
|
SDL_free(controller_name);
|
|
controller_name = NULL;
|
|
|
|
if (controller) {
|
|
if (controller->gamepad) {
|
|
name = SDL_GetGamepadName(controller->gamepad);
|
|
} else {
|
|
name = SDL_GetJoystickName(controller->joystick);
|
|
}
|
|
}
|
|
|
|
SDL_free(controller_name);
|
|
if (name) {
|
|
controller_name = SDL_strdup(name);
|
|
} else {
|
|
controller_name = SDL_strdup("");
|
|
}
|
|
}
|
|
|
|
static void SetAndFreeGamepadMapping(char *mapping)
|
|
{
|
|
SDL_SetGamepadMapping(controller->id, mapping);
|
|
SDL_free(mapping);
|
|
}
|
|
|
|
static void SetCurrentBindingElement(int element, SDL_bool flow)
|
|
{
|
|
int i;
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
RefreshControllerName();
|
|
}
|
|
|
|
if (element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
last_binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
|
|
} else {
|
|
last_binding_element = binding_element;
|
|
}
|
|
binding_element = element;
|
|
binding_flow = flow || (element == SDL_GAMEPAD_BUTTON_A);
|
|
binding_advance_time = 0;
|
|
|
|
for (i = 0; i < controller->num_axes; ++i) {
|
|
controller->axis_state[i].m_nFarthestValue = controller->axis_state[i].m_nStartingValue;
|
|
}
|
|
|
|
SetGamepadDisplaySelected(gamepad_elements, element);
|
|
}
|
|
|
|
static void SetNextBindingElement(void)
|
|
{
|
|
int i;
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < SDL_arraysize(s_arrBindingOrder); ++i) {
|
|
if (binding_element == s_arrBindingOrder[i]) {
|
|
SetCurrentBindingElement(s_arrBindingOrder[i + 1], SDL_TRUE);
|
|
return;
|
|
}
|
|
}
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
}
|
|
|
|
static void SetPrevBindingElement(void)
|
|
{
|
|
int i;
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
return;
|
|
}
|
|
|
|
for (i = 1; i < SDL_arraysize(s_arrBindingOrder); ++i) {
|
|
if (binding_element == s_arrBindingOrder[i]) {
|
|
SetCurrentBindingElement(s_arrBindingOrder[i - 1], SDL_TRUE);
|
|
return;
|
|
}
|
|
}
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
}
|
|
|
|
static void StopBinding(void)
|
|
{
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
}
|
|
|
|
typedef struct
|
|
{
|
|
int axis;
|
|
int direction;
|
|
} AxisInfo;
|
|
|
|
static SDL_bool ParseAxisInfo(const char *description, AxisInfo *info)
|
|
{
|
|
if (!description) {
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
if (*description == '-') {
|
|
info->direction = -1;
|
|
++description;
|
|
} else if (*description == '+') {
|
|
info->direction = 1;
|
|
++description;
|
|
} else {
|
|
info->direction = 0;
|
|
}
|
|
|
|
if (description[0] == 'a' && SDL_isdigit(description[1])) {
|
|
++description;
|
|
info->axis = SDL_atoi(description);
|
|
return SDL_TRUE;
|
|
}
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
static void CommitBindingElement(const char *binding, SDL_bool force)
|
|
{
|
|
char *mapping;
|
|
int direction = 1;
|
|
SDL_bool ignore_binding = SDL_FALSE;
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
return;
|
|
}
|
|
|
|
if (controller->mapping) {
|
|
mapping = SDL_strdup(controller->mapping);
|
|
} else {
|
|
mapping = NULL;
|
|
}
|
|
|
|
/* If the controller generates multiple events for a single element, pick the best one */
|
|
if (!force && binding_advance_time) {
|
|
char *current = GetElementBinding(mapping, binding_element);
|
|
SDL_bool native_button = (binding_element < SDL_GAMEPAD_BUTTON_MAX);
|
|
SDL_bool native_axis = (binding_element >= SDL_GAMEPAD_BUTTON_MAX &&
|
|
binding_element <= SDL_GAMEPAD_ELEMENT_AXIS_MAX);
|
|
SDL_bool native_trigger = (binding_element == SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER ||
|
|
binding_element == SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER);
|
|
SDL_bool native_dpad = (binding_element == SDL_GAMEPAD_BUTTON_DPAD_UP ||
|
|
binding_element == SDL_GAMEPAD_BUTTON_DPAD_DOWN ||
|
|
binding_element == SDL_GAMEPAD_BUTTON_DPAD_LEFT ||
|
|
binding_element == SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
|
|
|
|
if (native_button) {
|
|
SDL_bool current_button = (current && *current == 'b');
|
|
SDL_bool proposed_button = (binding && *binding == 'b');
|
|
if (current_button && !proposed_button) {
|
|
ignore_binding = SDL_TRUE;
|
|
}
|
|
/* Use the lower index button (we map from lower to higher button index) */
|
|
if (current_button && proposed_button && current[1] < binding[1]) {
|
|
ignore_binding = SDL_TRUE;
|
|
}
|
|
}
|
|
if (native_axis) {
|
|
AxisInfo current_axis_info;
|
|
AxisInfo proposed_axis_info;
|
|
SDL_bool current_axis = ParseAxisInfo(current, ¤t_axis_info);
|
|
SDL_bool proposed_axis = ParseAxisInfo(binding, &proposed_axis_info);
|
|
|
|
if (current_axis) {
|
|
/* Ignore this unless the proposed binding extends the existing axis */
|
|
ignore_binding = SDL_TRUE;
|
|
|
|
if (native_trigger &&
|
|
((*current == '-' && *binding == '+' &&
|
|
SDL_strcmp(current + 1, binding + 1) == 0) ||
|
|
(*current == '+' && *binding == '-' &&
|
|
SDL_strcmp(current + 1, binding + 1) == 0))) {
|
|
/* Merge two half axes into a whole axis for a trigger */
|
|
++binding;
|
|
ignore_binding = SDL_FALSE;
|
|
}
|
|
|
|
/* Use the lower index axis (we map from lower to higher axis index) */
|
|
if (proposed_axis && proposed_axis_info.axis < current_axis_info.axis) {
|
|
ignore_binding = SDL_FALSE;
|
|
}
|
|
}
|
|
}
|
|
if (native_dpad) {
|
|
SDL_bool current_hat = (current && *current == 'h');
|
|
SDL_bool proposed_hat = (binding && *binding == 'h');
|
|
if (current_hat && !proposed_hat) {
|
|
ignore_binding = SDL_TRUE;
|
|
}
|
|
/* Use the lower index hat (we map from lower to higher hat index) */
|
|
if (current_hat && proposed_hat && current[1] < binding[1]) {
|
|
ignore_binding = SDL_TRUE;
|
|
}
|
|
}
|
|
SDL_free(current);
|
|
}
|
|
|
|
if (!ignore_binding && binding_flow && !force) {
|
|
int existing = GetElementForBinding(mapping, binding);
|
|
if (existing != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
if (existing == SDL_GAMEPAD_BUTTON_A) {
|
|
if (binding_element == SDL_GAMEPAD_BUTTON_A) {
|
|
/* Just move on to the next one */
|
|
ignore_binding = SDL_TRUE;
|
|
SetNextBindingElement();
|
|
} else {
|
|
/* Clear the current binding and move to the next one */
|
|
binding = NULL;
|
|
direction = 1;
|
|
force = SDL_TRUE;
|
|
}
|
|
} else if (existing == SDL_GAMEPAD_BUTTON_B) {
|
|
if (binding_element != SDL_GAMEPAD_BUTTON_A &&
|
|
last_binding_element != SDL_GAMEPAD_BUTTON_A) {
|
|
/* Clear the current binding and move to the previous one */
|
|
binding = NULL;
|
|
direction = -1;
|
|
force = SDL_TRUE;
|
|
}
|
|
} else if (existing == binding_element) {
|
|
/* We're rebinding the same thing, just move to the next one */
|
|
ignore_binding = SDL_TRUE;
|
|
SetNextBindingElement();
|
|
} else if (binding_element != SDL_GAMEPAD_BUTTON_A &&
|
|
binding_element != SDL_GAMEPAD_BUTTON_B) {
|
|
ignore_binding = SDL_TRUE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ignore_binding) {
|
|
SDL_free(mapping);
|
|
return;
|
|
}
|
|
|
|
mapping = ClearMappingBinding(mapping, binding);
|
|
mapping = SetElementBinding(mapping, binding_element, binding);
|
|
SetAndFreeGamepadMapping(mapping);
|
|
|
|
if (force) {
|
|
if (binding_flow) {
|
|
if (direction > 0) {
|
|
SetNextBindingElement();
|
|
} else if (direction < 0) {
|
|
SetPrevBindingElement();
|
|
}
|
|
} else {
|
|
StopBinding();
|
|
}
|
|
} else {
|
|
/* Wait to see if any more bindings come in */
|
|
binding_advance_time = SDL_GetTicks() + 30;
|
|
}
|
|
}
|
|
|
|
static void ClearBinding(void)
|
|
{
|
|
CommitBindingElement(NULL, SDL_TRUE);
|
|
}
|
|
|
|
static void SetDisplayMode(ControllerDisplayMode mode)
|
|
{
|
|
float x, y;
|
|
Uint32 button_state;
|
|
|
|
if (mode == CONTROLLER_MODE_BINDING) {
|
|
/* Make a backup of the current mapping */
|
|
if (controller->mapping) {
|
|
backup_mapping = SDL_strdup(controller->mapping);
|
|
}
|
|
mapping_controller = controller->id;
|
|
if (MappingHasBindings(backup_mapping)) {
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
} else {
|
|
SetCurrentBindingElement(SDL_GAMEPAD_BUTTON_A, SDL_TRUE);
|
|
}
|
|
} else {
|
|
if (backup_mapping) {
|
|
SDL_free(backup_mapping);
|
|
backup_mapping = NULL;
|
|
}
|
|
mapping_controller = 0;
|
|
StopBinding();
|
|
}
|
|
|
|
display_mode = mode;
|
|
SetGamepadImageDisplayMode(image, mode);
|
|
SetGamepadDisplayDisplayMode(gamepad_elements, mode);
|
|
|
|
button_state = SDL_GetMouseState(&x, &y);
|
|
SDL_RenderCoordinatesFromWindow(screen, x, y, &x, &y);
|
|
UpdateButtonHighlights(x, y, button_state ? SDL_TRUE : SDL_FALSE);
|
|
}
|
|
|
|
static void CancelMapping(void)
|
|
{
|
|
SetAndFreeGamepadMapping(backup_mapping);
|
|
backup_mapping = NULL;
|
|
|
|
SetDisplayMode(CONTROLLER_MODE_TESTING);
|
|
}
|
|
|
|
static void ClearMapping(void)
|
|
{
|
|
SetAndFreeGamepadMapping(NULL);
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
|
|
}
|
|
|
|
static void CopyMapping(void)
|
|
{
|
|
if (controller && controller->mapping) {
|
|
SDL_SetClipboardText(controller->mapping);
|
|
}
|
|
}
|
|
|
|
static void PasteMapping(void)
|
|
{
|
|
if (controller) {
|
|
char *mapping = SDL_GetClipboardText();
|
|
if (MappingHasBindings(mapping)) {
|
|
StopBinding();
|
|
SetAndFreeGamepadMapping(mapping);
|
|
RefreshControllerName();
|
|
} else {
|
|
/* Not a valid mapping, ignore it */
|
|
SDL_free(mapping);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void CommitControllerName(void)
|
|
{
|
|
char *mapping = NULL;
|
|
|
|
if (controller->mapping) {
|
|
mapping = SDL_strdup(controller->mapping);
|
|
} else {
|
|
mapping = NULL;
|
|
}
|
|
mapping = SetMappingName(mapping, controller_name);
|
|
SetAndFreeGamepadMapping(mapping);
|
|
}
|
|
|
|
static void AddControllerNameText(const char *text)
|
|
{
|
|
size_t current_length = (controller_name ? SDL_strlen(controller_name) : 0);
|
|
size_t text_length = SDL_strlen(text);
|
|
size_t size = current_length + text_length + 1;
|
|
char *name = (char *)SDL_realloc(controller_name, size);
|
|
if (name) {
|
|
SDL_memcpy(&name[current_length], text, text_length + 1);
|
|
controller_name = name;
|
|
}
|
|
CommitControllerName();
|
|
}
|
|
|
|
static void BackspaceControllerName(void)
|
|
{
|
|
size_t length = (controller_name ? SDL_strlen(controller_name) : 0);
|
|
if (length > 0) {
|
|
controller_name[length - 1] = '\0';
|
|
}
|
|
CommitControllerName();
|
|
}
|
|
|
|
static void ClearControllerName(void)
|
|
{
|
|
if (controller_name) {
|
|
*controller_name = '\0';
|
|
}
|
|
CommitControllerName();
|
|
}
|
|
|
|
static void CopyControllerName(void)
|
|
{
|
|
SDL_SetClipboardText(controller_name);
|
|
}
|
|
|
|
static void PasteControllerName(void)
|
|
{
|
|
SDL_free(controller_name);
|
|
controller_name = SDL_GetClipboardText();
|
|
CommitControllerName();
|
|
}
|
|
|
|
static void CommitGamepadType(SDL_GamepadType type)
|
|
{
|
|
char *mapping = NULL;
|
|
|
|
if (controller->mapping) {
|
|
mapping = SDL_strdup(controller->mapping);
|
|
} else {
|
|
mapping = NULL;
|
|
}
|
|
mapping = SetMappingType(mapping, type);
|
|
SetAndFreeGamepadMapping(mapping);
|
|
}
|
|
|
|
static const char *GetBindingInstruction(void)
|
|
{
|
|
switch (binding_element) {
|
|
case SDL_GAMEPAD_ELEMENT_INVALID:
|
|
return "Select an element to bind from the list on the left";
|
|
case SDL_GAMEPAD_BUTTON_A:
|
|
if (GetGamepadImageFaceStyle(image) == GAMEPAD_IMAGE_FACE_SONY) {
|
|
return "Press the Cross (X) button";
|
|
} else {
|
|
return "Press the A button";
|
|
}
|
|
case SDL_GAMEPAD_BUTTON_B:
|
|
if (GetGamepadImageFaceStyle(image) == GAMEPAD_IMAGE_FACE_SONY) {
|
|
return "Press the Circle button";
|
|
} else {
|
|
return "Press the B button";
|
|
}
|
|
case SDL_GAMEPAD_BUTTON_X:
|
|
if (GetGamepadImageFaceStyle(image) == GAMEPAD_IMAGE_FACE_SONY) {
|
|
return "Press the Square button";
|
|
} else {
|
|
return "Press the X button";
|
|
}
|
|
case SDL_GAMEPAD_BUTTON_Y:
|
|
if (GetGamepadImageFaceStyle(image) == GAMEPAD_IMAGE_FACE_SONY) {
|
|
return "Press the Triangle button";
|
|
} else {
|
|
return "Press the Y button";
|
|
}
|
|
case SDL_GAMEPAD_BUTTON_BACK:
|
|
return "Press the left center button (Back/View/Share)";
|
|
case SDL_GAMEPAD_BUTTON_GUIDE:
|
|
return "Press the center button (Home/Guide)";
|
|
case SDL_GAMEPAD_BUTTON_START:
|
|
return "Press the right center button (Start/Menu/Options)";
|
|
case SDL_GAMEPAD_BUTTON_LEFT_STICK:
|
|
return "Press the left thumbstick button (LSB/L3)";
|
|
case SDL_GAMEPAD_BUTTON_RIGHT_STICK:
|
|
return "Press the right thumbstick button (RSB/R3)";
|
|
case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
|
|
return "Press the left shoulder button (LB/L1)";
|
|
case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
|
|
return "Press the right shoulder button (RB/R1)";
|
|
case SDL_GAMEPAD_BUTTON_DPAD_UP:
|
|
return "Press the D-Pad up";
|
|
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
|
|
return "Press the D-Pad down";
|
|
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
|
|
return "Press the D-Pad left";
|
|
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
|
|
return "Press the D-Pad right";
|
|
case SDL_GAMEPAD_BUTTON_MISC1:
|
|
return "Press the bottom center button (Share/Capture)";
|
|
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1:
|
|
return "Press the upper paddle under your right hand";
|
|
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE1:
|
|
return "Press the upper paddle under your left hand";
|
|
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2:
|
|
return "Press the lower paddle under your right hand";
|
|
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE2:
|
|
return "Press the lower paddle under your left hand";
|
|
case SDL_GAMEPAD_BUTTON_TOUCHPAD:
|
|
return "Press down on the touchpad";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE:
|
|
return "Move the left thumbstick to the left";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE:
|
|
return "Move the left thumbstick to the right";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE:
|
|
return "Move the left thumbstick up";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE:
|
|
return "Move the left thumbstick down";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE:
|
|
return "Move the right thumbstick to the left";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE:
|
|
return "Move the right thumbstick to the right";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE:
|
|
return "Move the right thumbstick up";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE:
|
|
return "Move the right thumbstick down";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER:
|
|
return "Pull the left trigger (LT/L2)";
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER:
|
|
return "Pull the right trigger (RT/R2)";
|
|
case SDL_GAMEPAD_ELEMENT_NAME:
|
|
return "Type the name of your controller";
|
|
case SDL_GAMEPAD_ELEMENT_TYPE:
|
|
return "Select the type of your controller";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
static int FindController(SDL_JoystickID id)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < num_controllers; ++i) {
|
|
if (id == controllers[i].id) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void SetController(SDL_JoystickID id)
|
|
{
|
|
int i = FindController(id);
|
|
|
|
if (i < 0 && num_controllers > 0) {
|
|
i = 0;
|
|
}
|
|
|
|
if (i >= 0) {
|
|
controller = &controllers[i];
|
|
} else {
|
|
controller = NULL;
|
|
}
|
|
|
|
RefreshControllerName();
|
|
}
|
|
|
|
static void AddController(SDL_JoystickID id, SDL_bool verbose)
|
|
{
|
|
Controller *new_controllers;
|
|
Controller *new_controller;
|
|
SDL_Joystick *joystick;
|
|
|
|
if (FindController(id) >= 0) {
|
|
/* We already have this controller */
|
|
return;
|
|
}
|
|
|
|
new_controllers = (Controller *)SDL_realloc(controllers, (num_controllers + 1) * sizeof(*controllers));
|
|
if (new_controllers == NULL) {
|
|
return;
|
|
}
|
|
|
|
controller = NULL;
|
|
controllers = new_controllers;
|
|
new_controller = &new_controllers[num_controllers++];
|
|
SDL_zerop(new_controller);
|
|
new_controller->id = id;
|
|
|
|
new_controller->joystick = SDL_OpenJoystick(id);
|
|
new_controller->num_axes = SDL_GetNumJoystickAxes(new_controller->joystick);
|
|
new_controller->axis_state = (AxisState *)SDL_calloc(new_controller->num_axes, sizeof(*new_controller->axis_state));
|
|
|
|
joystick = new_controller->joystick;
|
|
if (joystick) {
|
|
if (verbose && !SDL_IsGamepad(id)) {
|
|
const char *name = SDL_GetJoystickName(new_controller->joystick);
|
|
const char *path = SDL_GetJoystickPath(new_controller->joystick);
|
|
SDL_Log("Opened joystick %s%s%s\n", name, path ? ", " : "", path ? path : "");
|
|
}
|
|
} else {
|
|
SDL_Log("Couldn't open joystick: %s", SDL_GetError());
|
|
}
|
|
|
|
if (mapping_controller) {
|
|
SetController(mapping_controller);
|
|
} else {
|
|
SetController(id);
|
|
}
|
|
}
|
|
|
|
static void DelController(SDL_JoystickID id)
|
|
{
|
|
int i = FindController(id);
|
|
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
|
|
if (display_mode == CONTROLLER_MODE_BINDING && id == controller->id) {
|
|
SetDisplayMode(CONTROLLER_MODE_TESTING);
|
|
}
|
|
|
|
/* Reset trigger state */
|
|
if (controllers[i].trigger_effect != 0) {
|
|
controllers[i].trigger_effect = -1;
|
|
CyclePS5TriggerEffect(&controllers[i]);
|
|
}
|
|
SDL_assert(controllers[i].gamepad == NULL);
|
|
if (controllers[i].axis_state) {
|
|
SDL_free(controllers[i].axis_state);
|
|
}
|
|
if (controllers[i].joystick) {
|
|
SDL_CloseJoystick(controllers[i].joystick);
|
|
}
|
|
|
|
--num_controllers;
|
|
if (i < num_controllers) {
|
|
SDL_memcpy(&controllers[i], &controllers[i + 1], (num_controllers - i) * sizeof(*controllers));
|
|
}
|
|
|
|
if (mapping_controller) {
|
|
SetController(mapping_controller);
|
|
} else {
|
|
SetController(id);
|
|
}
|
|
}
|
|
|
|
static void HandleGamepadRemapped(SDL_JoystickID id)
|
|
{
|
|
char *mapping;
|
|
int i = FindController(id);
|
|
|
|
SDL_assert(i >= 0);
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
|
|
if (!controllers[i].gamepad) {
|
|
/* Failed to open this controller */
|
|
return;
|
|
}
|
|
|
|
/* Get the current mapping */
|
|
mapping = SDL_GetGamepadMapping(controllers[i].gamepad);
|
|
|
|
/* Make sure the mapping has a valid name */
|
|
if (mapping && !MappingHasName(mapping)) {
|
|
mapping = SetMappingName(mapping, SDL_GetJoystickName(controllers[i].joystick));
|
|
}
|
|
|
|
SDL_free(controllers[i].mapping);
|
|
controllers[i].mapping = mapping;
|
|
controllers[i].has_bindings = MappingHasBindings(mapping);
|
|
}
|
|
|
|
static void HandleGamepadAdded(SDL_JoystickID id, SDL_bool verbose)
|
|
{
|
|
SDL_Gamepad *gamepad;
|
|
Uint16 firmware_version;
|
|
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
|
|
};
|
|
int i;
|
|
|
|
i = FindController(id);
|
|
|
|
SDL_assert(i >= 0);
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
|
|
SDL_assert(!controllers[i].gamepad);
|
|
controllers[i].gamepad = SDL_OpenGamepad(id);
|
|
|
|
gamepad = controllers[i].gamepad;
|
|
if (gamepad) {
|
|
if (verbose) {
|
|
const char *name = SDL_GetGamepadName(gamepad);
|
|
const char *path = SDL_GetGamepadPath(gamepad);
|
|
SDL_Log("Opened gamepad %s%s%s\n", name, path ? ", " : "", path ? path : "");
|
|
|
|
firmware_version = SDL_GetGamepadFirmwareVersion(gamepad);
|
|
if (firmware_version) {
|
|
SDL_Log("Firmware version: 0x%x (%d)\n", firmware_version, firmware_version);
|
|
}
|
|
|
|
if (SDL_GamepadHasRumble(gamepad)) {
|
|
SDL_Log("Rumble supported");
|
|
}
|
|
|
|
if (SDL_GamepadHasRumbleTriggers(gamepad)) {
|
|
SDL_Log("Trigger rumble supported");
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < SDL_arraysize(sensors); ++i) {
|
|
SDL_SensorType sensor = sensors[i];
|
|
|
|
if (SDL_GamepadHasSensor(gamepad, sensor)) {
|
|
if (verbose) {
|
|
SDL_Log("Enabling %s at %.2f Hz\n", GetSensorName(sensor), SDL_GetGamepadSensorDataRate(gamepad, sensor));
|
|
}
|
|
SDL_SetGamepadSensorEnabled(gamepad, sensor, SDL_TRUE);
|
|
}
|
|
}
|
|
} else {
|
|
SDL_Log("Couldn't open gamepad: %s", SDL_GetError());
|
|
}
|
|
|
|
HandleGamepadRemapped(id);
|
|
}
|
|
|
|
static void HandleGamepadRemoved(SDL_JoystickID id)
|
|
{
|
|
int i = FindController(id);
|
|
|
|
SDL_assert(i >= 0);
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
|
|
if (controllers[i].mapping) {
|
|
SDL_free(controllers[i].mapping);
|
|
controllers[i].mapping = NULL;
|
|
}
|
|
if (controllers[i].gamepad) {
|
|
SDL_CloseGamepad(controllers[i].gamepad);
|
|
controllers[i].gamepad = NULL;
|
|
}
|
|
}
|
|
|
|
static Uint16 ConvertAxisToRumble(Sint16 axisval)
|
|
{
|
|
/* Only start rumbling if the axis is past the halfway point */
|
|
const Sint16 half_axis = (Sint16)SDL_ceil(SDL_JOYSTICK_AXIS_MAX / 2.0f);
|
|
if (axisval > half_axis) {
|
|
return (Uint16)(axisval - half_axis) * 4;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static SDL_bool ShowingFront(void)
|
|
{
|
|
SDL_bool showing_front = SDL_TRUE;
|
|
int i;
|
|
|
|
/* Show the back of the gamepad if the paddles are being held or bound */
|
|
for (i = SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1; i <= SDL_GAMEPAD_BUTTON_LEFT_PADDLE2; ++i) {
|
|
if (SDL_GetGamepadButton(controller->gamepad, (SDL_GamepadButton)i) == SDL_PRESSED ||
|
|
binding_element == i) {
|
|
showing_front = SDL_FALSE;
|
|
break;
|
|
}
|
|
}
|
|
if ((SDL_GetModState() & SDL_KMOD_SHIFT) && binding_element != SDL_GAMEPAD_ELEMENT_NAME) {
|
|
showing_front = SDL_FALSE;
|
|
}
|
|
return showing_front;
|
|
}
|
|
|
|
static void SDLCALL VirtualGamepadSetPlayerIndex(void *userdata, int player_index)
|
|
{
|
|
SDL_Log("Virtual Gamepad: player index set to %d\n", player_index);
|
|
}
|
|
|
|
static int SDLCALL VirtualGamepadRumble(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
|
|
{
|
|
SDL_Log("Virtual Gamepad: rumble set to %d/%d\n", low_frequency_rumble, high_frequency_rumble);
|
|
return 0;
|
|
}
|
|
|
|
static int SDLCALL VirtualGamepadRumbleTriggers(void *userdata, Uint16 left_rumble, Uint16 right_rumble)
|
|
{
|
|
SDL_Log("Virtual Gamepad: trigger rumble set to %d/%d\n", left_rumble, right_rumble);
|
|
return 0;
|
|
}
|
|
|
|
static int SDLCALL VirtualGamepadSetLED(void *userdata, Uint8 red, Uint8 green, Uint8 blue)
|
|
{
|
|
SDL_Log("Virtual Gamepad: LED set to RGB %d,%d,%d\n", red, green, blue);
|
|
return 0;
|
|
}
|
|
|
|
static void OpenVirtualGamepad(void)
|
|
{
|
|
SDL_VirtualJoystickDesc desc;
|
|
SDL_JoystickID virtual_id;
|
|
|
|
if (virtual_joystick) {
|
|
return;
|
|
}
|
|
|
|
SDL_zero(desc);
|
|
desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
|
|
desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
|
|
desc.naxes = SDL_GAMEPAD_AXIS_MAX;
|
|
desc.nbuttons = SDL_GAMEPAD_BUTTON_MAX;
|
|
desc.SetPlayerIndex = VirtualGamepadSetPlayerIndex;
|
|
desc.Rumble = VirtualGamepadRumble;
|
|
desc.RumbleTriggers = VirtualGamepadRumbleTriggers;
|
|
desc.SetLED = VirtualGamepadSetLED;
|
|
|
|
virtual_id = SDL_AttachVirtualJoystickEx(&desc);
|
|
if (virtual_id == 0) {
|
|
SDL_Log("Couldn't attach virtual device: %s\n", SDL_GetError());
|
|
} else {
|
|
virtual_joystick = SDL_OpenJoystick(virtual_id);
|
|
if (virtual_joystick == NULL) {
|
|
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError());
|
|
}
|
|
}
|
|
}
|
|
|
|
static void CloseVirtualGamepad(void)
|
|
{
|
|
int i;
|
|
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);
|
|
}
|
|
}
|
|
SDL_free(joysticks);
|
|
}
|
|
|
|
if (virtual_joystick) {
|
|
SDL_CloseJoystick(virtual_joystick);
|
|
virtual_joystick = NULL;
|
|
}
|
|
}
|
|
|
|
static void VirtualGamepadMouseMotion(float x, float y)
|
|
{
|
|
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
|
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
|
|
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) {
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
|
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
int range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
|
|
float distance = SDL_clamp((y - virtual_axis_start_y) / GetGamepadImageAxisHeight(image), 0.0f, 1.0f);
|
|
Sint16 value = (Sint16)(SDL_JOYSTICK_AXIS_MIN + (distance * range));
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, value);
|
|
} else {
|
|
float distanceX = SDL_clamp((x - virtual_axis_start_x) / GetGamepadImageAxisWidth(image), -1.0f, 1.0f);
|
|
float distanceY = SDL_clamp((y - virtual_axis_start_y) / GetGamepadImageAxisHeight(image), -1.0f, 1.0f);
|
|
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);
|
|
}
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, valueX);
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active + 1, valueY);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void VirtualGamepadMouseDown(float x, float y)
|
|
{
|
|
int element = GetGamepadImageElementAt(image, x, y);
|
|
|
|
if (element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
return;
|
|
}
|
|
|
|
if (element < SDL_GAMEPAD_BUTTON_MAX) {
|
|
virtual_button_active = (SDL_GamepadButton)element;
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_PRESSED);
|
|
} else {
|
|
switch (element) {
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE:
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_LEFTX;
|
|
break;
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE:
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE:
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_RIGHTX;
|
|
break;
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER:
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
|
|
break;
|
|
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER:
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
|
|
break;
|
|
}
|
|
virtual_axis_start_x = x;
|
|
virtual_axis_start_y = y;
|
|
}
|
|
}
|
|
|
|
static void VirtualGamepadMouseUp(float x, float y)
|
|
{
|
|
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
|
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
|
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
|
}
|
|
|
|
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) {
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, SDL_JOYSTICK_AXIS_MIN);
|
|
} else {
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, 0);
|
|
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active + 1, 0);
|
|
}
|
|
virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
|
|
}
|
|
}
|
|
|
|
static void DrawGamepadWaiting(SDL_Renderer *renderer)
|
|
{
|
|
const char *text = "Waiting for gamepad, press A to add a virtual controller";
|
|
float x, y;
|
|
|
|
x = (float)SCREEN_WIDTH / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2;
|
|
y = (float)TITLE_HEIGHT / 2 - FONT_CHARACTER_SIZE / 2;
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
}
|
|
|
|
static void DrawGamepadInfo(SDL_Renderer *renderer)
|
|
{
|
|
const char *type;
|
|
const char *serial;
|
|
char text[128];
|
|
float x, y;
|
|
|
|
if (title_highlighted) {
|
|
Uint8 r, g, b, a;
|
|
|
|
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
|
|
|
if (title_pressed) {
|
|
SDL_SetRenderDrawColor(renderer, PRESSED_COLOR);
|
|
} else {
|
|
SDL_SetRenderDrawColor(renderer, HIGHLIGHT_COLOR);
|
|
}
|
|
SDL_RenderFillRect(renderer, &title_area);
|
|
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
|
}
|
|
|
|
if (type_highlighted) {
|
|
Uint8 r, g, b, a;
|
|
|
|
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
|
|
|
if (type_pressed) {
|
|
SDL_SetRenderDrawColor(renderer, PRESSED_COLOR);
|
|
} else {
|
|
SDL_SetRenderDrawColor(renderer, HIGHLIGHT_COLOR);
|
|
}
|
|
SDL_RenderFillRect(renderer, &type_area);
|
|
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
|
}
|
|
|
|
if (controller_name && *controller_name) {
|
|
x = title_area.x + title_area.w / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(controller_name)) / 2;
|
|
y = title_area.y + title_area.h / 2 - FONT_CHARACTER_SIZE / 2;
|
|
SDLTest_DrawString(renderer, x, y, controller_name);
|
|
}
|
|
|
|
if (SDL_IsJoystickVirtual(controller->id)) {
|
|
SDL_strlcpy(text, "Click on the gamepad image below to generate input", sizeof(text));
|
|
x = (float)SCREEN_WIDTH / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2;
|
|
y = (float)TITLE_HEIGHT / 2 - FONT_CHARACTER_SIZE / 2 + FONT_LINE_HEIGHT + 2.0f;
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
}
|
|
|
|
type = GetGamepadTypeString(SDL_GetGamepadType(controller->gamepad));
|
|
x = type_area.x + type_area.w / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(type)) / 2;
|
|
y = type_area.y + type_area.h / 2 - FONT_CHARACTER_SIZE / 2;
|
|
SDLTest_DrawString(renderer, x, y, type);
|
|
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
SDL_snprintf(text, SDL_arraysize(text), "VID: 0x%.4x PID: 0x%.4x",
|
|
SDL_GetJoystickVendor(controller->joystick),
|
|
SDL_GetJoystickProduct(controller->joystick));
|
|
y = (float)SCREEN_HEIGHT - 8.0f - FONT_LINE_HEIGHT;
|
|
x = (float)SCREEN_WIDTH - 8.0f - (FONT_CHARACTER_SIZE * SDL_strlen(text));
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
serial = SDL_GetJoystickSerial(controller->joystick);
|
|
if (serial && *serial) {
|
|
SDL_snprintf(text, SDL_arraysize(text), "Serial: %s", serial);
|
|
x = (float)SCREEN_WIDTH / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2;
|
|
y = (float)SCREEN_HEIGHT - 8.0f - FONT_LINE_HEIGHT;
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void DrawBindingTips(SDL_Renderer *renderer)
|
|
{
|
|
const char *text;
|
|
SDL_Rect image_area, button_area;
|
|
int x, y;
|
|
|
|
GetGamepadImageArea(image, &image_area);
|
|
GetGamepadButtonArea(done_mapping_button, &button_area);
|
|
x = image_area.x + image_area.w / 2;
|
|
y = image_area.y + image_area.h;
|
|
y += (button_area.y - y - FONT_CHARACTER_SIZE) / 2;
|
|
|
|
text = GetBindingInstruction();
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
SDLTest_DrawString(renderer, (float)x - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2, (float)y, text);
|
|
} else {
|
|
Uint8 r, g, b, a;
|
|
SDL_FRect rect;
|
|
SDL_bool bound_A, bound_B;
|
|
|
|
y -= (FONT_CHARACTER_SIZE + BUTTON_MARGIN) / 2;
|
|
|
|
rect.w = 2.0f + (FONT_CHARACTER_SIZE * SDL_strlen(text)) + 2.0f;
|
|
rect.h = 2.0f + FONT_CHARACTER_SIZE + 2.0f;
|
|
rect.x = (float)x - rect.w / 2;
|
|
rect.y = (float)y - 2.0f;
|
|
|
|
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
|
SDL_SetRenderDrawColor(renderer, SELECTED_COLOR);
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
|
SDLTest_DrawString(renderer, (float)x - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2, (float)y, text);
|
|
|
|
y += (FONT_CHARACTER_SIZE + BUTTON_MARGIN);
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
text = "(press RETURN to complete)";
|
|
} else if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
|
|
text = "(press ESC to cancel)";
|
|
} else {
|
|
bound_A = MappingHasElement(controller->mapping, SDL_GAMEPAD_BUTTON_A);
|
|
bound_B = MappingHasElement(controller->mapping, SDL_GAMEPAD_BUTTON_B);
|
|
if (binding_flow && bound_A && bound_B) {
|
|
text = "(press A to skip, B to go back, and ESC to cancel)";
|
|
} else {
|
|
text = "(press SPACE to clear binding and ESC to cancel)";
|
|
}
|
|
}
|
|
SDLTest_DrawString(renderer, (float)x - (FONT_CHARACTER_SIZE * SDL_strlen(text)) / 2, (float)y, text);
|
|
}
|
|
}
|
|
|
|
static void UpdateGamepadEffects(void)
|
|
{
|
|
if (display_mode != CONTROLLER_MODE_TESTING || !controller->gamepad) {
|
|
return;
|
|
}
|
|
|
|
/* Update LED based on left thumbstick position */
|
|
{
|
|
Sint16 x = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_LEFTX);
|
|
Sint16 y = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_LEFTY);
|
|
|
|
if (!set_LED) {
|
|
set_LED = (x < -8000 || x > 8000 || y > 8000);
|
|
}
|
|
if (set_LED) {
|
|
Uint8 r, g, b;
|
|
|
|
if (x < 0) {
|
|
r = (Uint8)(((~x) * 255) / 32767);
|
|
b = 0;
|
|
} else {
|
|
r = 0;
|
|
b = (Uint8)(((int)(x)*255) / 32767);
|
|
}
|
|
if (y > 0) {
|
|
g = (Uint8)(((int)(y)*255) / 32767);
|
|
} else {
|
|
g = 0;
|
|
}
|
|
|
|
SDL_SetGamepadLED(controller->gamepad, r, g, b);
|
|
}
|
|
}
|
|
|
|
if (controller->trigger_effect == 0) {
|
|
/* Update rumble based on trigger state */
|
|
{
|
|
Sint16 left = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER);
|
|
Sint16 right = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
|
|
Uint16 low_frequency_rumble = ConvertAxisToRumble(left);
|
|
Uint16 high_frequency_rumble = ConvertAxisToRumble(right);
|
|
SDL_RumbleGamepad(controller->gamepad, low_frequency_rumble, high_frequency_rumble, 250);
|
|
}
|
|
|
|
/* Update trigger rumble based on thumbstick state */
|
|
{
|
|
Sint16 left = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_LEFTY);
|
|
Sint16 right = SDL_GetGamepadAxis(controller->gamepad, SDL_GAMEPAD_AXIS_RIGHTY);
|
|
Uint16 left_rumble = ConvertAxisToRumble(~left);
|
|
Uint16 right_rumble = ConvertAxisToRumble(~right);
|
|
|
|
SDL_RumbleGamepadTriggers(controller->gamepad, left_rumble, right_rumble, 250);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void loop(void *arg)
|
|
{
|
|
SDL_Event event;
|
|
|
|
/* Update to get the current event state */
|
|
SDL_PumpEvents();
|
|
|
|
/* Process all currently pending events */
|
|
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) == 1) {
|
|
SDL_ConvertEventToRenderCoordinates(screen, &event);
|
|
|
|
switch (event.type) {
|
|
case SDL_EVENT_JOYSTICK_ADDED:
|
|
AddController(event.jdevice.which, SDL_TRUE);
|
|
break;
|
|
|
|
case SDL_EVENT_JOYSTICK_REMOVED:
|
|
DelController(event.jdevice.which);
|
|
break;
|
|
|
|
case SDL_EVENT_JOYSTICK_AXIS_MOTION:
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
if (event.jaxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.jaxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
|
|
SetController(event.jaxis.which);
|
|
}
|
|
} else if (display_mode == CONTROLLER_MODE_BINDING &&
|
|
event.jaxis.which == controller->id &&
|
|
event.jaxis.axis < controller->num_axes &&
|
|
binding_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 gamepad needed 96 */
|
|
AxisState *pAxisState = &controller->axis_state[event.jaxis.axis];
|
|
int nValue = event.jaxis.value;
|
|
int nCurrentDistance, nFarthestDistance;
|
|
if (!pAxisState->m_bMoving) {
|
|
Sint16 nInitialValue;
|
|
pAxisState->m_bMoving = SDL_GetJoystickAxisInitialState(controller->joystick, event.jaxis.axis, &nInitialValue);
|
|
pAxisState->m_nLastValue = nValue;
|
|
pAxisState->m_nStartingValue = nInitialValue;
|
|
pAxisState->m_nFarthestValue = nInitialValue;
|
|
} else if (SDL_abs(nValue - pAxisState->m_nLastValue) <= MAX_ALLOWED_JITTER) {
|
|
break;
|
|
} else {
|
|
pAxisState->m_nLastValue = nValue;
|
|
}
|
|
nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
|
|
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
|
|
if (nCurrentDistance > nFarthestDistance) {
|
|
pAxisState->m_nFarthestValue = nValue;
|
|
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
|
|
}
|
|
|
|
#ifdef DEBUG_AXIS_MAPPING
|
|
SDL_Log("AXIS %d nValue %d nCurrentDistance %d nFarthestDistance %d\n", event.jaxis.axis, nValue, nCurrentDistance, nFarthestDistance);
|
|
#endif
|
|
/* If we've gone out far enough and started to come back, let's bind this axis */
|
|
if (nFarthestDistance >= 16000 && nCurrentDistance <= 10000) {
|
|
char binding[12];
|
|
int axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
|
|
int axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
|
|
|
|
if (axis_min == 0 && axis_max == SDL_JOYSTICK_AXIS_MIN) {
|
|
/* The negative half axis */
|
|
(void)SDL_snprintf(binding, sizeof(binding), "-a%d", event.jaxis.axis);
|
|
} else if (axis_min == 0 && axis_max == SDL_JOYSTICK_AXIS_MAX) {
|
|
/* The positive half axis */
|
|
(void)SDL_snprintf(binding, sizeof(binding), "+a%d", event.jaxis.axis);
|
|
} else {
|
|
(void)SDL_snprintf(binding, sizeof(binding), "a%d", event.jaxis.axis);
|
|
if (axis_min > axis_max) {
|
|
/* Invert the axis */
|
|
SDL_strlcat(binding, "~", SDL_arraysize(binding));
|
|
}
|
|
}
|
|
#ifdef DEBUG_AXIS_MAPPING
|
|
SDL_Log("AXIS %d axis_min = %d, axis_max = %d, binding = %s\n", event.jaxis.axis, axis_min, axis_max, binding);
|
|
#endif
|
|
CommitBindingElement(binding, SDL_FALSE);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
SetController(event.jbutton.which);
|
|
}
|
|
break;
|
|
|
|
case SDL_EVENT_JOYSTICK_BUTTON_UP:
|
|
if (display_mode == CONTROLLER_MODE_BINDING &&
|
|
event.jbutton.which == controller->id &&
|
|
binding_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
char binding[12];
|
|
|
|
SDL_snprintf(binding, sizeof(binding), "b%d", event.jbutton.button);
|
|
CommitBindingElement(binding, SDL_FALSE);
|
|
}
|
|
break;
|
|
|
|
case SDL_EVENT_JOYSTICK_HAT_MOTION:
|
|
if (display_mode == CONTROLLER_MODE_BINDING &&
|
|
event.jhat.which == controller->id &&
|
|
event.jhat.value != SDL_HAT_CENTERED &&
|
|
binding_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
char binding[12];
|
|
|
|
SDL_snprintf(binding, sizeof(binding), "h%d.%d", event.jhat.hat, event.jhat.value);
|
|
CommitBindingElement(binding, SDL_FALSE);
|
|
}
|
|
break;
|
|
|
|
case SDL_EVENT_GAMEPAD_ADDED:
|
|
HandleGamepadAdded(event.gdevice.which, SDL_TRUE);
|
|
break;
|
|
|
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
|
HandleGamepadRemoved(event.gdevice.which);
|
|
break;
|
|
|
|
case SDL_EVENT_GAMEPAD_REMAPPED:
|
|
HandleGamepadRemapped(event.gdevice.which);
|
|
break;
|
|
|
|
#ifdef VERBOSE_TOUCHPAD
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
|
|
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " touchpad %" SDL_PRIs32 " finger %" SDL_PRIs32 " %s %.2f, %.2f, %.2f\n",
|
|
event.gtouchpad.which,
|
|
event.gtouchpad.touchpad,
|
|
event.gtouchpad.finger,
|
|
(event.type == SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN ? "pressed at" : (event.type == SDL_EVENT_GAMEPAD_TOUCHPAD_UP ? "released at" : "moved to")),
|
|
event.gtouchpad.x,
|
|
event.gtouchpad.y,
|
|
event.gtouchpad.pressure);
|
|
break;
|
|
#endif /* VERBOSE_TOUCHPAD */
|
|
|
|
#ifdef VERBOSE_SENSORS
|
|
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " sensor %s: %.2f, %.2f, %.2f (%" SDL_PRIu64 ")\n",
|
|
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);
|
|
break;
|
|
#endif /* VERBOSE_SENSORS */
|
|
|
|
#ifdef VERBOSE_AXES
|
|
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
if (event.gaxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.gaxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
|
|
SetController(event.gaxis.which);
|
|
}
|
|
}
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " axis %s changed to %d\n",
|
|
event.gaxis.which,
|
|
SDL_GetGamepadStringForAxis((SDL_GamepadAxis) event.gaxis.axis),
|
|
event.gaxis.value);
|
|
break;
|
|
#endif /* VERBOSE_AXES */
|
|
|
|
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
|
case SDL_EVENT_GAMEPAD_BUTTON_UP:
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
|
SetController(event.gbutton.which);
|
|
}
|
|
}
|
|
#ifdef VERBOSE_BUTTONS
|
|
SDL_Log("Gamepad %" SDL_PRIu32 " button %s %s\n",
|
|
event.gbutton.which,
|
|
SDL_GetGamepadStringForButton((SDL_GamepadButton) event.gbutton.button),
|
|
event.gbutton.state ? "pressed" : "released");
|
|
#endif /* VERBOSE_BUTTONS */
|
|
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
/* Cycle PS5 trigger effects when the microphone button is pressed */
|
|
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN &&
|
|
controller && SDL_GetGamepadType(controller->gamepad) == SDL_GAMEPAD_TYPE_PS5 &&
|
|
event.gbutton.button == SDL_GAMEPAD_BUTTON_MISC1) {
|
|
CyclePS5TriggerEffect(controller);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
if (virtual_joystick && controller && controller->joystick == virtual_joystick) {
|
|
VirtualGamepadMouseDown(event.button.x, event.button.y);
|
|
}
|
|
UpdateButtonHighlights(event.button.x, event.button.y, event.button.state ? SDL_TRUE : SDL_FALSE);
|
|
break;
|
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
|
if (virtual_joystick && controller && controller->joystick == virtual_joystick) {
|
|
VirtualGamepadMouseUp(event.button.x, event.button.y);
|
|
}
|
|
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
if (GamepadButtonContains(setup_mapping_button, event.button.x, event.button.y)) {
|
|
SetDisplayMode(CONTROLLER_MODE_BINDING);
|
|
}
|
|
} else if (display_mode == CONTROLLER_MODE_BINDING) {
|
|
if (GamepadButtonContains(done_mapping_button, event.button.x, event.button.y)) {
|
|
if (controller->mapping) {
|
|
SDL_Log("Mapping complete:\n");
|
|
SDL_Log("%s\n", controller->mapping);
|
|
}
|
|
SetDisplayMode(CONTROLLER_MODE_TESTING);
|
|
} else if (GamepadButtonContains(cancel_button, event.button.x, event.button.y)) {
|
|
CancelMapping();
|
|
} else if (GamepadButtonContains(clear_button, event.button.x, event.button.y)) {
|
|
ClearMapping();
|
|
} else if (controller->has_bindings &&
|
|
GamepadButtonContains(copy_button, event.button.x, event.button.y)) {
|
|
CopyMapping();
|
|
} else if (GamepadButtonContains(paste_button, event.button.x, event.button.y)) {
|
|
PasteMapping();
|
|
} else if (title_pressed) {
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_NAME, SDL_FALSE);
|
|
} else if (type_pressed) {
|
|
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_TYPE, SDL_FALSE);
|
|
} else if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
|
|
int type = GetGamepadTypeDisplayAt(gamepad_type, event.button.x, event.button.y);
|
|
if (type != SDL_GAMEPAD_TYPE_UNSELECTED) {
|
|
CommitGamepadType((SDL_GamepadType)type);
|
|
StopBinding();
|
|
}
|
|
} else {
|
|
int gamepad_element = SDL_GAMEPAD_ELEMENT_INVALID;
|
|
char *joystick_element;
|
|
|
|
if (controller->joystick != virtual_joystick) {
|
|
gamepad_element = GetGamepadImageElementAt(image, event.button.x, event.button.y);
|
|
}
|
|
if (gamepad_element == SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
gamepad_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, event.button.x, event.button.y);
|
|
}
|
|
if (gamepad_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
/* Set this to SDL_FALSE if you don't want to start the binding flow at this point */
|
|
const SDL_bool should_start_flow = SDL_TRUE;
|
|
SetCurrentBindingElement(gamepad_element, should_start_flow);
|
|
}
|
|
|
|
joystick_element = GetJoystickDisplayElementAt(joystick_elements, controller->joystick, event.button.x, event.button.y);
|
|
if (joystick_element) {
|
|
CommitBindingElement(joystick_element, SDL_TRUE);
|
|
SDL_free(joystick_element);
|
|
}
|
|
}
|
|
}
|
|
UpdateButtonHighlights(event.button.x, event.button.y, event.button.state ? SDL_TRUE : SDL_FALSE);
|
|
break;
|
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
if (virtual_joystick && controller && controller->joystick == virtual_joystick) {
|
|
VirtualGamepadMouseMotion(event.motion.x, event.motion.y);
|
|
}
|
|
UpdateButtonHighlights(event.motion.x, event.motion.y, event.motion.state ? SDL_TRUE : SDL_FALSE);
|
|
break;
|
|
|
|
case SDL_EVENT_KEY_DOWN:
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
|
|
if (controller && controller->gamepad) {
|
|
int player_index = (event.key.keysym.sym - SDLK_0);
|
|
|
|
SDL_SetGamepadPlayerIndex(controller->gamepad, player_index);
|
|
}
|
|
break;
|
|
} else if (event.key.keysym.sym == SDLK_a) {
|
|
OpenVirtualGamepad();
|
|
} else if (event.key.keysym.sym == SDLK_d) {
|
|
CloseVirtualGamepad();
|
|
} else if (event.key.keysym.sym == SDLK_r && (event.key.keysym.mod & SDL_KMOD_CTRL)) {
|
|
SDL_ReloadGamepadMappings();
|
|
} else if (event.key.keysym.sym == SDLK_ESCAPE) {
|
|
done = SDL_TRUE;
|
|
}
|
|
} else if (display_mode == CONTROLLER_MODE_BINDING) {
|
|
if (event.key.keysym.sym == SDLK_c && (event.key.keysym.mod & SDL_KMOD_CTRL)) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
CopyControllerName();
|
|
} else {
|
|
CopyMapping();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_v && (event.key.keysym.mod & SDL_KMOD_CTRL)) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
ClearControllerName();
|
|
PasteControllerName();
|
|
} else {
|
|
PasteMapping();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_x && (event.key.keysym.mod & SDL_KMOD_CTRL)) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
CopyControllerName();
|
|
ClearControllerName();
|
|
} else {
|
|
CopyMapping();
|
|
ClearMapping();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_SPACE) {
|
|
if (binding_element != SDL_GAMEPAD_ELEMENT_NAME) {
|
|
ClearBinding();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_BACKSPACE) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
BackspaceControllerName();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_RETURN) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
StopBinding();
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_ESCAPE) {
|
|
if (binding_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
StopBinding();
|
|
} else {
|
|
CancelMapping();
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case SDL_EVENT_TEXT_INPUT:
|
|
if (display_mode == CONTROLLER_MODE_BINDING) {
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
|
|
AddControllerNameText(event.text.text);
|
|
}
|
|
}
|
|
break;
|
|
case SDL_EVENT_QUIT:
|
|
done = SDL_TRUE;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Wait 30 ms for joystick events to stop coming in,
|
|
in case a gamepad sends multiple events for a single control (e.g. axis and button for trigger)
|
|
*/
|
|
if (binding_advance_time && SDL_GetTicks() > (binding_advance_time + 30)) {
|
|
if (binding_flow) {
|
|
SetNextBindingElement();
|
|
} else {
|
|
StopBinding();
|
|
}
|
|
}
|
|
|
|
/* blank screen, set up for drawing this frame. */
|
|
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderClear(screen);
|
|
SDL_SetRenderDrawColor(screen, 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE);
|
|
|
|
if (controller) {
|
|
SetGamepadImageShowingFront(image, ShowingFront());
|
|
UpdateGamepadImageFromGamepad(image, controller->gamepad);
|
|
if (display_mode == CONTROLLER_MODE_BINDING &&
|
|
binding_element != SDL_GAMEPAD_ELEMENT_INVALID) {
|
|
SetGamepadImageElement(image, binding_element, SDL_TRUE);
|
|
}
|
|
RenderGamepadImage(image);
|
|
|
|
if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
|
|
SetGamepadTypeDisplayRealType(gamepad_type, SDL_GetRealGamepadType(controller->gamepad));
|
|
RenderGamepadTypeDisplay(gamepad_type);
|
|
} else {
|
|
RenderGamepadDisplay(gamepad_elements, controller->gamepad);
|
|
}
|
|
RenderJoystickDisplay(joystick_elements, controller->joystick);
|
|
|
|
if (display_mode == CONTROLLER_MODE_TESTING) {
|
|
RenderGamepadButton(setup_mapping_button);
|
|
} else if (display_mode == CONTROLLER_MODE_BINDING) {
|
|
DrawBindingTips(screen);
|
|
RenderGamepadButton(done_mapping_button);
|
|
RenderGamepadButton(cancel_button);
|
|
RenderGamepadButton(clear_button);
|
|
if (controller->has_bindings) {
|
|
RenderGamepadButton(copy_button);
|
|
}
|
|
RenderGamepadButton(paste_button);
|
|
}
|
|
|
|
DrawGamepadInfo(screen);
|
|
|
|
UpdateGamepadEffects();
|
|
} else {
|
|
DrawGamepadWaiting(screen);
|
|
}
|
|
SDL_Delay(16);
|
|
SDL_RenderPresent(screen);
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
if (done) {
|
|
emscripten_cancel_main_loop();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i;
|
|
float content_scale;
|
|
int screen_width, screen_height;
|
|
SDL_Rect area;
|
|
int gamepad_index = -1;
|
|
SDLTest_CommonState *state;
|
|
|
|
/* Initialize test framework */
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
|
if (state == NULL) {
|
|
return 1;
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_STEAM, "1");
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1");
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
|
|
SDL_SetHint(SDL_HINT_LINUX_JOYSTICK_DEADZONES, "1");
|
|
|
|
/* Enable standard application logging */
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
/* Parse commandline */
|
|
for (i = 1; i < argc;) {
|
|
int consumed;
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
if (!consumed) {
|
|
if (SDL_strcmp(argv[i], "--mappings") == 0) {
|
|
int map_i;
|
|
SDL_Log("Supported mappings:\n");
|
|
for (map_i = 0; map_i < SDL_GetNumGamepadMappings(); ++map_i) {
|
|
char *mapping = SDL_GetGamepadMappingForIndex(map_i);
|
|
if (mapping) {
|
|
SDL_Log("\t%s\n", mapping);
|
|
SDL_free(mapping);
|
|
}
|
|
}
|
|
SDL_Log("\n");
|
|
consumed = 1;
|
|
} else if (SDL_strcmp(argv[i], "--virtual") == 0) {
|
|
OpenVirtualGamepad();
|
|
consumed = 1;
|
|
} else if (gamepad_index < 0) {
|
|
char *endptr = NULL;
|
|
gamepad_index = (int)SDL_strtol(argv[i], &endptr, 0);
|
|
if (endptr != argv[i] && *endptr == '\0' && gamepad_index >= 0) {
|
|
consumed = 1;
|
|
}
|
|
}
|
|
}
|
|
if (consumed <= 0) {
|
|
static const char *options[] = { "[--mappings]", "[--virtual]", "[index]", NULL };
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
return 1;
|
|
}
|
|
|
|
i += consumed;
|
|
}
|
|
if (gamepad_index < 0) {
|
|
gamepad_index = 0;
|
|
}
|
|
|
|
/* Initialize SDL (Note: video is required to start event loop) */
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD) < 0) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
SDL_AddGamepadMappingsFromFile("gamecontrollerdb.txt");
|
|
|
|
/* Create a window to display gamepad state */
|
|
content_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
|
if (content_scale == 0.0f) {
|
|
content_scale = 1.0f;
|
|
}
|
|
screen_width = (int)SDL_ceilf(SCREEN_WIDTH * content_scale);
|
|
screen_height = (int)SDL_ceilf(SCREEN_HEIGHT * content_scale);
|
|
window = SDL_CreateWindow("SDL Controller Test", screen_width, screen_height, 0);
|
|
if (window == NULL) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
|
return 2;
|
|
}
|
|
|
|
screen = SDL_CreateRenderer(window, NULL, 0);
|
|
if (screen == NULL) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
|
SDL_DestroyWindow(window);
|
|
return 2;
|
|
}
|
|
|
|
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderClear(screen);
|
|
SDL_RenderPresent(screen);
|
|
|
|
/* scale for platforms that don't give you the window size you asked for. */
|
|
SDL_SetRenderLogicalPresentation(screen, SCREEN_WIDTH, SCREEN_HEIGHT,
|
|
SDL_LOGICAL_PRESENTATION_LETTERBOX,
|
|
SDL_SCALEMODE_LINEAR);
|
|
|
|
|
|
title_area.w = (float)GAMEPAD_WIDTH;
|
|
title_area.h = (float)FONT_CHARACTER_SIZE + 2 * BUTTON_MARGIN;
|
|
title_area.x = (float)PANEL_WIDTH + PANEL_SPACING;
|
|
title_area.y = (float)TITLE_HEIGHT / 2 - title_area.h / 2;
|
|
|
|
type_area.w = (float)PANEL_WIDTH - 2 * BUTTON_MARGIN;
|
|
type_area.h = (float)FONT_CHARACTER_SIZE + 2 * BUTTON_MARGIN;
|
|
type_area.x = (float)BUTTON_MARGIN;
|
|
type_area.y = (float)TITLE_HEIGHT / 2 - type_area.h / 2;
|
|
|
|
image = CreateGamepadImage(screen);
|
|
if (image == NULL) {
|
|
SDL_DestroyRenderer(screen);
|
|
SDL_DestroyWindow(window);
|
|
return 2;
|
|
}
|
|
SetGamepadImagePosition(image, PANEL_WIDTH + PANEL_SPACING, TITLE_HEIGHT);
|
|
|
|
gamepad_elements = CreateGamepadDisplay(screen);
|
|
area.x = 0;
|
|
area.y = TITLE_HEIGHT;
|
|
area.w = PANEL_WIDTH;
|
|
area.h = GAMEPAD_HEIGHT;
|
|
SetGamepadDisplayArea(gamepad_elements, &area);
|
|
|
|
gamepad_type = CreateGamepadTypeDisplay(screen);
|
|
area.x = 0;
|
|
area.y = TITLE_HEIGHT;
|
|
area.w = PANEL_WIDTH;
|
|
area.h = GAMEPAD_HEIGHT;
|
|
SetGamepadTypeDisplayArea(gamepad_type, &area);
|
|
|
|
joystick_elements = CreateJoystickDisplay(screen);
|
|
area.x = PANEL_WIDTH + PANEL_SPACING + GAMEPAD_WIDTH + PANEL_SPACING;
|
|
area.y = TITLE_HEIGHT;
|
|
area.w = PANEL_WIDTH;
|
|
area.h = GAMEPAD_HEIGHT;
|
|
SetJoystickDisplayArea(joystick_elements, &area);
|
|
|
|
setup_mapping_button = CreateGamepadButton(screen, "Setup Mapping");
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(setup_mapping_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(setup_mapping_button) + 2 * BUTTON_PADDING;
|
|
area.x = BUTTON_MARGIN;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(setup_mapping_button, &area);
|
|
|
|
cancel_button = CreateGamepadButton(screen, "Cancel");
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(cancel_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(cancel_button) + 2 * BUTTON_PADDING;
|
|
area.x = BUTTON_MARGIN;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(cancel_button, &area);
|
|
|
|
clear_button = CreateGamepadButton(screen, "Clear");
|
|
area.x += area.w + BUTTON_PADDING;
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(clear_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(clear_button) + 2 * BUTTON_PADDING;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(clear_button, &area);
|
|
|
|
copy_button = CreateGamepadButton(screen, "Copy");
|
|
area.x += area.w + BUTTON_PADDING;
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(copy_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(copy_button) + 2 * BUTTON_PADDING;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(copy_button, &area);
|
|
|
|
paste_button = CreateGamepadButton(screen, "Paste");
|
|
area.x += area.w + BUTTON_PADDING;
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(paste_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(paste_button) + 2 * BUTTON_PADDING;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(paste_button, &area);
|
|
|
|
done_mapping_button = CreateGamepadButton(screen, "Done");
|
|
area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(done_mapping_button) + 2 * BUTTON_PADDING);
|
|
area.h = GetGamepadButtonLabelHeight(done_mapping_button) + 2 * BUTTON_PADDING;
|
|
area.x = SCREEN_WIDTH / 2 - area.w / 2;
|
|
area.y = SCREEN_HEIGHT - BUTTON_MARGIN - area.h;
|
|
SetGamepadButtonArea(done_mapping_button, &area);
|
|
|
|
/* Process the initial gamepad list */
|
|
loop(NULL);
|
|
|
|
if (gamepad_index < num_controllers) {
|
|
SetController(controllers[gamepad_index].id);
|
|
} else if (num_controllers > 0) {
|
|
SetController(controllers[0].id);
|
|
}
|
|
|
|
/* Loop, getting gamepad events! */
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_set_main_loop_arg(loop, NULL, 0, 1);
|
|
#else
|
|
while (!done) {
|
|
loop(NULL);
|
|
}
|
|
#endif
|
|
|
|
CloseVirtualGamepad();
|
|
while (num_controllers > 0) {
|
|
HandleGamepadRemoved(controllers[0].id);
|
|
DelController(controllers[0].id);
|
|
}
|
|
SDL_free(controllers);
|
|
SDL_free(controller_name);
|
|
DestroyGamepadImage(image);
|
|
DestroyGamepadDisplay(gamepad_elements);
|
|
DestroyGamepadTypeDisplay(gamepad_type);
|
|
DestroyJoystickDisplay(joystick_elements);
|
|
DestroyGamepadButton(setup_mapping_button);
|
|
DestroyGamepadButton(done_mapping_button);
|
|
DestroyGamepadButton(cancel_button);
|
|
DestroyGamepadButton(clear_button);
|
|
DestroyGamepadButton(copy_button);
|
|
DestroyGamepadButton(paste_button);
|
|
SDLTest_CleanupTextDrawing();
|
|
SDL_DestroyRenderer(screen);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
SDLTest_CommonDestroyState(state);
|
|
return 0;
|
|
}
|