summaryrefslogtreecommitdiff
path: root/src/glfw/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/glfw/input.c')
-rw-r--r--src/glfw/input.c586
1 files changed, 586 insertions, 0 deletions
diff --git a/src/glfw/input.c b/src/glfw/input.c
new file mode 100644
index 0000000..4233d56
--- /dev/null
+++ b/src/glfw/input.c
@@ -0,0 +1,586 @@
+#include <lua.h>
+#include <lauxlib.h>
+#include <GLFW/glfw3.h>
+#include "setup.h"
+
+
+int glfwGetInputMode_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ int mode = luaL_checkinteger(L, 2);
+ int bind_result = glfwGetInputMode(*window, mode);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwSetInputMode_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ int mode = luaL_checkinteger(L, 2);
+ int value = luaL_checkinteger(L, 3);
+ glfwSetInputMode(*window, mode, value);
+ return 0;
+}
+
+
+int glfwRawMouseMotionSupported_bind(lua_State *L)
+{
+ int bind_result = glfwRawMouseMotionSupported();
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetKeyName_bind(lua_State *L)
+{
+ int key = luaL_checkinteger(L, 1);
+ int scancode = luaL_checkinteger(L, 2);
+ const char * bind_result = glfwGetKeyName(key, scancode);
+ lua_pushstring(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetKeyScancode_bind(lua_State *L)
+{
+ int key = luaL_checkinteger(L, 1);
+ int bind_result = glfwGetKeyScancode(key);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetKey_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ int key = luaL_checkinteger(L, 2);
+ int bind_result = glfwGetKey(*window, key);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetMouseButton_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ int button = luaL_checkinteger(L, 2);
+ int bind_result = glfwGetMouseButton(*window, button);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetCursorPos_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ double xpos, ypos;
+ glfwGetCursorPos(*window, &xpos, &ypos);
+ lua_pushnumber(L, xpos);
+ lua_pushnumber(L, ypos);
+ return 2;
+}
+
+
+int glfwSetCursorPos_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ double xpos = luaL_checknumber(L, 2);
+ double ypos = luaL_checknumber(L, 3);
+ glfwSetCursorPos(*window, xpos, ypos);
+ return 0;
+}
+
+
+static GLFWcursor ** create_cursor(lua_State *L)
+{
+ GLFWcursor **cursor = lua_newuserdata(L, sizeof(GLFWcursor *));
+ luaL_getmetatable(L, glfw_cursor_tname);
+ lua_setmetatable(L, -2);
+}
+
+int glfwCreateCursor_bind(lua_State *L)
+{
+ const GLFWimage * image = lua_touserdata(L, 1);
+ int xhot = luaL_checkinteger(L, 2);
+ int yhot = luaL_checkinteger(L, 3);
+
+ GLFWcursor **cursor = create_cursor(L);
+ *cursor = glfwCreateCursor(image, xhot, yhot);
+ return 1;
+}
+
+
+int glfwCreateStandardCursor_bind(lua_State *L)
+{
+ int shape = luaL_checkinteger(L, 1);
+ GLFWcursor **cursor = create_cursor(L);
+ *cursor = glfwCreateStandardCursor(shape);
+ return 1;
+}
+
+
+int glfwDestroyCursor_bind(lua_State *L)
+{
+ GLFWcursor ** cursor = luaL_checkudata(L, 1, glfw_cursor_tname);
+ glfwDestroyCursor(*cursor);
+ return 0;
+}
+
+
+int glfwSetCursor_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ GLFWcursor ** cursor = luaL_checkudata(L, 2, glfw_cursor_tname);
+ glfwSetCursor(*window, *cursor);
+ return 0;
+}
+
+
+/* helpers for creating callback setter binds */
+static GLFWwindow ** check_callback_args(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ int type = lua_type(L, 2);
+ if (type == LUA_TNIL || type == LUA_TFUNCTION) {
+ return window;
+ }
+ else {
+ luaL_typerror(L, 2, "nil or function");
+ }
+}
+
+#define SET_CALLBACK(L, win, setter, cb, key) \
+do { \
+ /* get window data */ \
+ struct h_glfw_window_data_t *data = glfwGetWindowUserPointer(win); \
+ \
+ /* push old callback */ \
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->key); \
+ \
+ /* set new callback */ \
+ if (lua_isnil(L, 2)) { \
+ /* new "callback" is nil, unset */ \
+ setter(win, NULL); \
+ data->key = LUA_NOREF; \
+ } \
+ else { \
+ /* new callback is function */ \
+ setter(win, cb); \
+ lua_pushvalue(L, 2); \
+ data->key = luaL_ref(L, LUA_REGISTRYINDEX); \
+ } \
+} while(0)
+
+#define RETRIEVE_CALLBACK(win, key) \
+struct h_glfw_window_data_t *data = glfwGetWindowUserPointer(win); \
+if (data->key == LUA_NOREF) { return; } \
+lua_State *L = data->L; \
+lua_rawgeti(L, LUA_REGISTRYINDEX, data->key);
+
+
+static void h_key_cb(
+ GLFWwindow *window,
+ int key, int scancode,
+ int action, int mods)
+{
+ RETRIEVE_CALLBACK(window, key_cb_ref);
+ /* push arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushinteger(L, key);
+ lua_pushinteger(L, scancode);
+ lua_pushinteger(L, action);
+ lua_pushinteger(L, mods);
+ /* call */
+ lua_call(L, 5, 0);
+}
+int glfwSetKeyCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetKeyCallback,
+ h_key_cb, key_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_char_cb(GLFWwindow *window, unsigned int codepoint)
+{
+ RETRIEVE_CALLBACK(window, char_cb_ref);
+ /* push arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushinteger(L, codepoint);
+ /* call */
+ lua_call(L, 2, 0);
+}
+int glfwSetCharCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetCharCallback,
+ h_char_cb, char_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_char_mods_cb(GLFWwindow *window, unsigned int codepoint, int mods)
+{
+ RETRIEVE_CALLBACK(window, char_mods_cb_ref);
+ /* push arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushinteger(L, codepoint);
+ lua_pushinteger(L, mods);
+ /* call */
+ lua_call(L, 3, 0);
+}
+int glfwSetCharModsCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetCharModsCallback,
+ h_char_mods_cb, char_mods_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_mouse_button_cb(GLFWwindow *window, int button, int action, int mods)
+{
+ RETRIEVE_CALLBACK(window, char_mods_cb_ref);
+ /* push arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushinteger(L, button);
+ lua_pushinteger(L, action);
+ lua_pushinteger(L, mods);
+ /* call */
+ lua_call(L, 4, 0);
+}
+int glfwSetMouseButtonCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetMouseButtonCallback,
+ h_mouse_button_cb, mouse_button_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_cursor_pos_cb(GLFWwindow *window, double xpos, double ypos)
+{
+ RETRIEVE_CALLBACK(window, cursor_pos_cb_ref);
+ /* push_arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushnumber(L, xpos);
+ lua_pushnumber(L, ypos);
+ /* call */
+ lua_call(L, 3, 0);
+}
+int glfwSetCursorPosCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetCursorPosCallback,
+ h_cursor_pos_cb, cursor_pos_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_cursor_enter_cb(GLFWwindow *window, int entered)
+{
+ RETRIEVE_CALLBACK(window, cursor_enter_cb_ref);
+ /* push_arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushinteger(L, entered);
+ /* call */
+ lua_call(L, 2, 0);
+}
+int glfwSetCursorEnterCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetCursorEnterCallback,
+ h_cursor_enter_cb, cursor_enter_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_scroll_cb(GLFWwindow *window, double xoffset, double yoffset)
+{
+ RETRIEVE_CALLBACK(window, scroll_cb_ref);
+ /* push_arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_pushnumber(L, xoffset);
+ lua_pushnumber(L, yoffset);
+ /* call */
+ lua_call(L, 3, 0);
+
+}
+int glfwSetScrollCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetScrollCallback,
+ h_scroll_cb, scroll_cb_ref
+ );
+ return 1;
+}
+
+
+static void h_drop_cb(GLFWwindow *window, int path_count, const char **paths)
+{
+ RETRIEVE_CALLBACK(window, drop_cb_ref);
+ /* push_arguments */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, data->self_ref);
+ lua_createtable(L, path_count, 0);
+ int tbl = lua_gettop(L);
+ for (int i=0; i<path_count; i++) {
+ lua_pushstring(L, paths[i]);
+ lua_rawseti(L, tbl, i+1);
+ }
+ /* call */
+ lua_call(L, 2, 0);
+
+}
+int glfwSetDropCallback_bind(lua_State *L)
+{
+ GLFWwindow **window = check_callback_args(L);
+ SET_CALLBACK(
+ L, *window,
+ glfwSetDropCallback,
+ h_drop_cb, drop_cb_ref
+ );
+ return 1;
+}
+
+
+int glfwJoystickPresent_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ int bind_result = glfwJoystickPresent(jid);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetJoystickAxes_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ int count;
+ const float * bind_result = glfwGetJoystickAxes(jid, &count);
+
+ lua_createtable(L, count, 0);
+ int tbl = lua_gettop(L);
+ for (int i=0; i<count; i++) {
+ lua_pushnumber(L, bind_result[i]);
+ lua_rawseti(L, tbl, i+1);
+ }
+ return 1;
+}
+
+
+int glfwGetJoystickButtons_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ int count;
+ const unsigned char * bind_result = glfwGetJoystickButtons(jid, &count);
+
+ lua_createtable(L, count, 0);
+ int tbl = lua_gettop(L);
+ for (int i=0; i<count; i++) {
+ lua_pushinteger(L, bind_result[i]);
+ lua_rawseti(L, tbl, i+1);
+ }
+ return 1;
+}
+
+
+int glfwGetJoystickHats_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ int count;
+ const unsigned char * bind_result = glfwGetJoystickHats(jid, &count);
+
+ lua_createtable(L, count, 0);
+ int tbl = lua_gettop(L);
+ for (int i=0; i<count; i++) {
+ lua_pushinteger(L, bind_result[i]);
+ lua_rawseti(L, tbl, i+1);
+ }
+ return 1;
+}
+
+
+int glfwGetJoystickName_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ const char * bind_result = glfwGetJoystickName(jid);
+ lua_pushstring(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetJoystickGUID_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ const char * bind_result = glfwGetJoystickGUID(jid);
+ lua_pushstring(L, bind_result);
+ return 1;
+}
+
+
+int glfwJoystickIsGamepad_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ int bind_result = glfwJoystickIsGamepad(jid);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+struct h_js_data_t {
+ lua_State *L;
+ int cb_ref;
+} static h_js_data = { NULL, LUA_NOREF };
+static void h_joystick_cb(int jid, int event)
+{
+ if (h_js_data.cb_ref == LUA_NOREF) { return; }
+
+ lua_State *L = h_js_data.L;
+ lua_rawgeti(L, LUA_REGISTRYINDEX, h_js_data.cb_ref);
+ lua_pushinteger(L, jid);
+ lua_pushinteger(L, event);
+ lua_call(L, 2, 0);
+}
+int glfwSetJoystickCallback_bind(lua_State *L)
+{
+ int type = lua_type(L, 1);
+ if (type != LUA_TNIL && type != LUA_TFUNCTION) {
+ return luaL_typerror(L, 1, "function or nil");
+ }
+
+ /* push old cb */
+ h_js_data.L = L;
+ lua_rawgeti(L, LUA_REGISTRYINDEX, h_js_data.cb_ref);
+
+ /* set new cb */
+ if (type == LUA_TNIL) {
+ glfwSetJoystickCallback(NULL);
+ h_js_data.cb_ref = LUA_NOREF;
+ }
+ else {
+ glfwSetJoystickCallback(h_joystick_cb);
+ lua_pushvalue(L, 1);
+ h_js_data.cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+ }
+
+ return 1;
+}
+
+
+int glfwUpdateGamepadMappings_bind(lua_State *L)
+{
+ const char * string = luaL_checkstring(L, 1);
+ int bind_result = glfwUpdateGamepadMappings(string);
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetGamepadName_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ const char * bind_result = glfwGetGamepadName(jid);
+ lua_pushstring(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetGamepadState_bind(lua_State *L)
+{
+ int jid = luaL_checkinteger(L, 1);
+ GLFWgamepadstate state;
+ int bind_result = glfwGetGamepadState(jid, &state);
+ lua_pushinteger(L, bind_result);
+ if (bind_result == GLFW_FALSE) {
+ lua_pushnil(L);
+ lua_pushnil(L);
+ }
+ else {
+ lua_createtable(L, 15, 0);
+ int buttons = lua_gettop(L);
+ for (int i=0; i<15; i++) {
+ lua_pushinteger(L, state.buttons[i]);
+ lua_rawseti(L, buttons, i+1);
+ }
+ lua_createtable(L, 6, 0);
+ int axes = lua_gettop(L);
+ for (int i=0; i<6; i++) {
+ lua_pushnumber(L, state.axes[i]);
+ lua_rawseti(L, axes, i+1);
+ }
+ }
+
+ return 3;
+}
+
+
+int glfwSetClipboardString_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ const char * string = luaL_checkstring(L, 2);
+ glfwSetClipboardString(*window, string);
+ return 0;
+}
+
+
+int glfwGetClipboardString_bind(lua_State *L)
+{
+ GLFWwindow ** window = luaL_checkudata(L, 1, glfw_window_tname);
+ const char * bind_result = glfwGetClipboardString(*window);
+ lua_pushstring(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetTime_bind(lua_State *L)
+{
+ double bind_result = glfwGetTime();
+ lua_pushnumber(L, bind_result);
+ return 1;
+}
+
+
+int glfwSetTime_bind(lua_State *L)
+{
+ double time = luaL_checknumber(L, 1);
+ glfwSetTime(time);
+ return 0;
+}
+
+
+int glfwGetTimerValue_bind(lua_State *L)
+{
+ uint64_t bind_result = glfwGetTimerValue();
+ lua_pushinteger(L, bind_result);
+ return 1;
+}
+
+
+int glfwGetTimerFrequency_bind(lua_State *L)
+{
+ uint64_t bind_result = glfwGetTimerFrequency();
+ lua_pushinteger(L, bind_result);
+ return 1;
+}