Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple fixes to EmscriptenOrientationChangeEvent #21428

Merged
merged 19 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion site/source/docs/api_reference/html5.h.rst
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,10 @@ Defines
Emscripten `orientationchange <https://w3c.github.io/screen-orientation/>`_ event.


.. c:macro:: EMSCRIPTEN_ORIENTATION_UNKNOWN

Either the orientation API is not supported or the orientation type is not known.

.. c:macro:: EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY

Primary portrait mode orientation.
Expand Down Expand Up @@ -954,7 +958,7 @@ Struct

.. c:member:: int orientationIndex

One of the :c:type:`EM_ORIENTATION_PORTRAIT_xxx <EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY>` fields, or -1 if unknown.
One of the :c:type:`EM_ORIENTATION_PORTRAIT_xxx <EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY>` fields, or :c:type:`EMSCRIPTEN_ORIENTATION_UNKNOWN` if unknown.

.. c:member:: int orientationAngle

Expand Down
51 changes: 30 additions & 21 deletions src/library_html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,23 +929,39 @@ var LibraryHTML5 = {
},

$screenOrientation: () => {
if (!screen) return undefined;
return screen.orientation || screen.mozOrientation || screen.webkitOrientation || screen.msOrientation;
if (!window.screen) return undefined;
return screen.orientation || screen['mozOrientation'] || screen['webkitOrientation'];
},

$fillOrientationChangeEventData__deps: ['$screenOrientation'],
$fillOrientationChangeEventData: (eventStruct) => {
var orientations = ["portrait-primary", "portrait-secondary", "landscape-primary", "landscape-secondary"];
var orientations2 = ["portrait", "portrait", "landscape", "landscape"];

var orientationString = screenOrientation();
var orientation = orientations.indexOf(orientationString);
if (orientation == -1) {
orientation = orientations2.indexOf(orientationString);
// 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 = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:This might be clearer if it was {{{ cDefs.EMSCRIPTEN_ORIENTATION_UNSUPPORTED }}}, although that would require updating struct_info.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP, I'll take a look (the CI is broken so it'll keep me busy until that's resolved).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested nitpick added to #21550.

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;
cwoffenden marked this conversation as resolved.
Show resolved Hide resolved
}
orientationAngle = screenOrientObj.angle;
}
#if MIN_SAFARI_VERSION < 0x100400
cwoffenden marked this conversation as resolved.
Show resolved Hide resolved
else {
// fallback for Safari earlier than 16.4 (March 2023)
orientationAngle = window.orientation;
}
#endif

{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationIndex, '1 << orientation', 'i32') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationAngle, 'orientation', 'i32') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationIndex, 'orientationIndex', 'i32') }}};
{{{ makeSetValue('eventStruct', C_STRUCTS.EmscriptenOrientationChangeEvent.orientationAngle, 'orientationAngle', 'i32') }}};
},

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

if (eventTypeId == {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}} && screen.mozOrientation !== undefined) {
eventTypeString = "mozorientationchange";
}
cwoffenden marked this conversation as resolved.
Show resolved Hide resolved

var eventHandler = {
target,
eventTypeString,
Expand All @@ -988,13 +1000,14 @@ var LibraryHTML5 = {
emscripten_set_orientationchange_callback_on_thread__proxy: 'sync',
emscripten_set_orientationchange_callback_on_thread__deps: ['$registerOrientationChangeEventCallback'],
emscripten_set_orientationchange_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
if (!screen || !screen['addEventListener']) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
return registerOrientationChangeEventCallback(screen, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}}, "orientationchange", targetThread);
if (!window.screen || !screen.orientation) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
return registerOrientationChangeEventCallback(screen.orientation, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}}, 'change', targetThread);
},

emscripten_get_orientation_status__proxy: 'sync',
emscripten_get_orientation_status__deps: ['$fillOrientationChangeEventData', '$screenOrientation'],
emscripten_get_orientation_status: (orientationChangeEvent) => {
// screenOrientation() resolving standard, window.orientation being the deprecated mobile-only
if (!screenOrientation() && typeof orientation == 'undefined') return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
fillOrientationChangeEventData(orientationChangeEvent);
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
Expand All @@ -1014,8 +1027,6 @@ var LibraryHTML5 = {
succeeded = screen.mozLockOrientation(orientations);
} else if (screen.webkitLockOrientation) {
succeeded = screen.webkitLockOrientation(orientations);
} else if (screen.msLockOrientation) {
succeeded = screen.msLockOrientation(orientations);
} else {
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
}
Expand All @@ -1033,8 +1044,6 @@ var LibraryHTML5 = {
screen.mozUnlockOrientation();
} else if (screen.webkitUnlockOrientation) {
screen.webkitUnlockOrientation();
} else if (screen.msUnlockOrientation) {
screen.msUnlockOrientation();
} else {
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
}
Expand Down
1 change: 1 addition & 0 deletions system/include/emscripten/html5.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ EMSCRIPTEN_RESULT emscripten_set_devicemotion_callback_on_thread(void *userData,

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

#define EMSCRIPTEN_ORIENTATION_UNSUPPORTED 0
#define EMSCRIPTEN_ORIENTATION_PORTRAIT_PRIMARY 1
#define EMSCRIPTEN_ORIENTATION_PORTRAIT_SECONDARY 2
#define EMSCRIPTEN_ORIENTATION_LANDSCAPE_PRIMARY 4
Expand Down
Loading