Skip to content

Commit e15deee

Browse files
committed
Merge branch 'master' of https://github.com/CliffyA/emscripten into incoming
Conflicts: tests/glfw3_events.c tests/test_browser.py
2 parents baac644 + 2a9ce3e commit e15deee

File tree

3 files changed

+283
-19
lines changed

3 files changed

+283
-19
lines changed

src/library_glfw.js

+77-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var LibraryGLFW = {
129129
/* https://developer.mozilla.org/en/Document_Object_Model_%28DOM%29/KeyboardEvent and GLFW/glfw3.h */
130130
DOMToGLFWKeyCode: function(keycode) {
131131
switch (keycode) {
132+
// these keycodes are only defined for GLFW3, assume they are the same for GLFW2
132133
case 0x20:return 32; // DOM_VK_SPACE -> GLFW_KEY_SPACE
133134
case 0xDE:return 39; // DOM_VK_QUOTE -> GLFW_KEY_APOSTROPHE
134135
case 0xBC:return 44; // DOM_VK_COMMA -> GLFW_KEY_COMMA
@@ -177,6 +178,81 @@ var LibraryGLFW = {
177178
case 0xDC:return 92; // DOM_VK_BACKSLASH -> GLFW_KEY_BACKSLASH
178179
case 0xDD:return 93; // DOM_VK_CLOSE_BRACKET -> GLFW_KEY_RIGHT_BRACKET
179180
case 0xC0:return 94; // DOM_VK_BACK_QUOTE -> GLFW_KEY_GRAVE_ACCENT
181+
182+
#if USE_GLFW == 2
183+
//#define GLFW_KEY_SPECIAL 256
184+
case 0x1B:return (256+1); // DOM_VK_ESCAPE -> GLFW_KEY_ESC
185+
case 0x70:return (256+2); // DOM_VK_F1 -> GLFW_KEY_F1
186+
case 0x71:return (256+3); // DOM_VK_F2 -> GLFW_KEY_F2
187+
case 0x72:return (256+4); // DOM_VK_F3 -> GLFW_KEY_F3
188+
case 0x73:return (256+5); // DOM_VK_F4 -> GLFW_KEY_F4
189+
case 0x74:return (256+6); // DOM_VK_F5 -> GLFW_KEY_F5
190+
case 0x75:return (256+7); // DOM_VK_F6 -> GLFW_KEY_F6
191+
case 0x76:return (256+8); // DOM_VK_F7 -> GLFW_KEY_F7
192+
case 0x77:return (256+9); // DOM_VK_F8 -> GLFW_KEY_F8
193+
case 0x78:return (256+10); // DOM_VK_F9 -> GLFW_KEY_F9
194+
case 0x79:return (256+11); // DOM_VK_F10 -> GLFW_KEY_F10
195+
case 0x7A:return (256+12); // DOM_VK_F11 -> GLFW_KEY_F11
196+
case 0x7B:return (256+13); // DOM_VK_F12 -> GLFW_KEY_F12
197+
case 0x7C:return (256+14); // DOM_VK_F13 -> GLFW_KEY_F13
198+
case 0x7D:return (256+15); // DOM_VK_F14 -> GLFW_KEY_F14
199+
case 0x7E:return (256+16); // DOM_VK_F15 -> GLFW_KEY_F15
200+
case 0x7F:return (256+17); // DOM_VK_F16 -> GLFW_KEY_F16
201+
case 0x80:return (256+18); // DOM_VK_F17 -> GLFW_KEY_F17
202+
case 0x81:return (256+19); // DOM_VK_F18 -> GLFW_KEY_F18
203+
case 0x82:return (256+20); // DOM_VK_F19 -> GLFW_KEY_F19
204+
case 0x83:return (256+21); // DOM_VK_F20 -> GLFW_KEY_F20
205+
case 0x84:return (256+22); // DOM_VK_F21 -> GLFW_KEY_F21
206+
case 0x85:return (256+23); // DOM_VK_F22 -> GLFW_KEY_F22
207+
case 0x86:return (256+24); // DOM_VK_F23 -> GLFW_KEY_F23
208+
case 0x87:return (256+25); // DOM_VK_F24 -> GLFW_KEY_F24
209+
case 0x88:return (256+26); // 0x88 (not used?) -> GLFW_KEY_F25
210+
case 0x27:return (256+27); // DOM_VK_RIGHT -> GLFW_KEY_RIGHT
211+
case 0x25:return (256+28); // DOM_VK_LEFT -> GLFW_KEY_LEFT
212+
case 0x28:return (256+29); // DOM_VK_DOWN -> GLFW_KEY_DOWN
213+
case 0x26:return (256+30); // DOM_VK_UP -> GLFW_KEY_UP
214+
case 0x10:return (256+31); // DOM_VK_SHIFT -> GLFW_KEY_LSHIFT
215+
// #define GLFW_KEY_RSHIFT (GLFW_KEY_SPECIAL+32)
216+
case 0x11:return (256+33); // DOM_VK_CONTROL -> GLFW_KEY_LCTRL
217+
// #define GLFW_KEY_RCTRL (GLFW_KEY_SPECIAL+34)
218+
case 0x12:return (256+35); // DOM_VK_ALT -> GLFW_KEY_LALT
219+
// #define GLFW_KEY_RALT (GLFW_KEY_SPECIAL+36)
220+
case 0x09:return (256+37); // DOM_VK_TAB -> GLFW_KEY_TAB
221+
case 0x0D:return (256+38); // DOM_VK_RETURN -> GLFW_KEY_ENTER
222+
case 0x08:return (256+39); // DOM_VK_BACK -> GLFW_KEY_BACKSPACE
223+
case 0x2D:return (256+40); // DOM_VK_INSERT -> GLFW_KEY_INSERT
224+
case 0x2E:return (256+41); // DOM_VK_DELETE -> GLFW_KEY_DEL
225+
case 0x21:return (256+42); // DOM_VK_PAGE_UP -> GLFW_KEY_PAGEUP
226+
case 0x22:return (256+43); // DOM_VK_PAGE_DOWN -> GLFW_KEY_PAGEDOWN
227+
case 0x24:return (256+44); // DOM_VK_HOME -> GLFW_KEY_HOME
228+
case 0x23:return (256+45); // DOM_VK_END -> GLFW_KEY_END
229+
case 0x60:return (256+46); // DOM_VK_NUMPAD0 -> GLFW_KEY_KP_0
230+
case 0x61:return (256+47); // DOM_VK_NUMPAD1 -> GLFW_KEY_KP_1
231+
case 0x62:return (256+48); // DOM_VK_NUMPAD2 -> GLFW_KEY_KP_2
232+
case 0x63:return (256+49); // DOM_VK_NUMPAD3 -> GLFW_KEY_KP_3
233+
case 0x64:return (256+50); // DOM_VK_NUMPAD4 -> GLFW_KEY_KP_4
234+
case 0x65:return (256+51); // DOM_VK_NUMPAD5 -> GLFW_KEY_KP_5
235+
case 0x66:return (256+52); // DOM_VK_NUMPAD6 -> GLFW_KEY_KP_6
236+
case 0x67:return (256+53); // DOM_VK_NUMPAD7 -> GLFW_KEY_KP_7
237+
case 0x68:return (256+54); // DOM_VK_NUMPAD8 -> GLFW_KEY_KP_8
238+
case 0x69:return (256+55); // DOM_VK_NUMPAD9 -> GLFW_KEY_KP_9
239+
case 0x6F:return (256+56); // DOM_VK_DIVIDE -> GLFW_KEY_KP_DIVIDE
240+
case 0x6A:return (256+57); // DOM_VK_MULTIPLY -> GLFW_KEY_KP_MULTIPLY
241+
case 0x6D:return (256+58); // DOM_VK_SUBTRACT -> GLFW_KEY_KP_SUBTRACT
242+
case 0x6B:return (256+59); // DOM_VK_ADD -> GLFW_KEY_KP_ADD
243+
case 0x6E:return (256+60); // DOM_VK_DECIMAL -> GLFW_KEY_KP_DECIMAL
244+
// #define GLFW_KEY_KP_EQUAL (GLFW_KEY_SPECIAL+61)
245+
// #define GLFW_KEY_KP_ENTER (GLFW_KEY_SPECIAL+62)
246+
case 0x90:return (256+63); // DOM_VK_NUM_LOCK -> GLFW_KEY_KP_NUM_LOCK
247+
case 0x14:return (256+64); // DOM_VK_CAPS_LOCK -> GLFW_KEY_CAPS_LOCK
248+
case 0x91:return (256+65); // DOM_VK_SCROLL_LOCK -> GLFW_KEY_SCROLL_LOCK
249+
case 0x13:return (256+66); // DOM_VK_PAUSE -> GLFW_KEY_PAUSE
250+
case 0x5B:return (256+67); // DOM_VK_WIN -> GLFW_KEY_LSUPER
251+
// #define GLFW_KEY_RSUPER (GLFW_KEY_SPECIAL+68)
252+
case 0x5D:return (256+69); // DOM_VK_CONTEXT_MENU -> GLFW_KEY_MENU
253+
#endif
254+
255+
#if USE_GLFW == 3
180256
case 0x1B:return 256; // DOM_VK_ESCAPE -> GLFW_KEY_ESCAPE
181257
case 0x0D:return 257; // DOM_VK_RETURN -> GLFW_KEY_ENTER
182258
case 0x09:return 258; // DOM_VK_TAB -> GLFW_KEY_TAB
@@ -247,8 +323,8 @@ var LibraryGLFW = {
247323
// case 0x12:return 346; // DOM_VK_ALT -> GLFW_KEY_RIGHT_ALT (DOM_KEY_LOCATION_RIGHT)
248324
// case 0x5B:return 347; // DOM_VK_WIN -> GLFW_KEY_RIGHT_SUPER (DOM_KEY_LOCATION_RIGHT)
249325
case 0x5D:return 348; // DOM_VK_CONTEXT_MENU -> GLFW_KEY_MENU
250-
251326
// XXX: GLFW_KEY_WORLD_1, GLFW_KEY_WORLD_2 what are these?
327+
#endif
252328
default:return -1; // GLFW_KEY_UNKNOWN
253329
};
254330
},

tests/glfw_events.c

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#ifdef USE_GLFW
2+
#if USE_GLFW == 2
3+
#include <GL/glfw.h>
4+
#else
5+
#include <GLFW/glfw3.h>
6+
#endif
7+
#include <stdio.h>
8+
#include <emscripten.h>
9+
10+
#define MULTILINE(...) #__VA_ARGS__
11+
#define WIDTH 640
12+
#define HEIGHT 480
13+
14+
// Setup tests
15+
typedef struct {
16+
double x, y;
17+
int button;
18+
int action;
19+
int modify;
20+
} test_args_t;
21+
22+
typedef struct {
23+
char cmd[80];
24+
test_args_t args;
25+
} test_t;
26+
27+
// Javascript event.button 0 = left, 1 = middle, 2 = right
28+
test_t g_tests[] = {
29+
{ "Module.injectMouseEvent(10.0, 10.0, 'mousedown', 0)", { 10.0, 10.0, GLFW_MOUSE_BUTTON_1, GLFW_PRESS, -1 } },
30+
{ "Module.injectMouseEvent(10.0, 20.0, 'mouseup', 0)", { 10.0, 20.0, GLFW_MOUSE_BUTTON_1, GLFW_RELEASE, -1 } },
31+
{ "Module.injectMouseEvent(10.0, 30.0, 'mousedown', 2)", { 10.0, 30.0, GLFW_MOUSE_BUTTON_2, GLFW_PRESS, -1 } },
32+
{ "Module.injectMouseEvent(10.0, 40.0, 'mouseup', 2)", { 10.0, 40.0, GLFW_MOUSE_BUTTON_2, GLFW_RELEASE, -1 } },
33+
//{ "Module.injectMouseEvent(10.0, 50.0, 'mousewheel', 0)", { 10.0, 50.0, -1, -1, -1 } },
34+
//{ "Module.injectMouseEvent(10.0, 60.0, 'mousemove', 0)", { 10.0, 60.0, -1, -1, -1 } }
35+
36+
{ "Module.injectKeyEvent('keydown', 0x08)", { 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_PRESS, -1 } },
37+
{ "Module.injectKeyEvent('keyup', 0x08)", { 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_RELEASE, -1 } },
38+
{ "Module.injectKeyEvent('keydown', 0x09)", { 0.0, 0.0, GLFW_KEY_TAB, GLFW_PRESS, -1 } },
39+
{ "Module.injectKeyEvent('keydown', 0x70)", { 0.0, 0.0, GLFW_KEY_F1, GLFW_PRESS, -1 } },
40+
41+
#if USE_GLFW == 2
42+
{ "Module.injectKeyEvent('keydown', 0x1B)", { 0.0, 0.0, GLFW_KEY_ESC, GLFW_PRESS, -1 } },
43+
#else
44+
{ "Module.injectKeyEvent('keydown', 0x1B)", { 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_PRESS, -1 } },
45+
#endif
46+
};
47+
48+
static unsigned int g_test_actual = 0;
49+
static unsigned int g_test_count = sizeof(g_tests) / sizeof(test_t);
50+
static unsigned int g_state = 0;
51+
52+
#if USE_GLFW == 2
53+
static void on_mouse_button_vallback(int button, int action)
54+
#else
55+
static void on_mouse_button_vallback(GLFWwindow* window, int button, int action, int modify)
56+
#endif
57+
{
58+
test_args_t args = g_tests[g_test_actual].args;
59+
if (args.button == button && args.action == action)
60+
{
61+
g_state |= 1 << g_test_actual;
62+
}
63+
else
64+
{
65+
printf("Test %d: FAIL\n", g_test_actual);
66+
}
67+
}
68+
69+
#if USE_GLFW == 2
70+
static void on_mouse_move(int x, int y)
71+
#else
72+
static void on_mouse_move(GLFWwindow* window, double x, double y)
73+
#endif
74+
{
75+
test_args_t args = g_tests[g_test_actual].args;
76+
if (args.x == x && args.y == y)
77+
{
78+
g_state |= 1 << g_test_actual;
79+
}
80+
else
81+
{
82+
printf("Test %d: FAIL\n", g_test_actual);
83+
}
84+
}
85+
86+
#if USE_GLFW == 2
87+
static void on_key_callback(int key, int action)
88+
#else
89+
static void on_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
90+
#endif
91+
{
92+
test_args_t args = g_tests[g_test_actual].args;
93+
if (args.button == key && args.action == action)
94+
{
95+
g_state |= 1 << g_test_actual;
96+
}
97+
else
98+
{
99+
printf("Test %d: FAIL\n", g_test_actual);
100+
}
101+
}
102+
103+
#if USE_GLFW == 3
104+
static void on_mouse_wheel(GLFWwindow* window, double x, double y)
105+
{
106+
test_args_t args = g_tests[g_test_actual].args;
107+
if (args.x == x && args.y == y)
108+
{
109+
g_state |= 1 << g_test_actual;
110+
}
111+
else
112+
{
113+
printf("Test %d: FAIL\n", g_test_actual);
114+
}
115+
}
116+
117+
static void on_error(int error, const char *msg)
118+
{
119+
printf("%d: %s\n", error, msg);
120+
}
121+
#endif
122+
123+
int main()
124+
{
125+
int result = 0;
126+
unsigned int success = 0;
127+
128+
emscripten_run_script(MULTILINE(
129+
Module.injectMouseEvent = function(x, y, event_, button) {
130+
var canvas = Module['canvas'];
131+
var event = new MouseEvent(event_, {
132+
'view': window,
133+
'bubbles': true,
134+
'cancelable': true,
135+
'screenX': canvas.offsetLeft + x,
136+
'screenY': canvas.offsetTop + y,
137+
'clientX': canvas.offsetLeft + x,
138+
'clientY': canvas.offsetTop + y,
139+
'button': button
140+
});
141+
canvas.dispatchEvent(event);
142+
143+
//var event = document.createEvent("MouseEvents");
144+
//var canvas = Module['canvas'];
145+
//event.initMouseEvent(event_, true, true, window, 0, canvas.offsetLeft + x, canvas.offsetTop + y, canvas.offsetLeft + x, canvas.offsetTop + y, 0, 0, 0, 0, button, null);
146+
//canvas.dispatchEvent(event);
147+
};
148+
149+
Module.injectKeyEvent = function(type, keyCode) {
150+
var keyboardEvent = document.createEvent("KeyboardEvent");
151+
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
152+
keyboardEvent[initMethod](type, true, true, window, false, false, false, false, keyCode, 0);
153+
canvas.dispatchEvent(keyboardEvent);
154+
};
155+
));
156+
157+
158+
glfwInit();
159+
160+
161+
#if USE_GLFW == 2
162+
glfwOpenWindow(WIDTH, HEIGHT, 5, 6, 5, 0, 0, 0, GLFW_WINDOW); // != GL_TRUE)
163+
164+
glfwSetMousePosCallback(on_mouse_move);
165+
glfwSetMouseButtonCallback(on_mouse_button_vallback);
166+
glfwSetKeyCallback(on_key_callback);
167+
//glfwSetCharCallback(...);
168+
#else
169+
glfwSetErrorCallback(on_error);
170+
printf("%s\n", glfwGetVersionString());
171+
172+
GLFWwindow * _mainWindow = NULL;
173+
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
174+
_mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "glfw3_events", NULL, NULL);
175+
glfwMakeContextCurrent(_mainWindow);
176+
177+
glfwSetMouseButtonCallback(_mainWindow, on_mouse_button_vallback);
178+
glfwSetCursorPosCallback(_mainWindow, on_mouse_move);
179+
glfwSetScrollCallback(_mainWindow, on_mouse_wheel);
180+
glfwSetKeyCallback(_mainWindow, on_key_callback);
181+
//glfwSetCharCallback(_mainWindow, ...);
182+
#endif
183+
184+
for (int i = 0; i < g_test_count; ++i)
185+
{
186+
g_test_actual = i;
187+
emscripten_run_script(g_tests[g_test_actual].cmd);
188+
}
189+
190+
glfwTerminate();
191+
192+
success = (1 << (sizeof(g_tests) / sizeof(test_t))) - 1; // (2^count)-1
193+
194+
#ifdef REPORT_RESULT
195+
result = g_state == success;
196+
REPORT_RESULT();
197+
#else
198+
printf("%d == %d = %d", g_state, success, g_state == success);
199+
#endif
200+
201+
return 0;
202+
}
203+
#endif

tests/test_browser.py

+3-18
Original file line numberDiff line numberDiff line change
@@ -2184,24 +2184,9 @@ def in_html(expected):
21842184
def test_glfw3(self):
21852185
self.btest(path_from_root('tests', 'glfw3.c'), args=['-s', 'LEGACY_GL_EMULATION=1', '-s', 'USE_GLFW=3'], expected='1')
21862186

2187-
def test_glfw3_events(self):
2188-
open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
2189-
function keydown(c) {
2190-
var event = document.createEvent("KeyboardEvent");
2191-
event.initKeyEvent('keydown', true, true, window,
2192-
0, 0, 0, 0,
2193-
c, c);
2194-
document.dispatchEvent(event);
2195-
}
2196-
function keyup(c) {
2197-
var event = document.createEvent("KeyboardEvent");
2198-
event.initKeyEvent('keyup', true, true, window,
2199-
0, 0, 0, 0,
2200-
c, c);
2201-
document.dispatchEvent(event);
2202-
}
2203-
''')
2204-
self.btest(path_from_root('tests', 'glfw3_events.c'), args=['--pre-js', 'pre.js', '-s', 'LEGACY_GL_EMULATION=1', '-s', 'USE_GLFW=3'], expected='1')
2187+
def test_glfw_events(self):
2188+
self.btest(path_from_root('tests', 'glfw_events.c'), args=['-s', 'USE_GLFW=2', "-DUSE_GLFW=2"], expected='1')
2189+
self.btest(path_from_root('tests', 'glfw_events.c'), args=['-s', 'USE_GLFW=3', "-DUSE_GLFW=3"], expected='1')
22052190

22062191
def test_asm_swapping(self):
22072192
self.clear()

0 commit comments

Comments
 (0)