src/video/android/SDL_androidtouch.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Nov 2012 20:11:51 -0800
changeset 6651 17ef8a7cab55
parent 6400 2ed5671bc5e0
child 6885 700f1b25f77f
permissions -rw-r--r--
Added mouse emulation for touch events on Android.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 
    31 #include "SDL_androidtouch.h"
    32 
    33 
    34 #define ACTION_DOWN 0
    35 #define ACTION_UP 1
    36 #define ACTION_MOVE 2
    37 #define ACTION_CANCEL 3
    38 #define ACTION_OUTSIDE 4
    39 // The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2
    40 #define ACTION_POINTER_1_DOWN 5
    41 #define ACTION_POINTER_1_UP 6
    42 
    43 static SDL_FingerID leftFingerDown = 0;
    44 
    45 static void Android_GetWindowCoordinates(float x, float y,
    46                                          int *window_x, int *window_y)
    47 {
    48     int window_w, window_h;
    49 
    50     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    51     *window_x = (int)(x * window_w);
    52     *window_y = (int)(y * window_h);
    53 }
    54 
    55 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
    56 {
    57     SDL_TouchID touchDeviceId = 0;
    58     SDL_FingerID fingerId = 0;
    59     int window_x, window_y;
    60 
    61     if (!Android_Window) {
    62         return;
    63     }
    64 
    65     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    66     if (!SDL_GetTouch(touchDeviceId)) {
    67         SDL_Touch touch;
    68         memset( &touch, 0, sizeof(touch) );
    69         touch.id = touchDeviceId;
    70         touch.x_min = 0.0f;
    71         touch.x_max = 1.0f;
    72         touch.native_xres = touch.x_max - touch.x_min;
    73         touch.y_min = 0.0f;
    74         touch.y_max = 1.0f;
    75         touch.native_yres = touch.y_max - touch.y_min;
    76         touch.pressure_min = 0.0f;
    77         touch.pressure_max = 1.0f;
    78         touch.native_pressureres = touch.pressure_max - touch.pressure_min;
    79         if (SDL_AddTouch(&touch, "") < 0) {
    80              SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    81         }
    82     }
    83 
    84     fingerId = (SDL_FingerID)pointer_finger_id_in;
    85     switch (action) {
    86         case ACTION_DOWN:
    87         case ACTION_POINTER_1_DOWN:
    88             if (!leftFingerDown) {
    89                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    90 
    91                 /* send moved event */
    92                 SDL_SendMouseMotion(NULL, 0, window_x, window_y);
    93 
    94                 /* send mouse down event */
    95                 SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
    96 
    97                 leftFingerDown = fingerId;
    98             }
    99             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
   100             break;
   101         case ACTION_MOVE:
   102             if (!leftFingerDown) {
   103                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   104 
   105                 /* send moved event */
   106                 SDL_SendMouseMotion(NULL, 0, window_x, window_y);
   107             }
   108             SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   109             break;
   110         case ACTION_UP:
   111         case ACTION_POINTER_1_UP:
   112             if (fingerId == leftFingerDown) {
   113                 /* send mouse up */
   114                 SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
   115                 leftFingerDown = 0;
   116             }
   117             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   118             break;
   119         default:
   120             break;
   121     } 
   122 }
   123 
   124 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   125 
   126 /* vi: set ts=4 sw=4 expandtab: */