Isolate sixense dynamic linking for OSX

This commit is contained in:
Atlante45 2015-10-20 21:25:49 -07:00
parent 143e92ee1b
commit 298ac650d8
3 changed files with 109 additions and 107 deletions

View file

@ -9,12 +9,21 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <vector>
#include "SixenseManager.h"
#ifdef HAVE_SIXENSE
#include "sixense.h"
#endif
#include <QCoreApplication>
#include <QtCore/QSysInfo>
#include <QtGlobal>
// TODO: This should not be here
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(inputplugins)
Q_LOGGING_CATEGORY(inputplugins, "hifi.inputplugins")
#include <GLMHelpers.h>
#include <NumericalConstants.h>
#include <PerfStat.h>
@ -25,40 +34,12 @@
#include <UserActivityLogger.h>
#include <controllers/UserInputMapper.h>
#include "SixenseManager.h"
#ifdef HAVE_SIXENSE
#include "sixense.h"
#ifdef __APPLE__
static QLibrary* _sixenseLibrary { nullptr };
#endif
#endif
// TODO: This should not be here
#include <QLoggingCategory>
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<controller::UserInputMapper>();
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<controller::UserInputMapper>();
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<float>::epsilon()) {
velocity = (position - prevPose.getTranslation()) / deltaTime;

View file

@ -12,18 +12,6 @@
#ifndef hifi_SixenseManager_h
#define hifi_SixenseManager_h
#ifdef HAVE_SIXENSE
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include "sixense.h"
#ifdef __APPLE__
#include <QCoreApplication>
#include <qlibrary.h>
#endif
#endif
#include <SimpleMovingAverage.h>
#include <controllers/InputDevice.h>
@ -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> _inputDevice { std::make_shared<InputDevice>() };
static const QString NAME;

View file

@ -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 <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QLibrary>
using std::string;
using std::unique_ptr;
using SixenseBaseFunction = int (*)();
using SixenseTakeIntFunction = int (*)(int);
using SixenseTakeIntAndSixenseControllerData = int (*)(int, sixenseControllerData*);
static unique_ptr<QLibrary> 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<typename Func>
Func resolve(const char* name) {
Q_ASSERT_X(SIXENSE && SIXENSE->isLoaded(), __FUNCTION__, "Sixense library not loaded");
auto func = reinterpret_cast<Func>(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<SixenseBaseFunction>("sixenseInit");
return sixenseInit();
}
int sixenseExit() {
auto sixenseExit = resolve<SixenseBaseFunction>("sixenseExit");
auto returnCode = sixenseExit();
unloadSixense();
return returnCode;
}
int sixenseSetFilterEnabled(int input) {
auto sixenseSetFilterEnabled = resolve<SixenseTakeIntFunction>("sixenseSetFilterEnabled");
return sixenseSetFilterEnabled(input);
}
int sixenseGetNumActiveControllers() {
auto sixenseGetNumActiveControllers = resolve<SixenseBaseFunction>("sixenseGetNumActiveControllers");
return sixenseGetNumActiveControllers();
}
int sixenseGetMaxControllers() {
auto sixenseGetMaxControllers = resolve<SixenseBaseFunction>("sixenseGetMaxControllers");
return sixenseGetMaxControllers();
}
int sixenseIsControllerEnabled(int input) {
auto sixenseIsControllerEnabled = resolve<SixenseTakeIntFunction>("sixenseIsControllerEnabled");
return sixenseIsControllerEnabled(input);
}
int sixenseGetNewestData(int input1, sixenseControllerData* intput2) {
auto sixenseGetNewestData = resolve<SixenseTakeIntAndSixenseControllerData>("sixenseGetNewestData");
return sixenseGetNewestData(input1, intput2);
}
#endif