src/events/SDL_touch.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5455
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9730
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5455
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: 5455
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: 5455
diff changeset
     7
  arising from the use of this software.
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5455
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: 5455
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: 5455
diff changeset
    11
  freely, subject to the following restrictions:
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5455
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: 5455
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: 5455
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: 5455
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5455
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: 5455
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: 5455
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7719
diff changeset
    21
#include "../SDL_internal.h"
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    22
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    23
/* General touch handling code for SDL */
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    24
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    25
#include "SDL_assert.h"
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    26
#include "SDL_events.h"
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    27
#include "SDL_events_c.h"
4648
0350b634c044 Fixed windo build errors. Should now be compilable.
jimtla
parents: 4646
diff changeset
    28
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    29
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    30
static int SDL_num_touch = 0;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    31
static SDL_Touch **SDL_touchDevices = NULL;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    32
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    33
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    34
/* Public functions */
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    35
int
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    36
SDL_TouchInit(void)
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    37
{
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    38
    return (0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    39
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    40
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    41
int
7170
d5f5a491fe49 Fixed bug 1845 - SDL_GetNumTouchDevices() has incorrect prototype
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
    42
SDL_GetNumTouchDevices(void)
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    43
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    44
    return SDL_num_touch;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    45
}
4642
057e8762d2a1 Added reading of event* for touch events.
Jim Grandpre <jim.tla@gmail.com>
parents: 4641
diff changeset
    46
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    47
SDL_TouchID
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    48
SDL_GetTouchDevice(int index)
4641
49a97daea6ec Added touch event definitions. Heavily modified events/SDL_touch*.
Jim Grandpre <jim.tla@gmail.com>
parents: 4640
diff changeset
    49
{
4642
057e8762d2a1 Added reading of event* for touch events.
Jim Grandpre <jim.tla@gmail.com>
parents: 4641
diff changeset
    50
    if (index < 0 || index >= SDL_num_touch) {
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    51
        SDL_SetError("Unknown touch device");
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    52
        return 0;
4642
057e8762d2a1 Added reading of event* for touch events.
Jim Grandpre <jim.tla@gmail.com>
parents: 4641
diff changeset
    53
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    54
    return SDL_touchDevices[index]->id;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    55
}
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    56
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    57
static int
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    58
SDL_GetTouchIndex(SDL_TouchID id)
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    59
{
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    60
    int index;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    61
    SDL_Touch *touch;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    62
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    63
    for (index = 0; index < SDL_num_touch; ++index) {
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    64
        touch = SDL_touchDevices[index];
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    65
        if (touch->id == id) {
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    66
            return index;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    67
        }
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    68
    }
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    69
    return -1;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    70
}
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    71
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    72
SDL_Touch *
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    73
SDL_GetTouch(SDL_TouchID id)
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
    74
{
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    75
    int index = SDL_GetTouchIndex(id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    76
    if (index < 0 || index >= SDL_num_touch) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    77
        SDL_SetError("Unknown touch device");
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    78
        return NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    79
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    80
    return SDL_touchDevices[index];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    81
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    82
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    83
static int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    84
SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    85
{
5076
8bf5781fc582 Fixed size_t warnings on 64-bit build
Sam Lantinga <slouken@libsdl.org>
parents: 4919
diff changeset
    86
    int index;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    87
    for (index = 0; index < touch->num_fingers; ++index) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    88
        if (touch->fingers[index]->id == fingerid) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    89
            return index;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    90
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    91
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    92
    return -1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    93
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    94
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    95
SDL_Finger *
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    96
SDL_GetFinger(const SDL_Touch * touch, SDL_FingerID id)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    97
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    98
    int index = SDL_GetFingerIndex(touch, id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    99
    if (index < 0 || index >= touch->num_fingers) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   100
        return NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   101
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   102
    return touch->fingers[index];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   103
}
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   104
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   105
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   106
SDL_GetNumTouchFingers(SDL_TouchID touchID)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   107
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   108
    SDL_Touch *touch = SDL_GetTouch(touchID);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   109
    if (touch) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   110
        return touch->num_fingers;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   111
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   112
    return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   113
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   114
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   115
SDL_Finger *
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   116
SDL_GetTouchFinger(SDL_TouchID touchID, int index)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   117
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   118
    SDL_Touch *touch = SDL_GetTouch(touchID);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   119
    if (!touch) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   120
        return NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   121
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   122
    if (index < 0 || index >= touch->num_fingers) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   123
        SDL_SetError("Unknown touch finger");
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   124
        return NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   125
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   126
    return touch->fingers[index];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   127
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   128
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   129
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   130
SDL_AddTouch(SDL_TouchID touchID, const char *name)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   131
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   132
    SDL_Touch **touchDevices;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   133
    int index;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   134
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   135
    index = SDL_GetTouchIndex(touchID);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   136
    if (index >= 0) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   137
        return index;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   138
    }
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   139
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   140
    /* Add the touch to the list of touch */
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   141
    touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   142
                                      (SDL_num_touch + 1) * sizeof(*touchDevices));
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   143
    if (!touchDevices) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   144
        return SDL_OutOfMemory();
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   145
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   146
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   147
    SDL_touchDevices = touchDevices;
9730
3136a8c1cf0b Fixed crash if allocation for touch device failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   148
    index = SDL_num_touch;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   149
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   150
    SDL_touchDevices[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchDevices[index]));
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   151
    if (!SDL_touchDevices[index]) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   152
        return SDL_OutOfMemory();
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   153
    }
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   154
9730
3136a8c1cf0b Fixed crash if allocation for touch device failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   155
    /* Added touch to list */
3136a8c1cf0b Fixed crash if allocation for touch device failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   156
    ++SDL_num_touch;
3136a8c1cf0b Fixed crash if allocation for touch device failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   157
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   158
    /* we're setting the touch properties */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   159
    SDL_touchDevices[index]->id = touchID;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   160
    SDL_touchDevices[index]->num_fingers = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   161
    SDL_touchDevices[index]->max_fingers = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   162
    SDL_touchDevices[index]->fingers = NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   163
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   164
    /* Record this touch device for gestures */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   165
    /* We could do this on the fly in the gesture code if we wanted */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   166
    SDL_GestureAddTouch(touchID);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   167
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   168
    return index;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   169
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   170
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   171
static int
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   172
SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   173
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   174
    SDL_Finger *finger;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   175
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   176
    if (touch->num_fingers == touch->max_fingers) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   177
        SDL_Finger **new_fingers;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   178
        new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers+1)*sizeof(*touch->fingers));
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   179
        if (!new_fingers) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   180
            return SDL_OutOfMemory();
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   181
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   182
        touch->fingers = new_fingers;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   183
        touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   184
        if (!touch->fingers[touch->max_fingers]) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   185
            return SDL_OutOfMemory();
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   186
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   187
        touch->max_fingers++;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   188
    }
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   189
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   190
    finger = touch->fingers[touch->num_fingers++];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   191
    finger->id = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   192
    finger->x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   193
    finger->y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   194
    finger->pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   195
    return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   196
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   197
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   198
static int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   199
SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   200
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   201
    SDL_Finger *temp;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   202
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   203
    int index = SDL_GetFingerIndex(touch, fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   204
    if (index < 0) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   205
        return -1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   206
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   207
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   208
    touch->num_fingers--;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   209
    temp = touch->fingers[index];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   210
    touch->fingers[index] = touch->fingers[touch->num_fingers];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   211
    touch->fingers[touch->num_fingers] = temp;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   212
    return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   213
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   214
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   215
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   216
SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   217
              SDL_bool down, float x, float y, float pressure)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   218
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   219
    int posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   220
    SDL_Finger *finger;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   221
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   222
    SDL_Touch* touch = SDL_GetTouch(id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   223
    if (!touch) {
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   224
        return -1;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   225
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   226
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   227
    finger = SDL_GetFinger(touch, fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   228
    if (down) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   229
        if (finger) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   230
            /* This finger is already down */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   231
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   232
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   233
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   234
        if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   235
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   236
        }
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   237
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   238
        posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   239
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   240
            SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   241
            event.tfinger.type = SDL_FINGERDOWN;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   242
            event.tfinger.touchId = id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   243
            event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   244
            event.tfinger.x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   245
            event.tfinger.y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   246
            event.tfinger.dx = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   247
            event.tfinger.dy = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   248
            event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   249
            posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   250
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   251
    } else {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   252
        if (!finger) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   253
            /* This finger is already up */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   254
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   255
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   256
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   257
        posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   258
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   259
            SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   260
            event.tfinger.type = SDL_FINGERUP;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   261
            event.tfinger.touchId =  id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   262
            event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   263
            /* I don't trust the coordinates passed on fingerUp */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   264
            event.tfinger.x = finger->x;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   265
            event.tfinger.y = finger->y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   266
            event.tfinger.dx = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   267
            event.tfinger.dy = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   268
            event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   269
            posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   270
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   271
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   272
        SDL_DelFinger(touch, fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   273
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   274
    return posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   275
}
4642
057e8762d2a1 Added reading of event* for touch events.
Jim Grandpre <jim.tla@gmail.com>
parents: 4641
diff changeset
   276
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   277
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   278
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   279
                    float x, float y, float pressure)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   280
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   281
    SDL_Touch *touch;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   282
    SDL_Finger *finger;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   283
    int posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   284
    float xrel, yrel, prel;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   285
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   286
    touch = SDL_GetTouch(id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   287
    if (!touch) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   288
        return -1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   289
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   290
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   291
    finger = SDL_GetFinger(touch,fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   292
    if (!finger) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   293
        return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   294
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   295
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   296
    xrel = x - finger->x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   297
    yrel = y - finger->y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   298
    prel = pressure - finger->pressure;
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents: 4655
diff changeset
   299
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   300
    /* Drop events that don't change state */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   301
    if (!xrel && !yrel && !prel) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   302
#if 0
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   303
        printf("Touch event didn't change state - dropped!\n");
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   304
#endif
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   305
        return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   306
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   307
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   308
    /* Update internal touch coordinates */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   309
    finger->x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   310
    finger->y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   311
    finger->pressure = pressure;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   312
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   313
    /* Post the event, if desired */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   314
    posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   315
    if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   316
        SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   317
        event.tfinger.type = SDL_FINGERMOTION;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   318
        event.tfinger.touchId = id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   319
        event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   320
        event.tfinger.x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   321
        event.tfinger.y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   322
        event.tfinger.dx = xrel;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   323
        event.tfinger.dy = yrel;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   324
        event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   325
        posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   326
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   327
    return posted;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   328
}
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   329
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   330
void
4678
f8431f66613d Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
jimtla
parents: 4676
diff changeset
   331
SDL_DelTouch(SDL_TouchID id)
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   332
{
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   333
    int i;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   334
    int index = SDL_GetTouchIndex(id);
4641
49a97daea6ec Added touch event definitions. Heavily modified events/SDL_touch*.
Jim Grandpre <jim.tla@gmail.com>
parents: 4640
diff changeset
   335
    SDL_Touch *touch = SDL_GetTouch(id);
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   336
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   337
    if (!touch) {
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   338
        return;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   339
    }
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   340
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   341
    for (i = 0; i < touch->max_fingers; ++i) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   342
        SDL_free(touch->fingers[i]);
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   343
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   344
    SDL_free(touch->fingers);
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   345
    SDL_free(touch);
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   346
4641
49a97daea6ec Added touch event definitions. Heavily modified events/SDL_touch*.
Jim Grandpre <jim.tla@gmail.com>
parents: 4640
diff changeset
   347
    SDL_num_touch--;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   348
    SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   349
}
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   350
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   351
void
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   352
SDL_TouchQuit(void)
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   353
{
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   354
    int i;
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   355
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   356
    for (i = SDL_num_touch; i--; ) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   357
        SDL_DelTouch(SDL_touchDevices[i]->id);
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   358
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   359
    SDL_assert(SDL_num_touch == 0);
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   360
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   361
    SDL_free(SDL_touchDevices);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   362
    SDL_touchDevices = NULL;
4640
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   363
}
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   364
f068a6dfc858 Added SDL_touch.c/SDL_touch_c.h as slightly modifeind SDL_mouse*. Made reads in touchSimp non-blocking.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
   365
/* vi: set ts=4 sw=4 expandtab: */