示例#1
0
static int
init_stick( int which, const char *const device,
	    const char *const calibration )
{
  switch( JSInit( &jsd[which], device, calibration, JSFlagNonBlocking ) ) {

  case JSSuccess:
    if( JSLoadCalibrationUNIX( &jsd[which] ) && errno != ENOENT ) {
      ui_error( UI_ERROR_ERROR,
		"failed to read calibration for joystick %i: %s", which + 1,
		strerror( errno ) );
      break;
    }

    if( jsd[which].total_axises < 2 || jsd[which].total_buttons < 1 )
    {
      ui_error( UI_ERROR_ERROR, "sorry, joystick %i (%s) is inadequate!",
		which + 1, device );
      break;
    }
    return 0;

  case JSBadValue:
    ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i: %s",
	      which + 1, "invalid parameter/value");
    break;

  case JSNoAccess:

    /* FIXME: why is this commented out? */
/*
    ui_error (UI_ERROR_ERROR,
	      "failed to initialise joystick %i: %s",
	      which + 1, "cannot access device");
*/
    break;

  case JSNoBuffers:
    ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i: %s",
	      which + 1, "not enough memory" );
    break;

  default:
    ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i", which + 1 );
    break;

  }

  JSClose( &jsd[which] );

  return 1;
}
示例#2
0
文件: main.c 项目: herrsergio/libjsw
/*
 *      Initializes the joystick and stores the new initialized values
 *      into the jsd structure.
 *
 *      If the device is not specified (set to NULL), then it will
 *      be defauled to JSDefaultDevice.
 *
 *      If the calibration file is not specified (set to NULL), then
 *      it will be defaulted to JSDefaultCalibration. The HOME
 *      enviroment value will be used as the prefix to the path of
 *      JSDefaultCalibration. The calibration file does not have to
 *      exist.
 *
 *	Available flags are:
 *
 *	JSFlagNonBlocking		Open in non-blocking mode.
 *	JSFlagForceFeedback		Open in read/write mode.
 */
int JSInit(
	js_data_struct *jsd,
	const char *device,
	const char *calibration,
	unsigned int flags
)
{
	int i;
	js_axis_struct *axis;
	js_button_struct *button;

#if defined(__linux__) || defined(__FreeBSD__)
	unsigned char axes = 2;
	unsigned char buttons = 2;
	int version = 0x000800;

# ifndef LINUX_JS_NAME_MAX
#  define LINUX_JS_NAME_MAX	128
# endif

	char name[LINUX_JS_NAME_MAX] = "Unknown";

#endif	/* __linux__ || __FreeBSD__ */


	if(jsd == NULL)
	    return(JSBadValue);

	/* Reset values */
	jsd->name = NULL;

	jsd->axis = NULL;
	jsd->total_axises = 0;

	jsd->button = NULL;
	jsd->total_buttons = 0;

	jsd->device_name = NULL;
	jsd->calibration_file = NULL;

	jsd->events_received = 0;
	jsd->events_sent = 0;

	jsd->fd = -1;
	jsd->flags = 0;
	jsd->driver_version = 0;
	jsd->last_calibrated = 0;
	jsd->force_feedback = NULL;


	/* Set default device name as needed */
	if(device == NULL)
	    device = JSDefaultDevice;

	/* Set default calibration file name as needed */
	if(calibration == NULL)
	{
	    const char *home = getenv("HOME");
	    calibration = PrefixPaths(
		(home != NULL) ? home : "/",
		JSDefaultCalibration
	    );
	    if(calibration == NULL)
		calibration = JSDefaultCalibration;
	}

	/* From this point on device and calibration are not NULL, so
	 * record device and calibration file names on the jsd
	 */
	jsd->device_name = STRDUP(device);
	jsd->calibration_file = STRDUP(calibration);


#if defined(__linux__) || defined(__FreeBSD__)
	/* Open joystick */
	jsd->fd = open(jsd->device_name, O_RDONLY);
	if(jsd->fd < 0)
	{
	    JSClose(jsd);
	    return(JSNoAccess);
	}
#endif

#if defined(__linux__)
	/* Fetch device values */
	/* Raw version string */
	ioctl(jsd->fd, JSIOCGVERSION, &version);
	jsd->driver_version = (unsigned int)version;

	/* Total number of axises */
	ioctl(jsd->fd, JSIOCGAXES, &axes);	/* Total axises */
	jsd->total_axises = axes;

	/* Total number of buttons */
	ioctl(jsd->fd, JSIOCGBUTTONS, &buttons);	/* Total buttons */
	jsd->total_buttons = buttons;

	/* Device descriptive name */
	ioctl(jsd->fd, JSIOCGNAME(LINUX_JS_NAME_MAX), name);
	jsd->name = STRDUP(name);
#elif defined(__FreeBSD__)
	jsd->driver_version = version = 1;
	jsd->total_axises = axes = 2;
	jsd->total_buttons = buttons = 2;
	strlcpy(name, "FreeBSD-Gameport", LINUX_JS_NAME_MAX);
	jsd->name = STRDUP(name);
#endif
	/* Allocate axises */
	if(jsd->total_axises > 0)
	{
	    jsd->axis = (js_axis_struct **)calloc(
	        jsd->total_axises,
	        sizeof(js_axis_struct *)
	    );
	    if(jsd->axis == NULL)
	    {
	        jsd->total_axises = 0;
	        JSClose(jsd);
	        return(JSNoBuffers);
	    }
	}
	for(i = 0; i < jsd->total_axises; i++)
	{
	    jsd->axis[i] = axis = (js_axis_struct *)calloc(
		1, sizeof(js_axis_struct)
	    );
	    if(axis == NULL)
	    {
		JSClose(jsd);
		return(JSNoBuffers);
	    }

	    /* Reset axis values */
	    axis->cur = JSDefaultCenter;
	    axis->min = JSDefaultMin;
	    axis->max = JSDefaultMax;
	    axis->cen = JSDefaultCenter;
	    axis->nz = JSDefaultNullZone;
	    axis->tolorance = JSDefaultTolorance;
	    axis->flags = 0;
	}

	/* Allocate buttons */  
	if(jsd->total_buttons > 0)
	{
	    jsd->button = (js_button_struct **)calloc(
		jsd->total_buttons,
		sizeof(js_button_struct *)
	    );
	    if(jsd->button == NULL)   
	    {
		jsd->total_buttons = 0;
		JSClose(jsd);   
		return(JSNoBuffers);
	    }
	}
	for(i = 0; i < jsd->total_buttons; i++)
	{
	    jsd->button[i] = button = (js_button_struct *)calloc(
		1, sizeof(js_button_struct)
	    );
	    if(button == NULL)
	    {
		JSClose(jsd);
		return(JSNoBuffers);
	    }

	    /* Reset button values */
	    button->state = JSButtonStateOff;
	}

	/* Set to non-blocking? */
	if(flags & JSFlagNonBlocking)
	{
	    fcntl(jsd->fd, F_SETFL, O_NONBLOCK);
	    jsd->flags |= JSFlagNonBlocking;
 	}

	/* Mark successful initialization */
	jsd->flags |= JSFlagIsInit;

	/* Load calibration from calibration file */
	JSLoadCalibrationUNIX(jsd);

	/* Set axis tolorance for error correction */
	JSResetAllAxisTolorance(jsd);


	return(JSSuccess);
}