/*  $Id: input_buffer.cpp,v 1.6 2003/07/20 22:31:49 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "Display/display_precomp.h"
#include "API/Display/input_buffer.h"
#include "API/Display/input_context.h"
#include "input_buffer_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_InputBuffer construction:

CL_InputBuffer::CL_InputBuffer()
: impl(new CL_InputBuffer_Generic)
{
	impl->add_ref();
}

CL_InputBuffer::CL_InputBuffer(CL_InputDevice &device)
: impl(new CL_InputBuffer_Generic)
{
	add_device(device);
	impl->add_ref();
}

CL_InputBuffer::CL_InputBuffer(CL_InputContext *ic)
: impl(new CL_InputBuffer_Generic)
{
	add_devices(ic);
	impl->add_ref();
}

CL_InputBuffer::CL_InputBuffer(const CL_InputBuffer &copy)
: impl(copy.impl)
{
	if (impl) impl->add_ref();
}

CL_InputBuffer::~CL_InputBuffer()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputBuffer attributes:

int CL_InputBuffer::keys_left() const
{
	return impl->buffer.size();
}

CL_InputEvent CL_InputBuffer::peek_key() const
{
	if (impl->buffer.empty()) return CL_InputEvent();
	return impl->buffer.front();
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputBuffer operations:

CL_InputBuffer &CL_InputBuffer::operator =(const CL_InputBuffer &copy)
{
	if (impl) impl->release_ref();
	impl = copy.impl;
	if (impl) impl->add_ref();
	return *this;
}

void CL_InputBuffer::add_device(CL_InputDevice &device)
{
	impl->slots.connect(device.sig_key_down(), impl, &CL_InputBuffer_Generic::on_key_down);
	impl->slots.connect(device.sig_key_up(), impl, &CL_InputBuffer_Generic::on_key_up);
}

void CL_InputBuffer::add_devices(CL_InputContext *ic)
{
	for (int k = 0; k < ic->get_keyboard_count(); k++)
	{
		impl->slots.connect(ic->get_keyboard(k).sig_key_down(), impl, &CL_InputBuffer_Generic::on_key_down);
		impl->slots.connect(ic->get_keyboard(k).sig_key_up(), impl, &CL_InputBuffer_Generic::on_key_up);
	}

	for (int m = 0; m < ic->get_mouse_count(); m++)
	{
		impl->slots.connect(ic->get_mouse(m).sig_key_down(), impl, &CL_InputBuffer_Generic::on_key_down);
		impl->slots.connect(ic->get_mouse(m).sig_key_up(), impl, &CL_InputBuffer_Generic::on_key_up);
	}

	for (int j = 0; j < ic->get_joystick_count(); j++)
	{
		impl->slots.connect(ic->get_joystick(j).sig_key_down(), impl, &CL_InputBuffer_Generic::on_key_down);
		impl->slots.connect(ic->get_joystick(j).sig_key_up(), impl, &CL_InputBuffer_Generic::on_key_up);
	}
}

CL_InputEvent CL_InputBuffer::pop_key()
{
	if (impl->buffer.empty()) return CL_InputEvent();

	CL_InputEvent k = impl->buffer.front();
	impl->buffer.pop();
	return k;
}

void CL_InputBuffer::clear()
{
	impl->buffer = std::queue<CL_InputEvent>();
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputBuffer implementation:
