comparison src/joystick/linux/SDL_sysjoystick.c @ 4352:c8bf421431f9 SDL-1.2

Fixed bug #853 Ludwig Nussel 2009-10-18 05:34:18 PDT src/joystick/linux/SDL_sysjoystick.c has some problems: - test_bit() might break with strict aliasing - test_bit() assumes array is Uint32 but its actually "unsigned long" on 64bit systems sizeof(long) != sizeof(Uint32). - the keybit array is too small - the arrays are unitialized so the number of detected buttons is quite random
author Sam Lantinga <slouken@libsdl.org>
date Sun, 18 Oct 2009 16:14:57 +0000
parents 0530394b5830
children 42012a6afb5b
comparison
equal deleted inserted replaced
4351:3ae3624c3cbc 4352:c8bf421431f9
372 372
373 #endif /* USE_LOGICAL_JOYSTICKS */ 373 #endif /* USE_LOGICAL_JOYSTICKS */
374 374
375 #if SDL_INPUT_LINUXEV 375 #if SDL_INPUT_LINUXEV
376 #define test_bit(nr, addr) \ 376 #define test_bit(nr, addr) \
377 (((1UL << ((nr) & 31)) & (((const Uint32 *) addr)[(nr) >> 5])) != 0) 377 (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
378 #define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
378 379
379 static int EV_IsJoystick(int fd) 380 static int EV_IsJoystick(int fd)
380 { 381 {
381 unsigned long evbit[40]; 382 unsigned long evbit[NBITS(EV_MAX)] = { 0 };
382 unsigned long keybit[40]; 383 unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
383 unsigned long absbit[40]; 384 unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
384 385
385 if ( (ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) || 386 if ( (ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
386 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) || 387 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
387 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0) ) { 388 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0) ) {
388 return(0); 389 return(0);
659 #if SDL_INPUT_LINUXEV 660 #if SDL_INPUT_LINUXEV
660 661
661 static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd) 662 static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd)
662 { 663 {
663 int i, t; 664 int i, t;
664 unsigned long keybit[40]; 665 unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
665 unsigned long absbit[40]; 666 unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
666 unsigned long relbit[40]; 667 unsigned long relbit[NBITS(REL_MAX)] = { 0 };
667 668
668 /* See if this device uses the new unified event API */ 669 /* See if this device uses the new unified event API */
669 if ( (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) && 670 if ( (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
670 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) && 671 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
671 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) ) { 672 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) ) {