Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/async_iostream.cpp
4150 views
1
// Copyright 2015 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdio.h>
7
#include <iostream>
8
#include <emscripten.h>
9
#include <emscripten/html5.h>
10
11
using namespace std;
12
13
int seen = 0;
14
15
bool mouse_callback(int eventType, const EmscriptenMouseEvent *e, void* userData) {
16
cout << "mouse_callback+" << endl;
17
seen++;
18
return 0;
19
}
20
21
void mainLoop() {
22
emscripten_sleep(100);
23
static int counter = 0;
24
counter++;
25
EM_ASM(
26
function sendEvent(type, data) {
27
setTimeout(function() {
28
var event = document.createEvent('Event');
29
event.initEvent(type, true, true);
30
for(var d in data) event[d] = data[d];
31
Module['canvas'].dispatchEvent(event);
32
}, Math.random()*100);
33
}
34
sendEvent('mousedown', { screenX: 1, screenY: 1, clientX: 1, clientY: 1, button: 0, buttons: 1 });
35
);
36
cout << "sent event " << counter << "\n";
37
if (seen >= 10) {
38
emscripten_cancel_main_loop();
39
cout << "Success.\n";
40
REPORT_RESULT(1);
41
return;
42
}
43
if (counter >= 100) {
44
emscripten_cancel_main_loop();
45
cout << "FAIL\n";
46
REPORT_RESULT(9999);
47
return;
48
}
49
}
50
51
int main() {
52
cout << "HelloWorld" << endl;
53
emscripten_set_mousedown_callback("#canvas", 0, 1, mouse_callback);
54
emscripten_set_main_loop(mainLoop, 0, 0);
55
return 0;
56
}
57
58
59