From: Michal MalĂ˝ Date: Fri, 31 Jul 2015 11:36:04 +0000 (+0200) Subject: Add support for constant effect via SDL2 X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=a4fc847af0904adfa57e395ef72e5f8a01702bc5;p=FFBChecker.git Add support for constant effect via SDL2 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a1bd4e2..eabde32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,8 @@ if (SDL2_FOUND) ${FFBChecker_SRCS} sdl2ffbeffectfactory.cpp sdl2ffbdevice.cpp + sdl2ffbeffect.cpp + sdl2ffbconstanteffect.cpp sdl2deviceprober.cpp) endif() diff --git a/ffbeffectparameters.h b/ffbeffectparameters.h index 9db30d9..b31dbae 100644 --- a/ffbeffectparameters.h +++ b/ffbeffectparameters.h @@ -19,7 +19,7 @@ public: int direction; int repeat; int replayDelay; - int replayLength; + int64_t replayLength; }; #endif // FFBEFFECTPARAMETERS_H diff --git a/linuxffbeffect.cpp b/linuxffbeffect.cpp index 26e0e29..75a1b11 100644 --- a/linuxffbeffect.cpp +++ b/linuxffbeffect.cpp @@ -46,7 +46,7 @@ bool LinuxFFBEffect::checkGenericParameters(const std::shared_ptrreplayLength, 0, 0x7FFF)) { + if (!checkBoundsInclusive(params->replayLength, static_cast(0), static_cast(0x7FFF))) { reportError("Replay length out of bounds."); return false; } diff --git a/sdl2ffbconstanteffect.cpp b/sdl2ffbconstanteffect.cpp new file mode 100644 index 0000000..d21f1db --- /dev/null +++ b/sdl2ffbconstanteffect.cpp @@ -0,0 +1,65 @@ +#include "sdl2ffbconstanteffect.h" +#include "globalsettings.h" +#include + +SDL2FFBConstantEffect::SDL2FFBConstantEffect() : + SDL2FFBEffect(FFBEffectTypes::CONSTANT) +{} + +SDL_HapticEffect* SDL2FFBConstantEffect::createFFStruct() +{ + SDL_HapticEffect* effect = SDL2FFBEffect::createFFstruct(); + if (effect == nullptr) + return nullptr; + + effect->type = SDL_HAPTIC_CONSTANT; + effect->constant.direction.type = SDL_HAPTIC_POLAR; + effect->constant.direction.dir[0] = m_params->direction; + + if (m_params->replayLength == 0) + effect->constant.length = SDL_HAPTIC_INFINITY; + else + effect->constant.length = m_params->replayLength; + effect->constant.delay = m_params->replayDelay; + + effect->constant.attack_length = m_params->attackLength; + effect->constant.attack_level = m_params->attackLevel; + effect->constant.fade_length = m_params->fadeLength; + effect->constant.fade_level = m_params->fadeLevel; + + effect->constant.level = m_params->level; + + return effect; +} + +bool SDL2FFBConstantEffect::setParameters(const std::shared_ptr params) +{ + try { + const std::shared_ptr iParams = std::dynamic_pointer_cast(params); + return setParameters(iParams); + } catch (std::bad_cast& ) { + return false; + } +} + + +bool SDL2FFBConstantEffect::setParameters(const std::shared_ptr params) +{ + if (!GlobalSettings::GS()->doSanityChecks) + return true; + + if (!checkGenericParameters(m_params)) + return false; + + if (!checkEnvelopeParameters(m_params->attackLength, m_params->attackLevel, m_params->fadeLength, m_params->fadeLevel)) + return false; + + if (!checkBoundsInclusive(m_params->level, -0x7FFF, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Level parameters must be within <-32767; 32767>"); + return false; + } + + m_params = params; + return true; +} + diff --git a/sdl2ffbconstanteffect.h b/sdl2ffbconstanteffect.h new file mode 100644 index 0000000..c7fa5c8 --- /dev/null +++ b/sdl2ffbconstanteffect.h @@ -0,0 +1,20 @@ +#ifndef SDL2FFBCONSTANTEFFECT_H +#define SDL2FFBCONSTANTEFFECT_H + +#include "sdl2ffbeffect.h" +#include "ffbconstanteffectparameters.h" + +class SDL2FFBConstantEffect : public SDL2FFBEffect +{ +public: + explicit SDL2FFBConstantEffect(); + SDL_HapticEffect* createFFStruct(); + inline const std::shared_ptr parameters() const { return m_params; } + bool setParameters(const std::shared_ptr params); + bool setParameters(const std::shared_ptr params); + +private: + std::shared_ptr m_params; +}; + +#endif // SDL2FFBCONSTANTEFFECT_H diff --git a/sdl2ffbeffect.cpp b/sdl2ffbeffect.cpp new file mode 100644 index 0000000..9133d3d --- /dev/null +++ b/sdl2ffbeffect.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2015 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "sdl2ffbeffect.h" +#include "globalsettings.h" +#include + +const QString SDL2FFBEffect::CAPTION("Invalid FFB parameter"); + +SDL_HapticEffect* SDL2FFBEffect::createFFstruct() +{ + return new SDL_HapticEffect; +} + +bool SDL2FFBEffect::checkEnvelopeParameters(const int attackLength, const int attackLevel, const int fadeLength, const int fadeLevel) +{ + if (!GlobalSettings::GS()->doSanityChecks) + return true; + + if (!checkBoundsInclusive(attackLength, 0, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Attack length must be within <0; 32767>"); + return false; + } + + if (!checkBoundsInclusive(attackLevel, 0, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Attack level must be within <0; 32767>"); + return false; + } + + if (!checkBoundsInclusive(fadeLength, 0, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Fade length must be within <0; 32767>"); + return false; + } + + if (!checkBoundsInclusive(fadeLevel, 0, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Fade level must be within <0; 32767>"); + return false; + } + + return true; +} + +bool SDL2FFBEffect::checkGenericParameters(const std::shared_ptr params) +{ + if (!GlobalSettings::GS()->doSanityChecks) + return true; + + if (!checkBoundsInclusive(params->direction, 0, 35999)) { + QMessageBox::warning(nullptr, CAPTION, "Direction must be within <0; 35999)"); + return false; + } + + if (!checkBoundsInclusive(params->replayLength, static_cast(0), static_cast(0x7FFF))) { + QMessageBox::warning(nullptr, CAPTION, "Replay length must be within <0; 32767>"); + return false; + } + + if (!checkBoundsInclusive(params->replayDelay, 0, 0xFFFF)) { + QMessageBox::warning(nullptr, CAPTION, "Replay delay must be within <0; 65535>"); + return false; + } + + return true; +} + + diff --git a/sdl2ffbeffect.h b/sdl2ffbeffect.h new file mode 100644 index 0000000..833727f --- /dev/null +++ b/sdl2ffbeffect.h @@ -0,0 +1,20 @@ +#ifndef SDL2FFBEFFECT_H +#define SDL2FFBEFFECT_H + +#include "ffbeffect.h" +#include "SDL.h" + +class SDL2FFBEffect : public FFBEffect +{ +public: + explicit SDL2FFBEffect(FFBEffectTypes type) : FFBEffect(type) {} + virtual SDL_HapticEffect* createFFstruct(); + +protected: + virtual bool checkGenericParameters(const std::shared_ptr params); + bool checkEnvelopeParameters(const int attackLength, const int attackLevel, const int fadeLength, const int fadeLevel); + + static const QString CAPTION; +}; + +#endif // SDL2FFBEFFECT_H diff --git a/sdl2ffbeffectfactory.cpp b/sdl2ffbeffectfactory.cpp index 5ace543..8682e90 100644 --- a/sdl2ffbeffectfactory.cpp +++ b/sdl2ffbeffectfactory.cpp @@ -5,6 +5,8 @@ std::shared_ptr SDL2FFBEffectFactory::createEffect(FFBEffectTypes typ switch (type) { case FFBEffectTypes::NONE: return std::shared_ptr(new FFBNullEffect()); + case FFBEffectTypes::CONSTANT: + return std::shared_ptr(new SDL2FFBConstantEffect()); default: return std::shared_ptr(new FFBNullEffect()); } diff --git a/sdl2ffbeffectfactory.h b/sdl2ffbeffectfactory.h index 599b021..1ca167c 100644 --- a/sdl2ffbeffectfactory.h +++ b/sdl2ffbeffectfactory.h @@ -3,6 +3,7 @@ #include "globals.h" #include "ffbnulleffect.h" +#include "sdl2ffbconstanteffect.h" class SDL2FFBEffectFactory {