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
0 commit comments