/*	XGPeriodic.cpp
 *
 *		This is a mixin for delivering periodic events. There are
 *	two such events--a tick before the event is processed, and one
 *	right after an event is processed.
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>
#include <XEvent.h>

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

XGPeriodic *XGPeriodic::gList = NULL;

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGPeriodic::XGPeriodic
 *
 *		Create me and add me to the linked list of periodic receivers.
 *	This linked list is separate from the list maintained by the XGBroadcaster
 *	global object, which makes this a bit faster for both.
 */

XGPeriodic::XGPeriodic()
{
	fPrev = NULL;
	if (gList) gList->fPrev = this;
	fNext = gList;
	gList = this;
}

/*$	XGPeriodic::~XGPeriodic
 *
 *		Remove me from the linked list.
 */

XGPeriodic::~XGPeriodic()
{
	if (gList == this) {
		gList = fNext;
		if (gList) gList->fPrev = NULL;
	} else {
		fPrev->fNext = fNext;
		if (fNext) fNext->fPrev = fPrev;
	}
}

/************************************************************************/
/*																		*/
/*	Event placeholders													*/
/*																		*/
/************************************************************************/

/*$	XGPeriodic::DoPrePeriodic
 *
 *		This method is called when this object receives an event
 *	#before# the event loop reads and processes an event. Normally
 *	this does nothing; override if you are interested in receiving
 *	periodic events.
 */

void XGPeriodic::DoPrePeriodic()
{
}

/*$	XGPeriodic::DoPostPeriodic
 *
 *		This method is called when this object receives an event
 *	#after# the event loop reads and processes an event. Normally
 *	this does nothing; override if you are interested in receiving
 *	periodic events.
 */

void XGPeriodic::DoPostPeriodic()
{
}

/************************************************************************/
/*																		*/
/*	Send Message														*/
/*																		*/
/************************************************************************/

/*$	XGPeriodic::SendPeriodic
 *
 *		Send either the pre or post periodic event, depending on the
 *	setting of the #post# flag. This periodic event is only sent to
 *	the XGPeriodic objects, and is normally called by the YAAF
 *	event loop itself. Only call this if you know what you are
 *	doing.
 */

void XGPeriodic::SendPeriodic(bool post)
{
	XGPeriodic *foo;
	
	for (foo = gList; foo != NULL; foo = foo->fNext) {
		if (post) foo->DoPostPeriodic();
		else foo->DoPrePeriodic();
	}
}
