/*  $Id: input_device_x11mouse.cpp,v 1.1 2004/01/15 01:07:02 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 <cstdio>

#include "API/Display/input_device.h"
#include "API/Display/input_event.h"
#include "input_device_x11mouse.h"

#include "../../GL/GLX/display_window_opengl.h"



/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_X11Mouse construction:

CL_InputDevice_X11Mouse::CL_InputDevice_X11Mouse(CL_DisplayWindow_OpenGL *owner) :
	mouse_pos(-1,-1), time_at_last_press(0), dblclk_time(500), last_press_id(-1), owner(owner)
{
	type = CL_InputDevice::mouse;
	
	for( int i=0; i<5; i++)
		key_states[i] = false;
	
	slot_xevent = owner->sig_xevent.connect(
		this, &CL_InputDevice_X11Mouse::on_xevent);
}

CL_InputDevice_X11Mouse::~CL_InputDevice_X11Mouse()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_X11Mouse attributes:

int CL_InputDevice_X11Mouse::get_x() const
{
	return mouse_pos.x;
}

int CL_InputDevice_X11Mouse::get_y() const
{
	return mouse_pos.y;
}

bool CL_InputDevice_X11Mouse::get_keycode(int keycode) const
{
	if( keycode >= 0 && keycode < 5 )
		return key_states[keycode];
	return false;
}

float CL_InputDevice_X11Mouse::get_axis(int index) const
{
	return 0.0f;
}

std::string CL_InputDevice_X11Mouse::get_name() const
{
	return "System Mouse";
}

int CL_InputDevice_X11Mouse::get_axis_count() const
{
	return 0;
}

int CL_InputDevice_X11Mouse::get_button_count() const
{
	return -1;
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_X11Mouse operations:

std::string CL_InputDevice_X11Mouse::get_key_name(int id) const
{
	switch (id)
	{
	case 0: return "Mouse left";
	case 1: return "Mouse right";
	case 2: return "Mouse middle";
	case 3: return "Mouse wheel up";
	case 4: return "Mouse wheel down";
	}

	char buf[256];
	sprintf(buf, "Mouse button %d", id);
	return buf;
}

void CL_InputDevice_X11Mouse::set_position(int x, int y)
{
	XWarpPointer(
		owner->get_display(),
		None,
		owner->get_window(),
		0, 0,
		0, 0,
		x, y);
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_X11Mouse implementation:

void CL_InputDevice_X11Mouse::on_xevent(XEvent &event)
{
	// Only handle mouse events.
	if (event.type != ButtonPress
		&& event.type != ButtonRelease
		&& event.type != MotionNotify )
		return;
	
	if (event.type == ButtonPress || event.type == ButtonRelease )
		received_mouse_input(event);
	else
		received_mouse_move(event);
}

void CL_InputDevice_X11Mouse::received_mouse_input(XEvent &e)
{
	XButtonEvent event = (XButtonEvent&)e;
	
	int id = 0;
	bool down = false;
	bool dblclk = false;
	
	mouse_pos.x = event.x;
	mouse_pos.y = event.y;
	
	switch(event.button)
	{
		case 1: id = 0; break; // left
		case 3: id = 1; break; // right
		case 2: id = 2; break; // middle
		default: id = event.button-1;
	}
	
	
	// there is no double click event in xlib (at least I couldn't find one in the docs -Harry)
	if (event.type == ButtonPress
		&& last_press_id == id
		&& dblclk_time > (event.time - time_at_last_press))
		dblclk = true;
	
	if (event.type == ButtonPress)
	{
		time_at_last_press = event.time;
		last_press_id = id;
	}
	
	CL_InputEvent::Type event_type = CL_InputEvent::released;
	
	if (event.type == ButtonPress)
	{
		event_type = CL_InputEvent::pressed;
		down = true;
	}
	if (dblclk)
		event_type = CL_InputEvent::double_clicked;
	
	if( id >= 0 && id < 5 )
		key_states[id] = down;

	// Prepare event to be emitted:
	CL_InputEvent key;
	key.id = id;
	key.type = event_type;
	key.device = owner->mouse;
	key.mouse_pos = mouse_pos;
	key.left_alt = owner->left_alt_down;
	key.left_ctrl = owner->left_ctrl_down;
	key.left_shift = owner->left_shift_down;
	key.right_alt = owner->right_alt_down;
	key.right_ctrl = owner->right_ctrl_down;
	key.right_shift = owner->right_shift_down;
	
	// Emit message:
	if (down)
	{
		owner->mouse.sig_key_down().call(key);

		if (dblclk)
			owner->mouse.sig_key_dblclk().call(key);
	}
	else
		owner->mouse.sig_key_up().call(key);
}

void CL_InputDevice_X11Mouse::received_mouse_move(XEvent &e)
{
	XMotionEvent event = (XMotionEvent&)e;
	
	mouse_pos.x = event.x;
	mouse_pos.y = event.y;
	
	// Prepare event to be emitted:
	CL_InputEvent key;
	key.type = CL_InputEvent::moved;
	key.device = owner->mouse;
	key.left_alt = owner->left_alt_down;
	key.left_ctrl = owner->left_ctrl_down;
	key.left_shift = owner->left_shift_down;
	key.right_alt = owner->right_alt_down;
	key.right_ctrl = owner->right_ctrl_down;
	key.right_shift = owner->right_shift_down;
	key.mouse_pos = mouse_pos;
	
	// Fire off signal
	owner->mouse.sig_move().call(key);
}
