/* Function to open a joystick for use. The joystick to open is specified by the index field of the joystick. This should fill the nbuttons and naxes fields of the joystick structure. It returns 0, or -1 if there is an error. */ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) { int fd; SDL_logical_joydecl(int realindex); SDL_logical_joydecl(SDL_Joystick *realjoy = NULL); /* Open the joystick and set the joystick file descriptor */ #ifndef NO_LOGICAL_JOYSTICKS if (SDL_joylist[joystick->index].fname == NULL) { SDL_joylist_head(realindex, joystick->index); realjoy = SDL_JoystickOpen(realindex); if (realjoy == NULL) return(-1); fd = realjoy->hwdata->fd; } else { fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0); } SDL_joylist[joystick->index].joy = joystick; #else fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0); #endif if ( fd < 0 ) { SDL_SetError("Unable to open %s\n", SDL_joylist[joystick->index]); return(-1); } joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); if ( joystick->hwdata == NULL ) { SDL_OutOfMemory(); close(fd); return(-1); } SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); joystick->hwdata->fd = fd; /* Set the joystick to non-blocking read mode */ fcntl(fd, F_SETFL, O_NONBLOCK); /* Get the number of buttons and axes on the joystick */ #ifndef NO_LOGICAL_JOYSTICKS if (realjoy) ConfigLogicalJoystick(joystick); else #endif #if SDL_INPUT_LINUXEV if ( ! EV_ConfigJoystick(joystick, fd) ) #endif JS_ConfigJoystick(joystick, fd); return(0); }
/* Function to open a joystick for use. The joystick to open is specified by the index field of the joystick. This should fill the nbuttons and naxes fields of the joystick structure. It returns 0, or -1 if there is an error. */ int STJoystick::Sys_OpenJoystick(int device_index) { int fd; SDL_logical_joydecl(int realindex); SDL_logical_joydecl(STJoystick::STJoystickData* realjoy = NULL); /* Open the joystick and set the joystick file descriptor */ #ifndef NO_LOGICAL_JOYSTICKS if (SDL_joylist[device_index].fname == NULL) { SDL_joylist_head(realindex, device_index); int opened_success = Sys_OpenJoystick(realindex); if (opened_success == -1) return(-1); realjoy = joysticks[device_index]; fd = realjoy->hwdata->fd; } else { fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0); } SDL_joylist[device_index].joy = joysticks[device_index]; #else fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0); #endif if ( fd < 0 ) { Error("Unable to open device in Sys_OpenJoystick()\n"); return(-1); } joysticks[device_index]->hwdata = new joystick_hwdata; if ( joysticks[device_index]->hwdata == NULL ) { MemoryError(); close(fd); return(-1); } memset(joysticks[device_index]->hwdata, 0, sizeof(*joysticks[device_index]->hwdata)); joysticks[device_index]->hwdata->fd = fd; /* Set the joystick to non-blocking read mode */ fcntl(fd, F_SETFL, O_NONBLOCK); /* Get the number of buttons and axes on the joystick */ #ifndef NO_LOGICAL_JOYSTICKS if (realjoy) ConfigLogicalJoystick(joysticks[device_index]); else #endif #if SDL_INPUT_LINUXEV if ( ! EV_ConfigJoystick(joysticks[device_index], fd) ) #endif JS_ConfigJoystick(joysticks[device_index], fd); return(0); }
static __inline__ void HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value) { struct hwdata_hat *the_hat; const Uint8 position_map[3][3] = { { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP }, { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT }, { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN } }; SDL_logical_joydecl(SDL_Joystick *logicaljoy = NULL); SDL_logical_joydecl(struct joystick_logical_mapping* hats = NULL); if (stick->nhats <= hat) { return; /* whoops, that shouldn't happen! */ } the_hat = &stick->hwdata->hats[hat]; if ( value < 0 ) { value = 0; } else if ( value == 0 ) { value = 1; } else if ( value > 0 ) { value = 2; } if ( value != the_hat->axis[axis] ) { the_hat->axis[axis] = value; #ifndef NO_LOGICAL_JOYSTICKS /* if there's no map then this is just a regular joystick */ if (SDL_joylist[stick->index].map != NULL) { /* get the fake joystick that will receive the event */ hats = SDL_joylist[stick->index].map->hatmap+hat; logicaljoy = FindLogicalJoystick(stick, hats); } if (logicaljoy) { stick = logicaljoy; hat = hats->nthing; } #endif /* USE_LOGICAL_JOYSTICKS */ SDL_PrivateJoystickHat(stick, hat, position_map[the_hat->axis[1]][the_hat->axis[0]]); } }
static __inline__ void HandleHat(STJoystick::STJoystickData* stick, unsigned char hat, int axis, int value) { hwdata_hat *the_hat; const unsigned char position_map[3][3] = { { ST_JOYSTICK_HAT_LEFTUP, ST_JOYSTICK_HAT_UP, ST_JOYSTICK_HAT_RIGHTUP }, { ST_JOYSTICK_HAT_LEFT, ST_JOYSTICK_HAT_CENTERED, ST_JOYSTICK_HAT_RIGHT }, { ST_JOYSTICK_HAT_LEFTDOWN, ST_JOYSTICK_HAT_DOWN, ST_JOYSTICK_HAT_RIGHTDOWN } }; SDL_logical_joydecl(STJoystick::STJoystickData *logicaljoy = NULL); SDL_logical_joydecl(struct joystick_logical_mapping* hats = NULL); the_hat = &stick->hwdata->hats[hat]; if ( value < 0 ) { value = 0; } else if ( value == 0 ) { value = 1; } else if ( value > 0 ) { value = 2; } if ( value != the_hat->axis[axis] ) { the_hat->axis[axis] = value; #ifndef NO_LOGICAL_JOYSTICKS /* if there's no map then this is just a regular joystick */ if (SDL_joylist[stick->index].map != NULL) { /* get the fake joystick that will receive the event */ hats = SDL_joylist[stick->index].map->hatmap+hat; logicaljoy = FindLogicalJoystick(stick, hats); } if (logicaljoy) { stick = logicaljoy; hat = hats->nthing; } #endif /* USE_LOGICAL_JOYSTICKS */ stick->hats[hat] = position_map[the_hat->axis[1]][the_hat->axis[0]]; } }