comparison src/joystick/linux/SDL_sysjoystick.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
307 307
308 308
309 #ifndef NO_LOGICAL_JOYSTICKS 309 #ifndef NO_LOGICAL_JOYSTICKS
310 310
311 static int 311 static int
312 CountLogicalJoysticks (int max) 312 CountLogicalJoysticks(int max)
313 { 313 {
314 register int i, j, k, ret, prev; 314 register int i, j, k, ret, prev;
315 const char *name; 315 const char *name;
316 int nbuttons, fd; 316 int nbuttons, fd;
317 unsigned char n; 317 unsigned char n;
318 318
319 ret = 0; 319 ret = 0;
320 320
321 for (i = 0; i < max; i++) { 321 for (i = 0; i < max; i++) {
322 name = SDL_SYS_JoystickName (i); 322 name = SDL_SYS_JoystickName(i);
323 323
324 fd = open (SDL_joylist[i].fname, O_RDONLY, 0); 324 fd = open(SDL_joylist[i].fname, O_RDONLY, 0);
325 if (fd >= 0) { 325 if (fd >= 0) {
326 if (ioctl (fd, JSIOCGBUTTONS, &n) < 0) { 326 if (ioctl(fd, JSIOCGBUTTONS, &n) < 0) {
327 nbuttons = -1; 327 nbuttons = -1;
328 } else { 328 } else {
329 nbuttons = n; 329 nbuttons = n;
330 } 330 }
331 close (fd); 331 close(fd);
332 } else { 332 } else {
333 nbuttons = -1; 333 nbuttons = -1;
334 } 334 }
335 335
336 if (name) { 336 if (name) {
337 for (j = 0; j < SDL_arraysize (joystick_logicalmap); j++) { 337 for (j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
338 if (!SDL_strcmp (name, joystick_logicalmap[j].name) 338 if (!SDL_strcmp(name, joystick_logicalmap[j].name)
339 && (nbuttons == -1 339 && (nbuttons == -1
340 || nbuttons == joystick_logicalmap[j].nbuttons)) { 340 || nbuttons == joystick_logicalmap[j].nbuttons)) {
341 prev = i; 341 prev = i;
342 SDL_joylist[prev].map = &(joystick_logicalmap[j]); 342 SDL_joylist[prev].map = &(joystick_logicalmap[j]);
343 343
359 359
360 return ret; 360 return ret;
361 } 361 }
362 362
363 static void 363 static void
364 LogicalSuffix (int logicalno, char *namebuf, int len) 364 LogicalSuffix(int logicalno, char *namebuf, int len)
365 { 365 {
366 register int slen; 366 register int slen;
367 const static char suffixs[] = 367 const static char suffixs[] =
368 "01020304050607080910111213141516171819" "20212223242526272829303132"; 368 "01020304050607080910111213141516171819" "20212223242526272829303132";
369 const char *suffix; 369 const char *suffix;
370 slen = SDL_strlen (namebuf); 370 slen = SDL_strlen(namebuf);
371 suffix = NULL; 371 suffix = NULL;
372 372
373 if (logicalno * 2 < sizeof (suffixs)) 373 if (logicalno * 2 < sizeof(suffixs))
374 suffix = suffixs + (logicalno * 2); 374 suffix = suffixs + (logicalno * 2);
375 375
376 if (slen + 4 < len && suffix) { 376 if (slen + 4 < len && suffix) {
377 namebuf[slen++] = ' '; 377 namebuf[slen++] = ' ';
378 namebuf[slen++] = '#'; 378 namebuf[slen++] = '#';
387 #if SDL_INPUT_LINUXEV 387 #if SDL_INPUT_LINUXEV
388 #define test_bit(nr, addr) \ 388 #define test_bit(nr, addr) \
389 (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0) 389 (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
390 390
391 static int 391 static int
392 EV_IsJoystick (int fd) 392 EV_IsJoystick(int fd)
393 { 393 {
394 unsigned long evbit[40]; 394 unsigned long evbit[40];
395 unsigned long keybit[40]; 395 unsigned long keybit[40];
396 unsigned long absbit[40]; 396 unsigned long absbit[40];
397 397
398 if ((ioctl (fd, EVIOCGBIT (0, sizeof (evbit)), evbit) < 0) || 398 if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
399 (ioctl (fd, EVIOCGBIT (EV_KEY, sizeof (keybit)), keybit) < 0) || 399 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
400 (ioctl (fd, EVIOCGBIT (EV_ABS, sizeof (absbit)), absbit) < 0)) { 400 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
401 return (0); 401 return (0);
402 } 402 }
403 if (!(test_bit (EV_KEY, evbit) && test_bit (EV_ABS, evbit) && 403 if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
404 test_bit (ABS_X, absbit) && test_bit (ABS_Y, absbit) && 404 test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit) &&
405 (test_bit (BTN_TRIGGER, keybit) || test_bit (BTN_A, keybit) 405 (test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_A, keybit)
406 || test_bit (BTN_1, keybit)))) 406 || test_bit(BTN_1, keybit))))
407 return 0; 407 return 0;
408 return (1); 408 return (1);
409 } 409 }
410 410
411 #endif /* SDL_INPUT_LINUXEV */ 411 #endif /* SDL_INPUT_LINUXEV */
412 412
413 /* Function to scan the system for joysticks */ 413 /* Function to scan the system for joysticks */
414 int 414 int
415 SDL_SYS_JoystickInit (void) 415 SDL_SYS_JoystickInit(void)
416 { 416 {
417 /* The base path of the joystick devices */ 417 /* The base path of the joystick devices */
418 const char *joydev_pattern[] = { 418 const char *joydev_pattern[] = {
419 #if SDL_INPUT_LINUXEV 419 #if SDL_INPUT_LINUXEV
420 "/dev/input/event%d", 420 "/dev/input/event%d",
431 int n, duplicate; 431 int n, duplicate;
432 432
433 numjoysticks = 0; 433 numjoysticks = 0;
434 434
435 /* First see if the user specified a joystick to use */ 435 /* First see if the user specified a joystick to use */
436 if (SDL_getenv ("SDL_JOYSTICK_DEVICE") != NULL) { 436 if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
437 SDL_strlcpy (path, SDL_getenv ("SDL_JOYSTICK_DEVICE"), sizeof (path)); 437 SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
438 if (stat (path, &sb) == 0) { 438 if (stat(path, &sb) == 0) {
439 fd = open (path, O_RDONLY, 0); 439 fd = open(path, O_RDONLY, 0);
440 if (fd >= 0) { 440 if (fd >= 0) {
441 /* Assume the user knows what they're doing. */ 441 /* Assume the user knows what they're doing. */
442 SDL_joylist[numjoysticks].fname = SDL_strdup (path); 442 SDL_joylist[numjoysticks].fname = SDL_strdup(path);
443 if (SDL_joylist[numjoysticks].fname) { 443 if (SDL_joylist[numjoysticks].fname) {
444 dev_nums[numjoysticks] = sb.st_rdev; 444 dev_nums[numjoysticks] = sb.st_rdev;
445 ++numjoysticks; 445 ++numjoysticks;
446 } 446 }
447 close (fd); 447 close(fd);
448 } 448 }
449 } 449 }
450 } 450 }
451 451
452 for (i = 0; i < SDL_arraysize (joydev_pattern); ++i) { 452 for (i = 0; i < SDL_arraysize(joydev_pattern); ++i) {
453 for (j = 0; j < MAX_JOYSTICKS; ++j) { 453 for (j = 0; j < MAX_JOYSTICKS; ++j) {
454 SDL_snprintf (path, SDL_arraysize (path), joydev_pattern[i], j); 454 SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
455 455
456 /* rcg06302000 replaced access(F_OK) call with stat(). 456 /* rcg06302000 replaced access(F_OK) call with stat().
457 * stat() will fail if the file doesn't exist, so it's 457 * stat() will fail if the file doesn't exist, so it's
458 * equivalent behaviour. 458 * equivalent behaviour.
459 */ 459 */
460 if (stat (path, &sb) == 0) { 460 if (stat(path, &sb) == 0) {
461 /* Check to make sure it's not already in list. 461 /* Check to make sure it's not already in list.
462 * This happens when we see a stick via symlink. 462 * This happens when we see a stick via symlink.
463 */ 463 */
464 duplicate = 0; 464 duplicate = 0;
465 for (n = 0; (n < numjoysticks) && !duplicate; ++n) { 465 for (n = 0; (n < numjoysticks) && !duplicate; ++n) {
469 } 469 }
470 if (duplicate) { 470 if (duplicate) {
471 continue; 471 continue;
472 } 472 }
473 473
474 fd = open (path, O_RDONLY, 0); 474 fd = open(path, O_RDONLY, 0);
475 if (fd < 0) { 475 if (fd < 0) {
476 continue; 476 continue;
477 } 477 }
478 #if SDL_INPUT_LINUXEV 478 #if SDL_INPUT_LINUXEV
479 #ifdef DEBUG_INPUT_EVENTS 479 #ifdef DEBUG_INPUT_EVENTS
480 printf ("Checking %s\n", path); 480 printf("Checking %s\n", path);
481 #endif 481 #endif
482 if ((i == 0) && !EV_IsJoystick (fd)) { 482 if ((i == 0) && !EV_IsJoystick(fd)) {
483 close (fd); 483 close(fd);
484 continue; 484 continue;
485 } 485 }
486 #endif 486 #endif
487 close (fd); 487 close(fd);
488 488
489 /* We're fine, add this joystick */ 489 /* We're fine, add this joystick */
490 SDL_joylist[numjoysticks].fname = SDL_strdup (path); 490 SDL_joylist[numjoysticks].fname = SDL_strdup(path);
491 if (SDL_joylist[numjoysticks].fname) { 491 if (SDL_joylist[numjoysticks].fname) {
492 dev_nums[numjoysticks] = sb.st_rdev; 492 dev_nums[numjoysticks] = sb.st_rdev;
493 ++numjoysticks; 493 ++numjoysticks;
494 } 494 }
495 } else 495 } else
507 if ((i == 0) && (numjoysticks > 0)) 507 if ((i == 0) && (numjoysticks > 0))
508 break; 508 break;
509 #endif 509 #endif
510 } 510 }
511 #ifndef NO_LOGICAL_JOYSTICKS 511 #ifndef NO_LOGICAL_JOYSTICKS
512 numjoysticks += CountLogicalJoysticks (numjoysticks); 512 numjoysticks += CountLogicalJoysticks(numjoysticks);
513 #endif 513 #endif
514 514
515 return (numjoysticks); 515 return (numjoysticks);
516 } 516 }
517 517
518 /* Function to get the device-dependent name of a joystick */ 518 /* Function to get the device-dependent name of a joystick */
519 const char * 519 const char *
520 SDL_SYS_JoystickName (int index) 520 SDL_SYS_JoystickName(int index)
521 { 521 {
522 int fd; 522 int fd;
523 static char namebuf[128]; 523 static char namebuf[128];
524 char *name; 524 char *name;
525 SDL_logical_joydecl (int oindex = index); 525 SDL_logical_joydecl(int oindex = index);
526 526
527 #ifndef NO_LOGICAL_JOYSTICKS 527 #ifndef NO_LOGICAL_JOYSTICKS
528 SDL_joylist_head (index, index); 528 SDL_joylist_head(index, index);
529 #endif 529 #endif
530 name = NULL; 530 name = NULL;
531 fd = open (SDL_joylist[index].fname, O_RDONLY, 0); 531 fd = open(SDL_joylist[index].fname, O_RDONLY, 0);
532 if (fd >= 0) { 532 if (fd >= 0) {
533 if ( 533 if (
534 #if SDL_INPUT_LINUXEV 534 #if SDL_INPUT_LINUXEV
535 (ioctl (fd, EVIOCGNAME (sizeof (namebuf)), namebuf) <= 0) && 535 (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) &&
536 #endif 536 #endif
537 (ioctl (fd, JSIOCGNAME (sizeof (namebuf)), namebuf) <= 0)) { 537 (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0)) {
538 name = SDL_joylist[index].fname; 538 name = SDL_joylist[index].fname;
539 } else { 539 } else {
540 name = namebuf; 540 name = namebuf;
541 } 541 }
542 close (fd); 542 close(fd);
543 543
544 544
545 #ifndef NO_LOGICAL_JOYSTICKS 545 #ifndef NO_LOGICAL_JOYSTICKS
546 if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next 546 if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next
547 || index != oindex) { 547 || index != oindex) {
548 LogicalSuffix (SDL_joylist[oindex].logicalno, namebuf, 128); 548 LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128);
549 } 549 }
550 #endif 550 #endif
551 } 551 }
552 return name; 552 return name;
553 } 553 }
554 554
555 static int 555 static int
556 allocate_hatdata (SDL_Joystick * joystick) 556 allocate_hatdata(SDL_Joystick * joystick)
557 { 557 {
558 int i; 558 int i;
559 559
560 joystick->hwdata->hats = 560 joystick->hwdata->hats =
561 (struct hwdata_hat *) SDL_malloc (joystick->nhats * 561 (struct hwdata_hat *) SDL_malloc(joystick->nhats *
562 sizeof (struct hwdata_hat)); 562 sizeof(struct hwdata_hat));
563 if (joystick->hwdata->hats == NULL) { 563 if (joystick->hwdata->hats == NULL) {
564 return (-1); 564 return (-1);
565 } 565 }
566 for (i = 0; i < joystick->nhats; ++i) { 566 for (i = 0; i < joystick->nhats; ++i) {
567 joystick->hwdata->hats[i].axis[0] = 1; 567 joystick->hwdata->hats[i].axis[0] = 1;
569 } 569 }
570 return (0); 570 return (0);
571 } 571 }
572 572
573 static int 573 static int
574 allocate_balldata (SDL_Joystick * joystick) 574 allocate_balldata(SDL_Joystick * joystick)
575 { 575 {
576 int i; 576 int i;
577 577
578 joystick->hwdata->balls = 578 joystick->hwdata->balls =
579 (struct hwdata_ball *) SDL_malloc (joystick->nballs * 579 (struct hwdata_ball *) SDL_malloc(joystick->nballs *
580 sizeof (struct hwdata_ball)); 580 sizeof(struct hwdata_ball));
581 if (joystick->hwdata->balls == NULL) { 581 if (joystick->hwdata->balls == NULL) {
582 return (-1); 582 return (-1);
583 } 583 }
584 for (i = 0; i < joystick->nballs; ++i) { 584 for (i = 0; i < joystick->nballs; ++i) {
585 joystick->hwdata->balls[i].axis[0] = 0; 585 joystick->hwdata->balls[i].axis[0] = 0;
587 } 587 }
588 return (0); 588 return (0);
589 } 589 }
590 590
591 static SDL_bool 591 static SDL_bool
592 JS_ConfigJoystick (SDL_Joystick * joystick, int fd) 592 JS_ConfigJoystick(SDL_Joystick * joystick, int fd)
593 { 593 {
594 SDL_bool handled; 594 SDL_bool handled;
595 unsigned char n; 595 unsigned char n;
596 int old_axes, tmp_naxes, tmp_nhats, tmp_nballs; 596 int old_axes, tmp_naxes, tmp_nhats, tmp_nballs;
597 const char *name; 597 const char *name;
599 int i; 599 int i;
600 600
601 handled = SDL_FALSE; 601 handled = SDL_FALSE;
602 602
603 /* Default joystick device settings */ 603 /* Default joystick device settings */
604 if (ioctl (fd, JSIOCGAXES, &n) < 0) { 604 if (ioctl(fd, JSIOCGAXES, &n) < 0) {
605 joystick->naxes = 2; 605 joystick->naxes = 2;
606 } else { 606 } else {
607 joystick->naxes = n; 607 joystick->naxes = n;
608 } 608 }
609 if (ioctl (fd, JSIOCGBUTTONS, &n) < 0) { 609 if (ioctl(fd, JSIOCGBUTTONS, &n) < 0) {
610 joystick->nbuttons = 2; 610 joystick->nbuttons = 2;
611 } else { 611 } else {
612 joystick->nbuttons = n; 612 joystick->nbuttons = n;
613 } 613 }
614 614
615 name = SDL_SYS_JoystickName (joystick->index); 615 name = SDL_SYS_JoystickName(joystick->index);
616 old_axes = joystick->naxes; 616 old_axes = joystick->naxes;
617 617
618 /* Generic analog joystick support */ 618 /* Generic analog joystick support */
619 if (SDL_strstr (name, "Analog") == name && SDL_strstr (name, "-hat")) { 619 if (SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat")) {
620 if (SDL_sscanf (name, "Analog %d-axis %*d-button %d-hat", 620 if (SDL_sscanf(name, "Analog %d-axis %*d-button %d-hat",
621 &tmp_naxes, &tmp_nhats) == 2) { 621 &tmp_naxes, &tmp_nhats) == 2) {
622 622
623 joystick->naxes = tmp_naxes; 623 joystick->naxes = tmp_naxes;
624 joystick->nhats = tmp_nhats; 624 joystick->nhats = tmp_nhats;
625 625
626 handled = SDL_TRUE; 626 handled = SDL_TRUE;
627 } 627 }
628 } 628 }
629 629
630 /* Special joystick support */ 630 /* Special joystick support */
631 for (i = 0; i < SDL_arraysize (special_joysticks); ++i) { 631 for (i = 0; i < SDL_arraysize(special_joysticks); ++i) {
632 if (SDL_strcmp (name, special_joysticks[i].name) == 0) { 632 if (SDL_strcmp(name, special_joysticks[i].name) == 0) {
633 633
634 joystick->naxes = special_joysticks[i].naxes; 634 joystick->naxes = special_joysticks[i].naxes;
635 joystick->nhats = special_joysticks[i].nhats; 635 joystick->nhats = special_joysticks[i].nhats;
636 joystick->nballs = special_joysticks[i].nballs; 636 joystick->nballs = special_joysticks[i].nballs;
637 637
639 break; 639 break;
640 } 640 }
641 } 641 }
642 642
643 /* User environment joystick support */ 643 /* User environment joystick support */
644 if ((env = SDL_getenv ("SDL_LINUX_JOYSTICK"))) { 644 if ((env = SDL_getenv("SDL_LINUX_JOYSTICK"))) {
645 *env_name = '\0'; 645 *env_name = '\0';
646 if (*env == '\'' && SDL_sscanf (env, "'%[^']s'", env_name) == 1) 646 if (*env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1)
647 env += SDL_strlen (env_name) + 2; 647 env += SDL_strlen(env_name) + 2;
648 else if (SDL_sscanf (env, "%s", env_name) == 1) 648 else if (SDL_sscanf(env, "%s", env_name) == 1)
649 env += SDL_strlen (env_name); 649 env += SDL_strlen(env_name);
650 650
651 if (SDL_strcmp (name, env_name) == 0) { 651 if (SDL_strcmp(name, env_name) == 0) {
652 652
653 if (SDL_sscanf (env, "%d %d %d", &tmp_naxes, &tmp_nhats, 653 if (SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats,
654 &tmp_nballs) == 3) { 654 &tmp_nballs) == 3) {
655 655
656 joystick->naxes = tmp_naxes; 656 joystick->naxes = tmp_naxes;
657 joystick->nhats = tmp_nhats; 657 joystick->nhats = tmp_nhats;
658 joystick->nballs = tmp_nballs; 658 joystick->nballs = tmp_nballs;
659 659
663 } 663 }
664 664
665 /* Remap hats and balls */ 665 /* Remap hats and balls */
666 if (handled) { 666 if (handled) {
667 if (joystick->nhats > 0) { 667 if (joystick->nhats > 0) {
668 if (allocate_hatdata (joystick) < 0) { 668 if (allocate_hatdata(joystick) < 0) {
669 joystick->nhats = 0; 669 joystick->nhats = 0;
670 } 670 }
671 } 671 }
672 if (joystick->nballs > 0) { 672 if (joystick->nballs > 0) {
673 if (allocate_balldata (joystick) < 0) { 673 if (allocate_balldata(joystick) < 0) {
674 joystick->nballs = 0; 674 joystick->nballs = 0;
675 } 675 }
676 } 676 }
677 } 677 }
678 678
680 } 680 }
681 681
682 #if SDL_INPUT_LINUXEV 682 #if SDL_INPUT_LINUXEV
683 683
684 static SDL_bool 684 static SDL_bool
685 EV_ConfigJoystick (SDL_Joystick * joystick, int fd) 685 EV_ConfigJoystick(SDL_Joystick * joystick, int fd)
686 { 686 {
687 int i, t; 687 int i, t;
688 unsigned long keybit[40]; 688 unsigned long keybit[40];
689 unsigned long absbit[40]; 689 unsigned long absbit[40];
690 unsigned long relbit[40]; 690 unsigned long relbit[40];
691 691
692 /* See if this device uses the new unified event API */ 692 /* See if this device uses the new unified event API */
693 if ((ioctl (fd, EVIOCGBIT (EV_KEY, sizeof (keybit)), keybit) >= 0) && 693 if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
694 (ioctl (fd, EVIOCGBIT (EV_ABS, sizeof (absbit)), absbit) >= 0) && 694 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
695 (ioctl (fd, EVIOCGBIT (EV_REL, sizeof (relbit)), relbit) >= 0)) { 695 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
696 joystick->hwdata->is_hid = SDL_TRUE; 696 joystick->hwdata->is_hid = SDL_TRUE;
697 697
698 /* Get the number of buttons, axes, and other thingamajigs */ 698 /* Get the number of buttons, axes, and other thingamajigs */
699 for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) { 699 for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
700 if (test_bit (i, keybit)) { 700 if (test_bit(i, keybit)) {
701 #ifdef DEBUG_INPUT_EVENTS 701 #ifdef DEBUG_INPUT_EVENTS
702 printf ("Joystick has button: 0x%x\n", i); 702 printf("Joystick has button: 0x%x\n", i);
703 #endif 703 #endif
704 joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons; 704 joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
705 ++joystick->nbuttons; 705 ++joystick->nbuttons;
706 } 706 }
707 } 707 }
708 for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) { 708 for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
709 if (test_bit (i, keybit)) { 709 if (test_bit(i, keybit)) {
710 #ifdef DEBUG_INPUT_EVENTS 710 #ifdef DEBUG_INPUT_EVENTS
711 printf ("Joystick has button: 0x%x\n", i); 711 printf("Joystick has button: 0x%x\n", i);
712 #endif 712 #endif
713 joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons; 713 joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
714 ++joystick->nbuttons; 714 ++joystick->nbuttons;
715 } 715 }
716 } 716 }
718 /* Skip hats */ 718 /* Skip hats */
719 if (i == ABS_HAT0X) { 719 if (i == ABS_HAT0X) {
720 i = ABS_HAT3Y; 720 i = ABS_HAT3Y;
721 continue; 721 continue;
722 } 722 }
723 if (test_bit (i, absbit)) { 723 if (test_bit(i, absbit)) {
724 int values[5]; 724 int values[5];
725 725
726 if (ioctl (fd, EVIOCGABS (i), values) < 0) 726 if (ioctl(fd, EVIOCGABS(i), values) < 0)
727 continue; 727 continue;
728 #ifdef DEBUG_INPUT_EVENTS 728 #ifdef DEBUG_INPUT_EVENTS
729 printf ("Joystick has absolute axis: %x\n", i); 729 printf("Joystick has absolute axis: %x\n", i);
730 printf ("Values = { %d, %d, %d, %d, %d }\n", 730 printf("Values = { %d, %d, %d, %d, %d }\n",
731 values[0], values[1], 731 values[0], values[1], values[2], values[3], values[4]);
732 values[2], values[3], values[4]);
733 #endif /* DEBUG_INPUT_EVENTS */ 732 #endif /* DEBUG_INPUT_EVENTS */
734 joystick->hwdata->abs_map[i] = joystick->naxes; 733 joystick->hwdata->abs_map[i] = joystick->naxes;
735 if (values[1] == values[2]) { 734 if (values[1] == values[2]) {
736 joystick->hwdata->abs_correct[i].used = 0; 735 joystick->hwdata->abs_correct[i].used = 0;
737 } else { 736 } else {
750 } 749 }
751 ++joystick->naxes; 750 ++joystick->naxes;
752 } 751 }
753 } 752 }
754 for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) { 753 for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
755 if (test_bit (i, absbit) || test_bit (i + 1, absbit)) { 754 if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
756 #ifdef DEBUG_INPUT_EVENTS 755 #ifdef DEBUG_INPUT_EVENTS
757 printf ("Joystick has hat %d\n", (i - ABS_HAT0X) / 2); 756 printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
758 #endif 757 #endif
759 ++joystick->nhats; 758 ++joystick->nhats;
760 } 759 }
761 } 760 }
762 if (test_bit (REL_X, relbit) || test_bit (REL_Y, relbit)) { 761 if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
763 ++joystick->nballs; 762 ++joystick->nballs;
764 } 763 }
765 764
766 /* Allocate data to keep track of these thingamajigs */ 765 /* Allocate data to keep track of these thingamajigs */
767 if (joystick->nhats > 0) { 766 if (joystick->nhats > 0) {
768 if (allocate_hatdata (joystick) < 0) { 767 if (allocate_hatdata(joystick) < 0) {
769 joystick->nhats = 0; 768 joystick->nhats = 0;
770 } 769 }
771 } 770 }
772 if (joystick->nballs > 0) { 771 if (joystick->nballs > 0) {
773 if (allocate_balldata (joystick) < 0) { 772 if (allocate_balldata(joystick) < 0) {
774 joystick->nballs = 0; 773 joystick->nballs = 0;
775 } 774 }
776 } 775 }
777 } 776 }
778 return (joystick->hwdata->is_hid); 777 return (joystick->hwdata->is_hid);
780 779
781 #endif /* SDL_INPUT_LINUXEV */ 780 #endif /* SDL_INPUT_LINUXEV */
782 781
783 #ifndef NO_LOGICAL_JOYSTICKS 782 #ifndef NO_LOGICAL_JOYSTICKS
784 static void 783 static void
785 ConfigLogicalJoystick (SDL_Joystick * joystick) 784 ConfigLogicalJoystick(SDL_Joystick * joystick)
786 { 785 {
787 struct joystick_logical_layout *layout; 786 struct joystick_logical_layout *layout;
788 787
789 layout = SDL_joylist[joystick->index].map->layout + 788 layout = SDL_joylist[joystick->index].map->layout +
790 SDL_joylist[joystick->index].logicalno; 789 SDL_joylist[joystick->index].logicalno;
801 The joystick to open is specified by the index field of the joystick. 800 The joystick to open is specified by the index field of the joystick.
802 This should fill the nbuttons and naxes fields of the joystick structure. 801 This should fill the nbuttons and naxes fields of the joystick structure.
803 It returns 0, or -1 if there is an error. 802 It returns 0, or -1 if there is an error.
804 */ 803 */
805 int 804 int
806 SDL_SYS_JoystickOpen (SDL_Joystick * joystick) 805 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
807 { 806 {
808 int fd; 807 int fd;
809 SDL_logical_joydecl (int realindex); 808 SDL_logical_joydecl(int realindex);
810 SDL_logical_joydecl (SDL_Joystick * realjoy = NULL); 809 SDL_logical_joydecl(SDL_Joystick * realjoy = NULL);
811 810
812 /* Open the joystick and set the joystick file descriptor */ 811 /* Open the joystick and set the joystick file descriptor */
813 #ifndef NO_LOGICAL_JOYSTICKS 812 #ifndef NO_LOGICAL_JOYSTICKS
814 if (SDL_joylist[joystick->index].fname == NULL) { 813 if (SDL_joylist[joystick->index].fname == NULL) {
815 SDL_joylist_head (realindex, joystick->index); 814 SDL_joylist_head(realindex, joystick->index);
816 realjoy = SDL_JoystickOpen (realindex); 815 realjoy = SDL_JoystickOpen(realindex);
817 816
818 if (realjoy == NULL) 817 if (realjoy == NULL)
819 return (-1); 818 return (-1);
820 819
821 fd = realjoy->hwdata->fd; 820 fd = realjoy->hwdata->fd;
822 821
823 } else { 822 } else {
824 fd = open (SDL_joylist[joystick->index].fname, O_RDONLY, 0); 823 fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
825 } 824 }
826 SDL_joylist[joystick->index].joy = joystick; 825 SDL_joylist[joystick->index].joy = joystick;
827 #else 826 #else
828 fd = open (SDL_joylist[joystick->index].fname, O_RDONLY, 0); 827 fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
829 #endif 828 #endif
830 829
831 if (fd < 0) { 830 if (fd < 0) {
832 SDL_SetError ("Unable to open %s\n", SDL_joylist[joystick->index]); 831 SDL_SetError("Unable to open %s\n", SDL_joylist[joystick->index]);
833 return (-1); 832 return (-1);
834 } 833 }
835 joystick->hwdata = (struct joystick_hwdata *) 834 joystick->hwdata = (struct joystick_hwdata *)
836 SDL_malloc (sizeof (*joystick->hwdata)); 835 SDL_malloc(sizeof(*joystick->hwdata));
837 if (joystick->hwdata == NULL) { 836 if (joystick->hwdata == NULL) {
838 SDL_OutOfMemory (); 837 SDL_OutOfMemory();
839 close (fd); 838 close(fd);
840 return (-1); 839 return (-1);
841 } 840 }
842 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata)); 841 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
843 joystick->hwdata->fd = fd; 842 joystick->hwdata->fd = fd;
844 843
845 /* Set the joystick to non-blocking read mode */ 844 /* Set the joystick to non-blocking read mode */
846 fcntl (fd, F_SETFL, O_NONBLOCK); 845 fcntl(fd, F_SETFL, O_NONBLOCK);
847 846
848 /* Get the number of buttons and axes on the joystick */ 847 /* Get the number of buttons and axes on the joystick */
849 #ifndef NO_LOGICAL_JOYSTICKS 848 #ifndef NO_LOGICAL_JOYSTICKS
850 if (realjoy) 849 if (realjoy)
851 ConfigLogicalJoystick (joystick); 850 ConfigLogicalJoystick(joystick);
852 else 851 else
853 #endif 852 #endif
854 #if SDL_INPUT_LINUXEV 853 #if SDL_INPUT_LINUXEV
855 if (!EV_ConfigJoystick (joystick, fd)) 854 if (!EV_ConfigJoystick(joystick, fd))
856 #endif 855 #endif
857 JS_ConfigJoystick (joystick, fd); 856 JS_ConfigJoystick(joystick, fd);
858 857
859 return (0); 858 return (0);
860 } 859 }
861 860
862 #ifndef NO_LOGICAL_JOYSTICKS 861 #ifndef NO_LOGICAL_JOYSTICKS
863 862
864 static SDL_Joystick * 863 static SDL_Joystick *
865 FindLogicalJoystick (SDL_Joystick * joystick, 864 FindLogicalJoystick(SDL_Joystick * joystick,
866 struct joystick_logical_mapping *v) 865 struct joystick_logical_mapping *v)
867 { 866 {
868 SDL_Joystick *logicaljoy; 867 SDL_Joystick *logicaljoy;
869 register int i; 868 register int i;
870 869
871 i = joystick->index; 870 i = joystick->index;
889 888
890 return logicaljoy; 889 return logicaljoy;
891 } 890 }
892 891
893 static int 892 static int
894 LogicalJoystickButton (SDL_Joystick * joystick, Uint8 button, Uint8 state) 893 LogicalJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
895 { 894 {
896 struct joystick_logical_mapping *buttons; 895 struct joystick_logical_mapping *buttons;
897 SDL_Joystick *logicaljoy = NULL; 896 SDL_Joystick *logicaljoy = NULL;
898 897
899 /* if there's no map then this is just a regular joystick 898 /* if there's no map then this is just a regular joystick
902 return 0; 901 return 0;
903 902
904 /* get the logical joystick that will receive the event 903 /* get the logical joystick that will receive the event
905 */ 904 */
906 buttons = SDL_joylist[joystick->index].map->buttonmap + button; 905 buttons = SDL_joylist[joystick->index].map->buttonmap + button;
907 logicaljoy = FindLogicalJoystick (joystick, buttons); 906 logicaljoy = FindLogicalJoystick(joystick, buttons);
908 907
909 if (logicaljoy == NULL) 908 if (logicaljoy == NULL)
910 return 1; 909 return 1;
911 910
912 SDL_PrivateJoystickButton (logicaljoy, buttons->nthing, state); 911 SDL_PrivateJoystickButton(logicaljoy, buttons->nthing, state);
913 912
914 return 1; 913 return 1;
915 } 914 }
916 915
917 static int 916 static int
918 LogicalJoystickAxis (SDL_Joystick * joystick, Uint8 axis, Sint16 value) 917 LogicalJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
919 { 918 {
920 struct joystick_logical_mapping *axes; 919 struct joystick_logical_mapping *axes;
921 SDL_Joystick *logicaljoy = NULL; 920 SDL_Joystick *logicaljoy = NULL;
922 921
923 /* if there's no map then this is just a regular joystick 922 /* if there's no map then this is just a regular joystick
926 return 0; 925 return 0;
927 926
928 /* get the logical joystick that will receive the event 927 /* get the logical joystick that will receive the event
929 */ 928 */
930 axes = SDL_joylist[joystick->index].map->axismap + axis; 929 axes = SDL_joylist[joystick->index].map->axismap + axis;
931 logicaljoy = FindLogicalJoystick (joystick, axes); 930 logicaljoy = FindLogicalJoystick(joystick, axes);
932 931
933 if (logicaljoy == NULL) 932 if (logicaljoy == NULL)
934 return 1; 933 return 1;
935 934
936 SDL_PrivateJoystickAxis (logicaljoy, axes->nthing, value); 935 SDL_PrivateJoystickAxis(logicaljoy, axes->nthing, value);
937 936
938 return 1; 937 return 1;
939 } 938 }
940 #endif /* USE_LOGICAL_JOYSTICKS */ 939 #endif /* USE_LOGICAL_JOYSTICKS */
941 940
942 static __inline__ void 941 static __inline__ void
943 HandleHat (SDL_Joystick * stick, Uint8 hat, int axis, int value) 942 HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
944 { 943 {
945 struct hwdata_hat *the_hat; 944 struct hwdata_hat *the_hat;
946 const Uint8 position_map[3][3] = { 945 const Uint8 position_map[3][3] = {
947 {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP}, 946 {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
948 {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT}, 947 {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
949 {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN} 948 {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
950 }; 949 };
951 SDL_logical_joydecl (SDL_Joystick * logicaljoy = NULL); 950 SDL_logical_joydecl(SDL_Joystick * logicaljoy = NULL);
952 SDL_logical_joydecl (struct joystick_logical_mapping *hats = NULL); 951 SDL_logical_joydecl(struct joystick_logical_mapping *hats = NULL);
953 952
954 the_hat = &stick->hwdata->hats[hat]; 953 the_hat = &stick->hwdata->hats[hat];
955 if (value < 0) { 954 if (value < 0) {
956 value = 0; 955 value = 0;
957 } else if (value == 0) { 956 } else if (value == 0) {
968 if (SDL_joylist[stick->index].map != NULL) { 967 if (SDL_joylist[stick->index].map != NULL) {
969 968
970 /* get the fake joystick that will receive the event 969 /* get the fake joystick that will receive the event
971 */ 970 */
972 hats = SDL_joylist[stick->index].map->hatmap + hat; 971 hats = SDL_joylist[stick->index].map->hatmap + hat;
973 logicaljoy = FindLogicalJoystick (stick, hats); 972 logicaljoy = FindLogicalJoystick(stick, hats);
974 } 973 }
975 974
976 if (logicaljoy) { 975 if (logicaljoy) {
977 stick = logicaljoy; 976 stick = logicaljoy;
978 hat = hats->nthing; 977 hat = hats->nthing;
979 } 978 }
980 #endif /* USE_LOGICAL_JOYSTICKS */ 979 #endif /* USE_LOGICAL_JOYSTICKS */
981 980
982 SDL_PrivateJoystickHat (stick, hat, 981 SDL_PrivateJoystickHat(stick, hat,
983 position_map[the_hat->axis[1]][the_hat-> 982 position_map[the_hat->axis[1]][the_hat->
984 axis[0]]); 983 axis[0]]);
985 } 984 }
986 } 985 }
987 986
988 static __inline__ void 987 static __inline__ void
989 HandleBall (SDL_Joystick * stick, Uint8 ball, int axis, int value) 988 HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
990 { 989 {
991 stick->hwdata->balls[ball].axis[axis] += value; 990 stick->hwdata->balls[ball].axis[axis] += value;
992 } 991 }
993 992
994 /* Function to update the state of a joystick - called as a device poll. 993 /* Function to update the state of a joystick - called as a device poll.
995 * This function shouldn't update the joystick structure directly, 994 * This function shouldn't update the joystick structure directly,
996 * but instead should call SDL_PrivateJoystick*() to deliver events 995 * but instead should call SDL_PrivateJoystick*() to deliver events
997 * and update joystick device state. 996 * and update joystick device state.
998 */ 997 */
999 static __inline__ void 998 static __inline__ void
1000 JS_HandleEvents (SDL_Joystick * joystick) 999 JS_HandleEvents(SDL_Joystick * joystick)
1001 { 1000 {
1002 struct js_event events[32]; 1001 struct js_event events[32];
1003 int i, len; 1002 int i, len;
1004 Uint8 other_axis; 1003 Uint8 other_axis;
1005 1004
1006 #ifndef NO_LOGICAL_JOYSTICKS 1005 #ifndef NO_LOGICAL_JOYSTICKS
1007 if (SDL_joylist[joystick->index].fname == NULL) { 1006 if (SDL_joylist[joystick->index].fname == NULL) {
1008 SDL_joylist_head (i, joystick->index); 1007 SDL_joylist_head(i, joystick->index);
1009 JS_HandleEvents (SDL_joylist[i].joy); 1008 JS_HandleEvents(SDL_joylist[i].joy);
1010 return; 1009 return;
1011 } 1010 }
1012 #endif 1011 #endif
1013 1012
1014 while ((len = read (joystick->hwdata->fd, events, (sizeof events))) > 0) { 1013 while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
1015 len /= sizeof (events[0]); 1014 len /= sizeof(events[0]);
1016 for (i = 0; i < len; ++i) { 1015 for (i = 0; i < len; ++i) {
1017 switch (events[i].type & ~JS_EVENT_INIT) { 1016 switch (events[i].type & ~JS_EVENT_INIT) {
1018 case JS_EVENT_AXIS: 1017 case JS_EVENT_AXIS:
1019 if (events[i].number < joystick->naxes) { 1018 if (events[i].number < joystick->naxes) {
1020 #ifndef NO_LOGICAL_JOYSTICKS 1019 #ifndef NO_LOGICAL_JOYSTICKS
1021 if (!LogicalJoystickAxis (joystick, 1020 if (!LogicalJoystickAxis(joystick,
1022 events[i].number, 1021 events[i].number,
1023 events[i].value)) 1022 events[i].value))
1024 #endif 1023 #endif
1025 SDL_PrivateJoystickAxis (joystick, 1024 SDL_PrivateJoystickAxis(joystick,
1026 events[i].number, 1025 events[i].number,
1027 events[i].value); 1026 events[i].value);
1028 break; 1027 break;
1029 } 1028 }
1030 events[i].number -= joystick->naxes; 1029 events[i].number -= joystick->naxes;
1031 other_axis = (events[i].number / 2); 1030 other_axis = (events[i].number / 2);
1032 if (other_axis < joystick->nhats) { 1031 if (other_axis < joystick->nhats) {
1033 HandleHat (joystick, other_axis, 1032 HandleHat(joystick, other_axis,
1034 events[i].number % 2, events[i].value); 1033 events[i].number % 2, events[i].value);
1035 break; 1034 break;
1036 } 1035 }
1037 events[i].number -= joystick->nhats * 2; 1036 events[i].number -= joystick->nhats * 2;
1038 other_axis = (events[i].number / 2); 1037 other_axis = (events[i].number / 2);
1039 if (other_axis < joystick->nballs) { 1038 if (other_axis < joystick->nballs) {
1040 HandleBall (joystick, other_axis, 1039 HandleBall(joystick, other_axis,
1041 events[i].number % 2, events[i].value); 1040 events[i].number % 2, events[i].value);
1042 break; 1041 break;
1043 } 1042 }
1044 break; 1043 break;
1045 case JS_EVENT_BUTTON: 1044 case JS_EVENT_BUTTON:
1046 #ifndef NO_LOGICAL_JOYSTICKS 1045 #ifndef NO_LOGICAL_JOYSTICKS
1047 if (!LogicalJoystickButton (joystick, 1046 if (!LogicalJoystickButton(joystick,
1048 events[i].number, 1047 events[i].number, events[i].value))
1049 events[i].value)) 1048 #endif
1050 #endif 1049 SDL_PrivateJoystickButton(joystick,
1051 SDL_PrivateJoystickButton (joystick, 1050 events[i].number,
1052 events[i].number, 1051 events[i].value);
1053 events[i].value);
1054 break; 1052 break;
1055 default: 1053 default:
1056 /* ?? */ 1054 /* ?? */
1057 break; 1055 break;
1058 } 1056 }
1060 } 1058 }
1061 } 1059 }
1062 1060
1063 #if SDL_INPUT_LINUXEV 1061 #if SDL_INPUT_LINUXEV
1064 static __inline__ int 1062 static __inline__ int
1065 EV_AxisCorrect (SDL_Joystick * joystick, int which, int value) 1063 EV_AxisCorrect(SDL_Joystick * joystick, int which, int value)
1066 { 1064 {
1067 struct axis_correct *correct; 1065 struct axis_correct *correct;
1068 1066
1069 correct = &joystick->hwdata->abs_correct[which]; 1067 correct = &joystick->hwdata->abs_correct[which];
1070 if (correct->used) { 1068 if (correct->used) {
1088 1086
1089 return value; 1087 return value;
1090 } 1088 }
1091 1089
1092 static __inline__ void 1090 static __inline__ void
1093 EV_HandleEvents (SDL_Joystick * joystick) 1091 EV_HandleEvents(SDL_Joystick * joystick)
1094 { 1092 {
1095 struct input_event events[32]; 1093 struct input_event events[32];
1096 int i, len; 1094 int i, len;
1097 int code; 1095 int code;
1098 1096
1099 #ifndef NO_LOGICAL_JOYSTICKS 1097 #ifndef NO_LOGICAL_JOYSTICKS
1100 if (SDL_joylist[joystick->index].fname == NULL) { 1098 if (SDL_joylist[joystick->index].fname == NULL) {
1101 SDL_joylist_head (i, joystick->index); 1099 SDL_joylist_head(i, joystick->index);
1102 return EV_HandleEvents (SDL_joylist[i].joy); 1100 return EV_HandleEvents(SDL_joylist[i].joy);
1103 } 1101 }
1104 #endif 1102 #endif
1105 1103
1106 while ((len = read (joystick->hwdata->fd, events, (sizeof events))) > 0) { 1104 while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
1107 len /= sizeof (events[0]); 1105 len /= sizeof(events[0]);
1108 for (i = 0; i < len; ++i) { 1106 for (i = 0; i < len; ++i) {
1109 code = events[i].code; 1107 code = events[i].code;
1110 switch (events[i].type) { 1108 switch (events[i].type) {
1111 case EV_KEY: 1109 case EV_KEY:
1112 if (code >= BTN_MISC) { 1110 if (code >= BTN_MISC) {
1113 code -= BTN_MISC; 1111 code -= BTN_MISC;
1114 #ifndef NO_LOGICAL_JOYSTICKS 1112 #ifndef NO_LOGICAL_JOYSTICKS
1115 if (!LogicalJoystickButton (joystick, 1113 if (!LogicalJoystickButton(joystick,
1116 joystick->hwdata-> 1114 joystick->hwdata->
1117 key_map[code], 1115 key_map[code],
1118 events[i].value)) 1116 events[i].value))
1119 #endif 1117 #endif
1120 SDL_PrivateJoystickButton (joystick, 1118 SDL_PrivateJoystickButton(joystick,
1121 joystick->hwdata-> 1119 joystick->hwdata->
1122 key_map[code], 1120 key_map[code],
1123 events[i].value); 1121 events[i].value);
1124 } 1122 }
1125 break; 1123 break;
1126 case EV_ABS: 1124 case EV_ABS:
1127 switch (code) { 1125 switch (code) {
1128 case ABS_HAT0X: 1126 case ABS_HAT0X:
1132 case ABS_HAT2X: 1130 case ABS_HAT2X:
1133 case ABS_HAT2Y: 1131 case ABS_HAT2Y:
1134 case ABS_HAT3X: 1132 case ABS_HAT3X:
1135 case ABS_HAT3Y: 1133 case ABS_HAT3Y:
1136 code -= ABS_HAT0X; 1134 code -= ABS_HAT0X;
1137 HandleHat (joystick, code / 2, code % 2, events[i].value); 1135 HandleHat(joystick, code / 2, code % 2, events[i].value);
1138 break; 1136 break;
1139 default: 1137 default:
1140 events[i].value = 1138 events[i].value =
1141 EV_AxisCorrect (joystick, code, events[i].value); 1139 EV_AxisCorrect(joystick, code, events[i].value);
1142 #ifndef NO_LOGICAL_JOYSTICKS 1140 #ifndef NO_LOGICAL_JOYSTICKS
1143 if (!LogicalJoystickAxis (joystick, 1141 if (!LogicalJoystickAxis(joystick,
1144 joystick->hwdata-> 1142 joystick->hwdata->
1145 abs_map[code], events[i].value)) 1143 abs_map[code], events[i].value))
1146 #endif 1144 #endif
1147 SDL_PrivateJoystickAxis (joystick, 1145 SDL_PrivateJoystickAxis(joystick,
1148 joystick->hwdata-> 1146 joystick->hwdata->
1149 abs_map[code], 1147 abs_map[code],
1150 events[i].value); 1148 events[i].value);
1151 break; 1149 break;
1152 } 1150 }
1153 break; 1151 break;
1154 case EV_REL: 1152 case EV_REL:
1155 switch (code) { 1153 switch (code) {
1156 case REL_X: 1154 case REL_X:
1157 case REL_Y: 1155 case REL_Y:
1158 code -= REL_X; 1156 code -= REL_X;
1159 HandleBall (joystick, code / 2, code % 2, 1157 HandleBall(joystick, code / 2, code % 2, events[i].value);
1160 events[i].value);
1161 break; 1158 break;
1162 default: 1159 default:
1163 break; 1160 break;
1164 } 1161 }
1165 break; 1162 break;
1170 } 1167 }
1171 } 1168 }
1172 #endif /* SDL_INPUT_LINUXEV */ 1169 #endif /* SDL_INPUT_LINUXEV */
1173 1170
1174 void 1171 void
1175 SDL_SYS_JoystickUpdate (SDL_Joystick * joystick) 1172 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
1176 { 1173 {
1177 int i; 1174 int i;
1178 1175
1179 #if SDL_INPUT_LINUXEV 1176 #if SDL_INPUT_LINUXEV
1180 if (joystick->hwdata->is_hid) 1177 if (joystick->hwdata->is_hid)
1181 EV_HandleEvents (joystick); 1178 EV_HandleEvents(joystick);
1182 else 1179 else
1183 #endif 1180 #endif
1184 JS_HandleEvents (joystick); 1181 JS_HandleEvents(joystick);
1185 1182
1186 /* Deliver ball motion updates */ 1183 /* Deliver ball motion updates */
1187 for (i = 0; i < joystick->nballs; ++i) { 1184 for (i = 0; i < joystick->nballs; ++i) {
1188 int xrel, yrel; 1185 int xrel, yrel;
1189 1186
1190 xrel = joystick->hwdata->balls[i].axis[0]; 1187 xrel = joystick->hwdata->balls[i].axis[0];
1191 yrel = joystick->hwdata->balls[i].axis[1]; 1188 yrel = joystick->hwdata->balls[i].axis[1];
1192 if (xrel || yrel) { 1189 if (xrel || yrel) {
1193 joystick->hwdata->balls[i].axis[0] = 0; 1190 joystick->hwdata->balls[i].axis[0] = 0;
1194 joystick->hwdata->balls[i].axis[1] = 0; 1191 joystick->hwdata->balls[i].axis[1] = 0;
1195 SDL_PrivateJoystickBall (joystick, (Uint8) i, xrel, yrel); 1192 SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
1196 } 1193 }
1197 } 1194 }
1198 } 1195 }
1199 1196
1200 /* Function to close a joystick after use */ 1197 /* Function to close a joystick after use */
1201 void 1198 void
1202 SDL_SYS_JoystickClose (SDL_Joystick * joystick) 1199 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
1203 { 1200 {
1204 #ifndef NO_LOGICAL_JOYSTICKS 1201 #ifndef NO_LOGICAL_JOYSTICKS
1205 register int i; 1202 register int i;
1206 if (SDL_joylist[joystick->index].fname == NULL) { 1203 if (SDL_joylist[joystick->index].fname == NULL) {
1207 SDL_joylist_head (i, joystick->index); 1204 SDL_joylist_head(i, joystick->index);
1208 SDL_JoystickClose (SDL_joylist[i].joy); 1205 SDL_JoystickClose(SDL_joylist[i].joy);
1209 } 1206 }
1210 #endif 1207 #endif
1211 1208
1212 if (joystick->hwdata) { 1209 if (joystick->hwdata) {
1213 #ifndef NO_LOGICAL_JOYSTICKS 1210 #ifndef NO_LOGICAL_JOYSTICKS
1214 if (SDL_joylist[joystick->index].fname != NULL) 1211 if (SDL_joylist[joystick->index].fname != NULL)
1215 #endif 1212 #endif
1216 close (joystick->hwdata->fd); 1213 close(joystick->hwdata->fd);
1217 if (joystick->hwdata->hats) { 1214 if (joystick->hwdata->hats) {
1218 SDL_free (joystick->hwdata->hats); 1215 SDL_free(joystick->hwdata->hats);
1219 } 1216 }
1220 if (joystick->hwdata->balls) { 1217 if (joystick->hwdata->balls) {
1221 SDL_free (joystick->hwdata->balls); 1218 SDL_free(joystick->hwdata->balls);
1222 } 1219 }
1223 SDL_free (joystick->hwdata); 1220 SDL_free(joystick->hwdata);
1224 joystick->hwdata = NULL; 1221 joystick->hwdata = NULL;
1225 } 1222 }
1226 } 1223 }
1227 1224
1228 /* Function to perform any system-specific joystick related cleanup */ 1225 /* Function to perform any system-specific joystick related cleanup */
1229 void 1226 void
1230 SDL_SYS_JoystickQuit (void) 1227 SDL_SYS_JoystickQuit(void)
1231 { 1228 {
1232 int i; 1229 int i;
1233 1230
1234 for (i = 0; SDL_joylist[i].fname; ++i) { 1231 for (i = 0; SDL_joylist[i].fname; ++i) {
1235 SDL_free (SDL_joylist[i].fname); 1232 SDL_free(SDL_joylist[i].fname);
1236 } 1233 }
1237 SDL_joylist[0].fname = NULL; 1234 SDL_joylist[0].fname = NULL;
1238 } 1235 }
1239 1236
1240 #endif /* SDL_JOYSTICK_LINUX */ 1237 #endif /* SDL_JOYSTICK_LINUX */