Path: blob/main/test/browser/glut_touchevents.c
7085 views
/*1* Copyright 2013 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <assert.h>8#include <stdio.h>9#include <stdlib.h>10#include <GL/glut.h>11#include <EGL/egl.h>12#include <emscripten.h>1314#define MULTILINE(...) #__VA_ARGS__1516int touch_started_button = -1;17int touch_started_x = -1;18int touch_started_y = -1;1920int touch_ended_button = -1;21int touch_ended_x = -1;22int touch_ended_y = -1;2324void mouseCB(int button, int state, int x, int y)25{26if (button == GLUT_LEFT_BUTTON) {27if(state == GLUT_DOWN) {28printf("GLUT_DOWN: button: %d, x: %d, y: %d\n", button, x, y);29touch_started_button = button;30touch_started_x = x;31touch_started_y = y;32}33else if(state == GLUT_UP) {34printf("GLUT_UP: button: %d, x: %d, y: %d\n", button, x, y);35touch_ended_button = button;36touch_ended_x = x;37touch_ended_y = y;38}39}40}4142#define abs(x) ((x) < 0 ? -(x) : (x))4344int main(int argc, char *argv[])45{46emscripten_run_script(MULTILINE(47Module.injectEvent = function(eventType, wantedX, wantedY) {48// Desktop browsers do not have the event types for touch events,49// so we fake them by creating a plain-vanilla UIEvent and then50// filling in the fields that we look for with appropriate values.51var rect = Module["canvas"].getBoundingClientRect();52out('rect corner: ' + rect.left + ',' + rect.top);53out('wanted: ' + wantedX + ',' + wantedY);54var x = wantedX + rect.left;55var y = wantedY + rect.top;56var touch = {57identifier: 0,58clientX: x,59clientY: y,60screenX: x,61screenY: y,62pageX: x,63pageY: y,64target: Module['canvas']65};66var touches = [ touch ];67touches.item = function(i) { return this[i]; };6869var event = document.createEvent('UIEvent');70event.button = 0;71event.changedTouches = touches;72event.initUIEvent(eventType, true, true, window, 1);73Module['canvas'].dispatchEvent(event);74}75));7677// Fake a touch device so that glut sets up the appropriate event handlers.78emscripten_run_script("document.documentElement['ontouchstart'] = 1");79glutInit(&argc, argv);8081glutMouseFunc(&mouseCB);8283emscripten_run_script("Module.injectEvent('touchstart', 101, 112)");84emscripten_run_script("Module.injectEvent('touchend', 201, 212)");85printf("touchstarted: button:%d x:%d y:%d\n", touch_started_button, touch_started_x, touch_started_y);86printf("touchended: button:%d x:%d y:%d\n", touch_ended_button, touch_ended_x, touch_ended_y);87assert(touch_started_button == 0);88assert(abs(touch_started_x - 101) <= 1);89assert(abs(touch_started_y - 112) <= 1);90assert(touch_ended_button == 0);91assert(abs(touch_ended_x - 201) <= 1);92assert(abs(touch_ended_y - 212) <= 1);93return 0;94}959697