//	Zinc Application Framework - D_EVTMGR.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_evtmgr.hpp>
#include <z_sysevt.hpp>

// ----- ZafEventManager ---------------------------------------------------

int ZafEventManager::Get(ZafEventStruct &event, ZafQFlags flags)
{
	// Initialize the variables.
	ZafDevice *device;
	ZafQueueElement *element;
	int error = -1;

	// Stay in loop while no event conditions are met.
	do
	{
		// Call all the polled devices.
		if (PollDevices(flags))
		{
 			// Check for a user-device to do special message polling.
			for (device = First(); device; device = device->Next())
				device->Poll();
		}

		// Get the event.
		element = ReadFromEnd(flags) ? queueBlock.Last() : queueBlock.First();
		if (element)
		{
			event = element->event;
			if (DestroyEvent(flags))
				queueBlock.Subtract((ZafElement *)element);
			error = 0;		// Normal operation of the event manager.
		}
		else if (Blocked(flags))
		{
			event.type = S_REPAIR_DAMAGE;
			error = 0;
		}
		else
			return (-2);
	} while (error);

	// Return the error status.
	return (error);
}

bool ZafEventManager::Put(const ZafEventStruct &event, ZafQFlags flags)
{
	// Place the event back in the event queue.
	bool returnValue = false;
	ZafQueueElement *element;
	if (ReadFromEnd(flags))
		element = (ZafQueueElement *)queueBlock.Add(ZAF_NULLP(ZafQueueElement));
	else
		element = (ZafQueueElement *)queueBlock.Add(queueBlock.First());
	if (element)
	{
		element->event = event;
		returnValue = true;
	}

	return (returnValue);
}

void ZafEventManager::UnBlock(void)
{
}

