comparison src/joystick/bsd/SDL_sysjoystick.c @ 1565:57431b199aed

Fixed bug #52 Integrated most of the NetBSD and DragonFly patches at: ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/devel/SDL/patches/ Thanks to Thomas Klausner for defailed information on the patches
author Sam Lantinga <slouken@libsdl.org>
date Tue, 21 Mar 2006 08:54:50 +0000
parents e7f61857028a
children 92947e3a18db
comparison
equal deleted inserted replaced
1564:4950a25bd91e 1565:57431b199aed
35 #include <errno.h> 35 #include <errno.h>
36 36
37 #if defined(HAVE_USB_H) 37 #if defined(HAVE_USB_H)
38 #include <usb.h> 38 #include <usb.h>
39 #endif 39 #endif
40 #ifdef __DragonFly__
41 #include <bus/usb/usb.h>
42 #include <bus/usb/usbhid.h>
43 #else
40 #include <dev/usb/usb.h> 44 #include <dev/usb/usb.h>
41 #include <dev/usb/usbhid.h> 45 #include <dev/usb/usbhid.h>
46 #endif
42 47
43 #if defined(HAVE_USBHID_H) 48 #if defined(HAVE_USBHID_H)
44 #include <usbhid.h> 49 #include <usbhid.h>
45 #elif defined(HAVE_LIBUSB_H) 50 #elif defined(HAVE_LIBUSB_H)
46 #include <libusb.h> 51 #include <libusb.h>
47 #elif defined(HAVE_LIBUSBHID_H) 52 #elif defined(HAVE_LIBUSBHID_H)
48 #include <libusbhid.h> 53 #include <libusbhid.h>
49 #endif 54 #endif
50 55
51 #ifdef __FREEBSD__ 56 #ifdef __FREEBSD__
57 #ifndef __DragonFly__
52 #include <osreldate.h> 58 #include <osreldate.h>
59 #endif
53 #include <sys/joystick.h> 60 #include <sys/joystick.h>
54 #endif 61 #endif
55 62
56 #if defined(__NETBSD__) || (defined(__OPENBSD__) && defined(__i386__)) 63 #if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
57 #include <machine/joystick.h> 64 #include <machine/joystick.h>
58 #endif 65 #endif
59 66
60 #include "SDL_joystick.h" 67 #include "SDL_joystick.h"
61 #include "../SDL_sysjoystick.h" 68 #include "../SDL_sysjoystick.h"
229 struct joystick_hwdata *hw; 236 struct joystick_hwdata *hw;
230 struct hid_item hitem; 237 struct hid_item hitem;
231 struct hid_data *hdata; 238 struct hid_data *hdata;
232 struct report *rep; 239 struct report *rep;
233 int fd; 240 int fd;
241 int i;
234 242
235 fd = open(path, O_RDONLY); 243 fd = open(path, O_RDONLY);
236 if (fd == -1) { 244 if (fd == -1) {
237 SDL_SetError("%s: %s", path, strerror(errno)); 245 SDL_SetError("%s: %s", path, strerror(errno));
238 return (-1); 246 return (-1);
295 } 303 }
296 joy->naxes = 0; 304 joy->naxes = 0;
297 joy->nbuttons = 0; 305 joy->nbuttons = 0;
298 joy->nhats = 0; 306 joy->nhats = 0;
299 joy->nballs = 0; 307 joy->nballs = 0;
308 for (i=0; i<JOYAXE_count; i++)
309 hw->axis_map[i] = -1;
300 310
301 while (hid_get_item(hdata, &hitem) > 0) { 311 while (hid_get_item(hdata, &hitem) > 0) {
302 char *sp; 312 char *sp;
303 const char *s; 313 const char *s;
304 314
321 switch (HID_PAGE(hitem.usage)) { 331 switch (HID_PAGE(hitem.usage)) {
322 case HUP_GENERIC_DESKTOP: { 332 case HUP_GENERIC_DESKTOP: {
323 unsigned usage = HID_USAGE(hitem.usage); 333 unsigned usage = HID_USAGE(hitem.usage);
324 int joyaxe = usage_to_joyaxe(usage); 334 int joyaxe = usage_to_joyaxe(usage);
325 if (joyaxe >= 0) { 335 if (joyaxe >= 0) {
326 hw->axis_map[joyaxe] = joy->naxes; 336 hw->axis_map[joyaxe] = 1;
327 joy->naxes++;
328 } else if (usage == HUG_HAT_SWITCH) { 337 } else if (usage == HUG_HAT_SWITCH) {
329 joy->nhats++; 338 joy->nhats++;
330 } 339 }
331 break; 340 break;
332 } 341 }
340 default: 349 default:
341 break; 350 break;
342 } 351 }
343 } 352 }
344 hid_end_parse(hdata); 353 hid_end_parse(hdata);
354 for (i=0; i<JOYAXE_count; i++)
355 if (hw->axis_map[i] > 0)
356 hw->axis_map[i] = joy->naxes++;
345 357
346 usbend: 358 usbend:
347 /* The poll blocks the event thread. */ 359 /* The poll blocks the event thread. */
348 fcntl(fd, F_SETFL, O_NONBLOCK); 360 fcntl(fd, F_SETFL, O_NONBLOCK);
349 361
362 struct hid_data *hdata; 374 struct hid_data *hdata;
363 struct report *rep; 375 struct report *rep;
364 int nbutton, naxe = -1; 376 int nbutton, naxe = -1;
365 Sint32 v; 377 Sint32 v;
366 378
367 #if defined(__FREEBSD__) || defined(__NETBSD__) || (defined(__OPENBSD__) && defined(__i386__)) 379 #if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
368 struct joystick gameport; 380 struct joystick gameport;
369 static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0; 381 static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
370 382
371 if (joy->hwdata->type == BSDJOY_JOY) { 383 if (joy->hwdata->type == BSDJOY_JOY) {
372 if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport) 384 if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
411 if (gameport.b2 != joy->buttons[1]) { 423 if (gameport.b2 != joy->buttons[1]) {
412 SDL_PrivateJoystickButton(joy, 1, gameport.b2); 424 SDL_PrivateJoystickButton(joy, 1, gameport.b2);
413 } 425 }
414 return; 426 return;
415 } 427 }
416 #endif /* defined(__FREEBSD__) || defined(__NETBSD__) || defined(__OPENBSD__) */ 428 #endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
417 429
418 rep = &joy->hwdata->inreport; 430 rep = &joy->hwdata->inreport;
419 431
420 if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) { 432 if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
421 return; 433 return;
449 SDL_PrivateJoystickAxis(joy, naxe, v); 461 SDL_PrivateJoystickAxis(joy, naxe, v);
450 } 462 }
451 } else if (usage == HUG_HAT_SWITCH) { 463 } else if (usage == HUG_HAT_SWITCH) {
452 v = (Sint32)hid_get_data(REP_BUF_DATA(rep), 464 v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
453 &hitem); 465 &hitem);
454 SDL_PrivateJoystickHat(joy, 0, hatval_to_sdl(v)); 466 SDL_PrivateJoystickHat(joy, 0,
467 hatval_to_sdl(v)-hitem.logical_minimum);
455 } 468 }
456 break; 469 break;
457 } 470 }
458 case HUP_BUTTON: 471 case HUP_BUTTON:
459 v = (Sint32)hid_get_data(REP_BUF_DATA(rep), 472 v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
510 static int 523 static int
511 report_alloc(struct report *r, struct report_desc *rd, int repind) 524 report_alloc(struct report *r, struct report_desc *rd, int repind)
512 { 525 {
513 int len; 526 int len;
514 527
515 #ifdef __FREEBSD__ 528 #ifdef __DragonFly__
529 len = hid_report_size(rd, r->rid, repinfo[repind].kind);
530 #elif __FREEBSD__
516 # if (__FreeBSD_version >= 460000) 531 # if (__FreeBSD_version >= 460000)
517 # if (__FreeBSD_version <= 500111) 532 # if (__FreeBSD_version <= 500111)
518 len = hid_report_size(rd, r->rid, repinfo[repind].kind); 533 len = hid_report_size(rd, r->rid, repinfo[repind].kind);
519 # else 534 # else
520 len = hid_report_size(rd, repinfo[repind].kind, r->rid); 535 len = hid_report_size(rd, repinfo[repind].kind, r->rid);
521 # endif 536 # endif
522 # else 537 # else
523 len = hid_report_size(rd, repinfo[repind].kind, &r->rid); 538 len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
524 #endif 539 # endif
525 #else 540 #else
526 # ifdef USBHID_NEW 541 # ifdef USBHID_NEW
542 len = hid_report_size(rd, repinfo[repind].kind, r->rid);
543 # else
527 len = hid_report_size(rd, repinfo[repind].kind, &r->rid); 544 len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
528 # else
529 len = hid_report_size(rd, repinfo[repind].kind, r->rid);
530 # endif 545 # endif
531 #endif 546 #endif
532 547
533 if (len < 0) { 548 if (len < 0) {
534 SDL_SetError("Negative HID report size"); 549 SDL_SetError("Negative HID report size");