src/video/android/SDL_androidtouch.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 05 Oct 2013 17:08:19 +0200
changeset 7786 cfc1285375e1
parent 7261 df37802bf7db
child 8030 7b0d28c202d2
permissions -rw-r--r--
Added detection of touch devices before first touch events happen on Android.

On Android available touch devices are now added with video initialization (like
the keyboard). This fixes SDL_GetNumTouchDevices() returning 0 before any touch
events happened although there is a touch screen available. The adding of touch
devices after a touch event was received is still active to allow connecting
devices later (if this is possible) and to provide a fallback if the new init
did not work somehow. For the implementation JNI was used and API level 9 is
required. There seems to be nothing in the Android NDK's input header (input.h)
to implement everything on C side without communication with Java side.
slouken@5001
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@5001
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5001
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5001
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5001
    20
*/
slouken@5001
    21
#include "SDL_config.h"
slouken@5001
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_ANDROID
slouken@6044
    24
slouken@5001
    25
#include <android/log.h>
slouken@5001
    26
slouken@5001
    27
#include "SDL_events.h"
slouken@5001
    28
#include "../../events/SDL_mouse_c.h"
icculus@5982
    29
#include "../../events/SDL_touch_c.h"
philipp@7261
    30
#include "SDL_log.h"
slouken@5001
    31
slouken@5001
    32
#include "SDL_androidtouch.h"
slouken@5001
    33
philipp@7786
    34
#include "../../core/android/SDL_android.h"
slouken@5001
    35
slouken@5001
    36
#define ACTION_DOWN 0
slouken@5001
    37
#define ACTION_UP 1
slouken@5001
    38
#define ACTION_MOVE 2
slouken@5001
    39
#define ACTION_CANCEL 3
slouken@5001
    40
#define ACTION_OUTSIDE 4
slouken@7191
    41
/* The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2 */
icculus@5982
    42
#define ACTION_POINTER_1_DOWN 5
icculus@5982
    43
#define ACTION_POINTER_1_UP 6
slouken@5001
    44
slouken@6651
    45
static SDL_FingerID leftFingerDown = 0;
slouken@6651
    46
slouken@6651
    47
static void Android_GetWindowCoordinates(float x, float y,
slouken@6651
    48
                                         int *window_x, int *window_y)
slouken@6651
    49
{
slouken@6651
    50
    int window_w, window_h;
slouken@6651
    51
slouken@6651
    52
    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
slouken@6651
    53
    *window_x = (int)(x * window_w);
slouken@6651
    54
    *window_y = (int)(y * window_h);
slouken@6651
    55
}
slouken@6651
    56
philipp@7786
    57
void Android_InitTouch(void)
philipp@7786
    58
{
philipp@7786
    59
    int i;
philipp@7786
    60
    int* ids;
philipp@7786
    61
    int number = Android_JNI_GetTouchDeviceIds(&ids);
philipp@7786
    62
    if (0 < number) {
philipp@7786
    63
        for (i = 0; i < number; ++i) {
philipp@7786
    64
            SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
philipp@7786
    65
        }
philipp@7786
    66
        SDL_free(ids);
philipp@7786
    67
    }
philipp@7786
    68
}
philipp@7786
    69
slouken@7191
    70
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
slouken@5001
    71
{
icculus@5982
    72
    SDL_TouchID touchDeviceId = 0;
icculus@5982
    73
    SDL_FingerID fingerId = 0;
slouken@6651
    74
    int window_x, window_y;
slouken@6651
    75
slouken@5001
    76
    if (!Android_Window) {
slouken@5001
    77
        return;
slouken@5001
    78
    }
slouken@6651
    79
icculus@5982
    80
    touchDeviceId = (SDL_TouchID)touch_device_id_in;
slouken@7043
    81
    if (SDL_AddTouch(touchDeviceId, "") < 0) {
slouken@7043
    82
        SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@5982
    83
    }
slouken@5001
    84
icculus@5982
    85
    fingerId = (SDL_FingerID)pointer_finger_id_in;
icculus@5982
    86
    switch (action) {
slouken@5001
    87
        case ACTION_DOWN:
icculus@5982
    88
        case ACTION_POINTER_1_DOWN:
slouken@6651
    89
            if (!leftFingerDown) {
slouken@6651
    90
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
slouken@6651
    91
slouken@6651
    92
                /* send moved event */
slouken@6950
    93
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
slouken@6651
    94
slouken@6651
    95
                /* send mouse down event */
slouken@6950
    96
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@6651
    97
slouken@6651
    98
                leftFingerDown = fingerId;
slouken@6651
    99
            }
slouken@6951
   100
            SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
icculus@5982
   101
            break;
icculus@5982
   102
        case ACTION_MOVE:
slouken@6651
   103
            if (!leftFingerDown) {
slouken@6651
   104
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
slouken@6651
   105
slouken@6651
   106
                /* send moved event */
slouken@6950
   107
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
slouken@6651
   108
            }
slouken@6951
   109
            SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
slouken@5001
   110
            break;
slouken@5001
   111
        case ACTION_UP:
icculus@5982
   112
        case ACTION_POINTER_1_UP:
slouken@6651
   113
            if (fingerId == leftFingerDown) {
slouken@6651
   114
                /* send mouse up */
slouken@6950
   115
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@6651
   116
                leftFingerDown = 0;
slouken@6651
   117
            }
slouken@6951
   118
            SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
slouken@5001
   119
            break;
icculus@5982
   120
        default:
icculus@5982
   121
            break;
slouken@7191
   122
    }
slouken@5001
   123
}
slouken@5001
   124
slouken@6044
   125
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   126
slouken@5001
   127
/* vi: set ts=4 sw=4 expandtab: */