src/events/SDL_touch.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 01 May 2015 01:12:48 -0400
changeset 9580 d37ef6990bf9
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
checkkeys: Readded the KEYUP event test.
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
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 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;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   148
    index = SDL_num_touch++;
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
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   155
    /* we're setting the touch properties */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   156
    SDL_touchDevices[index]->id = touchID;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   157
    SDL_touchDevices[index]->num_fingers = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   158
    SDL_touchDevices[index]->max_fingers = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   159
    SDL_touchDevices[index]->fingers = NULL;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   160
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   161
    /* Record this touch device for gestures */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   162
    /* 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
   163
    SDL_GestureAddTouch(touchID);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   164
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   165
    return index;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   166
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   167
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   168
static int
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   169
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
   170
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   171
    SDL_Finger *finger;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   172
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   173
    if (touch->num_fingers == touch->max_fingers) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   174
        SDL_Finger **new_fingers;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   175
        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
   176
        if (!new_fingers) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   177
            return SDL_OutOfMemory();
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   178
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   179
        touch->fingers = new_fingers;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   180
        touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   181
        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
   182
            return SDL_OutOfMemory();
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   183
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   184
        touch->max_fingers++;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   185
    }
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
   186
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   187
    finger = touch->fingers[touch->num_fingers++];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   188
    finger->id = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   189
    finger->x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   190
    finger->y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   191
    finger->pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   192
    return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   193
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   194
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   195
static int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   196
SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
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
    SDL_Finger *temp;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   199
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   200
    int index = SDL_GetFingerIndex(touch, fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   201
    if (index < 0) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   202
        return -1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   203
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   204
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   205
    touch->num_fingers--;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   206
    temp = touch->fingers[index];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   207
    touch->fingers[index] = touch->fingers[touch->num_fingers];
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   208
    touch->fingers[touch->num_fingers] = temp;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   209
    return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   210
}
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   211
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   212
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   213
SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   214
              SDL_bool down, float x, float y, float pressure)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   215
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   216
    int posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   217
    SDL_Finger *finger;
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
    SDL_Touch* touch = SDL_GetTouch(id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   220
    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
   221
        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
   222
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   223
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   224
    finger = SDL_GetFinger(touch, fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   225
    if (down) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   226
        if (finger) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   227
            /* This finger is already down */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   228
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   229
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   230
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   231
        if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   232
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   233
        }
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
   234
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   235
        posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   236
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   237
            SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   238
            event.tfinger.type = SDL_FINGERDOWN;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   239
            event.tfinger.touchId = id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   240
            event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   241
            event.tfinger.x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   242
            event.tfinger.y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   243
            event.tfinger.dx = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   244
            event.tfinger.dy = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   245
            event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   246
            posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   247
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   248
    } else {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   249
        if (!finger) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   250
            /* This finger is already up */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   251
            return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   252
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   253
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   254
        posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   255
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   256
            SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   257
            event.tfinger.type = SDL_FINGERUP;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   258
            event.tfinger.touchId =  id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   259
            event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   260
            /* 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
   261
            event.tfinger.x = finger->x;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   262
            event.tfinger.y = finger->y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   263
            event.tfinger.dx = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   264
            event.tfinger.dy = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   265
            event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   266
            posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   267
        }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   268
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   269
        SDL_DelFinger(touch, fingerid);
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
    return posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   272
}
4642
057e8762d2a1 Added reading of event* for touch events.
Jim Grandpre <jim.tla@gmail.com>
parents: 4641
diff changeset
   273
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   274
int
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   275
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   276
                    float x, float y, float pressure)
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   277
{
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   278
    SDL_Touch *touch;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   279
    SDL_Finger *finger;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   280
    int posted;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   281
    float xrel, yrel, prel;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   282
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   283
    touch = SDL_GetTouch(id);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   284
    if (!touch) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   285
        return -1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   286
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   287
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   288
    finger = SDL_GetFinger(touch,fingerid);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   289
    if (!finger) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   290
        return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   291
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   292
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   293
    xrel = x - finger->x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   294
    yrel = y - finger->y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   295
    prel = pressure - finger->pressure;
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents: 4655
diff changeset
   296
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   297
    /* Drop events that don't change state */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   298
    if (!xrel && !yrel && !prel) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   299
#if 0
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   300
        printf("Touch event didn't change state - dropped!\n");
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   301
#endif
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   302
        return 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   303
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   304
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   305
    /* Update internal touch coordinates */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   306
    finger->x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   307
    finger->y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   308
    finger->pressure = pressure;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   309
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   310
    /* Post the event, if desired */
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   311
    posted = 0;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   312
    if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   313
        SDL_Event event;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   314
        event.tfinger.type = SDL_FINGERMOTION;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   315
        event.tfinger.touchId = id;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   316
        event.tfinger.fingerId = fingerid;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   317
        event.tfinger.x = x;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   318
        event.tfinger.y = y;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   319
        event.tfinger.dx = xrel;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7170
diff changeset
   320
        event.tfinger.dy = yrel;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   321
        event.tfinger.pressure = pressure;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   322
        posted = (SDL_PushEvent(&event) > 0);
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   323
    }
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   324
    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
   325
}
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
   326
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
   327
void
4678
f8431f66613d Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
jimtla
parents: 4676
diff changeset
   328
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
   329
{
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   330
    int i;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   331
    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
   332
    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
   333
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
   334
    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
   335
        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
   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
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   338
    for (i = 0; i < touch->max_fingers; ++i) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   339
        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
   340
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   341
    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
   342
    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
   343
4641
49a97daea6ec Added touch event definitions. Heavily modified events/SDL_touch*.
Jim Grandpre <jim.tla@gmail.com>
parents: 4640
diff changeset
   344
    SDL_num_touch--;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   345
    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
   346
}
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
   347
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
   348
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
   349
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
   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
    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
   352
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   353
    for (i = SDL_num_touch; i--; ) {
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   354
        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
   355
    }
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   356
    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
   357
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   358
    SDL_free(SDL_touchDevices);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   359
    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
   360
}
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
   361
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
   362
/* vi: set ts=4 sw=4 expandtab: */