Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ void Input::set_joy_axis(int p_device, JoyAxis p_axis, float p_value) {
_joy_axis[c] = p_value;
}

void Input::set_joy_features(int p_device, JoypadFeatures *p_features) {
Joypad *joypad = joy_names.getptr(p_device);
if (!joypad) {
return;
}
joypad->features = p_features;
_update_joypad_features(p_device);
}

void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
_THREAD_SAFE_METHOD_
if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
Expand Down Expand Up @@ -1478,6 +1487,15 @@ void Input::_update_action_cache(const StringName &p_action_name, ActionState &r
}
}

void Input::_update_joypad_features(int p_device) {
Joypad *joypad = joy_names.getptr(p_device);
if (!joypad || joypad->features == nullptr) {
return;
}
// Do something based on the features. For example, we can save the information about
// the joypad having motion sensors, LED light, etc.
}

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

Expand Down
13 changes: 13 additions & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class Input : public Object {
CURSOR_MAX
};

class JoypadFeatures {
public:
virtual ~JoypadFeatures() {}

// None at the moment, but later we can add new features like:
// virtual bool has_joy_accelerometer() const { return false; }
// virtual bool set_joy_accelerometer_enabled(bool p_enable) { return false; }
};

static constexpr int32_t JOYPADS_MAX = 16;

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

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

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

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

void set_joy_features(int p_device, JoypadFeatures *p_features);

void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
void stop_joy_vibration(int p_device);
void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);
Expand Down
1 change: 1 addition & 0 deletions drivers/sdl/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ if env["builtin_sdl"]:
"joystick/windows/SDL_xinputjoystick.c",
"thread/generic/SDL_syscond.c",
"thread/generic/SDL_sysrwlock.c",
"sensor/windows/SDL_windowssensor.c",
"thread/windows/SDL_syscond_cv.c",
"thread/windows/SDL_sysmutex.c",
"thread/windows/SDL_sysrwlock_srw.c",
Expand Down
5 changes: 4 additions & 1 deletion drivers/sdl/SDL_build_config_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#define SDL_DIALOG_DISABLED 1
#define SDL_FILESYSTEM_DUMMY 1
#define SDL_FSOPS_DUMMY 1
#define SDL_SENSOR_DISABLED 1
#define SDL_GPU_DISABLED 1
#define SDL_RENDER_DISABLED 1
#define SDL_POWER_DISABLED 1
Expand All @@ -73,6 +72,7 @@
#define SDL_THREAD_GENERIC_RWLOCK_SUFFIX 1
#define SDL_THREAD_WINDOWS 1
#define SDL_TIMER_WINDOWS 1
#define SDL_SENSOR_WINDOWS 1

// Linux defines
#elif defined(SDL_PLATFORM_LINUX)
Expand Down Expand Up @@ -113,6 +113,7 @@
#define SDL_HAPTIC_LINUX 1
#define SDL_TIMER_UNIX 1
#define SDL_JOYSTICK_LINUX 1
#define SDL_JOYSTICK_HIDAPI 1
#define SDL_INPUT_LINUXEV 1
#define SDL_THREAD_PTHREAD 1

Expand All @@ -126,8 +127,10 @@
#define SDL_HAPTIC_IOKIT 1
#define SDL_JOYSTICK_IOKIT 1
#define SDL_JOYSTICK_MFI 1
#define SDL_JOYSTICK_HIDAPI 1
#define SDL_TIMER_UNIX 1
#define SDL_THREAD_PTHREAD 1
#define SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1

// Other platforms are not supported (for now)
#else
Expand Down
10 changes: 10 additions & 0 deletions drivers/sdl/joypad_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ void JoypadSDL::process_events() {
device_name,
joypads[joy_id].guid,
joypad_info);

Input::get_singleton()->set_joy_features(joy_id, &joypads[joy_id]);
}
// An event for an attached joypad
} 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)) {
Expand Down Expand Up @@ -299,4 +301,12 @@ void JoypadSDL::close_joypad(int p_pad_idx) {
}
}

SDL_Joystick *JoypadSDL::Joypad::get_sdl_joystick() const {
return SDL_GetJoystickFromID(sdl_instance_idx);
}

SDL_Gamepad *JoypadSDL::Joypad::get_sdl_gamepad() const {
return SDL_GetGamepadFromID(sdl_instance_idx);
}

#endif // SDL_ENABLED
8 changes: 7 additions & 1 deletion drivers/sdl/joypad_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

typedef uint32_t SDL_JoystickID;
typedef struct HWND__ *HWND;
typedef struct SDL_Joystick SDL_Joystick;
typedef struct SDL_Gamepad SDL_Gamepad;

class JoypadSDL {
public:
Expand All @@ -50,14 +52,18 @@ class JoypadSDL {
void process_events();

private:
struct Joypad {
class Joypad : public Input::JoypadFeatures {
public:
bool attached = false;
StringName guid;

SDL_JoystickID sdl_instance_idx;

bool supports_force_feedback = false;
uint64_t ff_effect_timestamp = 0;

SDL_Joystick *get_sdl_joystick() const;
SDL_Gamepad *get_sdl_gamepad() const;
};

static JoypadSDL *singleton;
Expand Down
Loading