-
Notifications
You must be signed in to change notification settings - Fork 2
/
glfw.c
188 lines (162 loc) · 7.98 KB
/
glfw.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
#include "glfw.h"
#include "position.h"
#include "size.h"
#include "window.h"
#include "monitor.h"
//
// Top-level
//
JSValue glfw_poll_events(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
glfwPollEvents();
return JS_UNDEFINED;
}
JSValue glfw_wait_events(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
if (JS_IsNumber(argv[0])) {
double timeout;
if (JS_ToFloat64(ctx, &timeout, argv[0]))
return JS_EXCEPTION;
glfwWaitEventsTimeout(timeout);
} else {
glfwWaitEvents();
}
return JS_UNDEFINED;
}
JSValue glfw_post_empty_event(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
if (JS_IsNumber(argv[0])) {
double timeout;
if (JS_ToFloat64(ctx, &timeout, argv[0]))
return JS_EXCEPTION;
glfwWaitEventsTimeout(timeout);
} else {
glfwWaitEvents();
}
return JS_UNDEFINED;
}
// Context
JSValue glfw_context_get_current(JSContext *ctx, JSValueConst this_val) {
GLFWwindow* window = glfwGetCurrentContext();
if (!window) return JS_EXCEPTION;
// TODO: window is not owned so finalizer should not destroy it
return glfw_window_new_instance(ctx, window);
}
JSValue glfw_context_set_current(JSContext *ctx, JSValueConst this_val, JSValueConst value) {
GLFWwindow* window = JS_GetOpaque2(ctx, value, glfw_window_class_id);
if (!window) return JS_EXCEPTION;
glfwMakeContextCurrent(window);
return JS_UNDEFINED;
}
JSValue glfw_context_swap_interval(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
int interval;
if (JS_ToInt32(ctx, &interval, argv[0]))
return JS_EXCEPTION;
glfwSwapInterval(interval);
return JS_UNDEFINED;
}
static const JSCFunctionListEntry glfw_context_props[] = {
JS_CGETSET_DEF("current", glfw_context_get_current, glfw_context_set_current),
JS_CFUNC_DEF("swapInterval", 2, glfw_context_swap_interval),
};
// Version
JSValue glfw_version_to_string(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
return JS_NewString(ctx, glfwGetVersionString());
}
#define CONSTANTS(V) \
V(FOCUSED) \
V(ICONIFIED) \
V(RESIZABLE) \
V(VISIBLE) \
V(DECORATED) \
V(AUTO_ICONIFY) \
V(FLOATING) \
V(MAXIMIZED) \
V(CENTER_CURSOR) \
V(TRANSPARENT_FRAMEBUFFER) \
V(HOVERED) \
V(FOCUS_ON_SHOW) \
V(RED_BITS) \
V(GREEN_BITS) \
V(BLUE_BITS) \
V(ALPHA_BITS) \
V(DEPTH_BITS) \
V(STENCIL_BITS) \
V(ACCUM_RED_BITS) \
V(ACCUM_GREEN_BITS) \
V(ACCUM_BLUE_BITS) \
V(ACCUM_ALPHA_BITS) \
V(AUX_BUFFERS) \
V(STEREO) \
V(SAMPLES) \
V(SRGB_CAPABLE) \
V(REFRESH_RATE) \
V(DOUBLEBUFFER) \
V(CLIENT_API) \
V(CONTEXT_VERSION_MAJOR) \
V(CONTEXT_VERSION_MINOR) \
V(CONTEXT_REVISION) \
V(CONTEXT_ROBUSTNESS) \
V(OPENGL_FORWARD_COMPAT) \
V(OPENGL_DEBUG_CONTEXT) \
V(OPENGL_PROFILE) \
V(CONTEXT_RELEASE_BEHAVIOR) \
V(CONTEXT_NO_ERROR) \
V(CONTEXT_CREATION_API) \
V(SCALE_TO_MONITOR) \
V(COCOA_RETINA_FRAMEBUFFER) \
V(COCOA_FRAME_NAME) \
V(COCOA_GRAPHICS_SWITCHING) \
V(X11_CLASS_NAME) \
V(X11_INSTANCE_NAME) \
V(OPENGL_CORE_PROFILE) \
V(VERSION_MAJOR) \
V(VERSION_MINOR) \
V(VERSION_REVISION) \
V(JOYSTICK_HAT_BUTTONS) \
V(COCOA_CHDIR_RESOURCES) \
V(COCOA_MENUBAR)
#define DEFINE_CONSTANT(Name) \
JS_PROP_INT32_DEF(#Name, GLFW_ ## Name, 0),
const JSCFunctionListEntry glfw_exports[] = {
JS_CFUNC_DEF("poll", 2, glfw_poll_events),
JS_CFUNC_DEF("wait", 2, glfw_wait_events),
JS_CFUNC_DEF("postEmptyEvent", 2, glfw_post_empty_event),
JS_OBJECT_DEF("context", glfw_context_props, countof(glfw_context_props), JS_PROP_CONFIGURABLE),
CONSTANTS(DEFINE_CONSTANT)
};
#undef CONSTANTS
#undef DEFINE_CONSTANT
// Initialization
int glfw_init(JSContext* ctx, JSModuleDef* m) {
JS_SetModuleExportList(ctx, m, glfw_exports, countof(glfw_exports));
glfw_position_init(ctx, m);
glfw_size_init(ctx, m);
glfw_window_init(ctx, m);
glfw_monitor_init(ctx, m);
// TODO: lazy-load version info with a getter?
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
JSValue version = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, version, "major", JS_NewInt32(ctx, major));
JS_SetPropertyStr(ctx, version, "minor", JS_NewInt32(ctx, minor));
JS_SetPropertyStr(ctx, version, "revision", JS_NewInt32(ctx, revision));
JS_SetPropertyStr(ctx, version, "toString", JS_NewCFunction(ctx, glfw_version_to_string, "toString", 0));
JS_SetModuleExport(ctx, m, "version", version);
return 0;
}
int glfw_export(JSContext* ctx, JSModuleDef* m) {
JS_AddModuleExportList(ctx, m, glfw_exports, countof(glfw_exports));
glfw_position_export(ctx, m);
glfw_size_export(ctx, m);
glfw_window_export(ctx, m);
glfw_monitor_export(ctx, m);
JS_AddModuleExport(ctx, m, "version");
return 0;
}
JSModuleDef* js_init_module(JSContext* ctx, const char* module_name) {
JSModuleDef* m = JS_NewCModule(ctx, module_name, glfw_init);
if (!m) return NULL;
glfw_export(ctx, m);
// TODO: Is it possible to check errors for init and throw in module import?
glfwInit();
atexit(glfwTerminate);
return m;
}