Fixed part of bug 3227 - patch for multiple buttons at the same time not working
authorSam Lantinga <slouken@libsdl.org>
Sat, 12 Aug 2017 08:06:16 -0700
changeset 1123711c69137bfd5
parent 11236 8c3cba28b1fd
child 11238 c728c661cec7
Fixed part of bug 3227 - patch for multiple buttons at the same time not working

Philipp Wiesemann

There is another problem with the current implementation which maybe should be fixed first (to prevent some work). It was written as if it would get the number of a button from the Java side but actually it gets the state of all buttons. That is why it should not work if more than one button is pressed at once.
src/video/android/SDL_androidmouse.c
     1.1 --- a/src/video/android/SDL_androidmouse.c	Sat Aug 12 00:04:46 2017 -0700
     1.2 +++ b/src/video/android/SDL_androidmouse.c	Sat Aug 12 08:06:16 2017 -0700
     1.3 @@ -30,6 +30,7 @@
     1.4  
     1.5  #include "../../core/android/SDL_android.h"
     1.6  
     1.7 +/* See Android's MotionEvent class for constants */
     1.8  #define ACTION_DOWN 0
     1.9  #define ACTION_UP 1
    1.10  #define ACTION_MOVE 2
    1.11 @@ -41,41 +42,59 @@
    1.12  #define BUTTON_BACK 8
    1.13  #define BUTTON_FORWARD 16
    1.14  
    1.15 -static Uint8 SDLButton;
    1.16 +/* Last known Android mouse button state (includes all buttons) */
    1.17 +static int last_state;
    1.18  
    1.19  void
    1.20  Android_InitMouse(void)
    1.21  {
    1.22 -    SDLButton = 0;
    1.23 +    last_state = 0;
    1.24  }
    1.25  
    1.26 -void Android_OnMouse( int androidButton, int action, float x, float y) {
    1.27 +/* Translate Android mouse button state to SDL mouse button */
    1.28 +static Uint8
    1.29 +TranslateButton(int state)
    1.30 +{
    1.31 +    if (state & BUTTON_PRIMARY) {
    1.32 +        return SDL_BUTTON_LEFT;
    1.33 +    } else if (state & BUTTON_SECONDARY) {
    1.34 +        return SDL_BUTTON_RIGHT;
    1.35 +    } else if (state & BUTTON_TERTIARY) {
    1.36 +        return SDL_BUTTON_MIDDLE;
    1.37 +    } else if (state & BUTTON_FORWARD) {
    1.38 +        return SDL_BUTTON_X1;
    1.39 +    } else if (state & BUTTON_BACK) {
    1.40 +        return SDL_BUTTON_X2;
    1.41 +    } else {
    1.42 +        return 0;
    1.43 +    }
    1.44 +}
    1.45 +
    1.46 +void
    1.47 +Android_OnMouse(int state, int action, float x, float y)
    1.48 +{
    1.49 +    int changes;
    1.50 +    Uint8 button;
    1.51 +
    1.52      if (!Android_Window) {
    1.53          return;
    1.54      }
    1.55  
    1.56      switch(action) {
    1.57          case ACTION_DOWN:
    1.58 -            // Determine which button originated the event, and store it for ACTION_UP
    1.59 -            SDLButton = SDL_BUTTON_LEFT;
    1.60 -            if (androidButton == BUTTON_SECONDARY) {
    1.61 -                SDLButton = SDL_BUTTON_RIGHT;
    1.62 -            } else if (androidButton == BUTTON_TERTIARY) {
    1.63 -                SDLButton = SDL_BUTTON_MIDDLE;
    1.64 -            } else if (androidButton == BUTTON_FORWARD) {
    1.65 -                SDLButton = SDL_BUTTON_X1;
    1.66 -            } else if (androidButton == BUTTON_BACK) {
    1.67 -                SDLButton = SDL_BUTTON_X2;
    1.68 -            }
    1.69 +            changes = state & ~last_state;
    1.70 +            button = TranslateButton(changes);
    1.71 +            last_state = state;
    1.72              SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
    1.73 -            SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, SDLButton);
    1.74 +            SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
    1.75              break;
    1.76  
    1.77          case ACTION_UP:
    1.78 -            // Android won't give us the button that originated the ACTION_DOWN event, so we'll
    1.79 -            // assume it's the one we stored
    1.80 +            changes = last_state & ~state;
    1.81 +            button = TranslateButton(changes);
    1.82 +            last_state = state;
    1.83              SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
    1.84 -            SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, SDLButton);
    1.85 +            SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
    1.86              break;
    1.87  
    1.88          case ACTION_MOVE: