added haptic support to sdl and touch (needs testing)

This commit is contained in:
SamGondelman 2016-06-03 10:17:08 -07:00
parent 912b35693b
commit e2c4b4d306
5 changed files with 56 additions and 2 deletions

View file

@ -21,9 +21,10 @@ Joystick::Joystick(SDL_JoystickID instanceId, SDL_GameController* sdlGameControl
InputDevice("GamePad"),
_sdlGameController(sdlGameController),
_sdlJoystick(SDL_GameControllerGetJoystick(_sdlGameController)),
_sdlHaptic(SDL_HapticOpenFromJoystick(_sdlJoystick)),
_instanceId(instanceId)
{
SDL_HapticRumbleInit(_sdlHaptic);
}
Joystick::~Joystick() {
@ -31,6 +32,7 @@ Joystick::~Joystick() {
}
void Joystick::closeJoystick() {
SDL_HapticClose(_sdlHaptic);
SDL_GameControllerClose(_sdlGameController);
}
@ -62,6 +64,13 @@ void Joystick::handleButtonEvent(const SDL_ControllerButtonEvent& event) {
}
}
bool Joystick::triggerHapticPulse(float strength, float duration, bool leftHand) {
if (SDL_HapticRumblePlay(_sdlHaptic, strength, duration) != 0) {
return false;
}
return true;
}
controller::Input::NamedVector Joystick::getAvailableInputs() const {
using namespace controller;
static const Input::NamedVector availableInputs{

View file

@ -36,6 +36,8 @@ public:
virtual QString getDefaultMappingConfig() const override;
virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
virtual void focusOutEvent() override;
bool triggerHapticPulse(float strength, float duration, bool leftHand) override;
Joystick() : InputDevice("GamePad") {}
~Joystick();
@ -52,6 +54,7 @@ public:
private:
SDL_GameController* _sdlGameController;
SDL_Joystick* _sdlJoystick;
SDL_Haptic* _sdlHaptic;
SDL_JoystickID _instanceId;
};

View file

@ -49,7 +49,7 @@ SDL_JoystickID SDL2Manager::getInstanceId(SDL_GameController* controller) {
}
void SDL2Manager::init() {
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0);
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) == 0);
if (initSuccess) {
int joystickCount = SDL_NumJoysticks();

View file

@ -55,6 +55,11 @@ bool OculusControllerManager::activate() {
userInputMapper->registerDevice(_touch);
}
_leftHapticTimer.setSingleShot(true);
_rightHapticTimer.setSingleShot(true);
connect(&_leftHapticTimer, SIGNAL(timeout()), this, SLOT(stopHapticPulse(true)));
connect(&_rightHapticTimer, SIGNAL(timeout()), this, SLOT(stopHapticPulse(false)));
return true;
}
@ -105,6 +110,12 @@ void OculusControllerManager::pluginFocusOutEvent() {
}
}
void OculusControllerManager::stopHapticPulse(bool leftHand) {
if (_touch) {
_touch->stopHapticPulse(leftHand);
}
}
using namespace controller;
static const std::vector<std::pair<ovrButton, StandardButtonChannel>> BUTTON_MAP { {
@ -228,6 +239,26 @@ void OculusControllerManager::TouchDevice::handlePose(float deltaTime,
pose.velocity = toGlm(handPose.LinearVelocity);
}
bool OculusControllerManager::TouchDevice::triggerHapticPulse(float strength, float duration, bool leftHand) {
auto handType = (leftHand ? ovrControllerType_LTouch : ovrControllerType_RTouch);
if (ovr_SetControllerVibration(_parent._session, handType, 1.0f, strength) != ovrSuccess) {
return false;
}
if (leftHand) {
_parent._leftHapticTimer.start(duration);
} else {
_parent._rightHapticTimer.start(duration);
}
return true;
}
void OculusControllerManager::TouchDevice::stopHapticPulse(bool leftHand) {
auto handType = (leftHand ? ovrControllerType_LTouch : ovrControllerType_RTouch);
ovr_SetControllerVibration(_parent._session, handType, 0.0f, 0.0f);
}
controller::Input::NamedVector OculusControllerManager::TouchDevice::getAvailableInputs() const {
using namespace controller;
QVector<Input::NamedPair> availableInputs{

View file

@ -10,6 +10,7 @@
#define hifi__OculusControllerManager
#include <QObject>
#include <QTimer>
#include <unordered_set>
#include <GLMHelpers.h>
@ -32,6 +33,9 @@ public:
void pluginFocusOutEvent() override;
void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
private slots:
void stopHapticPulse(bool leftHand);
private:
class OculusInputDevice : public controller::InputDevice {
public:
@ -64,6 +68,11 @@ private:
void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
void focusOutEvent() override;
bool triggerHapticPulse(float strength, float duration, bool leftHand) override;
private:
void stopHapticPulse(bool leftHand);
private:
void handlePose(float deltaTime, const controller::InputCalibrationData& inputCalibrationData, ovrHandType hand, const ovrPoseStatef& handPose);
int _trackedControllers { 0 };
@ -73,6 +82,8 @@ private:
ovrSession _session { nullptr };
ovrInputState _inputState {};
RemoteDevice::Pointer _remote;
QTimer _leftHapticTimer;
QTimer _rightHapticTimer;
TouchDevice::Pointer _touch;
static const QString NAME;
};