src/video/android/SDL_androidtouch.c
author Joseba García Etxebarria <joseba.gar@gmail.com>
Tue, 24 Mar 2015 20:45:29 +0100
changeset 9438 fff5af5de6dd
parent 8963 f94215b55583
child 9439 0d01b53cdbea
permissions -rw-r--r--
* Improve mouse support in Android. These changes require Android API v12 to compile
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 #include <android/log.h>
    26 
    27 #include "SDL_events.h"
    28 #include "../../events/SDL_mouse_c.h"
    29 #include "../../events/SDL_touch_c.h"
    30 #include "SDL_log.h"
    31 
    32 #include "SDL_androidtouch.h"
    33 
    34 #include "../../core/android/SDL_android.h"
    35 
    36 #define ACTION_DOWN 0
    37 #define ACTION_UP 1
    38 #define ACTION_MOVE 2
    39 #define ACTION_CANCEL 3
    40 #define ACTION_OUTSIDE 4
    41 #define ACTION_POINTER_DOWN 5
    42 #define ACTION_POINTER_UP 6
    43 
    44 static void Android_GetWindowCoordinates(float x, float y,
    45                                          int *window_x, int *window_y)
    46 {
    47     int window_w, window_h;
    48 
    49     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    50     *window_x = (int)(x * window_w);
    51     *window_y = (int)(y * window_h);
    52 }
    53 
    54 void Android_InitTouch(void)
    55 {
    56     int i;
    57     int* ids;
    58     int number = Android_JNI_GetTouchDeviceIds(&ids);
    59     if (0 < number) {
    60         for (i = 0; i < number; ++i) {
    61             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
    62         }
    63         SDL_free(ids);
    64     }
    65 }
    66 
    67 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
    68 {
    69     SDL_TouchID touchDeviceId = 0;
    70     SDL_FingerID fingerId = 0;
    71     int window_x, window_y;
    72     char * hint;
    73     static SDL_FingerID pointerFingerID = 0;
    74 
    75     if (!Android_Window) {
    76         return;
    77     }
    78 
    79     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    80     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    81         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    82     }
    83 
    84     fingerId = (SDL_FingerID)pointer_finger_id_in;
    85     hint = SDL_GetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH");
    86     switch (action) {
    87         case ACTION_DOWN:
    88             /* Primary pointer down */
    89             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    90             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
    91                 /* send moved event */
    92                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    93                 /* send mouse down event */
    94                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    95             }
    96             pointerFingerID = fingerId;
    97         case ACTION_POINTER_DOWN:
    98             /* Non primary pointer down */
    99             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
   100             break;
   101 
   102         case ACTION_MOVE:
   103             if (!pointerFingerID) {
   104                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   105 
   106                 if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   107                     /* send moved event */
   108                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   109                 }
   110             }
   111             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   112             break;
   113 
   114         case ACTION_UP:
   115             /* Primary pointer up */
   116             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   117                 /* send mouse up */
   118                 pointerFingerID = (SDL_FingerID) 0;
   119                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   120             }
   121         case ACTION_POINTER_UP:
   122             /* Non primary pointer up */
   123             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   124             break;
   125 
   126         default:
   127             break;
   128     }
   129 }
   130 
   131 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   132 
   133 /* vi: set ts=4 sw=4 expandtab: */