Files
kaizen/test/gamepadutils.c
irisz64 b22c6238d5 Squashed 'external/SDL/' changes from 90fd2a3cbe..279dabfc96
279dabfc96 cocoa: Don't use trick of briefly focusing the Dock on newer macOS releases.
afa27243df Ignore sensor delta values that look like they're out of range
cd98b66114 Allow overriding SDL_FORK_MESSAGEBOX in build environment
8d5bf053f7 Fixed detecting 8BitDo sensor timestamp on older firmware
0ac1241b7a x11: Always update the borders on frame extent events
f40bf44928 Removed unnecessary include
6d3e8b749e add 8BitDo Controller sensor_timestamp (#13278)
727b4924c8 Update the viewport when logical presentation changes
de6a23028a Fix #13276: Crash in SDL_GetAudioDeviceChannelMap
8442d4f0f7 joystick: Add support Xbox Adaptive Controller
3cd979b353 SDL_render.h: enum SDL_TextureAddressMode: Removed trailing comma
db3a35e9bc joystick: Fix MSVC errors C2099 with `/fp:strict`
9ed83e71f6 Fixed memory leaks in KMSDRM property handling
603118c340 Sync SDL3 wiki -> header
aa4f916b71 Renamed SDL_PROP_AUDIOSTREAM_KEEP_ON_SHUTDOWN_BOOLEAN to SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN
6cfe211142 Sync SDL3 wiki -> header
274aa0242e audio: Let apps save an audio stream from destruction during SDL_Quit(). (#13244)
af8bee2dd1 alsa: change an SDL_LogError into SDL_LogDebug.
bbc674b9e7 test: Fix resource paths in testtray
796961acec Resolve bug for calibration Nintendo Switch Pro Controller (#13260)
3a6f9e01f8 Fixed Nintendo Switch thumbstick calibration
038a3806eb Fixed Nintendo Switch Pro thumbstick calibration
e6c2649afc Updated testffmpeg for ffmpeg 7.1
e80d084766 expose events originating from a live-resize set the data1 field to 1.
6aedc488d3 win32: Invalidate window message mouse button flags when reading buttons from raw input or GameInput
81e3066303 hidapi/libusb: disable C5287 warning in MSVC builds
7d9fd48557 alsa: recover from snd_pcm_avail() returning -EPIPE
e68f5ca99a alsa: use udev if available instead of a hotplug thread
051ce0ff89 alsa: fixed disconnecting the microphone when opened
5fcc83d93b Vita Render: Limit the scope of cliprect to viewport
1bd5110ff0 Vita: Fixed absence of clipping when viewport is set
eb04219efe audio: Enumerating audio devices will skip zombie devices still in the hash.
d06b6e42d2 Sync SDL3 wiki -> header
c19ad189dc Clarify that SDL_GetAudioStreamDevice() returns the logical device.
7882e60f0e Don't log an error for disconnected audio devices
e4e29b8601 Fix support for F21 to F24 scancodes on Linux
d7939abf42 Use consistent style for pointer declarations and casts
390fe65323 test: Fix a window parenting bug in testmodal
ca9b7c8ea3 video: Explicitly disallow setting the parent of a window to itself
c04624972e alsa: Simplify ALSA_WaitDevice.
b4ac5f43f5 include: Fixed copy/paste error in endian-specific SDL_Read function docs.
3896b1b3f4 wikiheaders: Let each subproject specify how to find their property symbols.
efed3c63b3 Added support for an alternate version of the Vader 4 Pro
d6bae53341 Added support for an alternate version of the Vader 4 Pro
f62c982bcf Reverted Accelerometer and Gyro displays to throttled display (10hz)
b833c618a9 Improved reliability detecting FlyDigi Vader controllers
0e262dfd44 Sync SDL3 wiki -> header
6c406dd122 include: a couple of documentation tweaks and typo fixes.
42c9fe119d Sync SDL3 wiki -> header
f5d1402c28 Flydigi Vader 4 Pro IMU rate correction (#13215)
1e886c8a2f 8 bitdo polling rate corrections (#13221)
913b611ccd Added tools for evaluating gyroscope accuracy and IMU polling rates. (#13209)
e2239c36d3 wayland: Ensure that the tablet tool list is always initialized to empty
ec1297199e wayland: Implement the pointer warp protocol
558a89fdb6 Read Switch controller gyro/accel sensitivity coeffs (SDL3)
fd4ffa6d27 Sync SDL3 wiki -> header
c5b1341757 Tweak Vulkan include guard check and 64-bit platform defines (#13210)
e18a6313de Added gyro support for the Flydigi Vader 3 Pro controller
71f479b4aa Fix a broken link inside README-cmake.md that's meant to send to README-linux.md also made said link more consistent with other links by adding .md suffix
f2bcfe3dd2 Correct the texture format used for the N-Gage (#13192)
685f1720fe Revert "[Nokia N-Gage] Fix alpha transparency in 4K color mode using BitBltMasked"
a89a20a9ab Fix Markdown typo in Arch Linux dependencies docs
cbc9d662ea [Nokia N-Gage] Add define to disable FPS counter by default.
d9af41b5ac cmake: more private definitions
5826966873 Fixed replacing existing specific gamepad mappings
f90a21483c Added support for the ZEROPLUS P4 Wired Gamepad
638acdc02a Remove the CRC from automatically generated gamepad mappings
45eb6310a8 x11: Resize fixed-size windows after mapping on xmonad
7dd5e765df joystick: Report battery on GIP controllers
c54a017f47 joystick: Clean up Elite Button handling
559efd58e2 joystick: Add headset stub to GIP driver
7cc3feeb1b keyboard: Search for the correct base key value when querying the keycode from a scancode
22fa45b3c1 win32: Ensure that text input is initially disabled when creating a window
c240ed976f Sync SDL3 wiki -> header
27464ffb08 Clarify that icon and cursor alternate images are added with SDL_AddSurfaceAlternateImage()
2ef7944170 [Nokia N-Gage] Fix alpha transparency in 4K color mode using BitBltMasked
d86fb8a83a gdk: Ignore focus loss events caused by text input showing the OSK
6622f4e1ea Added support for the NACON Revolution X Unlimited controller on macOS
45aa497813 [Nokia N-Gage] Fix SDL_GetPerformanceFrequency and SDL_GetPerformanceCounter
7457857304 Fixed input from the MayFlash GameCube adapter with version 7 firmware
1ec12b38e5 Sync SDL3 wiki -> header
b6c4a46b85 workflows: Use stable Steam Runtime 3 on both x86_64 and arm64
fbba5b272a GPU: Binding validation and prevent null dereference if expected binding is missing (#13164)
cf6c42e6e6 Use HEAPU8.set rather than Module.HEAPU8.set (thanks @sbc100!)
4b0f48c4cf Mark gamepads as invalid if they can't be opened
a314a58e7e Sync SDL3 wiki -> header
d283f4651c Sync SDL3 wiki -> header
8510331f66 .wikiheaders-options: Add Tray to quickreference categories.
b8187e2abd wikiheaders: Trim whitespace from end of lines in section headers.
9e0d9f30a7 x11: Be a little less aggressive with Xinput2IsInitialized checks.
57b6e6c7f9 Checks if xinput is loaded before trying to call xinput functions
ac3ab026fe audio: corrected comment about device format minimums.
83cc3bc234 audio: Opened device spec must be >= simple minimums, not device's defaults.
14a4ae521a pulseaudio: Request more recording data per-fragment.
3a4de2ad89 Sync SDL3 wiki -> header
47717f22fd cocoa: Always add a window-sized NSTrackingArea to each window.
f61d956a04 cocoa: add explicit tracking areas to the window.
58afb0d8ae wayland: Update xkbcommon names and types
c764e8864b wayland: Update xkbcommon to avoid using deprecated modifier names
b61586b492 [Nokia N-Gage] Increase max. texture size from 256 to 1024; the previous setting was chosen at random and does not necessarily make sense in practice.
329b5f27d6 fix preprocessor directive for SDL_RESTRICT definition in C99 case.
0d58594e66 fix inverted __STDC_VERSION__ condition in SDL_RESTRICT definition.
6a686185f7 SDL_begin_code.h: Actually define SDL_RESTRICT.
51dfca813b Pass text input rect to steam deck keyboard invocation
520d73ae57 include/SDL_gpu: fix bool members documentation indentation
a26e5f32e0 Gyro degrees per second is 2000 across all 8bitdo controllers (#13147)
fc1c0618de kmsdrm: fixed creating GBM surfaces on NVIDIA cards
03a6d98aee Update SDL_pixels.h
885e611f3c Added macros to push/pop error messages while cleaning up
61d105247e Fixed typos
7db0ac7380 Updated SDL_StretchSurface() documentation
ca988dbc2c Bail out if Xrandr returns zero CRTCs in the X11 messagebox implementation
c147ddf3e3 Do not use Xrandr if the extension is not queryable in the X11 messagebox implementation
df07c09f55 Use visuals and colormapsin the X11 messagebox implementation
737b9e117d Removed obsolete documentation
d2be547642 Removed spurious parentheses
36936cbf37 Set the serial number to the Bluetooth MAC address for 8BitDo controllers
f80e819c5b Sync SDL3 wiki -> header
b7082e7e18 Sync SDL3 wiki -> header
3940c660be begin_code: Added SDL_ALIGNED macro.
aa870d511e Disable SDL_HINT_JOYSTICK_RAWINPUT by default
db972604a8 gpu: Xbox buildfix
8a2e2e75d1 Simplified loop logic
b4f7948410 Ensure that report 06 command reads normally
e3d44cdd51 wayland: Use raw timestamps to calculate the elapsed repeat time on a key up event
b51273512b Sync SDL3 wiki -> header
988c0be934 Sync SDL3 wiki -> header
9b025e3cab PSP fullscreen is the only mode (#13125)
08b6f6175a Sync SDL3 wiki -> header
5e50d39b38 audio: Added SDL_PutAudioStreamDataNoCopy.
25db127450 SDL_video.c: remove two stray line continuation chars .
168d1a9253 emscripten: Proxy Emscripten_GetSystemTheme and EMSCRIPTENAUDIO_OpenDevice to the main thread.
f62572344f Clean up INTERFACE_COMPILE_OPTIONS
b70919ecd9 x11: Assume the window was mapped after showing
0657ece55d x11: Always send fullscreen dimensions except on XWayland
1f6b5c681d Set the initial axis values for HIDAPI and XInput controllers
fdc4f8fa39 x11: Include the XTest header when needed
2b4d61e4a6 VITA: support only fullscreen windows. Fixes #13079
4cd889cb13 joystick: Several minor GIP fixes
0a7548230c Sync SDL3 wiki -> header
c9cf4c42e0 Improved HIDAPI support for Flydigi controllers
ead32c706d fix error handling in WideCharToMultiByte
8ddb074889 bugfix advancing UTF-8 length in UTF-16 string
dcdbc0bf9f README-documentation-rules: Add a note about being UTF-8 only!
56bb73a8d2 README-ngage.md: Really actually got all the non-UTF-8 characters this time!
ecef0d3564 README-ngage.md: Found a few more non-UTF-8 things.
fb7afabbd6 README-ngage.md: Removed non-UTF-8 character.
73ca59b423 Sync SDL3 wiki -> header
136a44b0bd Sync SDL3 wiki -> header
7ae64592c9 Restore support for the Nokia N-Gage (#12148)
26f9940f82 Sync SDL3 wiki -> header
6f994cc7a8 Fixed macOS build
354895d975 Add Flydigi VADER 4 Pro HIDAPI support (#12874)
8d9a4fe843 stdinc: Corrected documentation for SDL_atan2 and SDL_atan2f.
abcfa1b7de wayland: Use SDL_memcpy instead of SDL_copyp to copy the repeated text string
22828d5f2a Fix #13083 segfault in `SDL_RemoveTrayEntry()` for submenu entries
264eb8d440 Fixed the Bluetooth flag for the combined Joy-Con controller
05f779f61e wayland: Cap the max key repeat elapsed time
20e8ac0075 wayland: Fix keymap changed event spam with non-latin keyboard layouts
b8e055ce64 use SDL style - else on same line as closing brace
8e22194217 get preferred locales on android
c08b1049d3 gpu/d3d12: Acknowledge that we've bound vertex buffers (#13088)
c89357bf60 Fix wrong callback type
f4942b3eae GPU: Update D3D12 to create multisample textures with default MSAA alignment
c7549eb0b6 wayland: Set the text input cursor rect properly
3be57ec774 docs: fix man page return value section generation
2dad6534b8 GPU: Add missing compute-writeable texture formats
992e4c59bd Fixed rare crash trying to interrupt SDL_WaitEvent()
4db8fe2f9b Sync SDL3 wiki -> header
6c61a94a4b wayland: Don't add the nanosecond timestamp offset to the pre-conversion millisecond value
25f2376e79 Fixed touch not being delivered as mouse events by default on Vita
afd1e51023 Fix: GameCube controller adapter hotplug not working
510126ee63 gpu: Check shader format support in PrepareDriver
8289656a4e GPU: Update to set supported shader formats inside CreateDevice
514d96de07 Sync SDL3 wiki -> header
d6a99752b7 Fix properties name in SDL_CreateGPUDeviceWithProperties
3dceb728b7 Suppress spammy gamepad and joystick update events
b0a282e31f wayland: Enable relative pointer mode based on the window flag
968222e74f Fix #13057 - fixes bug with NSEventTypeMouseMoved having a NULL window causing us to suppress future mouse move events because the window was considered out of focus.
d16371b923 Fix reference to nonexistent "README-3ds.md"
945eb6dc87 MacOS: fix cocoa clipboard text
b08d79b832 GPU: Check that a texture format is valid for compute writes (#13044)
604c192154 GPU: Always return NULL if beginning a pass fails an assert check
252129f433 GPU: Debug mode layer and level index checks
db154c8b9b Added HIDAPI mapping for the 8BitDo SF30 Pro
ec685e87fd Clarify logic in UpdateLogicalPresentation()
8aa5b97bb5 renderer: Always use the output size when updating the main view
1f7aa16eae wikiheaders: Man pages should escape apostrophe chars.
cf62637261 joystick: Add support for Xbox One Chatpad attachment
83cbf7f811 Sync SDL3 wiki -> header
f85f83ec7c SDL GPU: Implemented opt out Vulkan device features (#13016)
1eeffc5933 Revert "x11: Send key events for dead keys consumed by the IME"
3304d24bea Revert "x11: Filter out duplicate key presses when an IME is active"
cd95152b2c Fixed crash if out of memory in the Vulkan GPU driver
70b2d162e3 audio: Assert that all devices from device_hash are the appropriate type
83d4dce697 wayland: Remove all window references from seats when destroying a window
0a34279578 audio: Fix SDL_GetAudioDeviceName() not working with logical devices.
e2f7c4046c Sync SDL3 wiki -> header
f6c1e81394 [Process API] Quoting enhancements (#12946)
9a6f70d75a tray: Fix wrong `fByPositon` parameter of SetMenuItemInfoW in SDL_SetTrayEntryLabel
38da39c8c9 Added rightx and righty
2ae3418260 GPU Vulkan: set correct destination usage mode for storage buffer read/write bindings (#13009)
c6e9d6cc79 AAudio: Implemented sample frames hint
aaa5d70efc wayland: Check the cursor visibility flag when updating seat pointers
e1066ceea1 Revert "pulseaudio: cleanup TLS every time we finish a threaded-mainloop callback."
84308e7fba x11: Fix the Openbox quirk flag
6344712b04 GPU Vulkan: fix for Swapchain Semaphore Reuse
3f2226a917 Add progress bar support for Linux
e90f7ac4a8 Add hid_version and hid_version_str to renamed LIBUSB impl symbols
f92843da83 Sync SDL3 wiki -> header
86b206dadf GPU: Special case to avoid assert on GenerateMipmaps (#12995)
7b3bd8c538 joystick: Improve GIP metadata retry
367cf9ba63 joystick: Always continue processing GIP packets
955a49c883 joystick: Add quirk for no impulse vibration on the Spectra Pro
82899501b5 joystick: Fix GIP fast ACK issue
9e0edea16f joystick: Assume GIP gamepads have the LED command if no metadata is present
9a96960728 joystick: Sort out GIP vendor messages into the supported interfaces
72dd79752e joystick: Add initial support for GIP flight sticks
2248d3812e joystick: Rework GIP code to allow separate states for individual attachments
87fe9ef79b Fixed crash if WGI isn't correlated in RAWINPUT_JoystickRumble()
29d2116495 Define illegal_instruction() when it will be actually used
2aa0957081 Fixed 8BitDo Ultimate 2 Wireless controller on macOS
ad55597715 ci: Update Steam Linux Runtime tasks.
36c3a7a5e2 ci: Add slrsniper-arm64 task
8690a9ab54 Prefer the HIDAPI driver for 8BitDo controllers on macOS
2b57d58f7d Added GCController mapping for the 8BitDo SN30 Pro on macOS
d157600d3d Added GCController mapping for the 8BitDo Pro 2 on macOS
09c8d4b556 Sync SDL3 wiki -> header
37b86a6d2f windows: fix Unicode function and type inconsistencies
d9e58baa6d fixed the  field of SDL_MouseMotionTransformCallback
e7a765d648 Replace SDL_free with delete for new-allocated objects
5bee85408c Cleanup 8BitDo HIDAPI support for SF30 Pro and SN30 Pro
89a8cf2505 Sync SDL3 wiki -> header
6b048f59d7 fix #12963
2b3c481215 add 8BitDo Controller (#12964)
ca47dc59a9 Fixed building with the OpenVR video driver
195ad85ba4 Fixed typo
f4813ca2cf x11: Filter out duplicate key presses when an IME is active
8e1f4bafb4 [emscripten] Remove referenc to Module['createContext']
1dbb813316 VITA: fix audio playback
20f783532b Sync SDL3 wiki -> header
bbd973c8d2 Sync SDL3 wiki -> header
71303b41bf Sync SDL3 wiki -> header
a7344206e8 wikiheaders: Patched to compile.
06c2f9fcfc wikiheaders: Correct wiki README digest links.
ad46394e82 wikiheaders: README.md should be READMEs.md
2c97a48c51 wikiheaders: READMEs go in the base dir of the wiki now.
92a5417a98 docs: Tweak some minor things to keep wikiheaders happy.
d357aa29a3 Fixed initializing the OpenVR driver
f8c77908ad Use motor sequence ID 0 in the HIDAPI GIP driver
33e5f4885a x11: Don't update grab on enter when the mouse is captured
1abac3ccc3 Revert "x11: Better handle XInput2 mouse tracking outside the window"
85d2345bd8 Sync SDL3 wiki -> header
5bd886519b Sync SDL3 wiki -> header
c6362b4788 tests: Revert some leftover testing code
c91f9f6968 x11: #ifdef the XRandR path in the message box code
7490471796 cpuinfo: Use auxv for AltiVec on Linux if possible
17bba029ba don't prototype strdup() for __clang_analyzer__ case in windows builds.
15c6acf74f capture g27 shifter r input
795d1ae1fb testmessage: Don't push the event if "Retry" was chosen.
d73fe0bc53 win32: Hide the borders when showing a fullscreen window
ab12b7cbba Added support for the share button on the GameSir-K1 FLUX controller
1e6a25324c Added support for the PowerA Battle Dragon Advanced Wireless Controller
57346f2ba8 dialog: Cocoa backend should reactivate the app after the modal dialog.
caf269ce00 Added a link to the GIP documentation
34c045aa39 Fixed paddles on the Xbox Elite controller using the GIP driver
0447c2f3c3 events: Add integer wheel fields for sdl2-compat
ae251a05be Fixed warning: no previous prototype for function 'GIP_ParseMetadata'
193b0c8963 uikit: Use SDL_RunOnMainThread instead of dispatch_sync for message boxes.
691cc5bb5e dialog: Cocoa shouldn't crash if there's a '.' in the filters.
4ef8b6ce1b X11: Center Message Box on Multi Monitor Displays (#12819)
c5d5967c3a video: Store the preferred fullscreen display in a property for sdl2-compat
11e1d4a145 Sync SDL3 wiki -> header
00f3a82ada Joystick: Add new GIP driver to replace old Xbox One wired driver
48dfc03a87 Added the gamepad hint SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS
21a7bbbf14 Restore compatibility with older GameCube mappings
3730128e33 Simplify WIN_CreateHCursor (#12933)
6a0505c090 wayland: Remove unnecessary function call
fdd8b5d630 wayland: Query the mouse global button states from the seats
e3df61b070 Fixed right mouse button emulation when using a pen
106ccc722e Fixed missing simulated mouse events using a Wacom tablet
8ae962c904 Fixed build
e04064350f Fixed right click mouse emulation for the Wacom tablet
a163257295 GPU: Validate that textures are not bound for both read and write on render passes (#12925)
ea67133e4f pen: Windows can't check WM_POINTER[DOWN|UP] for touches directly.
f131791005 Sync SDL3 wiki -> header
e1a41c1c97 GPU Vulkan: Add locks for layout object lookups (#12924)
fd5380ffc6 Sync SDL3 wiki -> header
b871ac0d97 Add support for non-constrained and non-grabbing popups
8abcc27535 Removed timeout in SDL_RunOnMainThread()
ef54c3bf18 Always enable D3D12 GPU on Windows
39d3148185 events: Fix undefined behavior when disabling some event types
510c7edd9b migrate usage of SetCursor(NULL) to RedrawCursor()
4779499048 Split the redraw effect to own function
6c172e5220 SDL_SetCursor should not skip focus check
cbf44700d8 bytepusher example: always render text to render target and remove unused variable
faddff1bcc Sync SDL3 wiki -> header
31650d566c Added SDL_GAMEPAD_TYPE_GAMECUBE
c4d5cc358f streamline boolean logic
441e7e488f invert cursor_visible to hide_cursor
d553372682 rename cursor_shown to cursor_visible
016ba86f93 tray, unix: make pointers g_object_ref and g_object_ref_sink static
6f5892e543 cocoa: Immediately update the mouse focus when showing/hiding a popup menu
8c733d1f7b x11: Better handle XInput2 mouse tracking outside the window
0dbf585ace fix SDL_audiocvt.c MSVC build.
af0972c33f audio: SDL_PutAudioStreamPlanarData should take a channel count.
5f03cb3882 d3d12: Patched to compile with GDK builds.
360cc2791f test/msdf_font.bmp: mark as non-executable.
34fe967f3e Sync SDL3 wiki -> header
22692e308f events: Added SDL_GetEventDescription().
c2ed58db7b windows: Fix stale zoom/iconic state in WM_WINDOWPOSCHANGED handler
14ae45c2b8 GDK: Handle ProjectDir with space
faa2e40406 gpu: Warn about Direct3D 12 texture alignment requirements.
2e45198299 Sync SDL3 wiki -> header
608f706a95 audio: Added SDL_SetAudioIterationCallbacks().
18a86ea6bb testaudio: Use SDL_SetAppMetadata at startup.
a9bee3c0bf pipewire: Use a more specific stream name than "Audio Stream".
2767c1a440 aaudio: We PlayDevice first and WaitDevice after; reduce semaphore count by 1.
7642c0468d Log information about Wayland environment detection
5b1e92ae88 cmake: use CMAKE_ANDROID_NDK variable as root of Android ndk
09b2aae47e cocoa: Popup menus always accept first click
29ef31d589 Sync SDL3 wiki -> header
15cb9bd610 Sync SDL3 wiki -> header
b28449a58c audio: Tweak SDL_GetAudioDeviceName.
da3c864d4c Reset the keyboard when entering a modal loop on Windows
a82f70dc21 use GetMessagePos instead of GetCursorPos
bfbeaca29f inline WIN_UpdateMouseCapture
de2c5c33b7 SDL_hidapi_steamdeck: report touch controller events
5cacdf2513 Fix crash in UIKit indirect pointer handling
32bbabe2a5 Add Padix Rockfire devices (#12884)
7e1d4f843c Emscripten: fix incorrect error check for WebGL context creation
fcdaff4110 Fixed compiler warning
dd625a6763 defer clipcursor refresh unto pump finish
c84c2aa2c4 pass ground-truth cursor pos to WIN_UpdateFocus
11a3296a42 x11: Handle size/position events arriving before state events
d50520462a Add and use SDL_HapticEffectID typedef
fb940fff80 audio: Fix warning C4701: potentially uninitialized local variable 'isstack' used
e8bd9cc150 audio: SDL_GetAudioDeviceName() doesn't need a full device lock.
3343cb2147 gpu: rework alpha-to-coverage validation
ceb9fecfc1 Joystick: Add trigger rumble resend
c81b62293a GPU D3D12 - Update to use typeless formats for depth buffer (#12701)
27b256022b SDL_GetRectIntersectionFloat(): Allow rendering zero-sized srcrect
d04b28926c GPU: Make D3D12 debug layers optional
695cad459b Corrected documentation for SDL_SetGPUSwapchainParameters()
2b16c961ea Add and use typedefs SDL_HapticEffectType and SDL_HapticDirectionType
a1332d77f6 wayland: Cleanup timestamp handling
b5ed0d0138 stb_image.h: apply mainstream PR/1736
e3181a0bcd Sync SDL3 wiki -> header
e3507b390e examples: added audio/05-planar-data
3905aa0587 audio: Added SDL_PutAudioStreamPlanarData.
483d1c36ed comments: Fixed a few typos I ran into in the audio code.
727a5fae59 Sync SDL3 wiki -> header
5a8be8e4c2 Fixed formatting
6e4ace310c GPU: Validate shader bytecode
89d7d406ab Added tests to cover invalid surface blits
fe849f1572 SDL_BlitSurfaceScaled(): Do not divide by zero
8017d38adc SDL_BlitSurfaceScaled(): Do not blit if surfaces have no pixel data
18fbe6a92f Renamed SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_STENCIL_UINT8 to SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_STENCIL_NUMBER
6a6917b0df Sync SDL3 wiki -> header
e70a49892b Add missing type suffix to SDL_PROP_WINDOW_OPENVR_OVERLAY_ID
a696b108ac SDL_BlitSurface() comment: Remove sentence about final blit rect being stored in srcrect and dstrect
c076b0d17f Removed incorrect documentation for SDL_RenderViewportSet()
47162a4168 x11: Send key events for dead keys consumed by the IME
3aed80cd82 Updated documentation for SDL_HINT_GPU_DRIVER
86b3369491 wayland: Check the origin of clipboard offers before forwarding them to the client
23a7a74133 Sync SDL3 wiki -> header
3be0ec5631 Sync SDL3 wiki -> header
5bd1578a45 wayland: Account for timer rollover when calculating the elapsed time for key repeats.
10f51da28d cocoa: Use CGFLOAT_MAX for unbounded max window dimensions
3519a59d8f The SDL_BUTTON_*MASK defines must come immediately after the SDL_MouseButtonFlags typedef to be associated
8e0b39f465 Sync SDL3 wiki -> header
9da46bc37f win32: Check internal data when positioning child windows

git-subtree-dir: external/SDL
git-subtree-split: 279dabfc96631965f8dad2e39d57daa4b9130d24
2025-06-26 22:23:47 +02:00

3585 lines
115 KiB
C

/*
Copyright (C) 1997-2025 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.
*/
#include <SDL3/SDL.h>
#include <SDL3/SDL_test_font.h>
#include "gamepadutils.h"
#include "gamepad_front.h"
#include "gamepad_back.h"
#include "gamepad_face_abxy.h"
#include "gamepad_face_axby.h"
#include "gamepad_face_bayx.h"
#include "gamepad_face_sony.h"
#include "gamepad_battery.h"
#include "gamepad_battery_wired.h"
#include "gamepad_touchpad.h"
#include "gamepad_button.h"
#include "gamepad_button_small.h"
#include "gamepad_axis.h"
#include "gamepad_axis_arrow.h"
#include "gamepad_button_background.h"
#include "gamepad_wired.h"
#include "gamepad_wireless.h"
#include <limits.h>
#define RAD_TO_DEG (180.0f / SDL_PI_F)
/* Used to draw a 3D cube to represent the gyroscope orientation */
typedef struct
{
float x, y, z;
} Vector3;
struct Quaternion
{
float x, y, z, w;
};
static const Vector3 debug_cube_vertices[] = {
{ -1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f },
{ 1.0f, 1.0f, -1.0f },
{ -1.0f, 1.0f, -1.0f },
{ -1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f },
};
static const int debug_cube_edges[][2] = {
{ 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, /* bottom square */
{ 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, /* top square */
{ 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 }, /* verticals */
};
static Vector3 RotateVectorByQuaternion(const Vector3 *v, const Quaternion *q) {
/* v' = q * v * q^-1 */
float x = v->x, y = v->y, z = v->z;
float qx = q->x, qy = q->y, qz = q->z, qw = q->w;
/* Calculate quaternion *vector */
float ix = qw * x + qy * z - qz * y;
float iy = qw * y + qz * x - qx * z;
float iz = qw * z + qx * y - qy * x;
float iw = -qx * x - qy * y - qz * z;
/* Result = result * conjugate(q) */
Vector3 out;
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return out;
}
#ifdef GYRO_ISOMETRIC_PROJECTION
static SDL_FPoint ProjectVec3ToRect(const Vector3 *v, const SDL_FRect *rect)
{
SDL_FPoint out;
/* Simple orthographic projection using X and Y; scale to fit into rect */
out.x = rect->x + (rect->w / 2.0f) + (v->x * (rect->w / 2.0f));
out.y = rect->y + (rect->h / 2.0f) - (v->y * (rect->h / 2.0f)); /* Y inverted */
return out;
}
#else
static SDL_FPoint ProjectVec3ToRect(const Vector3 *v, const SDL_FRect *rect)
{
const float verticalFOV_deg = 40.0f;
const float cameraZ = 4.0f; /* Camera is at(0, 0, +4), looking toward origin */
float aspect = rect->w / rect->h;
float fovScaleY = SDL_tanf((verticalFOV_deg * SDL_PI_F / 180.0f) * 0.5f);
float fovScaleX = fovScaleY * aspect;
float relZ = cameraZ - v->z;
if (relZ < 0.01f)
relZ = 0.01f; /* Prevent division by 0 or negative depth */
float ndc_x = (v->x / relZ) / fovScaleX;
float ndc_y = (v->y / relZ) / fovScaleY;
/* Convert to screen space */
SDL_FPoint out;
out.x = rect->x + (rect->w / 2.0f) + (ndc_x * rect->w / 2.0f);
out.y = rect->y + (rect->h / 2.0f) - (ndc_y * rect->h / 2.0f); /* flip Y */
return out;
}
#endif
void DrawGyroDebugCube(SDL_Renderer *renderer, const Quaternion *orientation, const SDL_FRect *rect)
{
SDL_FPoint projected[8];
int i;
for (i = 0; i < 8; ++i) {
Vector3 rotated = RotateVectorByQuaternion(&debug_cube_vertices[i], orientation);
projected[i] = ProjectVec3ToRect(&rotated, rect);
}
for (i = 0; i < 12; ++i) {
const SDL_FPoint p0 = projected[debug_cube_edges[i][0]];
const SDL_FPoint p1 = projected[debug_cube_edges[i][1]];
SDL_RenderLine(renderer, p0.x, p0.y, p1.x, p1.y);
}
}
#define CIRCLE_SEGMENTS 64
static Vector3 kCirclePoints3D_XY_Plane[CIRCLE_SEGMENTS];
static Vector3 kCirclePoints3D_XZ_Plane[CIRCLE_SEGMENTS];
static Vector3 kCirclePoints3D_YZ_Plane[CIRCLE_SEGMENTS];
void InitCirclePoints3D(void)
{
int i;
for (i = 0; i < CIRCLE_SEGMENTS; ++i) {
float theta = ((float)i / CIRCLE_SEGMENTS) * SDL_PI_F * 2.0f;
kCirclePoints3D_XY_Plane[i].x = SDL_cosf(theta);
kCirclePoints3D_XY_Plane[i].y = SDL_sinf(theta);
kCirclePoints3D_XY_Plane[i].z = 0.0f;
}
for (i = 0; i < CIRCLE_SEGMENTS; ++i) {
float theta = ((float)i / CIRCLE_SEGMENTS) * SDL_PI_F * 2.0f;
kCirclePoints3D_XZ_Plane[i].x = SDL_cosf(theta);
kCirclePoints3D_XZ_Plane[i].y = 0.0f;
kCirclePoints3D_XZ_Plane[i].z = SDL_sinf(theta);
}
for (i = 0; i < CIRCLE_SEGMENTS; ++i) {
float theta = ((float)i / CIRCLE_SEGMENTS) * SDL_PI_F * 2.0f;
kCirclePoints3D_YZ_Plane[i].x = 0.0f;
kCirclePoints3D_YZ_Plane[i].y = SDL_cosf(theta);
kCirclePoints3D_YZ_Plane[i].z = SDL_sinf(theta);
}
}
void DrawGyroCircle(
SDL_Renderer *renderer,
const Vector3 *circlePoints,
int numSegments,
const Quaternion *orientation,
const SDL_FRect *bounds,
Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FPoint lastScreenPt = { 0 };
bool hasLast = false;
int i;
for (i = 0; i <= numSegments; ++i) {
int index = i % numSegments;
Vector3 rotated = RotateVectorByQuaternion(&circlePoints[index], orientation);
SDL_FPoint screenPtVec2 = ProjectVec3ToRect(&rotated, bounds);
SDL_FPoint screenPt;
screenPt.x = screenPtVec2.x;
screenPt.y = screenPtVec2.y;
if (hasLast) {
SDL_RenderLine(renderer, lastScreenPt.x, lastScreenPt.y, screenPt.x, screenPt.y);
}
lastScreenPt = screenPt;
hasLast = true;
}
}
void DrawGyroDebugCircle(SDL_Renderer *renderer, const Quaternion *orientation, const SDL_FRect *bounds)
{
/* Store current color */
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
DrawGyroCircle(renderer, kCirclePoints3D_YZ_Plane, CIRCLE_SEGMENTS, orientation, bounds, GYRO_COLOR_RED); /* X axis - pitch */
DrawGyroCircle(renderer, kCirclePoints3D_XZ_Plane, CIRCLE_SEGMENTS, orientation, bounds, GYRO_COLOR_GREEN); /* Y axis - yaw */
DrawGyroCircle(renderer, kCirclePoints3D_XY_Plane, CIRCLE_SEGMENTS, orientation, bounds, GYRO_COLOR_BLUE); /* Z axis - Roll */
/* Restore current color */
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
void DrawAccelerometerDebugArrow(SDL_Renderer *renderer, const Quaternion *gyro_quaternion, const float *accel_data, const SDL_FRect *bounds)
{
/* Store current color */
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
const float flGravity = 9.81f;
Vector3 vAccel;
vAccel.x = accel_data[0] / flGravity;
vAccel.y = accel_data[1] / flGravity;
vAccel.z = accel_data[2] / flGravity;
Vector3 origin = { 0.0f, 0.0f, 0.0f };
Vector3 rotated_accel = RotateVectorByQuaternion(&vAccel, gyro_quaternion);
/* Project the origin and rotated vector to screen space */
SDL_FPoint origin_screen = ProjectVec3ToRect(&origin, bounds);
SDL_FPoint accel_screen = ProjectVec3ToRect(&rotated_accel, bounds);
/* Draw the line from origin to the rotated accelerometer vector */
SDL_SetRenderDrawColor(renderer, GYRO_COLOR_ORANGE);
SDL_RenderLine(renderer, origin_screen.x, origin_screen.y, accel_screen.x, accel_screen.y);
const float head_width = 4.0f;
SDL_FRect arrow_head_rect;
arrow_head_rect.x = accel_screen.x - head_width * 0.5f;
arrow_head_rect.y = accel_screen.y - head_width * 0.5f;
arrow_head_rect.w = head_width;
arrow_head_rect.h = head_width;
SDL_RenderRect(renderer, &arrow_head_rect);
/* Restore current color */
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
/* This is indexed by gamepad element */
static const struct
{
int x;
int y;
} button_positions[] = {
{ 413, 190 }, /* SDL_GAMEPAD_BUTTON_SOUTH */
{ 456, 156 }, /* SDL_GAMEPAD_BUTTON_EAST */
{ 372, 159 }, /* SDL_GAMEPAD_BUTTON_WEST */
{ 415, 127 }, /* SDL_GAMEPAD_BUTTON_NORTH */
{ 199, 157 }, /* SDL_GAMEPAD_BUTTON_BACK */
{ 257, 153 }, /* SDL_GAMEPAD_BUTTON_GUIDE */
{ 314, 157 }, /* SDL_GAMEPAD_BUTTON_START */
{ 98, 177 }, /* SDL_GAMEPAD_BUTTON_LEFT_STICK */
{ 331, 254 }, /* SDL_GAMEPAD_BUTTON_RIGHT_STICK */
{ 102, 65 }, /* SDL_GAMEPAD_BUTTON_LEFT_SHOULDER */
{ 421, 61 }, /* SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER */
{ 179, 213 }, /* SDL_GAMEPAD_BUTTON_DPAD_UP */
{ 179, 274 }, /* SDL_GAMEPAD_BUTTON_DPAD_DOWN */
{ 141, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
{ 211, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
{ 257, 199 }, /* SDL_GAMEPAD_BUTTON_MISC1 */
{ 157, 160 }, /* SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1 */
{ 355, 160 }, /* SDL_GAMEPAD_BUTTON_LEFT_PADDLE1 */
{ 157, 200 }, /* SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2 */
{ 355, 200 }, /* SDL_GAMEPAD_BUTTON_LEFT_PADDLE2 */
};
/* This is indexed by gamepad element */
static const struct
{
int x;
int y;
double angle;
} axis_positions[] = {
{ 99, 178, 270.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE */
{ 99, 178, 90.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE */
{ 99, 178, 0.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE */
{ 99, 178, 180.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE */
{ 331, 256, 270.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE */
{ 331, 256, 90.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE */
{ 331, 256, 0.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE */
{ 331, 256, 180.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE */
{ 116, 5, 180.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER */
{ 400, 5, 180.0 }, /* SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER */
};
static SDL_FRect touchpad_area = {
148.0f, 20.0f, 216.0f, 118.0f
};
typedef struct
{
bool down;
float x;
float y;
float pressure;
} GamepadTouchpadFinger;
struct GamepadImage
{
SDL_Renderer *renderer;
SDL_Texture *front_texture;
SDL_Texture *back_texture;
SDL_Texture *face_abxy_texture;
SDL_Texture *face_axby_texture;
SDL_Texture *face_bayx_texture;
SDL_Texture *face_sony_texture;
SDL_Texture *connection_texture[2];
SDL_Texture *battery_texture[2];
SDL_Texture *touchpad_texture;
SDL_Texture *button_texture;
SDL_Texture *axis_texture;
float gamepad_width;
float gamepad_height;
float face_width;
float face_height;
float connection_width;
float connection_height;
float battery_width;
float battery_height;
float touchpad_width;
float touchpad_height;
float button_width;
float button_height;
float axis_width;
float axis_height;
float x;
float y;
bool showing_front;
bool showing_touchpad;
SDL_GamepadType type;
ControllerDisplayMode display_mode;
bool elements[SDL_GAMEPAD_ELEMENT_MAX];
SDL_JoystickConnectionState connection_state;
SDL_PowerState battery_state;
int battery_percent;
int num_fingers;
GamepadTouchpadFinger *fingers;
};
static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, unsigned int len)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadBMP_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
}
}
return texture;
}
GamepadImage *CreateGamepadImage(SDL_Renderer *renderer)
{
GamepadImage *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->front_texture = CreateTexture(renderer, gamepad_front_bmp, gamepad_front_bmp_len);
ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len);
SDL_GetTextureSize(ctx->front_texture, &ctx->gamepad_width, &ctx->gamepad_height);
ctx->face_abxy_texture = CreateTexture(renderer, gamepad_face_abxy_bmp, gamepad_face_abxy_bmp_len);
ctx->face_axby_texture = CreateTexture(renderer, gamepad_face_axby_bmp, gamepad_face_axby_bmp_len);
ctx->face_bayx_texture = CreateTexture(renderer, gamepad_face_bayx_bmp, gamepad_face_bayx_bmp_len);
ctx->face_sony_texture = CreateTexture(renderer, gamepad_face_sony_bmp, gamepad_face_sony_bmp_len);
SDL_GetTextureSize(ctx->face_abxy_texture, &ctx->face_width, &ctx->face_height);
ctx->connection_texture[0] = CreateTexture(renderer, gamepad_wired_bmp, gamepad_wired_bmp_len);
ctx->connection_texture[1] = CreateTexture(renderer, gamepad_wireless_bmp, gamepad_wireless_bmp_len);
SDL_GetTextureSize(ctx->connection_texture[0], &ctx->connection_width, &ctx->connection_height);
ctx->battery_texture[0] = CreateTexture(renderer, gamepad_battery_bmp, gamepad_battery_bmp_len);
ctx->battery_texture[1] = CreateTexture(renderer, gamepad_battery_wired_bmp, gamepad_battery_wired_bmp_len);
SDL_GetTextureSize(ctx->battery_texture[0], &ctx->battery_width, &ctx->battery_height);
ctx->touchpad_texture = CreateTexture(renderer, gamepad_touchpad_bmp, gamepad_touchpad_bmp_len);
SDL_GetTextureSize(ctx->touchpad_texture, &ctx->touchpad_width, &ctx->touchpad_height);
ctx->button_texture = CreateTexture(renderer, gamepad_button_bmp, gamepad_button_bmp_len);
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
ctx->axis_texture = CreateTexture(renderer, gamepad_axis_bmp, gamepad_axis_bmp_len);
SDL_GetTextureSize(ctx->axis_texture, &ctx->axis_width, &ctx->axis_height);
SDL_SetTextureColorMod(ctx->axis_texture, 10, 255, 21);
ctx->showing_front = true;
}
return ctx;
}
void SetGamepadImagePosition(GamepadImage *ctx, float x, float y)
{
if (!ctx) {
return;
}
ctx->x = x;
ctx->y = y;
}
void GetGamepadImageArea(GamepadImage *ctx, SDL_FRect *area)
{
if (!ctx) {
SDL_zerop(area);
return;
}
area->x = ctx->x;
area->y = ctx->y;
area->w = ctx->gamepad_width;
area->h = ctx->gamepad_height;
if (ctx->showing_touchpad) {
area->h += ctx->touchpad_height;
}
}
void GetGamepadTouchpadArea(GamepadImage *ctx, SDL_FRect *area)
{
if (!ctx) {
SDL_zerop(area);
return;
}
area->x = ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2 + touchpad_area.x;
area->y = ctx->y + ctx->gamepad_height + touchpad_area.y;
area->w = touchpad_area.w;
area->h = touchpad_area.h;
}
void SetGamepadImageShowingFront(GamepadImage *ctx, bool showing_front)
{
if (!ctx) {
return;
}
ctx->showing_front = showing_front;
}
SDL_GamepadType GetGamepadImageType(GamepadImage *ctx)
{
if (!ctx) {
return SDL_GAMEPAD_TYPE_UNKNOWN;
}
return ctx->type;
}
void SetGamepadImageDisplayMode(GamepadImage *ctx, ControllerDisplayMode display_mode)
{
if (!ctx) {
return;
}
ctx->display_mode = display_mode;
}
float GetGamepadImageButtonWidth(GamepadImage *ctx)
{
if (!ctx) {
return 0;
}
return ctx->button_width;
}
float GetGamepadImageButtonHeight(GamepadImage *ctx)
{
if (!ctx) {
return 0;
}
return ctx->button_height;
}
float GetGamepadImageAxisWidth(GamepadImage *ctx)
{
if (!ctx) {
return 0;
}
return ctx->axis_width;
}
float GetGamepadImageAxisHeight(GamepadImage *ctx)
{
if (!ctx) {
return 0;
}
return ctx->axis_height;
}
int GetGamepadImageElementAt(GamepadImage *ctx, float x, float y)
{
SDL_FPoint point;
int i;
if (!ctx) {
return SDL_GAMEPAD_ELEMENT_INVALID;
}
point.x = x;
point.y = y;
if (ctx->showing_front) {
for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
const int element = SDL_GAMEPAD_BUTTON_COUNT + i;
SDL_FRect rect;
if (element == SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER ||
element == SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER) {
rect.w = ctx->axis_width;
rect.h = ctx->axis_height;
rect.x = ctx->x + axis_positions[i].x - rect.w / 2;
rect.y = ctx->y + axis_positions[i].y - rect.h / 2;
if (SDL_PointInRectFloat(&point, &rect)) {
if (element == SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER) {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER;
} else {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER;
}
}
} else if (element == SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE) {
rect.w = ctx->button_width * 2.0f;
rect.h = ctx->button_height * 2.0f;
rect.x = ctx->x + button_positions[SDL_GAMEPAD_BUTTON_LEFT_STICK].x - rect.w / 2;
rect.y = ctx->y + button_positions[SDL_GAMEPAD_BUTTON_LEFT_STICK].y - rect.h / 2;
if (SDL_PointInRectFloat(&point, &rect)) {
float delta_x, delta_y;
float delta_squared;
float thumbstick_radius = ctx->button_width * 0.1f;
delta_x = (x - (ctx->x + button_positions[SDL_GAMEPAD_BUTTON_LEFT_STICK].x));
delta_y = (y - (ctx->y + button_positions[SDL_GAMEPAD_BUTTON_LEFT_STICK].y));
delta_squared = (delta_x * delta_x) + (delta_y * delta_y);
if (delta_squared > (thumbstick_radius * thumbstick_radius)) {
float angle = SDL_atan2f(delta_y, delta_x) + SDL_PI_F;
if (angle < SDL_PI_F * 0.25f) {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE;
} else if (angle < SDL_PI_F * 0.75f) {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE;
} else if (angle < SDL_PI_F * 1.25f) {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE;
} else if (angle < SDL_PI_F * 1.75f) {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE;
} else {
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE;
}
}
}
} else if (element == SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE) {
rect.w = ctx->button_width * 2.0f;
rect.h = ctx->button_height * 2.0f;
rect.x = ctx->x + button_positions[SDL_GAMEPAD_BUTTON_RIGHT_STICK].x - rect.w / 2;
rect.y = ctx->y + button_positions[SDL_GAMEPAD_BUTTON_RIGHT_STICK].y - rect.h / 2;
if (SDL_PointInRectFloat(&point, &rect)) {
float delta_x, delta_y;
float delta_squared;
float thumbstick_radius = ctx->button_width * 0.1f;
delta_x = (x - (ctx->x + button_positions[SDL_GAMEPAD_BUTTON_RIGHT_STICK].x));
delta_y = (y - (ctx->y + button_positions[SDL_GAMEPAD_BUTTON_RIGHT_STICK].y));
delta_squared = (delta_x * delta_x) + (delta_y * delta_y);
if (delta_squared > (thumbstick_radius * thumbstick_radius)) {
float angle = SDL_atan2f(delta_y, delta_x) + SDL_PI_F;
if (angle < SDL_PI_F * 0.25f) {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE;
} else if (angle < SDL_PI_F * 0.75f) {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE;
} else if (angle < SDL_PI_F * 1.25f) {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE;
} else if (angle < SDL_PI_F * 1.75f) {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE;
} else {
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE;
}
}
}
}
}
}
for (i = 0; i < SDL_arraysize(button_positions); ++i) {
bool on_front = true;
if (i >= SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_LEFT_PADDLE2) {
on_front = false;
}
if (on_front == ctx->showing_front) {
SDL_FRect rect;
rect.x = ctx->x + button_positions[i].x - ctx->button_width / 2;
rect.y = ctx->y + button_positions[i].y - ctx->button_height / 2;
rect.w = ctx->button_width;
rect.h = ctx->button_height;
if (SDL_PointInRectFloat(&point, &rect)) {
return (SDL_GamepadButton)i;
}
}
}
return SDL_GAMEPAD_ELEMENT_INVALID;
}
void ClearGamepadImage(GamepadImage *ctx)
{
if (!ctx) {
return;
}
SDL_zeroa(ctx->elements);
}
void SetGamepadImageElement(GamepadImage *ctx, int element, bool active)
{
if (!ctx) {
return;
}
ctx->elements[element] = active;
}
void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
{
int i;
if (!ctx) {
return;
}
ctx->type = SDL_GetGamepadType(gamepad);
char *mapping = SDL_GetGamepadMapping(gamepad);
if (mapping) {
if (SDL_strstr(mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS")) {
/* Just for display purposes */
ctx->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
}
SDL_free(mapping);
}
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
const SDL_GamepadButton button = (SDL_GamepadButton)i;
SetGamepadImageElement(ctx, button, SDL_GetGamepadButton(gamepad, button));
}
for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
const SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
const Sint16 value = SDL_GetGamepadAxis(gamepad, axis);
switch (i) {
case SDL_GAMEPAD_AXIS_LEFTX:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE, (value < -deadzone));
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE, (value > deadzone));
break;
case SDL_GAMEPAD_AXIS_RIGHTX:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE, (value < -deadzone));
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE, (value > deadzone));
break;
case SDL_GAMEPAD_AXIS_LEFTY:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE, (value < -deadzone));
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE, (value > deadzone));
break;
case SDL_GAMEPAD_AXIS_RIGHTY:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE, (value < -deadzone));
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE, (value > deadzone));
break;
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER, (value > deadzone));
break;
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
SetGamepadImageElement(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER, (value > deadzone));
break;
default:
break;
}
}
ctx->connection_state = SDL_GetGamepadConnectionState(gamepad);
ctx->battery_state = SDL_GetGamepadPowerInfo(gamepad, &ctx->battery_percent);
if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
if (num_fingers != ctx->num_fingers) {
GamepadTouchpadFinger *fingers = (GamepadTouchpadFinger *)SDL_realloc(ctx->fingers, num_fingers * sizeof(*fingers));
if (fingers) {
ctx->fingers = fingers;
ctx->num_fingers = num_fingers;
} else {
num_fingers = SDL_min(ctx->num_fingers, num_fingers);
}
}
for (i = 0; i < num_fingers; ++i) {
GamepadTouchpadFinger *finger = &ctx->fingers[i];
SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &finger->down, &finger->x, &finger->y, &finger->pressure);
}
ctx->showing_touchpad = true;
} else {
if (ctx->fingers) {
SDL_free(ctx->fingers);
ctx->fingers = NULL;
ctx->num_fingers = 0;
}
ctx->showing_touchpad = false;
}
}
void RenderGamepadImage(GamepadImage *ctx)
{
SDL_FRect dst;
int i;
if (!ctx) {
return;
}
dst.x = ctx->x;
dst.y = ctx->y;
dst.w = ctx->gamepad_width;
dst.h = ctx->gamepad_height;
if (ctx->showing_front) {
SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst);
} else {
SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst);
}
for (i = 0; i < SDL_arraysize(button_positions); ++i) {
if (ctx->elements[i]) {
SDL_GamepadButton button_position = (SDL_GamepadButton)i;
bool on_front = true;
if (i >= SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_LEFT_PADDLE2) {
on_front = false;
}
if (on_front == ctx->showing_front) {
dst.w = ctx->button_width;
dst.h = ctx->button_height;
dst.x = ctx->x + button_positions[button_position].x - dst.w / 2;
dst.y = ctx->y + button_positions[button_position].y - dst.h / 2;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
}
}
}
if (ctx->showing_front) {
dst.x = ctx->x + 363;
dst.y = ctx->y + 118;
dst.w = ctx->face_width;
dst.h = ctx->face_height;
switch (SDL_GetGamepadButtonLabelForType(ctx->type, SDL_GAMEPAD_BUTTON_EAST)) {
case SDL_GAMEPAD_BUTTON_LABEL_B:
SDL_RenderTexture(ctx->renderer, ctx->face_abxy_texture, NULL, &dst);
break;
case SDL_GAMEPAD_BUTTON_LABEL_X:
SDL_RenderTexture(ctx->renderer, ctx->face_axby_texture, NULL, &dst);
break;
case SDL_GAMEPAD_BUTTON_LABEL_A:
SDL_RenderTexture(ctx->renderer, ctx->face_bayx_texture, NULL, &dst);
break;
case SDL_GAMEPAD_BUTTON_LABEL_CIRCLE:
SDL_RenderTexture(ctx->renderer, ctx->face_sony_texture, NULL, &dst);
break;
default:
break;
}
}
if (ctx->showing_front) {
for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
const int element = SDL_GAMEPAD_BUTTON_COUNT + i;
if (ctx->elements[element]) {
const double angle = axis_positions[i].angle;
dst.w = ctx->axis_width;
dst.h = ctx->axis_height;
dst.x = ctx->x + axis_positions[i].x - dst.w / 2;
dst.y = ctx->y + axis_positions[i].y - dst.h / 2;
SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
}
}
}
if (ctx->display_mode == CONTROLLER_MODE_TESTING) {
dst.x = ctx->x + ctx->gamepad_width - ctx->battery_width - 4 - ctx->connection_width;
dst.y = ctx->y;
dst.w = ctx->connection_width;
dst.h = ctx->connection_height;
switch (ctx->connection_state) {
case SDL_JOYSTICK_CONNECTION_WIRED:
SDL_RenderTexture(ctx->renderer, ctx->connection_texture[0], NULL, &dst);
break;
case SDL_JOYSTICK_CONNECTION_WIRELESS:
SDL_RenderTexture(ctx->renderer, ctx->connection_texture[1], NULL, &dst);
break;
default:
break;
}
}
if (ctx->display_mode == CONTROLLER_MODE_TESTING &&
ctx->battery_state != SDL_POWERSTATE_NO_BATTERY &&
ctx->battery_state != SDL_POWERSTATE_UNKNOWN) {
Uint8 r, g, b, a;
SDL_FRect fill;
dst.x = ctx->x + ctx->gamepad_width - ctx->battery_width;
dst.y = ctx->y;
dst.w = ctx->battery_width;
dst.h = ctx->battery_height;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (ctx->battery_percent > 40) {
SDL_SetRenderDrawColor(ctx->renderer, 0x00, 0xD4, 0x50, 0xFF);
} else if (ctx->battery_percent > 10) {
SDL_SetRenderDrawColor(ctx->renderer, 0xFF, 0xC7, 0x00, 0xFF);
} else {
SDL_SetRenderDrawColor(ctx->renderer, 0xC8, 0x1D, 0x13, 0xFF);
}
fill = dst;
fill.x += 2;
fill.y += 2;
fill.h -= 4;
fill.w = 25.0f * (ctx->battery_percent / 100.0f);
SDL_RenderFillRect(ctx->renderer, &fill);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
if (ctx->battery_state == SDL_POWERSTATE_ON_BATTERY) {
SDL_RenderTexture(ctx->renderer, ctx->battery_texture[0], NULL, &dst);
} else {
SDL_RenderTexture(ctx->renderer, ctx->battery_texture[1], NULL, &dst);
}
}
if (ctx->display_mode == CONTROLLER_MODE_TESTING && ctx->showing_touchpad) {
dst.x = ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
dst.y = ctx->y + ctx->gamepad_height;
dst.w = ctx->touchpad_width;
dst.h = ctx->touchpad_height;
SDL_RenderTexture(ctx->renderer, ctx->touchpad_texture, NULL, &dst);
for (i = 0; i < ctx->num_fingers; ++i) {
GamepadTouchpadFinger *finger = &ctx->fingers[i];
if (finger->down) {
dst.x = ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
dst.x += touchpad_area.x + finger->x * touchpad_area.w;
dst.x -= ctx->button_width / 2;
dst.y = ctx->y + ctx->gamepad_height;
dst.y += touchpad_area.y + finger->y * touchpad_area.h;
dst.y -= ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
SDL_SetTextureAlphaMod(ctx->button_texture, (Uint8)(finger->pressure * SDL_ALPHA_OPAQUE));
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
SDL_SetTextureAlphaMod(ctx->button_texture, SDL_ALPHA_OPAQUE);
}
}
}
}
void DestroyGamepadImage(GamepadImage *ctx)
{
if (ctx) {
int i;
SDL_DestroyTexture(ctx->front_texture);
SDL_DestroyTexture(ctx->back_texture);
SDL_DestroyTexture(ctx->face_abxy_texture);
SDL_DestroyTexture(ctx->face_axby_texture);
SDL_DestroyTexture(ctx->face_bayx_texture);
SDL_DestroyTexture(ctx->face_sony_texture);
for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) {
SDL_DestroyTexture(ctx->battery_texture[i]);
}
SDL_DestroyTexture(ctx->touchpad_texture);
SDL_DestroyTexture(ctx->button_texture);
SDL_DestroyTexture(ctx->axis_texture);
SDL_free(ctx);
}
}
static const char *gamepad_button_names[] = {
"South",
"East",
"West",
"North",
"Back",
"Guide",
"Start",
"Left Stick",
"Right Stick",
"Left Shoulder",
"Right Shoulder",
"DPAD Up",
"DPAD Down",
"DPAD Left",
"DPAD Right",
"Misc1",
"Right Paddle 1",
"Left Paddle 1",
"Right Paddle 2",
"Left Paddle 2",
"Touchpad",
"Misc2",
"Misc3",
"Misc4",
"Misc5",
"Misc6",
};
SDL_COMPILE_TIME_ASSERT(gamepad_button_names, SDL_arraysize(gamepad_button_names) == SDL_GAMEPAD_BUTTON_COUNT);
static const char *gamepad_axis_names[] = {
"LeftX",
"LeftY",
"RightX",
"RightY",
"Left Trigger",
"Right Trigger",
};
SDL_COMPILE_TIME_ASSERT(gamepad_axis_names, SDL_arraysize(gamepad_axis_names) == SDL_GAMEPAD_AXIS_COUNT);
struct GamepadDisplay
{
SDL_Renderer *renderer;
SDL_Texture *button_texture;
SDL_Texture *arrow_texture;
float button_width;
float button_height;
float arrow_width;
float arrow_height;
float accel_data[3];
float gyro_data[3];
float gyro_drift_correction_data[3];
Uint64 last_sensor_update;
ControllerDisplayMode display_mode;
int element_highlighted;
bool element_pressed;
int element_selected;
SDL_FRect area;
};
GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer)
{
GamepadDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
SDL_GetTextureSize(ctx->arrow_texture, &ctx->arrow_width, &ctx->arrow_height);
ctx->element_highlighted = SDL_GAMEPAD_ELEMENT_INVALID;
ctx->element_selected = SDL_GAMEPAD_ELEMENT_INVALID;
SDL_zeroa(ctx->accel_data);
SDL_zeroa(ctx->gyro_data);
SDL_zeroa(ctx->gyro_drift_correction_data);
}
return ctx;
}
struct GyroDisplay
{
SDL_Renderer *renderer;
/* Main drawing area */
SDL_FRect area;
/* This part displays extra info from the IMUstate in order to figure out actual polling rates. */
float gyro_drift_solution[3];
int reported_sensor_rate_hz; /*hz - comes from HIDsdl implementation. Could be fixed, platform time, or true sensor time*/
int estimated_sensor_rate_hz; /*hz - our estimation of the actual polling rate by observing packets received*/
float euler_displacement_angles[3]; /* pitch, yaw, roll */
Quaternion gyro_quaternion; /* Rotation since startup/reset, comprised of each gyro speed packet times sensor delta time. */
float drift_calibration_progress_frac; /* [0..1] */
float accelerometer_noise_sq; /* Distance between last noise and new noise. Used to indicate motion.*/
GamepadButton *reset_gyro_button;
GamepadButton *calibrate_gyro_button;
};
GyroDisplay *CreateGyroDisplay(SDL_Renderer *renderer)
{
GyroDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
{
ctx->renderer = renderer;
ctx->estimated_sensor_rate_hz = 0;
SDL_zeroa(ctx->gyro_drift_solution);
Quaternion quat_identity = { 0.0f, 0.0f, 0.0f, 1.0f };
ctx->gyro_quaternion = quat_identity;
ctx->reset_gyro_button = CreateGamepadButton(renderer, "Reset View");
ctx->calibrate_gyro_button = CreateGamepadButton(renderer, "Recalibrate Drift");
}
return ctx;
}
void SetGyroDisplayArea(GyroDisplay *ctx, const SDL_FRect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
/* Place the reset button to the bottom right of the gyro display area.*/
SDL_FRect reset_button_area;
reset_button_area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(ctx->reset_gyro_button) + 2 * BUTTON_PADDING);
reset_button_area.h = GetGamepadButtonLabelHeight(ctx->reset_gyro_button) + BUTTON_PADDING;
reset_button_area.x = area->x + area->w - reset_button_area.w - BUTTON_PADDING;
reset_button_area.y = area->y + area->h - reset_button_area.h - BUTTON_PADDING;
SetGamepadButtonArea(ctx->reset_gyro_button, &reset_button_area);
}
void SetGamepadDisplayDisplayMode(GamepadDisplay *ctx, ControllerDisplayMode display_mode)
{
if (!ctx) {
return;
}
ctx->display_mode = display_mode;
}
void SetGamepadDisplayArea(GamepadDisplay *ctx, const SDL_FRect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
}
void SetGamepadDisplayGyroDriftCorrection(GamepadDisplay *ctx, float *gyro_drift_correction)
{
if (!ctx) {
return;
}
ctx->gyro_drift_correction_data[0] = gyro_drift_correction[0];
ctx->gyro_drift_correction_data[1] = gyro_drift_correction[1];
ctx->gyro_drift_correction_data[2] = gyro_drift_correction[2];
}
static bool GetBindingString(const char *label, const char *mapping, char *text, size_t size)
{
char *key;
char *value, *end;
size_t length;
bool found = false;
*text = '\0';
if (!mapping) {
return false;
}
key = SDL_strstr(mapping, label);
while (key && size > 1) {
if (found) {
*text++ = ',';
*text = '\0';
--size;
} else {
found = true;
}
value = key + SDL_strlen(label);
end = SDL_strchr(value, ',');
if (end) {
length = (end - value);
} else {
length = SDL_strlen(value);
}
if (length >= size) {
length = size - 1;
}
SDL_memcpy(text, value, length);
text[length] = '\0';
text += length;
size -= length;
key = SDL_strstr(value, label);
}
return found;
}
static bool GetButtonBindingString(SDL_GamepadButton button, const char *mapping, char *text, size_t size)
{
char label[32];
bool baxy_mapping = false;
if (!mapping) {
return false;
}
SDL_snprintf(label, sizeof(label), ",%s:", SDL_GetGamepadStringForButton(button));
if (GetBindingString(label, mapping, text, size)) {
return true;
}
/* Try the legacy button names */
if (SDL_strstr(mapping, ",hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") != NULL) {
baxy_mapping = true;
}
switch (button) {
case SDL_GAMEPAD_BUTTON_SOUTH:
if (baxy_mapping) {
return GetBindingString(",b:", mapping, text, size);
} else {
return GetBindingString(",a:", mapping, text, size);
}
case SDL_GAMEPAD_BUTTON_EAST:
if (baxy_mapping) {
return GetBindingString(",a:", mapping, text, size);
} else {
return GetBindingString(",b:", mapping, text, size);
}
case SDL_GAMEPAD_BUTTON_WEST:
if (baxy_mapping) {
return GetBindingString(",y:", mapping, text, size);
} else {
return GetBindingString(",x:", mapping, text, size);
}
case SDL_GAMEPAD_BUTTON_NORTH:
if (baxy_mapping) {
return GetBindingString(",x:", mapping, text, size);
} else {
return GetBindingString(",y:", mapping, text, size);
}
default:
return false;
}
}
static bool GetAxisBindingString(SDL_GamepadAxis axis, int direction, const char *mapping, char *text, size_t size)
{
char label[32];
/* Check for explicit half-axis */
if (direction < 0) {
SDL_snprintf(label, sizeof(label), ",-%s:", SDL_GetGamepadStringForAxis(axis));
} else {
SDL_snprintf(label, sizeof(label), ",+%s:", SDL_GetGamepadStringForAxis(axis));
}
if (GetBindingString(label, mapping, text, size)) {
return true;
}
/* Get the binding for the whole axis and split it if necessary */
SDL_snprintf(label, sizeof(label), ",%s:", SDL_GetGamepadStringForAxis(axis));
if (!GetBindingString(label, mapping, text, size)) {
return false;
}
if (axis != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && axis != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
if (*text == 'a') {
/* Split the axis */
size_t length = SDL_strlen(text) + 1;
if ((length + 1) <= size) {
SDL_memmove(text + 1, text, length);
if (text[SDL_strlen(text) - 1] == '~') {
direction *= -1;
text[SDL_strlen(text) - 1] = '\0';
}
if (direction > 0) {
*text = '+';
} else {
*text = '-';
}
}
}
}
return true;
}
void SetGamepadDisplayHighlight(GamepadDisplay *ctx, int element, bool pressed)
{
if (!ctx) {
return;
}
ctx->element_highlighted = element;
ctx->element_pressed = pressed;
}
void SetGamepadDisplaySelected(GamepadDisplay *ctx, int element)
{
if (!ctx) {
return;
}
ctx->element_selected = element;
}
int GetGamepadDisplayElementAt(GamepadDisplay *ctx, SDL_Gamepad *gamepad, float x, float y)
{
int i;
const float margin = 8.0f;
const float center = ctx->area.w / 2.0f;
const float arrow_extent = 48.0f;
SDL_FPoint point;
SDL_FRect rect;
if (!ctx) {
return SDL_GAMEPAD_ELEMENT_INVALID;
}
point.x = x;
point.y = y;
rect.x = ctx->area.x + margin;
rect.y = ctx->area.y + margin + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
rect.w = ctx->area.w - (margin * 2);
rect.h = ctx->button_height;
for (i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) {
SDL_GamepadButton button = (SDL_GamepadButton)i;
if (ctx->display_mode == CONTROLLER_MODE_TESTING &&
!SDL_GamepadHasButton(gamepad, button)) {
continue;
}
if (SDL_PointInRectFloat(&point, &rect)) {
return i;
}
rect.y += ctx->button_height + 2.0f;
}
for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
SDL_FRect area;
if (ctx->display_mode == CONTROLLER_MODE_TESTING &&
!SDL_GamepadHasAxis(gamepad, axis)) {
continue;
}
area.x = rect.x + center + 2.0f;
area.y = rect.y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
area.w = ctx->arrow_width + arrow_extent;
area.h = ctx->button_height;
if (SDL_PointInRectFloat(&point, &area)) {
switch (axis) {
case SDL_GAMEPAD_AXIS_LEFTX:
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE;
case SDL_GAMEPAD_AXIS_LEFTY:
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE;
case SDL_GAMEPAD_AXIS_RIGHTX:
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE;
case SDL_GAMEPAD_AXIS_RIGHTY:
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE;
default:
break;
}
}
area.x += area.w;
if (SDL_PointInRectFloat(&point, &area)) {
switch (axis) {
case SDL_GAMEPAD_AXIS_LEFTX:
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE;
case SDL_GAMEPAD_AXIS_LEFTY:
return SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE;
case SDL_GAMEPAD_AXIS_RIGHTX:
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE;
case SDL_GAMEPAD_AXIS_RIGHTY:
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE;
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
return SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER;
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
return SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER;
default:
break;
}
}
rect.y += ctx->button_height + 2.0f;
}
return SDL_GAMEPAD_ELEMENT_INVALID;
}
static void RenderGamepadElementHighlight(GamepadDisplay *ctx, int element, const SDL_FRect *area)
{
if (element == ctx->element_highlighted || element == ctx->element_selected) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (element == ctx->element_highlighted) {
if (ctx->element_pressed) {
SDL_SetRenderDrawColor(ctx->renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(ctx->renderer, HIGHLIGHT_COLOR);
}
} else {
SDL_SetRenderDrawColor(ctx->renderer, SELECTED_COLOR);
}
SDL_RenderFillRect(ctx->renderer, area);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
}
bool BHasCachedGyroDriftSolution(GyroDisplay *ctx)
{
if (!ctx) {
return false;
}
return (ctx->gyro_drift_solution[0] != 0.0f ||
ctx->gyro_drift_solution[1] != 0.0f ||
ctx->gyro_drift_solution[2] != 0.0f);
}
void SetGamepadDisplayIMUValues(GyroDisplay *ctx, float *gyro_drift_solution, float *euler_displacement_angles, Quaternion *gyro_quaternion, int reported_senor_rate_hz, int estimated_sensor_rate_hz, float drift_calibration_progress_frac, float accelerometer_noise_sq)
{
if (!ctx) {
return;
}
SDL_memcpy(ctx->gyro_drift_solution, gyro_drift_solution, sizeof(ctx->gyro_drift_solution));
ctx->estimated_sensor_rate_hz = estimated_sensor_rate_hz;
if (reported_senor_rate_hz != 0)
ctx->reported_sensor_rate_hz = reported_senor_rate_hz;
SDL_memcpy(ctx->euler_displacement_angles, euler_displacement_angles, sizeof(ctx->euler_displacement_angles));
ctx->gyro_quaternion = *gyro_quaternion;
ctx->drift_calibration_progress_frac = drift_calibration_progress_frac;
ctx->accelerometer_noise_sq = accelerometer_noise_sq;
}
extern GamepadButton *GetGyroResetButton(GyroDisplay *ctx)
{
if (!ctx) {
return NULL;
}
return ctx->reset_gyro_button;
}
extern GamepadButton *GetGyroCalibrateButton(GyroDisplay *ctx)
{
if (!ctx) {
return NULL;
}
return ctx->calibrate_gyro_button;
}
void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
{
float x, y;
int i;
char text[128], binding[32];
const float margin = 8.0f;
const float center = ctx->area.w / 2.0f;
const float arrow_extent = 48.0f;
SDL_FRect dst, rect, highlight;
Uint8 r, g, b, a;
char *mapping;
bool has_accel;
bool has_gyro;
if (!ctx) {
return;
}
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
mapping = SDL_GetGamepadMapping(gamepad);
x = ctx->area.x + margin;
y = ctx->area.y + margin;
for (i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) {
SDL_GamepadButton button = (SDL_GamepadButton)i;
if (ctx->display_mode == CONTROLLER_MODE_TESTING &&
!SDL_GamepadHasButton(gamepad, button)) {
continue;
}
highlight.x = x;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = ctx->area.w - (margin * 2);
highlight.h = ctx->button_height;
RenderGamepadElementHighlight(ctx, i, &highlight);
SDL_snprintf(text, sizeof(text), "%s:", gamepad_button_names[i]);
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
if (SDL_GetGamepadButton(gamepad, button)) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x = x + center + 2.0f;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
if (ctx->display_mode == CONTROLLER_MODE_BINDING) {
if (GetButtonBindingString(button, mapping, binding, sizeof(binding))) {
dst.x += dst.w + 2 * margin;
SDLTest_DrawString(ctx->renderer, dst.x, y, binding);
}
}
y += ctx->button_height + 2.0f;
}
for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
bool has_negative = (axis != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && axis != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
Sint16 value;
if (ctx->display_mode == CONTROLLER_MODE_TESTING &&
!SDL_GamepadHasAxis(gamepad, axis)) {
continue;
}
value = SDL_GetGamepadAxis(gamepad, axis);
SDL_snprintf(text, sizeof(text), "%s:", gamepad_axis_names[i]);
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
highlight.x = x + center + 2.0f;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = ctx->arrow_width + arrow_extent;
highlight.h = ctx->button_height;
switch (axis) {
case SDL_GAMEPAD_AXIS_LEFTX:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_LEFTY:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_RIGHTX:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_RIGHTY:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE, &highlight);
break;
default:
break;
}
highlight.x += highlight.w;
switch (axis) {
case SDL_GAMEPAD_AXIS_LEFTX:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_LEFTY:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_RIGHTX:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_RIGHTY:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE, &highlight);
break;
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER, &highlight);
break;
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
RenderGamepadElementHighlight(ctx, SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER, &highlight);
break;
default:
break;
}
dst.x = x + center + 2.0f;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
dst.w = ctx->arrow_width;
dst.h = ctx->arrow_height;
if (has_negative) {
if (value == SDL_MIN_SINT16) {
SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
}
SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
}
dst.x += ctx->arrow_width;
SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
rect.x = dst.x + arrow_extent - 2.0f;
rect.y = dst.y;
rect.w = 4.0f;
rect.h = ctx->arrow_height;
SDL_RenderFillRect(ctx->renderer, &rect);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
if (value < 0) {
SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
rect.x = dst.x + arrow_extent - rect.w;
rect.y = dst.y + ctx->arrow_height * 0.25f;
rect.h = ctx->arrow_height / 2.0f;
SDL_RenderFillRect(ctx->renderer, &rect);
}
if (ctx->display_mode == CONTROLLER_MODE_BINDING && has_negative) {
if (GetAxisBindingString(axis, -1, mapping, binding, sizeof(binding))) {
float text_x;
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
text_x = dst.x + arrow_extent / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(binding)) / 2;
SDLTest_DrawString(ctx->renderer, text_x, y, binding);
}
}
dst.x += arrow_extent;
if (value > 0) {
SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
rect.x = dst.x;
rect.y = dst.y + ctx->arrow_height * 0.25f;
rect.h = ctx->arrow_height / 2.0f;
SDL_RenderFillRect(ctx->renderer, &rect);
}
if (ctx->display_mode == CONTROLLER_MODE_BINDING) {
if (GetAxisBindingString(axis, 1, mapping, binding, sizeof(binding))) {
float text_x;
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
text_x = dst.x + arrow_extent / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(binding)) / 2;
SDLTest_DrawString(ctx->renderer, text_x, y, binding);
}
}
dst.x += arrow_extent;
if (value == SDL_MAX_SINT16) {
SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
}
SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
y += ctx->button_height + 2.0f;
}
if (ctx->display_mode == CONTROLLER_MODE_TESTING) {
if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
for (i = 0; i < num_fingers; ++i) {
bool down;
float finger_x, finger_y, finger_pressure;
if (!SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &down, &finger_x, &finger_y, &finger_pressure)) {
continue;
}
SDL_snprintf(text, sizeof(text), "Touch finger %d:", i);
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
if (down) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x = x + center + 2.0f;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
if (down) {
SDL_snprintf(text, sizeof(text), "(%.2f,%.2f)", finger_x, finger_y);
SDLTest_DrawString(ctx->renderer, x + center + ctx->button_width + 4.0f, y, text);
}
y += ctx->button_height + 2.0f;
}
}
has_accel = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
has_gyro = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
if (has_accel || has_gyro) {
const int SENSOR_UPDATE_INTERVAL_MS = 100;
Uint64 now = SDL_GetTicks();
if (now >= ctx->last_sensor_update + SENSOR_UPDATE_INTERVAL_MS) {
if (has_accel) {
SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, ctx->accel_data, SDL_arraysize(ctx->accel_data));
}
if (has_gyro) {
SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_GYRO, ctx->gyro_data, SDL_arraysize(ctx->gyro_data));
}
ctx->last_sensor_update = now;
}
if (has_accel) {
SDL_strlcpy(text, "Accelerometer:", sizeof(text));
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
SDL_snprintf(text, sizeof(text), "[%.2f,%.2f,%.2f]m/s%s", ctx->accel_data[0], ctx->accel_data[1], ctx->accel_data[2], SQUARED_UTF8 );
SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
y += ctx->button_height + 2.0f;
}
if (has_gyro) {
SDL_strlcpy(text, "Gyro:", sizeof(text));
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
SDL_snprintf(text, sizeof(text), "[%.2f,%.2f,%.2f]%s/s", ctx->gyro_data[0] * RAD_TO_DEG, ctx->gyro_data[1] * RAD_TO_DEG, ctx->gyro_data[2] * RAD_TO_DEG, DEGREE_UTF8);
SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
/* Display the testcontroller tool's evaluation of drift. This is also useful to get an average rate of turn in calibrated turntable tests. */
if (ctx->gyro_drift_correction_data[0] != 0.0f && ctx->gyro_drift_correction_data[2] != 0.0f && ctx->gyro_drift_correction_data[2] != 0.0f )
{
y += ctx->button_height + 2.0f;
SDL_strlcpy(text, "Gyro Drift:", sizeof(text));
SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
SDL_snprintf(text, sizeof(text), "[%.2f,%.2f,%.2f]%s/s", ctx->gyro_drift_correction_data[0] * RAD_TO_DEG, ctx->gyro_drift_correction_data[1] * RAD_TO_DEG, ctx->gyro_drift_correction_data[2] * RAD_TO_DEG, DEGREE_UTF8);
SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
}
}
}
}
SDL_free(mapping);
}
void DestroyGamepadDisplay(GamepadDisplay *ctx)
{
if (!ctx) {
return;
}
SDL_DestroyTexture(ctx->button_texture);
SDL_DestroyTexture(ctx->arrow_texture);
SDL_free(ctx);
}
void RenderSensorTimingInfo(GyroDisplay *ctx, GamepadDisplay *gamepad_display)
{
/* Sensor timing section */
char text[128];
const float new_line_height = gamepad_display->button_height + 2.0f;
const float text_offset_x = ctx->area.x + ctx->area.w / 4.0f + 40.0f;
/* Anchor to bottom left of principle rect. */
float text_y_pos = ctx->area.y + ctx->area.h - new_line_height * 2;
/*
* Display rate of gyro as reported by the HID implementation.
* This could be based on a hardware time stamp (PS5), or it could be generated by the HID implementation.
* One should expect this to match the estimated rate below, assuming a wired connection.
*/
SDL_strlcpy(text, "HID Sensor Time:", sizeof(text));
SDLTest_DrawString(ctx->renderer, text_offset_x - SDL_strlen(text) * FONT_CHARACTER_SIZE, text_y_pos, text);
if (ctx->reported_sensor_rate_hz > 0) {
/* Convert to micro seconds */
const int delta_time_us = (int)1e6 / ctx->reported_sensor_rate_hz;
SDL_snprintf(text, sizeof(text), "%d%ss %dhz", delta_time_us, MICRO_UTF8, ctx->reported_sensor_rate_hz);
} else {
SDL_snprintf(text, sizeof(text), "????%ss ???hz", MICRO_UTF8);
}
SDLTest_DrawString(ctx->renderer, text_offset_x + 2.0f, text_y_pos, text);
/*
* Display the instrumentation's count of all sensor packets received over time.
* This may represent a more accurate polling rate for the IMU
* But only when using a wired connection.
* It does not necessarily reflect the rate at which the IMU is sampled.
*/
text_y_pos += new_line_height;
SDL_strlcpy(text, "Est.Sensor Time:", sizeof(text));
SDLTest_DrawString(ctx->renderer, text_offset_x - SDL_strlen(text) * FONT_CHARACTER_SIZE, text_y_pos, text);
if (ctx->estimated_sensor_rate_hz > 0) {
/* Convert to micro seconds */
const int delta_time_us = (int)1e6 / ctx->estimated_sensor_rate_hz;
SDL_snprintf(text, sizeof(text), "%d%ss %dhz", delta_time_us, MICRO_UTF8, ctx->estimated_sensor_rate_hz);
} else {
SDL_snprintf(text, sizeof(text), "????%ss ???hz", MICRO_UTF8);
}
SDLTest_DrawString(ctx->renderer, text_offset_x + 2.0f, text_y_pos, text);
}
void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_display )
{
char label_text[128];
float log_y = ctx->area.y + BUTTON_PADDING;
const float new_line_height = gamepad_display->button_height + 2.0f;
GamepadButton *start_calibration_button = GetGyroCalibrateButton(ctx);
bool bHasCachedDriftSolution = BHasCachedGyroDriftSolution(ctx);
/* Show the recalibration progress bar. */
float recalibrate_button_width = GetGamepadButtonLabelWidth(start_calibration_button) + 2 * BUTTON_PADDING;
SDL_FRect recalibrate_button_area;
recalibrate_button_area.x = ctx->area.x + ctx->area.w - recalibrate_button_width - BUTTON_PADDING;
recalibrate_button_area.y = log_y + FONT_CHARACTER_SIZE * 0.5f - gamepad_display->button_height * 0.5f;
recalibrate_button_area.w = GetGamepadButtonLabelWidth(start_calibration_button) + 2.0f * BUTTON_PADDING;
recalibrate_button_area.h = gamepad_display->button_height + BUTTON_PADDING * 2.0f;
if (!bHasCachedDriftSolution) {
SDL_snprintf(label_text, sizeof(label_text), "Progress: %3.0f%% ", ctx->drift_calibration_progress_frac * 100.0f);
} else {
SDL_strlcpy(label_text, "Calibrate Drift", sizeof(label_text));
}
SetGamepadButtonLabel(start_calibration_button, label_text);
SetGamepadButtonArea(start_calibration_button, &recalibrate_button_area);
RenderGamepadButton(start_calibration_button);
/* Above button */
SDL_strlcpy(label_text, "Gyro Orientation:", sizeof(label_text));
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y - new_line_height, label_text);
if (!bHasCachedDriftSolution) {
float flNoiseFraction = SDL_clamp(SDL_sqrtf(ctx->accelerometer_noise_sq) / ACCELEROMETER_NOISE_THRESHOLD, 0.0f, 1.0f);
bool bTooMuchNoise = (flNoiseFraction == 1.0f);
float noise_bar_height = gamepad_display->button_height;
SDL_FRect noise_bar_rect;
noise_bar_rect.x = recalibrate_button_area.x;
noise_bar_rect.y = recalibrate_button_area.y + recalibrate_button_area.h + BUTTON_PADDING;
noise_bar_rect.w = recalibrate_button_area.w;
noise_bar_rect.h = noise_bar_height;
/* Adjust the noise bar rectangle based on the accelerometer noise value */
float noise_bar_fill_width = flNoiseFraction * noise_bar_rect.w; /* Scale the width based on the noise value */
SDL_FRect noise_bar_fill_rect;
noise_bar_fill_rect.x = noise_bar_rect.x + (noise_bar_rect.w - noise_bar_fill_width) * 0.5f;
noise_bar_fill_rect.y = noise_bar_rect.y;
noise_bar_fill_rect.w = noise_bar_fill_width;
noise_bar_fill_rect.h = noise_bar_height;
/* Set the color based on the noise value */
Uint8 red = (Uint8)(flNoiseFraction * 255.0f);
Uint8 green = (Uint8)((1.0f - flNoiseFraction) * 255.0f);
SDL_SetRenderDrawColor(ctx->renderer, red, green, 0, 255); /* red when high noise, green when low noise */
SDL_RenderFillRect(ctx->renderer, &noise_bar_fill_rect); /* draw the filled rectangle */
SDL_SetRenderDrawColor(ctx->renderer, 100, 100, 100, 255); /* gray box */
SDL_RenderRect(ctx->renderer, &noise_bar_rect); /* draw the outline rectangle */
/* Explicit warning message if we detect too much movement */
if (bTooMuchNoise) {
SDL_strlcpy(label_text, "Place GamePad Down!", sizeof(label_text));
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, noise_bar_rect.y + noise_bar_rect.h + new_line_height, label_text);
}
/* Drift progress bar */
/* Demonstrate how far we are through the drift progress, and how it resets when there's "high noise", i.e if flNoiseFraction == 1.0f */
SDL_FRect progress_bar_rect;
progress_bar_rect.x = recalibrate_button_area.x + BUTTON_PADDING;
progress_bar_rect.y = recalibrate_button_area.y + recalibrate_button_area.h * 0.5f + BUTTON_PADDING * 0.5f;
progress_bar_rect.w = recalibrate_button_area.w - BUTTON_PADDING * 2.0f;
progress_bar_rect.h = BUTTON_PADDING * 0.5f;
/* Adjust the drift bar rectangle based on the drift calibration progress fraction */
float drift_bar_fill_width = bTooMuchNoise ? 1.0f : ctx->drift_calibration_progress_frac * progress_bar_rect.w;
SDL_FRect progress_bar_fill;
progress_bar_fill.x = progress_bar_rect.x;
progress_bar_fill.y = progress_bar_rect.y;
progress_bar_fill.w = drift_bar_fill_width;
progress_bar_fill.h = progress_bar_rect.h;
/* Set the color based on the drift calibration progress fraction */
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_GREEN); /* red when too much noise, green when low noise*/
/* Now draw the bars with the filled, then empty rectangles */
SDL_RenderFillRect(ctx->renderer, &progress_bar_fill); /* draw the filled rectangle*/
SDL_SetRenderDrawColor(ctx->renderer, 100, 100, 100, 255); /* gray box*/
SDL_RenderRect(ctx->renderer, &progress_bar_rect); /* draw the outline rectangle*/
/* If there is too much movement, we are going to draw two diagonal red lines between the progress rect corners.*/
if (bTooMuchNoise) {
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_RED); /* red */
SDL_RenderFillRect(ctx->renderer, &progress_bar_fill); /* draw the filled rectangle */
}
}
}
float RenderEulerReadout(GyroDisplay *ctx, GamepadDisplay *gamepad_display )
{
/* Get the mater button's width and base our width off that */
GamepadButton *master_button = GetGyroCalibrateButton(ctx);
SDL_FRect gyro_calibrate_button_rect;
GetGamepadButtonArea(master_button, &gyro_calibrate_button_rect);
char text[128];
float log_y = gyro_calibrate_button_rect.y + gyro_calibrate_button_rect.h + BUTTON_PADDING;
const float new_line_height = gamepad_display->button_height + 2.0f;
float log_gyro_euler_text_x = gyro_calibrate_button_rect.x;
/* Pitch Readout */
SDL_snprintf(text, sizeof(text), "Pitch: %6.2f%s", ctx->euler_displacement_angles[0], DEGREE_UTF8);
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
/* Yaw Readout */
log_y += new_line_height;
SDL_snprintf(text, sizeof(text), " Yaw: %6.2f%s", ctx->euler_displacement_angles[1], DEGREE_UTF8);
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
/* Roll Readout */
log_y += new_line_height;
SDL_snprintf(text, sizeof(text), " Roll: %6.2f%s", ctx->euler_displacement_angles[2], DEGREE_UTF8);
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
return log_y + new_line_height; /* Return the next y position for further rendering */
}
/* Draws the 3D cube, circles and accel arrow, positioning itself relative to the calibrate button. */
void RenderGyroGizmo(GyroDisplay *ctx, SDL_Gamepad *gamepad, float top)
{
/* Get the calibrate button's on-screen area: */
GamepadButton *btn = GetGyroCalibrateButton(ctx);
SDL_FRect btnArea;
GetGamepadButtonArea(btn, &btnArea);
float gizmoSize = btnArea.w;
/* Position it centered horizontally above the button with a small gap */
SDL_FRect gizmoRect;
gizmoRect.x = btnArea.x + (btnArea.w - gizmoSize) * 0.5f;
gizmoRect.y = top;
gizmoRect.w = gizmoSize;
gizmoRect.h = gizmoSize;
/* Draw the rotated cube */
DrawGyroDebugCube(ctx->renderer, &ctx->gyro_quaternion, &gizmoRect);
/* Overlay the XYZ circles */
DrawGyroDebugCircle(ctx->renderer, &ctx->gyro_quaternion, &gizmoRect);
/* If we have accel, draw that arrow too */
if (SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL)) {
float accel[3];
SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, accel, SDL_arraysize(accel));
DrawAccelerometerDebugArrow(ctx->renderer, &ctx->gyro_quaternion, accel, &gizmoRect);
}
/* Follow the size of the main button, but position it below the gizmo */
GamepadButton *reset_button = GetGyroResetButton(ctx);
if (reset_button) {
SDL_FRect reset_area;
GetGamepadButtonArea(reset_button, &reset_area);
/* Position the reset button below the gizmo */
reset_area.x = btnArea.x;
reset_area.y = gizmoRect.y + gizmoRect.h + BUTTON_PADDING * 0.5f;
reset_area.w = btnArea.w;
reset_area.h = btnArea.h;
SetGamepadButtonArea(reset_button, &reset_area);
RenderGamepadButton(reset_button);
}
}
void RenderGyroDisplay(GyroDisplay *ctx, GamepadDisplay *gamepadElements, SDL_Gamepad *gamepad)
{
if (!ctx)
return;
bool bHasAccelerometer = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
bool bHasGyroscope = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
bool bHasIMU = bHasAccelerometer || bHasGyroscope;
if (!bHasIMU)
return;
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
RenderSensorTimingInfo(ctx, gamepadElements);
RenderGyroDriftCalibrationButton(ctx, gamepadElements);
bool bHasCachedDriftSolution = BHasCachedGyroDriftSolution(ctx);
if (bHasCachedDriftSolution) {
float bottom = RenderEulerReadout(ctx, gamepadElements);
RenderGyroGizmo(ctx, gamepad, bottom);
}
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
void DestroyGyroDisplay(GyroDisplay *ctx)
{
if (!ctx) {
return;
}
DestroyGamepadButton(ctx->reset_gyro_button);
DestroyGamepadButton(ctx->calibrate_gyro_button);
SDL_free(ctx);
}
struct GamepadTypeDisplay
{
SDL_Renderer *renderer;
int type_highlighted;
bool type_pressed;
int type_selected;
SDL_GamepadType real_type;
SDL_FRect area;
};
GamepadTypeDisplay *CreateGamepadTypeDisplay(SDL_Renderer *renderer)
{
GamepadTypeDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->type_highlighted = SDL_GAMEPAD_TYPE_UNSELECTED;
ctx->type_selected = SDL_GAMEPAD_TYPE_UNSELECTED;
ctx->real_type = SDL_GAMEPAD_TYPE_UNKNOWN;
}
return ctx;
}
void SetGamepadTypeDisplayArea(GamepadTypeDisplay *ctx, const SDL_FRect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
}
void SetGamepadTypeDisplayHighlight(GamepadTypeDisplay *ctx, int type, bool pressed)
{
if (!ctx) {
return;
}
ctx->type_highlighted = type;
ctx->type_pressed = pressed;
}
void SetGamepadTypeDisplaySelected(GamepadTypeDisplay *ctx, int type)
{
if (!ctx) {
return;
}
ctx->type_selected = type;
}
void SetGamepadTypeDisplayRealType(GamepadTypeDisplay *ctx, SDL_GamepadType type)
{
if (!ctx) {
return;
}
ctx->real_type = type;
}
int GetGamepadTypeDisplayAt(GamepadTypeDisplay *ctx, float x, float y)
{
int i;
const float margin = 8.0f;
const float line_height = 16.0f;
SDL_FRect highlight;
SDL_FPoint point;
if (!ctx) {
return SDL_GAMEPAD_TYPE_UNSELECTED;
}
point.x = x;
point.y = y;
x = ctx->area.x + margin;
y = ctx->area.y + margin;
for (i = SDL_GAMEPAD_TYPE_UNKNOWN; i < SDL_GAMEPAD_TYPE_COUNT; ++i) {
highlight.x = x;
highlight.y = y;
highlight.w = ctx->area.w - (margin * 2);
highlight.h = line_height;
if (SDL_PointInRectFloat(&point, &highlight)) {
return i;
}
y += line_height;
}
return SDL_GAMEPAD_TYPE_UNSELECTED;
}
static void RenderGamepadTypeHighlight(GamepadTypeDisplay *ctx, int type, const SDL_FRect *area)
{
if (type == ctx->type_highlighted || type == ctx->type_selected) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (type == ctx->type_highlighted) {
if (ctx->type_pressed) {
SDL_SetRenderDrawColor(ctx->renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(ctx->renderer, HIGHLIGHT_COLOR);
}
} else {
SDL_SetRenderDrawColor(ctx->renderer, SELECTED_COLOR);
}
SDL_RenderFillRect(ctx->renderer, area);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
}
void RenderGamepadTypeDisplay(GamepadTypeDisplay *ctx)
{
float x, y;
int i;
char text[128];
const float margin = 8.0f;
const float line_height = 16.0f;
SDL_FPoint dst;
SDL_FRect highlight;
if (!ctx) {
return;
}
x = ctx->area.x + margin;
y = ctx->area.y + margin;
for (i = SDL_GAMEPAD_TYPE_UNKNOWN; i < SDL_GAMEPAD_TYPE_COUNT; ++i) {
highlight.x = x;
highlight.y = y;
highlight.w = ctx->area.w - (margin * 2);
highlight.h = line_height;
RenderGamepadTypeHighlight(ctx, i, &highlight);
if (i == SDL_GAMEPAD_TYPE_UNKNOWN) {
if (ctx->real_type == SDL_GAMEPAD_TYPE_UNKNOWN ||
ctx->real_type == SDL_GAMEPAD_TYPE_STANDARD) {
SDL_strlcpy(text, "Auto (Standard)", sizeof(text));
} else {
SDL_snprintf(text, sizeof(text), "Auto (%s)", GetGamepadTypeString(ctx->real_type));
}
} else if (i == SDL_GAMEPAD_TYPE_STANDARD) {
SDL_strlcpy(text, "Standard", sizeof(text));
} else {
SDL_strlcpy(text, GetGamepadTypeString((SDL_GamepadType)i), sizeof(text));
}
dst.x = x + margin;
dst.y = y + line_height / 2 - FONT_CHARACTER_SIZE / 2;
SDLTest_DrawString(ctx->renderer, dst.x, dst.y, text);
y += line_height;
}
}
void DestroyGamepadTypeDisplay(GamepadTypeDisplay *ctx)
{
if (!ctx) {
return;
}
SDL_free(ctx);
}
struct JoystickDisplay
{
SDL_Renderer *renderer;
SDL_Texture *button_texture;
SDL_Texture *arrow_texture;
float button_width;
float button_height;
float arrow_width;
float arrow_height;
SDL_FRect area;
char *element_highlighted;
bool element_pressed;
};
JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
{
JoystickDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
SDL_GetTextureSize(ctx->arrow_texture, &ctx->arrow_width, &ctx->arrow_height);
}
return ctx;
}
void SetJoystickDisplayArea(JoystickDisplay *ctx, const SDL_FRect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
}
char *GetJoystickDisplayElementAt(JoystickDisplay *ctx, SDL_Joystick *joystick, float x, float y)
{
SDL_FPoint point;
int i;
int nbuttons = SDL_GetNumJoystickButtons(joystick);
int naxes = SDL_GetNumJoystickAxes(joystick);
int nhats = SDL_GetNumJoystickHats(joystick);
char text[32];
const float margin = 8.0f;
const float center = 80.0f;
const float arrow_extent = 48.0f;
SDL_FRect dst, highlight;
char *element = NULL;
if (!ctx) {
return NULL;
}
point.x = x;
point.y = y;
x = ctx->area.x + margin;
y = ctx->area.y + margin;
if (nbuttons > 0) {
y += FONT_LINE_HEIGHT + 2;
for (i = 0; i < nbuttons; ++i) {
highlight.x = x;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = center - (margin * 2);
highlight.h = ctx->button_height;
if (SDL_PointInRectFloat(&point, &highlight)) {
SDL_asprintf(&element, "b%d", i);
return element;
}
y += ctx->button_height + 2;
}
}
x = ctx->area.x + margin + center + margin;
y = ctx->area.y + margin;
if (naxes > 0) {
y += FONT_LINE_HEIGHT + 2;
for (i = 0; i < naxes; ++i) {
SDL_snprintf(text, sizeof(text), "%d:", i);
highlight.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = ctx->arrow_width + arrow_extent;
highlight.h = ctx->button_height;
if (SDL_PointInRectFloat(&point, &highlight)) {
SDL_asprintf(&element, "-a%d", i);
return element;
}
highlight.x += highlight.w;
if (SDL_PointInRectFloat(&point, &highlight)) {
SDL_asprintf(&element, "+a%d", i);
return element;
}
y += ctx->button_height + 2;
}
}
y += FONT_LINE_HEIGHT + 2;
if (nhats > 0) {
y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
for (i = 0; i < nhats; ++i) {
SDL_snprintf(text, sizeof(text), "%d:", i);
dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
if (SDL_PointInRectFloat(&point, &dst)) {
SDL_asprintf(&element, "h%d.%d", i, SDL_HAT_LEFT);
return element;
}
dst.x += ctx->button_width;
dst.y -= ctx->button_height;
if (SDL_PointInRectFloat(&point, &dst)) {
SDL_asprintf(&element, "h%d.%d", i, SDL_HAT_UP);
return element;
}
dst.y += ctx->button_height * 2;
if (SDL_PointInRectFloat(&point, &dst)) {
SDL_asprintf(&element, "h%d.%d", i, SDL_HAT_DOWN);
return element;
}
dst.x += ctx->button_width;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
if (SDL_PointInRectFloat(&point, &dst)) {
SDL_asprintf(&element, "h%d.%d", i, SDL_HAT_RIGHT);
return element;
}
y += 3 * ctx->button_height + 2;
}
}
return NULL;
}
void SetJoystickDisplayHighlight(JoystickDisplay *ctx, const char *element, bool pressed)
{
if (ctx->element_highlighted) {
SDL_free(ctx->element_highlighted);
ctx->element_highlighted = NULL;
ctx->element_pressed = false;
}
if (element) {
ctx->element_highlighted = SDL_strdup(element);
ctx->element_pressed = pressed;
}
}
static void RenderJoystickButtonHighlight(JoystickDisplay *ctx, int button, const SDL_FRect *area)
{
if (!ctx->element_highlighted || *ctx->element_highlighted != 'b') {
return;
}
if (SDL_atoi(ctx->element_highlighted + 1) == button) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (ctx->element_pressed) {
SDL_SetRenderDrawColor(ctx->renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(ctx->renderer, HIGHLIGHT_COLOR);
}
SDL_RenderFillRect(ctx->renderer, area);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
}
static void RenderJoystickAxisHighlight(JoystickDisplay *ctx, int axis, int direction, const SDL_FRect *area)
{
char prefix = (direction < 0 ? '-' : '+');
if (!ctx->element_highlighted ||
ctx->element_highlighted[0] != prefix ||
ctx->element_highlighted[1] != 'a') {
return;
}
if (SDL_atoi(ctx->element_highlighted + 2) == axis) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (ctx->element_pressed) {
SDL_SetRenderDrawColor(ctx->renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(ctx->renderer, HIGHLIGHT_COLOR);
}
SDL_RenderFillRect(ctx->renderer, area);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
}
static bool SetupJoystickHatHighlight(JoystickDisplay *ctx, int hat, int direction)
{
if (!ctx->element_highlighted || *ctx->element_highlighted != 'h') {
return false;
}
if (SDL_atoi(ctx->element_highlighted + 1) == hat &&
ctx->element_highlighted[2] == '.' &&
SDL_atoi(ctx->element_highlighted + 3) == direction) {
if (ctx->element_pressed) {
SDL_SetTextureColorMod(ctx->button_texture, PRESSED_TEXTURE_MOD);
} else {
SDL_SetTextureColorMod(ctx->button_texture, HIGHLIGHT_TEXTURE_MOD);
}
return true;
}
return false;
}
void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick)
{
float x, y;
int i;
int nbuttons = SDL_GetNumJoystickButtons(joystick);
int naxes = SDL_GetNumJoystickAxes(joystick);
int nhats = SDL_GetNumJoystickHats(joystick);
char text[32];
const float margin = 8.0f;
const float center = 80.0f;
const float arrow_extent = 48.0f;
SDL_FRect dst, rect, highlight;
Uint8 r, g, b, a;
if (!ctx) {
return;
}
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
x = ctx->area.x + margin;
y = ctx->area.y + margin;
if (nbuttons > 0) {
SDLTest_DrawString(ctx->renderer, x, y, "BUTTONS");
y += FONT_LINE_HEIGHT + 2;
for (i = 0; i < nbuttons; ++i) {
highlight.x = x;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = center - (margin * 2);
highlight.h = ctx->button_height;
RenderJoystickButtonHighlight(ctx, i, &highlight);
SDL_snprintf(text, sizeof(text), "%2d:", i);
SDLTest_DrawString(ctx->renderer, x, y, text);
if (SDL_GetJoystickButton(joystick, (Uint8)i)) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
y += ctx->button_height + 2;
}
}
x = ctx->area.x + margin + center + margin;
y = ctx->area.y + margin;
if (naxes > 0) {
SDLTest_DrawString(ctx->renderer, x, y, "AXES");
y += FONT_LINE_HEIGHT + 2;
for (i = 0; i < naxes; ++i) {
Sint16 value = SDL_GetJoystickAxis(joystick, i);
SDL_snprintf(text, sizeof(text), "%d:", i);
SDLTest_DrawString(ctx->renderer, x, y, text);
highlight.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
highlight.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
highlight.w = ctx->arrow_width + arrow_extent;
highlight.h = ctx->button_height;
RenderJoystickAxisHighlight(ctx, i, -1, &highlight);
highlight.x += highlight.w;
RenderJoystickAxisHighlight(ctx, i, 1, &highlight);
dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
dst.w = ctx->arrow_width;
dst.h = ctx->arrow_height;
if (value == SDL_MIN_SINT16) {
SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
}
SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
dst.x += ctx->arrow_width;
SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
rect.x = dst.x + arrow_extent - 2.0f;
rect.y = dst.y;
rect.w = 4.0f;
rect.h = ctx->arrow_height;
SDL_RenderFillRect(ctx->renderer, &rect);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
if (value < 0) {
SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
rect.x = dst.x + arrow_extent - rect.w;
rect.y = dst.y + ctx->arrow_height * 0.25f;
rect.h = ctx->arrow_height / 2.0f;
SDL_RenderFillRect(ctx->renderer, &rect);
}
dst.x += arrow_extent;
if (value > 0) {
SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
rect.x = dst.x;
rect.y = dst.y + ctx->arrow_height * 0.25f;
rect.h = ctx->arrow_height / 2.0f;
SDL_RenderFillRect(ctx->renderer, &rect);
}
dst.x += arrow_extent;
if (value == SDL_MAX_SINT16) {
SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
} else {
SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
}
SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
y += ctx->button_height + 2;
}
}
y += FONT_LINE_HEIGHT + 2;
if (nhats > 0) {
SDLTest_DrawString(ctx->renderer, x, y, "HATS");
y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
for (i = 0; i < nhats; ++i) {
Uint8 value = SDL_GetJoystickHat(joystick, i);
SDL_snprintf(text, sizeof(text), "%d:", i);
SDLTest_DrawString(ctx->renderer, x, y, text);
if (value & SDL_HAT_LEFT) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else if (!SetupJoystickHatHighlight(ctx, i, SDL_HAT_LEFT)) {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
dst.w = ctx->button_width;
dst.h = ctx->button_height;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
if (value & SDL_HAT_UP) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else if (!SetupJoystickHatHighlight(ctx, i, SDL_HAT_UP)) {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x += ctx->button_width;
dst.y -= ctx->button_height;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
if (value & SDL_HAT_DOWN) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else if (!SetupJoystickHatHighlight(ctx, i, SDL_HAT_DOWN)) {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.y += ctx->button_height * 2;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
if (value & SDL_HAT_RIGHT) {
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
} else if (!SetupJoystickHatHighlight(ctx, i, SDL_HAT_RIGHT)) {
SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
}
dst.x += ctx->button_width;
dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
y += 3 * ctx->button_height + 2;
}
}
}
void DestroyJoystickDisplay(JoystickDisplay *ctx)
{
if (!ctx) {
return;
}
SDL_DestroyTexture(ctx->button_texture);
SDL_DestroyTexture(ctx->arrow_texture);
SDL_free(ctx);
}
struct GamepadButton
{
SDL_Renderer *renderer;
SDL_Texture *background;
float background_width;
float background_height;
SDL_FRect area;
char *label;
float label_width;
float label_height;
bool highlight;
bool pressed;
};
GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label)
{
GamepadButton *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->background = CreateTexture(renderer, gamepad_button_background_bmp, gamepad_button_background_bmp_len);
SDL_GetTextureSize(ctx->background, &ctx->background_width, &ctx->background_height);
SetGamepadButtonLabel(ctx, label);
}
return ctx;
}
void SetGamepadButtonLabel(GamepadButton *ctx, const char *label)
{
if (!ctx) {
return;
}
if (ctx->label) {
SDL_free(ctx->label);
}
ctx->label = SDL_strdup(label);
ctx->label_width = (float)(FONT_CHARACTER_SIZE * SDL_strlen(label));
ctx->label_height = (float)FONT_CHARACTER_SIZE;
}
void SetGamepadButtonArea(GamepadButton *ctx, const SDL_FRect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
}
void GetGamepadButtonArea(GamepadButton *ctx, SDL_FRect *area)
{
if (!ctx) {
SDL_zerop(area);
return;
}
SDL_copyp(area, &ctx->area);
}
void SetGamepadButtonHighlight(GamepadButton *ctx, bool highlight, bool pressed)
{
if (!ctx) {
return;
}
ctx->highlight = highlight;
if (highlight) {
ctx->pressed = pressed;
} else {
ctx->pressed = false;
}
}
float GetGamepadButtonLabelWidth(GamepadButton *ctx)
{
if (!ctx) {
return 0;
}
return ctx->label_width;
}
float GetGamepadButtonLabelHeight(GamepadButton *ctx)
{
if (!ctx) {
return 0;
}
return ctx->label_height;
}
bool GamepadButtonContains(GamepadButton *ctx, float x, float y)
{
SDL_FPoint point;
if (!ctx) {
return false;
}
point.x = x;
point.y = y;
return SDL_PointInRectFloat(&point, &ctx->area);
}
void RenderGamepadButton(GamepadButton *ctx)
{
SDL_FRect src, dst;
float one_third_src_width;
float one_third_src_height;
if (!ctx) {
return;
}
one_third_src_width = ctx->background_width / 3;
one_third_src_height = ctx->background_height / 3;
if (ctx->pressed) {
SDL_SetTextureColorMod(ctx->background, PRESSED_TEXTURE_MOD);
} else if (ctx->highlight) {
SDL_SetTextureColorMod(ctx->background, HIGHLIGHT_TEXTURE_MOD);
} else {
SDL_SetTextureColorMod(ctx->background, 255, 255, 255);
}
/* Top left */
src.x = 0.0f;
src.y = 0.0f;
src.w = one_third_src_width;
src.h = one_third_src_height;
dst.x = ctx->area.x;
dst.y = ctx->area.y;
dst.w = src.w;
dst.h = src.h;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Bottom left */
src.y = ctx->background_height - src.h;
dst.y = ctx->area.y + ctx->area.h - dst.h;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Bottom right */
src.x = ctx->background_width - src.w;
dst.x = ctx->area.x + ctx->area.w - dst.w;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Top right */
src.y = 0.0f;
dst.y = ctx->area.y;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Left */
src.x = 0.0f;
src.y = one_third_src_height;
dst.x = ctx->area.x;
dst.y = ctx->area.y + one_third_src_height;
dst.w = one_third_src_width;
dst.h = ctx->area.h - 2 * one_third_src_height;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Right */
src.x = ctx->background_width - one_third_src_width;
dst.x = ctx->area.x + ctx->area.w - one_third_src_width;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Top */
src.x = one_third_src_width;
src.y = 0.0f;
dst.x = ctx->area.x + one_third_src_width;
dst.y = ctx->area.y;
dst.w = ctx->area.w - 2 * one_third_src_width;
dst.h = one_third_src_height;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Bottom */
src.y = ctx->background_height - src.h;
dst.y = ctx->area.y + ctx->area.h - one_third_src_height;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Center */
src.x = one_third_src_width;
src.y = one_third_src_height;
dst.x = ctx->area.x + one_third_src_width;
dst.y = ctx->area.y + one_third_src_height;
dst.w = ctx->area.w - 2 * one_third_src_width;
dst.h = ctx->area.h - 2 * one_third_src_height;
SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
/* Label */
dst.x = ctx->area.x + ctx->area.w / 2 - ctx->label_width / 2;
dst.y = ctx->area.y + ctx->area.h / 2 - ctx->label_height / 2;
SDLTest_DrawString(ctx->renderer, dst.x, dst.y, ctx->label);
}
void DestroyGamepadButton(GamepadButton *ctx)
{
if (!ctx) {
return;
}
SDL_DestroyTexture(ctx->background);
SDL_free(ctx->label);
SDL_free(ctx);
}
typedef struct
{
char *guid;
char *name;
int num_elements;
char **keys;
char **values;
} MappingParts;
static bool AddMappingKeyValue(MappingParts *parts, char *key, char *value);
static bool AddMappingHalfAxisValue(MappingParts *parts, const char *key, const char *value, char sign)
{
char *new_key, *new_value;
if (SDL_asprintf(&new_key, "%c%s", sign, key) < 0) {
return false;
}
if (*value && value[SDL_strlen(value) - 1] == '~') {
/* Invert the sign of the bound axis */
if (sign == '+') {
sign = '-';
} else {
sign = '+';
}
}
if (SDL_asprintf(&new_value, "%c%s", sign, value) < 0) {
SDL_free(new_key);
return false;
}
if (new_value[SDL_strlen(new_value) - 1] == '~') {
new_value[SDL_strlen(new_value) - 1] = '\0';
}
return AddMappingKeyValue(parts, new_key, new_value);
}
static bool AddMappingKeyValue(MappingParts *parts, char *key, char *value)
{
int axis;
char **new_keys, **new_values;
if (!key || !value) {
SDL_free(key);
SDL_free(value);
return false;
}
/* Split axis values for easy binding purposes */
for (axis = 0; axis < SDL_GAMEPAD_AXIS_LEFT_TRIGGER; ++axis) {
if (SDL_strcmp(key, SDL_GetGamepadStringForAxis((SDL_GamepadAxis)axis)) == 0) {
bool result;
result = AddMappingHalfAxisValue(parts, key, value, '-') &&
AddMappingHalfAxisValue(parts, key, value, '+');
SDL_free(key);
SDL_free(value);
return result;
}
}
new_keys = (char **)SDL_realloc(parts->keys, (parts->num_elements + 1) * sizeof(*new_keys));
if (!new_keys) {
return false;
}
parts->keys = new_keys;
new_values = (char **)SDL_realloc(parts->values, (parts->num_elements + 1) * sizeof(*new_values));
if (!new_values) {
return false;
}
parts->values = new_values;
new_keys[parts->num_elements] = key;
new_values[parts->num_elements] = value;
++parts->num_elements;
return true;
}
static void SplitMapping(const char *mapping, MappingParts *parts)
{
const char *current, *comma, *colon, *key, *value;
char *new_key, *new_value;
SDL_zerop(parts);
if (!mapping || !*mapping) {
return;
}
/* Get the guid */
current = mapping;
comma = SDL_strchr(current, ',');
if (!comma) {
parts->guid = SDL_strdup(current);
return;
}
parts->guid = SDL_strndup(current, (comma - current));
current = comma + 1;
/* Get the name */
comma = SDL_strchr(current, ',');
if (!comma) {
parts->name = SDL_strdup(current);
return;
}
if (*current != '*') {
parts->name = SDL_strndup(current, (comma - current));
}
current = comma + 1;
for (;;) {
colon = SDL_strchr(current, ':');
if (!colon) {
break;
}
key = current;
value = colon + 1;
comma = SDL_strchr(value, ',');
new_key = SDL_strndup(key, (colon - key));
if (comma) {
new_value = SDL_strndup(value, (comma - value));
} else {
new_value = SDL_strdup(value);
}
if (!AddMappingKeyValue(parts, new_key, new_value)) {
break;
}
if (comma) {
current = comma + 1;
} else {
break;
}
}
}
static int FindMappingKey(const MappingParts *parts, const char *key)
{
int i;
if (key) {
for (i = 0; i < parts->num_elements; ++i) {
if (SDL_strcmp(key, parts->keys[i]) == 0) {
return i;
}
}
}
return -1;
}
static void RemoveMappingValueAt(MappingParts *parts, int index)
{
SDL_free(parts->keys[index]);
SDL_free(parts->values[index]);
--parts->num_elements;
if (index < parts->num_elements) {
SDL_memmove(&parts->keys[index], &parts->keys[index] + 1, (parts->num_elements - index) * sizeof(parts->keys[index]));
SDL_memmove(&parts->values[index], &parts->values[index] + 1, (parts->num_elements - index) * sizeof(parts->values[index]));
}
}
static void ConvertBAXYMapping(MappingParts *parts)
{
int i;
bool baxy_mapping = false;
for (i = 0; i < parts->num_elements; ++i) {
const char *key = parts->keys[i];
const char *value = parts->values[i];
if (SDL_strcmp(key, "hint") == 0 &&
SDL_strcmp(value, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") == 0) {
baxy_mapping = true;
}
}
if (!baxy_mapping) {
return;
}
/* Swap buttons, invert hint */
for (i = 0; i < parts->num_elements; ++i) {
char *key = parts->keys[i];
char *value = parts->values[i];
if (SDL_strcmp(key, "a") == 0) {
parts->keys[i] = SDL_strdup("b");
SDL_free(key);
} else if (SDL_strcmp(key, "b") == 0) {
parts->keys[i] = SDL_strdup("a");
SDL_free(key);
} else if (SDL_strcmp(key, "x") == 0) {
parts->keys[i] = SDL_strdup("y");
SDL_free(key);
} else if (SDL_strcmp(key, "y") == 0) {
parts->keys[i] = SDL_strdup("x");
SDL_free(key);
} else if (SDL_strcmp(key, "hint") == 0 &&
SDL_strcmp(value, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") == 0) {
parts->values[i] = SDL_strdup("!SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1");
SDL_free(value);
}
}
}
static void UpdateLegacyElements(MappingParts *parts)
{
ConvertBAXYMapping(parts);
}
static bool CombineMappingAxes(MappingParts *parts)
{
int i, matching, axis;
for (i = 0; i < parts->num_elements; ++i) {
char *key = parts->keys[i];
char *current_value;
char *matching_key;
char *matching_value;
if (*key != '-' && *key != '+') {
continue;
}
for (axis = 0; axis < SDL_GAMEPAD_AXIS_LEFT_TRIGGER; ++axis) {
if (SDL_strcmp(key + 1, SDL_GetGamepadStringForAxis((SDL_GamepadAxis)axis)) == 0) {
/* Look for a matching axis with the opposite sign */
if (SDL_asprintf(&matching_key, "%c%s", (*key == '-' ? '+' : '-'), key + 1) < 0) {
return false;
}
matching = FindMappingKey(parts, matching_key);
if (matching >= 0) {
/* Check to see if they're bound to the same axis */
current_value = parts->values[i];
matching_value = parts->values[matching];
if (((*current_value == '-' && *matching_value == '+') ||
(*current_value == '+' && *matching_value == '-')) &&
SDL_strcmp(current_value + 1, matching_value + 1) == 0) {
/* Combine these axes */
if (*key == *current_value) {
SDL_memmove(current_value, current_value + 1, SDL_strlen(current_value));
} else {
/* Invert the bound axis */
SDL_memmove(current_value, current_value + 1, SDL_strlen(current_value)-1);
current_value[SDL_strlen(current_value) - 1] = '~';
}
SDL_memmove(key, key + 1, SDL_strlen(key));
RemoveMappingValueAt(parts, matching);
}
}
SDL_free(matching_key);
break;
}
}
}
return true;
}
typedef struct
{
MappingParts *parts;
int index;
} MappingSortEntry;
static int SDLCALL SortMapping(const void *a, const void *b)
{
MappingSortEntry *A = (MappingSortEntry *)a;
MappingSortEntry *B = (MappingSortEntry *)b;
const char *keyA = A->parts->keys[A->index];
const char *keyB = B->parts->keys[B->index];
return SDL_strcmp(keyA, keyB);
}
static void MoveSortedEntry(const char *key, MappingSortEntry *sort_order, int num_elements, bool front)
{
int i;
for (i = 0; i < num_elements; ++i) {
MappingSortEntry *entry = &sort_order[i];
if (SDL_strcmp(key, entry->parts->keys[entry->index]) == 0) {
if (front && i != 0) {
MappingSortEntry tmp = sort_order[i];
SDL_memmove(&sort_order[1], &sort_order[0], sizeof(*sort_order)*i);
sort_order[0] = tmp;
} else if (!front && i != (num_elements - 1)) {
MappingSortEntry tmp = sort_order[i];
SDL_memmove(&sort_order[i], &sort_order[i + 1], sizeof(*sort_order)*(num_elements - i - 1));
sort_order[num_elements - 1] = tmp;
}
break;
}
}
}
static char *JoinMapping(MappingParts *parts)
{
int i;
size_t length;
char *mapping;
const char *guid;
const char *name;
MappingSortEntry *sort_order;
UpdateLegacyElements(parts);
CombineMappingAxes(parts);
guid = parts->guid;
if (!guid || !*guid) {
guid = "*";
}
name = parts->name;
if (!name || !*name) {
name = "*";
}
length = SDL_strlen(guid) + 1 + SDL_strlen(name) + 1;
for (i = 0; i < parts->num_elements; ++i) {
length += SDL_strlen(parts->keys[i]) + 1;
length += SDL_strlen(parts->values[i]) + 1;
}
length += 1;
/* The sort order is: crc, platform, type, *, sdk, hint */
sort_order = SDL_stack_alloc(MappingSortEntry, parts->num_elements);
for (i = 0; i < parts->num_elements; ++i) {
sort_order[i].parts = parts;
sort_order[i].index = i;
}
SDL_qsort(sort_order, parts->num_elements, sizeof(*sort_order), SortMapping);
MoveSortedEntry("face", sort_order, parts->num_elements, true);
MoveSortedEntry("type", sort_order, parts->num_elements, true);
MoveSortedEntry("platform", sort_order, parts->num_elements, true);
MoveSortedEntry("crc", sort_order, parts->num_elements, true);
MoveSortedEntry("sdk>=", sort_order, parts->num_elements, false);
MoveSortedEntry("sdk<=", sort_order, parts->num_elements, false);
MoveSortedEntry("hint", sort_order, parts->num_elements, false);
/* Move platform to the front */
mapping = (char *)SDL_malloc(length);
if (mapping) {
*mapping = '\0';
SDL_strlcat(mapping, guid, length);
SDL_strlcat(mapping, ",", length);
SDL_strlcat(mapping, name, length);
SDL_strlcat(mapping, ",", length);
for (i = 0; i < parts->num_elements; ++i) {
int next = sort_order[i].index;
SDL_strlcat(mapping, parts->keys[next], length);
SDL_strlcat(mapping, ":", length);
SDL_strlcat(mapping, parts->values[next], length);
SDL_strlcat(mapping, ",", length);
}
}
SDL_stack_free(sort_order);
return mapping;
}
static void FreeMappingParts(MappingParts *parts)
{
int i;
SDL_free(parts->guid);
SDL_free(parts->name);
for (i = 0; i < parts->num_elements; ++i) {
SDL_free(parts->keys[i]);
SDL_free(parts->values[i]);
}
SDL_free(parts->keys);
SDL_free(parts->values);
SDL_zerop(parts);
}
/* Create a new mapping from the parts and free the old mapping and parts */
static char *RecreateMapping(MappingParts *parts, char *mapping)
{
char *new_mapping = JoinMapping(parts);
if (new_mapping) {
SDL_free(mapping);
mapping = new_mapping;
}
FreeMappingParts(parts);
return mapping;
}
static const char *GetLegacyKey(const char *key, bool baxy)
{
if (SDL_strcmp(key, SDL_GetGamepadStringForButton(SDL_GAMEPAD_BUTTON_SOUTH)) == 0) {
if (baxy) {
return "b";
} else {
return "a";
}
}
if (SDL_strcmp(key, SDL_GetGamepadStringForButton(SDL_GAMEPAD_BUTTON_EAST)) == 0) {
if (baxy) {
return "a";
} else {
return "b";
}
}
if (SDL_strcmp(key, SDL_GetGamepadStringForButton(SDL_GAMEPAD_BUTTON_WEST)) == 0) {
if (baxy) {
return "y";
} else {
return "x";
}
}
if (SDL_strcmp(key, SDL_GetGamepadStringForButton(SDL_GAMEPAD_BUTTON_NORTH)) == 0) {
if (baxy) {
return "y";
} else {
return "x";
}
}
return key;
}
static bool MappingHasKey(const char *mapping, const char *key)
{
int i;
MappingParts parts;
bool result = false;
SplitMapping(mapping, &parts);
i = FindMappingKey(&parts, key);
if (i < 0) {
bool baxy_mapping = false;
if (mapping && SDL_strstr(mapping, ",hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") != NULL) {
baxy_mapping = true;
}
i = FindMappingKey(&parts, GetLegacyKey(key, baxy_mapping));
}
if (i >= 0) {
result = true;
}
FreeMappingParts(&parts);
return result;
}
static char *GetMappingValue(const char *mapping, const char *key)
{
int i;
MappingParts parts;
char *value = NULL;
SplitMapping(mapping, &parts);
i = FindMappingKey(&parts, key);
if (i < 0) {
bool baxy_mapping = false;
if (mapping && SDL_strstr(mapping, ",hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") != NULL) {
baxy_mapping = true;
}
i = FindMappingKey(&parts, GetLegacyKey(key, baxy_mapping));
}
if (i >= 0) {
value = parts.values[i];
parts.values[i] = NULL; /* So we don't free it */
}
FreeMappingParts(&parts);
return value;
}
static char *SetMappingValue(char *mapping, const char *key, const char *value)
{
MappingParts parts;
int i;
char *new_key = NULL;
char *new_value = NULL;
char **new_keys = NULL;
char **new_values = NULL;
bool result = false;
if (!key) {
return mapping;
}
SplitMapping(mapping, &parts);
i = FindMappingKey(&parts, key);
if (i >= 0) {
new_value = SDL_strdup(value);
if (new_value) {
SDL_free(parts.values[i]);
parts.values[i] = new_value;
result = true;
}
} else {
int count = parts.num_elements;
new_key = SDL_strdup(key);
if (new_key) {
new_value = SDL_strdup(value);
if (new_value) {
new_keys = (char **)SDL_realloc(parts.keys, (count + 1) * sizeof(*new_keys));
if (new_keys) {
new_values = (char **)SDL_realloc(parts.values, (count + 1) * sizeof(*new_values));
if (new_values) {
new_keys[count] = new_key;
new_values[count] = new_value;
parts.num_elements = (count + 1);
parts.keys = new_keys;
parts.values = new_values;
result = true;
}
}
}
}
}
if (result) {
mapping = RecreateMapping(&parts, mapping);
} else {
SDL_free(new_key);
SDL_free(new_value);
SDL_free(new_keys);
SDL_free(new_values);
}
return mapping;
}
static char *RemoveMappingValue(char *mapping, const char *key)
{
MappingParts parts;
int i;
SplitMapping(mapping, &parts);
i = FindMappingKey(&parts, key);
if (i >= 0) {
RemoveMappingValueAt(&parts, i);
}
return RecreateMapping(&parts, mapping);
}
bool MappingHasBindings(const char *mapping)
{
MappingParts parts;
int i;
bool result = false;
if (!mapping || !*mapping) {
return false;
}
SplitMapping(mapping, &parts);
for (i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) {
if (FindMappingKey(&parts, SDL_GetGamepadStringForButton((SDL_GamepadButton)i)) >= 0) {
result = true;
break;
}
}
if (!result) {
for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
if (FindMappingKey(&parts, SDL_GetGamepadStringForAxis((SDL_GamepadAxis)i)) >= 0) {
result = true;
break;
}
}
}
FreeMappingParts(&parts);
return result;
}
bool MappingHasName(const char *mapping)
{
MappingParts parts;
bool result;
SplitMapping(mapping, &parts);
result = parts.name ? true : false;
FreeMappingParts(&parts);
return result;
}
char *GetMappingName(const char *mapping)
{
MappingParts parts;
char *name;
SplitMapping(mapping, &parts);
name = parts.name;
parts.name = NULL; /* Don't free the name we're about to return */
FreeMappingParts(&parts);
return name;
}
char *SetMappingName(char *mapping, const char *name)
{
MappingParts parts;
char *new_name, *spot;
size_t length;
if (!name) {
return mapping;
}
/* Remove any leading whitespace */
while (*name && SDL_isspace(*name)) {
++name;
}
new_name = SDL_strdup(name);
if (!new_name) {
return mapping;
}
/* Remove any commas, which are field separators in the mapping */
length = SDL_strlen(new_name);
while ((spot = SDL_strchr(new_name, ',')) != NULL) {
SDL_memmove(spot, spot + 1, length - (spot - new_name) + 1);
--length;
}
/* Remove any trailing whitespace */
while (length > 0 && SDL_isspace(new_name[length - 1])) {
--length;
}
/* See if we have anything left */
if (length == 0) {
SDL_free(new_name);
return mapping;
}
/* null terminate to cut off anything we've trimmed */
new_name[length] = '\0';
SplitMapping(mapping, &parts);
SDL_free(parts.name);
parts.name = new_name;
return RecreateMapping(&parts, mapping);
}
const char *GetGamepadTypeString(SDL_GamepadType type)
{
switch (type) {
case SDL_GAMEPAD_TYPE_XBOX360:
return "Xbox 360";
case SDL_GAMEPAD_TYPE_XBOXONE:
return "Xbox One";
case SDL_GAMEPAD_TYPE_PS3:
return "PS3";
case SDL_GAMEPAD_TYPE_PS4:
return "PS4";
case SDL_GAMEPAD_TYPE_PS5:
return "PS5";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
return "Nintendo Switch";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
return "Joy-Con (L)";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
return "Joy-Con (R)";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
return "Joy-Con Pair";
case SDL_GAMEPAD_TYPE_GAMECUBE:
return "GameCube";
default:
return "";
}
}
SDL_GamepadType GetMappingType(const char *mapping)
{
return SDL_GetGamepadTypeFromString(GetMappingValue(mapping, "type"));
}
char *SetMappingType(char *mapping, SDL_GamepadType type)
{
const char *type_string = SDL_GetGamepadStringForType(type);
if (!type_string || type == SDL_GAMEPAD_TYPE_UNKNOWN) {
return RemoveMappingValue(mapping, "type");
} else {
return SetMappingValue(mapping, "type", type_string);
}
}
static const char *GetElementKey(int element)
{
if (element < SDL_GAMEPAD_BUTTON_COUNT) {
return SDL_GetGamepadStringForButton((SDL_GamepadButton)element);
} else {
static char key[16];
switch (element) {
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE:
SDL_snprintf(key, sizeof(key), "-%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTX));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE:
SDL_snprintf(key, sizeof(key), "+%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTX));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE:
SDL_snprintf(key, sizeof(key), "-%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTY));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE:
SDL_snprintf(key, sizeof(key), "+%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTY));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE:
SDL_snprintf(key, sizeof(key), "-%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTX));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE:
SDL_snprintf(key, sizeof(key), "+%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTX));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE:
SDL_snprintf(key, sizeof(key), "-%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTY));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE:
SDL_snprintf(key, sizeof(key), "+%s", SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTY));
break;
case SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER:
return SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFT_TRIGGER);
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER:
return SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
default:
return NULL;
}
return key;
}
}
bool MappingHasElement(const char *mapping, int element)
{
const char *key;
key = GetElementKey(element);
if (!key) {
return false;
}
return MappingHasKey(mapping, key);
}
char *GetElementBinding(const char *mapping, int element)
{
const char *key;
key = GetElementKey(element);
if (!key) {
return NULL;
}
return GetMappingValue(mapping, key);
}
char *SetElementBinding(char *mapping, int element, const char *binding)
{
if (binding) {
return SetMappingValue(mapping, GetElementKey(element), binding);
} else {
return RemoveMappingValue(mapping, GetElementKey(element));
}
}
int GetElementForBinding(char *mapping, const char *binding)
{
MappingParts parts;
int i, element;
int result = SDL_GAMEPAD_ELEMENT_INVALID;
if (!binding) {
return SDL_GAMEPAD_ELEMENT_INVALID;
}
SplitMapping(mapping, &parts);
for (i = 0; i < parts.num_elements; ++i) {
if (SDL_strcmp(binding, parts.values[i]) == 0) {
for (element = 0; element < SDL_GAMEPAD_ELEMENT_MAX; ++element) {
const char *key = GetElementKey(element);
if (key && SDL_strcmp(key, parts.keys[i]) == 0) {
result = element;
break;
}
}
break;
}
}
FreeMappingParts(&parts);
return result;
}
bool MappingHasBinding(const char *mapping, const char *binding)
{
MappingParts parts;
int i;
bool result = false;
if (!binding) {
return false;
}
SplitMapping(mapping, &parts);
for (i = 0; i < parts.num_elements; ++i) {
if (SDL_strcmp(binding, parts.values[i]) == 0) {
result = true;
break;
}
}
FreeMappingParts(&parts);
return result;
}
char *ClearMappingBinding(char *mapping, const char *binding)
{
MappingParts parts;
int i;
bool modified = false;
if (!binding) {
return mapping;
}
SplitMapping(mapping, &parts);
for (i = parts.num_elements - 1; i >= 0; --i) {
if (SDL_strcmp(binding, parts.values[i]) == 0) {
RemoveMappingValueAt(&parts, i);
modified = true;
}
}
if (modified) {
return RecreateMapping(&parts, mapping);
} else {
FreeMappingParts(&parts);
return mapping;
}
}