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).
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 #include <android/log.h>
    26 
    27 #include "SDL_events.h"
    28 #include "../../events/SDL_mouse_c.h"
    29 #include "../../events/SDL_touch_c.h"
    30 
    31 #include "SDL_androidtouch.h"
    32 
    33 
    34 #define ACTION_DOWN 0
    35 #define ACTION_UP 1
    36 #define ACTION_MOVE 2
    37 #define ACTION_CANCEL 3
    38 #define ACTION_OUTSIDE 4
    39 // The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2
    40 #define ACTION_POINTER_1_DOWN 5
    41 #define ACTION_POINTER_1_UP 6
    42 
    43 static SDL_FingerID leftFingerDown = 0;
    44 
    45 static void Android_GetWindowCoordinates(float x, float y,
    46                                          int *window_x, int *window_y)
    47 {
    48     int window_w, window_h;
    49 
    50     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    51     *window_x = (int)(x * window_w);
    52     *window_y = (int)(y * window_h);
    53 }
    54 
    55 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
    56 {
    57     SDL_TouchID touchDeviceId = 0;
    58     SDL_FingerID fingerId = 0;
    59     int window_x, window_y;
    60 
    61     if (!Android_Window) {
    62         return;
    63     }
    64 
    65     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    66     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    67         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    68     }
    69 
    70     fingerId = (SDL_FingerID)pointer_finger_id_in;
    71     switch (action) {
    72         case ACTION_DOWN:
    73         case ACTION_POINTER_1_DOWN:
    74             if (!leftFingerDown) {
    75                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    76 
    77                 /* send moved event */
    78                 SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    79 
    80                 /* send mouse down event */
    81                 SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    82 
    83                 leftFingerDown = fingerId;
    84             }
    85             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    86             break;
    87         case ACTION_MOVE:
    88             if (!leftFingerDown) {
    89                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    90 
    91                 /* send moved event */
    92                 SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    93             }
    94             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
    95             break;
    96         case ACTION_UP:
    97         case ACTION_POINTER_1_UP:
    98             if (fingerId == leftFingerDown) {
    99                 /* send mouse up */
   100                 SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   101                 leftFingerDown = 0;
   102             }
   103             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   104             break;
   105         default:
   106             break;
   107     } 
   108 }
   109 
   110 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   111 
   112 /* vi: set ts=4 sw=4 expandtab: */