33
#include "SensorManager.h"
37
#include <Inventor/SbTime.h>
38
#include <Inventor/SoDB.h>
39
#include <Inventor/SoRenderManager.h>
40
#include <Inventor/C/threads/thread.h>
42
#include "SignalThread.h"
45
using namespace SIM::Coin3D::Quarter;
47
SensorManager::SensorManager()
50
this->mainthreadid = cc_thread_id();
51
this->signalthread = new SignalThread();
53
QObject::connect(this->signalthread, &SignalThread::triggerSignal,
54
this, &SensorManager::sensorQueueChanged);
56
this->idletimer = new QTimer;
57
this->delaytimer = new QTimer;
58
this->timerqueuetimer = new QTimer;
60
this->idletimer->setSingleShot(true);
61
this->delaytimer->setSingleShot(true);
62
this->timerqueuetimer->setSingleShot(true);
64
this->connect(this->idletimer, &QTimer::timeout, this, &SensorManager::idleTimeout);
65
this->connect(this->delaytimer, &QTimer::timeout, this, &SensorManager::delayTimeout);
66
this->connect(this->timerqueuetimer, &QTimer::timeout, this, &SensorManager::timerQueueTimeout);
68
SoDB::getSensorManager()->setChangedCallback(SensorManager::sensorQueueChangedCB, this);
69
this->timerEpsilon = 1.0 / 5000.0;
71
SoDB::setRealTimeInterval(1.0 / 25.0);
72
SoRenderManager::enableRealTimeUpdate(false);
75
SensorManager::~SensorManager()
78
SoDB::getSensorManager()->setChangedCallback(nullptr, nullptr);
80
if (this->signalthread->isRunning()) {
81
this->signalthread->stopThread();
82
this->signalthread->wait();
84
delete this->signalthread;
85
delete this->idletimer;
86
delete this->delaytimer;
87
delete this->timerqueuetimer;
91
SensorManager::sensorQueueChangedCB(void * closure)
93
SensorManager * thisp = (SensorManager * ) closure;
98
if (cc_thread_id() != thisp->mainthreadid) {
99
if (!thisp->signalthread->isRunning()) thisp->signalthread->start();
100
thisp->signalthread->trigger();
103
thisp->sensorQueueChanged();
108
SensorManager::sensorQueueChanged()
110
SoSensorManager * sensormanager = SoDB::getSensorManager();
111
assert(sensormanager);
114
if (sensormanager->isTimerSensorPending(interval)) {
115
interval -= SbTime::getTimeOfDay();
116
if (interval.getValue() < this->timerEpsilon) {
117
interval.setValue(this->timerEpsilon);
119
if (!this->timerqueuetimer->isActive()) {
120
this->timerqueuetimer->start(interval.getMsecValue());
122
this->timerqueuetimer->setInterval(interval.getMsecValue());
124
} else if (this->timerqueuetimer->isActive()) {
125
this->timerqueuetimer->stop();
128
if (sensormanager->isDelaySensorPending()) {
129
this->idletimer->start(0);
131
if (!this->delaytimer->isActive()) {
132
SbTime time = SoDB::getDelaySensorTimeout();
133
if (time != SbTime::zero()) {
134
this->delaytimer->start(time.getMsecValue());
138
if (this->idletimer->isActive()) {
139
this->idletimer->stop();
141
if (this->delaytimer->isActive()) {
142
this->delaytimer->stop();
148
SensorManager::idleTimeout()
150
SoDB::getSensorManager()->processTimerQueue();
151
SoDB::getSensorManager()->processDelayQueue(true);
152
this->sensorQueueChanged();
156
SensorManager::timerQueueTimeout()
158
SoDB::getSensorManager()->processTimerQueue();
159
this->sensorQueueChanged();
163
SensorManager::delayTimeout()
165
SoDB::getSensorManager()->processTimerQueue();
166
SoDB::getSensorManager()->processDelayQueue(false);
167
this->sensorQueueChanged();
171
SensorManager::setTimerEpsilon(double sec)
173
this->timerEpsilon = sec;