1 /*********************************************************** 2 * Copyright (C) 1997, Be Inc. Copyright (C) 1999, Jake Hamby. 3 * 4 * This program is freely distributable without licensing fees 5 * and is provided without guarantee or warrantee expressed or 6 * implied. This program is -not- in the public domain. 7 * 8 * FILE: glutBlocker.cpp 9 * 10 * DESCRIPTION: helper class for GLUT event loop. 11 * if a window receives an event, wake up the event loop. 12 ***********************************************************/ 13 14 /*********************************************************** 15 * Headers 16 ***********************************************************/ 17 #include "glutBlocker.h" 18 19 /*********************************************************** 20 * Global variable 21 ***********************************************************/ 22 GlutBlocker gBlock; 23 24 /*********************************************************** 25 * Member functions 26 ***********************************************************/ 27 GlutBlocker::GlutBlocker() { 28 gSem = create_sem(1, "gSem"); 29 eSem = create_sem(0, "eSem"); 30 events = false; 31 sleeping = false; 32 } 33 34 GlutBlocker::~GlutBlocker() { 35 delete_sem(eSem); 36 delete_sem(gSem); 37 } 38 39 void GlutBlocker::WaitEvent() { 40 acquire_sem(gSem); 41 if(!events) { // wait for new event 42 sleeping = true; 43 release_sem(gSem); 44 acquire_sem(eSem); // next event will release eSem 45 } else { 46 release_sem(gSem); 47 } 48 } 49 50 void GlutBlocker::WaitEvent(bigtime_t usecs) { 51 acquire_sem(gSem); 52 if(!events) { // wait for new event 53 sleeping = true; 54 release_sem(gSem); 55 acquire_sem_etc(eSem, 1, B_TIMEOUT, usecs); // wait for next event or timeout 56 } else { 57 release_sem(gSem); 58 } 59 } 60 61 void GlutBlocker::NewEvent() { 62 acquire_sem(gSem); 63 events = true; // next call to WaitEvent returns immediately 64 if(sleeping) { 65 sleeping = false; 66 release_sem(eSem); // if event loop is blocking, wake it up 67 } 68 release_sem(gSem); 69 } 70