Skip to content

Commit 52e7b97

Browse files
committed
Merge pull request #111681 from Nintorch/joypad-led
Add support for setting a joypad's LED light color
2 parents 0b5ad6c + a552427 commit 52e7b97

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

core/input/input.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ void Input::_bind_methods() {
151151
ClassDB::bind_method(D_METHOD("set_accelerometer", "value"), &Input::set_accelerometer);
152152
ClassDB::bind_method(D_METHOD("set_magnetometer", "value"), &Input::set_magnetometer);
153153
ClassDB::bind_method(D_METHOD("set_gyroscope", "value"), &Input::set_gyroscope);
154+
ClassDB::bind_method(D_METHOD("set_joy_light", "device", "color"), &Input::set_joy_light);
155+
ClassDB::bind_method(D_METHOD("has_joy_light", "device"), &Input::has_joy_light);
154156
ClassDB::bind_method(D_METHOD("get_last_mouse_velocity"), &Input::get_last_mouse_velocity);
155157
ClassDB::bind_method(D_METHOD("get_last_mouse_screen_velocity"), &Input::get_last_mouse_screen_velocity);
156158
ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
@@ -1001,6 +1003,19 @@ void Input::set_joy_features(int p_device, JoypadFeatures *p_features) {
10011003
_update_joypad_features(p_device);
10021004
}
10031005

1006+
bool Input::set_joy_light(int p_device, const Color &p_color) {
1007+
Joypad *joypad = joy_names.getptr(p_device);
1008+
if (!joypad || joypad->features == nullptr) {
1009+
return false;
1010+
}
1011+
return joypad->features->set_joy_light(p_color);
1012+
}
1013+
1014+
bool Input::has_joy_light(int p_device) const {
1015+
const Joypad *joypad = joy_names.getptr(p_device);
1016+
return joypad && joypad->has_light;
1017+
}
1018+
10041019
void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
10051020
_THREAD_SAFE_METHOD_
10061021
if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
@@ -1498,8 +1513,9 @@ void Input::_update_joypad_features(int p_device) {
14981513
if (!joypad || joypad->features == nullptr) {
14991514
return;
15001515
}
1501-
// Do something based on the features. For example, we can save the information about
1502-
// the joypad having motion sensors, LED light, etc.
1516+
if (joypad->features->has_joy_light()) {
1517+
joypad->has_light = true;
1518+
}
15031519
}
15041520

15051521
Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button) {

core/input/input.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ class Input : public Object {
8383
public:
8484
virtual ~JoypadFeatures() {}
8585

86-
// None at the moment, but later we can add new features like:
87-
// virtual bool has_joy_accelerometer() const { return false; }
88-
// virtual bool set_joy_accelerometer_enabled(bool p_enable) { return false; }
86+
virtual bool has_joy_light() const { return false; }
87+
virtual bool set_joy_light(const Color &p_color) { return false; }
8988
};
9089

9190
static constexpr int32_t JOYPADS_MAX = 16;
@@ -184,6 +183,7 @@ class Input : public Object {
184183
int mapping = -1;
185184
int hat_current = 0;
186185
Dictionary info;
186+
bool has_light = false;
187187
Input::JoypadFeatures *features = nullptr;
188188
};
189189

@@ -360,6 +360,9 @@ class Input : public Object {
360360

361361
void set_joy_features(int p_device, JoypadFeatures *p_features);
362362

363+
bool set_joy_light(int p_device, const Color &p_color);
364+
bool has_joy_light(int p_device) const;
365+
363366
void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
364367
void stop_joy_vibration(int p_device);
365368
void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);

doc/classes/Input.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@
200200
By default, the deadzone is automatically calculated from the average of the action deadzones. However, you can override the deadzone to be whatever you want (on the range of 0 to 1).
201201
</description>
202202
</method>
203+
<method name="has_joy_light" qualifiers="const">
204+
<return type="bool" />
205+
<param index="0" name="device" type="int" />
206+
<description>
207+
Returns [code]true[/code] if the joypad has an LED light that can change colors and/or brightness. See also [method set_joy_light].
208+
[b]Note:[/b] This feature is only supported on Windows, Linux, and macOS.
209+
</description>
210+
</method>
203211
<method name="is_action_just_pressed" qualifiers="const">
204212
<return type="bool" />
205213
<param index="0" name="action" type="StringName" />
@@ -389,6 +397,16 @@
389397
[b]Note:[/b] This value can be immediately overwritten by the hardware sensor value on Android and iOS.
390398
</description>
391399
</method>
400+
<method name="set_joy_light">
401+
<return type="bool" />
402+
<param index="0" name="device" type="int" />
403+
<param index="1" name="color" type="Color" />
404+
<description>
405+
Sets the joypad's LED light, if available, to the specified color. Returns [code]true[/code] if the operation was successful. See also [method has_joy_light].
406+
[b]Note:[/b] There is no way to get the color of the light from a joypad. If you need to know the assigned color, store it separately.
407+
[b]Note:[/b] This feature is only supported on Windows, Linux, and macOS.
408+
</description>
409+
</method>
392410
<method name="set_magnetometer">
393411
<return type="void" />
394412
<param index="0" name="value" type="Vector3" />

drivers/sdl/joypad_sdl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ void JoypadSDL::close_joypad(int p_pad_idx) {
302302
}
303303
}
304304

305+
bool JoypadSDL::Joypad::has_joy_light() const {
306+
SDL_PropertiesID properties_id = SDL_GetJoystickProperties(get_sdl_joystick());
307+
if (properties_id == 0) {
308+
return false;
309+
}
310+
return SDL_GetBooleanProperty(properties_id, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false) || SDL_GetBooleanProperty(properties_id, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, false);
311+
}
312+
313+
bool JoypadSDL::Joypad::set_joy_light(const Color &p_color) {
314+
Color linear = p_color.srgb_to_linear();
315+
return SDL_SetJoystickLED(get_sdl_joystick(), linear.get_r8(), linear.get_g8(), linear.get_b8());
316+
}
317+
305318
SDL_Joystick *JoypadSDL::Joypad::get_sdl_joystick() const {
306319
return SDL_GetJoystickFromID(sdl_instance_idx);
307320
}

drivers/sdl/joypad_sdl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class JoypadSDL {
6262
bool supports_force_feedback = false;
6363
uint64_t ff_effect_timestamp = 0;
6464

65+
virtual bool has_joy_light() const override;
66+
virtual bool set_joy_light(const Color &p_color) override;
67+
6568
SDL_Joystick *get_sdl_joystick() const;
6669
SDL_Gamepad *get_sdl_gamepad() const;
6770
};

0 commit comments

Comments
 (0)