From f665200df7bd6d1660f6dd8255ad66f5c675770d Mon Sep 17 00:00:00 2001 From: Wilhem Barbier Date: Wed, 15 Jun 2016 07:25:35 +0200 Subject: Add joystick vibration support on Linux (#5043) --- main/input_default.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ main/input_default.h | 17 +++++++++++++++++ 2 files changed, 64 insertions(+) (limited to 'main') diff --git a/main/input_default.cpp b/main/input_default.cpp index e36306727d..a6f14ae1f5 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -137,6 +137,30 @@ String InputDefault::get_joy_name(int p_idx) { return joy_names[p_idx].name; }; +Vector2 InputDefault::get_joy_vibration_strength(int p_device) { + if (joy_vibration.has(p_device)) { + return Vector2(joy_vibration[p_device].weak_magnitude, joy_vibration[p_device].strong_magnitude); + } else { + return Vector2(0, 0); + } +} + +uint64_t InputDefault::get_joy_vibration_timestamp(int p_device) { + if (joy_vibration.has(p_device)) { + return joy_vibration[p_device].timestamp; + } else { + return 0; + } +} + +float InputDefault::get_joy_vibration_duration(int p_device) { + if (joy_vibration.has(p_device)) { + return joy_vibration[p_device].duration; + } else { + return 0.f; + } +} + static String _hex_str(uint8_t p_byte) { static const char* dict = "0123456789abcdef"; @@ -294,6 +318,29 @@ void InputDefault::set_joy_axis(int p_device,int p_axis,float p_value) { _joy_axis[c]=p_value; } +void InputDefault::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) { + return; + } + VibrationInfo vibration; + vibration.weak_magnitude = p_weak_magnitude; + vibration.strong_magnitude = p_strong_magnitude; + vibration.duration = p_duration; + vibration.timestamp = OS::get_singleton()->get_unix_time(); + joy_vibration[p_device] = vibration; +} + +void InputDefault::stop_joy_vibration(int p_device) { + _THREAD_SAFE_METHOD_ + VibrationInfo vibration; + vibration.weak_magnitude = 0; + vibration.strong_magnitude = 0; + vibration.duration = 0; + vibration.timestamp = OS::get_singleton()->get_unix_time(); + joy_vibration[p_device] = vibration; +} + void InputDefault::set_accelerometer(const Vector3& p_accel) { _THREAD_SAFE_METHOD_ diff --git a/main/input_default.h b/main/input_default.h index 8f6a430436..01b813f3ca 100644 --- a/main/input_default.h +++ b/main/input_default.h @@ -3,6 +3,7 @@ #include "os/input.h" + class InputDefault : public Input { OBJ_TYPE( InputDefault, Input ); @@ -19,6 +20,16 @@ class InputDefault : public Input { MainLoop *main_loop; bool emulate_touch; + + struct VibrationInfo { + float weak_magnitude; + float strong_magnitude; + float duration; // Duration in seconds + uint64_t timestamp; + }; + + Map joy_vibration; + struct SpeedTrack { uint64_t last_tick; @@ -129,6 +140,9 @@ public: virtual float get_joy_axis(int p_device,int p_axis); String get_joy_name(int p_idx); + virtual Vector2 get_joy_vibration_strength(int p_device); + virtual float get_joy_vibration_duration(int p_device); + virtual uint64_t get_joy_vibration_timestamp(int p_device); void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = ""); void parse_joystick_mapping(String p_mapping, bool p_update_existing); @@ -147,6 +161,9 @@ public: void set_magnetometer(const Vector3& p_magnetometer); void set_joy_axis(int p_device,int p_axis,float p_value); + virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration); + virtual void stop_joy_vibration(int p_device); + void set_main_loop(MainLoop *main_loop); void set_mouse_pos(const Point2& p_posf); -- cgit v1.2.3