src/video/android/SDL_androidtouch.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5982 f324bd81b52c
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@5001
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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"
slouken@5001
    30
slouken@5001
    31
#include "SDL_androidtouch.h"
slouken@5001
    32
slouken@5001
    33
slouken@5001
    34
#define ACTION_DOWN 0
slouken@5001
    35
#define ACTION_UP 1
slouken@5001
    36
#define ACTION_MOVE 2
slouken@5001
    37
#define ACTION_CANCEL 3
slouken@5001
    38
#define ACTION_OUTSIDE 4
icculus@5982
    39
// 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
    40
#define ACTION_POINTER_1_DOWN 5
icculus@5982
    41
#define ACTION_POINTER_1_UP 6
slouken@5001
    42
icculus@5982
    43
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
slouken@5001
    44
{
icculus@5982
    45
    SDL_TouchID touchDeviceId = 0;
icculus@5982
    46
    SDL_FingerID fingerId = 0;
icculus@5982
    47
    
slouken@5001
    48
    if (!Android_Window) {
slouken@5001
    49
        return;
slouken@5001
    50
    }
icculus@5982
    51
    
icculus@5982
    52
    touchDeviceId = (SDL_TouchID)touch_device_id_in;
icculus@5982
    53
    if (!SDL_GetTouch(touchDeviceId)) {
icculus@5982
    54
        SDL_Touch touch;
icculus@5982
    55
        memset( &touch, 0, sizeof(touch) );
icculus@5982
    56
        touch.id = touchDeviceId;
icculus@5982
    57
        touch.x_min = 0.0f;
icculus@5982
    58
        touch.x_max = (float)Android_ScreenWidth;
icculus@5982
    59
        touch.native_xres = touch.x_max - touch.x_min;
icculus@5982
    60
        touch.y_min = 0.0f;
icculus@5982
    61
        touch.y_max = (float)Android_ScreenHeight;
icculus@5982
    62
        touch.native_yres = touch.y_max - touch.y_min;
icculus@5982
    63
        touch.pressure_min = 0.0f;
icculus@5982
    64
        touch.pressure_max = 1.0f;
icculus@5982
    65
        touch.native_pressureres = touch.pressure_max - touch.pressure_min;
icculus@5982
    66
        if (SDL_AddTouch(&touch, "") < 0) {
icculus@5982
    67
             SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@5982
    68
        }
icculus@5982
    69
    }
slouken@5001
    70
icculus@5982
    71
    
icculus@5982
    72
    fingerId = (SDL_FingerID)pointer_finger_id_in;
icculus@5982
    73
    switch (action) {
slouken@5001
    74
        case ACTION_DOWN:
icculus@5982
    75
        case ACTION_POINTER_1_DOWN:
icculus@5982
    76
            SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
icculus@5982
    77
            break;
icculus@5982
    78
        case ACTION_MOVE:
icculus@5982
    79
            SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
slouken@5001
    80
            break;
slouken@5001
    81
        case ACTION_UP:
icculus@5982
    82
        case ACTION_POINTER_1_UP:
icculus@5982
    83
            SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
slouken@5001
    84
            break;
icculus@5982
    85
        default:
icculus@5982
    86
            break;
icculus@5982
    87
    } 
slouken@5001
    88
}
slouken@5001
    89
slouken@6044
    90
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
    91
slouken@5001
    92
/* vi: set ts=4 sw=4 expandtab: */