src/video/android/SDL_androidtouch.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 8030 7b0d28c202d2
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

The new header will include SDL_config.h, but allows for other global stuff.
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
*/
icculus@8093
    21
#include "../../SDL_internal.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
gabomdq@8030
    41
#define ACTION_POINTER_DOWN 5
gabomdq@8030
    42
#define ACTION_POINTER_UP 6
slouken@6651
    43
slouken@6651
    44
static void Android_GetWindowCoordinates(float x, float y,
slouken@6651
    45
                                         int *window_x, int *window_y)
slouken@6651
    46
{
slouken@6651
    47
    int window_w, window_h;
slouken@6651
    48
slouken@6651
    49
    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
slouken@6651
    50
    *window_x = (int)(x * window_w);
slouken@6651
    51
    *window_y = (int)(y * window_h);
slouken@6651
    52
}
slouken@6651
    53
philipp@7786
    54
void Android_InitTouch(void)
philipp@7786
    55
{
philipp@7786
    56
    int i;
philipp@7786
    57
    int* ids;
philipp@7786
    58
    int number = Android_JNI_GetTouchDeviceIds(&ids);
philipp@7786
    59
    if (0 < number) {
philipp@7786
    60
        for (i = 0; i < number; ++i) {
philipp@7786
    61
            SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
philipp@7786
    62
        }
philipp@7786
    63
        SDL_free(ids);
philipp@7786
    64
    }
philipp@7786
    65
}
philipp@7786
    66
slouken@7191
    67
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
slouken@5001
    68
{
icculus@5982
    69
    SDL_TouchID touchDeviceId = 0;
icculus@5982
    70
    SDL_FingerID fingerId = 0;
slouken@6651
    71
    int window_x, window_y;
gabomdq@8030
    72
    static SDL_FingerID pointerFingerID = 0;
slouken@6651
    73
slouken@5001
    74
    if (!Android_Window) {
slouken@5001
    75
        return;
slouken@5001
    76
    }
slouken@6651
    77
icculus@5982
    78
    touchDeviceId = (SDL_TouchID)touch_device_id_in;
slouken@7043
    79
    if (SDL_AddTouch(touchDeviceId, "") < 0) {
slouken@7043
    80
        SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@5982
    81
    }
slouken@5001
    82
icculus@5982
    83
    fingerId = (SDL_FingerID)pointer_finger_id_in;
icculus@5982
    84
    switch (action) {
slouken@5001
    85
        case ACTION_DOWN:
gabomdq@8030
    86
            /* Primary pointer down */
gabomdq@8030
    87
            Android_GetWindowCoordinates(x, y, &window_x, &window_y);
gabomdq@8030
    88
            /* send moved event */
gabomdq@8030
    89
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
gabomdq@8030
    90
            /* send mouse down event */
gabomdq@8030
    91
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
gabomdq@8030
    92
            pointerFingerID = fingerId;
gabomdq@8030
    93
        case ACTION_POINTER_DOWN:
gabomdq@8030
    94
            /* Non primary pointer down */
slouken@6951
    95
            SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
icculus@5982
    96
            break;
gabomdq@8030
    97
            
icculus@5982
    98
        case ACTION_MOVE:
gabomdq@8030
    99
            if (!pointerFingerID) {
slouken@6651
   100
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
slouken@6651
   101
slouken@6651
   102
                /* send moved event */
slouken@6950
   103
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
slouken@6651
   104
            }
slouken@6951
   105
            SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
slouken@5001
   106
            break;
gabomdq@8030
   107
            
slouken@5001
   108
        case ACTION_UP:
gabomdq@8030
   109
            /* Primary pointer up */
gabomdq@8030
   110
            /* send mouse up */
gabomdq@8030
   111
            pointerFingerID = (SDL_FingerID) 0;
gabomdq@8030
   112
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
gabomdq@8030
   113
        case ACTION_POINTER_UP:
gabomdq@8030
   114
            /* Non primary pointer up */
slouken@6951
   115
            SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
slouken@5001
   116
            break;
gabomdq@8030
   117
            
icculus@5982
   118
        default:
icculus@5982
   119
            break;
slouken@7191
   120
    }
slouken@5001
   121
}
slouken@5001
   122
slouken@6044
   123
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   124
slouken@5001
   125
/* vi: set ts=4 sw=4 expandtab: */