Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/glut_touchevents.c
7085 views
1
/*
2
* Copyright 2013 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
#include <assert.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <GL/glut.h>
12
#include <EGL/egl.h>
13
#include <emscripten.h>
14
15
#define MULTILINE(...) #__VA_ARGS__
16
17
int touch_started_button = -1;
18
int touch_started_x = -1;
19
int touch_started_y = -1;
20
21
int touch_ended_button = -1;
22
int touch_ended_x = -1;
23
int touch_ended_y = -1;
24
25
void mouseCB(int button, int state, int x, int y)
26
{
27
if (button == GLUT_LEFT_BUTTON) {
28
if(state == GLUT_DOWN) {
29
printf("GLUT_DOWN: button: %d, x: %d, y: %d\n", button, x, y);
30
touch_started_button = button;
31
touch_started_x = x;
32
touch_started_y = y;
33
}
34
else if(state == GLUT_UP) {
35
printf("GLUT_UP: button: %d, x: %d, y: %d\n", button, x, y);
36
touch_ended_button = button;
37
touch_ended_x = x;
38
touch_ended_y = y;
39
}
40
}
41
}
42
43
#define abs(x) ((x) < 0 ? -(x) : (x))
44
45
int main(int argc, char *argv[])
46
{
47
emscripten_run_script(MULTILINE(
48
Module.injectEvent = function(eventType, wantedX, wantedY) {
49
// Desktop browsers do not have the event types for touch events,
50
// so we fake them by creating a plain-vanilla UIEvent and then
51
// filling in the fields that we look for with appropriate values.
52
var rect = Module["canvas"].getBoundingClientRect();
53
out('rect corner: ' + rect.left + ',' + rect.top);
54
out('wanted: ' + wantedX + ',' + wantedY);
55
var x = wantedX + rect.left;
56
var y = wantedY + rect.top;
57
var touch = {
58
identifier: 0,
59
clientX: x,
60
clientY: y,
61
screenX: x,
62
screenY: y,
63
pageX: x,
64
pageY: y,
65
target: Module['canvas']
66
};
67
var touches = [ touch ];
68
touches.item = function(i) { return this[i]; };
69
70
var event = document.createEvent('UIEvent');
71
event.button = 0;
72
event.changedTouches = touches;
73
event.initUIEvent(eventType, true, true, window, 1);
74
Module['canvas'].dispatchEvent(event);
75
}
76
));
77
78
// Fake a touch device so that glut sets up the appropriate event handlers.
79
emscripten_run_script("document.documentElement['ontouchstart'] = 1");
80
glutInit(&argc, argv);
81
82
glutMouseFunc(&mouseCB);
83
84
emscripten_run_script("Module.injectEvent('touchstart', 101, 112)");
85
emscripten_run_script("Module.injectEvent('touchend', 201, 212)");
86
printf("touchstarted: button:%d x:%d y:%d\n", touch_started_button, touch_started_x, touch_started_y);
87
printf("touchended: button:%d x:%d y:%d\n", touch_ended_button, touch_ended_x, touch_ended_y);
88
assert(touch_started_button == 0);
89
assert(abs(touch_started_x - 101) <= 1);
90
assert(abs(touch_started_y - 112) <= 1);
91
assert(touch_ended_button == 0);
92
assert(abs(touch_ended_x - 201) <= 1);
93
assert(abs(touch_ended_y - 212) <= 1);
94
return 0;
95
}
96
97