-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
library_html5.js
2512 lines (2241 loc) · 127 KB
/
library_html5.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2014 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
var LibraryHTML5 = {
$JSEvents__deps: [
#if PTHREADS
'_emscripten_run_callback_on_thread',
#endif
],
$JSEvents: {
/* We do not depend on the exact initial values of falsey member fields - these
fields can be populated on-demand to save code size.
(but still documented here to keep track of what is supposed to be present)
// pointers to structs malloc()ed to Emscripten HEAP for JS->C interop.
keyEvent: 0,
mouseEvent: 0,
wheelEvent: 0,
uiEvent: 0,
focusEvent: 0,
deviceOrientationEvent: 0,
deviceMotionEvent: 0,
fullscreenChangeEvent: 0,
pointerlockChangeEvent: 0,
visibilityChangeEvent: 0,
touchEvent: 0,
// When we transition from fullscreen to windowed mode, we remember here the
// element that was just in fullscreen mode so that we can report
// information about that element in the event message.
previousFullscreenElement: null,
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
// Remember the current mouse coordinates in case we need to emulate
// movementXY generation for browsers that don't support it.
// Some browsers (e.g. Safari 6.0.5) only give movementXY when Pointerlock is active.
previousScreenX: null,
previousScreenY: null,
#endif
// When the C runtime exits via exit(), we unregister all event handlers
// added by this library to be nice and clean.
// Track in this field whether we have yet registered that __ATEXIT__ handler.
removeEventListenersRegistered: false,
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
// If we are in an event handler, specifies the event handler object from
// the eventHandlers array that is currently running.
currentEventHandler: null,
#endif
*/
removeAllEventListeners() {
while (JSEvents.eventHandlers.length) {
JSEvents._removeHandler(JSEvents.eventHandlers.length - 1);
}
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
JSEvents.deferredCalls = [];
#endif
},
#if EXIT_RUNTIME
registerRemoveEventListeners() {
if (!JSEvents.removeEventListenersRegistered) {
__ATEXIT__.push(JSEvents.removeAllEventListeners);
JSEvents.removeEventListenersRegistered = true;
}
},
#endif
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
// If positive, we are currently executing in a JS event handler.
// (this particular property must be initialized to zero, as we ++/-- it)
inEventHandler: 0,
deferredCalls: [],
// Queues the given function call to occur the next time we enter an event handler.
// Existing implementations of pointerlock apis have required that
// the target element is active in fullscreen mode first. Therefore give
// fullscreen mode request a precedence of 1 and pointer lock a precedence of 2
// and sort by that to always request fullscreen before pointer lock.
deferCall(targetFunction, precedence, argsList) {
function arraysHaveEqualContent(arrA, arrB) {
if (arrA.length != arrB.length) return false;
for (var i in arrA) {
if (arrA[i] != arrB[i]) return false;
}
return true;
}
// Test if the given call was already queued, and if so, don't add it again.
for (var i in JSEvents.deferredCalls) {
var call = JSEvents.deferredCalls[i];
if (call.targetFunction == targetFunction && arraysHaveEqualContent(call.argsList, argsList)) {
return;
}
}
JSEvents.deferredCalls.push({
targetFunction,
precedence,
argsList
});
JSEvents.deferredCalls.sort((x,y) => x.precedence < y.precedence);
},
// Erases all deferred calls to the given target function from the queue list.
removeDeferredCalls(targetFunction) {
for (var i = 0; i < JSEvents.deferredCalls.length; ++i) {
if (JSEvents.deferredCalls[i].targetFunction == targetFunction) {
JSEvents.deferredCalls.splice(i, 1);
--i;
}
}
},
canPerformEventHandlerRequests() {
if (navigator.userActivation) {
// Verify against transient activation status from UserActivation API
// whether it is possible to perform a request here without needing to defer. See
// https://developer.mozilla.org/en-US/docs/Web/Security/User_activation#transient_activation
// and https://caniuse.com/mdn-api_useractivation
// At the time of writing, Firefox does not support this API: https://bugzilla.mozilla.org/show_bug.cgi?id=1791079
return navigator.userActivation.isActive;
}
return JSEvents.inEventHandler && JSEvents.currentEventHandler.allowsDeferredCalls;
},
runDeferredCalls() {
if (!JSEvents.canPerformEventHandlerRequests()) {
return;
}
for (var i = 0; i < JSEvents.deferredCalls.length; ++i) {
var call = JSEvents.deferredCalls[i];
JSEvents.deferredCalls.splice(i, 1);
--i;
call.targetFunction(...call.argsList);
}
},
#endif
// Stores objects representing each currently registered JS event handler.
eventHandlers: [],
// Removes all event handlers on the given DOM element of the given type.
// Pass in eventTypeString == undefined/null to remove all event handlers
// regardless of the type.
removeAllHandlersOnTarget: (target, eventTypeString) => {
for (var i = 0; i < JSEvents.eventHandlers.length; ++i) {
if (JSEvents.eventHandlers[i].target == target &&
(!eventTypeString || eventTypeString == JSEvents.eventHandlers[i].eventTypeString)) {
JSEvents._removeHandler(i--);
}
}
},
_removeHandler(i) {
var h = JSEvents.eventHandlers[i];
h.target.removeEventListener(h.eventTypeString, h.eventListenerFunc, h.useCapture);
JSEvents.eventHandlers.splice(i, 1);
},
registerOrRemoveHandler(eventHandler) {
if (!eventHandler.target) {
#if ASSERTIONS
err('registerOrRemoveHandler: the target element for event handler registration does not exist, when processing the following event handler registration:');
console.dir(eventHandler);
#endif
return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
}
if (eventHandler.callbackfunc) {
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
eventHandler.eventListenerFunc = function(event) {
// Increment nesting count for the event handler.
++JSEvents.inEventHandler;
JSEvents.currentEventHandler = eventHandler;
// Process any old deferred calls the user has placed.
JSEvents.runDeferredCalls();
// Process the actual event, calls back to user C code handler.
eventHandler.handlerFunc(event);
// Process any new deferred calls that were placed right now from this event handler.
JSEvents.runDeferredCalls();
// Out of event handler - restore nesting count.
--JSEvents.inEventHandler;
};
#else
eventHandler.eventListenerFunc = eventHandler.handlerFunc;
#endif
eventHandler.target.addEventListener(eventHandler.eventTypeString,
eventHandler.eventListenerFunc,
eventHandler.useCapture);
JSEvents.eventHandlers.push(eventHandler);
#if EXIT_RUNTIME
JSEvents.registerRemoveEventListeners();
#endif
} else {
for (var i = 0; i < JSEvents.eventHandlers.length; ++i) {
if (JSEvents.eventHandlers[i].target == eventHandler.target
&& JSEvents.eventHandlers[i].eventTypeString == eventHandler.eventTypeString) {
JSEvents._removeHandler(i--);
}
}
}
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},
#if PTHREADS
getTargetThreadForEventCallback(targetThread) {
switch (targetThread) {
case {{{ cDefs.EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD }}}:
// The event callback for the current event should be called on the
// main browser thread. (0 == don't proxy)
return 0;
case {{{ cDefs.EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD }}}:
// The event callback for the current event should be backproxied to
// the thread that is registering the event.
// This can be 0 in the case that the caller uses
// EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD but on the main thread
// itself.
return PThread.currentProxiedOperationCallerThread;
default:
// The event callback for the current event should be proxied to the
// given specific thread.
return targetThread;
}
},
#endif
getNodeNameForTarget(target) {
if (!target) return '';
if (target == window) return '#window';
if (target == screen) return '#screen';
return target?.nodeName || '';
},
fullscreenEnabled() {
return document.fullscreenEnabled
#if MIN_FIREFOX_VERSION <= 63
// Firefox 64 shipped unprefixed form of fullscreenEnabled (https://caniuse.com/#feat=mdn-api_document_fullscreenenabled)
|| document.mozFullScreenEnabled
#endif
#if MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED
// Safari 13.0.3 on macOS Catalina 10.15.1 still ships with prefixed webkitFullscreenEnabled.
// TODO: If Safari at some point ships with unprefixed version, update the version check above.
|| document.webkitFullscreenEnabled
#endif
;
},
},
$registerKeyEventCallback__deps: ['$JSEvents', '$findEventTarget', '$stringToUTF8', 'malloc'],
$registerKeyEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.keyEvent) JSEvents.keyEvent = _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}});
var keyEventHandlerFunc = (e) => {
#if ASSERTIONS
assert(e);
#endif
#if PTHREADS
var keyEventData = targetThread ? _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}}) : JSEvents.keyEvent; // This allocated block is passed as satellite data to the proxied function call, so the call frees up the data block when done.
#else
var keyEventData = JSEvents.keyEvent;
#endif
{{{ makeSetValue('keyEventData', C_STRUCTS.EmscriptenKeyboardEvent.timestamp, 'e.timeStamp', 'double') }}};
var idx ={{{ getHeapOffset('keyEventData', 'i32') }}};
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.location / 4}}}] = e.location;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.ctrlKey / 4}}}] = e.ctrlKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.shiftKey / 4}}}] = e.shiftKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.altKey / 4}}}] = e.altKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.metaKey / 4}}}] = e.metaKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.repeat / 4}}}] = e.repeat;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.charCode / 4}}}] = e.charCode;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.keyCode / 4}}}] = e.keyCode;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenKeyboardEvent.which / 4}}}] = e.which;
stringToUTF8(e.key || '', keyEventData + {{{ C_STRUCTS.EmscriptenKeyboardEvent.key }}}, {{{ cDefs.EM_HTML5_SHORT_STRING_LEN_BYTES }}});
stringToUTF8(e.code || '', keyEventData + {{{ C_STRUCTS.EmscriptenKeyboardEvent.code }}}, {{{ cDefs.EM_HTML5_SHORT_STRING_LEN_BYTES }}});
stringToUTF8(e.char || '', keyEventData + {{{ C_STRUCTS.EmscriptenKeyboardEvent.charValue }}}, {{{ cDefs.EM_HTML5_SHORT_STRING_LEN_BYTES }}});
stringToUTF8(e.locale || '', keyEventData + {{{ C_STRUCTS.EmscriptenKeyboardEvent.locale }}}, {{{ cDefs.EM_HTML5_SHORT_STRING_LEN_BYTES }}});
#if PTHREADS
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, keyEventData, userData);
else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, keyEventData, userData)) e.preventDefault();
};
var eventHandler = {
target: findEventTarget(target),
eventTypeString,
callbackfunc,
handlerFunc: keyEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
// In DOM capturing and bubbling sequence, there are two special elements at the top of the event chain that can be of interest
// to register many events to: document and window. These cannot be addressed by using document.querySelector(), so
// a special mechanism to address them is needed. (For any other special object, such as screen.orientation, no general access
// scheme should be needed, but the object-specific event callback registration functions should handle them individually).
//
// Users can also add more special event targets, basically by just doing something like
// specialHTMLTargets["!canvas"] = Module.canvas;
// (that will let !canvas map to the canvas held in Module.canvas).
$specialHTMLTargets__docs: '/** @type {Object} */',
#if ENVIRONMENT_MAY_BE_WORKER || ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL || PTHREADS
$specialHTMLTargets: "[0, typeof document != 'undefined' ? document : 0, typeof window != 'undefined' ? window : 0]",
#else
$specialHTMLTargets: "[0, document, window]",
#endif
#if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
$maybeCStringToJsString: (cString) => {
// "cString > 2" checks if the input is a number, and isn't of the special
// values we accept here, EMSCRIPTEN_EVENT_TARGET_* (which map to 0, 1, 2).
// In other words, if cString > 2 then it's a pointer to a valid place in
// memory, and points to a C string.
return cString > 2 ? UTF8ToString(cString) : cString;
},
$findEventTarget__deps: ['$maybeCStringToJsString', '$specialHTMLTargets'],
$findEventTarget: (target) => {
target = maybeCStringToJsString(target);
#if ENVIRONMENT_MAY_BE_WORKER || ENVIRONMENT_MAY_BE_NODE
var domElement = specialHTMLTargets[target] || (typeof document != 'undefined' ? document.querySelector(target) : undefined);
#else
var domElement = specialHTMLTargets[target] || document.querySelector(target);
#endif
return domElement;
},
#if OFFSCREENCANVAS_SUPPORT
$findCanvasEventTarget__deps: ['$GL', '$maybeCStringToJsString'],
$findCanvasEventTarget: (target) => {
target = maybeCStringToJsString(target);
// When compiling with OffscreenCanvas support and looking up a canvas to target,
// we first look up if the target Canvas has been transferred to OffscreenCanvas use.
// These transfers are represented/tracked by GL.offscreenCanvases object, which contain
// the OffscreenCanvas element for each regular Canvas element that has been transferred.
// Note that each pthread/worker have their own set of GL.offscreenCanvases. That is,
// when an OffscreenCanvas is transferred from a pthread/main thread to another pthread,
// it will move in the GL.offscreenCanvases array between threads. Hence GL.offscreenCanvases
// represents the set of OffscreenCanvases owned by the current calling thread.
// First check out the list of OffscreenCanvases by CSS selector ID ('#myCanvasID')
return GL.offscreenCanvases[target.substr(1)] // Remove '#' prefix
// If not found, if one is querying by using DOM tag name selector 'canvas', grab the first
// OffscreenCanvas that we can find.
|| (target == 'canvas' && Object.keys(GL.offscreenCanvases)[0])
// If that is not found either, query via the regular DOM selector.
#if PTHREADS
|| (typeof document != 'undefined' && document.querySelector(target));
#else
|| document.querySelector(target);
#endif
},
#else
$findCanvasEventTarget: '$findEventTarget',
#endif
#else
// Find a DOM element with the given ID.
$findEventTarget__deps: ['$specialHTMLTargets'],
$findEventTarget: (target) => {
#if ASSERTIONS
warnOnce('Rules for selecting event targets in HTML5 API are changing: instead of using document.getElementById() that only can refer to elements by their DOM ID, new event target selection mechanism uses the more flexible function document.querySelector() that can look up element names, classes, and complex CSS selectors. Build with -sDISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR to change to the new lookup rules. See https://github.com/emscripten-core/emscripten/pull/7977 for more details.');
#endif
try {
// The sensible "default" target varies between events, but use window as the default
// since DOM events mostly can default to that. Specific callback registrations
// override their own defaults.
if (!target) return window;
if (typeof target == "number") target = specialHTMLTargets[target] || UTF8ToString(target);
if (target === '#window') return window;
else if (target === '#document') return document;
else if (target === '#screen') return screen;
else if (target === '#canvas') return Module['canvas'];
return (typeof target == 'string') ? document.getElementById(target) : target;
} catch(e) {
// In Web Workers, some objects above, such as '#document' do not exist. Gracefully
// return null for them.
return null;
}
},
// Like findEventTarget, but looks for OffscreenCanvas elements first
$findCanvasEventTarget__deps: ['$findEventTarget'],
$findCanvasEventTarget: (target) => {
if (typeof target == 'number') target = UTF8ToString(target);
if (!target || target === '#canvas') {
if (typeof GL != 'undefined' && GL.offscreenCanvases['canvas']) return GL.offscreenCanvases['canvas']; // TODO: Remove this line, target '#canvas' should refer only to Module['canvas'], not to GL.offscreenCanvases['canvas'] - but need stricter tests to be able to remove this line.
return Module['canvas'];
}
if (typeof GL != 'undefined' && GL.offscreenCanvases[target]) return GL.offscreenCanvases[target];
return findEventTarget(target);
},
#endif
emscripten_set_keypress_callback_on_thread__proxy: 'sync',
emscripten_set_keypress_callback_on_thread__deps: ['$registerKeyEventCallback'],
emscripten_set_keypress_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerKeyEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_KEYPRESS }}}, "keypress", targetThread),
emscripten_set_keydown_callback_on_thread__proxy: 'sync',
emscripten_set_keydown_callback_on_thread__deps: ['$registerKeyEventCallback'],
emscripten_set_keydown_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerKeyEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_KEYDOWN }}}, "keydown", targetThread),
emscripten_set_keyup_callback_on_thread__proxy: 'sync',
emscripten_set_keyup_callback_on_thread__deps: ['$registerKeyEventCallback'],
emscripten_set_keyup_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerKeyEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_KEYUP }}}, "keyup", targetThread),
// Outline access to function .getBoundingClientRect() since it is a long string. Closure compiler does not outline access to it by itself, but it can inline access if
// there is only one caller to this function.
$getBoundingClientRect__deps: ['$specialHTMLTargets'],
$getBoundingClientRect: (e) => specialHTMLTargets.indexOf(e) < 0 ? e.getBoundingClientRect() : {'left':0,'top':0},
// Copies mouse event data from the given JS mouse event 'e' to the specified Emscripten mouse event structure in the HEAP.
// eventStruct: the structure to populate.
// e: The JS mouse event to read data from.
// target: Specifies a target DOM element that will be used as the reference to populate targetX and targetY parameters.
$fillMouseEventData__deps: ['$JSEvents', '$getBoundingClientRect', '$specialHTMLTargets'],
$fillMouseEventData: (eventStruct, e, target) => {
#if ASSERTIONS
assert(eventStruct % 4 == 0);
#endif
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenMouseEvent.timestamp, 'e.timeStamp', 'double') }}};
var idx = {{{ getHeapOffset('eventStruct', 'i32') }}};
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.screenX / 4 }}}] = e.screenX;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.screenY / 4 }}}] = e.screenY;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.clientX / 4 }}}] = e.clientX;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.clientY / 4 }}}] = e.clientY;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.ctrlKey / 4 }}}] = e.ctrlKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.shiftKey / 4 }}}] = e.shiftKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.altKey / 4 }}}] = e.altKey;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.metaKey / 4 }}}] = e.metaKey;
HEAP16[idx*2 + {{{ C_STRUCTS.EmscriptenMouseEvent.button / 2 }}}] = e.button;
HEAP16[idx*2 + {{{ C_STRUCTS.EmscriptenMouseEvent.buttons / 2 }}}] = e.buttons;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.movementX / 4 }}}] = e["movementX"]
#if MIN_FIREFOX_VERSION <= 40
// https://caniuse.com/#feat=mdn-api_mouseevent_movementx
|| e["mozMovementX"]
#endif
#if MIN_CHROME_VERSION <= 36 // || MIN_ANDROID_BROWSER_VERSION <= 4.4.4
|| e["webkitMovementX"]
#endif
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
|| (e.screenX-JSEvents.previousScreenX)
#endif
;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.movementY / 4 }}}] = e["movementY"]
#if MIN_FIREFOX_VERSION <= 40
|| e["mozMovementY"]
#endif
#if MIN_CHROME_VERSION <= 36 // || MIN_ANDROID_BROWSER_VERSION <= 4.4.4
|| e["webkitMovementY"]
#endif
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
|| (e.screenY-JSEvents.previousScreenY)
#endif
;
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
if (Module['canvas']) {
var rect = getBoundingClientRect(Module['canvas']);
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasX / 4 }}}] = e.clientX - (rect.left | 0);
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasY / 4 }}}] = e.clientY - (rect.top | 0);
} else { // Canvas is not initialized, return 0.
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasX / 4 }}}] = 0;
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasY / 4 }}}] = 0;
}
#endif
// Note: rect contains doubles (truncated to placate SAFE_HEAP, which is the same behaviour when writing to HEAP32 anyway)
var rect = getBoundingClientRect(target);
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetX / 4 }}}] = e.clientX - (rect.left | 0);
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetY / 4 }}}] = e.clientY - (rect.top | 0);
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
#if MIN_CHROME_VERSION <= 76
// wheel and mousewheel events contain wrong screenX/screenY on chrome/opera <= 76,
// so there we should not record previous screen coordinates on wheel events.
// https://bugs.chromium.org/p/chromium/issues/detail?id=699956
// https://github.com/emscripten-core/emscripten/pull/4997
if (e.type !== 'wheel') {
#endif
JSEvents.previousScreenX = e.screenX;
JSEvents.previousScreenY = e.screenY;
#if MIN_CHROME_VERSION <= 76
}
#endif
#endif
},
$registerMouseEventCallback__deps: ['$JSEvents', '$fillMouseEventData', '$findEventTarget', 'malloc'],
$registerMouseEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.mouseEvent) JSEvents.mouseEvent = _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}});
target = findEventTarget(target);
var mouseEventHandlerFunc = (e = event) => {
// TODO: Make this access thread safe, or this could update live while app is reading it.
fillMouseEventData(JSEvents.mouseEvent, e, target);
#if PTHREADS
if (targetThread) {
var mouseEventData = _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}}); // This allocated block is passed as satellite data to the proxied function call, so the call frees up the data block when done.
fillMouseEventData(mouseEventData, e, target);
__emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, mouseEventData, userData);
} else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, JSEvents.mouseEvent, userData)) e.preventDefault();
};
var eventHandler = {
target,
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
allowsDeferredCalls: eventTypeString != 'mousemove' && eventTypeString != 'mouseenter' && eventTypeString != 'mouseleave', // Mouse move events do not allow fullscreen/pointer lock requests to be handled in them!
#endif
eventTypeString,
callbackfunc,
handlerFunc: mouseEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_click_callback_on_thread__proxy: 'sync',
emscripten_set_click_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_click_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_CLICK }}}, "click", targetThread),
emscripten_set_mousedown_callback_on_thread__proxy: 'sync',
emscripten_set_mousedown_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mousedown_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEDOWN }}}, "mousedown", targetThread),
emscripten_set_mouseup_callback_on_thread__proxy: 'sync',
emscripten_set_mouseup_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mouseup_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEUP }}}, "mouseup", targetThread),
emscripten_set_dblclick_callback_on_thread__proxy: 'sync',
emscripten_set_dblclick_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_dblclick_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_DBLCLICK }}}, "dblclick", targetThread),
emscripten_set_mousemove_callback_on_thread__proxy: 'sync',
emscripten_set_mousemove_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mousemove_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEMOVE }}}, "mousemove", targetThread),
emscripten_set_mouseenter_callback_on_thread__proxy: 'sync',
emscripten_set_mouseenter_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mouseenter_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEENTER }}}, "mouseenter", targetThread),
emscripten_set_mouseleave_callback_on_thread__proxy: 'sync',
emscripten_set_mouseleave_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mouseleave_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSELEAVE }}}, "mouseleave", targetThread),
emscripten_set_mouseover_callback_on_thread__proxy: 'sync',
emscripten_set_mouseover_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mouseover_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEOVER }}}, "mouseover", targetThread),
emscripten_set_mouseout_callback_on_thread__proxy: 'sync',
emscripten_set_mouseout_callback_on_thread__deps: ['$registerMouseEventCallback'],
emscripten_set_mouseout_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerMouseEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_MOUSEOUT }}}, "mouseout", targetThread),
emscripten_get_mouse_status__proxy: 'sync',
emscripten_get_mouse_status__deps: ['$JSEvents'],
emscripten_get_mouse_status: (mouseState) => {
if (!JSEvents.mouseEvent) return {{{ cDefs.EMSCRIPTEN_RESULT_NO_DATA }}};
// HTML5 does not really have a polling API for mouse events, so implement one manually by
// returning the data from the most recently received event. This requires that user has registered
// at least some no-op function as an event handler to any of the mouse function.
HEAP8.set(HEAP8.subarray(JSEvents.mouseEvent, JSEvents.mouseEvent + {{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}}), mouseState);
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},
$registerWheelEventCallback__deps: ['$JSEvents', '$fillMouseEventData', '$findEventTarget', 'malloc'],
$registerWheelEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.wheelEvent) JSEvents.wheelEvent = _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}});
// The DOM Level 3 events spec event 'wheel'
var wheelHandlerFunc = (e = event) => {
#if PTHREADS
var wheelEvent = targetThread ? _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}}) : JSEvents.wheelEvent; // This allocated block is passed as satellite data to the proxied function call, so the call frees up the data block when done.
#else
var wheelEvent = JSEvents.wheelEvent;
#endif
fillMouseEventData(wheelEvent, e, target);
{{{ makeSetValue('wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaX, 'e["deltaX"]', 'double') }}};
{{{ makeSetValue('wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaY, 'e["deltaY"]', 'double') }}};
{{{ makeSetValue('wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaZ, 'e["deltaZ"]', 'double') }}};
{{{ makeSetValue('wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaMode, 'e["deltaMode"]', 'i32') }}};
#if PTHREADS
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, wheelEvent, userData);
else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, wheelEvent, userData)) e.preventDefault();
};
#if MIN_SAFARI_VERSION < 60100 // Browsers that do not support https://caniuse.com/#feat=mdn-api_wheelevent
// The 'mousewheel' event as implemented in Safari 6.0.5
var mouseWheelHandlerFunc = (e = event) => {
fillMouseEventData(JSEvents.wheelEvent, e, target);
{{{ makeSetValue('JSEvents.wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaX, 'e["wheelDeltaX"] || 0', 'double') }}};
/* 1. Invert to unify direction with the DOM Level 3 wheel event. 2. MSIE does not provide wheelDeltaY, so wheelDelta is used as a fallback. */
var wheelDeltaY = -(e["wheelDeltaY"] || e["wheelDelta"])
{{{ makeSetValue('JSEvents.wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaY, 'wheelDeltaY', 'double') }}};
{{{ makeSetValue('JSEvents.wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaZ, '0 /* Not available */', 'double') }}};
{{{ makeSetValue('JSEvents.wheelEvent', C_STRUCTS.EmscriptenWheelEvent.deltaMode, '0 /* DOM_DELTA_PIXEL */', 'i32') }}};
var shouldCancel = {{{ makeDynCall('iipp', 'callbackfunc') }}}( eventTypeId, JSEvents.wheelEvent, userData);
if (shouldCancel) {
e.preventDefault();
}
};
#endif
var eventHandler = {
target,
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
allowsDeferredCalls: true,
#endif
eventTypeString,
callbackfunc,
#if MIN_SAFARI_VERSION < 60100 // Browsers that do not support https://caniuse.com/#feat=mdn-api_wheelevent
handlerFunc: (eventTypeString == 'wheel') ? wheelHandlerFunc : mouseWheelHandlerFunc,
#else
handlerFunc: wheelHandlerFunc,
#endif
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_wheel_callback_on_thread__proxy: 'sync',
emscripten_set_wheel_callback_on_thread__deps: ['$JSEvents', '$registerWheelEventCallback', '$findEventTarget'],
emscripten_set_wheel_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
target = findEventTarget(target);
if (!target) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
if (typeof target.onwheel != 'undefined') {
return registerWheelEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_WHEEL }}}, "wheel", targetThread);
#if MIN_SAFARI_VERSION < 60100 // Browsers that do not support https://caniuse.com/#feat=mdn-api_wheelevent
} else if (typeof target.onmousewheel != 'undefined') {
return registerWheelEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_WHEEL }}}, "mousewheel", targetThread);
#endif
} else {
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
}
},
$registerUiEventCallback__deps: ['$JSEvents', '$findEventTarget', 'malloc'],
$registerUiEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.uiEvent) JSEvents.uiEvent = _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}});
#if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
target = findEventTarget(target);
#else
if (eventTypeId == {{{ cDefs.EMSCRIPTEN_EVENT_SCROLL }}} && !target) {
target = document; // By default read scroll events on document rather than window.
} else {
target = findEventTarget(target);
}
#else
#endif
var uiEventHandlerFunc = (e = event) => {
if (e.target != target) {
// Never take ui events such as scroll via a 'bubbled' route, but always from the direct element that
// was targeted. Otherwise e.g. if app logs a message in response to a page scroll, the Emscripten log
// message box could cause to scroll, generating a new (bubbled) scroll message, causing a new log print,
// causing a new scroll, etc..
return;
}
var b = document.body; // Take document.body to a variable, Closure compiler does not outline access to it on its own.
if (!b) {
// During a page unload 'body' can be null, with "Cannot read property 'clientWidth' of null" being thrown
return;
}
#if PTHREADS
var uiEvent = targetThread ? _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}}) : JSEvents.uiEvent;
#else
var uiEvent = JSEvents.uiEvent;
#endif
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.detail, '0', 'i32') }}}; // always zero for resize and scroll
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.documentBodyClientWidth, 'b.clientWidth', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.documentBodyClientHeight, 'b.clientHeight', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.windowInnerWidth, 'innerWidth', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.windowInnerHeight, 'innerHeight', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.windowOuterWidth, 'outerWidth', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.windowOuterHeight, 'outerHeight', 'i32') }}};
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.scrollTop, 'pageXOffset | 0', 'i32') }}}; // scroll offsets are float
{{{ makeSetValue('uiEvent', C_STRUCTS.EmscriptenUiEvent.scrollLeft, 'pageYOffset | 0', 'i32') }}};
#if PTHREADS
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, uiEvent, userData);
else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, uiEvent, userData)) e.preventDefault();
};
var eventHandler = {
target,
eventTypeString,
callbackfunc,
handlerFunc: uiEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_resize_callback_on_thread__proxy: 'sync',
emscripten_set_resize_callback_on_thread__deps: ['$registerUiEventCallback'],
emscripten_set_resize_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerUiEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_RESIZE }}}, "resize", targetThread),
emscripten_set_scroll_callback_on_thread__proxy: 'sync',
emscripten_set_scroll_callback_on_thread__deps: ['$registerUiEventCallback'],
emscripten_set_scroll_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerUiEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_SCROLL }}}, "scroll", targetThread),
$registerFocusEventCallback__deps: ['$JSEvents', '$findEventTarget', 'malloc', '$stringToUTF8'],
$registerFocusEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.focusEvent) JSEvents.focusEvent = _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}});
var focusEventHandlerFunc = (e = event) => {
var nodeName = JSEvents.getNodeNameForTarget(e.target);
var id = e.target.id ? e.target.id : '';
#if PTHREADS
var focusEvent = targetThread ? _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}}) : JSEvents.focusEvent;
#else
var focusEvent = JSEvents.focusEvent;
#endif
stringToUTF8(nodeName, focusEvent + {{{ C_STRUCTS.EmscriptenFocusEvent.nodeName }}}, {{{ cDefs.EM_HTML5_LONG_STRING_LEN_BYTES }}});
stringToUTF8(id, focusEvent + {{{ C_STRUCTS.EmscriptenFocusEvent.id }}}, {{{ cDefs.EM_HTML5_LONG_STRING_LEN_BYTES }}});
#if PTHREADS
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, focusEvent, userData);
else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, focusEvent, userData)) e.preventDefault();
};
var eventHandler = {
target: findEventTarget(target),
eventTypeString,
callbackfunc,
handlerFunc: focusEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_blur_callback_on_thread__proxy: 'sync',
emscripten_set_blur_callback_on_thread__deps: ['$registerFocusEventCallback'],
emscripten_set_blur_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_BLUR }}}, "blur", targetThread),
emscripten_set_focus_callback_on_thread__proxy: 'sync',
emscripten_set_focus_callback_on_thread__deps: ['$registerFocusEventCallback'],
emscripten_set_focus_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUS }}}, "focus", targetThread),
emscripten_set_focusin_callback_on_thread__proxy: 'sync',
emscripten_set_focusin_callback_on_thread__deps: ['$registerFocusEventCallback'],
emscripten_set_focusin_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSIN }}}, "focusin", targetThread),
emscripten_set_focusout_callback_on_thread__proxy: 'sync',
emscripten_set_focusout_callback_on_thread__deps: ['$registerFocusEventCallback'],
emscripten_set_focusout_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSOUT }}}, "focusout", targetThread),
$fillDeviceOrientationEventData__deps: ['$JSEvents'],
$fillDeviceOrientationEventData: (eventStruct, e, target) => {
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceOrientationEvent.alpha, 'e.alpha', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceOrientationEvent.beta, 'e.beta', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceOrientationEvent.gamma, 'e.gamma', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceOrientationEvent.absolute, 'e.absolute', 'i32') }}};
},
$registerDeviceOrientationEventCallback__deps: ['$JSEvents', '$fillDeviceOrientationEventData', '$findEventTarget'],
$registerDeviceOrientationEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.deviceOrientationEvent) JSEvents.deviceOrientationEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});
var deviceOrientationEventHandlerFunc = (e = event) => {
fillDeviceOrientationEventData(JSEvents.deviceOrientationEvent, e, target); // TODO: Thread-safety with respect to emscripten_get_deviceorientation_status()
#if PTHREADS
if (targetThread) {
var deviceOrientationEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});
fillDeviceOrientationEventData(deviceOrientationEvent, e, target);
__emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, deviceOrientationEvent, userData);
} else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, JSEvents.deviceOrientationEvent, userData)) e.preventDefault();
};
var eventHandler = {
target: findEventTarget(target),
eventTypeString,
callbackfunc,
handlerFunc: deviceOrientationEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_deviceorientation_callback_on_thread__proxy: 'sync',
emscripten_set_deviceorientation_callback_on_thread__deps: ['$registerDeviceOrientationEventCallback'],
emscripten_set_deviceorientation_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
return registerDeviceOrientationEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_DEVICEORIENTATION }}}, "deviceorientation", targetThread);
},
emscripten_get_deviceorientation_status__proxy: 'sync',
emscripten_get_deviceorientation_status__deps: ['$JSEvents', '$registerDeviceOrientationEventCallback'],
emscripten_get_deviceorientation_status: (orientationState) => {
if (!JSEvents.deviceOrientationEvent) return {{{ cDefs.EMSCRIPTEN_RESULT_NO_DATA }}};
// HTML5 does not really have a polling API for device orientation events, so implement one manually by
// returning the data from the most recently received event. This requires that user has registered
// at least some no-op function as an event handler.
HEAP32.set(HEAP32.subarray(JSEvents.deviceOrientationEvent, {{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}}), orientationState);
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},
$fillDeviceMotionEventData__deps: ['$JSEvents'],
$fillDeviceMotionEventData: (eventStruct, e, target) => {
var supportedFields = 0;
var a = e['acceleration'];
supportedFields |= a && {{{ cDefs.EMSCRIPTEN_DEVICE_MOTION_EVENT_SUPPORTS_ACCELERATION }}};
var ag = e['accelerationIncludingGravity'];
supportedFields |= ag && {{{ cDefs.EMSCRIPTEN_DEVICE_MOTION_EVENT_SUPPORTS_ACCELERATION_INCLUDING_GRAVITY }}};
var rr = e['rotationRate'];
supportedFields |= rr && {{{ cDefs.EMSCRIPTEN_DEVICE_MOTION_EVENT_SUPPORTS_ROTATION_RATE }}};
a = a || {};
ag = ag || {};
rr = rr || {};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationX, 'a["x"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationY, 'a["y"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationZ, 'a["z"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationIncludingGravityX, 'ag["x"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationIncludingGravityY, 'ag["y"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.accelerationIncludingGravityZ, 'ag["z"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.rotationRateAlpha, 'rr["alpha"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.rotationRateBeta, 'rr["beta"]', 'double') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenDeviceMotionEvent.rotationRateGamma, 'rr["gamma"]', 'double') }}};
},
$registerDeviceMotionEventCallback__deps: ['$JSEvents', '$fillDeviceMotionEventData', '$findEventTarget', 'malloc'],
$registerDeviceMotionEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.deviceMotionEvent) JSEvents.deviceMotionEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceMotionEvent.__size__ }}});
var deviceMotionEventHandlerFunc = (e = event) => {
fillDeviceMotionEventData(JSEvents.deviceMotionEvent, e, target); // TODO: Thread-safety with respect to emscripten_get_devicemotion_status()
#if PTHREADS
if (targetThread) {
var deviceMotionEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceMotionEvent.__size__ }}});
fillDeviceMotionEventData(deviceMotionEvent, e, target);
__emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, deviceMotionEvent, userData);
} else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, JSEvents.deviceMotionEvent, userData)) e.preventDefault();
};
var eventHandler = {
target: findEventTarget(target),
eventTypeString,
callbackfunc,
handlerFunc: deviceMotionEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_devicemotion_callback_on_thread__proxy: 'sync',
emscripten_set_devicemotion_callback_on_thread__deps: ['$registerDeviceMotionEventCallback'],
emscripten_set_devicemotion_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
return registerDeviceMotionEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_DEVICEMOTION }}}, "devicemotion", targetThread);
},
emscripten_get_devicemotion_status__proxy: 'sync',
emscripten_get_devicemotion_status__deps: ['$JSEvents'],
emscripten_get_devicemotion_status: (motionState) => {
if (!JSEvents.deviceMotionEvent) return {{{ cDefs.EMSCRIPTEN_RESULT_NO_DATA }}};
// HTML5 does not really have a polling API for device motion events, so implement one manually by
// returning the data from the most recently received event. This requires that user has registered
// at least some no-op function as an event handler.
HEAP32.set(HEAP32.subarray(JSEvents.deviceMotionEvent, {{{ C_STRUCTS.EmscriptenDeviceMotionEvent.__size__ }}}), motionState);
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},
$screenOrientation: () => {
if (!window.screen) return undefined;
return screen.orientation || screen['mozOrientation'] || screen['webkitOrientation'];
},
$fillOrientationChangeEventData__deps: ['$screenOrientation'],
$fillOrientationChangeEventData: (eventStruct) => {
// OrientationType enum
var orientationsType1 = ['portrait-primary', 'portrait-secondary', 'landscape-primary', 'landscape-secondary'];
// alternative selection from OrientationLockType enum
var orientationsType2 = ['portrait', 'portrait', 'landscape', 'landscape'];
var orientationIndex = {{{ cDefs.EMSCRIPTEN_ORIENTATION_UNSUPPORTED }}};
var orientationAngle = 0;
var screenOrientObj = screenOrientation();
if (typeof screenOrientObj === 'object') {
orientationIndex = orientationsType1.indexOf(screenOrientObj.type);
if (orientationIndex < 0) {
orientationIndex = orientationsType2.indexOf(screenOrientObj.type);
}
if (orientationIndex >= 0) {
orientationIndex = 1 << orientationIndex;
}
orientationAngle = screenOrientObj.angle;
}
#if MIN_SAFARI_VERSION < 0x100400
else {
// fallback for Safari earlier than 16.4 (March 2023)
orientationAngle = window.orientation;
}
#endif
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationIndex, 'orientationIndex', 'i32') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationAngle, 'orientationAngle', 'i32') }}};
},
$registerOrientationChangeEventCallback__deps: ['$JSEvents', '$fillOrientationChangeEventData', '$findEventTarget', 'malloc'],
$registerOrientationChangeEventCallback: (target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) => {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.orientationChangeEvent) JSEvents.orientationChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenOrientationChangeEvent.__size__ }}});
var orientationChangeEventHandlerFunc = (e = event) => {
#if PTHREADS
var orientationChangeEvent = targetThread ? _malloc({{{ C_STRUCTS.EmscriptenOrientationChangeEvent.__size__ }}}) : JSEvents.orientationChangeEvent;
#else
var orientationChangeEvent = JSEvents.orientationChangeEvent;
#endif
fillOrientationChangeEventData(orientationChangeEvent);
#if PTHREADS
if (targetThread) __emscripten_run_callback_on_thread(targetThread, callbackfunc, eventTypeId, orientationChangeEvent, userData);
else
#endif
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, orientationChangeEvent, userData)) e.preventDefault();
};
var eventHandler = {
target,
eventTypeString,
callbackfunc,
handlerFunc: orientationChangeEventHandlerFunc,
useCapture
};
return JSEvents.registerOrRemoveHandler(eventHandler);
},
emscripten_set_orientationchange_callback_on_thread__proxy: 'sync',