From 4a90a37fd776d42a83224b0ae5155f8129e6213d Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Wed, 2 Jul 2008 08:24:35 +0000 Subject: [PATCH] Exposed some of the joystick stuff to the haptic subsystem. Added SDL_JoystickIsHaptic(). --- include/SDL_haptic.h | 17 +++++++++ src/haptic/SDL_haptic.c | 34 +++++++++++++++++ src/haptic/linux/SDL_syshaptic.c | 25 +++++++++++- src/joystick/SDL_joystick.c | 28 ++++++++------ src/joystick/SDL_joystick_c.h | 4 ++ src/joystick/linux/SDL_sysjoystick.c | 33 +--------------- src/joystick/linux/SDL_sysjoystick_c.h | 53 ++++++++++++++++++++++++++ 7 files changed, 149 insertions(+), 45 deletions(-) create mode 100644 src/joystick/linux/SDL_sysjoystick_c.h diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index 4aa86fed6..d25d56371 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -31,6 +31,7 @@ #include "SDL_stdinc.h" #include "SDL_error.h" +#include "SDL_joystick.h" #include "begin_code.h" /* Set up for C function definitions, even when using C++ */ @@ -207,6 +208,22 @@ extern DECLSPEC const char *SDLCALL SDL_HapticName(int device_index); */ extern DECLSPEC SDL_Haptic * SDL_HapticOpen(int device_index); +/* + * Checks to see if a joystick has haptic features. + * + * Returns SDL_TRUE if the joystick is haptic, SDL_FALSE if it isn't + * and -1 on error. + */ +extern DECLSPEC int SDL_JoystickIsHaptic(SDL_Joystick * joystick); + +/* + * Opens a Haptic device for usage from a Joystick device. + * + * Returns a valid pointer to a haptic device on success or NULL + * if an error occurred. + */ +extern DECLSPEC SDL_Haptic * SDL_HapticOpenFromJoystick(SDL_Joystick * joystick); + /* * Closes a Haptic device previously opened with SDL_HapticOpen. */ diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 84fb5bada..c983e2e31 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -23,6 +23,7 @@ #include "SDL_haptic_c.h" #include "SDL_syshaptic.h" +#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */ static Uint8 SDL_numhaptics = 0; @@ -130,6 +131,39 @@ SDL_HapticOpen(int device_index) } +/* + * Returns SDL_TRUE if joystick has haptic features. + */ +int +SDL_JoystickIsHaptic(SDL_Joystick * joystick) +{ + int ret; + + 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) +{ + if (!SDL_PrivateJoystickValid(&joystick)) { + return -1; + } + return -1; +} + + /* * Checks to see if the haptic device is valid */ diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 606eeb94e..5f830e657 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -26,6 +26,9 @@ #include "SDL_haptic.h" #include "../SDL_haptic_c.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 /* close */ #include @@ -243,6 +246,27 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic) } +/* + * Checks to see if a joystick has haptic features. + */ +int +SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick) +{ + if (EV_IsHaptic(joystick->hwdata->fd) > 0) + return 1; + return 0; +} + + +/* + * Opens a SDL_Haptic from a SDL_Joystick. + */ +int +SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick) +{ +} + + /* * Closes the haptic device. */ @@ -604,5 +628,4 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) } - #endif /* SDL_HAPTIC_LINUX */ diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index c230da858..964689d4d 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -193,8 +193,12 @@ SDL_JoystickOpened(int device_index) return (opened); } -static int -ValidJoystick(SDL_Joystick ** joystick) + +/* + * Checks to make sure the joystick is valid. + */ +int +SDL_PrivateJoystickValid(SDL_Joystick ** joystick) { int valid; @@ -216,7 +220,7 @@ ValidJoystick(SDL_Joystick ** joystick) int SDL_JoystickIndex(SDL_Joystick * joystick) { - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } return (joystick->index); @@ -228,7 +232,7 @@ SDL_JoystickIndex(SDL_Joystick * joystick) int SDL_JoystickNumAxes(SDL_Joystick * joystick) { - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } return (joystick->naxes); @@ -240,7 +244,7 @@ SDL_JoystickNumAxes(SDL_Joystick * joystick) int SDL_JoystickNumHats(SDL_Joystick * joystick) { - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } return (joystick->nhats); @@ -252,7 +256,7 @@ SDL_JoystickNumHats(SDL_Joystick * joystick) int SDL_JoystickNumBalls(SDL_Joystick * joystick) { - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } return (joystick->nballs); @@ -264,7 +268,7 @@ SDL_JoystickNumBalls(SDL_Joystick * joystick) int SDL_JoystickNumButtons(SDL_Joystick * joystick) { - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } return (joystick->nbuttons); @@ -278,7 +282,7 @@ SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis) { Sint16 state; - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (0); } if (axis < joystick->naxes) { @@ -298,7 +302,7 @@ SDL_JoystickGetHat(SDL_Joystick * joystick, int hat) { Uint8 state; - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (0); } if (hat < joystick->nhats) { @@ -318,7 +322,7 @@ SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy) { int retval; - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (-1); } @@ -347,7 +351,7 @@ SDL_JoystickGetButton(SDL_Joystick * joystick, int button) { Uint8 state; - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return (0); } if (button < joystick->nbuttons) { @@ -367,7 +371,7 @@ SDL_JoystickClose(SDL_Joystick * joystick) { int i; - if (!ValidJoystick(&joystick)) { + if (!SDL_PrivateJoystickValid(&joystick)) { return; } diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h index d489ac234..0c9b31e6a 100644 --- a/src/joystick/SDL_joystick_c.h +++ b/src/joystick/SDL_joystick_c.h @@ -36,4 +36,8 @@ extern int SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value); extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state); + +/* Internal sanity checking functions */ +extern int SDL_PrivateJoystickValid(SDL_Joystick ** joystick); + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index faec85383..8c6637cd7 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -31,13 +31,11 @@ #include #include /* For the definition of PATH_MAX */ #include -#if SDL_INPUT_LINUXEV -#include -#endif #include "SDL_joystick.h" #include "../SDL_sysjoystick.h" #include "../SDL_joystick_c.h" +#include "SDL_sysjoystick_c.h" /* Special joystick configurations */ static struct @@ -278,35 +276,6 @@ static struct } 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 static int diff --git a/src/joystick/linux/SDL_sysjoystick_c.h b/src/joystick/linux/SDL_sysjoystick_c.h new file mode 100644 index 000000000..b319c8ba5 --- /dev/null +++ b/src/joystick/linux/SDL_sysjoystick_c.h @@ -0,0 +1,53 @@ +/* + 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 +#endif + +/* 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 +};