Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
authorSam Lantinga <slouken@libsdl.org>
Tue, 28 May 2002 20:01:29 +0000
changeset 381bc1401311390
parent 380 bce7171e7a85
child 382 74bdcae44bf5
Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
configure.in
src/joystick/bsd/SDL_sysjoystick.c
     1.1 --- a/configure.in	Tue May 28 19:31:32 2002 +0000
     1.2 +++ b/configure.in	Tue May 28 20:01:29 2002 +0000
     1.3 @@ -1445,6 +1445,86 @@
     1.4      fi
     1.5  }
     1.6  
     1.7 +dnl Check for the usbhid(3) library on *BSD
     1.8 +CheckUSBHID()
     1.9 +{
    1.10 +    if test x$enable_joystick = xyes; then
    1.11 +        have_libusbhid=no
    1.12 +        have_libusb=no
    1.13 +        AC_CHECK_LIB(usbhid, hid_init, have_libusbhid=yes)
    1.14 +        AC_CHECK_LIB(usb, hid_init, have_libusb=yes)
    1.15 +        if test x$have_libusbhid = xyes; then
    1.16 +            SYSTEM_LIBS="$SYSTEM_LIBS -lusbhid"
    1.17 +        fi
    1.18 +        if test x$have_libusb = xyes; then
    1.19 +            SYSTEM_LIBS="$SYSTEM_LIBS -lusb"
    1.20 +        fi
    1.21 +
    1.22 +        AC_CHECK_HEADER(usbhid.h, have_usbhid_h=yes)
    1.23 +        AC_CHECK_HEADER(libusb.h, have_libusb_h=yes)
    1.24 +        AC_CHECK_HEADER(libusbhid.h, have_libusbhid_h=yes)
    1.25 +        if test x$have_usbhid_h = xyes; then
    1.26 +            CFLAGS="$CFLAGS -DHAVE_USBHID_H"
    1.27 +        fi
    1.28 +        if test x$have_libusb_h = xyes; then
    1.29 +            CFLAGS="$CFLAGS -DHAVE_LIBUSB_H"
    1.30 +        fi
    1.31 +        if test x$have_libusbhid_h = xyes; then
    1.32 +            CFLAGS="$CFLAGS -DHAVE_LIBUSBHID_H"
    1.33 +        fi
    1.34 +
    1.35 +        AC_MSG_CHECKING(for usbhid)
    1.36 +        have_usbhid=no
    1.37 +        AC_TRY_COMPILE([
    1.38 +          #include <dev/usb/usb.h>
    1.39 +          #include <dev/usb/usbhid.h>
    1.40 +          #if defined(HAVE_USBHID_H)
    1.41 +          #include <usbhid.h>
    1.42 +          #elif defined(HAVE_LIBUSB_H)
    1.43 +          #include <libusb.h>
    1.44 +          #elif defined(HAVE_LIBUSBHID_H)
    1.45 +          #include <libusbhid.h>
    1.46 +          #endif
    1.47 +        ],[
    1.48 +          struct report_desc *repdesc;
    1.49 +          struct usb_ctl_report *repbuf;
    1.50 +          enum hid_kind hidkind;
    1.51 +        ],[
    1.52 +        have_usbhid=yes
    1.53 +        ])
    1.54 +        AC_MSG_RESULT($have_usbhid)
    1.55 +
    1.56 +        if test x$have_usbhid = xyes; then
    1.57 +            AC_MSG_CHECKING(for ucr_data member of usb_ctl_report)
    1.58 +            have_usbhid_ucr_data=no
    1.59 +            AC_TRY_COMPILE([
    1.60 +              #include <dev/usb/usb.h>
    1.61 +              #include <dev/usb/usbhid.h>
    1.62 +              #include <stdio.h>
    1.63 +              #if defined(HAVE_USBHID_H)
    1.64 +              #include <usbhid.h>
    1.65 +              #elif defined(HAVE_LIBUSB_H)
    1.66 +              #include <libusb.h>
    1.67 +              #elif defined(HAVE_LIBUSBHID_H)
    1.68 +              #include <libusbhid.h>
    1.69 +              #endif
    1.70 +            ],[
    1.71 +              struct usb_ctl_report buf;
    1.72 +              if (buf.ucr_data) { }
    1.73 +            ],[
    1.74 +            have_usbhid_ucr_data=yes
    1.75 +            ])
    1.76 +            if test x$have_usbhid_ucr_data = xyes; then
    1.77 +                CFLAGS="$CFLAGS -DUSBHID_UCR_DATA"
    1.78 +            fi
    1.79 +            AC_MSG_RESULT($have_usbhid_ucr_data)
    1.80 +
    1.81 +            JOYSTICK_SUBDIRS="$JOYSTICK_SUBDIRS bsd"
    1.82 +            JOYSTICK_DRIVERS="$JOYSTICK_DRIVERS bsd/libjoystick_bsd.la"
    1.83 +        fi
    1.84 +    fi
    1.85 +}
    1.86 +
    1.87  case "$target" in
    1.88      *-*-linux*)
    1.89          ARCH=linux
    1.90 @@ -1598,6 +1678,7 @@
    1.91          CheckAAlib
    1.92          CheckOpenGL
    1.93          CheckPTHREAD
    1.94 +        CheckUSBHID
    1.95          # Set up files for the main() stub
    1.96          COPY_ARCH_SRC(src/main, linux, SDL_main.c)
    1.97          # Set up files for the audio library
    1.98 @@ -1607,12 +1688,6 @@
    1.99          #    AUDIO_SUBDIRS="$AUDIO_SUBDIRS sun"
   1.100          #    AUDIO_DRIVERS="$AUDIO_DRIVERS sun/libaudio_sun.la"
   1.101          #fi
   1.102 -        # Set up files for the joystick library
   1.103 -        if test x$enable_joystick = xyes; then
   1.104 -            JOYSTICK_SUBDIRS="$JOYSTICK_SUBDIRS bsd"
   1.105 -            JOYSTICK_DRIVERS="$JOYSTICK_DRIVERS bsd/libjoystick_bsd.la"
   1.106 -            SYSTEM_LIBS="$SYSTEM_LIBS -lusb"
   1.107 -        fi
   1.108          # Set up files for the cdrom library
   1.109          if test x$enable_cdrom = xyes; then
   1.110              CDROM_SUBDIRS="$CDROM_SUBDIRS freebsd"
   1.111 @@ -1652,6 +1727,7 @@
   1.112          CheckAAlib
   1.113          CheckOpenGL
   1.114          CheckPTHREAD
   1.115 +        CheckUSBHID
   1.116          # Set up files for the main() stub
   1.117          COPY_ARCH_SRC(src/main, linux, SDL_main.c)
   1.118          # Set up files for the audio library
   1.119 @@ -1660,12 +1736,6 @@
   1.120              AUDIO_SUBDIRS="$AUDIO_SUBDIRS sun"
   1.121              AUDIO_DRIVERS="$AUDIO_DRIVERS sun/libaudio_sun.la"
   1.122          fi
   1.123 -        # Set up files for the joystick library
   1.124 -        if test x$enable_joystick = xyes; then
   1.125 -            JOYSTICK_SUBDIRS="$JOYSTICK_SUBDIRS bsd"
   1.126 -            JOYSTICK_DRIVERS="$JOYSTICK_DRIVERS bsd/libjoystick_bsd.la"
   1.127 -            SYSTEM_LIBS="$SYSTEM_LIBS -lusb"
   1.128 -        fi
   1.129          # Set up files for the cdrom library
   1.130          if test x$enable_cdrom = xyes; then
   1.131              CDROM_SUBDIRS="$CDROM_SUBDIRS openbsd"
   1.132 @@ -1707,6 +1777,7 @@
   1.133          CheckAAlib
   1.134          CheckOpenGL
   1.135          CheckPTHREAD
   1.136 +        CheckUSBHID
   1.137          # Set up files for the main() stub
   1.138          COPY_ARCH_SRC(src/main, linux, SDL_main.c)
   1.139          # Set up files for the audio library
   1.140 @@ -1719,12 +1790,6 @@
   1.141          if test x$have_oss = xyes; then
   1.142              SYSTEM_LIBS="$SYSTEM_LIBS -lossaudio"
   1.143          fi
   1.144 -        # Set up files for the joystick library
   1.145 -        if test x$enable_joystick = xyes; then
   1.146 -            JOYSTICK_SUBDIRS="$JOYSTICK_SUBDIRS bsd"
   1.147 -            JOYSTICK_DRIVERS="$JOYSTICK_DRIVERS bsd/libjoystick_bsd.la"
   1.148 -            SYSTEM_LIBS="$SYSTEM_LIBS -lusbhid"
   1.149 -        fi
   1.150          # Set up files for the cdrom library
   1.151          if test x$enable_cdrom = xyes; then
   1.152              CDROM_SUBDIRS="$CDROM_SUBDIRS openbsd"
     2.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Tue May 28 19:31:32 2002 +0000
     2.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Tue May 28 20:01:29 2002 +0000
     2.3 @@ -42,10 +42,12 @@
     2.4  #include <dev/usb/usb.h>
     2.5  #include <dev/usb/usbhid.h>
     2.6  
     2.7 -#if defined(__FreeBSD__)
     2.8 -# include <libusb.h>
     2.9 -#else
    2.10 -# include <usbhid.h>
    2.11 +#if defined(HAVE_USBHID_H)
    2.12 +#include <usbhid.h>
    2.13 +#elif defined(HAVE_LIBUSB_H)
    2.14 +#include <libusb.h>
    2.15 +#elif defined(HAVE_LIBUSBHID_H)
    2.16 +#include <libusbhid.h>
    2.17  #endif
    2.18  
    2.19  #include "SDL_error.h"
    2.20 @@ -113,6 +115,12 @@
    2.21  static int	report_alloc(struct report *, struct report_desc *, int);
    2.22  static void	report_free(struct report *);
    2.23  
    2.24 +#ifdef USBHID_UCR_DATA
    2.25 +#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
    2.26 +#else
    2.27 +#define REP_BUF_DATA(rep) ((rep)->buf->data)
    2.28 +#endif
    2.29 +
    2.30  int
    2.31  SDL_SYS_JoystickInit(void)
    2.32  {
    2.33 @@ -274,14 +282,15 @@
    2.34  void
    2.35  SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
    2.36  {
    2.37 -	static struct hid_item hitem;
    2.38 -	static struct hid_data *hdata;
    2.39 -	static struct report *rep;
    2.40 +	struct hid_item hitem;
    2.41 +	struct hid_data *hdata;
    2.42 +	struct report *rep;
    2.43  	int nbutton, naxe = -1;
    2.44  	Sint32 v;
    2.45  	
    2.46  	rep = &joy->hwdata->inreport;
    2.47 -	if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) {
    2.48 +
    2.49 +	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
    2.50  		return;
    2.51  	}
    2.52  	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
    2.53 @@ -316,7 +325,8 @@
    2.54  					goto scaleaxe;
    2.55  				}
    2.56  scaleaxe:
    2.57 -				v = (Sint32)hid_get_data(rep->buf->data, &hitem);
    2.58 +				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
    2.59 +				    &hitem);
    2.60  				if (v != 127) {
    2.61  					if (v < 127) {
    2.62  						v = -(256 - v);
    2.63 @@ -335,7 +345,7 @@
    2.64  				}
    2.65  				break;
    2.66  			case HUP_BUTTON:
    2.67 -				v = (Sint32)hid_get_data(rep->buf->data,
    2.68 +				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
    2.69  				    &hitem);
    2.70  				if (joy->buttons[nbutton] != v) {
    2.71  					SDL_PrivateJoystickButton(joy,
    2.72 @@ -395,7 +405,7 @@
    2.73  	r->size = len;
    2.74  
    2.75  	if (r->size > 0) {
    2.76 -		r->buf = malloc(sizeof(*r->buf) - sizeof(r->buf->data) +
    2.77 +		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
    2.78  		    r->size);
    2.79  		if (r->buf == NULL) {
    2.80  			SDL_OutOfMemory();