Skip to content

Commit 71cee0e

Browse files
authored
Multiple fixes to EmscriptenOrientationChangeEvent (#21428)
1 parent 086c84a commit 71cee0e

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

site/source/docs/api_reference/html5.h.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,10 @@ Defines
927927
Emscripten `orientationchange <https://w3c.github.io/screen-orientation/>`_ event.
928928
929929
930+
.. c:macro:: EMSCRIPTEN_ORIENTATION_UNKNOWN
931+
932+
Either the orientation API is not supported or the orientation type is not known.
933+
930934
.. c:macro:: EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY
931935
932936
Primary portrait mode orientation.
@@ -954,7 +958,7 @@ Struct
954958
955959
.. c:member:: int orientationIndex
956960
957-
One of the :c:type:`EM_ORIENTATION_PORTRAIT_xxx <EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY>` fields, or -1 if unknown.
961+
One of the :c:type:`EM_ORIENTATION_PORTRAIT_xxx <EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY>` fields, or :c:type:`EMSCRIPTEN_ORIENTATION_UNKNOWN` if unknown.
958962
959963
.. c:member:: int orientationAngle
960964

src/library_html5.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -929,23 +929,39 @@ var LibraryHTML5 = {
929929
},
930930

931931
$screenOrientation: () => {
932-
if (!screen) return undefined;
933-
return screen.orientation || screen.mozOrientation || screen.webkitOrientation || screen.msOrientation;
932+
if (!window.screen) return undefined;
933+
return screen.orientation || screen['mozOrientation'] || screen['webkitOrientation'];
934934
},
935935

936936
$fillOrientationChangeEventData__deps: ['$screenOrientation'],
937937
$fillOrientationChangeEventData: (eventStruct) => {
938-
var orientations = ["portrait-primary", "portrait-secondary", "landscape-primary", "landscape-secondary"];
939-
var orientations2 = ["portrait", "portrait", "landscape", "landscape"];
940-
941-
var orientationString = screenOrientation();
942-
var orientation = orientations.indexOf(orientationString);
943-
if (orientation == -1) {
944-
orientation = orientations2.indexOf(orientationString);
938+
// OrientationType enum
939+
var orientationsType1 = ['portrait-primary', 'portrait-secondary', 'landscape-primary', 'landscape-secondary'];
940+
// alternative selection from OrientationLockType enum
941+
var orientationsType2 = ['portrait', 'portrait', 'landscape', 'landscape'];
942+
943+
var orientationIndex = 0;
944+
var orientationAngle = 0;
945+
var screenOrientObj = screenOrientation();
946+
if (typeof screenOrientObj === 'object') {
947+
orientationIndex = orientationsType1.indexOf(screenOrientObj.type);
948+
if (orientationIndex < 0) {
949+
orientationIndex = orientationsType2.indexOf(screenOrientObj.type);
950+
}
951+
if (orientationIndex >= 0) {
952+
orientationIndex = 1 << orientationIndex;
953+
}
954+
orientationAngle = screenOrientObj.angle;
955+
}
956+
#if MIN_SAFARI_VERSION < 0x100400
957+
else {
958+
// fallback for Safari earlier than 16.4 (March 2023)
959+
orientationAngle = window.orientation;
945960
}
961+
#endif
946962

947-
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationIndex, '1 << orientation', 'i32') }}};
948-
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationAngle, 'orientation', 'i32') }}};
963+
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationIndex, 'orientationIndex', 'i32') }}};
964+
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationAngle, 'orientationAngle', 'i32') }}};
949965
},
950966

951967
$registerOrientationChangeEventCallback__deps: ['$JSEvents', '$fillOrientationChangeEventData', '$findEventTarget', 'malloc'],
@@ -971,10 +987,6 @@ var LibraryHTML5 = {
971987
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, orientationChangeEvent, userData)) e.preventDefault();
972988
};
973989

974-
if (eventTypeId == {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}} && screen.mozOrientation !== undefined) {
975-
eventTypeString = "mozorientationchange";
976-
}
977-
978990
var eventHandler = {
979991
target,
980992
eventTypeString,
@@ -988,13 +1000,14 @@ var LibraryHTML5 = {
9881000
emscripten_set_orientationchange_callback_on_thread__proxy: 'sync',
9891001
emscripten_set_orientationchange_callback_on_thread__deps: ['$registerOrientationChangeEventCallback'],
9901002
emscripten_set_orientationchange_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
991-
if (!screen || !screen['addEventListener']) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
992-
return registerOrientationChangeEventCallback(screen, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}}, "orientationchange", targetThread);
1003+
if (!window.screen || !screen.orientation) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
1004+
return registerOrientationChangeEventCallback(screen.orientation, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}}, 'change', targetThread);
9931005
},
9941006

9951007
emscripten_get_orientation_status__proxy: 'sync',
9961008
emscripten_get_orientation_status__deps: ['$fillOrientationChangeEventData', '$screenOrientation'],
9971009
emscripten_get_orientation_status: (orientationChangeEvent) => {
1010+
// screenOrientation() resolving standard, window.orientation being the deprecated mobile-only
9981011
if (!screenOrientation() && typeof orientation == 'undefined') return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
9991012
fillOrientationChangeEventData(orientationChangeEvent);
10001013
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
@@ -1014,8 +1027,6 @@ var LibraryHTML5 = {
10141027
succeeded = screen.mozLockOrientation(orientations);
10151028
} else if (screen.webkitLockOrientation) {
10161029
succeeded = screen.webkitLockOrientation(orientations);
1017-
} else if (screen.msLockOrientation) {
1018-
succeeded = screen.msLockOrientation(orientations);
10191030
} else {
10201031
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
10211032
}
@@ -1033,8 +1044,6 @@ var LibraryHTML5 = {
10331044
screen.mozUnlockOrientation();
10341045
} else if (screen.webkitUnlockOrientation) {
10351046
screen.webkitUnlockOrientation();
1036-
} else if (screen.msUnlockOrientation) {
1037-
screen.msUnlockOrientation();
10381047
} else {
10391048
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
10401049
}

system/include/emscripten/html5.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ EMSCRIPTEN_RESULT emscripten_set_devicemotion_callback_on_thread(void *userData,
224224

225225
EMSCRIPTEN_RESULT emscripten_get_devicemotion_status(EmscriptenDeviceMotionEvent *motionState __attribute__((nonnull)));
226226

227+
#define EMSCRIPTEN_ORIENTATION_UNSUPPORTED 0
227228
#define EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY 1
228229
#define EMSCRIPTEN_ORIENTATION_PORTRAIT_SECONDARY 2
229230
#define EMSCRIPTEN_ORIENTATION_LANDSCAPE_PRIMARY 4

0 commit comments

Comments
 (0)