SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) { SDL_JoystickGUID guid; /* the GUID is just the first 16 chars of the name for now */ const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index ); SDL_zero( guid ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); return guid; }
/* * Get the implementation dependent name of a joystick */ const char * SDL_JoystickNameForIndex(int device_index) { if (device_index < 0 || device_index >= SDL_NumJoysticks()) { SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); return (NULL); } return (SDL_SYS_JoystickNameForDeviceIndex(device_index)); }
static int CountLogicalJoysticks(int max) { register int i, j, k, ret, prev; const char *name; int nbuttons, fd; unsigned char n; ret = 0; for (i = 0; i < max; i++) { name = SDL_SYS_JoystickNameForDeviceIndex(i); fd = open(SDL_joylist[i].fname, O_RDONLY, 0); if (fd >= 0) { if (ioctl(fd, JSIOCGBUTTONS, &n) < 0) { nbuttons = -1; } else { nbuttons = n; } close(fd); } else { nbuttons = -1; } if (name) { for (j = 0; j < SDL_arraysize(joystick_logicalmap); j++) { if (!SDL_strcmp(name, joystick_logicalmap[j].name) && (nbuttons == -1 || nbuttons == joystick_logicalmap[j].nbuttons)) { prev = i; SDL_joylist[prev].map = &(joystick_logicalmap[j]); for (k = 1; k < joystick_logicalmap[j].njoys; k++) { SDL_joylist[prev].next = max + ret; SDL_joylist[max + ret].prev = prev; prev = max + ret; SDL_joylist[prev].logicalno = k; SDL_joylist[prev].map = &(joystick_logicalmap[j]); ret++; } break; } } } } return ret; }
/* * 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); }
/* * 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); }
static SDL_bool JS_ConfigJoystick(SDL_Joystick * joystick, int fd) { SDL_bool handled; unsigned char n; int tmp_naxes, tmp_nhats, tmp_nballs; const char *name; char *env, env_name[128]; int i; handled = SDL_FALSE; /* Default joystick device settings */ if (ioctl(fd, JSIOCGAXES, &n) < 0) { joystick->naxes = 2; } else { joystick->naxes = n; } if (ioctl(fd, JSIOCGBUTTONS, &n) < 0) { joystick->nbuttons = 2; } else { joystick->nbuttons = n; } name = SDL_SYS_JoystickNameForDeviceIndex(joystick->instance_id); /* Generic analog joystick support */ if (SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat")) { if (SDL_sscanf(name, "Analog %d-axis %*d-button %d-hat", &tmp_naxes, &tmp_nhats) == 2) { joystick->naxes = tmp_naxes; joystick->nhats = tmp_nhats; handled = SDL_TRUE; } } /* Special joystick support */ for (i = 0; i < SDL_arraysize(special_joysticks); ++i) { if (SDL_strcmp(name, special_joysticks[i].name) == 0) { joystick->naxes = special_joysticks[i].naxes; joystick->nhats = special_joysticks[i].nhats; joystick->nballs = special_joysticks[i].nballs; handled = SDL_TRUE; break; } } /* User environment joystick support */ if ((env = SDL_getenv("SDL_LINUX_JOYSTICK"))) { *env_name = '\0'; if (*env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1) env += SDL_strlen(env_name) + 2; else if (SDL_sscanf(env, "%s", env_name) == 1) env += SDL_strlen(env_name); if (SDL_strcmp(name, env_name) == 0) { if (SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats, &tmp_nballs) == 3) { joystick->naxes = tmp_naxes; joystick->nhats = tmp_nhats; joystick->nballs = tmp_nballs; handled = SDL_TRUE; } } } /* Remap hats and balls */ if (handled) { if (joystick->nhats > 0) { if (allocate_hatdata(joystick) < 0) { joystick->nhats = 0; } } if (joystick->nballs > 0) { if (allocate_balldata(joystick) < 0) { joystick->nballs = 0; } } } return (handled); }