| 1 |
/**************************************************************************\ |
| 2 |
* Copyright (c) Kongsberg Oil & Gas Technologies AS |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Neither the name of the copyright holder nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
\**************************************************************************/ |
| 32 |
|
| 33 |
#include "SensorManager.h" |
| 34 |
|
| 35 |
#include <QTimer> |
| 36 |
|
| 37 |
#include <Inventor/SbTime.h> |
| 38 |
#include <Inventor/SoDB.h> |
| 39 |
#include <Inventor/SoRenderManager.h> |
| 40 |
#include <Inventor/C/threads/thread.h> |
| 41 |
|
| 42 |
#include "SignalThread.h" |
| 43 |
|
| 44 |
|
| 45 |
using namespace SIM::Coin3D::Quarter; |
| 46 |
|
| 47 |
SensorManager::SensorManager() |
| 48 |
: inherited() |
| 49 |
{ |
| 50 |
this->mainthreadid = cc_thread_id(); |
| 51 |
this->signalthread = new SignalThread(); |
| 52 |
|
| 53 |
QObject::connect(this->signalthread, &SignalThread::triggerSignal, |
| 54 |
this, &SensorManager::sensorQueueChanged); |
| 55 |
|
| 56 |
this->idletimer = new QTimer; |
| 57 |
this->delaytimer = new QTimer; |
| 58 |
this->timerqueuetimer = new QTimer; |
| 59 |
|
| 60 |
this->idletimer->setSingleShot(true); |
| 61 |
this->delaytimer->setSingleShot(true); |
| 62 |
this->timerqueuetimer->setSingleShot(true); |
| 63 |
|
| 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); |
| 67 |
|
| 68 |
SoDB::getSensorManager()->setChangedCallback(SensorManager::sensorQueueChangedCB, this); |
| 69 |
this->timerEpsilon = 1.0 / 5000.0; |
| 70 |
|
| 71 |
SoDB::setRealTimeInterval(1.0 / 25.0); |
| 72 |
SoRenderManager::enableRealTimeUpdate(false); |
| 73 |
} |
| 74 |
|
| 75 |
SensorManager::~SensorManager() |
| 76 |
{ |
| 77 |
// remove the Coin callback before shutting down |
| 78 |
SoDB::getSensorManager()->setChangedCallback(nullptr, nullptr); |
| 79 |
|
| 80 |
if (this->signalthread->isRunning()) { |
| 81 |
this->signalthread->stopThread(); |
| 82 |
this->signalthread->wait(); |
| 83 |
} |
| 84 |
delete this->signalthread; |
| 85 |
delete this->idletimer; |
| 86 |
delete this->delaytimer; |
| 87 |
delete this->timerqueuetimer; |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
SensorManager::sensorQueueChangedCB(void * closure) |
| 92 |
{ |
| 93 |
SensorManager * thisp = (SensorManager * ) closure; |
| 94 |
|
| 95 |
// if we get a callback from another thread, route the callback |
| 96 |
// through SignalThread so that we receive the callback in the |
| 97 |
// QApplication thread (needed since QTimer isn't thread safe) |
| 98 |
if (cc_thread_id() != thisp->mainthreadid) { |
| 99 |
if (!thisp->signalthread->isRunning()) thisp->signalthread->start(); |
| 100 |
thisp->signalthread->trigger(); |
| 101 |
} |
| 102 |
else { |
| 103 |
thisp->sensorQueueChanged(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
SensorManager::sensorQueueChanged() |
| 109 |
{ |
| 110 |
SoSensorManager * sensormanager = SoDB::getSensorManager(); |
| 111 |
assert(sensormanager); |
| 112 |
|
| 113 |
SbTime interval; |
| 114 |
if (sensormanager->isTimerSensorPending(interval)) { |
| 115 |
interval -= SbTime::getTimeOfDay(); |
| 116 |
if (interval.getValue() < this->timerEpsilon) { |
| 117 |
interval.setValue(this->timerEpsilon); |
| 118 |
} |
| 119 |
if (!this->timerqueuetimer->isActive()) { |
| 120 |
this->timerqueuetimer->start(interval.getMsecValue()); |
| 121 |
} else { |
| 122 |
this->timerqueuetimer->setInterval(interval.getMsecValue()); |
| 123 |
} |
| 124 |
} else if (this->timerqueuetimer->isActive()) { |
| 125 |
this->timerqueuetimer->stop(); |
| 126 |
} |
| 127 |
|
| 128 |
if (sensormanager->isDelaySensorPending()) { |
| 129 |
this->idletimer->start(0); |
| 130 |
|
| 131 |
if (!this->delaytimer->isActive()) { |
| 132 |
SbTime time = SoDB::getDelaySensorTimeout(); |
| 133 |
if (time != SbTime::zero()) { |
| 134 |
this->delaytimer->start(time.getMsecValue()); |
| 135 |
} |
| 136 |
} |
| 137 |
} else { |
| 138 |
if (this->idletimer->isActive()) { |
| 139 |
this->idletimer->stop(); |
| 140 |
} |
| 141 |
if (this->delaytimer->isActive()) { |
| 142 |
this->delaytimer->stop(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
SensorManager::idleTimeout() |
| 149 |
{ |
| 150 |
SoDB::getSensorManager()->processTimerQueue(); |
| 151 |
SoDB::getSensorManager()->processDelayQueue(true); |
| 152 |
this->sensorQueueChanged(); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
SensorManager::timerQueueTimeout() |
| 157 |
{ |
| 158 |
SoDB::getSensorManager()->processTimerQueue(); |
| 159 |
this->sensorQueueChanged(); |
| 160 |
} |
| 161 |
|
| 162 |
void |
| 163 |
SensorManager::delayTimeout() |
| 164 |
{ |
| 165 |
SoDB::getSensorManager()->processTimerQueue(); |
| 166 |
SoDB::getSensorManager()->processDelayQueue(false); |
| 167 |
this->sensorQueueChanged(); |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
SensorManager::setTimerEpsilon(double sec) |
| 172 |
{ |
| 173 |
this->timerEpsilon = sec; |
| 174 |
} |