diff options
author | sanine <sanine.not@pm.me> | 2023-03-10 02:05:18 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-03-10 02:05:18 -0600 |
commit | 51c7235d4e0a2df109dd5050328a0ad4a1878ae4 (patch) | |
tree | 201b065e93bf6a35d2bab0e49bcd32f7a971da94 /src/glfw/monitor.c | |
parent | 5bb783912ac384156b8abbe6e83a5a61da73881d (diff) |
refactor: move glfw functions into separate table
Diffstat (limited to 'src/glfw/monitor.c')
-rw-r--r-- | src/glfw/monitor.c | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/src/glfw/monitor.c b/src/glfw/monitor.c new file mode 100644 index 0000000..0f1b3fc --- /dev/null +++ b/src/glfw/monitor.c @@ -0,0 +1,250 @@ +#include <stdlib.h> +#include <lua.h> +#include <lauxlib.h> +#include <GLFW/glfw3.h> +#include "setup.h" + + +GLFWmonitor ** create_monitor(lua_State *L) +{ + GLFWmonitor ** monitor = lua_newuserdata(L, sizeof(GLFWmonitor *)); + luaL_getmetatable(L, glfw_monitor_tname); + lua_setmetatable(L, -2); + return monitor; +} + +int glfwGetMonitors_bind(lua_State *L) +{ + int count; + GLFWmonitor ** bind_result = glfwGetMonitors(&count); + lua_createtable(L, count, 0); + int tbl = lua_gettop(L); + for (int i=0; i<count; i++) { + GLFWmonitor **monitor = create_monitor(L); + *monitor = bind_result[i]; + lua_rawseti(L, tbl, i+1); + } + return 1; +} + + +int glfwGetPrimaryMonitor_bind(lua_State *L) +{ + GLFWmonitor **monitor = create_monitor(L); + *monitor = glfwGetPrimaryMonitor(); + return 1; +} + + +int glfwGetMonitorPos_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + int xpos, ypos; + glfwGetMonitorPos(monitor, &xpos, &ypos); + lua_pushinteger(L, xpos); + lua_pushinteger(L, ypos); + return 2; +} + + +int glfwGetMonitorWorkarea_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + int xpos, ypos, width, height; + glfwGetMonitorWorkarea(monitor, &xpos, &ypos, &width, &height); + lua_pushinteger(L, xpos); + lua_pushinteger(L, ypos); + lua_pushinteger(L, width); + lua_pushinteger(L, height); + return 4; +} + + +int glfwGetMonitorPhysicalSize_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + int widthMM, heightMM; + glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM); + lua_pushinteger(L, widthMM); + lua_pushinteger(L, heightMM); + return 2; +} + + +int glfwGetMonitorContentScale_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + float xscale, yscale; + glfwGetMonitorContentScale(monitor, &xscale, &yscale); + lua_pushnumber(L, xscale); + lua_pushnumber(L, yscale); + return 2; +} + + +int glfwGetMonitorName_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + const char * bind_result = glfwGetMonitorName(monitor); + lua_pushstring(L, bind_result); + return 1; +} + + +struct h_glfw_monitor_data_t { + lua_State *L; + int cb_ref; +} static h_monitor_data = { NULL, LUA_NOREF }; +static void h_monitor_cb(GLFWmonitor *monitor, int event) +{ + if (h_monitor_data.cb_ref == LUA_NOREF) { return; } + + lua_State *L = h_monitor_data.L; + lua_rawgeti(L, LUA_REGISTRYINDEX, h_monitor_data.cb_ref); + GLFWmonitor **m = create_monitor(L); + *m = monitor; + lua_pushinteger(L, event); + lua_call(L, 2, 0); +} + +int glfwSetMonitorCallback_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_monitor_data.L = L; + lua_rawgeti(L, LUA_REGISTRYINDEX, h_monitor_data.cb_ref); + + /* set new cb */ + if (type == LUA_TNIL) { + glfwSetMonitorCallback(NULL); + h_monitor_data.cb_ref = LUA_NOREF; + } + else { + glfwSetMonitorCallback(h_monitor_cb); + lua_pushvalue(L, 1); + h_monitor_data.cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + + return 1; +} + + +static void push_vidmode(lua_State *L, GLFWvidmode mode) +{ + lua_createtable(L, 0, 6); + int tbl = lua_gettop(L); + + lua_pushinteger(L, mode.width); + lua_setfield(L, tbl, "width"); + lua_pushinteger(L, mode.height); + lua_setfield(L, tbl, "height"); + lua_pushinteger(L, mode.redBits); + lua_setfield(L, tbl, "redBits"); + lua_pushinteger(L, mode.greenBits); + lua_setfield(L, tbl, "greenBits"); + lua_pushinteger(L, mode.blueBits); + lua_setfield(L, tbl, "blueBits"); + lua_pushinteger(L, mode.refreshRate); + lua_setfield(L, tbl, "refreshRate"); +} +int glfwGetVideoModes_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + int count; + const GLFWvidmode * bind_result = glfwGetVideoModes(monitor, &count); + + lua_createtable(L, count, 0); + int tbl = lua_gettop(L); + for (int i=0; i<count; i++) { + push_vidmode(L, bind_result[i]); + lua_rawseti(L, tbl, i+1); + } + return 1; +} + + +int glfwGetVideoMode_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + const GLFWvidmode * bind_result = glfwGetVideoMode(monitor); + push_vidmode(L, *bind_result); + return 1; +} + + +int glfwSetGamma_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + float gamma = luaL_checknumber(L, 2); + glfwSetGamma(monitor, gamma); + return 0; +} + + +int glfwGetGammaRamp_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + const GLFWgammaramp * ramp = glfwGetGammaRamp(monitor); + lua_createtable(L, ramp->size, 0); + int ramp_tbl = lua_gettop(L); + for (int i=0; i<ramp->size; i++) { + lua_createtable(L, 3, 0); + int tbl = lua_gettop(L); + lua_pushinteger(L, ramp->red[i]); + lua_rawseti(L, tbl, 1); + lua_pushinteger(L, ramp->green[i]); + lua_rawseti(L, tbl, 2); + lua_pushinteger(L, ramp->blue[i]); + lua_rawseti(L, tbl, 3); + lua_rawseti(L, ramp_tbl, i+1); + } + + return 1; +} + + +int glfwSetGammaRamp_bind(lua_State *L) +{ + GLFWmonitor * monitor = luaL_checkudata(L, 1, glfw_monitor_tname); + luaL_checktype(L, 2, LUA_TTABLE); + + GLFWgammaramp ramp = { NULL, NULL, NULL, 0 }; + size_t len = lua_objlen(L, 2); + ramp.red = malloc(len * sizeof(unsigned short)); + ramp.green = malloc(len * sizeof(unsigned short)); + ramp.blue = malloc(len * sizeof(unsigned short)); + ramp.size = len; + if ( + ramp.red == NULL || + ramp.green == NULL || + ramp.blue == NULL + ) { + return luaL_error( + L, + "failed to allocate one or more gamma ramp " + "color buffers of %lu bytes each", + len * sizeof(unsigned short) + ); + } + + for (int i=0; i<len; i++) { + lua_rawgeti(L, 2, i+1); + int tbl = lua_gettop(L); + lua_rawgeti(L, tbl, 1); + ramp.red[i] = lua_tonumber(L, -1); + lua_rawgeti(L, tbl, 2); + ramp.green[i] = lua_tonumber(L, -1); + lua_rawgeti(L, tbl, 3); + ramp.blue[i] = lua_tonumber(L, -1); + lua_pop(L, 4); + } + + glfwSetGammaRamp(monitor, &ramp); + return 0; +} + + |