From 72a590e34cce4116618840c33499fe3d7f2f57fc Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 28 Jul 2015 11:02:56 -0700 Subject: [PATCH] Initialize, start, and stop eye tracking with simulated data --- interface/src/Application.cpp | 14 +++--- interface/src/devices/EyeTracker.cpp | 65 ++++++++++++++++++++++++++++ interface/src/devices/EyeTracker.h | 12 +++++ 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1c7b2ff2df..217b8f334b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -595,6 +595,12 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : loadScripts(); } +#ifdef HAVE_IVIEWHMD + // Do this before loading settings + auto eyeTracker = DependencyManager::get(); + eyeTracker->init(); +#endif + loadSettings(); int SAVE_SETTINGS_INTERVAL = 10 * MSECS_PER_SECOND; // Let's save every seconds for now connect(&_settingsTimer, &QTimer::timeout, this, &Application::saveSettings); @@ -636,11 +642,6 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(ddeTracker.data(), &FaceTracker::muteToggled, this, &Application::faceTrackerMuteToggled); #endif -#ifdef HAVE_IVIEWHMD - auto eyeTracker = DependencyManager::get(); - eyeTracker->init(); -#endif - auto applicationUpdater = DependencyManager::get(); connect(applicationUpdater.data(), &AutoUpdater::newVersionIsAvailable, dialogsManager.data(), &DialogsManager::showUpdateDialog); applicationUpdater->checkForUpdate(); @@ -703,6 +704,9 @@ void Application::cleanupBeforeQuit() { #ifdef HAVE_DDE DependencyManager::destroy(); #endif +#ifdef HAVE_IVIEWHMD + DependencyManager::destroy(); +#endif } void Application::emptyLocalCache() { diff --git a/interface/src/devices/EyeTracker.cpp b/interface/src/devices/EyeTracker.cpp index 3e0d9b78dd..ce6afc1d97 100644 --- a/interface/src/devices/EyeTracker.cpp +++ b/interface/src/devices/EyeTracker.cpp @@ -11,10 +11,75 @@ #include "EyeTracker.h" +#include "InterfaceLogging.h" + +#ifdef HAVE_IVIEWHMD +static void CALLBACK eyeTrackerCallback(smi_CallbackDataStruct* data) { + auto eyeTracker = DependencyManager::get(); + if (eyeTracker) { // Guard against a few callbacks that continue to be received after smi_quit(). + eyeTracker->processData(data); + } +} +#endif + +EyeTracker::~EyeTracker() { +#ifdef HAVE_IVIEWHMD + int result = smi_quit(); + if (result != SMI_RET_SUCCESS) { + qCWarning(interfaceapp) << "Eye Tracker: Error terminating tracking:" << result; + } +#endif +} + +void EyeTracker::processData(smi_CallbackDataStruct* data) { + if (!_isEnabled) { + return; + } + +#ifdef HAVE_IVIEWHMD + if (data->type == SMI_SIMPLE_GAZE_SAMPLE) { + smi_SampleHMDStruct* sample = (smi_SampleHMDStruct*)data->result; + } +#endif +} + void EyeTracker::init() { + if (_isInitialized) { + qCWarning(interfaceapp) << "Eye Tracker: Already initialized"; + return; + } + +#ifdef HAVE_IVIEWHMD + int result = smi_setCallback(eyeTrackerCallback); + if (result != SMI_RET_SUCCESS) { + qCWarning(interfaceapp) << "Eye Tracker: Error setting callback:" << result; + } else { + _isInitialized = true; + } +#endif } void EyeTracker::setEnabled(bool enabled) { + if (!_isInitialized) { + qCWarning(interfaceapp) << "Eye Tracker: Not initialized before setting enabled"; + return; + } + +#ifdef HAVE_IVIEWHMD + qCDebug(interfaceapp) << "Eye Tracker: Set enabled =" << enabled; + + if (enabled && !_isStreaming) { + // There is no smi_stopStreaming() method so start streaming a maximum of once per program run. + int result = smi_startStreaming(true); + if (result != SMI_RET_SUCCESS) { + qCWarning(interfaceapp) << "Eye Tracker: Error starting streaming:" << result; + } else { + _isStreaming = true; + } + } + + _isEnabled = enabled && _isStreaming; +#endif } void EyeTracker::reset() { diff --git a/interface/src/devices/EyeTracker.h b/interface/src/devices/EyeTracker.h index a5529770a9..1410da3669 100644 --- a/interface/src/devices/EyeTracker.h +++ b/interface/src/devices/EyeTracker.h @@ -15,15 +15,27 @@ #include #include +#include + class EyeTracker : public QObject, public Dependency { Q_OBJECT SINGLETON_DEPENDENCY +public: + ~EyeTracker(); + + void processData(smi_CallbackDataStruct* data); + public slots: void init(); void setEnabled(bool enabled); void reset(); + +private: + bool _isInitialized = false; + bool _isStreaming = false; + bool _isEnabled = false; }; #endif // hifi_EyeTracker_h