-
Notifications
You must be signed in to change notification settings - Fork 2
/
window.c
296 lines (243 loc) · 10.4 KB
/
window.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "glfw.h"
#include "monitor.h"
#include "position.h"
#include "size.h"
#include "window.h"
// Constructor/Destructor
JSValue glfw_window_ctor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst *argv) {
int width, height;
const char* title;
GLFWmonitor* monitor = NULL;
GLFWwindow* share = NULL;
if (JS_ToInt32(ctx, &width, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &height, argv[1]))
return JS_EXCEPTION;
title = JS_ToCString(ctx, argv[2]);
if (!title) return JS_EXCEPTION;
// if (JS_IsObject(argv[3])) {
// monitor = JS_GetOpaque2(ctx, argv[3], glfw_monitor_class_id);
// }
// if (JS_IsObject(argv[4])) {
// share = JS_GetOpaque2(ctx, argv[4], glfw_window_class_id);
// }
return glfw_window_create_window(ctx, width, height, title, monitor, share);
}
// Instance Methods
JSValue glfw_window_make_context_current(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
glfwMakeContextCurrent(window);
return JS_UNDEFINED;
}
JSValue glfw_window_swap_buffers(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
glfwSwapBuffers(window);
return JS_UNDEFINED;
}
// Static Methods
JSValue glfw_window_hint(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
int key;
if (JS_ToInt32(ctx, &key, argv[0]))
return JS_EXCEPTION;
if (JS_IsString(argv[1])) {
const char* value = JS_ToCString(ctx, argv[1]);
glfwWindowHintString(key, value);
} else if (JS_IsBool(argv[1])) {
int value = JS_VALUE_GET_BOOL(argv[1]) == 1 ? GL_TRUE : GL_FALSE;
glfwWindowHint(key, value);
} else if (JS_IsNumber(argv[1])) {
int value;
if (JS_ToInt32(ctx, &value, argv[1]))
return JS_EXCEPTION;
glfwWindowHint(key, value);
} else {
JS_ThrowTypeError(ctx, "Value must be a number or string");
return JS_EXCEPTION;
}
return JS_UNDEFINED;
}
JSValue glfw_window_default_hints(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
glfwDefaultWindowHints();
return JS_UNDEFINED;
}
// Properties
JSValue glfw_window_get_should_close(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
if (glfwWindowShouldClose(window))
return JS_TRUE;
else
return JS_FALSE;
}
JSValue glfw_window_set_should_close(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
int shouldClose = JS_VALUE_GET_BOOL(value) == 1 ? GL_TRUE : GL_FALSE;
glfwSetWindowShouldClose(window, shouldClose);
return JS_UNDEFINED;
}
JSValue glfw_window_set_title(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
if (!JS_IsString(value)) {
JS_ThrowTypeError(ctx, "Title must be a string");
return JS_EXCEPTION;
}
const char* title = JS_ToCString(ctx, value);
glfwSetWindowTitle(window, title);
return JS_UNDEFINED;
}
JSValue glfw_window_set_position(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWPosition* position = JS_GetOpaque2(ctx, value, glfw_position_class_id);
if (!position) return JS_EXCEPTION;
glfwSetWindowPos(window, position->x, position->y);
return JS_UNDEFINED;
}
JSValue glfw_window_get_position(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWPosition* position = js_mallocz(ctx, sizeof(*position));
glfwGetWindowPos(window, &position->x, &position->y);
return glfw_position_new_instance(ctx, position);
}
// TODO: magic these with position setter/getter?
JSValue glfw_window_set_size(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWSize* size = JS_GetOpaque2(ctx, value, glfw_size_class_id);
if (!size) return JS_EXCEPTION;
glfwSetWindowSize(window, size->width, size->height);
return JS_UNDEFINED;
}
JSValue glfw_window_get_size(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWSize* size = js_mallocz(ctx, sizeof(*size));
glfwGetWindowSize(window, &size->width, &size->height);
return glfw_size_new_instance(ctx, size);
}
JSValue glfw_window_get_framebuffer_size(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWSize* size = js_mallocz(ctx, sizeof(*size));
glfwGetFramebufferSize(window, &size->width, &size->height);
return glfw_size_new_instance(ctx, size);
}
JSValue glfw_window_set_opacity(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
double opacity;
if (JS_ToFloat64(ctx, &opacity, value))
return JS_EXCEPTION;
glfwSetWindowOpacity(window, opacity);
return JS_UNDEFINED;
}
JSValue glfw_window_get_opacity(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
return JS_NewFloat64(ctx, glfwGetWindowOpacity(window));
}
JSValue glfw_window_get_monitor(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
GLFWmonitor* monitor = glfwGetWindowMonitor(window);
if (!monitor) {
return JS_UNDEFINED;
}
return glfw_monitor_new_instance(ctx, monitor);
}
// Generate a few simple methods with macros...because I'm lazy. :O
#define TRIGGER_FUNCTIONS(V) \
V(IconifyWindow, iconify) \
V(RestoreWindow, restore) \
V(MaximizeWindow, maximize) \
V(ShowWindow, show) \
V(HideWindow, hide) \
V(FocusWindow, focus) \
V(RequestWindowAttention, requestAttention)
#define MAKE_TRIGGER_METHOD(NativeName, JSName) \
JSValue glfw_window_ ## JSName(JSContext *ctx, JSValueConst this_val, \
int argc, JSValueConst *argv) { \
GLFWwindow* window = JS_GetOpaque2(ctx, this_val, glfw_window_class_id); \
glfw ## NativeName(window); \
if (!window) return JS_EXCEPTION; \
return JS_UNDEFINED; \
}
TRIGGER_FUNCTIONS(MAKE_TRIGGER_METHOD)
#undef MAKE_TRIGGER_METHODS
// Initialization
JSClassDef glfw_window_class_def = {
"Window",
};
#define MAKE_TRIGGER_METHOD_ENTRY(NativeName, JSName) \
JS_CFUNC_DEF(#JSName, 0, glfw_window_ ## JSName),
const JSCFunctionListEntry glfw_window_proto_funcs[] = {
JS_CFUNC_DEF("makeContextCurrent", 0, glfw_window_make_context_current),
JS_CFUNC_DEF("swapBuffers", 0, glfw_window_swap_buffers),
JS_CGETSET_DEF("shouldClose", glfw_window_get_should_close, glfw_window_set_should_close),
JS_CGETSET_DEF("title", NULL, glfw_window_set_title),
JS_CGETSET_DEF("position", glfw_window_get_position, glfw_window_set_position),
JS_CGETSET_DEF("size", glfw_window_get_size, glfw_window_set_size),
JS_CGETSET_DEF("framebufferSize", glfw_window_get_framebuffer_size, NULL),
JS_CGETSET_DEF("opacity", glfw_window_get_opacity, glfw_window_set_opacity),
JS_CGETSET_DEF("monitor", glfw_window_get_monitor, NULL),
TRIGGER_FUNCTIONS(MAKE_TRIGGER_METHOD_ENTRY)
};
#undef MAKE_TRIGGER_METHOD_ENTRY
const JSCFunctionListEntry glfw_window_funcs[] = {
JS_CFUNC_DEF("hint", 0, glfw_window_hint),
JS_CFUNC_DEF("defaultHints", 0, glfw_window_default_hints),
};
#undef TRIGGER_FUNCTIONS
JSValue glfw_window_proto, glfw_window_class;
JSValue glfw_window_constructor(JSContext* ctx) {
JSRuntime* rt = JS_GetRuntime(ctx);
if (!JS_IsRegisteredClass(rt, glfw_window_class_id)) {
JS_NewClassID(&glfw_window_class_id);
JS_NewClass(rt, glfw_window_class_id, &glfw_window_class_def);
glfw_window_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, glfw_window_proto, glfw_window_proto_funcs, countof(glfw_window_proto_funcs));
JS_SetClassProto(ctx, glfw_window_class_id, glfw_window_proto);
glfw_window_class = JS_NewCFunction2(ctx, glfw_window_ctor, "Window", 5, JS_CFUNC_constructor, 0);
JS_SetPropertyFunctionList(ctx, glfw_window_class, glfw_window_funcs, countof(glfw_window_funcs));
JS_SetConstructor(ctx, glfw_window_class, glfw_window_proto);
}
return glfw_window_class;
}
JSValue glfw_window_create_window(JSContext* ctx, int width, int height,
const char* title, GLFWmonitor* monitor,
GLFWwindow* share) {
GLFWwindow* window = glfwCreateWindow(width, height, title, monitor, share);
if (window == NULL) {
glfw_throw(ctx);
return JS_EXCEPTION;
}
return glfw_window_new_instance(ctx, window);
}
JSValue glfw_window_new_instance(JSContext* ctx, GLFWwindow* window) {
JSValue obj = JS_UNDEFINED;
JSValue proto;
proto = JS_GetPropertyStr(ctx, glfw_window_class, "prototype");
if (JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, glfw_window_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, window);
return obj;
fail:
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
int glfw_window_init(JSContext* ctx, JSModuleDef* m) {
JS_SetModuleExport(ctx, m, "Window", glfw_window_constructor(ctx));
return 0;
}
int glfw_window_export(JSContext* ctx, JSModuleDef* m) {
return JS_AddModuleExport(ctx, m, "Window");
}