/* Some heuristics to see if the device is a keyboard. */ static bool is_keyboard(int fd) { int i; unsigned long evbits[NLONGS(EV_CNT)] = { 0 }; unsigned long keybits[NLONGS(KEY_CNT)] = { 0 }; errno = 0; ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits); if (errno) return false; if (!evdev_bit_is_set(evbits, EV_KEY)) return false; errno = 0; ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits); if (errno) return false; for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++) if (evdev_bit_is_set(keybits, i)) return true; return false; }
static bool fill_joystick_buttons(ALLEGRO_JOYSTICK_LINUX *joy, int fd) { unsigned long key_bits[NLONGS(KEY_CNT)] = {0}; int b; int i; if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits) < 0) return false; b = 0; for (i = LJOY_BTN_RANGE_START; i < LJOY_BTN_RANGE_END; i++) { if (TEST_BIT(i, key_bits) && is_joystick_button(i)) { joy->button_mapping[b].ev_code = i; ALLEGRO_DEBUG("Input event code %d maps to button %d\n", i, b); joy->parent.info.button[b].name = al_malloc(32); snprintf((char *)joy->parent.info.button[b].name, 32, "B%d", b+1); b++; if (b == _AL_MAX_JOYSTICK_BUTTONS) break; } } joy->parent.info.num_buttons = b; /* Clear the rest. */ for (; b < _AL_MAX_JOYSTICK_BUTTONS; b++) { joy->button_mapping[b].ev_code = -1; } return true; }
/* Check if the device has at least one joystick-related absolute axis. * Some devices, like (wireless) keyboards, may actually have absolute axes * such as a volume axis for the volume up/down buttons. * Also, some devices like a PS3 controller report many unusual axes, probably * used in nonstandard ways by the PS3 console. All such axes are ignored by * this function and by this driver. */ static bool have_joystick_axis(int fd) { unsigned long abs_bits[NLONGS(ABS_CNT)] = {0}; int i; if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits) < 0) return false; for (i = LJOY_AXIS_RANGE_START; i < LJOY_AXIS_RANGE_END; i++) { if (TEST_BIT(i, abs_bits)) return true; } return false; }
/* Check if the device has at least once joystick-related button. */ static bool have_joystick_button(int fd) { unsigned long key_bits[NLONGS(KEY_CNT)] = {0}; int i; if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits) < 0) return false; for (i = LJOY_BTN_RANGE_START; i < LJOY_BTN_RANGE_END; i++) { if (TEST_BIT(i, key_bits) && is_joystick_button(i)) return true; } return false; }
qboolean evdev_ismouse( int fd ) { long event_bits[NLONGS(EV_CNT)]; memset( event_bits, 0, sizeof( event_bits ) ); if( ioctl( fd, EVIOCGBIT(0, EV_MAX), event_bits) < 0 ) return qfalse; // jag vet inte //show_bit( EV_REL, event_bits ); //show_bit( EV_KEY, event_bits ); //show_bit( EV_ABS, event_bits ); if( !test_bit( EV_REL, event_bits ) ) return qfalse; if( !test_bit( EV_KEY, event_bits ) ) return qfalse; return qtrue; }
static bool fill_joystick_axes(ALLEGRO_JOYSTICK_LINUX *joy, int fd) { unsigned long abs_bits[NLONGS(ABS_CNT)] = {0}; int stick; int axis; int name_sticks; int name_throttles; int res, i; /* Scan the axes to get their properties. */ res = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits); if (res < 0) return false; stick = 0; axis = 0; name_sticks = 0; name_throttles = 0; for (i = LJOY_AXIS_RANGE_START; i < LJOY_AXIS_RANGE_END; i++) { struct input_absinfo absinfo; if (!TEST_BIT(i, abs_bits)) continue; if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) continue; if (is_single_axis_throttle(i)) { /* One axis throttle. */ name_throttles++; joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_ANALOGUE; joy->parent.info.stick[stick].num_axes = 1; joy->parent.info.stick[stick].axis[0].name = "X"; joy->parent.info.stick[stick].name = al_malloc(32); snprintf((char *)joy->parent.info.stick[stick].name, 32, "Throttle %d", name_throttles); set_axis_mapping(&joy->axis_mapping[i], stick, 0, &absinfo); stick++; } else { /* Regular axis, two axis stick. */ if (axis == 0) { /* First axis of new joystick. */ name_sticks++; if (is_hat_axis(i)) { joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_DIGITAL; } else { joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_ANALOGUE; } joy->parent.info.stick[stick].num_axes = 2; joy->parent.info.stick[stick].axis[0].name = "X"; joy->parent.info.stick[stick].axis[1].name = "Y"; joy->parent.info.stick[stick].name = al_malloc(32); snprintf((char *)joy->parent.info.stick[stick].name, 32, "Stick %d", name_sticks); set_axis_mapping(&joy->axis_mapping[i], stick, axis, &absinfo); axis++; } else { /* Second axis. */ ASSERT(axis == 1); set_axis_mapping(&joy->axis_mapping[i], stick, axis, &absinfo); stick++; axis = 0; } } } joy->parent.info.num_sticks = stick; return true; }