Skip to content

Commit 7ae6781

Browse files
committed
Support adding advanced joypad features
1 parent 36b9212 commit 7ae6781

File tree

9 files changed

+560
-3
lines changed

9 files changed

+560
-3
lines changed

core/input/input.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,15 @@ void Input::set_joy_axis(int p_device, JoyAxis p_axis, float p_value) {
986986
_joy_axis[c] = p_value;
987987
}
988988

989+
void Input::set_joy_features(int p_device, JoypadFeatures *p_features) {
990+
Joypad *joypad = joy_names.getptr(p_device);
991+
if (!joypad) {
992+
return;
993+
}
994+
joypad->features = p_features;
995+
_update_joypad_features(p_device);
996+
}
997+
989998
void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
990999
_THREAD_SAFE_METHOD_
9911000
if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
@@ -1478,6 +1487,15 @@ void Input::_update_action_cache(const StringName &p_action_name, ActionState &r
14781487
}
14791488
}
14801489

1490+
void Input::_update_joypad_features(int p_device) {
1491+
Joypad *joypad = joy_names.getptr(p_device);
1492+
if (!joypad || joypad->features == nullptr) {
1493+
return;
1494+
}
1495+
// Do something based on the features. For example, we can save the information about
1496+
// the joypad having motion sensors, LED light, etc.
1497+
}
1498+
14811499
Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button) {
14821500
JoyEvent event;
14831501

core/input/input.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ class Input : public Object {
7979
CURSOR_MAX
8080
};
8181

82+
class JoypadFeatures {
83+
public:
84+
virtual ~JoypadFeatures() {}
85+
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; }
89+
};
90+
8291
static constexpr int32_t JOYPADS_MAX = 16;
8392

8493
typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event);
@@ -174,6 +183,7 @@ class Input : public Object {
174183
int mapping = -1;
175184
int hat_current = 0;
176185
Dictionary info;
186+
Input::JoypadFeatures *features = nullptr;
177187
};
178188

179189
VelocityTrack mouse_velocity_track;
@@ -253,6 +263,7 @@ class Input : public Object {
253263
void _button_event(int p_device, JoyButton p_index, bool p_pressed);
254264
void _axis_event(int p_device, JoyAxis p_axis, float p_value);
255265
void _update_action_cache(const StringName &p_action_name, ActionState &r_action_state);
266+
void _update_joypad_features(int p_device);
256267

257268
void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
258269

@@ -346,6 +357,8 @@ class Input : public Object {
346357
void set_gyroscope(const Vector3 &p_gyroscope);
347358
void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
348359

360+
void set_joy_features(int p_device, JoypadFeatures *p_features);
361+
349362
void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
350363
void stop_joy_vibration(int p_device);
351364
void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);

drivers/sdl/SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ if env["builtin_sdl"]:
178178
"joystick/windows/SDL_xinputjoystick.c",
179179
"thread/generic/SDL_syscond.c",
180180
"thread/generic/SDL_sysrwlock.c",
181+
"sensor/windows/SDL_windowssensor.c",
181182
"thread/windows/SDL_syscond_cv.c",
182183
"thread/windows/SDL_sysmutex.c",
183184
"thread/windows/SDL_sysrwlock_srw.c",

drivers/sdl/SDL_build_config_private.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#define SDL_DIALOG_DISABLED 1
4848
#define SDL_FILESYSTEM_DUMMY 1
4949
#define SDL_FSOPS_DUMMY 1
50-
#define SDL_SENSOR_DISABLED 1
5150
#define SDL_GPU_DISABLED 1
5251
#define SDL_RENDER_DISABLED 1
5352
#define SDL_POWER_DISABLED 1
@@ -73,6 +72,7 @@
7372
#define SDL_THREAD_GENERIC_RWLOCK_SUFFIX 1
7473
#define SDL_THREAD_WINDOWS 1
7574
#define SDL_TIMER_WINDOWS 1
75+
#define SDL_SENSOR_WINDOWS 1
7676

7777
// Linux defines
7878
#elif defined(SDL_PLATFORM_LINUX)
@@ -113,6 +113,7 @@
113113
#define SDL_HAPTIC_LINUX 1
114114
#define SDL_TIMER_UNIX 1
115115
#define SDL_JOYSTICK_LINUX 1
116+
#define SDL_JOYSTICK_HIDAPI 1
116117
#define SDL_INPUT_LINUXEV 1
117118
#define SDL_THREAD_PTHREAD 1
118119

@@ -126,8 +127,10 @@
126127
#define SDL_HAPTIC_IOKIT 1
127128
#define SDL_JOYSTICK_IOKIT 1
128129
#define SDL_JOYSTICK_MFI 1
130+
#define SDL_JOYSTICK_HIDAPI 1
129131
#define SDL_TIMER_UNIX 1
130132
#define SDL_THREAD_PTHREAD 1
133+
#define SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1
131134

132135
// Other platforms are not supported (for now)
133136
#else

drivers/sdl/joypad_sdl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ void JoypadSDL::process_events() {
208208
device_name,
209209
joypads[joy_id].guid,
210210
joypad_info);
211+
212+
Input::get_singleton()->set_joy_features(joy_id, &joypads[joy_id]);
211213
}
212214
// An event for an attached joypad
213215
} else if (sdl_event.type >= SDL_EVENT_JOYSTICK_AXIS_MOTION && sdl_event.type < SDL_EVENT_FINGER_DOWN && sdl_instance_id_to_joypad_id.has(sdl_event.jdevice.which)) {
@@ -299,4 +301,12 @@ void JoypadSDL::close_joypad(int p_pad_idx) {
299301
}
300302
}
301303

304+
SDL_Joystick *JoypadSDL::Joypad::get_sdl_joystick() const {
305+
return SDL_GetJoystickFromID(sdl_instance_idx);
306+
}
307+
308+
SDL_Gamepad *JoypadSDL::Joypad::get_sdl_gamepad() const {
309+
return SDL_GetGamepadFromID(sdl_instance_idx);
310+
}
311+
302312
#endif // SDL_ENABLED

drivers/sdl/joypad_sdl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
typedef uint32_t SDL_JoystickID;
3737
typedef struct HWND__ *HWND;
38+
typedef struct SDL_Joystick SDL_Joystick;
39+
typedef struct SDL_Gamepad SDL_Gamepad;
3840

3941
class JoypadSDL {
4042
public:
@@ -50,14 +52,18 @@ class JoypadSDL {
5052
void process_events();
5153

5254
private:
53-
struct Joypad {
55+
class Joypad : public Input::JoypadFeatures {
56+
public:
5457
bool attached = false;
5558
StringName guid;
5659

5760
SDL_JoystickID sdl_instance_idx;
5861

5962
bool supports_force_feedback = false;
6063
uint64_t ff_effect_timestamp = 0;
64+
65+
SDL_Joystick *get_sdl_joystick() const;
66+
SDL_Gamepad *get_sdl_gamepad() const;
6167
};
6268

6369
static JoypadSDL *singleton;

0 commit comments

Comments
 (0)