Description
Tested with the latest main (6105e76) and 3.1.55 Emscripten. Adding an orientation change callback is either unsupported (iOS/Safari) or silently fails (Android/Chrome), but manually running the JS calls does work. Here's a minimal test:
#include <stdio.h>
#include <emscripten/em_js.h>
#include <emscripten/html5.h>
EM_JS(void, manually_add, (), {
screen.orientation.addEventListener("change",
function(e) {
Module.print("Orientation (JS) changed: type: " + e.target.type + ", angle: " + e.target.angle);
}
);
});
EM_BOOL orientation(int type, const EmscriptenOrientationChangeEvent* e, void* data) {
printf("Orientation (C) changed: type:%d, angle: %d\n", e->orientationIndex, e->orientationAngle);
return EM_TRUE;
}
int main() {
// add via Emscripten's HTML5 call
EMSCRIPTEN_RESULT res = emscripten_set_orientationchange_callback(NULL, EM_FALSE, orientation);
if (res == EMSCRIPTEN_RESULT_SUCCESS) {
printf("Orientation callback supported and added\n");
} else {
printf("Unable to add orientation callback\n");
}
// add directly via JavaScript
manually_add();
}
Build with: emcc -O0 -g3 -s ASSERTIONS=2 -o index.html test.c
. A pre-built version of the repro can be seen here:
https://wip.numfum.com/2024-02-28/emscripten/orientation-cb/index.html
Either test on a tablet on in a browser's dev tools with the device toolbar enabled (e.g. Apple + Shift + M in Chrome). Rotate the device or change the dev tool rotation. Note that emscripten_set_orientationchange_callback()
either fails to add or simply does nothing, but the JS screen orientation listener works on iOS, Android and desktop.
This is different from the ASSERTION
issue reported in #21447.