From 298ac650d8ea36f12d877d28ce35ac585e6d4c8f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 20 Oct 2015 21:25:49 -0700 Subject: [PATCH] Isolate sixense dynamic linking for OSX --- .../src/input-plugins/SixenseManager.cpp | 101 +++--------------- .../src/input-plugins/SixenseManager.h | 18 ---- .../src/input-plugins/SixenseSupportOSX.cpp | 97 +++++++++++++++++ 3 files changed, 109 insertions(+), 107 deletions(-) create mode 100644 libraries/input-plugins/src/input-plugins/SixenseSupportOSX.cpp diff --git a/libraries/input-plugins/src/input-plugins/SixenseManager.cpp b/libraries/input-plugins/src/input-plugins/SixenseManager.cpp index 33b4332430..24b1b60356 100644 --- a/libraries/input-plugins/src/input-plugins/SixenseManager.cpp +++ b/libraries/input-plugins/src/input-plugins/SixenseManager.cpp @@ -9,12 +9,21 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include "SixenseManager.h" + +#ifdef HAVE_SIXENSE +#include "sixense.h" +#endif #include #include #include +// TODO: This should not be here +#include +Q_DECLARE_LOGGING_CATEGORY(inputplugins) +Q_LOGGING_CATEGORY(inputplugins, "hifi.inputplugins") + #include #include #include @@ -25,40 +34,12 @@ #include #include -#include "SixenseManager.h" - - -#ifdef HAVE_SIXENSE - #include "sixense.h" - -#ifdef __APPLE__ -static QLibrary* _sixenseLibrary { nullptr }; -#endif - -#endif - -// TODO: This should not be here -#include -Q_DECLARE_LOGGING_CATEGORY(inputplugins) -Q_LOGGING_CATEGORY(inputplugins, "hifi.inputplugins") - -#ifdef HAVE_SIXENSE - +#include "UserActivityLogger.h" const glm::vec3 SixenseManager::DEFAULT_AVATAR_POSITION { -0.25f, -0.35f, -0.3f }; // in hydra frame const float SixenseManager::CONTROLLER_THRESHOLD { 0.35f }; const float SixenseManager::DEFAULT_REACH_LENGTH { 1.5f }; -#endif - -#ifdef __APPLE__ -typedef int (*SixenseBaseFunction)(); -typedef int (*SixenseTakeIntFunction)(int); -#ifdef HAVE_SIXENSE -typedef int (*SixenseTakeIntAndSixenseControllerData)(int, sixenseControllerData*); -#endif -#endif - const QString SixenseManager::NAME = "Sixense"; const QString SixenseManager::HYDRA_ID_STRING = "Razer Hydra"; @@ -94,31 +75,6 @@ void SixenseManager::activate() { auto userInputMapper = DependencyManager::get(); userInputMapper->registerDevice(_inputDevice); -#ifdef __APPLE__ - - if (!_sixenseLibrary) { - -#ifdef SIXENSE_LIB_FILENAME - _sixenseLibrary = new QLibrary(SIXENSE_LIB_FILENAME); -#else - const QString SIXENSE_LIBRARY_NAME = "libsixense_x64"; - QString frameworkSixenseLibrary = QCoreApplication::applicationDirPath() + "/../Frameworks/" - + SIXENSE_LIBRARY_NAME; - - _sixenseLibrary = new QLibrary(frameworkSixenseLibrary); -#endif - } - - if (_sixenseLibrary->load()){ - qCDebug(inputplugins) << "Loaded sixense library for hydra support -" << _sixenseLibrary->fileName(); - } else { - qCDebug(inputplugins) << "Sixense library at" << _sixenseLibrary->fileName() << "failed to load." - << "Continuing without hydra support."; - return; - } - - SixenseBaseFunction sixenseInit = (SixenseBaseFunction) _sixenseLibrary->resolve("sixenseInit"); -#endif loadSettings(); sixenseInit(); #endif @@ -139,26 +95,13 @@ void SixenseManager::deactivate() { userInputMapper->removeDevice(_inputDevice->_deviceID); } -#ifdef __APPLE__ - SixenseBaseFunction sixenseExit = (SixenseBaseFunction)_sixenseLibrary->resolve("sixenseExit"); -#endif - sixenseExit(); - -#ifdef __APPLE__ - delete _sixenseLibrary; -#endif - #endif } void SixenseManager::setSixenseFilter(bool filter) { #ifdef HAVE_SIXENSE -#ifdef __APPLE__ - SixenseTakeIntFunction sixenseSetFilterEnabled = (SixenseTakeIntFunction) _sixenseLibrary->resolve("sixenseSetFilterEnabled"); -#endif - int newFilter = filter ? 1 : 0; - sixenseSetFilterEnabled(newFilter); + sixenseSetFilterEnabled(filter ? 1 : 0); #endif } @@ -180,11 +123,6 @@ void SixenseManager::InputDevice::update(float deltaTime, bool jointsCaptured) { #ifdef HAVE_SIXENSE _buttonPressedMap.clear(); -#ifdef __APPLE__ - SixenseBaseFunction sixenseGetNumActiveControllers = - (SixenseBaseFunction) _sixenseLibrary->resolve("sixenseGetNumActiveControllers"); -#endif - auto userInputMapper = DependencyManager::get(); static const float MAX_DISCONNECTED_TIME = 2.0f; @@ -213,24 +151,11 @@ void SixenseManager::InputDevice::update(float deltaTime, bool jointsCaptured) { // FIXME send this message once when we've positively identified hydra hardware //UserActivityLogger::getInstance().connectedDevice("spatial_controller", "hydra"); -#ifdef __APPLE__ - SixenseBaseFunction sixenseGetMaxControllers = - (SixenseBaseFunction) _sixenseLibrary->resolve("sixenseGetMaxControllers"); -#endif - int maxControllers = sixenseGetMaxControllers(); // we only support two controllers sixenseControllerData controllers[2]; -#ifdef __APPLE__ - SixenseTakeIntFunction sixenseIsControllerEnabled = - (SixenseTakeIntFunction) _sixenseLibrary->resolve("sixenseIsControllerEnabled"); - - SixenseTakeIntAndSixenseControllerData sixenseGetNewestData = - (SixenseTakeIntAndSixenseControllerData) _sixenseLibrary->resolve("sixenseGetNewestData"); -#endif - int numActiveControllers = 0; for (int i = 0; i < maxControllers && numActiveControllers < 2; i++) { if (!sixenseIsControllerEnabled(i)) { @@ -479,8 +404,6 @@ void SixenseManager::InputDevice::handlePoseEvent(float deltaTime, glm::vec3 pos glm::vec3 velocity(0.0f); glm::quat angularVelocity; - - if (prevPose.isValid() && deltaTime > std::numeric_limits::epsilon()) { velocity = (position - prevPose.getTranslation()) / deltaTime; diff --git a/libraries/input-plugins/src/input-plugins/SixenseManager.h b/libraries/input-plugins/src/input-plugins/SixenseManager.h index 062a27390c..bf657d347b 100644 --- a/libraries/input-plugins/src/input-plugins/SixenseManager.h +++ b/libraries/input-plugins/src/input-plugins/SixenseManager.h @@ -12,18 +12,6 @@ #ifndef hifi_SixenseManager_h #define hifi_SixenseManager_h -#ifdef HAVE_SIXENSE - #include - #include - #include "sixense.h" - -#ifdef __APPLE__ - #include - #include -#endif - -#endif - #include #include @@ -31,8 +19,6 @@ #include "InputPlugin.h" -class QLibrary; - const unsigned int BUTTON_0 = 1U << 0; // the skinny button between 1 and 2 const unsigned int BUTTON_1 = 1U << 5; const unsigned int BUTTON_2 = 1U << 6; @@ -75,7 +61,6 @@ private: static const float CONTROLLER_THRESHOLD; static const float DEFAULT_REACH_LENGTH; - using Samples = std::pair< MovingAverage< glm::vec3, MAX_NUM_AVERAGING_SAMPLES>, MovingAverage< glm::vec4, MAX_NUM_AVERAGING_SAMPLES> >; using MovingAverageMap = std::map< int, Samples >; @@ -113,9 +98,6 @@ private: glm::vec3 _reachRight; }; - - - bool _useSixenseFilter = true; std::shared_ptr _inputDevice { std::make_shared() }; static const QString NAME; diff --git a/libraries/input-plugins/src/input-plugins/SixenseSupportOSX.cpp b/libraries/input-plugins/src/input-plugins/SixenseSupportOSX.cpp new file mode 100644 index 0000000000..c5c3f7f14a --- /dev/null +++ b/libraries/input-plugins/src/input-plugins/SixenseSupportOSX.cpp @@ -0,0 +1,97 @@ +// +// SixenseSupportOSX.cpp +// +// +// Created by Clement on 10/20/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifdef __APPLE__ +#include "sixense.h" + +#include +#include +#include + +using std::string; +using std::unique_ptr; +using SixenseBaseFunction = int (*)(); +using SixenseTakeIntFunction = int (*)(int); +using SixenseTakeIntAndSixenseControllerData = int (*)(int, sixenseControllerData*); + +static unique_ptr SIXENSE; + +bool loadSixense() { + if (!SIXENSE) { + static const QString LIBRARY_PATH = +#ifdef SIXENSE_LIB_FILENAME + SIXENSE_LIB_FILENAME; +#else + QCoreApplication::applicationDirPath() + "/../Frameworks/libsixense_x64"; +#endif + SIXENSE.reset(new QLibrary(LIBRARY_PATH)); + } + + if (SIXENSE->load()){ + qDebug() << "Loaded sixense library for hydra support -" << SIXENSE->fileName(); + } else { + qDebug() << "Sixense library at" << SIXENSE->fileName() << "failed to load:" << SIXENSE->errorString(); + qDebug() << "Continuing without hydra support."; + } + return SIXENSE->isLoaded(); +} + +void unloadSixense() { + SIXENSE->unload(); +} + +template +Func resolve(const char* name) { + Q_ASSERT_X(SIXENSE && SIXENSE->isLoaded(), __FUNCTION__, "Sixense library not loaded"); + auto func = reinterpret_cast(SIXENSE->resolve(name)); + Q_ASSERT_X(func, __FUNCTION__, string("Could not resolve ").append(name).c_str()); + return func; +} + +// sixense.h wrapper for OSX dynamic linking +int sixenseInit() { + loadSixense(); + auto sixenseInit = resolve("sixenseInit"); + return sixenseInit(); +} + +int sixenseExit() { + auto sixenseExit = resolve("sixenseExit"); + auto returnCode = sixenseExit(); + unloadSixense(); + return returnCode; +} + +int sixenseSetFilterEnabled(int input) { + auto sixenseSetFilterEnabled = resolve("sixenseSetFilterEnabled"); + return sixenseSetFilterEnabled(input); +} + +int sixenseGetNumActiveControllers() { + auto sixenseGetNumActiveControllers = resolve("sixenseGetNumActiveControllers"); + return sixenseGetNumActiveControllers(); +} + +int sixenseGetMaxControllers() { + auto sixenseGetMaxControllers = resolve("sixenseGetMaxControllers"); + return sixenseGetMaxControllers(); +} + +int sixenseIsControllerEnabled(int input) { + auto sixenseIsControllerEnabled = resolve("sixenseIsControllerEnabled"); + return sixenseIsControllerEnabled(input); +} + +int sixenseGetNewestData(int input1, sixenseControllerData* intput2) { + auto sixenseGetNewestData = resolve("sixenseGetNewestData"); + return sixenseGetNewestData(input1, intput2); +} +#endif