Esempio n. 1
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 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);
}