Exemple #1
0
char *ljoy_GetName(int id)
{
	char *name = malloc(128);
	if(ioctl(id,JSIOCGNAME(128),name) < 0)
		strcpy(name,"Unknown");
	return(name);
}
Exemple #2
0
vrpn_Joylin::vrpn_Joylin(char * name, 
			 vrpn_Connection * c,
			 char *portname):
  vrpn_Analog(name, c), vrpn_Button_Filter(name, c)
{ 
  namelen = 128;
  num_channel = 2;     // inherited : default for generic me-know-nothing PC joystick
  num_buttons = 2;      // inherited : this value is corrected by the ioctl call below.
  fd = -1;
  version = 0x000800;
  name = new char[namelen];

  strncpy(name, "Unknown", namelen); // paranoia for future changes of namelen
  name[namelen-1] = 0;
  
  if ((fd = open(portname, O_RDONLY)) < 0) {  /* FIX LATER */
    fprintf(stderr, "vrpn_Joylin constructor could not open %s", portname);
    perror(" joystick device");
    exit(1);
  }

  ioctl(fd, JSIOCGVERSION, &version);
  ioctl(fd, JSIOCGAXES, &num_channel);
  ioctl(fd, JSIOCGBUTTONS, &num_buttons);
  ioctl(fd, JSIOCGNAME(namelen), name);
  
  fprintf(stderr, "Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",
	  name, num_channel, num_buttons, version >> 16, (version >> 8) & 0xff, version & 0xff);
}
Exemple #3
0
static bool isJoyName(ifc_type_t ifc, int fd, void *param)
{
  const char *joystickName = (const char *)param;
  char name[NAME_LENGTH] = "Unknown";
  switch(ifc){
    case e_JS:
      if(ioctl(fd, JSIOCGNAME(NAME_LENGTH), name) < 0){
        ltr_int_my_perror("ioctl(JSIOCGNAME)");
        return false;
      }
      break;
    case e_EVDEV:
      if(ioctl(fd, EVIOCGNAME(NAME_LENGTH), name) < 0){
        ltr_int_my_perror("ioctl(EVIOCGNAME)");
        return false;
      }
      break;
  }
  //printf("Received name '%s'.\n", name);
  size_t max_len = (strlen(joystickName) < NAME_LENGTH) ? strlen(joystickName) : NAME_LENGTH;
  if(strncmp(name, joystickName, max_len) == 0){
    return true;
  }
  return false;
}
nglInputDeviceLinux::nglInputDeviceLinux (const nglPath& rDevice) : nglInputDeviceInstance(), nglEvent()
{
  mFlags = Read|Error;
  mFD = open((char*)rDevice.GetPathName().GetChars(), O_RDONLY);
  if (mFD == -1)
    return;

  char byte;
  char name[128];

  // Get number of axes
  ioctl(mFD, JSIOCGAXES, &byte);
  mAxes.resize(byte);

  // Get number of buttons
  ioctl(mFD, JSIOCGBUTTONS, &byte);
  mButtons.resize(byte);

  // Fetch name
  if (ioctl(mFD, JSIOCGNAME(sizeof(name)), name) < 0)
    mName = "unkown";
  else
    mName = name;

  // Synthetize port name
  mPort.Format("%s", rDevice.GetPathName().GetChars());

  App->AddEvent(this);
}
JoystickDriverLinux::JoystickDriverLinux() :
    m_joy_fd(0),
    m_initialized(false)
{
    if ((m_joy_fd = open("/dev/input/js0", O_RDONLY)) < 0)
    {
        return; // This will keep the driver uninitialized and reporting 0 joysticks
    }

    // Set to non-blocking reads
    fcntl(m_joy_fd, F_SETFL, O_NONBLOCK);

    char name[NAME_LENGTH] = "Unknown";
    ioctl(m_joy_fd, JSIOCGNAME(NAME_LENGTH), name);
    m_name = name;

    unsigned char axis = 0;
    unsigned char buttons = 0;
    ioctl(m_joy_fd, JSIOCGAXES, &axis);
    ioctl(m_joy_fd, JSIOCGBUTTONS, &buttons);

    m_numberOfAxis = (unsigned int) axis;
    m_numberOfButtons = (unsigned int) buttons;

    m_initialized = true;
}
Exemple #6
0
static bool storeJoyNames(ifc_type_t ifc, int fd, void *param)
{
  joystickNames_t *jsNames = (joystickNames_t *)param;

  char name[NAME_LENGTH] = "Unknown";

  switch(ifc){
    case e_JS:
      if(ioctl(fd, JSIOCGNAME(NAME_LENGTH), name) < 0){
        ltr_int_my_perror("ioctl(JSIOCGNAME)");
        return false;
      }
      break;
    case e_EVDEV:
      if(ioctl(fd, EVIOCGNAME(NAME_LENGTH), name) < 0){
        ltr_int_my_perror("ioctl(EVIOCGNAME)");
        return false;
      }
      break;
  }

  if(!arrayBigEnough((void ***)&(jsNames->nameList), &(jsNames->nameListSize), &(jsNames->namesFound), sizeof(char *))){
    return false;
  }

  jsNames->nameList[jsNames->namesFound] = strdup(name);
  ++jsNames->namesFound;
  return false;
}
	InputDeviceProvider_LinuxJoystick::InputDeviceProvider_LinuxJoystick(X11Window *window, const std::string &device)
		: window(window), device(device), fd(-1), new_event(false)
	{
		fd = open(device.c_str(), O_RDONLY | O_NONBLOCK);

		if (fd == -1)
		{
			throw Exception("Cannot Open Joystick");
		}

		char number_of_axes;
		char number_of_buttons;

		ioctl(fd, JSIOCGBUTTONS, &number_of_buttons);
		ioctl(fd, JSIOCGAXES, &number_of_axes);

		char name_cstr[256] = { '\0' };
		if (ioctl(fd, JSIOCGNAME(sizeof(name_cstr)), name_cstr) < 0)
			strncpy(name_cstr, "Unknown", sizeof(name_cstr));

		_name = name_cstr;

		axis_states.resize(number_of_axes);
		button_states.resize(number_of_buttons);
	}
const char* stjoystick_sys_get_name(int index)
{
	int fd;
	static char namebuf[128];
	char *name;
	SDL_logical_joydecl(int oindex = index);

#ifndef NO_LOGICAL_JOYSTICKS
	SDL_joylist_head(index, index);
#endif
	name = NULL;
	fd = open(SDL_joylist[index].fname, O_RDONLY, 0);
	if ( fd >= 0 ) {
		if ( 
#if SDL_INPUT_LINUXEV
		     (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) &&
#endif
		     (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0) ) {
			name = SDL_joylist[index].fname;
		} else {
			name = namebuf;
		}
		close(fd);


#ifndef NO_LOGICAL_JOYSTICKS
		if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next || index!=oindex)
		{
       		   LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128);
		}
#endif
	}
	return name;
}
bool joystick_load(int id)
{
  checkPositiveId(false);
  if (size_t(id) >= enigma::joysticks.size())
    enigma::joysticks.resize(id+1, 0);
  else
     delete enigma::joysticks[id];
  
  char sps[32]; sprintf(sps,"/dev/input/js%d",id);
  string devn(sps);
  int device = open(devn.c_str(), O_RDONLY|O_NONBLOCK);
  if (device == -1)
    return false;
  
  int ac = 4, bc = 4;
  ioctl(device, I_SRDOPT, RMSGN);
  if (ioctl(device, JSIOCGAXES, &ac) or ioctl(device, JSIOCGBUTTONS, &bc))
    return (close(device), false);
  
  char name[256]; name[0] = 0;
  if (ioctl(device, JSIOCGNAME(256), name) > 0)
    devn = name;
  printf("Joystick name: %s\n",name);
  
  enigma::e_joystick* const jsn = new enigma::e_joystick(device, devn, ac, bc);
  enigma::joysticks[id] = jsn;
  enigma::handle_joystick(jsn);
  return true;
}
Exemple #10
0
MJoystickLinux::MJoystickLinux(const char* joydev)
{
	if(joydev == NULL)
	{
		return;
	}
	
	m_numAxes = 0;
	m_numButtons = 0;
	m_axis = NULL;
	m_button = NULL;
	m_dAxis = NULL;
	
	if((m_file = open(joydev, O_RDONLY)) == -1)
	{
		return;
	}
	
	ioctl(m_file, JSIOCGAXES, &m_numAxes);
	ioctl(m_file, JSIOCGBUTTONS, &m_numButtons);
	ioctl(m_file, JSIOCGNAME(80), &m_joystickName);
	
	m_axis = (int*) calloc(m_numAxes, sizeof(int));
	m_dAxis = (float*) calloc(m_numAxes, sizeof(float));
	m_button = (char*) calloc(m_numButtons, sizeof(char));
	
	fcntl(m_file, F_SETFL, O_NONBLOCK);
}
Exemple #11
0
/*---------------------------------------------------------------
  Name     : SetJoystick
  Argument : void
  Return   : 0 (succeed), other (failed)
  About    : Set up the Joystick controler
  Version  : Ver 1.0
  Date     : 2014/03/21
  Author   : Ryodo Tanaka (Kyushu Institute of Technology)
----------------------------------------------------------------- */
int SetJoystick(void)
{
  //File open
  if( (JSfd=open(PORT, O_RDONLY)) == -1){
    printLOG("File Open JoyStick");
    return 1;
  }

  //Get JoyStick information
  ioctl(JSfd, JSIOCGAXES, &num_of_axis);
  ioctl(JSfd, JSIOCGBUTTONS, &num_of_buttons);
  ioctl(JSfd, JSIOCGNAME(80), &JSname);

  //Get data space for axis & buttons
  axis = (int*)calloc(num_of_axis, sizeof(int));
  if(!axis){
    printLOG("calloc JoyStick axis");
    return 2;
  }
  button = (char*)calloc(num_of_buttons, sizeof(char));
  if(!button){
    printLOG("calloc JoyStick axis");
    return 3;
  }

  //Use non-blocking mode
  fcntl(JSfd, F_SETFL, O_NONBLOCK);

  printf("%s\tis Connected ...\n", JSname);
  
  return 0;

}
Exemple #12
0
void			Controler::update()
{
  char			tmp;
  std::stringstream	stm;

  stm << "/dev/input/js" << _id;
  if (access(stm.str().c_str(), R_OK) == -1)
    clean();
  if (_fd != -1)
    return ;
  _fd = open(stm.str().c_str(), O_RDONLY);
  if (_fd == -1)
    clean();
  ioctl(_fd, JSIOCGAXES, &tmp);
  if (tmp < 2 || tmp > 50)
    clean();
  _axe.resize(tmp);
  ioctl(_fd, JSIOCGBUTTONS, &tmp);
  if (tmp < 6 || tmp > 80)
    clean();
  _but.resize(tmp);
  ioctl(_fd, JSIOCGNAME(sizeof(_name)), &_name);

  fcntl(_fd, F_SETFL, O_NONBLOCK);
  //std::cout << _name << " Axe[" << _axe.size() <<
  //  "] Buttons [" << _but.size() << "]" << std::endl;
}
Exemple #13
0
void updateCapabilities(int joystick)
{
    if (joystick<0 || MAXJOYSTICKS<=joystick) {
        return;
    }

    capabilities[joystick].povs=0;
    capabilities[joystick].axes=0;
    capabilities[joystick].buttons=0;
    capabilities[joystick].version=0;
    strcpy(capabilities[joystick].name, "Unknown");

    if (fd[joystick]<0 && openJoystick(joystick)<0)
        return;
  
    ioctl(fd[joystick], JSIOCGAXES, &capabilities[joystick].axes);
    ioctl(fd[joystick], JSIOCGBUTTONS, &capabilities[joystick].buttons);
    ioctl(fd[joystick], JSIOCGVERSION, &capabilities[joystick].version);

    int nameLen = ioctl(fd[joystick], JSIOCGNAME(JOYSTICK_NAME_SIZE), capabilities[joystick].name);
    if (nameLen > 0) {
        // NULL terminate just in case.
        capabilities[joystick].name[JOYSTICK_NAME_SIZE - 1] = 0;
    }
    else {
        strcpy(capabilities[joystick].name, "Unknown");
    }
}
// Private methods -----------------------------------------------
// Runs all the necessary functions to ensure connectivity
void Initialize(Controller controller){
  int fd = open(JOYSTICK_DEVICE, O_RDONLY);

  if(fd > 0){
    char name[128];
    int version;
    js_fd = fd;
    controller->active = true;
    
    // store driver version, if it is less than
    // version 1.0 there will be no support for it
    ioctl(fd, JSIOCGVERSION, &version);
    if (version < 0x010000){
      return;
    }
    controller->version = version;

    // store size of the name and makes sure its not null
    // if its valid store the name into our struct.
    int ret = ioctl(fd, JSIOCGNAME(sizeof(name)), name);
    if (ret < 0){
      return;
    }
    
    controller->name = strdup(name);

    deviceInfo(controller);
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_create(&controller->thread,&attr, Loop, (void*)controller);
  }

}
Exemple #15
0
int joystick_init(const char* device)
{
    if((joy.fd = open(device, O_RDONLY)) < 0)
        return 1;

    joy.mapping = 0;
    joy.calibration = 0;

    ioctl(joy.fd, JSIOCGNAME(JOYSTICK_NAME_LEN), joy.name);
    ioctl(joy.fd, JSIOCGAXES, &joy.axes);
    ioctl(joy.fd, JSIOCGBUTTONS, &joy.buttons);
    ioctl(joy.fd, JSIOCGVERSION, &joy.version);

    /*MSG("Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",*/
        /*joy.name, joy.axes, joy.buttons,*/
        /*joy.version >> 16, (joy.version >> 8) & 0xff, joy.version & 0xff);*/

    joy.axis = calloc(joy.axes, sizeof(int));
    joy.button = calloc(joy.buttons, sizeof(int));

    /*if(pthread_mutex_init(&js_update_mtx, NULL))*/
        /*return 2;*/

    if(pthread_create(&joystick_update_tid, NULL, &joystick_update_thread, NULL))
        return 3;

    MSG("Joystick %s opened\n", device);

    return 0;
}
Exemple #16
0
    bool joystick::connect(const unsigned id)
    {
        string str = string("/dev/input/js") << string::dec(id);

        if((descriptor_ = open(str(), O_RDONLY)) > 0)
        {
            fcntl(descriptor_, F_SETFL, O_NONBLOCK);

            char axis_count, button_count, name[128];
            ioctl(descriptor_, JSIOCGAXES,      &axis_count);
            ioctl(descriptor_, JSIOCGBUTTONS,   &button_count);

            if(ioctl(descriptor_, JSIOCGNAME(sizeof(name)), name))
                name_ = name;

            for(signed pos = name_.find("  "); pos != -1; pos = name_.find("  "))
                name_.erase(pos, 1);

            while(name_.substr(-1) == " ")
                name_.erase(-1);

            axes_       = axis_count;
            buttons_    = button_count;
            id_         = static_cast<signed>(id);

            events_.clear();

           return true;
        }

        return false;
    }
Exemple #17
0
void Joystick::create(const char* deviceFileName)
{
  int axes = 0;
  int buttons = 0;
  char deviceName[JS_NAME_LENGTH + 1];

  /* Init the poll thread to NULL */
  m_thread = 0;

  m_fd = ::open(deviceFileName, O_RDONLY);
  if (m_fd != -1)
    {
      /* Copy the device file name to struct */
      m_fdName = QString(deviceFileName);

      /* Get number of axes */
      if (ioctl(m_fd, JSIOCGAXES, &axes) == -1)
	{
	  axes = 0;
	  perror("ioctl");
	}
      else
	{
	  fillAxisNames(axes);
      	}

      /* Get number of buttons */
      if (ioctl(m_fd, JSIOCGBUTTONS, &buttons) == -1)
	{
	  buttons = 0;
	  perror("ioctl");
	}
      else
	{
          fillButtonNames(buttons);
	}

      /* Get the name of the joystick device */
      if (ioctl(m_fd, JSIOCGNAME(JS_NAME_LENGTH), &deviceName) == -1)
	{
	  strcpy(deviceName, "Unknown");
	  perror("ioctl");
	}

      m_name = QString(deviceName);

      printf("Device: %s\nName: %s\nAxes: %d\nButtons: %d\n",
	     (const char*) m_fdName, (const char*) m_name, axes, buttons);

      /* Close this joystick until we really want to use it */
      close();

      m_valid = true;
    }
  else
    {
      m_valid = false;
    }
}
Exemple #18
0
wxString wxJoystick::GetProductName() const
{
    char name[128];

    if (ioctl(m_device, JSIOCGNAME(sizeof(name)), name) < 0)
        strcpy(name, "Unknown");
    return wxString(name, wxConvLibc);
}
Exemple #19
0
/*
  Function: joy_open

  Opens a USB joystick and fills its information.

  Parameters:

  joynumber - Joystick's identifier (0 reserved for GP2X's builtin Joystick).

  Returns:

  Filled usbjoy structure.

*/
struct usbjoy *joy_open(int joynumber)
{
	int fd;
	char path [128];
	struct usbjoy * joy = NULL;
	struct js_event event;
	static char insmod_done = 0;

	// notaz: on my system I get unresolved input_* symbols, so have to 'insmod input' too
	// also we should insmod only once, not on every joy_open() call.
	if (!insmod_done) {
		system ("insmod input");
		system ("insmod joydev"); // Loads joydev module
		insmod_done = 1;
	}

	if (joynumber == 0) {
	}
	else if (joynumber > 0) {
		sprintf (path, "/dev/input/js%d", joynumber-1);
		fd = open(path, O_RDONLY, 0);
		if (fd > 0) {
			joy = (struct usbjoy *) malloc(sizeof(*joy));
			if (joy == NULL) { close(fd); return NULL; }
			memset(joy, 0, sizeof(*joy));

			// Set the joystick to non-blocking read mode
			fcntl(fd, F_SETFL, O_NONBLOCK);

			// notaz: maybe we should flush init events now.
			// My pad returns axis as active when I plug it in, which is kind of annoying.
			while (read(fd, &event, sizeof(event)) > 0);

			// Joystick's file descriptor
			joy->fd = fd;

			// Joystick's name
			ioctl(joy->fd, JSIOCGNAME(128*sizeof(char)), joy->name);

			// Joystick's device
			strcpy(joy->device, path);

			// Joystick's buttons
			ioctl(joy->fd, JSIOCGBUTTONS, &joy->numbuttons);

			// Joystick's axes
			ioctl(joy->fd, JSIOCGAXES, &joy->numaxes);

			// Joystick's type (derived from name)
			if (strncasecmp(joy->name, "logitech", strlen("logitech")) == 0)
			     joy->type = JOY_TYPE_LOGITECH;
			else joy->type = JOY_TYPE_GENERIC;
		} else {
			// printf ("ERROR: No Joystick found\n");
		}
	}
	return joy;
}
Exemple #20
0
int main()
{
	int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
	char *button=NULL, name_of_joystick[80];
	struct js_event js;

	if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
	{
		printf( "Couldn't open joystick\n" );
		return -1;
	}

	ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
	ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
	ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );

	axis = (int *) calloc( num_of_axis, sizeof( int ) );
	button = (char *) calloc( num_of_buttons, sizeof( char ) );

	printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
		, name_of_joystick
		, num_of_axis
		, num_of_buttons );

	fcntl( joy_fd, F_SETFL, O_NONBLOCK );	/* use non-blocking mode */

	while( 1 ) 	/* infinite loop */
	{

			/* read the joystick state */
		read(joy_fd, &js, sizeof(struct js_event));
		
			/* see what to do with the event */
		switch (js.type & ~JS_EVENT_INIT)
		{
			case JS_EVENT_AXIS:
				axis   [ js.number ] = js.value;
				break;
			case JS_EVENT_BUTTON:
				button [ js.number ] = js.value;
				break;
		}

			/* print the results */
        printf( "X1: %6d  Y1: %6d  ", (int)((axis[0]+32767)*0.00091554131), (int)((axis[1]+32767)*0.00091554131));
		printf( "X2: %6d  Y2: %6d  ", (int)((axis[2]+32767)*0.00091554131), (int)((axis[3]+32767)*0.00091554131));
		printf( "X3: %6d  Y3: %6d  ", (int)((axis[4]+32767)*0.00091554131), (int)((axis[5]+32767)*0.00091554131));
			
		for( x=0 ; x<num_of_buttons ; ++x )
			printf("B%d: %d  ", x, button[x] );

		printf("  \r");
		fflush(stdout);
	}

	close( joy_fd );	/* too bad we never get here */
	return 0;
}
Exemple #21
0
// tests the open joystick file descriptor to see if it's really a JW, and set to read rawdata
bool CSensorLinuxUSBJW::testJoystick()
{
   // if made it here, then we have opened a joystick file descriptor
   m_iNumAxes = 0;
   m_iNumButtons = 0;
   memset(m_strJoystick, 0x00, 80);

   ioctl(m_fdJoy, JSIOCGAXES, &m_iNumAxes);
   ioctl(m_fdJoy, JSIOCGBUTTONS, &m_iNumButtons);
   ioctl(m_fdJoy, JSIOCGNAME(80), m_strJoystick);

//fprintf(stdout, "joystick found = %s\n", m_strJoystick);
//fflush(stdout);

   // compare the name of device, and number of buttons & axes with valid JoyWarrior values
   if (strcmp(m_strJoystick, IDSTR_JW24F8)
     || m_iNumButtons != NUM_BUTTON_JW24F8
     || m_iNumAxes != NUM_AXES_JW24F8) {
         closePort();  // this far in, we need to close the port!
         return false;
   }

   m_piAxes = (int *) calloc( m_iNumAxes, sizeof( int ) );
   memset(m_piAxes, 0x00, sizeof(int) * m_iNumAxes);
   m_strButton = (char *) calloc( m_iNumButtons, sizeof( char ) );
   memset(m_strButton, 0x00, sizeof(char) * m_iNumButtons);
  
   fcntl( m_fdJoy, F_SETFL, O_NONBLOCK );   // use non-blocking mode

   // try a read
   float x,y,z;
   // "prime" the joystick reader
   if (! read_xyz(x,y,z)) {
      closePort();  // this far in, we need to close the port!
      return false;
   }

   // if made it here, then it's a joywarrior, set to raw data mode
   struct js_corr corr[NUM_AXES_JW24F8];

   // Zero correction coefficient structure and set all axes to Raw mode 
   for (int i=0; i<NUM_AXES_JW24F8; i++) {
     corr[i].type = JS_CORR_NONE;
     corr[i].prec = 0;
     for (int j=0; j<8; j++) {
        corr[i].coef[j] = 0;
     }
   }

   if (ioctl(m_fdJoy, JSIOCSCORR, &corr)) {
      fprintf(stderr, "CSensorLinuxUSBJW:: error setting correction for raw data reads\n");
   }

   setType(SENSOR_USB_JW24F8);
   setPort(getTypeEnum());
   
   return true; // if here we can return true, i.e Joywarrior found on Linux joystick port, and hopefully set to read raw data
}
Exemple #22
0
// Attempt to open the specified joystick device
//
static void openJoystickDevice(const char* path)
{
#if defined(__linux__)
    char axisCount, buttonCount;
    char name[256];
    int joy, fd, version;

    for (joy = GLFW_JOYSTICK_1;  joy <= GLFW_JOYSTICK_LAST;  joy++)
    {
        if (!_glfw->linux_js.js[joy].present)
            continue;

        if (strcmp(_glfw->linux_js.js[joy].path, path) == 0)
            return;
    }

    for (joy = GLFW_JOYSTICK_1;  joy <= GLFW_JOYSTICK_LAST;  joy++)
    {
        if (!_glfw->linux_js.js[joy].present)
            break;
    }

    if (joy > GLFW_JOYSTICK_LAST)
        return;

    fd = open(path, O_RDONLY | O_NONBLOCK);
    if (fd == -1)
        return;

    _glfw->linux_js.js[joy].fd = fd;

    // Verify that the joystick driver version is at least 1.0
    ioctl(fd, JSIOCGVERSION, &version);
    if (version < 0x010000)
    {
        // It's an old 0.x interface (we don't support it)
        close(fd);
        return;
    }

    if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
        strncpy(name, "Unknown", sizeof(name));

    _glfw->linux_js.js[joy].name = strdup(name);
    _glfw->linux_js.js[joy].path = strdup(path);

    ioctl(fd, JSIOCGAXES, &axisCount);
    _glfw->linux_js.js[joy].axisCount = (int) axisCount;

    ioctl(fd, JSIOCGBUTTONS, &buttonCount);
    _glfw->linux_js.js[joy].buttonCount = (int) buttonCount;

    _glfw->linux_js.js[joy].axes = calloc(axisCount, sizeof(float));
    _glfw->linux_js.js[joy].buttons = calloc(buttonCount, 1);

    _glfw->linux_js.js[joy].present = GL_TRUE;
#endif // __linux__
}
Exemple #23
0
/**
 * Hauptfunktion
 * @return 
 */
int main(void) {

	int joy_fd, axis[4] = {1,2,3,-4}, num_of_axis = 0, num_of_buttons = 0;
	char button[9] = {}, name_of_joystick[80];
	struct js_event js;

	if((joy_fd = open(JOY_DEV, O_RDONLY | O_NDELAY)) == -1) {
		printf("Konnte Joystick nicht oeffnen!\n");
		return -1;
	}

	ioctl(joy_fd, JSIOCGAXES, &num_of_axis);
	ioctl(joy_fd, JSIOCGBUTTONS, &num_of_buttons);
	ioctl(joy_fd, JSIOCGNAME(80), &name_of_joystick);

	if(DEBUG)
            printf(
                    "Joystick gefunden: %s\n\t%d Axen\n\t%d Buttons\n\n",
                    name_of_joystick,
                    num_of_axis,
                    num_of_buttons
            );

	fcntl(joy_fd, F_SETFL, O_NONBLOCK); // use non-blocking mode 
        
	while(1)
	{

		// read the joystick state
		read(joy_fd, &js, sizeof(struct js_event));

		// see what to do with the event
		switch(js.type & ~JS_EVENT_INIT)
		{
			case JS_EVENT_AXIS:
				axis[js.number] = js.value;
				break;
			case JS_EVENT_BUTTON:
				button[js.number] = js.value;
				break;
		}
                
                // Abbruch?
		if(button[6] != 0) { 
                    
                    
                    break;
                    
		}
                
                socket_send_js(axis, button);
                
                usleep(5000);
       
        
	} // while
    
} // main
Exemple #24
0
static GLFWbool openJoystickDevice(const char* path)
{
    char axisCount, buttonCount;
    char name[256];
    int joy, fd, version;
    _GLFWjoystickLinux* js;

    for (joy = GLFW_JOYSTICK_1;  joy <= GLFW_JOYSTICK_LAST;  joy++)
    {
        if (!_glfw.linux_js.js[joy].present)
            continue;

        if (strcmp(_glfw.linux_js.js[joy].path, path) == 0)
            return GLFW_FALSE;
    }

    for (joy = GLFW_JOYSTICK_1;  joy <= GLFW_JOYSTICK_LAST;  joy++)
    {
        if (!_glfw.linux_js.js[joy].present)
            break;
    }

    if (joy > GLFW_JOYSTICK_LAST)
        return GLFW_FALSE;

    fd = open(path, O_RDONLY | O_NONBLOCK);
    if (fd == -1)
        return GLFW_FALSE;

    // Verify that the joystick driver version is at least 1.0
    ioctl(fd, JSIOCGVERSION, &version);
    if (version < 0x010000)
    {
        // It's an old 0.x interface (we don't support it)
        close(fd);
        return GLFW_FALSE;
    }

    if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
        strncpy(name, "Unknown", sizeof(name));

    js = _glfw.linux_js.js + joy;
    js->present = GLFW_TRUE;
    js->name = strdup(name);
    js->path = strdup(path);
    js->fd = fd;

    ioctl(fd, JSIOCGAXES, &axisCount);
    js->axisCount = (int) axisCount;
    js->axes = calloc(axisCount, sizeof(float));

    ioctl(fd, JSIOCGBUTTONS, &buttonCount);
    js->buttonCount = (int) buttonCount;
    js->buttons = calloc(buttonCount, 1);

    _glfwInputJoystickChange(joy, GLFW_CONNECTED);
    return GLFW_TRUE;
}
Exemple #25
0
void IN_StartupJoystick (void) { 
	int value = 0;
        char joy_desc[1024];
 
 	// assume no joystick
	joy_avail = false; 

	// only initialize if the user wants it
	if (!COM_CheckParm ("-joystick")) 
		return; 

	Cvar_SetCurrentGroup(CVAR_GROUP_INPUT_JOY);
	Cvar_Register (&joy_name);
	Cvar_Register (&joy_advanced);
	Cvar_Register (&joy_advaxisx);
	Cvar_Register (&joy_advaxisy);
	Cvar_Register (&joy_advaxisz);
	Cvar_Register (&joy_advaxisr);
	Cvar_Register (&joy_advaxisu);
	Cvar_Register (&joy_advaxisv);
	Cvar_Register (&joy_forwardthreshold);
	Cvar_Register (&joy_sidethreshold);
	Cvar_Register (&joy_flythreshold);
	Cvar_Register (&joy_pitchthreshold);
	Cvar_Register (&joy_yawthreshold);
	Cvar_Register (&joy_forwardsensitivity);
	Cvar_Register (&joy_sidesensitivity);
	Cvar_Register (&joy_flysensitivity);
	Cvar_Register (&joy_pitchsensitivity);
	Cvar_Register (&joy_yawsensitivity);
	Cvar_Register (&joy_wwhack1);
	Cvar_Register (&joy_wwhack2);
	Cvar_ResetCurrentGroup();

	joystick = open (JOYSTICK_DEVICE_S, O_RDONLY|O_NONBLOCK);
        if (joystick == 0) {
		Com_Printf ("\njoystick not found -- driver not present\n\n");
		return;
        }

        // save the joystick's number of buttons and POV status
        ioctl (joystick, JSIOCGNAME (sizeof (joy_desc)), joy_desc);
        ioctl (joystick, JSIOCGBUTTONS, &value);
	joy_numbuttons = value;
	joy_haspov = 0;

	// old button and POV states default to no buttons pressed
	joy_oldbuttonstate = joy_oldpovstate = 0;

	// mark the joystick as available and advanced initialization not completed
	// this is needed as cvars are not available during initialization
	joy_avail = true; 
	joy_advancedinit = false;

	Com_Printf ("\njoystick detected (%s, %d buttons)\n\n", joy_desc, joy_numbuttons); 

}
void InputHandler_Linux_Joystick::Attach(CString path)
{
	struct stat st;
	if( stat( path.c_str(), &st ) == -1 )
	{
		if( errno != ENOENT )
			LOG->Warn( "Couldn't stat %s: %s", path.c_str(), strerror(errno) );
		return;
	}

	if( !S_ISCHR( st.st_mode ) )
	{
		LOG->Warn( "Ignoring %s: not a character device", path.c_str() );
		return;
	}

	int fd = open( path.c_str(), O_RDONLY );

	if (fd != -1)
	{
		CString name;

		char szName[1024];
		ZERO( szName );
		if( ioctl(fd, JSIOCGNAME(sizeof(szName)), szName) < 0 )
			name = ssprintf( "Unknown joystick at %s", path.c_str() );
		else
			name = szName;

		for (int i = 0; i < NUM_JOYSTICKS; ++i)
		{
			if (m_Devs[i].fd < 0)
			{
				m_Devs[i].path = path;
				m_Devs[i].fd = fd;
				m_Devs[i].name = name;

				/* ScreenManager is not loaded when the joysticks are first initialized */
				if (SCREENMAN)
				{
					SCREENMAN->SystemMessageNoAnimate(ssprintf("Joystick %d \"%s\" attached", i+1, name.c_str()));
					if (INPUTMAPPER)
					{
						LOG->Info("Remapping joysticks after hotplug.");
						INPUTMAPPER->AutoMapJoysticksForCurrentGame();
					}
				}
				return;
			}
		}

		LOG->Warn("Couldn't find a free device slot for new joystick!");
	} else {
		LOG->Warn("Couldn't open %s: %s", path.c_str(), strerror(errno));
	}
}
Exemple #27
0
bool Joystick::open (const std::string device="")
{
    joy_fd=-1;
    if (!device.empty()) {
        joy_fd = ::open (device.c_str(), O_RDONLY | O_NONBLOCK);
        devPath=device;
    } else {
        // No device specified, use the first successfully opened joystick from the
        // hard coded list
        bool foundJS=false;       
        for (unsigned int i=0; i<devList.size() && joy_fd==-1; i++) {
            joy_fd = ::open (devList[i].c_str(), O_RDONLY | O_NONBLOCK);
            devPath=devList[i];
        }
    }

    if (joy_fd == -1) {
        return false;
    }
    else {
        
        std::cerr << "Successfully opened joystick device: " << devPath << std::endl;

        char d_buttons;
        char d_axes;
        char d_name[128];


        // get device name
        if (ioctl(joy_fd, JSIOCGNAME (sizeof(d_name)), d_name) < 0) 
            name = "Unknown";
        else
            name = d_name;

        
        // get the version
        ioctl(joy_fd, JSIOCGVERSION, &version);
    
        // get number of buttons
        ioctl (joy_fd, JSIOCGBUTTONS, &d_buttons);
        numButtons = d_buttons;
        
        // get number of axes
        ioctl (joy_fd, JSIOCGAXES, &d_axes);
        numAxes = d_axes;

        // Reserve space for the buttons and axes
        buttons.resize(numButtons);
        axes.resize(numAxes);
        

    }
    
    opened=true;
    return true;
}
/**
 * Search all Joystick devices
 */
gboolean search_devices(GList **list_controllers) {
  int i;
  Controller_info *ctrl_info;
  struct udev *udev;
  struct udev_device *dev;

  for(i = 0; i < 32; ++i) {
    gchar *str = NULL;
    str = g_strdup_printf("/dev/input/js%d", i);
    int fd = open(str, O_RDONLY);
    if (fd < 0) {
      //printf("Could not found joystick: %s\n", str->str);
      break;
    } else {
      ctrl_info = g_malloc(sizeof(Controller_info));
      uint8_t num_axis   = 0;
      uint8_t num_button = 0;
      ioctl(fd, JSIOCGAXES,    &num_axis);
      ioctl(fd, JSIOCGBUTTONS, &num_button);
      ctrl_info->filename    = g_strdup(str);
      ctrl_info->num_axis    = num_axis;
      ctrl_info->num_buttons = num_button;
      // Get Name 
      char name_c_str[1024];
      if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0) {
          printf("%s : %s", str, strerror(errno));
          break;
      } else {
         ctrl_info->name = g_convert_with_fallback(name_c_str, sizeof(name_c_str), "UTF-8", "ISO-8859-1", NULL, NULL, NULL, NULL);
      }

      /* Create the udev object */
      udev = udev_new();
      if (!udev) {
        printf("Can't create udev\n");
        exit(0);
      }

      dev = udev_device_new_from_subsystem_sysname(udev, "input", g_strdup_printf("js%d", i));
      if (dev == NULL)
            break;

      ctrl_info->serial = uint32_atoi(g_strdup_printf("%s%s", udev_list_entry_get_value(udev_list_entry_get_by_name(udev_device_get_properties_list_entry(dev), "ID_VENDOR_ID")), udev_list_entry_get_value(udev_list_entry_get_by_name(udev_device_get_properties_list_entry(dev), "ID_MODEL_ID"))));

      udev_device_unref(dev);
      udev_unref(udev);
      printf("%s : %d, %d, 0x%08x\n", ctrl_info->name, ctrl_info->num_axis, ctrl_info->num_buttons, ctrl_info->serial);
    }
    *list_controllers = g_list_append(*list_controllers, ctrl_info);
    close(fd);
  }

  return TRUE;
}
Exemple #29
0
/**
 * Constructor. Throw an exception in case of failure.
 *
 * Param1 : The device name to read from. e.g: /dev/input/js0
 * Param2 : The JoystickListener that will be notified with jostick events
 *
 */
Joystick::Joystick(char *dev_name, JoystickListener *lstnr)
{
	mutex = PTHREAD_MUTEX_INITIALIZER;

    // Try to open the device.
    joystickFD = ::open(dev_name, O_RDONLY | O_NONBLOCK);
    if (joystickFD < 0)
    {
        string err = "Unable to open device '";
        err = err + dev_name;
        err = err + "' : ";
        err = err + strerror(errno);
        throw err;
    }
    
    // Device successfully opened. Keep track of device name.
    ::strncpy(deviceName, dev_name, MAX_DEVNAME_LEN);
    
    // Now get Joystick ID.
    if (::ioctl(joystickFD, JSIOCGNAME(MAX_JOYID_LEN), joystickID) < 0)
    {
		::strncpy(joystickID, "Unknown", MAX_JOYID_LEN);
	}

	// Now get button number.
    if (::ioctl(joystickFD, JSIOCGBUTTONS, &buttonNbr) < 0)
    {
		buttonNbr = 0;
	}	
	
    // Now get Axis number.
    if (::ioctl(joystickFD, JSIOCGAXES, &axisNbr) < 0)
    {
		axisNbr = 0;
	}

    // Now get driver version.
    if (::ioctl(joystickFD, JSIOCGVERSION, &driverVersion) < 0)
    {
		driverVersion = 0xFFFFFFFF;
	}
	
    // Keep track of listener for upcoming events.
    setListener(lstnr);
    
    // Set default polling period.
    setPollingPeriod(DEFAULT_POLLING_PERIOD); // in milliseconds.

	// Start device polling.
	done = false;
	this->start();
}
Exemple #30
0
Joystick::Joystick(VRDevice::Factory* sFactory,VRDeviceManager* sDeviceManager,Misc::ConfigurationFile& configFile)
    :VRDevice(sFactory,sDeviceManager,configFile),
     joystickDeviceFd(open(configFile.retrieveString("./joystickDeviceFile").c_str(),O_RDONLY)),
     axisGains(0),
     reportEvents(false),
     buttonStates(0),valuatorStates(0)
{
    /* Check if the joystick device port was properly opened: */
    if(joystickDeviceFd<0)
        Misc::throwStdErr("Joystick: Unable to open joystick device port");

    /* Query the joystick's geometry: */
    char cNumButtons,cNumAxes;
    ioctl(joystickDeviceFd,JSIOCGBUTTONS,&cNumButtons);
    ioctl(joystickDeviceFd,JSIOCGAXES,&cNumAxes);
#ifdef VERBOSE
    /* Query the joystick's name: */
    char joystickName[256];
    if(ioctl(joystickDeviceFd,JSIOCGNAME(sizeof(joystickName)),joystickName)>=0)
    {
        joystickName[sizeof(joystickName)-1]='\0';
        printf("Joystick: %s with %d buttons and %d axes found\n",joystickName,int(cNumButtons),int(cNumAxes));
    }
    else
        printf("Joystick: Unknown joystick with %d buttons and %d axes found\n",int(cNumButtons),int(cNumAxes));
#endif

    /* Set device configuration: */
    setNumTrackers(0,configFile);
    setNumButtons(cNumButtons,configFile);
    setNumValuators(cNumAxes,configFile);

    /* Initialize gain arrays: */
    axisGains=new float[getNumValuators()];
    for(int i=0; i<getNumValuators(); ++i)
    {
        char axisGainTag[40];
        snprintf(axisGainTag,sizeof(axisGainTag),"./axisGain%d",i);
        axisGains[i]=configFile.retrieveValue<float>(axisGainTag,1.0f);
    }

    /* Initialize state arrays: */
    buttonStates=new bool[getNumButtons()];
    for(int i=0; i<getNumButtons(); ++i)
        buttonStates[i]=false;
    valuatorStates=new float[getNumValuators()];
    for(int i=0; i<getNumValuators(); ++i)
        valuatorStates[i]=0.0f;

    /* Start device thread (joystick device cannot be disabled): */
    startDeviceThread();
}