From 78c84e7029cd6ff13a55b7a1c2c66795f781cf56 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 12 Aug 2017 08:06:16 -0700 Subject: [PATCH] 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 | 55 +++++++++++++++++++--------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/video/android/SDL_androidmouse.c b/src/video/android/SDL_androidmouse.c index 7b827bb7f7610..16782db312d87 100644 --- a/src/video/android/SDL_androidmouse.c +++ b/src/video/android/SDL_androidmouse.c @@ -30,6 +30,7 @@ #include "../../core/android/SDL_android.h" +/* See Android's MotionEvent class for constants */ #define ACTION_DOWN 0 #define ACTION_UP 1 #define ACTION_MOVE 2 @@ -41,41 +42,59 @@ #define BUTTON_BACK 8 #define BUTTON_FORWARD 16 -static Uint8 SDLButton; +/* Last known Android mouse button state (includes all buttons) */ +static int last_state; void Android_InitMouse(void) { - SDLButton = 0; + last_state = 0; } -void Android_OnMouse( int androidButton, int action, float x, float y) { +/* Translate Android mouse button state to SDL mouse button */ +static Uint8 +TranslateButton(int state) +{ + if (state & BUTTON_PRIMARY) { + return SDL_BUTTON_LEFT; + } else if (state & BUTTON_SECONDARY) { + return SDL_BUTTON_RIGHT; + } else if (state & BUTTON_TERTIARY) { + return SDL_BUTTON_MIDDLE; + } else if (state & BUTTON_FORWARD) { + return SDL_BUTTON_X1; + } else if (state & BUTTON_BACK) { + return SDL_BUTTON_X2; + } else { + return 0; + } +} + +void +Android_OnMouse(int state, int action, float x, float y) +{ + int changes; + Uint8 button; + if (!Android_Window) { return; } switch(action) { case ACTION_DOWN: - // Determine which button originated the event, and store it for ACTION_UP - SDLButton = SDL_BUTTON_LEFT; - if (androidButton == BUTTON_SECONDARY) { - SDLButton = SDL_BUTTON_RIGHT; - } else if (androidButton == BUTTON_TERTIARY) { - SDLButton = SDL_BUTTON_MIDDLE; - } else if (androidButton == BUTTON_FORWARD) { - SDLButton = SDL_BUTTON_X1; - } else if (androidButton == BUTTON_BACK) { - SDLButton = SDL_BUTTON_X2; - } + changes = state & ~last_state; + button = TranslateButton(changes); + last_state = state; SDL_SendMouseMotion(Android_Window, 0, 0, x, y); - SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, SDLButton); + SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button); break; case ACTION_UP: - // Android won't give us the button that originated the ACTION_DOWN event, so we'll - // assume it's the one we stored + changes = last_state & ~state; + button = TranslateButton(changes); + last_state = state; SDL_SendMouseMotion(Android_Window, 0, 0, x, y); - SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, SDLButton); + SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button); break; case ACTION_MOVE: