Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
authorSam Lantinga
Tue, 06 Jan 2004 04:07:12 +0000
changeset 77618922ae3ee07
parent 775 08b7fc2b5225
child 777 6f4fe7f2063b
Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
src/joystick/bsd/SDL_sysjoystick.c
     1.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Mon Jan 05 12:08:38 2004 +0000
     1.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Tue Jan 06 04:07:12 2004 +0000
     1.3 @@ -57,6 +57,11 @@
     1.4  
     1.5  #ifdef __FreeBSD__
     1.6  #include <osreldate.h>
     1.7 +#include <sys/joystick.h>
     1.8 +#endif
     1.9 +
    1.10 +#if defined(__NetBSD__) || defined(__OpenBSD__)
    1.11 +#include <machine/joystick.h>
    1.12  #endif
    1.13  
    1.14  #include "SDL_error.h"
    1.15 @@ -139,8 +144,8 @@
    1.16  
    1.17  	SDL_numjoysticks = 0;
    1.18  
    1.19 -	memset(joynames, NULL, sizeof(joynames));
    1.20 -	memset(joydevnames, NULL, sizeof(joydevnames));
    1.21 +	memset(joynames, 0, sizeof(joynames));
    1.22 +	memset(joydevnames, 0, sizeof(joydevnames));
    1.23  
    1.24  	for (i = 0; i < MAX_UHID_JOYS; i++) {
    1.25  		SDL_Joystick nj;
    1.26 @@ -249,7 +254,18 @@
    1.27  	joy->hwdata = hw;
    1.28  	hw->fd = fd;
    1.29  	hw->path = strdup(path);
    1.30 -	hw->type = BSDJOY_UHID;
    1.31 +	if (! strncmp(path, "/dev/joy", 8)) {
    1.32 +		hw->type = BSDJOY_JOY;
    1.33 +		joy->naxes = 2;
    1.34 +		joy->nbuttons = 2;
    1.35 +		joy->nhats = 0;
    1.36 +		joy->nballs = 0;
    1.37 +		joydevnames[joy->index] = strdup("Gameport joystick");
    1.38 +		goto usbend;
    1.39 +	} else {
    1.40 +		hw->type = BSDJOY_UHID;
    1.41 +	}
    1.42 +
    1.43  	{
    1.44  	    int ax;
    1.45  	    for (ax = 0; ax < JOYAXE_count; ax++)
    1.46 @@ -332,6 +348,7 @@
    1.47  	}
    1.48  	hid_end_parse(hdata);
    1.49  
    1.50 +usbend:
    1.51  	/* The poll blocks the event thread. */
    1.52  	fcntl(fd, F_SETFL, O_NONBLOCK);
    1.53  
    1.54 @@ -351,6 +368,57 @@
    1.55  	struct report *rep;
    1.56  	int nbutton, naxe = -1;
    1.57  	Sint32 v;
    1.58 +
    1.59 +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
    1.60 +	struct joystick gameport;
    1.61 +	static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
    1.62 + 
    1.63 +	if (joy->hwdata->type == BSDJOY_JOY) {
    1.64 +		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
    1.65 +			return;
    1.66 +		if (abs(x - gameport.x) > 8) {
    1.67 +			x = gameport.x;
    1.68 +			if (x < xmin) {
    1.69 +				xmin = x;
    1.70 +			}
    1.71 +			if (x > xmax) {
    1.72 +				xmax = x;
    1.73 +			}
    1.74 +			if (xmin == xmax) {
    1.75 +				xmin--;
    1.76 +				xmax++;
    1.77 +			}
    1.78 +			v = (Sint32)x;
    1.79 +			v -= (xmax + xmin + 1)/2;
    1.80 +			v *= 32768/((xmax - xmin + 1)/2);
    1.81 +			SDL_PrivateJoystickAxis(joy, 0, v);
    1.82 +		}
    1.83 +		if (abs(y - gameport.y) > 8) {
    1.84 +			y = gameport.y;
    1.85 +			if (y < ymin) {
    1.86 +				ymin = y;
    1.87 +			}
    1.88 +			if (y > ymax) {
    1.89 +				ymax = y;
    1.90 +			}
    1.91 +			if (ymin == ymax) {
    1.92 +				ymin--;
    1.93 +				ymax++;
    1.94 +			}
    1.95 +			v = (Sint32)y;
    1.96 +			v -= (ymax + ymin + 1)/2;
    1.97 +			v *= 32768/((ymax - ymin + 1)/2);
    1.98 +			SDL_PrivateJoystickAxis(joy, 1, v);
    1.99 +		}
   1.100 +		if (gameport.b1 != joy->buttons[0]) {
   1.101 +			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
   1.102 +		}
   1.103 +		if (gameport.b2 != joy->buttons[1]) {
   1.104 +			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
   1.105 +		}
   1.106 +		return;
   1.107 +	}
   1.108 +#endif /* defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) */
   1.109  	
   1.110  	rep = &joy->hwdata->inreport;
   1.111  
   1.112 @@ -418,8 +486,10 @@
   1.113  void
   1.114  SDL_SYS_JoystickClose(SDL_Joystick *joy)
   1.115  {
   1.116 -	report_free(&joy->hwdata->inreport);
   1.117 -	hid_dispose_report_desc(joy->hwdata->repdesc);
   1.118 +	if (strncmp(joy->hwdata->path, "/dev/joy", 8))	{
   1.119 +		report_free(&joy->hwdata->inreport);
   1.120 +		hid_dispose_report_desc(joy->hwdata->repdesc);
   1.121 +	}
   1.122  	close(joy->hwdata->fd);
   1.123  	free(joy->hwdata->path);
   1.124  	free(joy->hwdata);