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).
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"
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
slouken@6651
    43
static SDL_FingerID leftFingerDown = 0;
slouken@6651
    44
slouken@6651
    45
static void Android_GetWindowCoordinates(float x, float y,
slouken@6651
    46
                                         int *window_x, int *window_y)
slouken@6651
    47
{
slouken@6651
    48
    int window_w, window_h;
slouken@6651
    49
slouken@6651
    50
    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
slouken@6651
    51
    *window_x = (int)(x * window_w);
slouken@6651
    52
    *window_y = (int)(y * window_h);
slouken@6651
    53
}
slouken@6651
    54
icculus@5982
    55
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
slouken@5001
    56
{
icculus@5982
    57
    SDL_TouchID touchDeviceId = 0;
icculus@5982
    58
    SDL_FingerID fingerId = 0;
slouken@6651
    59
    int window_x, window_y;
slouken@6651
    60
slouken@5001
    61
    if (!Android_Window) {
slouken@5001
    62
        return;
slouken@5001
    63
    }
slouken@6651
    64
icculus@5982
    65
    touchDeviceId = (SDL_TouchID)touch_device_id_in;
slouken@7043
    66
    if (SDL_AddTouch(touchDeviceId, "") < 0) {
slouken@7043
    67
        SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@5982
    68
    }
slouken@5001
    69
icculus@5982
    70
    fingerId = (SDL_FingerID)pointer_finger_id_in;
icculus@5982
    71
    switch (action) {
slouken@5001
    72
        case ACTION_DOWN:
icculus@5982
    73
        case ACTION_POINTER_1_DOWN:
slouken@6651
    74
            if (!leftFingerDown) {
slouken@6651
    75
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
slouken@6651
    76
slouken@6651
    77
                /* send moved event */
slouken@6950
    78
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
slouken@6651
    79
slouken@6651
    80
                /* send mouse down event */
slouken@6950
    81
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@6651
    82
slouken@6651
    83
                leftFingerDown = fingerId;
slouken@6651
    84
            }
slouken@6951
    85
            SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
icculus@5982
    86
            break;
icculus@5982
    87
        case ACTION_MOVE:
slouken@6651
    88
            if (!leftFingerDown) {
slouken@6651
    89
                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
slouken@6651
    90
slouken@6651
    91
                /* send moved event */
slouken@6950
    92
                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
slouken@6651
    93
            }
slouken@6951
    94
            SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
slouken@5001
    95
            break;
slouken@5001
    96
        case ACTION_UP:
icculus@5982
    97
        case ACTION_POINTER_1_UP:
slouken@6651
    98
            if (fingerId == leftFingerDown) {
slouken@6651
    99
                /* send mouse up */
slouken@6950
   100
                SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@6651
   101
                leftFingerDown = 0;
slouken@6651
   102
            }
slouken@6951
   103
            SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
slouken@5001
   104
            break;
icculus@5982
   105
        default:
icculus@5982
   106
            break;
icculus@5982
   107
    } 
slouken@5001
   108
}
slouken@5001
   109
slouken@6044
   110
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   111
slouken@5001
   112
/* vi: set ts=4 sw=4 expandtab: */