Skip to content

emscripten_request_fullscreen_strategy() fails where EM_ASM(Module.requestFullscreen(1, 1)) succeeds, fullscreen request deferred from glfwSetKeyCallback #5124

@satoshinm

Description

@satoshinm

Attempting to add a keyboard shortcut to toggle between fullscreen/fullwindow (HTML5 Full Screen and emscripten's "soft fullscreen"), mostly working example here (it should show a solid gray page covering the entire window, press F11 to toggle to fullscreen and back to fullwindow):

However, in the glfwSetKeyCallback() callback function, the only successful method I've found to enter fullscreen is:

EM_ASM(Module.requestFullscreen(1, 1));

This matches what the "Fullscreen" button does in the default shell.html, if both checkboxes are checked:

  <span><input type="checkbox" id="resize">Resize canvas</span>
  <span><input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer &nbsp;&nbsp;&nbsp;</span>
  <span><input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked, 
                                                                            document.getElementById('resize').checked)">

But if I change this EM_ASM(Module.requestFullscreen(1, 1)); call to emscripten_request_fullscreen_strategy(), fullscreen is no longer entered. Relevant snippet of code:

void on_key(GLFWwindow *window, int key, int scancode, int action, int mods) {
    if (action && GLFW_PRESS && key == GLFW_KEY_F11) {
        printf("glfwSetKeyCallback: F11 pressed\n");
        // F11 toggles between fullscreen and fullwindow ("soft fullscreen") mode. The app starts
        // in fullwindow, a solid gray page. When fullscreen is entered with F11, the entire
        // screen should be solid gray. Returning to fullwindow with F11 should also show the same.

        EmscriptenFullscreenChangeEvent fsce;
        EMSCRIPTEN_RESULT ret = emscripten_get_fullscreen_status(&fsce);

        if (!fsce.isFullscreen) {
            emscripten_exit_soft_fullscreen();

            // Enter fullscreen
            /* this returns 1=EMSCRIPTEN_RESULT_DEFERRED if EM_TRUE is given to defer
             * or -2=EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED if EM_FALSE
             * but the EM_ASM() JS works immediately?
             */
            EmscriptenFullscreenStrategy strategy = {
                .scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH,
                .canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF,
                .filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
                .canvasResizedCallback = on_canvassize_changed,
                .canvasResizedCallbackUserData = NULL
            };
            EMSCRIPTEN_RESULT ret = emscripten_request_fullscreen_strategy(NULL, EM_TRUE, &strategy);
            printf("emscripten_request_fullscreen_strategy = %d\n", ret);
            //EM_ASM(Module.requestFullscreen(1, 1));

Why would emscripten_request_fullscreen_strategy() fail or require deferral here, even though it is called in a key callback handler (user action), and EM_ASM(Module.requestFullscreen(1, 1)); succeeds (suggesting the failure is probably not due to browser security restrictions)?


Note: using EM_ASM(Module.requestFullscreen(1, 1)); over emscripten_request_fullscreen_strategy() is a decent workaround, but it doesn't have all of the same functionality, particularly setting .canvasResizedCallback (which is the only way I could find to set this through emscripten, partial replacement is EM_ASM() to window.addEventHandler('resize'), but there is a lot of useful other logic _strategy() provides.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions