comparison src/joystick/bsd/SDL_sysjoystick.c @ 776:18922ae3ee07

Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
author Sam Lantinga <slouken@libsdl.org>
date Tue, 06 Jan 2004 04:07:12 +0000
parents b8d311d90021
children b68e551205e9
comparison
equal deleted inserted replaced
775:08b7fc2b5225 776:18922ae3ee07
55 #include <libusbhid.h> 55 #include <libusbhid.h>
56 #endif 56 #endif
57 57
58 #ifdef __FreeBSD__ 58 #ifdef __FreeBSD__
59 #include <osreldate.h> 59 #include <osreldate.h>
60 #include <sys/joystick.h>
61 #endif
62
63 #if defined(__NetBSD__) || defined(__OpenBSD__)
64 #include <machine/joystick.h>
60 #endif 65 #endif
61 66
62 #include "SDL_error.h" 67 #include "SDL_error.h"
63 #include "SDL_joystick.h" 68 #include "SDL_joystick.h"
64 #include "SDL_sysjoystick.h" 69 #include "SDL_sysjoystick.h"
137 char s[16]; 142 char s[16];
138 int i, fd; 143 int i, fd;
139 144
140 SDL_numjoysticks = 0; 145 SDL_numjoysticks = 0;
141 146
142 memset(joynames, NULL, sizeof(joynames)); 147 memset(joynames, 0, sizeof(joynames));
143 memset(joydevnames, NULL, sizeof(joydevnames)); 148 memset(joydevnames, 0, sizeof(joydevnames));
144 149
145 for (i = 0; i < MAX_UHID_JOYS; i++) { 150 for (i = 0; i < MAX_UHID_JOYS; i++) {
146 SDL_Joystick nj; 151 SDL_Joystick nj;
147 152
148 sprintf(s, "/dev/uhid%d", i); 153 sprintf(s, "/dev/uhid%d", i);
247 return (-1); 252 return (-1);
248 } 253 }
249 joy->hwdata = hw; 254 joy->hwdata = hw;
250 hw->fd = fd; 255 hw->fd = fd;
251 hw->path = strdup(path); 256 hw->path = strdup(path);
252 hw->type = BSDJOY_UHID; 257 if (! strncmp(path, "/dev/joy", 8)) {
258 hw->type = BSDJOY_JOY;
259 joy->naxes = 2;
260 joy->nbuttons = 2;
261 joy->nhats = 0;
262 joy->nballs = 0;
263 joydevnames[joy->index] = strdup("Gameport joystick");
264 goto usbend;
265 } else {
266 hw->type = BSDJOY_UHID;
267 }
268
253 { 269 {
254 int ax; 270 int ax;
255 for (ax = 0; ax < JOYAXE_count; ax++) 271 for (ax = 0; ax < JOYAXE_count; ax++)
256 hw->axis_map[ax] = -1; 272 hw->axis_map[ax] = -1;
257 } 273 }
330 break; 346 break;
331 } 347 }
332 } 348 }
333 hid_end_parse(hdata); 349 hid_end_parse(hdata);
334 350
351 usbend:
335 /* The poll blocks the event thread. */ 352 /* The poll blocks the event thread. */
336 fcntl(fd, F_SETFL, O_NONBLOCK); 353 fcntl(fd, F_SETFL, O_NONBLOCK);
337 354
338 return (0); 355 return (0);
339 usberr: 356 usberr:
349 struct hid_item hitem; 366 struct hid_item hitem;
350 struct hid_data *hdata; 367 struct hid_data *hdata;
351 struct report *rep; 368 struct report *rep;
352 int nbutton, naxe = -1; 369 int nbutton, naxe = -1;
353 Sint32 v; 370 Sint32 v;
371
372 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
373 struct joystick gameport;
374 static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
375
376 if (joy->hwdata->type == BSDJOY_JOY) {
377 if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
378 return;
379 if (abs(x - gameport.x) > 8) {
380 x = gameport.x;
381 if (x < xmin) {
382 xmin = x;
383 }
384 if (x > xmax) {
385 xmax = x;
386 }
387 if (xmin == xmax) {
388 xmin--;
389 xmax++;
390 }
391 v = (Sint32)x;
392 v -= (xmax + xmin + 1)/2;
393 v *= 32768/((xmax - xmin + 1)/2);
394 SDL_PrivateJoystickAxis(joy, 0, v);
395 }
396 if (abs(y - gameport.y) > 8) {
397 y = gameport.y;
398 if (y < ymin) {
399 ymin = y;
400 }
401 if (y > ymax) {
402 ymax = y;
403 }
404 if (ymin == ymax) {
405 ymin--;
406 ymax++;
407 }
408 v = (Sint32)y;
409 v -= (ymax + ymin + 1)/2;
410 v *= 32768/((ymax - ymin + 1)/2);
411 SDL_PrivateJoystickAxis(joy, 1, v);
412 }
413 if (gameport.b1 != joy->buttons[0]) {
414 SDL_PrivateJoystickButton(joy, 0, gameport.b1);
415 }
416 if (gameport.b2 != joy->buttons[1]) {
417 SDL_PrivateJoystickButton(joy, 1, gameport.b2);
418 }
419 return;
420 }
421 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) */
354 422
355 rep = &joy->hwdata->inreport; 423 rep = &joy->hwdata->inreport;
356 424
357 if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) { 425 if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
358 return; 426 return;
416 484
417 /* Function to close a joystick after use */ 485 /* Function to close a joystick after use */
418 void 486 void
419 SDL_SYS_JoystickClose(SDL_Joystick *joy) 487 SDL_SYS_JoystickClose(SDL_Joystick *joy)
420 { 488 {
421 report_free(&joy->hwdata->inreport); 489 if (strncmp(joy->hwdata->path, "/dev/joy", 8)) {
422 hid_dispose_report_desc(joy->hwdata->repdesc); 490 report_free(&joy->hwdata->inreport);
491 hid_dispose_report_desc(joy->hwdata->repdesc);
492 }
423 close(joy->hwdata->fd); 493 close(joy->hwdata->fd);
424 free(joy->hwdata->path); 494 free(joy->hwdata->path);
425 free(joy->hwdata); 495 free(joy->hwdata);
426 496
427 return; 497 return;