summaryrefslogtreecommitdiff
path: root/src/glfw/monitor.c
blob: 0f1b3fcff60040dccf676d00336e6d0d6bfcc3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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;
}