src/video/android/SDL_androidtouch.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 25 Nov 2013 12:28:09 -0300
changeset 8030 7b0d28c202d2
parent 7786 cfc1285375e1
child 8093 b43765095a6f
permissions -rw-r--r--
[Android] Fixes #2228, reworked touch code

Lets Android take care of which is the primary pointer (the one acting as the
mouse in SDL), reorganized the Java side code as well to make it easier to
understand.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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_config.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     static SDL_FingerID pointerFingerID = 0;
    73 
    74     if (!Android_Window) {
    75         return;
    76     }
    77 
    78     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    79     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    80         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    81     }
    82 
    83     fingerId = (SDL_FingerID)pointer_finger_id_in;
    84     switch (action) {
    85         case ACTION_DOWN:
    86             /* Primary pointer down */
    87             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    88             /* send moved event */
    89             SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    90             /* send mouse down event */
    91             SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    92             pointerFingerID = fingerId;
    93         case ACTION_POINTER_DOWN:
    94             /* Non primary pointer down */
    95             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    96             break;
    97             
    98         case ACTION_MOVE:
    99             if (!pointerFingerID) {
   100                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   101 
   102                 /* send moved event */
   103                 SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   104             }
   105             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   106             break;
   107             
   108         case ACTION_UP:
   109             /* Primary pointer up */
   110             /* send mouse up */
   111             pointerFingerID = (SDL_FingerID) 0;
   112             SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   113         case ACTION_POINTER_UP:
   114             /* Non primary pointer up */
   115             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   116             break;
   117             
   118         default:
   119             break;
   120     }
   121 }
   122 
   123 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   124 
   125 /* vi: set ts=4 sw=4 expandtab: */