src/video/emscripten/SDL_emscriptenevents.c
changeset 10976 1da5502cf8ea
parent 10895 fd63ed9b0746
child 11002 ba0ce5b958d2
     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";