/*****************************************************************************
 * $LastChangedDate: 2011-03-17 19:59:52 -0400 (Thu, 17 Mar 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Interface of C functions for Lua scripts.
 * @remarks sim.Keyboard abstracts input::KeyboardDevice.
 *
 * ----------------------------
 * -- THIS IS AUTO-GENERATED --
 * ----------------------------
 *
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#include "base/module.hh"
using namespace base;
#include "input/module.hh"
using namespace input;
#include "lua_bind/module.hh"
#include "lua_bind/bind.hh"

namespace lua {


namespace bind {

#define BIND_CLASS_NAME      "Keyboard"
#define BIND_METATABLE_NAME  LUABIND_METATABLE_STRING(BIND_CLASS_NAME)

//==============================================================================
// C functions called by Lua that access C++ objects.
//==============================================================================

/*****************************************************************************
 * table|nil Keyboard:DequeueKey()
 * Returns table { press=bool, key=int, lkey=int, shift=bool, ctrl=bool, alt=bool, modifier=bool }. 
 * lkey means key converted to lower-case.
 *****************************************************************************/
INTERN int 
Keyboard_DequeueKey( lua_State* L ) 
{ 
   try { 

        input::KeyboardEvent keyboardEvent;
        if ( GET_KEYBOARD_DEVICE().Dequeue( keyboardEvent ) )
        {
            lua_newtable( L );
            SET_MEMBER_AS_BOOL( L, "press", keyboardEvent.mPress );
            SET_MEMBER_AS_INT(  L, "key",   keyboardEvent.mKey );
            SET_MEMBER_AS_INT(  L, "lkey",  tolower(keyboardEvent.mKey) );
            SET_MEMBER_AS_BOOL( L, "shift", keyboardEvent.mShift );
            SET_MEMBER_AS_BOOL( L, "ctrl",  keyboardEvent.mCtrl );
            SET_MEMBER_AS_BOOL( L, "alt",   keyboardEvent.mAlt );
            SET_MEMBER_AS_BOOL( L, "modifier", keyboardEvent.IfModifier() );  // true if any modifier (Shift/Ctrl/Alt)
        }
        else
        {
            lua_pushnil( L );  // no event
        }
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

//==============================================================================
// Register binding with Lua. 
//==============================================================================

#if ! DOXYGEN 

INTERN const struct luaL_reg sMethodTable[] =
{
  // New(),__gc() omitted as Lua cannot instantiate this C++ class.
  { "DequeueKey", Keyboard_DequeueKey },
  { NULL, NULL }
};

void
RegisterKeyboard( lua_State* L )
{
    REGISTER_BINDING( L,
                      BIND_METATABLE_NAME,
                      BIND_CLASS_NAME,
                      sMethodTable );
}

#endif // ! DOXYGEN 

} // namespace bind 

} // namespace lua 
