src/haptic/linux/SDL_syshaptic.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Nov 2014 11:48:43 -0800
changeset 9249 35a4fab04296
parent 9070 8973a237f360
child 9251 198989b162d3
permissions -rw-r--r--
Fixed bug 2766 - Haptic coding bugs and fixes for Linux FF: periodic.phase handled as time instead of angle; + direction clarification Elias Vanderstuyft It's not obvious from the general "haptic direction" description what the SDL direction actually means in terms of force magnitude sign, currently its meaning is only reflected by the example.
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);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   291
    if ( (item->fname == NULL) ) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   292
        SDL_free(item->fname);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   293
        SDL_free(item);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   294
        return -1;
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
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   297
    /* 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
   298
    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
   299
        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
   300
    } else {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   301
        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
   302
        SDL_hapticlist_tail = item;
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
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   305
    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
   306
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   307
    ++numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   308
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   309
    /* !!! 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
   310
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
    return numhaptics;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   314
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   315
static int
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   316
MaybeRemoveDevice(const char* path)
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   317
{
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   318
    SDL_hapticlist_item *item;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   319
    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
   320
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   321
    if (path == NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   322
        return -1;
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
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   325
    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
   326
        /* found it, remove it. */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   327
        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
   328
            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
   329
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   330
            if (prev != NULL) {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   331
                prev->next = item->next;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   332
            } else {
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   333
                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
   334
                SDL_hapticlist = item->next;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   335
            }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   336
            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
   337
                SDL_hapticlist_tail = prev;
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
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   340
            /* 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
   341
            --numhaptics;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   342
            /* !!! 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
   343
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   344
            SDL_free(item->fname);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   345
            SDL_free(item);
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   346
            return retval;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   347
        }
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   348
        prev = item;
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
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   351
    return -1;
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   352
}
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   353
#endif /* SDL_USE_LIBUDEV */
2713
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
 * 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
   357
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
static const char *
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
SDL_SYS_HapticNameFromFD(int fd)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   361
    static char namebuf[128];
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
    /* We use the evdev name ioctl. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
    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
   365
        return NULL;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
    return namebuf;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
 * 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
   374
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   375
const char *
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   376
SDL_SYS_HapticName(int index)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   377
{
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   378
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   379
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   380
    const char *name;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   381
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   382
    item = HapticByDevIndex(index);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   383
    /* Open the haptic device. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   384
    name = NULL;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   385
    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
   386
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
    if (fd >= 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   389
        name = SDL_SYS_HapticNameFromFD(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
        if (name == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   391
            /* 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
   392
            name = item->fname;
2713
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
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
    close(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
    return name;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   402
 * 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
   403
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
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
   406
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
    /* Allocate the hwdata */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   408
    haptic->hwdata = (struct haptic_hwdata *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   409
        SDL_malloc(sizeof(*haptic->hwdata));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
    if (haptic->hwdata == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
        SDL_OutOfMemory();
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
    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
   415
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
    /* Set the data. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
    haptic->hwdata->fd = fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
    haptic->supported = EV_IsHaptic(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
    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
   420
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
    /* Set the effects */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
    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
   423
        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
   424
                     strerror(errno));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
    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
   428
    haptic->effects = (struct haptic_effect *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
        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
   430
    if (haptic->effects == NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
        SDL_OutOfMemory();
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
        goto open_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
    /* Clear the memory */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
    SDL_memset(haptic->effects, 0,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
               sizeof(struct haptic_effect) * haptic->neffects);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
    /* Error handling */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
  open_err:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
    close(fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
    if (haptic->hwdata != NULL) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   444
        free(haptic->hwdata);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   445
        haptic->hwdata = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   447
    return -1;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   452
 * Opens a haptic device for usage.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   453
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   455
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   456
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   458
    int ret;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   459
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   460
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   461
    item = HapticByDevIndex(haptic->index);
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   462
    /* 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
   463
    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
   464
    if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   465
        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
   466
                            item->fname, strerror(errno));
2713
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   469
    /* Try to create the haptic. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   470
    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
   471
    if (ret < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   472
        return -1;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   475
    /* Set the fname. */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   476
    haptic->hwdata->fname = item->fname;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   477
    return 0;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   482
 * 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
   483
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   484
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   485
SDL_SYS_HapticMouse(void)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   486
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   487
    int fd;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   488
    int device_index = 0;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   489
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   490
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   491
    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
   492
        /* Open the device. */
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   493
        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
   494
        if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   495
            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
   496
                                item->fname, strerror(errno));
2713
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   499
        /* Is it a mouse? */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   500
        if (EV_IsMouse(fd)) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   501
            close(fd);
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   502
            return device_index;
2713
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   505
        close(fd);
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   506
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   507
        ++device_index;
2713
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   510
    return -1;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   515
 * 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
   516
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   517
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   518
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   520
    return EV_IsHaptic(joystick->hwdata->fd);
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   524
/*
7708
d5aa9910b1f7 Fixed comment typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 7678
diff changeset
   525
 * 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
   526
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   527
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   528
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
   529
{
7132
1415923c2eab Corrected spelling in C source files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
   530
    /* 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
   531
     * joystick methods. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   532
    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
   533
        return 1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   534
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   535
    return 0;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
 * 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
   541
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   543
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
   544
{
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   545
    int device_index = 0;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
    int fd;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   547
    int ret;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   548
    SDL_hapticlist_item *item;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   549
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   550
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   551
    /* 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
   552
    for (item = SDL_hapticlist; item; item = item->next) {
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   553
        if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   554
            haptic->index = device_index;
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   555
            break;
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   556
        }
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   557
        ++device_index;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
    }
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   559
    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
   560
        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
   561
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
    fd = open(joystick->hwdata->fname, O_RDWR, 0);
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   564
    if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   565
        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
   566
                            joystick->hwdata->fname, strerror(errno));
3080
4094b4f1c3a1 More verbosity and error checking.
Edgar Simo <bobbens@gmail.com>
parents: 2713
diff changeset
   567
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
    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
   569
    if (ret < 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   573
    haptic->hwdata->fname = item->fname;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
    return 0;
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
 * Closes the haptic device.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
SDL_SYS_HapticClose(SDL_Haptic * haptic)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   583
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   584
    if (haptic->hwdata) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
        /* Free effects. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   587
        SDL_free(haptic->effects);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
        haptic->effects = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
        haptic->neffects = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
        /* Clean up */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
        close(haptic->hwdata->fd);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
        /* Free */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
        SDL_free(haptic->hwdata);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
        haptic->hwdata = NULL;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
    /* Clear the rest. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
    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
   601
}
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   602
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   603
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   604
/*
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
 * 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
   606
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
SDL_SYS_HapticQuit(void)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   609
{
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   610
    SDL_hapticlist_item *item = NULL;
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   611
    SDL_hapticlist_item *next = NULL;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   613
    for (item = SDL_hapticlist; item; item = next) {
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   614
        next = item->next;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
        /* 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
   616
         * Close your haptic devices after usage. */
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   617
        SDL_free(item->fname);
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   618
        item->fname = NULL;
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   619
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   620
8170
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   621
#if SDL_USE_LIBUDEV
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   622
    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
   623
    SDL_UDEV_Quit();
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   624
#endif /* SDL_USE_LIBUDEV */
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   625
3b7bb407fcf7 Rework haptic backend to properly support hotplugging of haptic devices.
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   626
    numhaptics = 0;
8902
5192470456ae Fixed bug 2562 - SDL_hapticlist/_tail not set correctly
Sam Lantinga <slouken@libsdl.org>
parents: 8247
diff changeset
   627
    SDL_hapticlist = NULL;
5192470456ae Fixed bug 2562 - SDL_hapticlist/_tail not set correctly
Sam Lantinga <slouken@libsdl.org>
parents: 8247
diff changeset
   628
    SDL_hapticlist_tail = NULL;
2713
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   633
 * 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
   634
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
static Uint16
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   636
SDL_SYS_ToButton(Uint16 button)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   637
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
    Uint16 ff_button;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   639
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
    ff_button = 0;
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
    /*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
     * 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
   644
     * 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
   645
     */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
    if (button != 0) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
        ff_button = BTN_GAMEPAD + button - 1;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
    return ff_button;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
/*
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   655
 * 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
   656
 */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   657
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
   658
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
   659
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
    Uint32 tmp;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   662
    switch (src->type) {
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   663
    case SDL_HAPTIC_POLAR:
3680
7c18b38f0f9a Fixed bug #920
Sam Lantinga <slouken@libsdl.org>
parents: 3218
diff changeset
   664
        /* 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
   665
                (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
   666
                   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
   667
                   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
   668
                        0 deg -> 0x0000 (down)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   669
                        90 deg -> 0x4000 (left)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   670
                        180 deg -> 0x8000 (up)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   671
                        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
   672
                   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
   673
                   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
   674
                    */
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   675
        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
   676
        *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
   677
        break;
3680
7c18b38f0f9a Fixed bug #920
Sam Lantinga <slouken@libsdl.org>
parents: 3218
diff changeset
   678
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   679
    case SDL_HAPTIC_SPHERICAL:
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   680
            /*
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   681
                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
   682
                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
   683
                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
   684
                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
   685
                --> add 9000
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   686
                --> 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
   687
            */
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   688
            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
   689
        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
   690
        *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
   691
        break;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   692
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   693
    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
   694
        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
   695
            *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
   696
        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
   697
            *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
   698
        else {
16b5dda99337 Haptic: Explicitly avoid floating point arithmetic if it's not needed.
Ryan C. Gordon <icculus@icculus.org>
parents: 9068
diff changeset
   699
            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
   700
                    /*
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7132
diff changeset
   701
                      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
   702
                       - 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
   703
                       - 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
   704
                        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
   705
                        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
   706
                        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
   707
                      --> 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
   708
                      --> 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
   709
                    */
9068
a57a1d70ae07 Haptic: DInput's POLAR direction actually matches Linux's direction.
Ryan C. Gordon <icculus@icculus.org>
parents: 9067
diff changeset
   710
                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
   711
            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
   712
            *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
   713
        }
9067
2412ba127e23 Haptic: Don't interpret a direction of polar 35999 as "unsupported type".
Ryan C. Gordon <icculus@icculus.org>
parents: 8902
diff changeset
   714
        break;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   715
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   716
    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
   717
        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
   718
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   719
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   720
    return 0;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
#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
   725
/*
7132
1415923c2eab Corrected spelling in C source files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
   726
 * 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
   727
 * 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
   728
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   729
static int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   730
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
   731
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   732
    Uint32 tmp;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   733
    SDL_HapticConstant *constant;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   734
    SDL_HapticPeriodic *periodic;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   735
    SDL_HapticCondition *condition;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
    SDL_HapticRamp *ramp;
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   737
    SDL_HapticLeftRight *leftright;
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   738
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   739
    /* Clear up */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   740
    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
   741
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   742
    switch (src->type) {
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   743
    case SDL_HAPTIC_CONSTANT:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   744
        constant = &src->constant;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   745
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   746
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
        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
   748
        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
   749
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   750
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   751
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   752
        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
   753
            0 : CLAMP(constant->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   754
        dest->replay.delay = CLAMP(constant->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   755
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   756
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   757
        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
   758
        dest->trigger.interval = CLAMP(constant->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   759
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   760
        /* Constant */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   761
        dest->u.constant.level = constant->level;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   762
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   763
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   764
        dest->u.constant.envelope.attack_length =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   765
            CLAMP(constant->attack_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   766
        dest->u.constant.envelope.attack_level =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   767
            CLAMP(constant->attack_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   768
        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
   769
        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
   770
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   771
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   772
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   773
    case SDL_HAPTIC_SINE:
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   774
    /* !!! 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
   775
    /* case SDL_HAPTIC_SQUARE: */
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
    case SDL_HAPTIC_TRIANGLE:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   777
    case SDL_HAPTIC_SAWTOOTHUP:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   778
    case SDL_HAPTIC_SAWTOOTHDOWN:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   779
        periodic = &src->periodic;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   781
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   782
        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
   783
        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
   784
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   785
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
        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
   788
            0 : CLAMP(periodic->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   789
        dest->replay.delay = CLAMP(periodic->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   791
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   792
        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
   793
        dest->trigger.interval = CLAMP(periodic->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   794
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   795
        /* Periodic */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   796
        if (periodic->type == SDL_HAPTIC_SINE)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   797
            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
   798
        /* !!! 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
   799
        /* else if (periodic->type == SDL_HAPTIC_SQUARE)
7677
871d43c6968a OCD fixes: Adds a space before */
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7621
diff changeset
   800
            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
   801
        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
   802
            dest->u.periodic.waveform = FF_TRIANGLE;
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_SAWTOOTHUP)
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_UP;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   805
        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
   806
            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
   807
        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
   808
        dest->u.periodic.magnitude = periodic->magnitude;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   809
        dest->u.periodic.offset = periodic->offset;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   810
        /* Phase is calculated based of offset from period and then clamped. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   811
        tmp = ((periodic->phase % 36000) * dest->u.periodic.period) / 36000;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   812
        dest->u.periodic.phase = CLAMP(tmp);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   813
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   814
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   815
        dest->u.periodic.envelope.attack_length =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
            CLAMP(periodic->attack_length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   817
        dest->u.periodic.envelope.attack_level =
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   818
            CLAMP(periodic->attack_level);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   819
        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
   820
        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
   821
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   822
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   823
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   824
    case SDL_HAPTIC_SPRING:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   825
    case SDL_HAPTIC_DAMPER:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   826
    case SDL_HAPTIC_INERTIA:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   827
    case SDL_HAPTIC_FRICTION:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   828
        condition = &src->condition;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   829
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   830
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   831
        if (condition->type == SDL_HAPTIC_SPRING)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   832
            dest->type = FF_SPRING;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   833
        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
   834
            dest->type = FF_DAMPER;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   835
        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
   836
            dest->type = FF_INERTIA;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   837
        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
   838
            dest->type = FF_FRICTION;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   839
        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
   840
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   841
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   842
        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
   843
            0 : CLAMP(condition->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   844
        dest->replay.delay = CLAMP(condition->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   845
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   846
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   847
        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
   848
        dest->trigger.interval = CLAMP(condition->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   849
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   850
        /* Condition */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   851
        /* X axis */
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   852
        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
   853
        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
   854
        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
   855
        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
   856
        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
   857
        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
   858
        /* Y axis */
9070
8973a237f360 Haptic: Fix the saturation and deadband parameters' available range.
Ryan C. Gordon <icculus@icculus.org>
parents: 9069
diff changeset
   859
        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
   860
        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
   861
        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
   862
        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
   863
        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
   864
        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
   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
         * 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
   868
         */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   869
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   870
        break;
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
    case SDL_HAPTIC_RAMP:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   873
        ramp = &src->ramp;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   874
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   875
        /* Header */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   876
        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
   877
        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
   878
            return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   879
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   880
        /* Replay */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   881
        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
   882
            0 : CLAMP(ramp->length);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   883
        dest->replay.delay = CLAMP(ramp->delay);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   884
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   885
        /* Trigger */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   886
        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
   887
        dest->trigger.interval = CLAMP(ramp->interval);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   888
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   889
        /* Ramp */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   890
        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
   891
        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
   892
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
        /* Envelope */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
        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
   895
        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
   896
        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
   897
        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
   898
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   899
        break;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   900
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   901
    case SDL_HAPTIC_LEFTRIGHT:
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   902
        leftright = &src->leftright;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   903
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   904
        /* Header */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   905
        dest->type = FF_RUMBLE;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   906
        dest->direction = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   907
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   908
        /* Replay */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   909
        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
   910
            0 : CLAMP(leftright->length);
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   911
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   912
        /* Trigger */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   913
        dest->trigger.button = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   914
        dest->trigger.interval = 0;
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   915
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   916
        /* Rumble */
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   917
        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
   918
        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
   919
8182
92f71edde3be fix indentation and spaces
Edward Rudd <urkle@outoforder.cc>
parents: 8170
diff changeset
   920
        break;
7621
5caa5fb3deb6 Replaced SDL_HAPTIC_SQUARE with SDL_HAPTIC_LEFTRIGHT.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   921
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
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   924
        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
   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
    return 0;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   932
 * Creates a new haptic effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   933
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   934
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   935
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
   936
                        SDL_HapticEffect * base)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   937
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   938
    struct ff_effect *linux_effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   939
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   940
    /* Allocate the hardware effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   941
    effect->hweffect = (struct haptic_hweffect *)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   942
        SDL_malloc(sizeof(struct haptic_hweffect));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   943
    if (effect->hweffect == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   944
        return SDL_OutOfMemory();
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   945
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   946
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   947
    /* Prepare the ff_effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   948
    linux_effect = &effect->hweffect->effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   949
    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
   950
        goto new_effect_err;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   951
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   952
    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
   953
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   954
    /* Upload the effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   955
    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
   956
        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
   957
                     strerror(errno));
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   958
        goto new_effect_err;
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
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   961
    return 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   962
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   963
  new_effect_err:
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   964
    free(effect->hweffect);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   965
    effect->hweffect = NULL;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   966
    return -1;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   971
 * Updates an effect.
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
 * 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
   974
 * 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
   975
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   976
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   977
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   978
                           struct haptic_effect *effect,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   979
                           SDL_HapticEffect * data)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   980
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   981
    struct ff_effect linux_effect;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   982
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   983
    /* Create the new effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   984
    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
   985
        return -1;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   986
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   987
    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
   988
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   989
    /* See if it can be uploaded. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   990
    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
   991
        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
   992
                            strerror(errno));
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   993
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   994
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   995
    /* Copy the new effect into memory. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   996
    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
   997
               sizeof(struct ff_effect));
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
    return effect->hweffect->effect.id;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1004
 * Runs an effect.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1005
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1006
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1007
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
  1008
                        Uint32 iterations)
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1009
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1010
    struct input_event run;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1011
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1012
    /* Prepare to run the effect */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1013
    run.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1014
    run.code = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1015
    /* 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
  1016
    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
  1017
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1018
    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
  1019
        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
  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
    return 0;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1027
 * Stops an 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
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1030
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
  1031
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1032
    struct input_event stop;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1033
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1034
    stop.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1035
    stop.code = effect->hweffect->effect.id;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1036
    stop.value = 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1037
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1038
    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
  1039
        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
  1040
                            strerror(errno));
2713
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
    return 0;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1048
 * Frees the 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
void
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1051
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
  1052
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1053
    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
  1054
        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
  1055
                     strerror(errno));
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
    SDL_free(effect->hweffect);
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1058
    effect->hweffect = NULL;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1063
 * 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
  1064
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1065
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1066
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1067
                              struct haptic_effect *effect)
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
#if 0                           /* Not supported atm. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1070
    struct input_event ie;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1071
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1072
    ie.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1073
    ie.type = EV_FF_STATUS;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1074
    ie.code = effect->hweffect->effect.id;
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
    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
  1077
        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
  1078
    }
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 0;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1081
#endif
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
    return -1;
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
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
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1088
 * Sets the 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
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1091
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
  1092
{
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1093
    struct input_event ie;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1094
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1095
    ie.type = EV_FF;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1096
    ie.code = FF_GAIN;
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1097
    ie.value = (0xFFFFUL * gain) / 100;
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
    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
  1100
        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
  1101
    }
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1102