Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_glfw_events.c
4150 views
1
/*
2
* Copyright 2016 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#if USE_GLFW == 2
9
#include <GL/glfw.h>
10
#else
11
#include <GLFW/glfw3.h>
12
#endif
13
#include <stdio.h>
14
#include <emscripten.h>
15
16
#define MULTILINE(...) #__VA_ARGS__
17
#define WIDTH 640
18
#define HEIGHT 480
19
20
// Setup tests
21
typedef struct {
22
int mouse;
23
double x, y;
24
int button;
25
int action;
26
int modify;
27
int character;
28
} test_args_t;
29
30
typedef struct {
31
char cmd[80];
32
test_args_t args;
33
} test_t;
34
35
// Javascript event.button 0 = left, 1 = middle, 2 = right
36
test_t g_tests[] = {
37
{ "simulateMouseDown(10.0, 10.0, 0)", { 1, 10.0, 10.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, -1 } },
38
{ "simulateMouseUp (10.0, 20.0, 0)", { 1, 10.0, 20.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, -1 } },
39
{ "simulateMouseDown(10.0, 30.0, 1)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, -1 } },
40
{ "simulateMouseUp (10.0, 40.0, 1)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_RELEASE, -1 } },
41
{ "simulateMouseDown(10.0, 30.0, 2)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, -1 } },
42
{ "simulateMouseUp (10.0, 40.0, 2)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, -1 } },
43
//{ "Module.injectMouseEvent(10.0, 50.0, 'mousewheel', 0)", { 10.0, 50.0, -1, -1, -1 } },
44
//{ "Module.injectMouseEvent(10.0, 60.0, 'mousemove', 0)", { 10.0, 60.0, -1, -1, -1 } }
45
46
{ "simulateKeyDown(8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_PRESS, -1 } },
47
{ "simulateKeyUp (8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_RELEASE, -1 } },
48
{ "simulateKeyDown(9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_PRESS, -1 } },
49
{ "simulateKeyUp (9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_RELEASE, -1 } },
50
{ "simulateKeyDown(112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_PRESS, -1 } },
51
{ "simulateKeyUp (112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_RELEASE, -1 } },
52
{ "simulateKeyDown(37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_PRESS, -1 } },
53
{ "simulateKeyUp (37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_RELEASE, -1 } },
54
{ "simulateKeyDown(39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_PRESS, -1 } },
55
{ "simulateKeyUp (39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_RELEASE, -1 } },
56
{ "simulateKeyDown(38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_PRESS, -1 } },
57
{ "simulateKeyUp (38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_RELEASE, -1 } },
58
{ "simulateKeyDown(40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_PRESS, -1 } },
59
{ "simulateKeyUp (40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_RELEASE, -1 } },
60
#if USE_GLFW == 2
61
{ "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_PRESS, -1 } },
62
{ "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_RELEASE, -1 } },
63
64
{ "simulateKeyDown(65)", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } },
65
{ "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } },
66
{ "simulateKeyUp(65)", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } },
67
68
{ "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } },
69
{ "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } },
70
{ "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } },
71
#else
72
{ "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_PRESS, -1 } },
73
{ "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_RELEASE, -1 } },
74
75
{ "simulateKeyDown(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1 } },
76
{ "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } },
77
{ "simulateKeyUp(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1 } },
78
79
{ "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1, 'A' } },
80
{ "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } },
81
{ "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1, 'A' } },
82
#endif
83
};
84
85
static unsigned int g_test_actual = 0;
86
static unsigned int g_test_count = sizeof(g_tests) / sizeof(test_t);
87
static unsigned int g_state = 0;
88
89
#if USE_GLFW == 2
90
static void on_mouse_button_callback(int button, int action) {
91
#else
92
static void on_mouse_button_callback(GLFWwindow* window, int button, int action, int modify) {
93
#endif
94
test_args_t args = g_tests[g_test_actual].args;
95
if (args.button == button && args.action == action)
96
{
97
g_state |= 1 << g_test_actual;
98
}
99
else
100
{
101
printf("Test %d: FAIL\n", g_test_actual);
102
}
103
}
104
105
#if USE_GLFW == 2
106
static void on_mouse_move(int x, int y) {
107
#else
108
static void on_mouse_move(GLFWwindow* window, double x, double y) {
109
#endif
110
test_args_t args = g_tests[g_test_actual].args;
111
if (args.x == x && args.y == y)
112
{
113
g_state |= 1 << g_test_actual;
114
}
115
else
116
{
117
printf("Test %d: FAIL\n", g_test_actual);
118
}
119
}
120
121
#if USE_GLFW == 2
122
static void on_key_callback(int key, int action) {
123
#else
124
static void on_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
125
#endif
126
test_args_t args = g_tests[g_test_actual].args;
127
if (args.button == key && args.action == action)
128
{
129
g_state |= 1 << g_test_actual;
130
}
131
else
132
{
133
printf("Test %d: FAIL\n", g_test_actual);
134
}
135
}
136
137
#if USE_GLFW == 2
138
static void on_char_callback(int character, int action) {
139
#else
140
static void on_char_callback(GLFWwindow* window, unsigned int character) {
141
#endif
142
test_args_t args = g_tests[g_test_actual].args;
143
if (args.character != -1 && args.character == character) {
144
g_state |= 1 << g_test_actual;
145
} else {
146
printf("Test %d: FAIL\n", g_test_actual);
147
}
148
149
}
150
151
#if USE_GLFW == 3
152
static void on_mouse_wheel(GLFWwindow* window, double x, double y) {
153
test_args_t args = g_tests[g_test_actual].args;
154
if (args.x == x && args.y == y) {
155
g_state |= 1 << g_test_actual;
156
} else {
157
printf("Test %d: FAIL\n", g_test_actual);
158
}
159
}
160
161
static void on_error(int error, const char *msg) {
162
printf("%d: %s\n", error, msg);
163
}
164
#endif
165
166
int main() {
167
unsigned int success = (1 << (sizeof(g_tests) / sizeof(test_t))) - 1; // (2^count)-1;
168
169
glfwInit();
170
171
#if USE_GLFW == 2
172
glfwOpenWindow(WIDTH, HEIGHT, 5, 6, 5, 0, 0, 0, GLFW_WINDOW); // != GL_TRUE)
173
174
glfwSetMousePosCallback(on_mouse_move);
175
glfwSetCharCallback(on_char_callback);
176
#else
177
glfwSetErrorCallback(on_error);
178
printf("%s\n", glfwGetVersionString());
179
180
GLFWwindow * _mainWindow = NULL;
181
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
182
_mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "glfw3_events", NULL, NULL);
183
glfwMakeContextCurrent(_mainWindow);
184
185
glfwSetCursorPosCallback(_mainWindow, on_mouse_move);
186
glfwSetScrollCallback(_mainWindow, on_mouse_wheel);
187
glfwSetCharCallback(_mainWindow, on_char_callback);
188
#endif
189
190
for (int p = 0; p < 2; ++p) { // 2 passes, with and without callbacks.
191
printf("Running Test pass %d\n", p);
192
193
#if USE_GLFW == 2
194
glfwSetMouseButtonCallback(p == 0 ? NULL : on_mouse_button_callback);
195
glfwSetKeyCallback(p == 0 ? NULL : on_key_callback);
196
#else
197
glfwSetMouseButtonCallback(_mainWindow, p == 0 ? NULL : on_mouse_button_callback);
198
glfwSetKeyCallback(_mainWindow, p == 0 ? NULL : on_key_callback);
199
#endif
200
g_state = p == 0 ? success : 0;
201
202
for (int i = 0; i < g_test_count; ++i) {
203
g_test_actual = i;
204
test_t test = g_tests[g_test_actual];
205
206
if (test.args.character == -1) {
207
g_state |= 1 << g_test_actual;
208
}
209
210
emscripten_run_script(test.cmd);
211
212
if (test.args.mouse) {
213
#if USE_GLFW == 2
214
if (glfwGetMouseButton(test.args.button) != test.args.action) {
215
#else
216
if (glfwGetMouseButton(_mainWindow, test.args.button) != test.args.action) {
217
#endif
218
printf("Test %d: FAIL\n", g_test_actual);
219
g_state &= ~(1 << g_test_actual);
220
}
221
} else {
222
// Keyboard.
223
#if USE_GLFW == 2
224
if (test.args.action != -1 && glfwGetKey(test.args.button) != test.args.action) {
225
#else
226
if (test.args.action != -1 && glfwGetKey(_mainWindow, test.args.button) != test.args.action) {
227
#endif
228
printf("Test %d: FAIL\n", g_test_actual);
229
g_state &= ~(1 << g_test_actual);
230
}
231
}
232
}
233
if (g_state != success) {
234
break;
235
}
236
}
237
238
glfwTerminate();
239
240
printf("%d == %d = %d", g_state, success, g_state == success);
241
if (g_state != success) {
242
printf("test failed\n");
243
return 1;
244
}
245
printf("success\n");
246
return 0;
247
}
248
249