#include #include #include #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