From 6eac65d4fe97869d3fd601fd169d11fae9bbb709 Mon Sep 17 00:00:00 2001
From: bruvzg <7645683+bruvzg@users.noreply.github.com>
Date: Mon, 10 Jan 2022 13:43:44 +0200
Subject: [PATCH] Add joystick button index boundary check. Increase max.
button number to 128 (max. buttons supported by DirectInput).
(cherry picked from commit 61ea8f83377952a8ca8e1cb5cf8ec1099ca8369f)
---
core/os/input_event.h | 2 +-
doc/classes/@GlobalScope.xml | 7 +++++--
main/input_default.cpp | 3 ++-
3 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/core/os/input_event.h b/core/os/input_event.h
index 4b202407978..f0af4ee4be6 100644
--- a/core/os/input_event.h
+++ b/core/os/input_event.h
@@ -85,7 +85,7 @@ enum JoystickList {
JOY_BUTTON_20 = 20,
JOY_BUTTON_21 = 21,
JOY_BUTTON_22 = 22,
- JOY_BUTTON_MAX = 23,
+ JOY_BUTTON_MAX = 128, // Android supports up to 36 buttons. DirectInput supports up to 128 buttons.
JOY_L = JOY_BUTTON_4,
JOY_R = JOY_BUTTON_5,
diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml
index d1b3e3882ca..1ba70d06d3a 100644
--- a/doc/classes/@GlobalScope.xml
+++ b/doc/classes/@GlobalScope.xml
@@ -1015,8 +1015,11 @@
Gamepad button 22.
-
- Represents the maximum number of joystick buttons supported.
+
+ The maximum number of game controller buttons supported by the engine. The actual limit may be lower on specific platforms:
+ - Android: Up to 36 buttons.
+ - Linux: Up to 80 buttons.
+ - Windows and macOS: Up to 128 buttons.
DualShock circle button.
diff --git a/main/input_default.cpp b/main/input_default.cpp
index 7ca580f8ede..c2c874076fb 100644
--- a/main/input_default.cpp
+++ b/main/input_default.cpp
@@ -775,7 +775,8 @@ InputDefault::InputDefault() {
void InputDefault::joy_button(int p_device, int p_button, bool p_pressed) {
_THREAD_SAFE_METHOD_;
Joypad &joy = joy_names[p_device];
- //printf("got button %i, mapping is %i\n", p_button, joy.mapping);
+ ERR_FAIL_INDEX(p_button, JOY_BUTTON_MAX);
+
if (joy.last_buttons[p_button] == p_pressed) {
return;
}