src/video/android/SDL_androidtouch.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 02 Apr 2013 07:51:23 -0700
changeset 7043 fb7b426ab660
parent 6951 7833f01322b3
child 7191 75360622e65f
permissions -rw-r--r--
Fixed bug 1781 - SDL sets an error for the initial touch on Android. Philipp Wiesemann SDL sets an error to be queried with SDL_GetError() for the initial touch on Android. Android_OnTouch() in SDL_androidtouch.c uses SDL_GetTouch() to check if a touch device was already added. SDL_GetTouch() sets the error "Unknown touch device" for an out of range access because touch devices are added after initial touch. I think this error is confusing because it always happens by design. I attached a patch which removes the call to SDL_GetTouch() and only uses SDL_AddTouch() which does the check (if already added) again and does not set an error (if not added yet).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6651
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5982
diff changeset
    23
#if SDL_VIDEO_DRIVER_ANDROID
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5982
diff changeset
    24
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include <android/log.h>
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "SDL_events.h"
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "../../events/SDL_mouse_c.h"
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    29
#include "../../events/SDL_touch_c.h"
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "SDL_androidtouch.h"
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#define ACTION_DOWN 0
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#define ACTION_UP 1
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#define ACTION_MOVE 2
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#define ACTION_CANCEL 3
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#define ACTION_OUTSIDE 4
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    39
// The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    40
#define ACTION_POINTER_1_DOWN 5
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    41
#define ACTION_POINTER_1_UP 6
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    43
static SDL_FingerID leftFingerDown = 0;
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    44
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    45
static void Android_GetWindowCoordinates(float x, float y,
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    46
                                         int *window_x, int *window_y)
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    47
{
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    48
    int window_w, window_h;
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    49
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    50
    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    51
    *window_x = (int)(x * window_w);
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    52
    *window_y = (int)(y * window_h);
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    53
}
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    54
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    55
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    57
    SDL_TouchID touchDeviceId = 0;
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    58
    SDL_FingerID fingerId = 0;
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    59
    int window_x, window_y;
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    60
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
    if (!Android_Window) {
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
        return;
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    }
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    64
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    65
    touchDeviceId = (SDL_TouchID)touch_device_id_in;
7043
fb7b426ab660 Fixed bug 1781 - SDL sets an error for the initial touch on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6951
diff changeset
    66
    if (SDL_AddTouch(touchDeviceId, "") < 0) {
fb7b426ab660 Fixed bug 1781 - SDL sets an error for the initial touch on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6951
diff changeset
    67
        SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    68
    }
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    70
    fingerId = (SDL_FingerID)pointer_finger_id_in;
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    71
    switch (action) {
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
        case ACTION_DOWN:
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    73
        case ACTION_POINTER_1_DOWN:
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    74
            if (!leftFingerDown) {
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    75
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    76
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    77
                /* send moved event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    78
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    79
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    80
                /* send mouse down event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    81
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    82
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    83
                leftFingerDown = fingerId;
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    84
            }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
    85
            SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    86
            break;
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    87
        case ACTION_MOVE:
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    88
            if (!leftFingerDown) {
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    89
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    90
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    91
                /* send moved event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    92
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    93
            }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
    94
            SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
            break;
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
        case ACTION_UP:
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    97
        case ACTION_POINTER_1_UP:
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    98
            if (fingerId == leftFingerDown) {
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
    99
                /* send mouse up */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   100
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
6651
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
   101
                leftFingerDown = 0;
17ef8a7cab55 Added mouse emulation for touch events on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6400
diff changeset
   102
            }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   103
            SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
            break;
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   105
        default:
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   106
            break;
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   107
    } 
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
}
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5982
diff changeset
   110
#endif /* SDL_VIDEO_DRIVER_ANDROID */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5982
diff changeset
   111
5001
77df56570442 Added "mouse" support for the Android touch screen
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
/* vi: set ts=4 sw=4 expandtab: */