int SDL_SYS_JoystickInit(void) { char s[16]; int i, fd; SDL_numjoysticks = 0; SDL_memset(joynames, 0, sizeof(joynames)); SDL_memset(joydevnames, 0, sizeof(joydevnames)); for (i = 0; i < MAX_UHID_JOYS; i++) { SDL_Joystick nj; SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i); nj.index = SDL_numjoysticks; joynames[nj.index] = strdup(s); if (SDL_SYS_JoystickOpen(&nj) == 0) { SDL_SYS_JoystickClose(&nj); SDL_numjoysticks++; } else { SDL_free(joynames[nj.index]); joynames[nj.index] = NULL; } } for (i = 0; i < MAX_JOY_JOYS; i++) { SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i); fd = open(s, O_RDONLY); if (fd != -1) { joynames[SDL_numjoysticks++] = strdup(s); close(fd); } } /* Read the default USB HID usage table. */ hid_init(NULL); return (SDL_numjoysticks); }
/* * Open a joystick for use - the index passed as an argument refers to * the N'th joystick on the system. This index is the value which will * identify this joystick in future joystick events. * * This function returns a joystick identifier, or NULL if an error occurred. */ SDL_Joystick *SDL_JoystickOpen(int device_index) { int i; SDL_Joystick *joystick; if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { SDL_SetError("There are %d joysticks available", SDL_numjoysticks); return(NULL); } /* If the joystick is already open, return it */ for ( i=0; SDL_joysticks[i]; ++i ) { if ( device_index == SDL_joysticks[i]->index ) { joystick = SDL_joysticks[i]; ++joystick->ref_count; return(joystick); } } /* Create and initialize the joystick */ joystick = (SDL_Joystick *)malloc((sizeof *joystick)); if ( joystick != NULL ) { memset(joystick, 0, (sizeof *joystick)); joystick->index = device_index; if ( SDL_SYS_JoystickOpen(joystick) < 0 ) { free(joystick); joystick = NULL; } else { if ( joystick->naxes > 0 ) { joystick->axes = (Sint16 *)malloc (joystick->naxes*sizeof(Sint16)); } if ( joystick->nhats > 0 ) { joystick->hats = (Uint8 *)malloc (joystick->nhats*sizeof(Uint8)); } if ( joystick->nballs > 0 ) { joystick->balls = (struct balldelta *)malloc (joystick->nballs*sizeof(*joystick->balls)); } if ( joystick->nbuttons > 0 ) { joystick->buttons = (Uint8 *)malloc (joystick->nbuttons*sizeof(Uint8)); } if ( ((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); joystick = NULL; } if ( joystick->axes ) { memset(joystick->axes, 0, joystick->naxes*sizeof(Sint16)); } if ( joystick->hats ) { memset(joystick->hats, 0, joystick->nhats*sizeof(Uint8)); } if ( joystick->balls ) { memset(joystick->balls, 0, joystick->nballs*sizeof(*joystick->balls)); } if ( joystick->buttons ) { memset(joystick->buttons, 0, joystick->nbuttons*sizeof(Uint8)); } } } if ( joystick ) { /* Add joystick to list */ ++joystick->ref_count; SDL_Lock_EventThread(); for ( i=0; SDL_joysticks[i]; ++i ) /* Skip to next joystick */; SDL_joysticks[i] = joystick; SDL_Unlock_EventThread(); } return(joystick); }
/* * Open a joystick for use - the index passed as an argument refers to * the N'th joystick on the system. This index is the value which will * identify this joystick in future joystick events. * * This function returns a joystick identifier, or NULL if an error occurred. */ SDL_Joystick * SDL_JoystickOpen(int device_index) { SDL_Joystick *joystick; SDL_Joystick *joysticklist; const char *joystickname = NULL; if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) { SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); return (NULL); } joysticklist = SDL_joysticks; /* If the joystick is already open, return it * it is important that we have a single joystick * for each instance id */ while (joysticklist) { if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) { joystick = joysticklist; ++joystick->ref_count; return (joystick); } joysticklist = joysticklist->next; } /* Create and initialize the joystick */ joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick)); if (joystick == NULL) { SDL_OutOfMemory(); return NULL; } SDL_memset(joystick, 0, (sizeof *joystick)); if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) { SDL_free(joystick); return NULL; } joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index); if (joystickname) joystick->name = SDL_strdup(joystickname); else joystick->name = NULL; if (joystick->naxes > 0) { joystick->axes = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16)); joystick->axes_zero = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16)); } if (joystick->nhats > 0) { joystick->hats = (Uint8 *) SDL_malloc (joystick->nhats * sizeof(Uint8)); } if (joystick->nballs > 0) { joystick->balls = (struct balldelta *) SDL_malloc (joystick->nballs * sizeof(*joystick->balls)); } if (joystick->nbuttons > 0) { joystick->buttons = (Uint8 *) SDL_malloc (joystick->nbuttons * sizeof(Uint8)); } if (((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); return NULL; } if (joystick->axes) { SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16)); SDL_memset(joystick->axes_zero, 0, joystick->naxes * sizeof(Sint16)); } if (joystick->hats) { SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8)); } if (joystick->balls) { SDL_memset(joystick->balls, 0, joystick->nballs * sizeof(*joystick->balls)); } if (joystick->buttons) { SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8)); } joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN; /* Add joystick to list */ ++joystick->ref_count; /* Link the joystick in the list */ joystick->next = SDL_joysticks; SDL_joysticks = joystick; SDL_SYS_JoystickUpdate(joystick); return (joystick); }
/* * Open a joystick for use - the index passed as an argument refers to * the N'th joystick on the system. This index is the value which will * identify this joystick in future joystick events. * * This function returns a joystick identifier, or NULL if an error occurred. */ SDL_Joystick * SDL_JoystickOpen(int device_index) { SDL_Joystick *joystick; SDL_Joystick *joysticklist; const char *joystickname = NULL; if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) { SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); return (NULL); } SDL_LockJoystickList(); joysticklist = SDL_joysticks; /* If the joystick is already open, return it * it is important that we have a single joystick * for each instance id */ while (joysticklist) { if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) { joystick = joysticklist; ++joystick->ref_count; SDL_UnlockJoystickList(); return (joystick); } joysticklist = joysticklist->next; } /* Create and initialize the joystick */ joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1); if (joystick == NULL) { SDL_OutOfMemory(); SDL_UnlockJoystickList(); return NULL; } if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) { SDL_free(joystick); SDL_UnlockJoystickList(); return NULL; } joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index); if (joystickname) joystick->name = SDL_strdup(joystickname); else joystick->name = NULL; if (joystick->naxes > 0) { joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo)); } if (joystick->nhats > 0) { joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8)); } if (joystick->nballs > 0) { joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls)); } if (joystick->nbuttons > 0) { joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8)); } if (((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); SDL_UnlockJoystickList(); return NULL; } joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN; /* If this joystick is known to have all zero centered axes, skip the auto-centering code */ if (SDL_JoystickAxesCenteredAtZero(joystick)) { int i; for (i = 0; i < joystick->naxes; ++i) { joystick->axes[i].has_initial_value = SDL_TRUE; } } joystick->is_game_controller = SDL_IsGameController(device_index); /* Add joystick to list */ ++joystick->ref_count; /* Link the joystick in the list */ joystick->next = SDL_joysticks; SDL_joysticks = joystick; SDL_UnlockJoystickList(); SDL_SYS_JoystickUpdate(joystick); return (joystick); }
SDL_Joystick *SDL_JoystickOpen(int device_index) { int i; SDL_Joystick *joystick; if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { SDL_SetError("There are %d joysticks available", SDL_numjoysticks); return(NULL); } for ( i=0; SDL_joysticks[i]; ++i ) { if ( device_index == SDL_joysticks[i]->index ) { joystick = SDL_joysticks[i]; ++joystick->ref_count; return(joystick); } } joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick)); if ( !joystick ) { SDL_OutOfMemory(); return(NULL); } SDL_memset(joystick, 0, (sizeof *joystick)); joystick->index = device_index; if ( SDL_SYS_JoystickOpen(joystick) < 0 ) { SDL_free(joystick); return(NULL); } if ( joystick->naxes > 0 ) { joystick->axes = (Sint16 *)SDL_malloc (joystick->naxes*sizeof(Sint16)); } if ( joystick->nhats > 0 ) { joystick->hats = (Uint8 *)SDL_malloc (joystick->nhats*sizeof(Uint8)); } if ( joystick->nballs > 0 ) { joystick->balls = (struct balldelta *)SDL_malloc (joystick->nballs*sizeof(*joystick->balls)); } if ( joystick->nbuttons > 0 ) { joystick->buttons = (Uint8 *)SDL_malloc (joystick->nbuttons*sizeof(Uint8)); } if ( ((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); return(NULL); } if ( joystick->axes ) { SDL_memset(joystick->axes, 0, joystick->naxes*sizeof(Sint16)); } if ( joystick->hats ) { SDL_memset(joystick->hats, 0, joystick->nhats*sizeof(Uint8)); } if ( joystick->balls ) { SDL_memset(joystick->balls, 0, joystick->nballs*sizeof(*joystick->balls)); } if ( joystick->buttons ) { SDL_memset(joystick->buttons, 0, joystick->nbuttons*sizeof(Uint8)); } ++joystick->ref_count; SDL_Lock_EventThread(); for ( i=0; SDL_joysticks[i]; ++i ) ; SDL_joysticks[i] = joystick; SDL_Unlock_EventThread(); return(joystick); }