comparison src/joystick/linux/SDL_sysjoystick.c @ 3404:c9dcc73f6a36

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:35 +0000
parents 11c079bb52a8
children 493f252a455c
comparison
equal deleted inserted replaced
3403:5f812979e179 3404:c9dcc73f6a36
364 364
365 #endif /* USE_LOGICAL_JOYSTICKS */ 365 #endif /* USE_LOGICAL_JOYSTICKS */
366 366
367 #if SDL_INPUT_LINUXEV 367 #if SDL_INPUT_LINUXEV
368 #define test_bit(nr, addr) \ 368 #define test_bit(nr, addr) \
369 (((1UL << ((nr) & 31)) & (((const Uint32 *) addr)[(nr) >> 5])) != 0) 369 (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
370 #define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
370 371
371 static int 372 static int
372 EV_IsJoystick(int fd) 373 EV_IsJoystick(int fd)
373 { 374 {
374 unsigned long evbit[40]; 375 unsigned long evbit[NBITS(EV_MAX)] = { 0 };
375 unsigned long keybit[40]; 376 unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
376 unsigned long absbit[40]; 377 unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
377 378
378 if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) || 379 if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
379 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) || 380 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
380 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) { 381 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
381 return (0); 382 return (0);
662 663
663 static SDL_bool 664 static SDL_bool
664 EV_ConfigJoystick(SDL_Joystick * joystick, int fd) 665 EV_ConfigJoystick(SDL_Joystick * joystick, int fd)
665 { 666 {
666 int i, t; 667 int i, t;
667 unsigned long keybit[40]; 668 unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
668 unsigned long absbit[40]; 669 unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
669 unsigned long relbit[40]; 670 unsigned long relbit[NBITS(REL_MAX)] = { 0 };
670 671
671 /* See if this device uses the new unified event API */ 672 /* See if this device uses the new unified event API */
672 if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) && 673 if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
673 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) && 674 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
674 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) { 675 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {