src/video/android/SDL_androidtouch.c
author Joseba García Etxebarria <joseba.gar@gmail.com>
Tue, 24 Mar 2015 21:02:28 +0100
changeset 9439 0d01b53cdbea
parent 9438 fff5af5de6dd
child 9441 fc7522030729
permissions -rw-r--r--
* More Android patch work
     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_hints.h"
    28 #include "SDL_events.h"
    29 #include "SDL_log.h"
    30 #include "SDL_androidtouch.h"
    31 #include "../../events/SDL_mouse_c.h"
    32 #include "../../events/SDL_touch_c.h"
    33 #include "../../core/android/SDL_android.h"
    34 
    35 #define ACTION_DOWN 0
    36 #define ACTION_UP 1
    37 #define ACTION_MOVE 2
    38 #define ACTION_CANCEL 3
    39 #define ACTION_OUTSIDE 4
    40 #define ACTION_POINTER_DOWN 5
    41 #define ACTION_POINTER_UP 6
    42 
    43 static void Android_GetWindowCoordinates(float x, float y,
    44                                          int *window_x, int *window_y)
    45 {
    46     int window_w, window_h;
    47 
    48     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    49     *window_x = (int)(x * window_w);
    50     *window_y = (int)(y * window_h);
    51 }
    52 
    53 void Android_InitTouch(void)
    54 {
    55     int i;
    56     int* ids;
    57     int number = Android_JNI_GetTouchDeviceIds(&ids);
    58     if (0 < number) {
    59         for (i = 0; i < number; ++i) {
    60             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
    61         }
    62         SDL_free(ids);
    63     }
    64 }
    65 
    66 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
    67 {
    68     SDL_TouchID touchDeviceId = 0;
    69     SDL_FingerID fingerId = 0;
    70     int window_x, window_y;
    71     const char * hint;
    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     hint = SDL_GetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH");
    85     switch (action) {
    86         case ACTION_DOWN:
    87             /* Primary pointer down */
    88             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    89             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
    90                 /* send moved event */
    91                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    92                 /* send mouse down event */
    93                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    94             }
    95             pointerFingerID = fingerId;
    96         case ACTION_POINTER_DOWN:
    97             /* Non primary pointer down */
    98             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    99             break;
   100 
   101         case ACTION_MOVE:
   102             if (!pointerFingerID) {
   103                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   104 
   105                 if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   106                     /* send moved event */
   107                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   108                 }
   109             }
   110             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   111             break;
   112 
   113         case ACTION_UP:
   114             /* Primary pointer up */
   115             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   116                 /* send mouse up */
   117                 pointerFingerID = (SDL_FingerID) 0;
   118                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   119             }
   120         case ACTION_POINTER_UP:
   121             /* Non primary pointer up */
   122             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   123             break;
   124 
   125         default:
   126             break;
   127     }
   128 }
   129 
   130 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   131 
   132 /* vi: set ts=4 sw=4 expandtab: */