| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91,92 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.3 $ |
| 44 |
| |
| 45 |
| Description: |
| 46 |
| Defines the (internal) SoSensorManager class |
| 47 |
| |
| 48 |
| Author(s) : Nick Thompson, Paul S. Strauss |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <Inventor/sensors/SoSensorManager.h> |
| 55 |
#include <Inventor/sensors/SoSensors.h> |
| 56 |
#include <Inventor/errors/SoDebugError.h> |
| 57 |
#ifdef __sgi |
| 58 |
#include <osfcn.h> |
| 59 |
#endif // __sgi |
| 60 |
#ifdef __APPLE__ |
| 61 |
#include <unistd.h> |
| 62 |
#endif // __APPLE__ |
| 63 |
|
| 64 |
#ifdef DEBUG |
| 65 |
#include <SoDebug.h> |
| 66 |
static SbTime startTime; |
| 67 |
#endif |
| 68 |
|
| 69 |
//////////////////////////////////////////////////////////////////////// |
| 70 |
// |
| 71 |
// Description: |
| 72 |
// Constructor |
| 73 |
// |
| 74 |
// Use: public |
| 75 |
|
| 76 |
SoSensorManager::SoSensorManager() |
| 77 |
// |
| 78 |
//////////////////////////////////////////////////////////////////////// |
| 79 |
{ |
| 80 |
changedFunc = NULL; |
| 81 |
changedFuncData = NULL; |
| 82 |
|
| 83 |
delayQueue = NULL; |
| 84 |
timerQueue = NULL; |
| 85 |
rescheduleQueue = NULL; |
| 86 |
|
| 87 |
delayQTimeout = SbTime::zero(); |
| 88 |
delayQTimeoutSensor = new SoAlarmSensor(delayQTimeoutCB, this); |
| 89 |
#ifdef DEBUG |
| 90 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 91 |
SoDebug::NamePtr("delayQTimeoutSensor", delayQTimeoutSensor); |
| 92 |
startTime = SbTime::getTimeOfDay(); |
| 93 |
} |
| 94 |
#endif |
| 95 |
|
| 96 |
processingQueue = 0; |
| 97 |
|
| 98 |
delayQCount = 0; |
| 99 |
} |
| 100 |
|
| 101 |
//////////////////////////////////////////////////////////////////////// |
| 102 |
// |
| 103 |
// Description: |
| 104 |
// Destructor |
| 105 |
// |
| 106 |
// Use: public |
| 107 |
|
| 108 |
SoSensorManager::~SoSensorManager() |
| 109 |
// |
| 110 |
//////////////////////////////////////////////////////////////////////// |
| 111 |
{ |
| 112 |
delete delayQTimeoutSensor; |
| 113 |
} |
| 114 |
|
| 115 |
//////////////////////////////////////////////////////////////////////// |
| 116 |
// |
| 117 |
// Description: |
| 118 |
// Insert a delay queue sensor into the delay queue. |
| 119 |
// |
| 120 |
// Use: public |
| 121 |
|
| 122 |
void |
| 123 |
SoSensorManager::insertDelaySensor(SoDelayQueueSensor *s) |
| 124 |
// |
| 125 |
//////////////////////////////////////////////////////////////////////// |
| 126 |
{ |
| 127 |
// If the sensor's priority isn't zero, AND the delay queue |
| 128 |
// timeout isn't zero, and the delay queue sensor isn't already |
| 129 |
// scheduled, get the delay queue timeout ready to go |
| 130 |
// off. |
| 131 |
if ((s->getPriority() != 0) && !delayQTimeoutSensor->isScheduled() |
| 132 |
&& (delayQTimeout != SbTime::zero())) { |
| 133 |
|
| 134 |
delayQTimeoutSensor->unschedule(); |
| 135 |
delayQTimeoutSensor->setTimeFromNow(delayQTimeout); |
| 136 |
delayQTimeoutSensor->schedule(); |
| 137 |
|
| 138 |
} |
| 139 |
insert(s, delayQueue); |
| 140 |
|
| 141 |
if (s->getPriority() != 0) { |
| 142 |
#ifdef DEBUG |
| 143 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 144 |
SoDebug::RTPrintf("Inserted delay queue sensor: " |
| 145 |
"Name: %s, priority: %d\n", |
| 146 |
SoDebug::PtrName(s), s->getPriority()); |
| 147 |
} |
| 148 |
#endif |
| 149 |
// Call changed callbacks if necessary |
| 150 |
notifyChanged(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
//////////////////////////////////////////////////////////////////////// |
| 155 |
// |
| 156 |
// Description: |
| 157 |
// Insert a timer queue sensor into the timer queue. |
| 158 |
// |
| 159 |
// Use: public |
| 160 |
|
| 161 |
void |
| 162 |
SoSensorManager::insertTimerSensor(SoTimerQueueSensor *s) |
| 163 |
// |
| 164 |
//////////////////////////////////////////////////////////////////////// |
| 165 |
{ |
| 166 |
#ifdef DEBUG |
| 167 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 168 |
SoDebug::RTPrintf("Inserting timer queue sensor: " |
| 169 |
"Name: %s, trigger time: %g\n", |
| 170 |
SoDebug::PtrName(s), |
| 171 |
(s->getTriggerTime()-startTime).getValue()); |
| 172 |
} |
| 173 |
#endif |
| 174 |
insert(s, timerQueue); |
| 175 |
|
| 176 |
// Call changed callbacks if necessary |
| 177 |
notifyChanged(); |
| 178 |
} |
| 179 |
|
| 180 |
//////////////////////////////////////////////////////////////////////// |
| 181 |
// |
| 182 |
// Description: |
| 183 |
// Remove a delay queue sensor from the delay queue. |
| 184 |
// |
| 185 |
// Use: public |
| 186 |
|
| 187 |
void |
| 188 |
SoSensorManager::removeDelaySensor(SoDelayQueueSensor *s) |
| 189 |
// |
| 190 |
//////////////////////////////////////////////////////////////////////// |
| 191 |
{ |
| 192 |
remove(s, delayQueue); |
| 193 |
|
| 194 |
if (s->getPriority() != 0) { |
| 195 |
// Call changed callbacks if necessary |
| 196 |
notifyChanged(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
//////////////////////////////////////////////////////////////////////// |
| 201 |
// |
| 202 |
// Description: |
| 203 |
// Remove a timer queue sensor from the timer queue. |
| 204 |
// |
| 205 |
// Use: public |
| 206 |
|
| 207 |
void |
| 208 |
SoSensorManager::removeTimerSensor(SoTimerQueueSensor *s) |
| 209 |
// |
| 210 |
//////////////////////////////////////////////////////////////////////// |
| 211 |
{ |
| 212 |
remove(s, timerQueue); |
| 213 |
|
| 214 |
// Call changed callbacks if necessary |
| 215 |
notifyChanged(); |
| 216 |
} |
| 217 |
|
| 218 |
//////////////////////////////////////////////////////////////////////// |
| 219 |
// |
| 220 |
// Description: |
| 221 |
// Adds a timer to the 'to-be-rescheduled' queue. |
| 222 |
// |
| 223 |
// Use: public |
| 224 |
|
| 225 |
void |
| 226 |
SoSensorManager::rescheduleTimer(SoTimerSensor *s) |
| 227 |
// |
| 228 |
//////////////////////////////////////////////////////////////////////// |
| 229 |
{ |
| 230 |
insert(s, rescheduleQueue); |
| 231 |
} |
| 232 |
|
| 233 |
//////////////////////////////////////////////////////////////////////// |
| 234 |
// |
| 235 |
// Description: |
| 236 |
// Remove a timer from the 'to-be-rescheduled' queue. |
| 237 |
// |
| 238 |
// Use: public |
| 239 |
|
| 240 |
void |
| 241 |
SoSensorManager::removeRescheduledTimer(SoTimerQueueSensor *s) |
| 242 |
// |
| 243 |
//////////////////////////////////////////////////////////////////////// |
| 244 |
{ |
| 245 |
remove(s, rescheduleQueue); |
| 246 |
} |
| 247 |
|
| 248 |
//////////////////////////////////////////////////////////////////////// |
| 249 |
// |
| 250 |
// Description: |
| 251 |
// Sets the delay sensor queue timeout. |
| 252 |
// |
| 253 |
// Use: public |
| 254 |
|
| 255 |
void |
| 256 |
SoSensorManager::setDelaySensorTimeout(const SbTime &t) |
| 257 |
// |
| 258 |
//////////////////////////////////////////////////////////////////////// |
| 259 |
{ |
| 260 |
#ifdef DEBUG |
| 261 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 262 |
SoDebug::RTPrintf("Setting delay sensor timeout to %g\n", |
| 263 |
t.getValue()); |
| 264 |
} |
| 265 |
#endif |
| 266 |
|
| 267 |
delayQTimeout = t; |
| 268 |
|
| 269 |
if (t == SbTime::zero()) { |
| 270 |
delayQTimeoutSensor->unschedule(); |
| 271 |
} else { |
| 272 |
if (delayQueue != NULL) { |
| 273 |
delayQTimeoutSensor->setTimeFromNow(t); |
| 274 |
delayQTimeoutSensor->schedule(); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
//////////////////////////////////////////////////////////////////////// |
| 280 |
// |
| 281 |
// Description: |
| 282 |
// Gets the delay sensor queue timeout. |
| 283 |
// |
| 284 |
// Use: public |
| 285 |
|
| 286 |
const SbTime & |
| 287 |
SoSensorManager::getDelaySensorTimeout() |
| 288 |
// |
| 289 |
//////////////////////////////////////////////////////////////////////// |
| 290 |
{ |
| 291 |
return delayQTimeout; |
| 292 |
} |
| 293 |
|
| 294 |
//////////////////////////////////////////////////////////////////////// |
| 295 |
// |
| 296 |
// Description: |
| 297 |
// Callback function from alarm sensor that processed the delay |
| 298 |
// queue every once in a while. |
| 299 |
// |
| 300 |
// Use: private, static |
| 301 |
|
| 302 |
void |
| 303 |
SoSensorManager::delayQTimeoutCB(void *data, SoSensor *) |
| 304 |
|
| 305 |
// |
| 306 |
//////////////////////////////////////////////////////////////////////// |
| 307 |
{ |
| 308 |
#ifdef DEBUG |
| 309 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 310 |
SoDebug::RTPrintf("Processing delay queue; timeout expired\n"); |
| 311 |
} |
| 312 |
#endif |
| 313 |
|
| 314 |
SoSensorManager *s = (SoSensorManager *)data; |
| 315 |
s->processDelayQueue(FALSE); |
| 316 |
} |
| 317 |
|
| 318 |
//////////////////////////////////////////////////////////////////////// |
| 319 |
// |
| 320 |
// Description: |
| 321 |
// Processes all sensors in the delay queue. |
| 322 |
// |
| 323 |
// Use: public |
| 324 |
|
| 325 |
void |
| 326 |
SoSensorManager::processDelayQueue(SbBool isIdle) |
| 327 |
// |
| 328 |
//////////////////////////////////////////////////////////////////////// |
| 329 |
{ |
| 330 |
#ifdef DEBUG |
| 331 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 332 |
SoDebug::RTPrintf("Processing delay queue (isIdle:%d)\n", isIdle); |
| 333 |
} |
| 334 |
#endif |
| 335 |
|
| 336 |
SoSensor *prev; |
| 337 |
SoDelayQueueSensor *first; |
| 338 |
|
| 339 |
if (delayQueue == NULL) |
| 340 |
return; |
| 341 |
|
| 342 |
++processingQueue; |
| 343 |
|
| 344 |
// We want to process all of the sensors that are currently in the |
| 345 |
// queue. But we have to be careful to NOT process sensors that |
| 346 |
// are added to the queue while we are processing it. Therefore, |
| 347 |
// we first mark all of the sensors in the queue as being current. |
| 348 |
// Then we process only the current sensors. When there are no |
| 349 |
// current sensors left, we're done. NOTE: removing a sensor marks |
| 350 |
// it as being not-current; inserting a new sensor does not mark |
| 351 |
// it as current. |
| 352 |
#ifdef DEBUG |
| 353 |
if (((SoDelayQueueSensor *) delayQueue)->getPriority() == 0) |
| 354 |
SoDebugError::post("SoSensorManager::processDelayQueue", |
| 355 |
"Immediate sensor in delay queue!!!!"); |
| 356 |
#endif |
| 357 |
|
| 358 |
// We want to process all of the sensors currently in the queue. |
| 359 |
// To avoid looping infinitely here, we do not trigger any sensor |
| 360 |
// more than once. To detect this, we keep a running counter |
| 361 |
// every time this method is called. When a sensor is triggered |
| 362 |
// here, it is marked with the current counter. If a sensor in the |
| 363 |
// queue is so marked, we know it has already been triggered once, |
| 364 |
// and we can skip it the next time. |
| 365 |
|
| 366 |
// Increment the count |
| 367 |
delayQCount++; |
| 368 |
|
| 369 |
while (delayQueue != NULL) { |
| 370 |
|
| 371 |
// Find first sensor that has not yet been triggered (its |
| 372 |
// counter is not the same as our current counter), saving |
| 373 |
// previous sensor in queue (to keep links accurate) |
| 374 |
prev = NULL; |
| 375 |
|
| 376 |
first = (SoDelayQueueSensor *) delayQueue; |
| 377 |
while (first != NULL) { |
| 378 |
// Process this sensor IF: |
| 379 |
// -- it wasn't processed before, and |
| 380 |
// -- it is a OneShot, or an Idle and we have idle time |
| 381 |
if (first->getCounter() != delayQCount && |
| 382 |
(isIdle || !first->isIdleOnly())) break; |
| 383 |
prev = first; |
| 384 |
first = (SoDelayQueueSensor *) first->getNextInQueue(); |
| 385 |
} |
| 386 |
|
| 387 |
// If we didn't find one, we're done! |
| 388 |
if (first == NULL) |
| 389 |
break; |
| 390 |
|
| 391 |
// Remove sensor from queue |
| 392 |
if (prev == NULL) |
| 393 |
delayQueue = first->getNextInQueue(); |
| 394 |
else |
| 395 |
prev->setNextInQueue(first->getNextInQueue()); |
| 396 |
first->setNextInQueue(NULL); |
| 397 |
|
| 398 |
// Mark the sensor with the current counter so it won't |
| 399 |
// trigger again immediately if it gets added to the queue again |
| 400 |
first->setCounter(delayQCount); |
| 401 |
|
| 402 |
#ifdef DEBUG |
| 403 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 404 |
SoDebug::RTPrintf("Triggering delay queue sensor " |
| 405 |
"Name: %s, priority: %d\n", |
| 406 |
SoDebug::PtrName(first), first->getPriority()); |
| 407 |
|
| 408 |
} |
| 409 |
#endif |
| 410 |
// Trigger the sensor. NOTE: this can add new entries to the |
| 411 |
// queue or remove entries. |
| 412 |
((SoSensor *) first)->trigger(); |
| 413 |
} |
| 414 |
--processingQueue; |
| 415 |
|
| 416 |
// If there are still sensors left in the queue after processing, |
| 417 |
// setup the delay queue timeout: |
| 418 |
if ((delayQueue != NULL) && (delayQTimeout != SbTime::zero())) { |
| 419 |
delayQTimeoutSensor->setTimeFromNow(delayQTimeout); |
| 420 |
delayQTimeoutSensor->schedule(); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
//////////////////////////////////////////////////////////////////////// |
| 425 |
// |
| 426 |
// Description: |
| 427 |
// Processes all immediate (priority 0) sensors in the delay queue. |
| 428 |
// |
| 429 |
// Use: public |
| 430 |
|
| 431 |
void |
| 432 |
SoSensorManager::processImmediateQueue() |
| 433 |
{ |
| 434 |
SoDelayQueueSensor *first; |
| 435 |
static int processingImmediate = FALSE; |
| 436 |
|
| 437 |
if (delayQueue == NULL) |
| 438 |
return; |
| 439 |
|
| 440 |
// Triggering an immediate sensor may initiate notification, |
| 441 |
// calling this method recursively. To avoid doing extra work, |
| 442 |
// just return if we are already processing immediate sensors. |
| 443 |
if (processingImmediate) |
| 444 |
return; |
| 445 |
|
| 446 |
processingImmediate = TRUE; |
| 447 |
processingQueue++; |
| 448 |
|
| 449 |
// NOTE: all immediate sensors are processed as added, even if |
| 450 |
// this causes an infinite loop. (That's what they're supposed to |
| 451 |
// do.) No counter check is made here. |
| 452 |
|
| 453 |
while (delayQueue != NULL && |
| 454 |
((SoDelayQueueSensor *) delayQueue)->getPriority() == 0) { |
| 455 |
|
| 456 |
first = (SoDelayQueueSensor *) delayQueue; |
| 457 |
delayQueue = delayQueue->getNextInQueue(); |
| 458 |
|
| 459 |
first->setNextInQueue(NULL); |
| 460 |
|
| 461 |
#if 0 |
| 462 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 463 |
SoDebug::RTPrintf("Triggering immediate sensor " |
| 464 |
"Name: %s, priority: %d\n", |
| 465 |
SoDebug::PtrName(first), first->getPriority()); |
| 466 |
|
| 467 |
} |
| 468 |
#endif |
| 469 |
((SoSensor *) first)->trigger(); |
| 470 |
} |
| 471 |
|
| 472 |
processingImmediate = FALSE; |
| 473 |
processingQueue--; |
| 474 |
} |
| 475 |
|
| 476 |
//////////////////////////////////////////////////////////////////////// |
| 477 |
// |
| 478 |
// Description: |
| 479 |
// Processes all sensors in the timer queue that are before the |
| 480 |
// current time. |
| 481 |
// |
| 482 |
// Use: public |
| 483 |
|
| 484 |
void |
| 485 |
SoSensorManager::processTimerQueue() |
| 486 |
// |
| 487 |
//////////////////////////////////////////////////////////////////////// |
| 488 |
{ |
| 489 |
++processingQueue; |
| 490 |
SoSensor *first; |
| 491 |
|
| 492 |
// NOTE: Infinite loops are possible if timerQueue sensors |
| 493 |
// reschedule themselves inside their trigger() routine. This |
| 494 |
// isn't possible with Timer sensors, because they are added to |
| 495 |
// the rescheduleQueue and can only be triggered once per call to |
| 496 |
// processTimerQueue. |
| 497 |
// The assumption here is that if the user reschedules an alarm, |
| 498 |
// we assume they know what they are doing. |
| 499 |
|
| 500 |
// Stop if no sensors left or next sensor really is after now |
| 501 |
while (timerQueue != NULL && |
| 502 |
((SoTimerQueueSensor *) timerQueue)->getTriggerTime() <= |
| 503 |
SbTime::getTimeOfDay()) { |
| 504 |
|
| 505 |
// Get pointer to first sensor |
| 506 |
first = timerQueue; |
| 507 |
|
| 508 |
// Remove sensor from queue. |
| 509 |
timerQueue = timerQueue->getNextInQueue(); |
| 510 |
first->setNextInQueue(NULL); |
| 511 |
|
| 512 |
#ifdef DEBUG |
| 513 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 514 |
SoDebug::RTPrintf("Triggering timer sensor " |
| 515 |
"Name: %s, trig time: %g\n", |
| 516 |
SoDebug::PtrName(first), |
| 517 |
(((SoTimerQueueSensor *)first)-> |
| 518 |
getTriggerTime()-startTime).getValue()); |
| 519 |
} |
| 520 |
#endif |
| 521 |
// Trigger the sensor. |
| 522 |
first->trigger(); |
| 523 |
} |
| 524 |
|
| 525 |
// |
| 526 |
// Timers used to reschedule themselves at the end of their |
| 527 |
// trigger() methods. However, this can lead to timer queue |
| 528 |
// saturation if there are two sensors that take a long time in |
| 529 |
// their trigger() methods. The first sensor will schedule itself |
| 530 |
// for sometime in the future, but by the time the second sensor |
| 531 |
// is done triggering (and scheduling itself in the future), the |
| 532 |
// first sensor is ready to fire. |
| 533 |
// By scheduling all timers relative to one time, AFTER they have |
| 534 |
// been triggered, this problem is avoided. |
| 535 |
// |
| 536 |
if (rescheduleQueue != NULL) { |
| 537 |
SbTime now = SbTime::getTimeOfDay(); |
| 538 |
|
| 539 |
// Now reschedule timers, relative to now |
| 540 |
|
| 541 |
while (rescheduleQueue != NULL) { |
| 542 |
SoTimerSensor *timer = (SoTimerSensor *)rescheduleQueue; |
| 543 |
// Remove sensor from queue |
| 544 |
rescheduleQueue = rescheduleQueue->getNextInQueue(); |
| 545 |
timer->setNextInQueue(NULL); |
| 546 |
|
| 547 |
timer->reschedule(now); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
--processingQueue; |
| 552 |
} |
| 553 |
|
| 554 |
//////////////////////////////////////////////////////////////////////// |
| 555 |
// |
| 556 |
// Description: |
| 557 |
// Returns TRUE if there is at least one sensor in the timer queue, |
| 558 |
// and returns the time of the first sensor in tm. |
| 559 |
// |
| 560 |
// Use: public |
| 561 |
|
| 562 |
SbBool |
| 563 |
SoSensorManager::isTimerSensorPending(SbTime &tm) |
| 564 |
// |
| 565 |
//////////////////////////////////////////////////////////////////////// |
| 566 |
{ |
| 567 |
if (timerQueue != NULL) |
| 568 |
tm = ((SoTimerQueueSensor *) timerQueue)->getTriggerTime(); |
| 569 |
return (timerQueue != NULL); |
| 570 |
} |
| 571 |
|
| 572 |
//////////////////////////////////////////////////////////////////////// |
| 573 |
// |
| 574 |
// Description: |
| 575 |
// Just like U*X select() call, but does our tasks while waiting. |
| 576 |
// |
| 577 |
// Use: public |
| 578 |
|
| 579 |
int |
| 580 |
SoSensorManager::doSelect(int nfds, fd_set *readfds, fd_set *writefds, |
| 581 |
fd_set *exceptfds, struct timeval *userTimeOut) |
| 582 |
// |
| 583 |
//////////////////////////////////////////////////////////////////////// |
| 584 |
{ |
| 585 |
|
| 586 |
// Possible sources of events |
| 587 |
#define EVENT_SOURCE_FD 0x01 |
| 588 |
#define EVENT_SOURCE_TIMER 0x02 |
| 589 |
#define EVENT_SOURCE_IDLE 0x04 |
| 590 |
#define EVENT_SOURCE_USER_TIMEOUT 0x08 |
| 591 |
#define EVENT_SOURCE_TIMEOUT 0x10 |
| 592 |
|
| 593 |
SbTime now, userDeadline, timeOut, t; |
| 594 |
fd_set readfdsTmp, writefdsTmp, exceptfdsTmp; |
| 595 |
int32_t eventSources; |
| 596 |
struct timeval tvStruct, *tv; |
| 597 |
int selectReturn; |
| 598 |
|
| 599 |
now = SbTime::getTimeOfDay(); |
| 600 |
|
| 601 |
if (userTimeOut != NULL) |
| 602 |
userDeadline = now + SbTime(userTimeOut); |
| 603 |
|
| 604 |
while (1) { |
| 605 |
|
| 606 |
eventSources = 0; |
| 607 |
|
| 608 |
// The priority of stuff is: |
| 609 |
// |
| 610 |
// 1. Timers. The set of pending timers will be processed before |
| 611 |
// an event (or user timeout) is returned. Note that if |
| 612 |
// timers saturate the queue (e.g. there is a timer with an |
| 613 |
// interval of zero), only one event will be processed between |
| 614 |
// processing the timer queue. |
| 615 |
// |
| 616 |
// 2. User timeout. See the comment above. |
| 617 |
// |
| 618 |
// 3. Events. See the comment above. |
| 619 |
// |
| 620 |
// 4. Idle/Delay processes. OneShot sensors are processed |
| 621 |
// whenver there is idle time (there are no timers or events |
| 622 |
// pending) OR when a user-specifiable timeout occurs. Idle |
| 623 |
// sensors are processed only when there is idle time. |
| 624 |
// |
| 625 |
|
| 626 |
// If there are any delay sensors around, set the appropriate bit |
| 627 |
// and set the timeout to 0 |
| 628 |
if (isDelaySensorPending()) { |
| 629 |
timeOut = SbTime::zero(); |
| 630 |
eventSources |= EVENT_SOURCE_IDLE | EVENT_SOURCE_TIMEOUT; |
| 631 |
} |
| 632 |
|
| 633 |
// If there are timers around, see when they are supposed to occur. |
| 634 |
if (isTimerSensorPending(t)) { |
| 635 |
if (t < now) { |
| 636 |
// If there are timers pending, set the appropriate |
| 637 |
// bit and set the timeout to 0. |
| 638 |
timeOut = SbTime::zero(); |
| 639 |
eventSources = EVENT_SOURCE_TIMER | EVENT_SOURCE_TIMEOUT; |
| 640 |
} |
| 641 |
else { |
| 642 |
// If there are timer events in the future: |
| 643 |
if (eventSources & EVENT_SOURCE_IDLE) { |
| 644 |
// If there are delay sensors, they will time out |
| 645 |
// before us, so do nothing. |
| 646 |
} |
| 647 |
else { |
| 648 |
// Figure out the appropriate timeout and set the |
| 649 |
// right bits. |
| 650 |
timeOut = t - now; |
| 651 |
eventSources = EVENT_SOURCE_TIMER | |
| 652 |
EVENT_SOURCE_TIMEOUT; |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
// if there is a user timeout, set the appropriate bit and |
| 658 |
// timeout value. |
| 659 |
if (userTimeOut != NULL) { |
| 660 |
if (userDeadline <= now) { |
| 661 |
// If the user deadline has passed, set the appropriate |
| 662 |
// bit and set the timeout to 0. |
| 663 |
timeOut = SbTime::zero(); |
| 664 |
eventSources = EVENT_SOURCE_USER_TIMEOUT | |
| 665 |
EVENT_SOURCE_TIMEOUT; |
| 666 |
} |
| 667 |
else { |
| 668 |
// If there is a deadline in the future, find the |
| 669 |
// appropriate timeout interval and set the timeout bit. |
| 670 |
if (eventSources & EVENT_SOURCE_TIMEOUT) { |
| 671 |
// something is already scheduled to timeout. |
| 672 |
// see if the user timeout is earlier |
| 673 |
if (userDeadline - now < timeOut) { |
| 674 |
timeOut = userDeadline - now; |
| 675 |
eventSources = EVENT_SOURCE_USER_TIMEOUT | |
| 676 |
EVENT_SOURCE_TIMEOUT; |
| 677 |
} |
| 678 |
} |
| 679 |
else { |
| 680 |
// Nothing is scheduled to time out yet. |
| 681 |
// Use the user's timeout value. |
| 682 |
timeOut = userDeadline - now; |
| 683 |
eventSources = EVENT_SOURCE_USER_TIMEOUT | |
| 684 |
EVENT_SOURCE_TIMEOUT; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
if (eventSources & EVENT_SOURCE_TIMEOUT) { |
| 690 |
tv = &tvStruct; |
| 691 |
timeOut.getValue(tv); |
| 692 |
} |
| 693 |
else |
| 694 |
tv = NULL; |
| 695 |
|
| 696 |
// Save the fdset data |
| 697 |
if (readfds != NULL) |
| 698 |
readfdsTmp = *readfds; |
| 699 |
else |
| 700 |
FD_ZERO(&readfdsTmp); |
| 701 |
|
| 702 |
if (writefds != NULL) |
| 703 |
writefdsTmp = *writefds; |
| 704 |
else |
| 705 |
FD_ZERO(&writefdsTmp); |
| 706 |
|
| 707 |
if (exceptfds != NULL) |
| 708 |
exceptfdsTmp = *exceptfds; |
| 709 |
else |
| 710 |
FD_ZERO(&exceptfdsTmp); |
| 711 |
|
| 712 |
selectReturn = select(nfds, &readfdsTmp, &writefdsTmp, |
| 713 |
&exceptfdsTmp, tv); |
| 714 |
if (selectReturn < 0) { |
| 715 |
perror("select"); |
| 716 |
return selectReturn; |
| 717 |
} |
| 718 |
// if there is anything ready, set the appropriate bits |
| 719 |
else if (selectReturn > 0) |
| 720 |
eventSources |= EVENT_SOURCE_FD; |
| 721 |
|
| 722 |
// The order of these if statements determines priority of |
| 723 |
// event delivery |
| 724 |
if (eventSources & EVENT_SOURCE_USER_TIMEOUT || |
| 725 |
eventSources & EVENT_SOURCE_FD) { |
| 726 |
|
| 727 |
// set the fd masks |
| 728 |
if (readfds != NULL) |
| 729 |
*readfds = readfdsTmp; |
| 730 |
if (writefds != NULL) |
| 731 |
*writefds = writefdsTmp; |
| 732 |
if (exceptfds != NULL) |
| 733 |
*exceptfds = exceptfdsTmp; |
| 734 |
|
| 735 |
// Make sure timers get fired even if the event queue is swamped |
| 736 |
if (eventSources & EVENT_SOURCE_TIMER) |
| 737 |
processTimerQueue(); |
| 738 |
|
| 739 |
return selectReturn; |
| 740 |
} |
| 741 |
|
| 742 |
else if (eventSources & EVENT_SOURCE_TIMER) |
| 743 |
processTimerQueue(); |
| 744 |
|
| 745 |
else if (eventSources & EVENT_SOURCE_IDLE) |
| 746 |
processDelayQueue(TRUE); |
| 747 |
|
| 748 |
// Going around again... update our notion of current time. |
| 749 |
now = SbTime::getTimeOfDay(); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
#undef EVENT_SOURCE_FD |
| 754 |
#undef EVENT_SOURCE_TIMER |
| 755 |
#undef EVENT_SOURCE_IDLE |
| 756 |
#undef EVENT_SOURCE_USER_TIMEOUT |
| 757 |
#undef EVENT_SOURCE_TIMEOUT |
| 758 |
|
| 759 |
//////////////////////////////////////////////////////////////////////// |
| 760 |
// |
| 761 |
// Description: |
| 762 |
// Inserts a sensor into one of the queues. The head of the queue |
| 763 |
// is passed in by reference. |
| 764 |
// |
| 765 |
// Use: public |
| 766 |
|
| 767 |
void |
| 768 |
SoSensorManager::insert(SoSensor *s, SoSensor *&head) |
| 769 |
// |
| 770 |
//////////////////////////////////////////////////////////////////////// |
| 771 |
{ |
| 772 |
SoSensor *prev = NULL, *next; |
| 773 |
|
| 774 |
// Loop through all sensors in queue |
| 775 |
for (next = head; next != NULL; next = next->getNextInQueue()) { |
| 776 |
|
| 777 |
// Stop if the new sensor should be inserted before the |
| 778 |
// current one in the queue. Use the virtual isBefore() method |
| 779 |
// which works for all types of sensors. |
| 780 |
if (s->isBefore(next)) |
| 781 |
break; |
| 782 |
|
| 783 |
prev = next; |
| 784 |
} |
| 785 |
|
| 786 |
// Insert sensor into queue |
| 787 |
s->setNextInQueue(next); |
| 788 |
if (prev == NULL) |
| 789 |
head = s; |
| 790 |
else |
| 791 |
prev->setNextInQueue(s); |
| 792 |
} |
| 793 |
|
| 794 |
//////////////////////////////////////////////////////////////////////// |
| 795 |
// |
| 796 |
// Description: |
| 797 |
// Removes a sensor from one of the queues. The head of the queue |
| 798 |
// is passed in by reference. |
| 799 |
// |
| 800 |
// Use: public |
| 801 |
|
| 802 |
void |
| 803 |
SoSensorManager::remove(SoSensor *s, SoSensor *&head) |
| 804 |
// |
| 805 |
//////////////////////////////////////////////////////////////////////// |
| 806 |
{ |
| 807 |
SoSensor *prev = NULL, *cur; |
| 808 |
|
| 809 |
// Loop through all sensors in queue, looking for s |
| 810 |
for (cur = head; cur != NULL; cur = cur->getNextInQueue()) { |
| 811 |
|
| 812 |
if (cur == s) |
| 813 |
break; |
| 814 |
|
| 815 |
prev = cur; |
| 816 |
} |
| 817 |
|
| 818 |
// Remove sensor from queue |
| 819 |
if (prev == NULL) |
| 820 |
head = s->getNextInQueue(); |
| 821 |
else |
| 822 |
prev->setNextInQueue(s->getNextInQueue()); |
| 823 |
s->setNextInQueue(NULL); |
| 824 |
} |
| 825 |
|
| 826 |
//////////////////////////////////////////////////////////////////////// |
| 827 |
// |
| 828 |
// Description: |
| 829 |
// Calls callback functions to tell them about changes to the |
| 830 |
// sensor queues. The callbacks are for higher-level toolkits |
| 831 |
// (like Xt) so timeouts can be set. |
| 832 |
// |
| 833 |
// Use: public |
| 834 |
|
| 835 |
void |
| 836 |
SoSensorManager::notifyChanged() |
| 837 |
// |
| 838 |
//////////////////////////////////////////////////////////////////////// |
| 839 |
{ |
| 840 |
if (changedFunc != NULL && !processingQueue) { |
| 841 |
changedFunc(changedFuncData); |
| 842 |
|
| 843 |
#ifdef DEBUG |
| 844 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 845 |
SoDebug::RTPrintf("SoSensorManager::notifyChanged()\n"); |
| 846 |
} |
| 847 |
#endif |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
|