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