/* Copyright (C) 2006 Cole Krumbholz */ /* */ /* This is free software; you can redistribute it and/or modify it */ /* under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /* This program 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 */ /* General Public License for more details. */ #include "tabletracker-lua.h" #include static int TTlua_open(lua_State *L) { unsigned short port = (unsigned short) luaL_checkint(L, 1); lua_pushnumber(L, TT_open(port)); return 1; } static int TTlua_poll(lua_State *L) { unsigned int timeout = (unsigned int) luaL_checkint(L, 1); lua_pushboolean(L, TT_poll(timeout)); return 1; } static int TTlua_get(lua_State *L) { int id, type, err, args = 1; double xpos, ypos, angle; err = TT_get(&id, &xpos, &ypos, &angle, &type); lua_pushnumber(L, err); if (err == TT_OK) { lua_pushnumber(L, err); lua_pushnumber(L, id); lua_pushnumber(L, xpos); lua_pushnumber(L, ypos); lua_pushnumber(L, angle); lua_pushnumber(L, type); args = 6; } return args; } static int TTlua_close(lua_State *L) { TT_close(); return 0; } /* ------------------------------------------------------------------------ */ static void lua_function(lua_State *L, const char *name, lua_CFunction func) { lua_pushstring(L, name); lua_pushcfunction(L, func); lua_settable(L, -3); } static void lua_constant(lua_State *L, const char *name, int value) { lua_pushstring(L, name); lua_pushnumber(L, value); lua_settable(L, -3); } void luaopen_tabletracker(lua_State *L) { lua_pushstring(L, "TT"); lua_newtable(L); lua_function(L, "open", TTlua_open ); lua_function(L, "close", TTlua_close); lua_function(L, "poll", TTlua_poll ); lua_function(L, "get", TTlua_get ); lua_constant(L, "OK", TT_OK ); lua_constant(L, "ERR_MESG", TT_ERR_MESG); lua_constant(L, "ERR_BIND", TT_ERR_BIND); lua_constant(L, "ERR_SOCK", TT_ERR_SOCK); lua_settable(L, LUA_GLOBALSINDEX); }