Final merge of Google Summer of Code 2008 work...

Force Feedback for SDL
by Edgar Simo, mentored by Ryan C. Gordon

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403159
This commit is contained in:
Sam Lantinga 2008-08-25 09:55:03 +00:00
parent 0aca811202
commit 4fd9c25fe6
29 changed files with 6642 additions and 165 deletions

View file

@ -42,7 +42,7 @@ SDLMAIN_OBJECTS = @SDLMAIN_OBJECTS@
DIST = acinclude.m4 autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS docs docs.html include INSTALL Makefile.dc Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.qpg.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-OS2.zip Watcom-Win32.zip WhatsNew Xcode DIST = acinclude.m4 autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS docs docs.html include INSTALL Makefile.dc Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.qpg.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-OS2.zip Watcom-Win32.zip WhatsNew Xcode
HDRS = SDL.h SDL_audio.h SDL_cdrom.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_pixels.h SDL_platform.h SDL_quit.h SDL_rect.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h HDRS = SDL.h SDL_audio.h SDL_cdrom.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_pixels.h SDL_platform.h SDL_quit.h SDL_rect.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h
LT_AGE = @LT_AGE@ LT_AGE = @LT_AGE@
LT_CURRENT = @LT_CURRENT@ LT_CURRENT = @LT_CURRENT@

View file

@ -15,6 +15,7 @@ SOURCES = \
src/events/*.c \ src/events/*.c \
src/file/*.c \ src/file/*.c \
src/joystick/*.c \ src/joystick/*.c \
src/haptic/*.c \
src/stdlib/*.c \ src/stdlib/*.c \
src/thread/*.c \ src/thread/*.c \
src/timer/*.c \ src/timer/*.c \
@ -22,6 +23,7 @@ SOURCES = \
src/audio/dummy/*.c \ src/audio/dummy/*.c \
src/video/dummy/*.c \ src/video/dummy/*.c \
src/joystick/dummy/*.c \ src/joystick/dummy/*.c \
src/haptic/dummy/*.c \
src/cdrom/dummy/*.c \ src/cdrom/dummy/*.c \
src/thread/generic/*.c \ src/thread/generic/*.c \
src/timer/dummy/*.c \ src/timer/dummy/*.c \

View file

@ -235,6 +235,14 @@ if test x$enable_joystick != xyes; then
else else
SOURCES="$SOURCES $srcdir/src/joystick/*.c" SOURCES="$SOURCES $srcdir/src/joystick/*.c"
fi fi
AC_ARG_ENABLE(haptic,
AC_HELP_STRING([--enable-haptic], [Enable the haptic (force feedback) subsystem [[default=yes]]]),
, enable_haptic=yes)
if test x$enable_haptic != xyes; then
AC_DEFINE(SDL_HAPTIC_DISABLED)
else
SOURCES="$SOURCES $srcdir/src/haptic/*.c"
fi
AC_ARG_ENABLE(cdrom, AC_ARG_ENABLE(cdrom,
AC_HELP_STRING([--enable-cdrom], [Enable the cdrom subsystem [[default=yes]]]), AC_HELP_STRING([--enable-cdrom], [Enable the cdrom subsystem [[default=yes]]]),
, enable_cdrom=yes) , enable_cdrom=yes)
@ -2179,6 +2187,18 @@ case "$host" in
;; ;;
esac esac
fi fi
# Set up files for the haptic library
if test x$enable_haptic = xyes; then
if test x$use_input_events = xyes; then
case $ARCH in
linux)
AC_DEFINE(SDL_HAPTIC_LINUX)
SOURCES="$SOURCES $srcdir/src/haptic/linux/*.c"
have_haptic=yes
;;
esac
fi
fi
# Set up files for the cdrom library # Set up files for the cdrom library
if test x$enable_cdrom = xyes; then if test x$enable_cdrom = xyes; then
case $ARCH in case $ARCH in
@ -2316,6 +2336,13 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
fi fi
have_joystick=yes have_joystick=yes
fi fi
if test x$enable_haptic = xyes; then
if test x$have_dinput = xyes; then
AC_DEFINE(SDL_HAPTIC_DINPUT)
SOURCES="$SOURCES $srcdir/src/haptic/win32/SDL_syshaptic.c"
have_haptic=yes
fi
fi
# Set up files for the cdrom library # Set up files for the cdrom library
if test x$enable_cdrom = xyes; then if test x$enable_cdrom = xyes; then
AC_DEFINE(SDL_CDROM_WIN32) AC_DEFINE(SDL_CDROM_WIN32)
@ -2457,6 +2484,13 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c" SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c"
have_joystick=yes have_joystick=yes
fi fi
# Set up files for the haptic library
if test x$enable_haptic = xyes; then
AC_DEFINE(SDL_HAPTIC_IOKIT)
SOURCES="$SOURCES $srcdir/src/haptic/darwin/*.c"
have_haptic=yes
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,ForceFeedback"
fi
# Set up files for the cdrom library # Set up files for the cdrom library
if test x$enable_cdrom = xyes; then if test x$enable_cdrom = xyes; then
AC_DEFINE(SDL_CDROM_MACOSX) AC_DEFINE(SDL_CDROM_MACOSX)
@ -2565,6 +2599,12 @@ if test x$enable_joystick = xyes; then
SOURCES="$SOURCES $srcdir/src/joystick/dummy/*.c" SOURCES="$SOURCES $srcdir/src/joystick/dummy/*.c"
fi fi
fi fi
if test x$have_haptic != xyes; then
if test x$enable_haptic = xyes; then
AC_DEFINE(SDL_HAPTIC_DISABLED)
fi
SOURCES="$SOURCES $srcdir/src/haptic/dummy/*.c"
fi
if test x$have_cdrom != xyes; then if test x$have_cdrom != xyes; then
if test x$enable_cdrom = xyes; then if test x$enable_cdrom = xyes; then
AC_DEFINE(SDL_CDROM_DISABLED) AC_DEFINE(SDL_CDROM_DISABLED)

View file

@ -104,14 +104,15 @@ extern "C" {
/* These are the flags which may be passed to SDL_Init() -- you should /* These are the flags which may be passed to SDL_Init() -- you should
specify the subsystems which you will be using in your application. specify the subsystems which you will be using in your application.
*/ */
#define SDL_INIT_TIMER 0x00000001 #define SDL_INIT_TIMER 0x00000001
#define SDL_INIT_AUDIO 0x00000010 #define SDL_INIT_AUDIO 0x00000010
#define SDL_INIT_VIDEO 0x00000020 #define SDL_INIT_VIDEO 0x00000020
#define SDL_INIT_CDROM 0x00000100 #define SDL_INIT_CDROM 0x00000100
#define SDL_INIT_JOYSTICK 0x00000200 #define SDL_INIT_JOYSTICK 0x00000200
#define SDL_INIT_NOPARACHUTE 0x00100000 /* Don't catch fatal signals */ #define SDL_INIT_HAPTIC 0x00001000
#define SDL_INIT_EVENTTHREAD 0x01000000 /* Not supported on all OS's */ #define SDL_INIT_NOPARACHUTE 0x00100000 /* Don't catch fatal signals */
#define SDL_INIT_EVERYTHING 0x0000FFFF #define SDL_INIT_EVENTTHREAD 0x01000000 /* Not supported on all OS's */
#define SDL_INIT_EVERYTHING 0x0000FFFF
/* This function loads the SDL dynamically linked library and initializes /* This function loads the SDL dynamically linked library and initializes
* the subsystems specified by 'flags' (and those satisfying dependencies) * the subsystems specified by 'flags' (and those satisfying dependencies)

View file

@ -149,6 +149,7 @@
#undef SDL_EVENTS_DISABLED #undef SDL_EVENTS_DISABLED
#undef SDL_FILE_DISABLED #undef SDL_FILE_DISABLED
#undef SDL_JOYSTICK_DISABLED #undef SDL_JOYSTICK_DISABLED
#undef SDL_HAPTIC_DISABLED
#undef SDL_LOADSO_DISABLED #undef SDL_LOADSO_DISABLED
#undef SDL_THREADS_DISABLED #undef SDL_THREADS_DISABLED
#undef SDL_TIMERS_DISABLED #undef SDL_TIMERS_DISABLED
@ -215,6 +216,10 @@
#undef SDL_JOYSTICK_WINMM #undef SDL_JOYSTICK_WINMM
#undef SDL_JOYSTICK_USBHID #undef SDL_JOYSTICK_USBHID
#undef SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H #undef SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
#undef SDL_HAPTIC_DUMMY
#undef SDL_HAPTIC_LINUX
#undef SDL_HAPTIC_IOKIT
#undef SDL_HAPTIC_DINPUT
/* Enable various shared object loading systems */ /* Enable various shared object loading systems */
#undef SDL_LOADSO_BEOS #undef SDL_LOADSO_BEOS

View file

@ -89,6 +89,7 @@ typedef unsigned long uintptr_t;
/* Enable various input drivers */ /* Enable various input drivers */
#define SDL_JOYSTICK_DC 1 #define SDL_JOYSTICK_DC 1
#define SDL_HAPTIC_DUMMY 1
/* Enable various shared object loading systems */ /* Enable various shared object loading systems */
#define SDL_LOADSO_DUMMY 1 #define SDL_LOADSO_DUMMY 1

View file

@ -99,6 +99,7 @@
/* Enable various input drivers */ /* Enable various input drivers */
#define SDL_JOYSTICK_IOKIT 1 #define SDL_JOYSTICK_IOKIT 1
#define SDL_HAPTIC_IOKIT 1
/* Enable various shared object loading systems */ /* Enable various shared object loading systems */
#ifdef __ppc__ #ifdef __ppc__

View file

@ -47,6 +47,9 @@ typedef unsigned long uintptr_t;
/* Enable the stub joystick driver (src/joystick/dummy/\*.c) */ /* Enable the stub joystick driver (src/joystick/dummy/\*.c) */
#define SDL_JOYSTICK_DISABLED 1 #define SDL_JOYSTICK_DISABLED 1
/* Enable the stub haptic driver (src/haptic/dummy/\*.c) */
#define SDL_HAPTIC_DUMMY 1
/* Enable the stub shared object loader (src/loadso/dummy/\*.c) */ /* Enable the stub shared object loader (src/loadso/dummy/\*.c) */
#define SDL_LOADSO_DISABLED 1 #define SDL_LOADSO_DISABLED 1

View file

@ -118,6 +118,7 @@ typedef unsigned long long uint64_t;
/* Enable various input drivers */ /* Enable various input drivers */
#define SDL_JOYSTICK_OS2 1 #define SDL_JOYSTICK_OS2 1
#define SDL_HAPTIC_DUMMY 1
/* Enable various shared object loading systems */ /* Enable various shared object loading systems */
#define SDL_LOADSO_OS2 1 #define SDL_LOADSO_OS2 1

View file

@ -139,8 +139,10 @@ typedef unsigned int uintptr_t;
/* Enable various input drivers */ /* Enable various input drivers */
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
#define SDL_JOYSTICK_DISABLED 1 #define SDL_JOYSTICK_DISABLED 1
#define SDL_HAPTIC_DUMMY 1
#else #else
#define SDL_JOYSTICK_WINMM 1 #define SDL_JOYSTICK_DINPUT 1
#define SDL_HAPTIC_DINPUT 1
#endif #endif
/* Enable various shared object loading systems */ /* Enable various shared object loading systems */

1154
include/SDL_haptic.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -38,6 +38,10 @@
extern int SDL_JoystickInit(void); extern int SDL_JoystickInit(void);
extern void SDL_JoystickQuit(void); extern void SDL_JoystickQuit(void);
#endif #endif
#if !SDL_HAPTIC_DISABLED
extern int SDL_HapticInit(void);
extern int SDL_HapticQuit(void);
#endif
#if !SDL_CDROM_DISABLED #if !SDL_CDROM_DISABLED
extern int SDL_CDROMInit(void); extern int SDL_CDROMInit(void);
extern void SDL_CDROMQuit(void); extern void SDL_CDROMQuit(void);
@ -47,6 +51,10 @@ extern void SDL_StartTicks(void);
extern int SDL_TimerInit(void); extern int SDL_TimerInit(void);
extern void SDL_TimerQuit(void); extern void SDL_TimerQuit(void);
#endif #endif
#if defined(__WIN32__)
extern int SDL_HelperWindowCreate(void);
extern int SDL_HelperWindowDestroy(void);
#endif
/* The initialized subsystems */ /* The initialized subsystems */
static Uint32 SDL_initialized = 0; static Uint32 SDL_initialized = 0;
@ -123,6 +131,22 @@ SDL_InitSubSystem(Uint32 flags)
} }
#endif #endif
#if !SDL_HAPTIC_DISABLED
/* Initialize the haptic subsystem */
if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
if (SDL_HapticInit() < 0) {
return (-1);
}
SDL_initialized |= SDL_INIT_HAPTIC;
}
#else
if (flags & SDL_INIT_HAPTIC) {
SDL_SetError("SDL not built with haptic (force feedback) support");
return (-1);
}
#endif
#if !SDL_CDROM_DISABLED #if !SDL_CDROM_DISABLED
/* Initialize the CD-ROM subsystem */ /* Initialize the CD-ROM subsystem */
if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) { if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) {
@ -152,6 +176,12 @@ SDL_Init(Uint32 flags)
/* Clear the error message */ /* Clear the error message */
SDL_ClearError(); SDL_ClearError();
#if defined(__WIN32__)
if (SDL_HelperWindowCreate() < 0) {
return -1;
}
#endif
/* Initialize the desired subsystems */ /* Initialize the desired subsystems */
if (SDL_InitSubSystem(flags) < 0) { if (SDL_InitSubSystem(flags) < 0) {
return (-1); return (-1);
@ -180,6 +210,12 @@ SDL_QuitSubSystem(Uint32 flags)
SDL_initialized &= ~SDL_INIT_JOYSTICK; SDL_initialized &= ~SDL_INIT_JOYSTICK;
} }
#endif #endif
#if !SDL_HAPTIC_DISABLED
if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
SDL_HapticQuit();
SDL_initialized &= ~SDL_INIT_HAPTIC;
}
#endif
#if !SDL_TIMERS_DISABLED #if !SDL_TIMERS_DISABLED
if ((flags & SDL_initialized & SDL_INIT_TIMER)) { if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
SDL_TimerQuit(); SDL_TimerQuit();
@ -217,6 +253,10 @@ SDL_Quit(void)
printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
fflush(stdout); fflush(stdout);
#endif #endif
#if defined(__WIN32__)
SDL_HelperWindowDestroy();
#endif
SDL_QuitSubSystem(SDL_INIT_EVERYTHING); SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
#ifdef CHECK_LEAKS #ifdef CHECK_LEAKS

698
src/haptic/SDL_haptic.c Normal file
View file

@ -0,0 +1,698 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_syshaptic.h"
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
Uint8 SDL_numhaptics = 0;
SDL_Haptic **SDL_haptics = NULL;
/*
* Initializes the Haptic devices.
*/
int
SDL_HapticInit(void)
{
int arraylen;
int status;
SDL_numhaptics = 0;
status = SDL_SYS_HapticInit();
if (status >= 0) {
arraylen = (status + 1) * sizeof(*SDL_haptics);
SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
if (SDL_haptics == NULL) { /* Out of memory. */
SDL_numhaptics = 0;
} else {
SDL_memset(SDL_haptics, 0, arraylen);
SDL_numhaptics = status;
}
status = 0;
}
return status;
}
/*
* Checks to see if the haptic device is valid
*/
static int
ValidHaptic(SDL_Haptic * haptic)
{
int i;
int valid;
valid = 0;
for (i = 0; i < SDL_numhaptics; i++) {
if (SDL_haptics[i] == haptic) {
valid = 1;
break;
}
}
return valid;
}
/*
* Returns the number of available devices.
*/
int
SDL_NumHaptics(void)
{
return SDL_numhaptics;
}
/*
* Gets the name of a Haptic device by index.
*/
const char *
SDL_HapticName(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_numhaptics);
return NULL;
}
return SDL_SYS_HapticName(device_index);
}
/*
* Opens a Haptic device.
*/
SDL_Haptic *
SDL_HapticOpen(int device_index)
{
int i;
SDL_Haptic *haptic;
if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_numhaptics);
return NULL;
}
/* If the haptic is already open, return it */
for (i = 0; SDL_haptics[i]; i++) {
if (device_index == SDL_haptics[i]->index) {
haptic = SDL_haptics[i];
++haptic->ref_count;
return haptic;
}
}
/* Create the haptic device */
haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
if (haptic == NULL) {
SDL_OutOfMemory();
return NULL;
}
/* Initialize the haptic device */
SDL_memset(haptic, 0, (sizeof *haptic));
haptic->index = device_index;
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
return NULL;
}
/* Disable autocenter and set gain to max. */
if (haptic->supported & SDL_HAPTIC_GAIN)
SDL_HapticSetGain(haptic, 100);
if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
SDL_HapticSetAutocenter(haptic, 0);
/* Add haptic to list */
++haptic->ref_count;
for (i = 0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
SDL_haptics[i] = haptic;
return haptic;
}
/*
* Returns 1 if the device has been opened.
*/
int
SDL_HapticOpened(int device_index)
{
int i, opened;
opened = 0;
for (i = 0; SDL_haptics[i]; i++) {
if (SDL_haptics[i]->index == (Uint8) device_index) {
opened = 1;
break;
}
}
return opened;
}
/*
* Returns the index to a haptic device.
*/
int
SDL_HapticIndex(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->index;
}
/*
* Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
*/
int
SDL_MouseIsHaptic(void)
{
if (SDL_SYS_HapticMouse() < 0)
return SDL_FALSE;
return SDL_TRUE;
}
/*
* Returns the haptic device if mouse is haptic or NULL elsewise.
*/
SDL_Haptic *
SDL_HapticOpenFromMouse(void)
{
int device_index;
device_index = SDL_SYS_HapticMouse();
if (device_index < 0) {
SDL_SetError("Haptic: Mouse isn't a haptic device.");
return NULL;
}
return SDL_HapticOpen(device_index);
}
/*
* Returns SDL_TRUE if joystick has haptic features.
*/
int
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
{
int ret;
/* Must be a valid joystick */
if (!SDL_PrivateJoystickValid(&joystick)) {
return -1;
}
ret = SDL_SYS_JoystickIsHaptic(joystick);
if (ret > 0)
return SDL_TRUE;
else if (ret == 0)
return SDL_FALSE;
else
return -1;
}
/*
* Opens a haptic device from a joystick.
*/
SDL_Haptic *
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
{
int i;
SDL_Haptic *haptic;
/* Must be a valid joystick */
if (!SDL_PrivateJoystickValid(&joystick)) {
return NULL;
}
/* Joystick must be haptic */
if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
return NULL;
}
/* Check to see if joystick's haptic is already open */
for (i = 0; SDL_haptics[i]; i++) {
if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i], joystick)) {
haptic = SDL_haptics[i];
++haptic->ref_count;
return haptic;
}
}
/* Create the haptic device */
haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
if (haptic == NULL) {
SDL_OutOfMemory();
return NULL;
}
/* Initialize the haptic device */
SDL_memset(haptic, 0, sizeof(SDL_Haptic));
if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
SDL_free(haptic);
return NULL;
}
/* Add haptic to list */
++haptic->ref_count;
for (i = 0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
SDL_haptics[i] = haptic;
return haptic;
}
/*
* Closes a SDL_Haptic device.
*/
void
SDL_HapticClose(SDL_Haptic * haptic)
{
int i;
/* Must be valid */
if (!ValidHaptic(haptic)) {
return;
}
/* Check if it's still in use */
if (--haptic->ref_count < 0) {
return;
}
/* Close it, properly removing effects if needed */
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
SDL_HapticDestroyEffect(haptic, i);
}
}
SDL_SYS_HapticClose(haptic);
/* Remove from the list */
for (i = 0; SDL_haptics[i]; ++i) {
if (haptic == SDL_haptics[i]) {
SDL_haptics[i] = NULL;
SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
(SDL_numhaptics - i) * sizeof(haptic));
break;
}
}
/* Free */
SDL_free(haptic);
}
/*
* Cleans up after the subsystem.
*/
void
SDL_HapticQuit(void)
{
SDL_SYS_HapticQuit();
if (SDL_haptics != NULL) {
SDL_free(SDL_haptics);
SDL_haptics = NULL;
}
SDL_numhaptics = 0;
}
/*
* Returns the number of effects a haptic device has.
*/
int
SDL_HapticNumEffects(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->neffects;
}
/*
* Returns the number of effects a haptic device can play.
*/
int
SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->nplaying;
}
/*
* Returns supported effects by the device.
*/
unsigned int
SDL_HapticQuery(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->supported;
}
/*
* Returns the number of axis on the device.
*/
int
SDL_HapticNumAxes(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->naxes;
}
/*
* Checks to see if the device can support the effect.
*/
int
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & effect->type) != 0)
return SDL_TRUE;
return SDL_FALSE;
}
/*
* Creates a new haptic effect.
*/
int
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
{
int i;
/* Check for device validity. */
if (!ValidHaptic(haptic)) {
return -1;
}
/* Check to see if effect is supported */
if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) {
SDL_SetError("Haptic: Effect not supported by haptic device.");
return -1;
}
/* See if there's a free slot */
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect == NULL) {
/* Now let the backend create the real effect */
if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)
!= 0) {
return -1; /* Backend failed to create effect */
}
SDL_memcpy(&haptic->effects[i].effect, effect,
sizeof(SDL_HapticEffect));
return i;
}
}
SDL_SetError("Haptic: Device has no free space left.");
return -1;
}
/*
* Checks to see if an effect is valid.
*/
static int
ValidEffect(SDL_Haptic * haptic, int effect)
{
if ((effect < 0) || (effect >= haptic->neffects)) {
SDL_SetError("Haptic: Invalid effect identifier.");
return 0;
}
return 1;
}
/*
* Updates an effect.
*/
int
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect,
SDL_HapticEffect * data)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Can't change type dynamically. */
if (data->type != haptic->effects[effect].effect.type) {
SDL_SetError("Haptic: Updating effect type is illegal.");
return -1;
}
/* Updates the effect */
if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
0) {
return -1;
}
SDL_memcpy(&haptic->effects[effect].effect, data,
sizeof(SDL_HapticEffect));
return 0;
}
/*
* Runs the haptic effect on the device.
*/
int
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Run the effect */
if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)
< 0) {
return -1;
}
return 0;
}
/*
* Stops the haptic effect on the device.
*/
int
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Stop the effect */
if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
return -1;
}
return 0;
}
/*
* Gets rid of a haptic effect.
*/
void
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return;
}
/* Not allocated */
if (haptic->effects[effect].hweffect == NULL) {
return;
}
SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
}
/*
* Gets the status of a haptic effect.
*/
int
SDL_HapticGetEffectStatus(SDL_Haptic * haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
SDL_SetError("Haptic: Device does not support status queries.");
return -1;
}
return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
}
/*
* Sets the global gain of the device.
*/
int
SDL_HapticSetGain(SDL_Haptic * haptic, int gain)
{
const char *env;
int real_gain, max_gain;
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
SDL_SetError("Haptic: Device does not support setting gain.");
return -1;
}
if ((gain < 0) || (gain > 100)) {
SDL_SetError("Haptic: Gain must be between 0 and 100.");
return -1;
}
/* We use the envvar to get the maximum gain. */
env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
if (env != NULL) {
max_gain = SDL_atoi(env);
/* Check for sanity. */
if (max_gain < 0)
max_gain = 0;
else if (max_gain > 100)
max_gain = 100;
/* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
real_gain = (gain * max_gain) / 100;
} else {
real_gain = gain;
}
if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
return -1;
}
return 0;
}
/*
* Makes the device autocenter, 0 disables.
*/
int
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
SDL_SetError("Haptic: Device does not support setting autocenter.");
return -1;
}
if ((autocenter < 0) || (autocenter > 100)) {
SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
return -1;
}
if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
return -1;
}
return 0;
}
/*
* Pauses the haptic device.
*/
int
SDL_HapticPause(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
SDL_SetError("Haptic: Device does not support setting pausing.");
return -1;
}
return SDL_SYS_HapticPause(haptic);
}
/*
* Unpauses the haptic device.
*/
int
SDL_HapticUnpause(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
return 0; /* Not going to be paused, so we pretend it's unpaused. */
}
return SDL_SYS_HapticUnpause(haptic);
}
/*
* Stops all the currently playing effects.
*/
int
SDL_HapticStopAll(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return SDL_SYS_HapticStopAll(haptic);
}

201
src/haptic/SDL_syshaptic.h Normal file
View file

@ -0,0 +1,201 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_haptic.h"
/*
* Number of haptic devices on the system.
*/
extern Uint8 SDL_numhaptics;
struct haptic_effect
{
SDL_HapticEffect effect; /* The current event */
struct haptic_hweffect *hweffect; /* The hardware behind the event */
};
/*
* The real SDL_Haptic struct.
*/
struct _SDL_Haptic
{
Uint8 index; /* Stores index it is attached to */
struct haptic_effect *effects; /* Allocated effects */
int neffects; /* Maximum amount of effects */
int nplaying; /* Maximum amount of effects to play at the same time */
unsigned int supported; /* Supported effects */
int naxes; /* Number of axes on the device. */
struct haptic_hwdata *hwdata; /* Driver dependent */
int ref_count; /* Count for multiple opens */
};
/*
* Scans the system for haptic devices.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticInit(void);
/*
* Gets the device dependent name of the haptic device
*/
extern const char *SDL_SYS_HapticName(int index);
/*
* Opens the haptic device for usage. The haptic device should have
* the index value set previously.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticOpen(SDL_Haptic * haptic);
/*
* Returns the index of the haptic core pointer or -1 if none is found.
*/
int SDL_SYS_HapticMouse(void);
/*
* Checks to see if the joystick has haptic capabilities.
*
* Returns >0 if haptic capabilities are detected, 0 if haptic
* capabilities aren't detected and -1 on error.
*/
extern int SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick);
/*
* Opens the haptic device for usage using the same device as
* the joystick.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic,
SDL_Joystick * joystick);
/*
* Checks to see if haptic device and joystick device are the same.
*
* Returns 1 if they are the same, 0 if they aren't.
*/
extern int SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic,
SDL_Joystick * joystick);
/*
* Closes a haptic device after usage.
*/
extern void SDL_SYS_HapticClose(SDL_Haptic * haptic);
/*
* Performs a cleanup on the haptic subsystem.
*/
extern void SDL_SYS_HapticQuit(void);
/*
* Creates a new haptic effect on the haptic device using base
* as a template for the effect.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
SDL_HapticEffect * base);
/*
* Updates the haptic effect on the haptic device using data
* as a template.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
SDL_HapticEffect * data);
/*
* Runs the effect on the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
Uint32 iterations);
/*
* Stops the effect on the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticStopEffect(SDL_Haptic * haptic,
struct haptic_effect *effect);
/*
* Cleanups up the effect on the haptic device.
*/
extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic,
struct haptic_effect *effect);
/*
* Queries the device for the status of effect.
*
* Returns 0 if device is stopped, >0 if device is playing and
* -1 on error.
*/
extern int SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
struct haptic_effect *effect);
/*
* Sets the global gain of the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain);
/*
* Sets the autocenter feature of the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
/*
* Pauses the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticPause(SDL_Haptic * haptic);
/*
* Unpauses the haptic device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticUnpause(SDL_Haptic * haptic);
/*
* Stops all the currently playing haptic effects on the device.
*
* Returns 0 on success, -1 on error.
*/
extern int SDL_SYS_HapticStopAll(SDL_Haptic * haptic);

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,194 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#if defined(SDL_HAPTIC_DUMMY) || defined(SDL_HAPTIC_DISABLED)
#include "SDL_haptic.h"
#include "../SDL_syshaptic.h"
static int
SDL_SYS_LogicError(void)
{
SDL_SetError("Logic error: No haptic devices available.");;
}
int
SDL_SYS_HapticInit(void)
{
return 0;
}
const char *
SDL_SYS_HapticName(int index)
{
SDL_SYS_LogicError();
return NULL;
}
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticMouse(void)
{
return -1;
}
int
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
{
return 0;
}
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
return 0;
}
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
return;
}
void
SDL_SYS_HapticQuit(void)
{
return;
}
int
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
struct haptic_effect *effect, SDL_HapticEffect * base)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
SDL_HapticEffect * data)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
Uint32 iterations)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
SDL_SYS_LogicError();
return -1;
}
void
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
SDL_SYS_LogicError();
return;
}
int
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
struct haptic_effect *effect)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticPause(SDL_Haptic * haptic)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
{
SDL_SYS_LogicError();
return -1;
}
int
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
{
SDL_SYS_LogicError();
return -1;
}
#endif /* SDL_HAPTIC_DUMMY || SDL_HAPTIC_DISABLED */

View file

@ -0,0 +1,956 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifdef SDL_HAPTIC_LINUX
#include "SDL_haptic.h"
#include "../SDL_syshaptic.h"
#include "SDL_joystick.h"
#include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
#include "../../joystick/linux/SDL_sysjoystick_c.h" /* For joystick hwdata */
#include <unistd.h> /* close */
#include <linux/input.h> /* Force feedback linux stuff. */
#include <fcntl.h> /* O_RDWR */
#include <limits.h> /* INT_MAX */
#include <errno.h> /* errno, strerror */
#include <math.h> /* atan2 */
/* Just in case. */
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
#define MAX_HAPTICS 32 /* It's doubtful someone has more then 32 evdev */
/*
* List of available haptic devices.
*/
static struct
{
char *fname; /* Dev path name (like /dev/input/event1) */
SDL_Haptic *haptic; /* Assosciated haptic. */
} SDL_hapticlist[MAX_HAPTICS];
/*
* Haptic system hardware data.
*/
struct haptic_hwdata
{
int fd; /* File descriptor of the device. */
char *fname; /* Points to the name in SDL_hapticlist. */
};
/*
* Haptic system effect data.
*/
struct haptic_hweffect
{
struct ff_effect effect; /* The linux kernel effect structure. */
};
#define test_bit(nr, addr) \
(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
#define EV_TEST(ev,f) \
if (test_bit((ev), features)) ret |= (f);
/*
* Test whether a device has haptic properties.
* Returns available properties or 0 if there are none.
*/
static int
EV_IsHaptic(int fd)
{
unsigned int ret;
unsigned long features[1 + FF_MAX / sizeof(unsigned long)];
/* Ask device for what it has. */
ret = 0;
if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
SDL_SetError("Haptic: Unable to get device's features: %s",
strerror(errno));
return -1;
}
/* Convert supported features to SDL_HAPTIC platform-neutral features. */
EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE);
EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
/* Return what it supports. */
return ret;
}
/*
* Tests whether a device is a mouse or not.
*/
static int
EV_IsMouse(int fd)
{
unsigned long argp[40];
/* Ask for supported features. */
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
return -1;
}
/* Currently we only test for BTN_MOUSE which can give fake positives. */
if (test_bit(BTN_MOUSE, argp) != 0) {
return 1;
}
return 0;
}
/*
* Initializes the haptic subsystem by finding available devices.
*/
int
SDL_SYS_HapticInit(void)
{
const char joydev_pattern[] = "/dev/input/event%d";
dev_t dev_nums[MAX_HAPTICS];
char path[PATH_MAX];
struct stat sb;
int fd;
int i, j, k;
int duplicate;
int numhaptics;
numhaptics = 0;
/*
* Limit amount of checks to MAX_HAPTICS since we may or may not have
* permission to some or all devices.
*/
i = 0;
for (j = 0; j < MAX_HAPTICS; ++j) {
snprintf(path, PATH_MAX, joydev_pattern, i++);
/* check to see if file exists */
if (stat(path, &sb) != 0)
break;
/* check for duplicates */
duplicate = 0;
for (k = 0; (k < numhaptics) && !duplicate; ++k) {
if (sb.st_rdev == dev_nums[k]) {
duplicate = 1;
}
}
if (duplicate) {
continue;
}
/* try to open */
fd = open(path, O_RDWR, 0);
if (fd < 0)
continue;
#ifdef DEBUG_INPUT_EVENTS
printf("Checking %s\n", path);
#endif
/* see if it works */
if (EV_IsHaptic(fd) > 0) {
SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
SDL_hapticlist[numhaptics].haptic = NULL;
dev_nums[numhaptics] = sb.st_rdev;
++numhaptics;
}
close(fd);
}
return numhaptics;
}
/*
* Gets the name from a file descriptor.
*/
static const char *
SDL_SYS_HapticNameFromFD(int fd)
{
static char namebuf[128];
/* We use the evdev name ioctl. */
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
return NULL;
}
return namebuf;
}
/*
* Return the name of a haptic device, does not need to be opened.
*/
const char *
SDL_SYS_HapticName(int index)
{
int fd;
const char *name;
/* Open the haptic device. */
name = NULL;
fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
if (fd >= 0) {
name = SDL_SYS_HapticNameFromFD(fd);
if (name == NULL) {
/* No name found, return device character device */
name = SDL_hapticlist[index].fname;
}
}
close(fd);
return name;
}
/*
* Opens the haptic device from the file descriptor.
*/
static int
SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
{
const char *name;
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
SDL_OutOfMemory();
goto open_err;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* Set the data. */
haptic->hwdata->fd = fd;
haptic->supported = EV_IsHaptic(fd);
haptic->naxes = 2; /* Hardcoded for now, not sure if it's possible to find out. */
/* Set the effects */
if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
SDL_SetError("Haptic: Unable to query device memory: %s",
strerror(errno));
goto open_err;
}
haptic->nplaying = haptic->neffects; /* Linux makes no distinction. */
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (haptic->effects == NULL) {
SDL_OutOfMemory();
goto open_err;
}
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
return 0;
/* Error handling */
open_err:
close(fd);
if (haptic->hwdata != NULL) {
free(haptic->hwdata);
haptic->hwdata = NULL;
}
return -1;
}
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
int fd;
int ret;
/* Open the character device */
fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
if (fd < 0) {
SDL_SetError("Haptic: Unable to open %s: %s",
SDL_hapticlist[haptic->index], strerror(errno));
return -1;
}
/* Try to create the haptic. */
ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
if (ret < 0) {
return -1;
}
/* Set the fname. */
haptic->hwdata->fname = SDL_hapticlist[haptic->index].fname;
return 0;
}
/*
* Opens a haptic device from first mouse it finds for usage.
*/
int
SDL_SYS_HapticMouse(void)
{
int fd;
int i;
for (i = 0; i < SDL_numhaptics; i++) {
/* Open the device. */
fd = open(SDL_hapticlist[i].fname, O_RDWR, 0);
if (fd < 0) {
SDL_SetError("Haptic: Unable to open %s: %s",
SDL_hapticlist[i], strerror(errno));
return -1;
}
/* Is it a mouse? */
if (EV_IsMouse(fd)) {
close(fd);
return i;
}
close(fd);
}
return -1;
}
/*
* Checks to see if a joystick has haptic features.
*/
int
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
{
return EV_IsHaptic(joystick->hwdata->fd);
}
/*
* Checks to see if the haptic device and joystick and in reality the same.
*/
int
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
/* We are assuming linux is using evdev which should trump the old
* joystick methods. */
if (SDL_strcmp(joystick->hwdata->fname, haptic->hwdata->fname) == 0) {
return 1;
}
return 0;
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
int i;
int fd;
int ret;
/* Find the joystick in the haptic list. */
for (i = 0; i < MAX_HAPTICS; i++) {
if (SDL_hapticlist[i].fname != NULL) {
if (SDL_strcmp(SDL_hapticlist[i].fname, joystick->hwdata->fname)
== 0) {
haptic->index = i;
}
}
}
fd = open(joystick->hwdata->fname, O_RDWR, 0);
ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
if (ret < 0) {
return -1;
}
haptic->hwdata->fname = SDL_hapticlist[haptic->index].fname;
return 0;
}
/*
* Closes the haptic device.
*/
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
if (haptic->hwdata) {
/* Free effects. */
SDL_free(haptic->effects);
haptic->effects = NULL;
haptic->neffects = 0;
/* Clean up */
close(haptic->hwdata->fd);
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
/* Clear the rest. */
SDL_memset(haptic, 0, sizeof(SDL_Haptic));
}
/*
* Clean up after system specific haptic stuff
*/
void
SDL_SYS_HapticQuit(void)
{
int i;
for (i = 0; SDL_hapticlist[i].fname != NULL; i++) {
/* Opened and not closed haptics are leaked, this is on purpose.
* Close your haptic devices after usage. */
SDL_free(SDL_hapticlist[i].fname);
}
SDL_hapticlist[0].fname = NULL;
}
/*
* Converts an SDL button to a ff_trigger button.
*/
static Uint16
SDL_SYS_ToButton(Uint16 button)
{
Uint16 ff_button;
ff_button = 0;
/*
* Not sure what the proper syntax is because this actually isn't implemented
* in the current kernel from what I've seen (2.6.26).
*/
if (button != 0) {
ff_button = BTN_GAMEPAD + button - 1;
}
return ff_button;
}
/*
* Returns the ff_effect usable direction from a SDL_HapticDirection.
*/
static Uint16
SDL_SYS_ToDirection(SDL_HapticDirection * dir)
{
Uint32 tmp;
float f; /* Ideally we'd use fixed point math instead of floats... */
switch (dir->type) {
case SDL_HAPTIC_POLAR:
/* Linux directions are inverted. */
tmp = (((18000 + dir->dir[0]) % 36000) * 0xFFFF) / 36000;
return (Uint16) tmp;
case SDL_HAPTIC_CARTESIAN:
/* We must invert "x" and "y" to go clockwise. */
f = atan2(dir->dir[0], dir->dir[1]);
tmp = (int) (f * 18000. / M_PI) % 36000;
tmp = (tmp * 0xFFFF) / 36000;
return (Uint16) tmp;
case SDL_HAPTIC_SPHERICAL:
tmp = (36000 - dir->dir[0]) + 27000; /* Convert to polars */
tmp = (((18000 + tmp) % 36000) * 0xFFFF) / 36000;
return (Uint16) tmp;
default:
SDL_SetError("Haptic: Unsupported direction type.");
return (Uint16) - 1;
}
return 0;
}
#define CLAMP(x) (((x) > 32767) ? 32767 : x)
/*
* Initializes the linux effect struct from a haptic_effect.
* Values above 32767 (for unsigned) are unspecified so we must clamp.
*/
static int
SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect * src)
{
Uint32 tmp;
SDL_HapticConstant *constant;
SDL_HapticPeriodic *periodic;
SDL_HapticCondition *condition;
SDL_HapticRamp *ramp;
/* Clear up */
SDL_memset(dest, 0, sizeof(struct ff_effect));
switch (src->type) {
case SDL_HAPTIC_CONSTANT:
constant = &src->constant;
/* Header */
dest->type = FF_CONSTANT;
dest->direction = SDL_SYS_ToDirection(&constant->direction);
if (dest->direction == (Uint16) - 1)
return -1;
/* Replay */
dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(constant->length);
dest->replay.delay = CLAMP(constant->delay);
/* Trigger */
dest->trigger.button = SDL_SYS_ToButton(constant->button);
dest->trigger.interval = CLAMP(constant->interval);
/* Constant */
dest->u.constant.level = constant->level;
/* Envelope */
dest->u.constant.envelope.attack_length =
CLAMP(constant->attack_length);
dest->u.constant.envelope.attack_level =
CLAMP(constant->attack_level);
dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
break;
case SDL_HAPTIC_SINE:
case SDL_HAPTIC_SQUARE:
case SDL_HAPTIC_TRIANGLE:
case SDL_HAPTIC_SAWTOOTHUP:
case SDL_HAPTIC_SAWTOOTHDOWN:
periodic = &src->periodic;
/* Header */
dest->type = FF_PERIODIC;
dest->direction = SDL_SYS_ToDirection(&periodic->direction);
if (dest->direction == (Uint16) - 1)
return -1;
/* Replay */
dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(periodic->length);
dest->replay.delay = CLAMP(periodic->delay);
/* Trigger */
dest->trigger.button = SDL_SYS_ToButton(periodic->button);
dest->trigger.interval = CLAMP(periodic->interval);
/* Periodic */
if (periodic->type == SDL_HAPTIC_SINE)
dest->u.periodic.waveform = FF_SINE;
else if (periodic->type == SDL_HAPTIC_SQUARE)
dest->u.periodic.waveform = FF_SQUARE;
else if (periodic->type == SDL_HAPTIC_TRIANGLE)
dest->u.periodic.waveform = FF_TRIANGLE;
else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP)
dest->u.periodic.waveform = FF_SAW_UP;
else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN)
dest->u.periodic.waveform = FF_SAW_DOWN;
dest->u.periodic.period = CLAMP(periodic->period);
dest->u.periodic.magnitude = periodic->magnitude;
dest->u.periodic.offset = periodic->offset;
/* Phase is calculated based of offset from period and then clamped. */
tmp = ((periodic->phase % 36000) * dest->u.periodic.period) / 36000;
dest->u.periodic.phase = CLAMP(tmp);
/* Envelope */
dest->u.periodic.envelope.attack_length =
CLAMP(periodic->attack_length);
dest->u.periodic.envelope.attack_level =
CLAMP(periodic->attack_level);
dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
break;
case SDL_HAPTIC_SPRING:
case SDL_HAPTIC_DAMPER:
case SDL_HAPTIC_INERTIA:
case SDL_HAPTIC_FRICTION:
condition = &src->condition;
/* Header */
if (condition->type == SDL_HAPTIC_SPRING)
dest->type = FF_SPRING;
else if (condition->type == SDL_HAPTIC_DAMPER)
dest->type = FF_DAMPER;
else if (condition->type == SDL_HAPTIC_INERTIA)
dest->type = FF_INERTIA;
else if (condition->type == SDL_HAPTIC_FRICTION)
dest->type = FF_FRICTION;
dest->direction = 0; /* Handled by the condition-specifics. */
/* Replay */
dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(condition->length);
dest->replay.delay = CLAMP(condition->delay);
/* Trigger */
dest->trigger.button = SDL_SYS_ToButton(condition->button);
dest->trigger.interval = CLAMP(condition->interval);
/* Condition */
/* X axis */
dest->u.condition[0].right_saturation =
CLAMP(condition->right_sat[0]);
dest->u.condition[0].left_saturation = CLAMP(condition->left_sat[0]);
dest->u.condition[0].right_coeff = condition->right_coeff[0];
dest->u.condition[0].left_coeff = condition->left_coeff[0];
dest->u.condition[0].deadband = CLAMP(condition->deadband[0]);
dest->u.condition[0].center = condition->center[0];
/* Y axis */
dest->u.condition[1].right_saturation =
CLAMP(condition->right_sat[1]);
dest->u.condition[1].left_saturation = CLAMP(condition->left_sat[1]);
dest->u.condition[1].right_coeff = condition->right_coeff[1];
dest->u.condition[1].left_coeff = condition->left_coeff[1];
dest->u.condition[1].deadband = CLAMP(condition->deadband[1]);
dest->u.condition[1].center = condition->center[1];
/*
* There is no envelope in the linux force feedback api for conditions.
*/
break;
case SDL_HAPTIC_RAMP:
ramp = &src->ramp;
/* Header */
dest->type = FF_RAMP;
dest->direction = SDL_SYS_ToDirection(&ramp->direction);
if (dest->direction == (Uint16) - 1)
return -1;
/* Replay */
dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(ramp->length);
dest->replay.delay = CLAMP(ramp->delay);
/* Trigger */
dest->trigger.button = SDL_SYS_ToButton(ramp->button);
dest->trigger.interval = CLAMP(ramp->interval);
/* Ramp */
dest->u.ramp.start_level = ramp->start;
dest->u.ramp.end_level = ramp->end;
/* Envelope */
dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
break;
default:
SDL_SetError("Haptic: Unknown effect type.");
return -1;
}
return 0;
}
/*
* Creates a new haptic effect.
*/
int
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
SDL_HapticEffect * base)
{
struct ff_effect *linux_effect;
/* Allocate the hardware effect */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
if (effect->hweffect == NULL) {
SDL_OutOfMemory();
return -1;
}
/* Prepare the ff_effect */
linux_effect = &effect->hweffect->effect;
if (SDL_SYS_ToFFEffect(linux_effect, base) != 0) {
goto new_effect_err;
}
linux_effect->id = -1; /* Have the kernel give it an id */
/* Upload the effect */
if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
SDL_SetError("Haptic: Error uploading effect to the device: %s",
strerror(errno));
goto new_effect_err;
}
return 0;
new_effect_err:
free(effect->hweffect);
effect->hweffect = NULL;
return -1;
}
/*
* Updates an effect.
*
* Note: Dynamically updating the direction can in some cases force
* the effect to restart and run once.
*/
int
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
SDL_HapticEffect * data)
{
struct ff_effect linux_effect;
/* Create the new effect */
if (SDL_SYS_ToFFEffect(&linux_effect, data) != 0) {
return -1;
}
linux_effect.id = effect->hweffect->effect.id;
/* See if it can be uploaded. */
if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
SDL_SetError("Haptic: Error updating the effect: %s",
strerror(errno));
return -1;
}
/* Copy the new effect into memory. */
SDL_memcpy(&effect->hweffect->effect, &linux_effect,
sizeof(struct ff_effect));
return effect->hweffect->effect.id;
}
/*
* Runs an effect.
*/
int
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
Uint32 iterations)
{
struct input_event run;
/* Prepare to run the effect */
run.type = EV_FF;
run.code = effect->hweffect->effect.id;
/* We don't actually have infinity here, so we just do INT_MAX which is pretty damn close. */
run.value = (iterations > INT_MAX) ? INT_MAX : iterations;
if (write(haptic->hwdata->fd, (const void *) &run, sizeof(run)) < 0) {
SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno));
return -1;
}
return 0;
}
/*
* Stops an effect.
*/
int
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
struct input_event stop;
stop.type = EV_FF;
stop.code = effect->hweffect->effect.id;
stop.value = 0;
if (write(haptic->hwdata->fd, (const void *) &stop, sizeof(stop)) < 0) {
SDL_SetError("Haptic: Unable to stop the effect: %s",
strerror(errno));
return -1;
}
return 0;
}
/*
* Frees the effect.
*/
void
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
SDL_SetError("Haptic: Error removing the effect from the device: %s",
strerror(errno));
}
SDL_free(effect->hweffect);
effect->hweffect = NULL;
}
/*
* Gets the status of a haptic effect.
*/
int
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
struct haptic_effect *effect)
{
#if 0 /* Not supported atm. */
struct input_event ie;
ie.type = EV_FF;
ie.type = EV_FF_STATUS;
ie.code = effect->hweffect->effect.id;
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
SDL_SetError("Haptic: Error getting device status.");
return -1;
}
return 0;
#endif
return -1;
}
/*
* Sets the gain.
*/
int
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
{
struct input_event ie;
ie.type = EV_FF;
ie.code = FF_GAIN;
ie.value = (0xFFFFUL * gain) / 100;
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
SDL_SetError("Haptic: Error setting gain: %s", strerror(errno));
return -1;
}
return 0;
}
/*
* Sets the autocentering.
*/
int
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
struct input_event ie;
ie.type = EV_FF;
ie.code = FF_AUTOCENTER;
ie.value = (0xFFFFUL * autocenter) / 100;
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
SDL_SetError("Haptic: Error setting autocenter: %s", strerror(errno));
return -1;
}
return 0;
}
/*
* Pausing is not supported atm by linux.
*/
int
SDL_SYS_HapticPause(SDL_Haptic * haptic)
{
return -1;
}
/*
* Unpausing is not supported atm by linux.
*/
int
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
{
return -1;
}
/*
* Stops all the currently playing effects.
*/
int
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
{
int i, ret;
/* Linux does not support this natively so we have to loop. */
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
ret = SDL_SYS_HapticStopEffect(haptic, &haptic->effects[i]);
if (ret < 0) {
SDL_SetError
("Haptic: Error while trying to stop all playing effects.");
return -1;
}
}
}
return 0;
}
#endif /* SDL_HAPTIC_LINUX */

File diff suppressed because it is too large Load diff

View file

@ -193,8 +193,12 @@ SDL_JoystickOpened(int device_index)
return (opened); return (opened);
} }
static int
ValidJoystick(SDL_Joystick ** joystick) /*
* Checks to make sure the joystick is valid.
*/
int
SDL_PrivateJoystickValid(SDL_Joystick ** joystick)
{ {
int valid; int valid;
@ -216,7 +220,7 @@ ValidJoystick(SDL_Joystick ** joystick)
int int
SDL_JoystickIndex(SDL_Joystick * joystick) SDL_JoystickIndex(SDL_Joystick * joystick)
{ {
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
return (joystick->index); return (joystick->index);
@ -228,7 +232,7 @@ SDL_JoystickIndex(SDL_Joystick * joystick)
int int
SDL_JoystickNumAxes(SDL_Joystick * joystick) SDL_JoystickNumAxes(SDL_Joystick * joystick)
{ {
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
return (joystick->naxes); return (joystick->naxes);
@ -240,7 +244,7 @@ SDL_JoystickNumAxes(SDL_Joystick * joystick)
int int
SDL_JoystickNumHats(SDL_Joystick * joystick) SDL_JoystickNumHats(SDL_Joystick * joystick)
{ {
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
return (joystick->nhats); return (joystick->nhats);
@ -252,7 +256,7 @@ SDL_JoystickNumHats(SDL_Joystick * joystick)
int int
SDL_JoystickNumBalls(SDL_Joystick * joystick) SDL_JoystickNumBalls(SDL_Joystick * joystick)
{ {
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
return (joystick->nballs); return (joystick->nballs);
@ -264,7 +268,7 @@ SDL_JoystickNumBalls(SDL_Joystick * joystick)
int int
SDL_JoystickNumButtons(SDL_Joystick * joystick) SDL_JoystickNumButtons(SDL_Joystick * joystick)
{ {
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
return (joystick->nbuttons); return (joystick->nbuttons);
@ -278,7 +282,7 @@ SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
{ {
Sint16 state; Sint16 state;
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (0); return (0);
} }
if (axis < joystick->naxes) { if (axis < joystick->naxes) {
@ -298,7 +302,7 @@ SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
{ {
Uint8 state; Uint8 state;
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (0); return (0);
} }
if (hat < joystick->nhats) { if (hat < joystick->nhats) {
@ -318,7 +322,7 @@ SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
{ {
int retval; int retval;
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (-1); return (-1);
} }
@ -347,7 +351,7 @@ SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
{ {
Uint8 state; Uint8 state;
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return (0); return (0);
} }
if (button < joystick->nbuttons) { if (button < joystick->nbuttons) {
@ -367,7 +371,7 @@ SDL_JoystickClose(SDL_Joystick * joystick)
{ {
int i; int i;
if (!ValidJoystick(&joystick)) { if (!SDL_PrivateJoystickValid(&joystick)) {
return; return;
} }

View file

@ -36,4 +36,8 @@ extern int SDL_PrivateJoystickHat(SDL_Joystick * joystick,
Uint8 hat, Uint8 value); Uint8 hat, Uint8 value);
extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick, extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick,
Uint8 button, Uint8 state); Uint8 button, Uint8 state);
/* Internal sanity checking functions */
extern int SDL_PrivateJoystickValid(SDL_Joystick ** joystick);
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -48,59 +48,14 @@
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h> /* for NewPtrClear, DisposePtr */ #include <Carbon/Carbon.h> /* for NewPtrClear, DisposePtr */
/* For force feedback testing. */
#include <ForceFeedback/ForceFeedback.h>
#include <ForceFeedback/ForceFeedbackConstants.h>
#include "SDL_joystick.h" #include "SDL_joystick.h"
#include "../SDL_sysjoystick.h" #include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h" #include "../SDL_joystick_c.h"
#include "SDL_sysjoystick_c.h"
struct recElement
{
IOHIDElementCookie cookie; /* unique value which identifies element, will NOT change */
long min; /* reported min value possible */
long max; /* reported max value possible */
#if 0
/* TODO: maybe should handle the following stuff somehow? */
long scaledMin; /* reported scaled min value possible */
long scaledMax; /* reported scaled max value possible */
long size; /* size in bits of data return from element */
Boolean relative; /* are reports relative to last report (deltas) */
Boolean wrapping; /* does element wrap around (one value higher than max is min) */
Boolean nonLinear; /* are the values reported non-linear relative to element movement */
Boolean preferredState; /* does element have a preferred state (such as a button) */
Boolean nullState; /* does element have null state */
#endif /* 0 */
/* runtime variables used for auto-calibration */
long minReport; /* min returned value */
long maxReport; /* max returned value */
struct recElement *pNext; /* next element in list */
};
typedef struct recElement recElement;
struct joystick_hwdata
{
IOHIDDeviceInterface **interface; /* interface to device, NULL = no interface */
char product[256]; /* name of product */
long usage; /* usage page from IOUSBHID Parser.h which defines general usage */
long usagePage; /* usage within above page from IOUSBHID Parser.h which defines specific usage */
long axes; /* number of axis (calculated, not reported by device) */
long buttons; /* number of buttons (calculated, not reported by device) */
long hats; /* number of hat switches (calculated, not reported by device) */
long elements; /* number of total elements (shouldbe total of above) (calculated, not reported by device) */
recElement *firstAxis;
recElement *firstButton;
recElement *firstHat;
int removed;
int uncentered;
struct joystick_hwdata *pNext; /* next device */
};
typedef struct joystick_hwdata recDevice;
/* Linked list of all available devices */ /* Linked list of all available devices */
@ -594,6 +549,12 @@ HIDDisposeDevice(recDevice ** ppDevice)
/* save next device prior to disposing of this device */ /* save next device prior to disposing of this device */
pDeviceNext = (*ppDevice)->pNext; pDeviceNext = (*ppDevice)->pNext;
/* free posible io_service_t */
if ((*ppDevice)->ffservice) {
IOObjectRelease((*ppDevice)->ffservice);
(*ppDevice)->ffservice = 0;
}
/* free element lists */ /* free element lists */
HIDDisposeElementList(&(*ppDevice)->firstAxis); HIDDisposeElementList(&(*ppDevice)->firstAxis);
HIDDisposeElementList(&(*ppDevice)->firstButton); HIDDisposeElementList(&(*ppDevice)->firstButton);
@ -686,12 +647,6 @@ SDL_SYS_JoystickInit(void)
if (!device) if (!device)
continue; continue;
/* dump device object, it is no longer needed */
result = IOObjectRelease(ioHIDDeviceObject);
/* if (KERN_SUCCESS != result)
HIDReportErrorNum ("IOObjectRelease error with ioHIDDeviceObject.", result);
*/
/* Filter device list to non-keyboard/mouse stuff */ /* Filter device list to non-keyboard/mouse stuff */
if ((device->usagePage != kHIDPage_GenericDesktop) || if ((device->usagePage != kHIDPage_GenericDesktop) ||
((device->usage != kHIDUsage_GD_Joystick && ((device->usage != kHIDUsage_GD_Joystick &&
@ -704,6 +659,14 @@ SDL_SYS_JoystickInit(void)
continue; continue;
} }
/* We have to do some storage of the io_service_t for
* SDL_HapticOpenFromJoystick */
if (FFIsForceFeedback(ioHIDDeviceObject) == FF_OK) {
device->ffservice = ioHIDDeviceObject;
} else {
device->ffservice = 0;
}
/* Add device to the end of the list */ /* Add device to the end of the list */
if (lastDevice) if (lastDevice)
lastDevice->pNext = device; lastDevice->pNext = device;

View file

@ -0,0 +1,87 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2004 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifndef SDL_JOYSTICK_IOKIT_H
#if MAC_OS_X_VERSION_MIN_REQUIRED == 1030
#include "10.3.9-FIX/IOHIDLib.h"
#else
#include <IOKit/hid/IOHIDLib.h>
#endif
#include <IOKit/hid/IOHIDKeys.h>
struct recElement
{
IOHIDElementCookie cookie; /* unique value which identifies element, will NOT change */
long min; /* reported min value possible */
long max; /* reported max value possible */
#if 0
/* TODO: maybe should handle the following stuff somehow? */
long scaledMin; /* reported scaled min value possible */
long scaledMax; /* reported scaled max value possible */
long size; /* size in bits of data return from element */
Boolean relative; /* are reports relative to last report (deltas) */
Boolean wrapping; /* does element wrap around (one value higher than max is min) */
Boolean nonLinear; /* are the values reported non-linear relative to element movement */
Boolean preferredState; /* does element have a preferred state (such as a button) */
Boolean nullState; /* does element have null state */
#endif /* 0 */
/* runtime variables used for auto-calibration */
long minReport; /* min returned value */
long maxReport; /* max returned value */
struct recElement *pNext; /* next element in list */
};
typedef struct recElement recElement;
struct joystick_hwdata
{
io_service_t ffservice; /* Interface for force feedback, 0 = no ff */
IOHIDDeviceInterface **interface; /* interface to device, NULL = no interface */
char product[256]; /* name of product */
long usage; /* usage page from IOUSBHID Parser.h which defines general usage */
long usagePage; /* usage within above page from IOUSBHID Parser.h which defines specific usage */
long axes; /* number of axis (calculated, not reported by device) */
long buttons; /* number of buttons (calculated, not reported by device) */
long hats; /* number of hat switches (calculated, not reported by device) */
long elements; /* number of total elements (shouldbe total of above) (calculated, not reported by device) */
recElement *firstAxis;
recElement *firstButton;
recElement *firstHat;
int removed;
int uncentered;
struct joystick_hwdata *pNext; /* next device */
};
typedef struct joystick_hwdata recDevice;
#endif /* SDL_JOYSTICK_IOKIT_H */

View file

@ -31,13 +31,11 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <limits.h> /* For the definition of PATH_MAX */ #include <limits.h> /* For the definition of PATH_MAX */
#include <linux/joystick.h> #include <linux/joystick.h>
#if SDL_INPUT_LINUXEV
#include <linux/input.h>
#endif
#include "SDL_joystick.h" #include "SDL_joystick.h"
#include "../SDL_sysjoystick.h" #include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h" #include "../SDL_joystick_c.h"
#include "SDL_sysjoystick_c.h"
/* Special joystick configurations */ /* Special joystick configurations */
static struct static struct
@ -278,35 +276,6 @@ static struct
} SDL_joylist[MAX_JOYSTICKS]; } SDL_joylist[MAX_JOYSTICKS];
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
int fd;
/* The current linux joystick driver maps hats to two axes */
struct hwdata_hat
{
int axis[2];
} *hats;
/* The current linux joystick driver maps balls to two axes */
struct hwdata_ball
{
int axis[2];
} *balls;
/* Support for the Linux 2.4 unified input interface */
#if SDL_INPUT_LINUXEV
SDL_bool is_hid;
Uint8 key_map[KEY_MAX - BTN_MISC];
Uint8 abs_map[ABS_MAX];
struct axis_correct
{
int used;
int coef[3];
} abs_correct[ABS_MAX];
#endif
};
#ifndef NO_LOGICAL_JOYSTICKS #ifndef NO_LOGICAL_JOYSTICKS
static int static int
@ -805,6 +774,7 @@ int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick) SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
{ {
int fd; int fd;
char *fname;
SDL_logical_joydecl(int realindex); SDL_logical_joydecl(int realindex);
SDL_logical_joydecl(SDL_Joystick * realjoy = NULL); SDL_logical_joydecl(SDL_Joystick * realjoy = NULL);
@ -818,13 +788,16 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
return (-1); return (-1);
fd = realjoy->hwdata->fd; fd = realjoy->hwdata->fd;
fname = realjoy->hwdata->fname;
} else { } else {
fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0); fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
fname = SDL_joylist[joystick->index].fname;
} }
SDL_joylist[joystick->index].joy = joystick; SDL_joylist[joystick->index].joy = joystick;
#else #else
fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0); fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
fname = SDL_joylist[joystick->index].fname;
#endif #endif
if (fd < 0) { if (fd < 0) {
@ -840,6 +813,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
} }
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
joystick->hwdata->fd = fd; joystick->hwdata->fd = fd;
joystick->hwdata->fname = fname;
/* Set the joystick to non-blocking read mode */ /* Set the joystick to non-blocking read mode */
fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFL, O_NONBLOCK);

View file

@ -0,0 +1,55 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#if SDL_INPUT_LINUXEV
#include <linux/input.h>
#endif
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
int fd;
char *fname; /* Used in haptic subsystem */
/* The current linux joystick driver maps hats to two axes */
struct hwdata_hat
{
int axis[2];
} *hats;
/* The current linux joystick driver maps balls to two axes */
struct hwdata_ball
{
int axis[2];
} *balls;
/* Support for the Linux 2.4 unified input interface */
#if SDL_INPUT_LINUXEV
SDL_bool is_hid;
Uint8 key_map[KEY_MAX - BTN_MISC];
Uint8 abs_map[ABS_MAX];
struct axis_correct
{
int used;
int coef[3];
} abs_correct[ABS_MAX];
#endif
};

View file

@ -38,20 +38,7 @@
#include "SDL_joystick.h" #include "SDL_joystick.h"
#include "../SDL_sysjoystick.h" #include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h" #include "../SDL_joystick_c.h"
#include "SDL_dxjoystick_c.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0500
#include <dinput.h>
#ifdef _MSC_VER
/* Used for the c_dfDIJoystick2 symbol (no imports are used) */
# pragma comment (lib, "dinput.lib")
#endif
#include <dxerr9.h> /* From DirectX SDK 9c */
#ifdef _MSC_VER
# pragma comment (lib, "dxerr9.lib")
#endif
/* an ISO hack for VisualC++ */ /* an ISO hack for VisualC++ */
#ifdef _MSC_VER #ifdef _MSC_VER
@ -60,14 +47,12 @@
#define INPUT_QSIZE 32 /* Buffer up to 32 input messages */ #define INPUT_QSIZE 32 /* Buffer up to 32 input messages */
#define MAX_JOYSTICKS 8 #define MAX_JOYSTICKS 8
#define MAX_INPUTS 256 /* each joystick can have up to 256 inputs */
#define AXIS_MIN -32768 /* minimum value for axis coordinate */ #define AXIS_MIN -32768 /* minimum value for axis coordinate */
#define AXIS_MAX 32767 /* maximum value for axis coordinate */ #define AXIS_MAX 32767 /* maximum value for axis coordinate */
#define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */ #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
/* external variables referenced. */ /* external variables referenced. */
extern HINSTANCE SDL_Instance; extern HWND SDL_HelperWindow;
extern HWND SDL_Window;
/* local variables */ /* local variables */
@ -95,39 +80,12 @@ static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
Uint8 button, Uint8 state); Uint8 button, Uint8 state);
/* local types */
typedef enum Type
{ BUTTON, AXIS, HAT } Type;
typedef struct input_t
{
/* DirectInput offset for this input type: */
DWORD ofs;
/* Button, axis or hat: */
Type type;
/* SDL input offset: */
Uint8 num;
} input_t;
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
LPDIRECTINPUTDEVICE2 InputDevice;
DIDEVCAPS Capabilities;
int buffered;
input_t Inputs[MAX_INPUTS];
int NumInputs;
};
/* Convert a DirectInput return code to a text message */ /* Convert a DirectInput return code to a text message */
static void static void
SetDIerror(const char *function, HRESULT code) SetDIerror(const char *function, HRESULT code)
{ {
SDL_SetError("%s() [%s]: %s", function, SDL_SetError("%s() [%s]: %s", function,
DXGetErrorString9(code), DXGetErrorDescription9(code)); DXGetErrorString(code), DXGetErrorDescription(code));
} }
@ -140,6 +98,7 @@ int
SDL_SYS_JoystickInit(void) SDL_SYS_JoystickInit(void)
{ {
HRESULT result; HRESULT result;
HINSTANCE instance;
SYS_NumJoysticks = 0; SYS_NumJoysticks = 0;
@ -158,8 +117,13 @@ SDL_SYS_JoystickInit(void)
} }
/* Because we used CoCreateInstance, we need to Initialize it, first. */ /* Because we used CoCreateInstance, we need to Initialize it, first. */
result = instance = GetModuleHandle(NULL);
IDirectInput_Initialize(dinput, SDL_Instance, DIRECTINPUT_VERSION); if (instance == NULL) {
SDL_SetError("GetModuleHandle() failed with error code %d.",
GetLastError());
return (-1);
}
result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
if (FAILED(result)) { if (FAILED(result)) {
SetDIerror("IDirectInput::Initialize", result); SetDIerror("IDirectInput::Initialize", result);
@ -178,8 +142,8 @@ SDL_SYS_JoystickInit(void)
static BOOL CALLBACK static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext) EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{ {
memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
sizeof(DIDEVICEINSTANCE)); sizeof(DIDEVICEINSTANCE));
SYS_NumJoysticks++; SYS_NumJoysticks++;
if (SYS_NumJoysticks >= MAX_JOYSTICKS) if (SYS_NumJoysticks >= MAX_JOYSTICKS)
@ -208,19 +172,19 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
LPDIRECTINPUTDEVICE device; LPDIRECTINPUTDEVICE device;
DIPROPDWORD dipdw; DIPROPDWORD dipdw;
ZeroMemory(&dipdw, sizeof(DIPROPDWORD)); SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
/* allocate memory for system specific hardware data */ /* allocate memory for system specific hardware data */
joystick->hwdata = joystick->hwdata =
(struct joystick_hwdata *) malloc(sizeof(struct joystick_hwdata)); (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
if (joystick->hwdata == NULL) { if (joystick->hwdata == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return (-1); return (-1);
} }
ZeroMemory(joystick->hwdata, sizeof(struct joystick_hwdata)); SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
joystick->hwdata->buffered = 1; joystick->hwdata->buffered = 1;
joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS); joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
@ -250,7 +214,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
* though. */ * though. */
result = result =
IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata-> IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->
InputDevice, SDL_Window, InputDevice, SDL_HelperWindow,
DISCL_EXCLUSIVE | DISCL_EXCLUSIVE |
DISCL_BACKGROUND); DISCL_BACKGROUND);
if (FAILED(result)) { if (FAILED(result)) {
@ -635,7 +599,7 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
if (joystick->hwdata != NULL) { if (joystick->hwdata != NULL) {
/* free system specific hardware data */ /* free system specific hardware data */
free(joystick->hwdata); SDL_free(joystick->hwdata);
} }
} }

View file

@ -0,0 +1,81 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifndef SDL_JOYSTICK_DINPUT_H
/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
* A. Formiga's WINMM driver.
*
* Hats and sliders are completely untested; the app I'm writing this for mostly
* doesn't use them and I don't own any joysticks with them.
*
* We don't bother to use event notification here. It doesn't seem to work
* with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
* let it return 0 events. */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0700 /* Need version 7 for force feedback. */
#include <dinput.h>
#ifdef _MSC_VER
/* Used for the c_dfDIJoystick2 symbol (no imports are used) */
# pragma comment (lib, "dinput.lib")
#endif
#include <dxerr.h>
#ifdef _MSC_VER
# pragma comment (lib, "dxerr.lib")
#endif
#define MAX_INPUTS 256 /* each joystick can have up to 256 inputs */
/* local types */
typedef enum Type
{ BUTTON, AXIS, HAT } Type;
typedef struct input_t
{
/* DirectInput offset for this input type: */
DWORD ofs;
/* Button, axis or hat: */
Type type;
/* SDL input offset: */
Uint8 num;
} input_t;
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
LPDIRECTINPUTDEVICE2 InputDevice;
DIDEVCAPS Capabilities;
int buffered;
input_t Inputs[MAX_INPUTS];
int NumInputs;
};
#endif /* SDL_JOYSTICK_DINPUT_H */

View file

@ -47,6 +47,12 @@
extern HCTX *g_hCtx; /* the table of tablet event contexts, each windows has to have it's own tablet context */ extern HCTX *g_hCtx; /* the table of tablet event contexts, each windows has to have it's own tablet context */
int highestId = 0; /* the highest id of the tablet context */ int highestId = 0; /* the highest id of the tablet context */
/* Fake window to help with DirectInput events. */
HWND SDL_HelperWindow = NULL;
static const char *SDL_HelperWindowClassName = "SDLHelperWindowInputCatcher";
static const char *SDL_HelperWindowName = "SDLHelperWindowInputMsgWindow";
static ATOM SDL_HelperWindowClass = 0;
static int static int
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created) SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
{ {
@ -470,4 +476,66 @@ WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
} }
} }
/*
* Creates a HelperWindow used for DirectInput events.
*/
int
SDL_HelperWindowCreate(void)
{
HINSTANCE hInstance = GetModuleHandleA(NULL);
WNDCLASSEX wce;
/* Create the class. */
SDL_memset(&wce, 0, sizeof(wce));
wce.cbSize = sizeof(WNDCLASSEX);
wce.lpfnWndProc = DefWindowProcA;
wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
wce.hInstance = hInstance;
/* Register the class. */
SDL_HelperWindowClass = RegisterClassExA(&wce);
if (SDL_HelperWindowClass == 0) {
SDL_SetError("Unable to create Helper Window Class: error %d.",
GetLastError());
return -1;
}
/* Create the window. */
SDL_HelperWindow = CreateWindowExA(0, SDL_HelperWindowClassName,
SDL_HelperWindowName,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, HWND_MESSAGE, NULL,
hInstance, NULL);
if (SDL_HelperWindow == NULL) {
SDL_SetError("Unable to create Helper Window: error %d.",
GetLastError());
return -1;
}
return 0;
}
/*
* Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
*/
void
SDL_HelperWindowDestroy(void)
{
/* Destroy the window. */
if (SDL_HelperWindow) {
DestroyWindow(SDL_HelperWindow);
SDL_HelperWindow = NULL;
}
/* Unregister the class. */
if (SDL_HelperWindowClass) {
UnregisterClassA(SDL_HelperWindowClassName, GetModuleHandleA(NULL));
SDL_HelperWindowClass = 0;
}
}
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -7,7 +7,7 @@ EXE = @EXE@
CFLAGS = @CFLAGS@ CFLAGS = @CFLAGS@
LIBS = @LIBS@ LIBS = @LIBS@
TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE)
all: Makefile $(TARGETS) all: Makefile $(TARGETS)
@ -125,6 +125,9 @@ torturethread$(EXE): $(srcdir)/torturethread.c
testloadso$(EXE): $(srcdir)/testloadso.c testloadso$(EXE): $(srcdir)/testloadso.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) $(CC) -o $@ $? $(CFLAGS) $(LIBS)
testhaptic$(EXE): $(srcdir)/testhaptic.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
clean: clean:
rm -f $(TARGETS) rm -f $(TARGETS)

266
test/testhaptic.c Normal file
View file

@ -0,0 +1,266 @@
/*
Copyright (c) 2008, Edgar Simo Serra
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* includes
*/
#include "SDL.h"
#include "SDL_haptic.h"
#include <stdio.h> /* printf */
#include <string.h> /* strstr */
static SDL_Haptic *haptic;
/*
* prototypes
*/
static void abort_execution(void);
static void HapticPrintSupported(SDL_Haptic * haptic);
/**
* @brief The entry point of this force feedback demo.
* @param[in] argc Number of arguments.
* @param[in] argv Array of argc arguments.
*/
int
main(int argc, char **argv)
{
int i;
char *name;
SDL_HapticEffect efx[5];
int id[5];
int nefx;
unsigned int supported;
name = NULL;
if (argc > 1) {
name = argv[1];
if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
printf("USAGE: %s [device name]\n"
"If device name is specified, it will try to find a device whose name\n"
"contains device name for testing.\n", argv[0]);
return 0;
}
}
/* Initialize the force feedbackness */
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
SDL_INIT_HAPTIC);
printf("%d Haptic devices detected.\n", SDL_NumHaptics());
if (SDL_NumHaptics() > 0) {
/* We'll just use the first force feedback device found */
if (name == NULL) {
i = 0;
}
/* Try to find matching device */
else {
for (i = 0; i < SDL_NumHaptics(); i++) {
if (strstr(SDL_HapticName(i), name) != NULL)
break;
}
if (i >= SDL_NumHaptics()) {
printf("Unable to find device matching '%s', aborting.\n",
name);
return 1;
}
}
haptic = SDL_HapticOpen(i);
if (haptic == NULL) {
perror("Unable to create the haptic device");
return 1;
}
printf("Device: %s\n", SDL_HapticName(i));
HapticPrintSupported(haptic);
} else {
printf("No Haptic devices found!\n");
return 1;
}
/* We only want force feedback errors. */
SDL_ClearError();
/* Create effects. */
memset(&efx, 0, sizeof(efx));
nefx = 0;
supported = SDL_HapticQuery(haptic);
printf("\nUploading effects\n");
/* First we'll try a SINE effect. */
if (supported & SDL_HAPTIC_SINE) {
printf(" effect %d: Sine Wave\n", nefx);
efx[nefx].type = SDL_HAPTIC_SINE;
efx[nefx].periodic.period = 1000;
efx[nefx].periodic.magnitude = 0x4000;
efx[nefx].periodic.length = 5000;
efx[nefx].periodic.attack_length = 1000;
efx[nefx].periodic.fade_length = 1000;
id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
if (id[nefx] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
abort_execution();
}
nefx++;
}
/* Now we'll try a SAWTOOTHUP */
if (supported & SDL_HAPTIC_SAWTOOTHUP) {
printf(" effect %d: Sawtooth Up\n", nefx);
efx[nefx].type = SDL_HAPTIC_SQUARE;
efx[nefx].periodic.period = 500;
efx[nefx].periodic.magnitude = 0x5000;
efx[nefx].periodic.length = 5000;
efx[nefx].periodic.attack_length = 1000;
efx[nefx].periodic.fade_length = 1000;
id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
if (id[nefx] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
abort_execution();
}
nefx++;
}
/* Now the classical constant effect. */
if (supported & SDL_HAPTIC_CONSTANT) {
printf(" effect %d: Constant Force\n", nefx);
efx[nefx].type = SDL_HAPTIC_CONSTANT;
efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
efx[nefx].constant.length = 5000;
efx[nefx].constant.level = 0x6000;
efx[nefx].constant.attack_length = 1000;
efx[nefx].constant.fade_length = 1000;
id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
if (id[nefx] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
abort_execution();
}
nefx++;
}
/* The cute spring effect. */
if (supported & SDL_HAPTIC_SPRING) {
printf(" effect %d: Condition Spring\n", nefx);
efx[nefx].type = SDL_HAPTIC_SPRING;
efx[nefx].condition.length = 5000;
for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
efx[nefx].condition.right_sat[i] = 0x7FFF;
efx[nefx].condition.left_sat[i] = 0x7FFF;
efx[nefx].condition.right_coeff[i] = 0x2000;
efx[nefx].condition.left_coeff[i] = 0x2000;
efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
}
id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
if (id[nefx] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
abort_execution();
}
nefx++;
}
/* The pretty awesome inertia effect. */
if (supported & SDL_HAPTIC_INERTIA) {
printf(" effect %d: Condition Inertia\n", nefx);
efx[nefx].type = SDL_HAPTIC_SPRING;
efx[nefx].condition.length = 5000;
for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
efx[nefx].condition.right_sat[i] = 0x7FFF;
efx[nefx].condition.left_sat[i] = 0x7FFF;
efx[nefx].condition.right_coeff[i] = 0x2000;
efx[nefx].condition.left_coeff[i] = 0x2000;
}
id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
if (id[nefx] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
abort_execution();
}
nefx++;
}
printf
("\nNow playing effects for 5 seconds each with 1 second delay between\n");
for (i = 0; i < nefx; i++) {
printf(" Playing effect %d\n", i);
SDL_HapticRunEffect(haptic, id[i], 1);
SDL_Delay(6000); /* Effects only have length 5000 */
}
/* Quit */
if (haptic != NULL)
SDL_HapticClose(haptic);
SDL_Quit();
return 0;
}
/*
* Cleans up a bit.
*/
static void
abort_execution(void)
{
printf("\nAborting program execution.\n");
SDL_HapticClose(haptic);
SDL_Quit();
exit(1);
}
/*
* Displays information about the haptic device.
*/
static void
HapticPrintSupported(SDL_Haptic * haptic)
{
unsigned int supported;
supported = SDL_HapticQuery(haptic);
printf(" Supported effects [%d effects, %d playing]:\n",
SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
if (supported & SDL_HAPTIC_CONSTANT)
printf(" constant\n");
if (supported & SDL_HAPTIC_SINE)
printf(" sine\n");
if (supported & SDL_HAPTIC_SQUARE)
printf(" square\n");
if (supported & SDL_HAPTIC_TRIANGLE)
printf(" triangle\n");
if (supported & SDL_HAPTIC_SAWTOOTHUP)
printf(" sawtoothup\n");
if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
printf(" sawtoothdown\n");
if (supported & SDL_HAPTIC_RAMP)
printf(" ramp\n");
if (supported & SDL_HAPTIC_FRICTION)
printf(" friction\n");
if (supported & SDL_HAPTIC_SPRING)
printf(" spring\n");
if (supported & SDL_HAPTIC_DAMPER)
printf(" damper\n");
if (supported & SDL_HAPTIC_INERTIA)
printf(" intertia\n");
if (supported & SDL_HAPTIC_CUSTOM)
printf(" custom\n");
printf(" Supported capabilities:\n");
if (supported & SDL_HAPTIC_GAIN)
printf(" gain\n");
if (supported & SDL_HAPTIC_AUTOCENTER)
printf(" autocenter\n");
if (supported & SDL_HAPTIC_STATUS)
printf(" status\n");
}