emscripten: keep track of pointer lock losses and maybe regrab pointer later.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 20 Apr 2017 13:00:54 -0400
changeset 109761da5502cf8ea
parent 10975 602806fdeba6
child 10977 b14856808a3a
emscripten: keep track of pointer lock losses and maybe regrab pointer later.

If an Emscripten app is in relative mouse mode and the user presses Escape
(or whatever is appropriate), then the pointer lock is broken by the browser.

This keeps track of those losses, and next time the user presses a mouse
button down on the canvas, if the app is still meant to be in relative mouse
mode, we will attempt to regrab the pointer.

This makes it much more seamless for things like first-person shooters, and
the app doesn't need any manual intervention.
src/video/emscripten/SDL_emscriptenevents.c
src/video/emscripten/SDL_emscriptenvideo.h
     1.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Tue Apr 18 22:17:40 2017 -0700
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Thu Apr 20 13:00:54 2017 -0400
     1.3 @@ -297,13 +297,23 @@
     1.4      return SDL_TRUE;
     1.5  }
     1.6  
     1.7 +static EM_BOOL
     1.8 +Emscripten_HandlePointerLockChange(int eventType, const EmscriptenPointerlockChangeEvent *changeEvent, void *userData)
     1.9 +{
    1.10 +    SDL_WindowData *window_data = (SDL_WindowData *) userData;
    1.11 +    /* keep track of lock losses, so we can regrab if/when appropriate. */
    1.12 +    window_data->has_pointer_lock = changeEvent->isActive;
    1.13 +    return 0;
    1.14 +}
    1.15 +
    1.16 +
    1.17  EM_BOOL
    1.18  Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
    1.19  {
    1.20      SDL_WindowData *window_data = userData;
    1.21 +    const int isPointerLocked = window_data->has_pointer_lock;
    1.22      int mx, my;
    1.23      static double residualx = 0, residualy = 0;
    1.24 -    EmscriptenPointerlockChangeEvent pointerlock_status;
    1.25  
    1.26      /* rescale (in case canvas is being scaled)*/
    1.27      double client_w, client_h, xscale, yscale;
    1.28 @@ -311,10 +321,6 @@
    1.29      xscale = window_data->window->w / client_w;
    1.30      yscale = window_data->window->h / client_h;
    1.31  
    1.32 -    /* check for pointer lock */
    1.33 -    int isPointerLockSupported = emscripten_get_pointerlock_status(&pointerlock_status);
    1.34 -    int isPointerLocked = isPointerLockSupported == EMSCRIPTEN_RESULT_SUCCESS  ? pointerlock_status.isActive : SDL_FALSE;
    1.35 -
    1.36      if (isPointerLocked) {
    1.37          residualx += mouseEvent->movementX * xscale;
    1.38          residualy += mouseEvent->movementY * yscale;
    1.39 @@ -355,6 +361,9 @@
    1.40      }
    1.41  
    1.42      if (eventType == EMSCRIPTEN_EVENT_MOUSEDOWN) {
    1.43 +        if (SDL_GetMouse()->relative_mode && !window_data->has_pointer_lock) {
    1.44 +            emscripten_request_pointerlock(NULL, 0);  /* try to regrab lost pointer lock. */
    1.45 +        }
    1.46          sdl_button_state = SDL_PRESSED;
    1.47          sdl_event_type = SDL_MOUSEBUTTONDOWN;
    1.48      } else {
    1.49 @@ -371,11 +380,7 @@
    1.50      SDL_WindowData *window_data = userData;
    1.51  
    1.52      int mx = mouseEvent->canvasX, my = mouseEvent->canvasY;
    1.53 -    EmscriptenPointerlockChangeEvent pointerlock_status;
    1.54 -
    1.55 -    /* check for pointer lock */
    1.56 -    int isPointerLockSupported = emscripten_get_pointerlock_status(&pointerlock_status);
    1.57 -    int isPointerLocked = isPointerLockSupported == EMSCRIPTEN_RESULT_SUCCESS  ? pointerlock_status.isActive : SDL_FALSE;
    1.58 +    const int isPointerLocked = window_data->has_pointer_lock;
    1.59  
    1.60      if (!isPointerLocked) {
    1.61          /* rescale (in case canvas is being scaled)*/
    1.62 @@ -632,6 +637,8 @@
    1.63      emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.64      emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.65  
    1.66 +    emscripten_set_pointerlockchange_callback(NULL, data, 0, Emscripten_HandlePointerLockChange);
    1.67 +
    1.68      /* Keyboard events are awkward */
    1.69      const char *keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
    1.70      if (!keyElement) keyElement = "#window";
     2.1 --- a/src/video/emscripten/SDL_emscriptenvideo.h	Tue Apr 18 22:17:40 2017 -0700
     2.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.h	Thu Apr 20 13:00:54 2017 -0400
     2.3 @@ -47,6 +47,8 @@
     2.4  
     2.5      SDL_bool finger_touching;  /* for mapping touch events to mice */
     2.6      SDL_FingerID first_finger;
     2.7 +
     2.8 +    SDL_bool has_pointer_lock;
     2.9  } SDL_WindowData;
    2.10  
    2.11  #endif /* _SDL_emscriptenvideo_h */