src/haptic/linux/SDL_syshaptic.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2015 13:11:19 -0500
changeset 9360 c671d8ca48a1
parent 9266 f6355bfca853
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Remove unnecessary parentheses and an unnecessary free() of a NULL pointer. (Thanks, Simon!) Fixes Bugzilla #2881.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 4514
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>
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 4514
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: 4514
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: 4514
diff changeset
     7
  arising from the use of this software.
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 4514
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: 4514
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: 4514
diff changeset
    11
  freely, subject to the following restrictions:
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 4514
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: 4514
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: 4514
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: 4514
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 4514
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: 4514
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: 4514
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
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: 7708
diff changeset
    21
#include "../../SDL_internal.h"
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#ifdef SDL_HAPTIC_LINUX
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    25
#include "SDL_assert.h"
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include "SDL_haptic.h"
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "../SDL_syshaptic.h"
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "SDL_joystick.h"
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include "../../joystick/linux/SDL_sysjoystick_c.h"     /* For joystick hwdata */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    31
#include "../../core/linux/SDL_udev.h"
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#include <unistd.h>             /* close */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#include <linux/input.h>        /* Force feedback linux stuff. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#include <fcntl.h>              /* O_RDWR */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#include <limits.h>             /* INT_MAX */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#include <errno.h>              /* errno, strerror */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#include <math.h>               /* atan2 */
3218
81773a1eac83 Patch from David Carre:
Sam Lantinga <slouken@libsdl.org>
parents: 3080
diff changeset
    39
#include <sys/stat.h>           /* stat */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
/* Just in case. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
#ifndef M_PI
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
#  define M_PI     3.14159265358979323846
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
#endif
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
#define MAX_HAPTICS  32         /* It's doubtful someone has more then 32 evdev */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    49
static int MaybeAddDevice(const char *path);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    50
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    51
static int MaybeRemoveDevice(const char *path);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    52
void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    53
#endif /* SDL_USE_LIBUDEV */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
 * List of available haptic devices.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
 */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    58
typedef struct SDL_hapticlist_item
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    char *fname;                /* Dev path name (like /dev/input/event1) */
8184
2afb90ff0199 Fixed a typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 8183
diff changeset
    61
    SDL_Haptic *haptic;         /* Associated haptic. */
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
    62
    struct SDL_hapticlist_item *next;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    63
} SDL_hapticlist_item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
 * Haptic system hardware data.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
struct haptic_hwdata
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    int fd;                     /* File descriptor of the device. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    char *fname;                /* Points to the name in SDL_hapticlist. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
};
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
 * Haptic system effect data.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
struct haptic_hweffect
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    struct ff_effect effect;    /* The linux kernel effect structure. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
};
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    84
static SDL_hapticlist_item *SDL_hapticlist = NULL;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    85
static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    86
static int numhaptics = 0;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
#define test_bit(nr, addr) \
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
   (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
#define EV_TEST(ev,f) \
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
   if (test_bit((ev), features)) ret |= (f);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
 * Test whether a device has haptic properties.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
 * Returns available properties or 0 if there are none.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
EV_IsHaptic(int fd)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    unsigned int ret;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    unsigned long features[1 + FF_MAX / sizeof(unsigned long)];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
    /* Ask device for what it has. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
    ret = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   105
        return SDL_SetError("Haptic: Unable to get device's features: %s",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   106
                            strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    /* Convert supported features to SDL_HAPTIC platform-neutral features. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
    EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
    EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   112
    /* !!! FIXME: put this back when we have more bits in 2.1 */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   113
    /* EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE); */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
    EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
    EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
    EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
    EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
    EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
    EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   125
    EV_TEST(FF_RUMBLE, SDL_HAPTIC_LEFTRIGHT);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    /* Return what it supports. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
    return ret;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
 * Tests whether a device is a mouse or not.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   135
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
EV_IsMouse(int fd)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
    unsigned long argp[40];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    /* Ask for supported features. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
    if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
    /* Currently we only test for BTN_MOUSE which can give fake positives. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
    if (test_bit(BTN_MOUSE, argp) != 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
        return 1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
 * Initializes the haptic subsystem by finding available devices.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
SDL_SYS_HapticInit(void)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
    const char joydev_pattern[] = "/dev/input/event%d";
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
    char path[PATH_MAX];
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   161
    int i, j;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   163
    /*
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
     * Limit amount of checks to MAX_HAPTICS since we may or may not have
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
     * permission to some or all devices.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
     */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
    i = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
    for (j = 0; j < MAX_HAPTICS; ++j) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
        snprintf(path, PATH_MAX, joydev_pattern, i++);
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   171
        MaybeAddDevice(path);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   172
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   174
#if SDL_USE_LIBUDEV
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   175
    if (SDL_UDEV_Init() < 0) {
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   176
        return SDL_SetError("Could not initialize UDEV");
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   177
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   178
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   179
    if ( SDL_UDEV_AddCallback(haptic_udev_callback) < 0) {
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   180
        SDL_UDEV_Quit();
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   181
        return SDL_SetError("Could not setup haptic <-> udev callback");
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   182
    }
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   183
#endif /* SDL_USE_LIBUDEV */
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   184
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   185
    return numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   186
}
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   187
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   188
int
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   189
SDL_SYS_NumHaptics()
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   190
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   191
    return numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   192
}
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   193
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   194
static SDL_hapticlist_item *
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   195
HapticByDevIndex(int device_index)
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   196
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   197
    SDL_hapticlist_item *item = SDL_hapticlist;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   198
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   199
    if ((device_index < 0) || (device_index >= numhaptics)) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   200
        return NULL;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   201
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   202
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   203
    while (device_index > 0) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   204
        SDL_assert(item != NULL);
8183
f8404b0e00ac Fix device counting in HapticMouse and JoystickOpen routines. 0 is the first item in the list not the last
Edward Rudd <urkle@outoforder.cc>
parents: 8182
diff changeset
   205
        --device_index;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   206
        item = item->next;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   207
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   208
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   209
    return item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   210
}
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   212
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   213
void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   214
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   215
    if (devpath == NULL || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   216
        return;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   217
    }
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   218
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   219
    switch( udev_type )
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   220
    {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   221
        case SDL_UDEV_DEVICEADDED:
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   222
            MaybeAddDevice(devpath);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   223
            break;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   224
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   225
        case SDL_UDEV_DEVICEREMOVED:
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   226
            MaybeRemoveDevice(devpath);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   227
            break;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   228
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   229
        default:
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   230
            break;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   231
    }
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   232
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   233
}
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   234
#endif /* SDL_USE_LIBUDEV */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   235
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   236
static int
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   237
MaybeAddDevice(const char *path)
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   238
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   239
    dev_t dev_nums[MAX_HAPTICS];
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   240
    struct stat sb;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   241
    int fd;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   242
    int k;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   243
    int duplicate;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   244
    int success;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   245
    SDL_hapticlist_item *item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   246
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   247
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   248
    if (path == NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   249
        return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   250
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   251
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   252
    /* check to see if file exists */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   253
    if (stat(path, &sb) != 0) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   254
        return -1;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   255
    }
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   256
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   257
    /* check for duplicates */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   258
    duplicate = 0;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   259
    for (k = 0; (k < numhaptics) && !duplicate; ++k) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   260
        if (sb.st_rdev == dev_nums[k]) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   261
            duplicate = 1;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
        }
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   263
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   264
    if (duplicate) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   265
        return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   266
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   268
    /* try to open */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   269
    fd = open(path, O_RDWR, 0);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   270
    if (fd < 0) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   271
        return -1;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   272
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
#ifdef DEBUG_INPUT_EVENTS
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   275
    printf("Checking %s\n", path);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
#endif
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   278
    /* see if it works */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   279
    success = EV_IsHaptic(fd);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   280
    close(fd);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   281
    if (success <= 0) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   282
        return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   283
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   284
8247
fd220c4df418 Zero out haptic linked list items on creation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8184
diff changeset
   285
    item = (SDL_hapticlist_item *) SDL_calloc(1, sizeof (SDL_hapticlist_item));
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   286
    if (item == NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   287
        return -1;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
    }
8247
fd220c4df418 Zero out haptic linked list items on creation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8184
diff changeset
   289
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   290
    item->fname = SDL_strdup(path);
9360
c671d8ca48a1 Remove unnecessary parentheses and an unnecessary free() of a NULL pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 9266
diff changeset
   291
    if (item->fname == NULL) {
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   292
        SDL_free(item);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   293
        return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   294
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   295
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   296
    /* TODO: should we add instance IDs? */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   297
    if (SDL_hapticlist_tail == NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   298
        SDL_hapticlist = SDL_hapticlist_tail = item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   299
    } else {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   300
        SDL_hapticlist_tail->next = item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   301
        SDL_hapticlist_tail = item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   302
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   303
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   304
    dev_nums[numhaptics] = sb.st_rdev;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   305
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   306
    ++numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   307
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   308
    /* !!! TODO: Send a haptic add event? */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
    return numhaptics;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   313
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   314
static int
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   315
MaybeRemoveDevice(const char* path)
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   316
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   317
    SDL_hapticlist_item *item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   318
    SDL_hapticlist_item *prev = NULL;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   319
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   320
    if (path == NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   321
        return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   322
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   323
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   324
    for (item = SDL_hapticlist; item != NULL; item = item->next) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   325
        /* found it, remove it. */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   326
        if (SDL_strcmp(path, item->fname) == 0) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   327
            const int retval = item->haptic ? item->haptic->index : -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   328
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   329
            if (prev != NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   330
                prev->next = item->next;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   331
            } else {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   332
                SDL_assert(SDL_hapticlist == item);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   333
                SDL_hapticlist = item->next;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   334
            }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   335
            if (item == SDL_hapticlist_tail) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   336
                SDL_hapticlist_tail = prev;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   337
            }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   338
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   339
            /* Need to decrement the haptic count */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   340
            --numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   341
            /* !!! TODO: Send a haptic remove event? */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   342
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   343
            SDL_free(item->fname);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   344
            SDL_free(item);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   345
            return retval;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   346
        }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   347
        prev = item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   348
    }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   349
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   350
    return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   351
}
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   352
#endif /* SDL_USE_LIBUDEV */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
 * Gets the name from a file descriptor.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
static const char *
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
SDL_SYS_HapticNameFromFD(int fd)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
    static char namebuf[128];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   361
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
    /* We use the evdev name ioctl. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
    if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
        return NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
    return namebuf;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   370
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   371
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   372
 * Return the name of a haptic device, does not need to be opened.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   374
const char *
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   375
SDL_SYS_HapticName(int index)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   376
{
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   377
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   378
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   379
    const char *name;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   380
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   381
    item = HapticByDevIndex(index);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   382
    /* Open the haptic device. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   383
    name = NULL;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   384
    fd = open(item->fname, O_RDONLY, 0);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   385
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   386
    if (fd >= 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
        name = SDL_SYS_HapticNameFromFD(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   389
        if (name == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
            /* No name found, return device character device */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   391
            name = item->fname;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
        }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
    close(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
    return name;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   399
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   400
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   401
 * Opens the haptic device from the file descriptor.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   402
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   403
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
    /* Allocate the hwdata */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
    haptic->hwdata = (struct haptic_hwdata *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   408
        SDL_malloc(sizeof(*haptic->hwdata));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   409
    if (haptic->hwdata == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
        SDL_OutOfMemory();
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   415
    /* Set the data. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
    haptic->hwdata->fd = fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
    haptic->supported = EV_IsHaptic(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
    haptic->naxes = 2;          /* Hardcoded for now, not sure if it's possible to find out. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
    /* Set the effects */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
    if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
        SDL_SetError("Haptic: Unable to query device memory: %s",
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
                     strerror(errno));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
    haptic->nplaying = haptic->neffects;        /* Linux makes no distinction. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
    haptic->effects = (struct haptic_effect *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
        SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
    if (haptic->effects == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   430
        SDL_OutOfMemory();
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
    /* Clear the memory */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
    SDL_memset(haptic->effects, 0,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
               sizeof(struct haptic_effect) * haptic->neffects);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
    /* Error handling */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
  open_err:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
    close(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
    if (haptic->hwdata != NULL) {
9266
f6355bfca853 Replaced free() with SDL_free() because related allocation also uses wrapper.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9251
diff changeset
   443
        SDL_free(haptic->hwdata);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   444
        haptic->hwdata = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   445
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
    return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   447
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   448
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   449
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   450
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   451
 * Opens a haptic device for usage.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   452
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   453
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   455
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   456
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
    int ret;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   458
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   459
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   460
    item = HapticByDevIndex(haptic->index);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   461
    /* Open the character device */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   462
    fd = open(item->fname, O_RDWR, 0);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   463
    if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   464
        return SDL_SetError("Haptic: Unable to open %s: %s",
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   465
                            item->fname, strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   466
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   467
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   468
    /* Try to create the haptic. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   469
    ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   470
    if (ret < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   471
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   472
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   473
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   474
    /* Set the fname. */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   475
    haptic->hwdata->fname = item->fname;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   476
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   477
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   478
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   479
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   480
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   481
 * Opens a haptic device from first mouse it finds for usage.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   482
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   483
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   484
SDL_SYS_HapticMouse(void)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   485
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   486
    int fd;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   487
    int device_index = 0;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   488
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   489
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   490
    for (item = SDL_hapticlist; item; item = item->next) {
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   491
        /* Open the device. */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   492
        fd = open(item->fname, O_RDWR, 0);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   493
        if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   494
            return SDL_SetError("Haptic: Unable to open %s: %s",
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   495
                                item->fname, strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   496
        }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   497
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
        /* Is it a mouse? */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   499
        if (EV_IsMouse(fd)) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   500
            close(fd);
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   501
            return device_index;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   502
        }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   503
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   504
        close(fd);
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   505
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   506
        ++device_index;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   507
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   508
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   509
    return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   510
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   511
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   512
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   513
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   514
 * Checks to see if a joystick has haptic features.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   515
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   516
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   517
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   518
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
    return EV_IsHaptic(joystick->hwdata->fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   520
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   521
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   522
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   523
/*
7708
d5aa9910b1f7 Fixed comment typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 7678
diff changeset
   524
 * Checks to see if the haptic device and joystick are in reality the same.
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   525
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   526
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   527
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   528
{
7132
1415923c2eab Corrected spelling in C source files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
   529
    /* We are assuming Linux is using evdev which should trump the old
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   530
     * joystick methods. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   531
    if (SDL_strcmp(joystick->hwdata->fname, haptic->hwdata->fname) == 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   532
        return 1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   533
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   534
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   535
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   536
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   537
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   538
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   539
 * Opens a SDL_Haptic from a SDL_Joystick.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   543
{
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   544
    int device_index = 0;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
    int ret;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   547
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   548
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   549
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   550
    /* Find the joystick in the haptic list. */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   551
    for (item = SDL_hapticlist; item; item = item->next) {
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   552
        if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   553
            haptic->index = device_index;
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   554
            break;
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   555
        }
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   556
        ++device_index;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
    }
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   558
    if (device_index >= MAX_HAPTICS) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   559
        return SDL_SetError("Haptic: Joystick doesn't have Haptic capabilities");
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   560
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   561
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
    fd = open(joystick->hwdata->fname, O_RDWR, 0);
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   563
    if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   564
        return SDL_SetError("Haptic: Unable to open %s: %s",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   565
                            joystick->hwdata->fname, strerror(errno));
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   566
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   567
    ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
    if (ret < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   572
    haptic->hwdata->fname = item->fname;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   573
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   575
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   576
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   577
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   578
 * Closes the haptic device.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
SDL_SYS_HapticClose(SDL_Haptic * haptic)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   583
    if (haptic->hwdata) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   584
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
        /* Free effects. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
        SDL_free(haptic->effects);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   587
        haptic->effects = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
        haptic->neffects = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
        /* Clean up */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
        close(haptic->hwdata->fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
        /* Free */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
        SDL_free(haptic->hwdata);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
        haptic->hwdata = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   598
    /* Clear the rest. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
    SDL_memset(haptic, 0, sizeof(SDL_Haptic));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   601
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   602
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   603
/*
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   604
 * Clean up after system specific haptic stuff
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
SDL_SYS_HapticQuit(void)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
{
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   609
    SDL_hapticlist_item *item = NULL;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   610
    SDL_hapticlist_item *next = NULL;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   611
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   612
    for (item = SDL_hapticlist; item; item = next) {
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   613
        next = item->next;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
        /* Opened and not closed haptics are leaked, this is on purpose.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
         * Close your haptic devices after usage. */
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   616
        SDL_free(item->fname);
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   617
        item->fname = NULL;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   618
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   620
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   621
    SDL_UDEV_DelCallback(haptic_udev_callback);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   622
    SDL_UDEV_Quit();
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   623
#endif /* SDL_USE_LIBUDEV */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   624
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   625
    numhaptics = 0;
8902
5192470456ae Fixed bug 2562 - SDL_hapticlist/_tail not set correctly
Sam Lantinga <slouken@libsdl.org>
parents: 8247
diff changeset
   626
    SDL_hapticlist = NULL;
5192470456ae Fixed bug 2562 - SDL_hapticlist/_tail not set correctly
Sam Lantinga <slouken@libsdl.org>
parents: 8247
diff changeset
   627
    SDL_hapticlist_tail = NULL;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   628
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   630
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   631
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   632
 * Converts an SDL button to a ff_trigger button.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   633
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   634
static Uint16
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
SDL_SYS_ToButton(Uint16 button)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   636
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   637
    Uint16 ff_button;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   639
    ff_button = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
    /*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
     * Not sure what the proper syntax is because this actually isn't implemented
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
     * in the current kernel from what I've seen (2.6.26).
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
     */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   645
    if (button != 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
        ff_button = BTN_GAMEPAD + button - 1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
    return ff_button;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
/*
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   654
 * Initializes the ff_effect usable direction from a SDL_HapticDirection.
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
 */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   656
static int
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   657
SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection * src)
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
    Uint32 tmp;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   661
    switch (src->type) {
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   662
    case SDL_HAPTIC_POLAR:
3680
7c18b38f0f9a Fixed bug #920
Sam Lantinga <slouken@libsdl.org>
parents: 3218
diff changeset
   663
        /* Linux directions start from south.
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   664
                (and range from 0 to 0xFFFF)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   665
                   Quoting include/linux/input.h, line 926:
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   666
                   Direction of the effect is encoded as follows:
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   667
                        0 deg -> 0x0000 (down)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   668
                        90 deg -> 0x4000 (left)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   669
                        180 deg -> 0x8000 (up)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   670
                        270 deg -> 0xC000 (right)
9249
35a4fab04296 Fixed bug 2766 - Haptic coding bugs and fixes for Linux FF: periodic.phase handled as time instead of angle; + direction clarification
Sam Lantinga <slouken@libsdl.org>
parents: 9070
diff changeset
   671
                   The force pulls into the direction specified by Linux directions,
35a4fab04296 Fixed bug 2766 - Haptic coding bugs and fixes for Linux FF: periodic.phase handled as time instead of angle; + direction clarification
Sam Lantinga <slouken@libsdl.org>
parents: 9070
diff changeset
   672
                   i.e. the opposite convention of SDL directions.
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   673
                    */
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   674
        tmp = ((src->dir[0] % 36000) * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   675
        *dest = (Uint16) tmp;
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   676
        break;
3680
7c18b38f0f9a Fixed bug #920
Sam Lantinga <slouken@libsdl.org>
parents: 3218
diff changeset
   677
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   678
    case SDL_HAPTIC_SPHERICAL:
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   679
            /*
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   680
                We convert to polar, because that's the only supported direction on Linux.
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   681
                The first value of a spherical direction is practically the same as a
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   682
                Polar direction, except that we have to add 90 degrees. It is the angle
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   683
                from EAST {1,0} towards SOUTH {0,1}.
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   684
                --> add 9000
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   685
                --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   686
            */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   687
            tmp = ((src->dir[0]) + 9000) % 36000;    /* Convert to polars */
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   688
        tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   689
        *dest = (Uint16) tmp;
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   690
        break;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   691
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   692
    case SDL_HAPTIC_CARTESIAN:
9069
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   693
        if (!src->dir[1])
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   694
            *dest = (src->dir[0] >= 0 ? 0x4000 : 0xC000);
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   695
        else if (!src->dir[0])
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   696
            *dest = (src->dir[1] >= 0 ? 0x8000 : 0);
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   697
        else {
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   698
            float f = atan2(src->dir[1], src->dir[0]);    /* Ideally we'd use fixed point math instead of floats... */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   699
                    /*
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   700
                      atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   701
                       - Y-axis-value is the second coordinate (from center to SOUTH)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   702
                       - X-axis-value is the first coordinate (from center to EAST)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   703
                        We add 36000, because atan2 also returns negative values. Then we practically
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   704
                        have the first spherical value. Therefore we proceed as in case
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   705
                        SDL_HAPTIC_SPHERICAL and add another 9000 to get the polar value.
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   706
                      --> add 45000 in total
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   707
                      --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   708
                    */
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   709
                tmp = (((Sint32) (f * 18000. / M_PI)) + 45000) % 36000;
9069
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   710
            tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   711
            *dest = (Uint16) tmp;
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   712
        }
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   713
        break;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   714
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   715
    default:
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   716
        return SDL_SetError("Haptic: Unsupported direction type.");
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   717
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   718
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   719
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   720
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   721
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   722
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   723
#define  CLAMP(x)    (((x) > 32767) ? 32767 : x)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
/*
7132
1415923c2eab Corrected spelling in C source files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
   725
 * Initializes the Linux effect struct from a haptic_effect.
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   726
 * Values above 32767 (for unsigned) are unspecified so we must clamp.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   727
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   728
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   729
SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect * src)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   730
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   731
    SDL_HapticConstant *constant;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   732
    SDL_HapticPeriodic *periodic;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   733
    SDL_HapticCondition *condition;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   734
    SDL_HapticRamp *ramp;
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   735
    SDL_HapticLeftRight *leftright;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   737
    /* Clear up */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   738
    SDL_memset(dest, 0, sizeof(struct ff_effect));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   739
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   740
    switch (src->type) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   741
    case SDL_HAPTIC_CONSTANT:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   742
        constant = &src->constant;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   743
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   744
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   745
        dest->type = FF_CONSTANT;
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   746
        if (SDL_SYS_ToDirection(&dest->direction, &constant->direction) == -1)
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   748
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   749
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   750
        dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ?
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   751
            0 : CLAMP(constant->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   752
        dest->replay.delay = CLAMP(constant->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   753
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   754
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   755
        dest->trigger.button = SDL_SYS_ToButton(constant->button);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   756
        dest->trigger.interval = CLAMP(constant->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   757
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   758
        /* Constant */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   759
        dest->u.constant.level = constant->level;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   760
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   761
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   762
        dest->u.constant.envelope.attack_length =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   763
            CLAMP(constant->attack_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   764
        dest->u.constant.envelope.attack_level =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   765
            CLAMP(constant->attack_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   766
        dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   767
        dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   768
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   769
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   770
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   771
    case SDL_HAPTIC_SINE:
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   772
    /* !!! FIXME: put this back when we have more bits in 2.1 */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   773
    /* case SDL_HAPTIC_SQUARE: */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   774
    case SDL_HAPTIC_TRIANGLE:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   775
    case SDL_HAPTIC_SAWTOOTHUP:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
    case SDL_HAPTIC_SAWTOOTHDOWN:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   777
        periodic = &src->periodic;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   778
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   779
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
        dest->type = FF_PERIODIC;
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   781
        if (SDL_SYS_ToDirection(&dest->direction, &periodic->direction) == -1)
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   782
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   783
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   784
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   785
        dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ?
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
            0 : CLAMP(periodic->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
        dest->replay.delay = CLAMP(periodic->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   788
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   789
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
        dest->trigger.button = SDL_SYS_ToButton(periodic->button);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   791
        dest->trigger.interval = CLAMP(periodic->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   792
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   793
        /* Periodic */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   794
        if (periodic->type == SDL_HAPTIC_SINE)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   795
            dest->u.periodic.waveform = FF_SINE;
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   796
        /* !!! FIXME: put this back when we have more bits in 2.1 */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   797
        /* else if (periodic->type == SDL_HAPTIC_SQUARE)
7677
871d43c6968a OCD fixes: Adds a space before */
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7621
diff changeset
   798
            dest->u.periodic.waveform = FF_SQUARE; */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   799
        else if (periodic->type == SDL_HAPTIC_TRIANGLE)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   800
            dest->u.periodic.waveform = FF_TRIANGLE;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   801
        else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   802
            dest->u.periodic.waveform = FF_SAW_UP;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   803
        else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   804
            dest->u.periodic.waveform = FF_SAW_DOWN;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   805
        dest->u.periodic.period = CLAMP(periodic->period);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   806
        dest->u.periodic.magnitude = periodic->magnitude;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   807
        dest->u.periodic.offset = periodic->offset;
9251
198989b162d3 Fixed bug 2766 - Haptic coding bugs and fixes for Linux FF: periodic.phase handled as time instead of angle; + direction clarification
Sam Lantinga <slouken@libsdl.org>
parents: 9249
diff changeset
   808
        /* Linux phase is defined in interval "[0x0000, 0x10000[", corresponds with "[0deg, 360deg[" phase shift. */
198989b162d3 Fixed bug 2766 - Haptic coding bugs and fixes for Linux FF: periodic.phase handled as time instead of angle; + direction clarification
Sam Lantinga <slouken@libsdl.org>
parents: 9249
diff changeset
   809
        dest->u.periodic.phase = ((Uint32)periodic->phase * 0x10000U) / 36000;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   810
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   811
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   812
        dest->u.periodic.envelope.attack_length =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   813
            CLAMP(periodic->attack_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   814
        dest->u.periodic.envelope.attack_level =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   815
            CLAMP(periodic->attack_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
        dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   817
        dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   818
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   819
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   820
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   821
    case SDL_HAPTIC_SPRING:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   822
    case SDL_HAPTIC_DAMPER:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   823
    case SDL_HAPTIC_INERTIA:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   824
    case SDL_HAPTIC_FRICTION:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   825
        condition = &src->condition;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   826
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   827
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   828
        if (condition->type == SDL_HAPTIC_SPRING)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   829
            dest->type = FF_SPRING;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   830
        else if (condition->type == SDL_HAPTIC_DAMPER)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   831
            dest->type = FF_DAMPER;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   832
        else if (condition->type == SDL_HAPTIC_INERTIA)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   833
            dest->type = FF_INERTIA;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   834
        else if (condition->type == SDL_HAPTIC_FRICTION)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   835
            dest->type = FF_FRICTION;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   836
        dest->direction = 0;    /* Handled by the condition-specifics. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   837
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   838
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   839
        dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ?
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   840
            0 : CLAMP(condition->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   841
        dest->replay.delay = CLAMP(condition->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   842
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   843
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   844
        dest->trigger.button = SDL_SYS_ToButton(condition->button);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   845
        dest->trigger.interval = CLAMP(condition->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   846
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   847
        /* Condition */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   848
        /* X axis */
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   849
        dest->u.condition[0].right_saturation = condition->right_sat[0];
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   850
        dest->u.condition[0].left_saturation = condition->left_sat[0];
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   851
        dest->u.condition[0].right_coeff = condition->right_coeff[0];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   852
        dest->u.condition[0].left_coeff = condition->left_coeff[0];
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   853
        dest->u.condition[0].deadband = condition->deadband[0];
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   854
        dest->u.condition[0].center = condition->center[0];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   855
        /* Y axis */
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   856
        dest->u.condition[1].right_saturation = condition->right_sat[1];
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   857
        dest->u.condition[1].left_saturation = condition->left_sat[1];
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   858
        dest->u.condition[1].right_coeff = condition->right_coeff[1];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   859
        dest->u.condition[1].left_coeff = condition->left_coeff[1];
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   860
        dest->u.condition[1].deadband = condition->deadband[1];
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   861
        dest->u.condition[1].center = condition->center[1];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   862
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   863
        /*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   864
         * There is no envelope in the linux force feedback api for conditions.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   865
         */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   866
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   867
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   868
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   869
    case SDL_HAPTIC_RAMP:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   870
        ramp = &src->ramp;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   871
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   872
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   873
        dest->type = FF_RAMP;
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   874
        if (SDL_SYS_ToDirection(&dest->direction, &ramp->direction) == -1)
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   875
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   876
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   877
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   878
        dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ?
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   879
            0 : CLAMP(ramp->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   880
        dest->replay.delay = CLAMP(ramp->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   881
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   882
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   883
        dest->trigger.button = SDL_SYS_ToButton(ramp->button);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   884
        dest->trigger.interval = CLAMP(ramp->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   885
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   886
        /* Ramp */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   887
        dest->u.ramp.start_level = ramp->start;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   888
        dest->u.ramp.end_level = ramp->end;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   889
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   890
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   891
        dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   892
        dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
        dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
        dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   895
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   896
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   897
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   898
    case SDL_HAPTIC_LEFTRIGHT:
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   899
        leftright = &src->leftright;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   900
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   901
        /* Header */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   902
        dest->type = FF_RUMBLE;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   903
        dest->direction = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   904
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   905
        /* Replay */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   906
        dest->replay.length = (leftright->length == SDL_HAPTIC_INFINITY) ?
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   907
            0 : CLAMP(leftright->length);
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   908
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   909
        /* Trigger */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   910
        dest->trigger.button = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   911
        dest->trigger.interval = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   912
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   913
        /* Rumble */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   914
        dest->u.rumble.strong_magnitude = leftright->large_magnitude;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   915
        dest->u.rumble.weak_magnitude = leftright->small_magnitude;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   916
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   917
        break;
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   918
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   919
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   920
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   921
        return SDL_SetError("Haptic: Unknown effect type.");
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   922
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   923
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   924
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   925
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   926
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   927
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   928
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   929
 * Creates a new haptic effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   930
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   931
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   932
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   933
                        SDL_HapticEffect * base)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   934
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   935
    struct ff_effect *linux_effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   936
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   937
    /* Allocate the hardware effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   938
    effect->hweffect = (struct haptic_hweffect *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   939
        SDL_malloc(sizeof(struct haptic_hweffect));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   940
    if (effect->hweffect == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   941
        return SDL_OutOfMemory();
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   942
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   943
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   944
    /* Prepare the ff_effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   945
    linux_effect = &effect->hweffect->effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   946
    if (SDL_SYS_ToFFEffect(linux_effect, base) != 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   947
        goto new_effect_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   948
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   949
    linux_effect->id = -1;      /* Have the kernel give it an id */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   950
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   951
    /* Upload the effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   952
    if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   953
        SDL_SetError("Haptic: Error uploading effect to the device: %s",
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   954
                     strerror(errno));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   955
        goto new_effect_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   956
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   957
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   958
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   959
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   960
  new_effect_err:
9266
f6355bfca853 Replaced free() with SDL_free() because related allocation also uses wrapper.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9251
diff changeset
   961
    SDL_free(effect->hweffect);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   962
    effect->hweffect = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   963
    return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   964
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   965
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   966
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   967
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   968
 * Updates an effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   969
 *
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   970
 * Note: Dynamically updating the direction can in some cases force
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   971
 * the effect to restart and run once.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   972
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   973
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   974
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   975
                           struct haptic_effect *effect,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   976
                           SDL_HapticEffect * data)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   977
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   978
    struct ff_effect linux_effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   979
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   980
    /* Create the new effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   981
    if (SDL_SYS_ToFFEffect(&linux_effect, data) != 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   982
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   983
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   984
    linux_effect.id = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   985
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   986
    /* See if it can be uploaded. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   987
    if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   988
        return SDL_SetError("Haptic: Error updating the effect: %s",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   989
                            strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   990
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   991
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   992
    /* Copy the new effect into memory. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   993
    SDL_memcpy(&effect->hweffect->effect, &linux_effect,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   994
               sizeof(struct ff_effect));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   995
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   996
    return effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   997
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   998
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   999
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1000
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1001
 * Runs an effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1002
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1003
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1004
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1005
                        Uint32 iterations)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1006
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1007
    struct input_event run;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1008
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1009
    /* Prepare to run the effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1010
    run.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1011
    run.code = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1012
    /* We don't actually have infinity here, so we just do INT_MAX which is pretty damn close. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1013
    run.value = (iterations > INT_MAX) ? INT_MAX : iterations;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1014
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1015
    if (write(haptic->hwdata->fd, (const void *) &run, sizeof(run)) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
  1016
        return SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1017
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1018
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1019
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1020
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1021
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1022
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1023
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1024
 * Stops an effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1025
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1026
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1027
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1028
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1029
    struct input_event stop;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1030
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1031
    stop.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1032
    stop.code = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1033
    stop.value = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1034
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1035
    if (write(haptic->hwdata->fd, (const void *) &stop, sizeof(stop)) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
  1036
        return SDL_SetError("Haptic: Unable to stop the effect: %s",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
  1037
                            strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1038
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1039
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1040
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1041
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1042
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1043
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1044
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1045
 * Frees the effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1046
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1047
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1048
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1049
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1050
    if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1051
        SDL_SetError("Haptic: Error removing the effect from the device: %s",
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1052
                     strerror(errno));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1053
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1054
    SDL_free(effect->hweffect);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1055
    effect->hweffect = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1056
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1057
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1058
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1059
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1060
 * Gets the status of a haptic effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1061
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1062
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1063
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1064
                              struct haptic_effect *effect)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1065
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1066
#if 0                           /* Not supported atm. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1067
    struct input_event ie;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1068
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1069
    ie.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1070
    ie.type = EV_FF_STATUS;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1071
    ie.code = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1072
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1073
    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
  1074
        return SDL_SetError("Haptic: Error getting device status.");
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1075
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1076
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1077
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1078
#endif
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1079
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1080
    return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1081
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1082
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1083
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1084
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1085
 * Sets the gain.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1086
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1087
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1088
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1089
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1090
    struct input_event ie;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1091
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1092
    ie.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1093
    ie.code = FF_GAIN;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1094
    ie.value = (0xFFFFUL * gain) / 100;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1095
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1096
    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
  1097
        return SDL_SetError("Haptic: Error setting gain: %s", strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1098
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1099
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1100
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: