src/video/android/SDL_androidtouch.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 25 Mar 2015 10:59:10 -0400
changeset 9441 fc7522030729
parent 9439 0d01b53cdbea
child 9449 a65088766e35
permissions -rw-r--r--
Add a hint watch callback for SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH.
     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 static volatile SDL_bool separate_mouse_and_touch = SDL_FALSE;
    54 
    55 static void
    56 SeparateEventsHintWatcher(void *userdata, const char *name,
    57                           const char *oldValue, const char *newValue)
    58 {
    59     separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
    60 }
    61 
    62 void Android_InitTouch(void)
    63 {
    64     int i;
    65     int* ids;
    66     const int number = Android_JNI_GetTouchDeviceIds(&ids);
    67 
    68     SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
    69                         SeparateEventsHintWatcher, NULL);
    70 
    71     if (0 < number) {
    72         for (i = 0; i < number; ++i) {
    73             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
    74         }
    75         SDL_free(ids);
    76     }
    77 }
    78 
    79 void Android_QuitTouch(void)
    80 {
    81     SDL_DelHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
    82                         SeparateEventsHintWatcher, NULL);
    83     separate_mouse_and_touch = SDL_FALSE;
    84 }
    85 
    86 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
    87 {
    88     SDL_TouchID touchDeviceId = 0;
    89     SDL_FingerID fingerId = 0;
    90     int window_x, window_y;
    91     static SDL_FingerID pointerFingerID = 0;
    92 
    93     if (!Android_Window) {
    94         return;
    95     }
    96 
    97     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    98     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    99         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
   100     }
   101 
   102     fingerId = (SDL_FingerID)pointer_finger_id_in;
   103     switch (action) {
   104         case ACTION_DOWN:
   105             /* Primary pointer down */
   106             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   107             if (!separate_mouse_and_touch) {
   108                 /* send moved event */
   109                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   110                 /* send mouse down event */
   111                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
   112             }
   113             pointerFingerID = fingerId;
   114         case ACTION_POINTER_DOWN:
   115             /* Non primary pointer down */
   116             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
   117             break;
   118 
   119         case ACTION_MOVE:
   120             if (!pointerFingerID) {
   121                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   122                 if (!separate_mouse_and_touch) {
   123                     /* send moved event */
   124                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   125                 }
   126             }
   127             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   128             break;
   129 
   130         case ACTION_UP:
   131             /* Primary pointer up */
   132             if (!separate_mouse_and_touch) {
   133                 /* send mouse up */
   134                 pointerFingerID = (SDL_FingerID) 0;
   135                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   136             }
   137         case ACTION_POINTER_UP:
   138             /* Non primary pointer up */
   139             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   140             break;
   141 
   142         default:
   143             break;
   144     }
   145 }
   146 
   147 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   148 
   149 /* vi: set ts=4 sw=4 expandtab: */