src/joystick/bsd/SDL_sysjoystick.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1635 92947e3a18db
child 1668 4da1ee79c9af
     1.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -74,62 +74,69 @@
     1.4  #define MAX_JOY_JOYS	2
     1.5  #define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
     1.6  
     1.7 -struct report {
     1.8 -	struct	usb_ctl_report *buf;	/* Buffer */
     1.9 -	size_t	size;			/* Buffer size */
    1.10 -	int	rid;			/* Report ID */
    1.11 -	enum {
    1.12 -		SREPORT_UNINIT,
    1.13 -		SREPORT_CLEAN,
    1.14 -		SREPORT_DIRTY
    1.15 -	} status;
    1.16 +struct report
    1.17 +{
    1.18 +    struct usb_ctl_report *buf; /* Buffer */
    1.19 +    size_t size;                /* Buffer size */
    1.20 +    int rid;                    /* Report ID */
    1.21 +    enum
    1.22 +    {
    1.23 +        SREPORT_UNINIT,
    1.24 +        SREPORT_CLEAN,
    1.25 +        SREPORT_DIRTY
    1.26 +    } status;
    1.27  };
    1.28  
    1.29 -static struct {
    1.30 -	int	uhid_report;
    1.31 -	hid_kind_t kind;
    1.32 -	const	char *name;
    1.33 +static struct
    1.34 +{
    1.35 +    int uhid_report;
    1.36 +    hid_kind_t kind;
    1.37 +    const char *name;
    1.38  } const repinfo[] = {
    1.39 -	{ UHID_INPUT_REPORT,	hid_input,	"input" },
    1.40 -	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
    1.41 -	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
    1.42 +    {UHID_INPUT_REPORT, hid_input, "input"},
    1.43 +    {UHID_OUTPUT_REPORT, hid_output, "output"},
    1.44 +    {UHID_FEATURE_REPORT, hid_feature, "feature"}
    1.45  };
    1.46  
    1.47 -enum {
    1.48 -	REPORT_INPUT = 0,
    1.49 -	REPORT_OUTPUT = 1,
    1.50 -	REPORT_FEATURE = 2
    1.51 +enum
    1.52 +{
    1.53 +    REPORT_INPUT = 0,
    1.54 +    REPORT_OUTPUT = 1,
    1.55 +    REPORT_FEATURE = 2
    1.56  };
    1.57  
    1.58 -enum {
    1.59 -	JOYAXE_X,
    1.60 -	JOYAXE_Y,
    1.61 -	JOYAXE_Z,
    1.62 -	JOYAXE_SLIDER,
    1.63 -	JOYAXE_WHEEL,
    1.64 -	JOYAXE_RX,
    1.65 -	JOYAXE_RY,
    1.66 -	JOYAXE_RZ,
    1.67 -	JOYAXE_count
    1.68 +enum
    1.69 +{
    1.70 +    JOYAXE_X,
    1.71 +    JOYAXE_Y,
    1.72 +    JOYAXE_Z,
    1.73 +    JOYAXE_SLIDER,
    1.74 +    JOYAXE_WHEEL,
    1.75 +    JOYAXE_RX,
    1.76 +    JOYAXE_RY,
    1.77 +    JOYAXE_RZ,
    1.78 +    JOYAXE_count
    1.79  };
    1.80  
    1.81 -struct joystick_hwdata {
    1.82 -	int	fd;
    1.83 -	char	*path;
    1.84 -	enum {
    1.85 -		BSDJOY_UHID,	/* uhid(4) */
    1.86 -		BSDJOY_JOY	/* joy(4) */
    1.87 -	} type;
    1.88 -	struct	report_desc *repdesc;
    1.89 -	struct	report inreport;
    1.90 -	int	axis_map[JOYAXE_count];	/* map present JOYAXE_* to 0,1,..*/
    1.91 +struct joystick_hwdata
    1.92 +{
    1.93 +    int fd;
    1.94 +    char *path;
    1.95 +    enum
    1.96 +    {
    1.97 +        BSDJOY_UHID,            /* uhid(4) */
    1.98 +        BSDJOY_JOY              /* joy(4) */
    1.99 +    } type;
   1.100 +    struct report_desc *repdesc;
   1.101 +    struct report inreport;
   1.102 +    int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,.. */
   1.103  };
   1.104  
   1.105  static char *joynames[MAX_JOYS];
   1.106  static char *joydevnames[MAX_JOYS];
   1.107  
   1.108 -static int	report_alloc(struct report *, struct report_desc *, int);
   1.109 -static void	report_free(struct report *);
   1.110 +static int report_alloc (struct report *, struct report_desc *, int);
   1.111 +static void report_free (struct report *);
   1.112  
   1.113  #ifdef USBHID_UCR_DATA
   1.114  #define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
   1.115 @@ -138,443 +145,456 @@
   1.116  #endif
   1.117  
   1.118  int
   1.119 -SDL_SYS_JoystickInit(void)
   1.120 +SDL_SYS_JoystickInit (void)
   1.121  {
   1.122 -	char s[16];
   1.123 -	int i, fd;
   1.124 +    char s[16];
   1.125 +    int i, fd;
   1.126  
   1.127 -	SDL_numjoysticks = 0;
   1.128 +    SDL_numjoysticks = 0;
   1.129  
   1.130 -	SDL_memset(joynames, 0, sizeof(joynames));
   1.131 -	SDL_memset(joydevnames, 0, sizeof(joydevnames));
   1.132 +    SDL_memset (joynames, 0, sizeof (joynames));
   1.133 +    SDL_memset (joydevnames, 0, sizeof (joydevnames));
   1.134  
   1.135 -	for (i = 0; i < MAX_UHID_JOYS; i++) {
   1.136 -		SDL_Joystick nj;
   1.137 +    for (i = 0; i < MAX_UHID_JOYS; i++) {
   1.138 +        SDL_Joystick nj;
   1.139  
   1.140 -		SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
   1.141 +        SDL_snprintf (s, SDL_arraysize (s), "/dev/uhid%d", i);
   1.142  
   1.143 -		nj.index = SDL_numjoysticks;
   1.144 -		joynames[nj.index] = strdup(s);
   1.145 +        nj.index = SDL_numjoysticks;
   1.146 +        joynames[nj.index] = strdup (s);
   1.147  
   1.148 -		if (SDL_SYS_JoystickOpen(&nj) == 0) {
   1.149 -			SDL_SYS_JoystickClose(&nj);
   1.150 -			SDL_numjoysticks++;
   1.151 -		} else {
   1.152 -			SDL_free(joynames[nj.index]);
   1.153 -			joynames[nj.index] = NULL;
   1.154 -		}
   1.155 -	}
   1.156 -	for (i = 0; i < MAX_JOY_JOYS; i++) {
   1.157 -		SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
   1.158 -		fd = open(s, O_RDONLY);
   1.159 -		if (fd != -1) {
   1.160 -			joynames[SDL_numjoysticks++] = strdup(s);
   1.161 -			close(fd);
   1.162 -		}
   1.163 -	}
   1.164 +        if (SDL_SYS_JoystickOpen (&nj) == 0) {
   1.165 +            SDL_SYS_JoystickClose (&nj);
   1.166 +            SDL_numjoysticks++;
   1.167 +        } else {
   1.168 +            SDL_free (joynames[nj.index]);
   1.169 +            joynames[nj.index] = NULL;
   1.170 +        }
   1.171 +    }
   1.172 +    for (i = 0; i < MAX_JOY_JOYS; i++) {
   1.173 +        SDL_snprintf (s, SDL_arraysize (s), "/dev/joy%d", i);
   1.174 +        fd = open (s, O_RDONLY);
   1.175 +        if (fd != -1) {
   1.176 +            joynames[SDL_numjoysticks++] = strdup (s);
   1.177 +            close (fd);
   1.178 +        }
   1.179 +    }
   1.180  
   1.181 -	/* Read the default USB HID usage table. */
   1.182 -	hid_init(NULL);
   1.183 +    /* Read the default USB HID usage table. */
   1.184 +    hid_init (NULL);
   1.185  
   1.186 -	return (SDL_numjoysticks);
   1.187 +    return (SDL_numjoysticks);
   1.188  }
   1.189  
   1.190  const char *
   1.191 -SDL_SYS_JoystickName(int index)
   1.192 +SDL_SYS_JoystickName (int index)
   1.193  {
   1.194 -	if (joydevnames[index] != NULL) {
   1.195 -		return (joydevnames[index]);
   1.196 -	}
   1.197 -	return (joynames[index]);
   1.198 +    if (joydevnames[index] != NULL) {
   1.199 +        return (joydevnames[index]);
   1.200 +    }
   1.201 +    return (joynames[index]);
   1.202  }
   1.203  
   1.204  static int
   1.205 -usage_to_joyaxe(unsigned usage)
   1.206 +usage_to_joyaxe (unsigned usage)
   1.207  {
   1.208      int joyaxe;
   1.209      switch (usage) {
   1.210      case HUG_X:
   1.211 -	joyaxe = JOYAXE_X; break;
   1.212 +        joyaxe = JOYAXE_X;
   1.213 +        break;
   1.214      case HUG_Y:
   1.215 -	joyaxe = JOYAXE_Y; break;
   1.216 +        joyaxe = JOYAXE_Y;
   1.217 +        break;
   1.218      case HUG_Z:
   1.219 -	joyaxe = JOYAXE_Z; break;
   1.220 +        joyaxe = JOYAXE_Z;
   1.221 +        break;
   1.222      case HUG_SLIDER:
   1.223 -	joyaxe = JOYAXE_SLIDER; break;
   1.224 +        joyaxe = JOYAXE_SLIDER;
   1.225 +        break;
   1.226      case HUG_WHEEL:
   1.227 -	joyaxe = JOYAXE_WHEEL; break;
   1.228 +        joyaxe = JOYAXE_WHEEL;
   1.229 +        break;
   1.230      case HUG_RX:
   1.231 -	joyaxe = JOYAXE_RX; break;
   1.232 +        joyaxe = JOYAXE_RX;
   1.233 +        break;
   1.234      case HUG_RY:
   1.235 -	joyaxe = JOYAXE_RY; break;
   1.236 +        joyaxe = JOYAXE_RY;
   1.237 +        break;
   1.238      case HUG_RZ:
   1.239 -	joyaxe = JOYAXE_RZ; break;
   1.240 +        joyaxe = JOYAXE_RZ;
   1.241 +        break;
   1.242      default:
   1.243 -	joyaxe = -1;
   1.244 +        joyaxe = -1;
   1.245      }
   1.246 -    return joyaxe;    
   1.247 +    return joyaxe;
   1.248  }
   1.249  
   1.250  static unsigned
   1.251 -hatval_to_sdl(Sint32 hatval)
   1.252 +hatval_to_sdl (Sint32 hatval)
   1.253  {
   1.254      static const unsigned hat_dir_map[8] = {
   1.255 -	SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 
   1.256 -	SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
   1.257 +        SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
   1.258 +        SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
   1.259      };
   1.260      unsigned result;
   1.261 -    if ((hatval & 7) == hatval) 
   1.262 -	result = hat_dir_map[hatval];
   1.263 -    else 
   1.264 -	result = SDL_HAT_CENTERED;
   1.265 +    if ((hatval & 7) == hatval)
   1.266 +        result = hat_dir_map[hatval];
   1.267 +    else
   1.268 +        result = SDL_HAT_CENTERED;
   1.269      return result;
   1.270  }
   1.271  
   1.272  
   1.273  int
   1.274 -SDL_SYS_JoystickOpen(SDL_Joystick *joy)
   1.275 +SDL_SYS_JoystickOpen (SDL_Joystick * joy)
   1.276  {
   1.277 -	char *path = joynames[joy->index];
   1.278 -	struct joystick_hwdata *hw;
   1.279 -	struct hid_item hitem;
   1.280 -	struct hid_data *hdata;
   1.281 -	struct report *rep;
   1.282 -	int fd;
   1.283 -	int i;
   1.284 +    char *path = joynames[joy->index];
   1.285 +    struct joystick_hwdata *hw;
   1.286 +    struct hid_item hitem;
   1.287 +    struct hid_data *hdata;
   1.288 +    struct report *rep;
   1.289 +    int fd;
   1.290 +    int i;
   1.291  
   1.292 -	fd = open(path, O_RDONLY);
   1.293 -	if (fd == -1) {
   1.294 -		SDL_SetError("%s: %s", path, strerror(errno));
   1.295 -		return (-1);
   1.296 -	}
   1.297 +    fd = open (path, O_RDONLY);
   1.298 +    if (fd == -1) {
   1.299 +        SDL_SetError ("%s: %s", path, strerror (errno));
   1.300 +        return (-1);
   1.301 +    }
   1.302  
   1.303 -	hw = (struct joystick_hwdata *)SDL_malloc(sizeof(struct joystick_hwdata));
   1.304 -	if (hw == NULL) {
   1.305 -		SDL_OutOfMemory();
   1.306 -		close(fd);
   1.307 -		return (-1);
   1.308 -	}
   1.309 -	joy->hwdata = hw;
   1.310 -	hw->fd = fd;
   1.311 -	hw->path = strdup(path);
   1.312 -	if (! SDL_strncmp(path, "/dev/joy", 8)) {
   1.313 -		hw->type = BSDJOY_JOY;
   1.314 -		joy->naxes = 2;
   1.315 -		joy->nbuttons = 2;
   1.316 -		joy->nhats = 0;
   1.317 -		joy->nballs = 0;
   1.318 -		joydevnames[joy->index] = strdup("Gameport joystick");
   1.319 -		goto usbend;
   1.320 -	} else {
   1.321 -		hw->type = BSDJOY_UHID;
   1.322 -	}
   1.323 +    hw = (struct joystick_hwdata *)
   1.324 +        SDL_malloc (sizeof (struct joystick_hwdata));
   1.325 +    if (hw == NULL) {
   1.326 +        SDL_OutOfMemory ();
   1.327 +        close (fd);
   1.328 +        return (-1);
   1.329 +    }
   1.330 +    joy->hwdata = hw;
   1.331 +    hw->fd = fd;
   1.332 +    hw->path = strdup (path);
   1.333 +    if (!SDL_strncmp (path, "/dev/joy", 8)) {
   1.334 +        hw->type = BSDJOY_JOY;
   1.335 +        joy->naxes = 2;
   1.336 +        joy->nbuttons = 2;
   1.337 +        joy->nhats = 0;
   1.338 +        joy->nballs = 0;
   1.339 +        joydevnames[joy->index] = strdup ("Gameport joystick");
   1.340 +        goto usbend;
   1.341 +    } else {
   1.342 +        hw->type = BSDJOY_UHID;
   1.343 +    }
   1.344  
   1.345 -	{
   1.346 -	    int ax;
   1.347 -	    for (ax = 0; ax < JOYAXE_count; ax++)
   1.348 -		hw->axis_map[ax] = -1;
   1.349 -	}
   1.350 -	hw->repdesc = hid_get_report_desc(fd);
   1.351 -	if (hw->repdesc == NULL) {
   1.352 -		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
   1.353 -		    strerror(errno));
   1.354 -		goto usberr;
   1.355 -	}
   1.356 +    {
   1.357 +        int ax;
   1.358 +        for (ax = 0; ax < JOYAXE_count; ax++)
   1.359 +            hw->axis_map[ax] = -1;
   1.360 +    }
   1.361 +    hw->repdesc = hid_get_report_desc (fd);
   1.362 +    if (hw->repdesc == NULL) {
   1.363 +        SDL_SetError ("%s: USB_GET_REPORT_DESC: %s", hw->path,
   1.364 +                      strerror (errno));
   1.365 +        goto usberr;
   1.366 +    }
   1.367  
   1.368 -	rep = &hw->inreport;
   1.369 -	if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
   1.370 -		rep->rid = -1; /* XXX */
   1.371 -	}
   1.372 -	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   1.373 -		goto usberr;
   1.374 -	}
   1.375 -	if (rep->size <= 0) {
   1.376 -		SDL_SetError("%s: Input report descriptor has invalid length",
   1.377 -		    hw->path);
   1.378 -		goto usberr;
   1.379 -	}
   1.380 +    rep = &hw->inreport;
   1.381 +    if (ioctl (fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
   1.382 +        rep->rid = -1;          /* XXX */
   1.383 +    }
   1.384 +    if (report_alloc (rep, hw->repdesc, REPORT_INPUT) < 0) {
   1.385 +        goto usberr;
   1.386 +    }
   1.387 +    if (rep->size <= 0) {
   1.388 +        SDL_SetError ("%s: Input report descriptor has invalid length",
   1.389 +                      hw->path);
   1.390 +        goto usberr;
   1.391 +    }
   1.392 +#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   1.393 +    hdata = hid_start_parse (hw->repdesc, 1 << hid_input, rep->rid);
   1.394 +#else
   1.395 +    hdata = hid_start_parse (hw->repdesc, 1 << hid_input);
   1.396 +#endif
   1.397 +    if (hdata == NULL) {
   1.398 +        SDL_SetError ("%s: Cannot start HID parser", hw->path);
   1.399 +        goto usberr;
   1.400 +    }
   1.401 +    joy->naxes = 0;
   1.402 +    joy->nbuttons = 0;
   1.403 +    joy->nhats = 0;
   1.404 +    joy->nballs = 0;
   1.405 +    for (i = 0; i < JOYAXE_count; i++)
   1.406 +        hw->axis_map[i] = -1;
   1.407  
   1.408 -#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   1.409 -	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
   1.410 -#else
   1.411 -	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
   1.412 -#endif
   1.413 -	if (hdata == NULL) {
   1.414 -		SDL_SetError("%s: Cannot start HID parser", hw->path);
   1.415 -		goto usberr;
   1.416 -	}
   1.417 -	joy->naxes = 0;
   1.418 -	joy->nbuttons = 0;
   1.419 -	joy->nhats = 0;
   1.420 -	joy->nballs = 0;
   1.421 -	for (i=0; i<JOYAXE_count; i++)
   1.422 -		hw->axis_map[i] = -1;
   1.423 +    while (hid_get_item (hdata, &hitem) > 0) {
   1.424 +        char *sp;
   1.425 +        const char *s;
   1.426  
   1.427 -	while (hid_get_item(hdata, &hitem) > 0) {
   1.428 -		char *sp;
   1.429 -		const char *s;
   1.430 +        switch (hitem.kind) {
   1.431 +        case hid_collection:
   1.432 +            switch (HID_PAGE (hitem.usage)) {
   1.433 +            case HUP_GENERIC_DESKTOP:
   1.434 +                switch (HID_USAGE (hitem.usage)) {
   1.435 +                case HUG_JOYSTICK:
   1.436 +                case HUG_GAME_PAD:
   1.437 +                    s = hid_usage_in_page (hitem.usage);
   1.438 +                    sp = SDL_malloc (SDL_strlen (s) + 5);
   1.439 +                    SDL_snprintf (sp, SDL_strlen (s) + 5, "%s (%d)",
   1.440 +                                  s, joy->index);
   1.441 +                    joydevnames[joy->index] = sp;
   1.442 +                }
   1.443 +            }
   1.444 +            break;
   1.445 +        case hid_input:
   1.446 +            switch (HID_PAGE (hitem.usage)) {
   1.447 +            case HUP_GENERIC_DESKTOP:
   1.448 +                {
   1.449 +                    unsigned usage = HID_USAGE (hitem.usage);
   1.450 +                    int joyaxe = usage_to_joyaxe (usage);
   1.451 +                    if (joyaxe >= 0) {
   1.452 +                        hw->axis_map[joyaxe] = 1;
   1.453 +                    } else if (usage == HUG_HAT_SWITCH) {
   1.454 +                        joy->nhats++;
   1.455 +                    }
   1.456 +                    break;
   1.457 +                }
   1.458 +            case HUP_BUTTON:
   1.459 +                joy->nbuttons++;
   1.460 +                break;
   1.461 +            default:
   1.462 +                break;
   1.463 +            }
   1.464 +            break;
   1.465 +        default:
   1.466 +            break;
   1.467 +        }
   1.468 +    }
   1.469 +    hid_end_parse (hdata);
   1.470 +    for (i = 0; i < JOYAXE_count; i++)
   1.471 +        if (hw->axis_map[i] > 0)
   1.472 +            hw->axis_map[i] = joy->naxes++;
   1.473  
   1.474 -		switch (hitem.kind) {
   1.475 -		case hid_collection:
   1.476 -			switch (HID_PAGE(hitem.usage)) {
   1.477 -			case HUP_GENERIC_DESKTOP:
   1.478 -				switch (HID_USAGE(hitem.usage)) {
   1.479 -				case HUG_JOYSTICK:
   1.480 -				case HUG_GAME_PAD:
   1.481 -					s = hid_usage_in_page(hitem.usage);
   1.482 -					sp = SDL_malloc(SDL_strlen(s) + 5);
   1.483 -					SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)", s,
   1.484 -					    joy->index);
   1.485 -					joydevnames[joy->index] = sp;
   1.486 -				}
   1.487 -			}
   1.488 -			break;
   1.489 -		case hid_input:
   1.490 -			switch (HID_PAGE(hitem.usage)) {
   1.491 -			case HUP_GENERIC_DESKTOP: {
   1.492 -			    unsigned usage = HID_USAGE(hitem.usage);
   1.493 -			    int joyaxe = usage_to_joyaxe(usage);
   1.494 -			    if (joyaxe >= 0) {
   1.495 -				hw->axis_map[joyaxe] = 1;
   1.496 -			    } else if (usage == HUG_HAT_SWITCH) {
   1.497 -				joy->nhats++;
   1.498 -			    }
   1.499 -			    break;
   1.500 -			}
   1.501 -			case HUP_BUTTON:
   1.502 -				joy->nbuttons++;
   1.503 -				break;
   1.504 -			default:
   1.505 -				break;
   1.506 -			}
   1.507 -			break;
   1.508 -		default:
   1.509 -			break;
   1.510 -		}
   1.511 -	}
   1.512 -	hid_end_parse(hdata);
   1.513 -	for (i=0; i<JOYAXE_count; i++)
   1.514 -		if (hw->axis_map[i] > 0)
   1.515 -			hw->axis_map[i] = joy->naxes++;
   1.516 +  usbend:
   1.517 +    /* The poll blocks the event thread. */
   1.518 +    fcntl (fd, F_SETFL, O_NONBLOCK);
   1.519  
   1.520 -usbend:
   1.521 -	/* The poll blocks the event thread. */
   1.522 -	fcntl(fd, F_SETFL, O_NONBLOCK);
   1.523 -
   1.524 -	return (0);
   1.525 -usberr:
   1.526 -	close(hw->fd);
   1.527 -	SDL_free(hw->path);
   1.528 -	SDL_free(hw);
   1.529 -	return (-1);
   1.530 +    return (0);
   1.531 +  usberr:
   1.532 +    close (hw->fd);
   1.533 +    SDL_free (hw->path);
   1.534 +    SDL_free (hw);
   1.535 +    return (-1);
   1.536  }
   1.537  
   1.538  void
   1.539 -SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
   1.540 +SDL_SYS_JoystickUpdate (SDL_Joystick * joy)
   1.541  {
   1.542 -	struct hid_item hitem;
   1.543 -	struct hid_data *hdata;
   1.544 -	struct report *rep;
   1.545 -	int nbutton, naxe = -1;
   1.546 -	Sint32 v;
   1.547 +    struct hid_item hitem;
   1.548 +    struct hid_data *hdata;
   1.549 +    struct report *rep;
   1.550 +    int nbutton, naxe = -1;
   1.551 +    Sint32 v;
   1.552  
   1.553  #if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
   1.554 -	struct joystick gameport;
   1.555 -	static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
   1.556 - 
   1.557 -	if (joy->hwdata->type == BSDJOY_JOY) {
   1.558 -		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
   1.559 -			return;
   1.560 -		if (abs(x - gameport.x) > 8) {
   1.561 -			x = gameport.x;
   1.562 -			if (x < xmin) {
   1.563 -				xmin = x;
   1.564 -			}
   1.565 -			if (x > xmax) {
   1.566 -				xmax = x;
   1.567 -			}
   1.568 -			if (xmin == xmax) {
   1.569 -				xmin--;
   1.570 -				xmax++;
   1.571 -			}
   1.572 -			v = (Sint32)x;
   1.573 -			v -= (xmax + xmin + 1)/2;
   1.574 -			v *= 32768/((xmax - xmin + 1)/2);
   1.575 -			SDL_PrivateJoystickAxis(joy, 0, v);
   1.576 -		}
   1.577 -		if (abs(y - gameport.y) > 8) {
   1.578 -			y = gameport.y;
   1.579 -			if (y < ymin) {
   1.580 -				ymin = y;
   1.581 -			}
   1.582 -			if (y > ymax) {
   1.583 -				ymax = y;
   1.584 -			}
   1.585 -			if (ymin == ymax) {
   1.586 -				ymin--;
   1.587 -				ymax++;
   1.588 -			}
   1.589 -			v = (Sint32)y;
   1.590 -			v -= (ymax + ymin + 1)/2;
   1.591 -			v *= 32768/((ymax - ymin + 1)/2);
   1.592 -			SDL_PrivateJoystickAxis(joy, 1, v);
   1.593 -		}
   1.594 -		if (gameport.b1 != joy->buttons[0]) {
   1.595 -			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
   1.596 -		}
   1.597 -		if (gameport.b2 != joy->buttons[1]) {
   1.598 -			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
   1.599 -		}
   1.600 -		return;
   1.601 -	}
   1.602 +    struct joystick gameport;
   1.603 +    static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
   1.604 +
   1.605 +    if (joy->hwdata->type == BSDJOY_JOY) {
   1.606 +        if (read (joy->hwdata->fd, &gameport, sizeof gameport) !=
   1.607 +            sizeof gameport)
   1.608 +            return;
   1.609 +        if (abs (x - gameport.x) > 8) {
   1.610 +            x = gameport.x;
   1.611 +            if (x < xmin) {
   1.612 +                xmin = x;
   1.613 +            }
   1.614 +            if (x > xmax) {
   1.615 +                xmax = x;
   1.616 +            }
   1.617 +            if (xmin == xmax) {
   1.618 +                xmin--;
   1.619 +                xmax++;
   1.620 +            }
   1.621 +            v = (Sint32) x;
   1.622 +            v -= (xmax + xmin + 1) / 2;
   1.623 +            v *= 32768 / ((xmax - xmin + 1) / 2);
   1.624 +            SDL_PrivateJoystickAxis (joy, 0, v);
   1.625 +        }
   1.626 +        if (abs (y - gameport.y) > 8) {
   1.627 +            y = gameport.y;
   1.628 +            if (y < ymin) {
   1.629 +                ymin = y;
   1.630 +            }
   1.631 +            if (y > ymax) {
   1.632 +                ymax = y;
   1.633 +            }
   1.634 +            if (ymin == ymax) {
   1.635 +                ymin--;
   1.636 +                ymax++;
   1.637 +            }
   1.638 +            v = (Sint32) y;
   1.639 +            v -= (ymax + ymin + 1) / 2;
   1.640 +            v *= 32768 / ((ymax - ymin + 1) / 2);
   1.641 +            SDL_PrivateJoystickAxis (joy, 1, v);
   1.642 +        }
   1.643 +        if (gameport.b1 != joy->buttons[0]) {
   1.644 +            SDL_PrivateJoystickButton (joy, 0, gameport.b1);
   1.645 +        }
   1.646 +        if (gameport.b2 != joy->buttons[1]) {
   1.647 +            SDL_PrivateJoystickButton (joy, 1, gameport.b2);
   1.648 +        }
   1.649 +        return;
   1.650 +    }
   1.651  #endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
   1.652 -	
   1.653 -	rep = &joy->hwdata->inreport;
   1.654  
   1.655 -	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
   1.656 -		return;
   1.657 -	}
   1.658 +    rep = &joy->hwdata->inreport;
   1.659 +
   1.660 +    if (read (joy->hwdata->fd, REP_BUF_DATA (rep), rep->size) != rep->size) {
   1.661 +        return;
   1.662 +    }
   1.663  #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   1.664 -	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   1.665 +    hdata = hid_start_parse (joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   1.666  #else
   1.667 -	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   1.668 +    hdata = hid_start_parse (joy->hwdata->repdesc, 1 << hid_input);
   1.669  #endif
   1.670 -	if (hdata == NULL) {
   1.671 -		fprintf(stderr, "%s: Cannot start HID parser\n",
   1.672 -		    joy->hwdata->path);
   1.673 -		return;
   1.674 -	}
   1.675 +    if (hdata == NULL) {
   1.676 +        fprintf (stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);
   1.677 +        return;
   1.678 +    }
   1.679  
   1.680 -	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   1.681 -		switch (hitem.kind) {
   1.682 -		case hid_input:
   1.683 -			switch (HID_PAGE(hitem.usage)) {
   1.684 -			case HUP_GENERIC_DESKTOP: {
   1.685 -			    unsigned usage = HID_USAGE(hitem.usage);
   1.686 -			    int joyaxe = usage_to_joyaxe(usage);
   1.687 -			    if (joyaxe >= 0) {
   1.688 -				naxe = joy->hwdata->axis_map[joyaxe];
   1.689 -				/* scaleaxe */
   1.690 -				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   1.691 -							 &hitem);
   1.692 -				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
   1.693 -				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
   1.694 -				if (v != joy->axes[naxe]) {
   1.695 -				    SDL_PrivateJoystickAxis(joy, naxe, v);
   1.696 -				}
   1.697 -			    } else if (usage == HUG_HAT_SWITCH) {
   1.698 -				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   1.699 -							 &hitem);
   1.700 -				SDL_PrivateJoystickHat(joy, 0,
   1.701 -					hatval_to_sdl(v)-hitem.logical_minimum);
   1.702 -			    }
   1.703 -			    break;
   1.704 -			}
   1.705 -			case HUP_BUTTON:
   1.706 -				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   1.707 -				    &hitem);
   1.708 -				if (joy->buttons[nbutton] != v) {
   1.709 -					SDL_PrivateJoystickButton(joy,
   1.710 -					    nbutton, v);
   1.711 -				}
   1.712 -				nbutton++;
   1.713 -				break;
   1.714 -			default:
   1.715 -				continue;
   1.716 -			}
   1.717 -			break;
   1.718 -		default:
   1.719 -			break;
   1.720 -		}
   1.721 -	}
   1.722 -	hid_end_parse(hdata);
   1.723 +    for (nbutton = 0; hid_get_item (hdata, &hitem) > 0;) {
   1.724 +        switch (hitem.kind) {
   1.725 +        case hid_input:
   1.726 +            switch (HID_PAGE (hitem.usage)) {
   1.727 +            case HUP_GENERIC_DESKTOP:
   1.728 +                {
   1.729 +                    unsigned usage = HID_USAGE (hitem.usage);
   1.730 +                    int joyaxe = usage_to_joyaxe (usage);
   1.731 +                    if (joyaxe >= 0) {
   1.732 +                        naxe = joy->hwdata->axis_map[joyaxe];
   1.733 +                        /* scaleaxe */
   1.734 +                        v = (Sint32) hid_get_data (REP_BUF_DATA (rep),
   1.735 +                                                   &hitem);
   1.736 +                        v -= (hitem.logical_maximum +
   1.737 +                              hitem.logical_minimum + 1) / 2;
   1.738 +                        v *= 32768 /
   1.739 +                            ((hitem.logical_maximum -
   1.740 +                              hitem.logical_minimum + 1) / 2);
   1.741 +                        if (v != joy->axes[naxe]) {
   1.742 +                            SDL_PrivateJoystickAxis (joy, naxe, v);
   1.743 +                        }
   1.744 +                    } else if (usage == HUG_HAT_SWITCH) {
   1.745 +                        v = (Sint32) hid_get_data (REP_BUF_DATA (rep),
   1.746 +                                                   &hitem);
   1.747 +                        SDL_PrivateJoystickHat (joy, 0,
   1.748 +                                                hatval_to_sdl (v) -
   1.749 +                                                hitem.logical_minimum);
   1.750 +                    }
   1.751 +                    break;
   1.752 +                }
   1.753 +            case HUP_BUTTON:
   1.754 +                v = (Sint32) hid_get_data (REP_BUF_DATA (rep), &hitem);
   1.755 +                if (joy->buttons[nbutton] != v) {
   1.756 +                    SDL_PrivateJoystickButton (joy, nbutton, v);
   1.757 +                }
   1.758 +                nbutton++;
   1.759 +                break;
   1.760 +            default:
   1.761 +                continue;
   1.762 +            }
   1.763 +            break;
   1.764 +        default:
   1.765 +            break;
   1.766 +        }
   1.767 +    }
   1.768 +    hid_end_parse (hdata);
   1.769  
   1.770 -	return;
   1.771 +    return;
   1.772  }
   1.773  
   1.774  /* Function to close a joystick after use */
   1.775  void
   1.776 -SDL_SYS_JoystickClose(SDL_Joystick *joy)
   1.777 +SDL_SYS_JoystickClose (SDL_Joystick * joy)
   1.778  {
   1.779 -	if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8))	{
   1.780 -		report_free(&joy->hwdata->inreport);
   1.781 -		hid_dispose_report_desc(joy->hwdata->repdesc);
   1.782 -	}
   1.783 -	close(joy->hwdata->fd);
   1.784 -	SDL_free(joy->hwdata->path);
   1.785 -	SDL_free(joy->hwdata);
   1.786 +    if (SDL_strncmp (joy->hwdata->path, "/dev/joy", 8)) {
   1.787 +        report_free (&joy->hwdata->inreport);
   1.788 +        hid_dispose_report_desc (joy->hwdata->repdesc);
   1.789 +    }
   1.790 +    close (joy->hwdata->fd);
   1.791 +    SDL_free (joy->hwdata->path);
   1.792 +    SDL_free (joy->hwdata);
   1.793  
   1.794 -	return;
   1.795 +    return;
   1.796  }
   1.797  
   1.798  void
   1.799 -SDL_SYS_JoystickQuit(void)
   1.800 +SDL_SYS_JoystickQuit (void)
   1.801  {
   1.802 -	int i;
   1.803 +    int i;
   1.804  
   1.805 -	for (i = 0; i < MAX_JOYS; i++) {
   1.806 -		if (joynames[i] != NULL)
   1.807 -			SDL_free(joynames[i]);
   1.808 -		if (joydevnames[i] != NULL)
   1.809 -			SDL_free(joydevnames[i]);
   1.810 -	}
   1.811 +    for (i = 0; i < MAX_JOYS; i++) {
   1.812 +        if (joynames[i] != NULL)
   1.813 +            SDL_free (joynames[i]);
   1.814 +        if (joydevnames[i] != NULL)
   1.815 +            SDL_free (joydevnames[i]);
   1.816 +    }
   1.817  
   1.818 -	return;
   1.819 +    return;
   1.820  }
   1.821  
   1.822  static int
   1.823 -report_alloc(struct report *r, struct report_desc *rd, int repind)
   1.824 +report_alloc (struct report *r, struct report_desc *rd, int repind)
   1.825  {
   1.826 -	int len;
   1.827 +    int len;
   1.828  
   1.829  #ifdef __DragonFly__
   1.830 -	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   1.831 +    len = hid_report_size (rd, r->rid, repinfo[repind].kind);
   1.832  #elif __FREEBSD__
   1.833  # if (__FreeBSD_version >= 460000)
   1.834  #  if (__FreeBSD_version <= 500111)
   1.835 -	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   1.836 +    len = hid_report_size (rd, r->rid, repinfo[repind].kind);
   1.837  #  else
   1.838 -	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   1.839 +    len = hid_report_size (rd, repinfo[repind].kind, r->rid);
   1.840  #  endif
   1.841  # else
   1.842 -	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   1.843 +    len = hid_report_size (rd, repinfo[repind].kind, &r->rid);
   1.844  # endif
   1.845  #else
   1.846  # ifdef USBHID_NEW
   1.847 -	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   1.848 +    len = hid_report_size (rd, repinfo[repind].kind, r->rid);
   1.849  # else
   1.850 -	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   1.851 +    len = hid_report_size (rd, repinfo[repind].kind, &r->rid);
   1.852  # endif
   1.853  #endif
   1.854  
   1.855 -	if (len < 0) {
   1.856 -		SDL_SetError("Negative HID report size");
   1.857 -		return (-1);
   1.858 -	}
   1.859 -	r->size = len;
   1.860 +    if (len < 0) {
   1.861 +        SDL_SetError ("Negative HID report size");
   1.862 +        return (-1);
   1.863 +    }
   1.864 +    r->size = len;
   1.865  
   1.866 -	if (r->size > 0) {
   1.867 -		r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
   1.868 -		    r->size);
   1.869 -		if (r->buf == NULL) {
   1.870 -			SDL_OutOfMemory();
   1.871 -			return (-1);
   1.872 -		}
   1.873 -	} else {
   1.874 -		r->buf = NULL;
   1.875 -	}
   1.876 +    if (r->size > 0) {
   1.877 +        r->buf = SDL_malloc (sizeof (*r->buf) - sizeof (REP_BUF_DATA (r)) +
   1.878 +                             r->size);
   1.879 +        if (r->buf == NULL) {
   1.880 +            SDL_OutOfMemory ();
   1.881 +            return (-1);
   1.882 +        }
   1.883 +    } else {
   1.884 +        r->buf = NULL;
   1.885 +    }
   1.886  
   1.887 -	r->status = SREPORT_CLEAN;
   1.888 -	return (0);
   1.889 +    r->status = SREPORT_CLEAN;
   1.890 +    return (0);
   1.891  }
   1.892  
   1.893  static void
   1.894 -report_free(struct report *r)
   1.895 +report_free (struct report *r)
   1.896  {
   1.897 -	if (r->buf != NULL) {
   1.898 -		SDL_free(r->buf);
   1.899 -	}
   1.900 -	r->status = SREPORT_UNINIT;
   1.901 +    if (r->buf != NULL) {
   1.902 +        SDL_free (r->buf);
   1.903 +    }
   1.904 +    r->status = SREPORT_UNINIT;
   1.905  }
   1.906  
   1.907  #endif /* SDL_JOYSTICK_USBHID */
   1.908 +/* vi: set ts=4 sw=4 expandtab: */