/*****************************************************************************
 * $Id: keyboard_opengl.cc,v 1.2 2006/09/02 17:37:12 jimb Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Keyboard functions for input event queue.
 *//*
 * LEGAL:	COPYRIGHT (C) 2006 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define GFXINPUT_KEYBOARD_OPENGL_CC 1
#include "gfx.hh"
#include "gfx_gfxsys.hh"
using namespace gfx;
#include "input_queue.hh"
#include "keyboard_opengl.hh"

namespace gfxInput {

/*****************************************************************************
 * Install/activate keyboard handlers.
 *****************************************************************************/
void
GfxsysEnableKeyboard( void )
{
    // Key press/down.
	glutKeyboardFunc( OpenglKeyboard );
	glutSpecialFunc( OpenglKeyboardSpecial );
    // Key release/up.
	glutKeyboardUpFunc( OpenglKeyboardRelease );
	glutSpecialUpFunc( OpenglKeyboardSpecialRelease );
}

/*****************************************************************************
 * OpenGL keyboard handlers.
 *****************************************************************************/
// key press/down
static void
OpenglKeyboard( unsigned char key, int x, int y )
{
	OpenglKeyboardCommon( key, false, true );
}
static void
OpenglKeyboardSpecial( int key, int x, int y )
{
	OpenglKeyboardCommon( key, true, true );
}
// key release/up
static void
OpenglKeyboardRelease( unsigned char key, int x, int y )
{
	OpenglKeyboardCommon( key, false, false );
}
static void
OpenglKeyboardSpecialRelease( int key, int x, int y )
{
	OpenglKeyboardCommon( key, true, false );
}
static void
OpenglKeyboardCommon( int key, bool special, bool press )
{
	int modifiers = glutGetModifiers();
	bool shift = modifiers & GLUT_ACTIVE_SHIFT;
	bool ctrl  = modifiers & GLUT_ACTIVE_CTRL;
	bool alt   = modifiers & GLUT_ACTIVE_ALT;
	InputQueue::GetInstance()->PutKeyEvent( key, special, press, shift, ctrl, alt );
}

} // namespace gfxInput
