void check_quit_action() { // this is where we loop and check for joystick quit event (buttons 8 and 9); int i, status; const char *device = "/dev/input/js0"; const char *calib = "/home/spike/.joystick.bak"; js_data_struct jsd; signal(SIGINT, MySignalHandler); status = JSInit(&jsd, device, calib, JSFlagNonBlocking); if (status != JSSuccess) { printf("ERROR with joystick!\n"); JSClose(&jsd); exit(EXIT_FAILURE); } printf("Initialized joystick: %s\n", jsd.name); runlevel = 2; while(runlevel >= 2) { if (JSUpdate(&jsd) == JSGotEvent && (JSGetButtonState(&jsd, 8) && JSGetButtonState(&jsd, 9))) { JSClose(&jsd); return; } usleep(16000); } }
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; }
VistaJswJoystickDriver::~VistaJswJoystickDriver() { if(m_bOpened) JSClose(&m_jsd); VistaDeviceSensor *pSensor = GetSensorByIndex(0); GetFactory()->GetTranscoderFactoryForSensor("")->DestroyTranscoder( pSensor->GetMeasureTranscode() ); RemDeviceSensor(pSensor); delete pSensor; UnregisterAspect( m_pWorkspace, false ); delete m_pWorkspace; UnregisterAspect( m_pProtocol, false ); delete m_pProtocol; UnregisterAspect( m_pInfo, false ); delete m_pInfo; }
/* * 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); }
void ui_joystick_end( void ) { int i; for( i = 0; i < joysticks_supported; i++ ) JSClose( &jsd[i] ); }
/*============================================================================*/ bool VistaJswJoystickDriver::Connect() { int status = 0; const char *device = JSDefaultDevice; const char *calib = JSDefaultCalibration; IVistaDriverProtocolAspect::_cVersionTag tag; if( m_pProtocol->GetProtocol(tag) ) { if(!tag.m_strProtocolName.empty()) device = tag.m_strProtocolName.c_str(); if(!tag.m_strProtocolRevision.empty()) calib = tag.m_strProtocolRevision.c_str(); } status = JSInit( &m_jsd, device, calib, JSFlagNonBlocking ); if(status != JSSuccess) { JSClose(&m_jsd); return false; } VistaPropertyList &oWrite = m_pInfo->GetInfoPropsWrite(); oWrite.SetValue("NAME", (m_jsd.name ? std::string(m_jsd.name) : "<none>")); oWrite.SetValue("DEVICENAME", (m_jsd.device_name ? std::string(m_jsd.device_name) : "<none>")); oWrite.SetValue("CALIBRATIONFILE", (m_jsd.calibration_file ? std::string(m_jsd.calibration_file) : "<none>")); for(unsigned int n=0; n < 16; ++n) { if(JSIsAxisAllocated(&m_jsd, n)) { // ok, we have that axis js_axis_struct *axis_ptr = m_jsd.axis[n]; float nMin[3], nMax[3]; nMin[0] = nMin[1] = nMin[2] = 0.0f; nMax[0] = nMax[1] = nMax[2] = 0.0f; nMin[0] = axis_ptr->min; nMax[0] = axis_ptr->max; m_pWorkspace->SetWorkspace("AXIS_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->dz_min; nMax[0] = axis_ptr->dz_max; m_pWorkspace->SetWorkspace("DEADZONE_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->corr_coeff_min1; nMax[0] = axis_ptr->corr_coeff_max1; m_pWorkspace->SetWorkspace("CORR_COEFF1_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->corr_coeff_min2; nMax[0] = axis_ptr->corr_coeff_max2; m_pWorkspace->SetWorkspace("CORR_COEFF2_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); } } m_bOpened = true; return true; }