aboutsummaryrefslogtreecommitdiff
path: root/tests/glut_touchevents.c
blob: 1f09789586996bc7ce329415f2d1899f7875b045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <EGL/egl.h>
#include <emscripten.h>

#define MULTILINE(...) #__VA_ARGS__

int touch_started = 0;
int touch_ended = 0;

int result = 0;

void mouseCB(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON)
    {
        if(state == GLUT_DOWN)
        {
            touch_started = 1;
        }
        else if(state == GLUT_UP)
        {
            touch_ended = 1;
        }
    }
}

int main(int argc, char *argv[])
{
    emscripten_run_script(MULTILINE(
        Module.injectEvent = function(eventType, x, y) {
            // Desktop browsers do not have the event types for touch events,
            // so we fake them by creating a plain-vanilla UIEvent and then
            // filling in the fields that we look for with appropriate values.
            var touch = {
                pageX: x,
                pageY: y
            };
            var touches = [ touch ];
            touches.item = function(i) { return this[i]; };

            var event = document.createEvent('UIEvent');
            event.target = Module['canvas'];
            event.button = 0;
            event.touches = touches;
            event.initUIEvent(eventType, true, true);
            Module['canvas'].dispatchEvent(event);
        }
    ));

    // Fake a touch device so that glut sets up the appropriate event handlers.
    emscripten_run_script("document.documentElement['ontouchstart'] = 1");
    glutInit(&argc, argv);

    glutMouseFunc(&mouseCB);

    emscripten_run_script("Module.injectEvent('touchend', 100, 100)");
    emscripten_run_script("Module.injectEvent('touchstart', 100, 100)");
    result = touch_started && touch_ended;

    REPORT_RESULT();
    return 0;
}