File: html5_callbacks_on_calling_thread.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (75 lines) | stat: -rw-r--r-- 2,089 bytes parent folder | download | duplicates (2)
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
74
75
#include <stdio.h>
#include <emscripten.h>
#include <string.h>
#include <emscripten/html5.h>
#include <emscripten/threading.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>

// #define TEST_SYNC_BLOCKING_LOOP

void *mainRuntimeThreadId = 0;
void *registeringThreadId = 0;

bool mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData) {
  static int once;

  void *threadId = pthread_self();
  if (!once) {
    printf("pthread_self()=%p, registeringThreadId=%p, mainRuntimeThreadId=%p, "
           "emscripten_main_runtime_thread_id()=%p\n",
           threadId,
           registeringThreadId,
           mainRuntimeThreadId,
           emscripten_main_runtime_thread_id());
  }
  printf("eventType: %d, mouseEvent: %p, userData: %p, screen: (%ld,%ld), client: (%ld,%ld),%s%s%s%s button: %hu, buttons: %hu, movement: (%ld,%ld), canvas: (%ld,%ld)\n",
    eventType, e, userData,
    e->screenX, e->screenY, e->clientX, e->clientY,
    e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : "", 
    e->button, e->buttons, e->movementX, e->movementY, e->canvasX, e->canvasY);
  assert(threadId && threadId == registeringThreadId);
  assert(userData == (void*)0x42);
  assert(e);
  assert(eventType == EMSCRIPTEN_EVENT_MOUSEMOVE);

  if (once) {
    return 0;
  }
  once = 1;

#ifdef REPORT_RESULT
  REPORT_RESULT(1);
#endif
  return 0;
}

void *threadMain(void *arg) {
  registeringThreadId = pthread_self();

  EMSCRIPTEN_RESULT ret = emscripten_set_mousemove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, (void*)0x42, 1, mouse_callback);
  assert(ret == EMSCRIPTEN_RESULT_SUCCESS);

  printf("Please move the mouse cursor.\n");

#ifdef TEST_SYNC_BLOCKING_LOOP
  for (;;) {
    usleep(1000);
    emscripten_current_thread_process_queued_calls();
  }
#else
  emscripten_exit_with_live_runtime();
#endif
  return 0;
}

int main() {
  mainRuntimeThreadId = pthread_self();

  pthread_t thread;
  int rc = pthread_create(&thread, NULL, threadMain, 0);
  assert(rc == 0);

  emscripten_exit_with_live_runtime();
}