]> Devoid-pointer.net GitWeb - FFBChecker.git/commitdiff
Add support for constant effect via SDL2
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 31 Jul 2015 11:36:04 +0000 (13:36 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 31 Jul 2015 11:36:04 +0000 (13:36 +0200)
CMakeLists.txt
ffbeffectparameters.h
linuxffbeffect.cpp
sdl2ffbconstanteffect.cpp [new file with mode: 0644]
sdl2ffbconstanteffect.h [new file with mode: 0644]
sdl2ffbeffect.cpp [new file with mode: 0644]
sdl2ffbeffect.h [new file with mode: 0644]
sdl2ffbeffectfactory.cpp
sdl2ffbeffectfactory.h

index a1bd4e27002f3878e4a49cea2b98e89aac9778f7..eabde32eee2bd5891f79ecbbe5134b0894781449 100644 (file)
@@ -56,6 +56,8 @@ if (SDL2_FOUND)
         ${FFBChecker_SRCS}
         sdl2ffbeffectfactory.cpp
         sdl2ffbdevice.cpp
+        sdl2ffbeffect.cpp
+        sdl2ffbconstanteffect.cpp
         sdl2deviceprober.cpp)
 endif()
 
index 9db30d97a4bd8b3b960390ca6c80344764d24f75..b31dbaeffb905d56b4796c41c7511cfa5fcbf643 100644 (file)
@@ -19,7 +19,7 @@ public:
   int direction;
   int repeat;
   int replayDelay;
-  int replayLength;
+  int64_t replayLength;
 };
 
 #endif // FFBEFFECTPARAMETERS_H
index 26e0e29cb8bdb198fbd6ce514d3004dc815460b3..75a1b113cc63b94c0d727aaa177f2898641d6bbf 100644 (file)
@@ -46,7 +46,7 @@ bool LinuxFFBEffect::checkGenericParameters(const std::shared_ptr<FFBEffectParam
     return false;
   }
 
-  if (!checkBoundsInclusive(params->replayLength, 0, 0x7FFF)) {
+  if (!checkBoundsInclusive(params->replayLength, static_cast<int64_t>(0), static_cast<int64_t>(0x7FFF))) {
     reportError("Replay length out of bounds.");
     return false;
   }
diff --git a/sdl2ffbconstanteffect.cpp b/sdl2ffbconstanteffect.cpp
new file mode 100644 (file)
index 0000000..d21f1db
--- /dev/null
@@ -0,0 +1,65 @@
+#include "sdl2ffbconstanteffect.h"
+#include "globalsettings.h"
+#include <QtWidgets/QMessageBox>
+
+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<FFBEffectParameters> params)
+{
+  try {
+    const std::shared_ptr<FFBConstantEffectParameters> iParams = std::dynamic_pointer_cast<FFBConstantEffectParameters>(params);
+    return setParameters(iParams);
+  } catch (std::bad_cast& ) {
+    return false;
+  }
+}
+
+
+bool SDL2FFBConstantEffect::setParameters(const std::shared_ptr<FFBConstantEffectParameters> 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 (file)
index 0000000..c7fa5c8
--- /dev/null
@@ -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<FFBEffectParameters> parameters() const { return m_params; }
+  bool setParameters(const std::shared_ptr<FFBEffectParameters> params);
+  bool setParameters(const std::shared_ptr<FFBConstantEffectParameters> params);
+
+private:
+  std::shared_ptr<FFBConstantEffectParameters> m_params;
+};
+
+#endif // SDL2FFBCONSTANTEFFECT_H
diff --git a/sdl2ffbeffect.cpp b/sdl2ffbeffect.cpp
new file mode 100644 (file)
index 0000000..9133d3d
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2015 <copyright holder> <email>
+ * 
+ * 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 <QtWidgets/QMessageBox>
+
+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<FFBEffectParameters> 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<int64_t>(0), static_cast<int64_t>(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 (file)
index 0000000..833727f
--- /dev/null
@@ -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<FFBEffectParameters> params);
+  bool checkEnvelopeParameters(const int attackLength, const int attackLevel, const int fadeLength, const int fadeLevel);
+
+  static const QString CAPTION;
+};
+
+#endif // SDL2FFBEFFECT_H
index 5ace543da59af4785005ac2a0b577ee7245ed0b0..8682e9066d8042c307bb5b1f9c536848ef7f1dad 100644 (file)
@@ -5,6 +5,8 @@ std::shared_ptr<FFBEffect> SDL2FFBEffectFactory::createEffect(FFBEffectTypes typ
   switch (type) {
     case FFBEffectTypes::NONE:
       return std::shared_ptr<FFBEffect>(new FFBNullEffect());
+    case FFBEffectTypes::CONSTANT:
+      return std::shared_ptr<SDL2FFBEffect>(new SDL2FFBConstantEffect());
     default:
       return std::shared_ptr<FFBEffect>(new FFBNullEffect());
   }
index 599b0216f602050fdc428459ee77f3726a997de3..1ca167c09c13a15a3ad179bc672c4a7c42b94051 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "globals.h"
 #include "ffbnulleffect.h"
+#include "sdl2ffbconstanteffect.h"
 
 class SDL2FFBEffectFactory
 {