Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Exposed some of the joystick stuff to the haptic subsystem.
Added SDL_JoystickIsHaptic().
  • Loading branch information
bobbens committed Jul 2, 2008
1 parent b50f26e commit 4a90a37
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 45 deletions.
17 changes: 17 additions & 0 deletions include/SDL_haptic.h
Expand Up @@ -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++ */
Expand Down Expand Up @@ -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.
*/
Expand Down
34 changes: 34 additions & 0 deletions src/haptic/SDL_haptic.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
25 changes: 24 additions & 1 deletion src/haptic/linux/SDL_syshaptic.c
Expand Up @@ -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 <unistd.h> /* close */
#include <linux/input.h>
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -604,5 +628,4 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
}



#endif /* SDL_HAPTIC_LINUX */
28 changes: 16 additions & 12 deletions src/joystick/SDL_joystick.c
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -367,7 +371,7 @@ SDL_JoystickClose(SDL_Joystick * joystick)
{
int i;

if (!ValidJoystick(&joystick)) {
if (!SDL_PrivateJoystickValid(&joystick)) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/joystick/SDL_joystick_c.h
Expand Up @@ -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: */
33 changes: 1 addition & 32 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -31,13 +31,11 @@
#include <sys/ioctl.h>
#include <limits.h> /* For the definition of PATH_MAX */
#include <linux/joystick.h>
#if SDL_INPUT_LINUXEV
#include <linux/input.h>
#endif

#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "SDL_sysjoystick_c.h"

/* Special joystick configurations */
static struct
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions 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 <linux/input.h>
#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
};

0 comments on commit 4a90a37

Please sign in to comment.