emscripten-ports / SDL2

Other
166 stars 64 forks source link

SDL_WarpMouseInWindow doesn't warp the mouse to a new location #157

Closed gregbuchholz closed 2 years ago

gregbuchholz commented 2 years ago

It appears that the SDL_WarpMouseInWindow() function doesn't actually warp the mouse to a new location while running in Emscripten. I've tested with Firefox 95.0.1 in Windows 10, Firefox 95.0 in Lubuntu, and Edge 96.0.1054.57 on Windows 10, with the same result. But it does work as a native program. Here's the version of emscripten I've been using:

$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.0.0 (3fd52e107187b8a169bb04a02b9f982c8a075205)
clang version 14.0.0 (https://github.com/llvm/llvm-project 4348cd42c385e71b63e5da7e492172cff6a79d7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/greg/emsdk/upstream/bin

...and here is a test program to demonstrate the issue:

// Compile with:
//    emcc warp.c -s USE_SDL=2 -s ASYNCIFY -s ALLOW_MEMORY_GROWTH=1

#include <stdbool.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

int main(int argc, char ** argv)
{
    bool quit = false;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window * window = SDL_CreateWindow("Testing mouse warp",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

    int mouse_x = 0, mouse_y = 0;

    while (!quit)
    {
        Uint32 buttons;
        SDL_Delay(20);
        SDL_PollEvent(&event);

        switch (event.type)
        {
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_KEYDOWN:
                buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
                switch (event.key.keysym.sym)
                {
                case SDLK_LEFT:
                    SDL_WarpMouseInWindow(window, mouse_x-10, mouse_y);
                    break;
                case SDLK_RIGHT:  
                    SDL_WarpMouseInWindow(window, mouse_x+10, mouse_y);
                    break;
                case SDLK_UP:     
                    SDL_WarpMouseInWindow(window, mouse_x, mouse_y-10);
                    break;
                case SDLK_DOWN:   
                    SDL_WarpMouseInWindow(window, mouse_x, mouse_y+10);
                    break;
                }
                break;
            case SDL_MOUSEMOTION:
                mouse_x = event.motion.x;
                mouse_y = event.motion.y;
                break;
        }

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
        SDL_RenderDrawLine(renderer, 0, 0, mouse_x, mouse_y);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

...This program draws a red line from the upper left corner to the current mouse position. Use the arrow keys, and note the mouse position doesn't move. This may be a know limitation due to the browser, but I couldn't seem to find it documented anywhere. If there is a better place to report this, please let me know.

Thanks!

Daft-Freak commented 2 years ago

Yeah, this is one of the things you can't do in JS.

I don't think we have a list of limitations documented anywhere, maybe we should?

gregbuchholz commented 2 years ago

Closing since this seems like a known limitation.