File: glut_wheelevents.c

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (73 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
/*
 * Copyright 2013 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <EGL/egl.h>
#include <emscripten.h>

#define MULTILINE(...) #__VA_ARGS__

int wheel_up = 0;
int wheel_down = 0;

void mouseCB(int button, int state, int x, int y)
{
    if(button == 3)
    {
      wheel_up = 1;   
    }
    else if (button == 4)
    {
      wheel_down = 1;
    }
}

int main(int argc, char *argv[])
{
  emscripten_run_script(MULTILINE(
      Module.injectWheelEvent = function(x, y, delta) {
          var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
          var event = document.createEvent("MouseEvents");
          if (!isFirefox) {
          // mouse wheel event for IE9, Chrome, Safari, Opera
          event.initMouseEvent('mousewheel', true, true, window,
                               0, Module['canvas'].offsetLeft + x, Module['canvas'].offsetTop + y, Module['canvas'].offsetLeft + x, Module['canvas'].offsetTop + y,
                               0, 0, 0, 0, 0, null);
                               event.wheelDelta = delta;
          } else {
            // mouse wheel event for Firefox, the delta sign is inversed for that browser and is stored in the detail property of the mouse event
            event.initMouseEvent('DOMMouseScroll', true, true, window,
                                 -delta, Module['canvas'].offsetLeft + x, Module['canvas'].offsetTop + y, Module['canvas'].offsetLeft + x, Module['canvas'].offsetTop + y,
                                 0, 0, 0, 0, 0, null);
          }
          Module['canvas'].dispatchEvent(event);
      }
  ));


  glutInit(&argc, argv);

  glutMouseFunc(&mouseCB);

  // inject wheel up event (delta > 0)
  emscripten_run_script("Module.injectWheelEvent(100, 100, 1)");
  if (wheel_up) {
    printf("%s\n", "mouse wheel up event received");
  }
  // inject wheel down event (delta < 0)
  emscripten_run_script("Module.injectWheelEvent(100, 100, -1)");
  if (wheel_down) {
    printf("%s\n", "mouse wheel down event received");
  }

  assert(wheel_up && wheel_down);

  return 0;
}