⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inputdevice.cpp

📁 FreeWRLduneInputDevice和FreeWRL一起可以让用户用带有6DoF的输入设备检索3D VRML/X3D数据。它基于FreeWRL的"/tmp/inpdev"扩展传感器输入接口和w
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      if (stringncmp(string,"allrot")!=0)         {         set_zero_on_release(num_axis_x,value);         set_zero_on_release(num_axis_y,value);         set_zero_on_release(num_axis_z,value);         }      if (stringncmp(string,"allxyz")!=0)         {         set_zero_on_release(num_axis_xrot,value);         set_zero_on_release(num_axis_yrot,value);         set_zero_on_release(num_axis_zrot,value);             }      }   }// return value (relative to max) which will be ignoredfloat InputDevice::get_zero_size_fraction(int axis_number)   {   if ((zero_size_fraction==NULL) || (axis_number>=number_axes))      return 0;   else      return zero_size_fraction[axis_number];   }void InputDevice::set_zero_size_fraction(int axis_number,float value)   {   if (axis_number<number_axes)      {      if (zero_size_fraction==NULL)         {         zero_size_fraction=new float[number_axes];         for (int i=0;i<number_axes;i++)            zero_size_fraction[i]=0;         }      zero_size_fraction[axis_number]=value;      }   else      fprintf(stderr,"axis number %d do not exist \n",axis_number);   }void InputDevice::set_zero_size_fraction(char* string,float value)   {   for (int i=0;i<sizeof(axesinfo)/sizeof(char*);i++)      if (stringncmp(string,axesinfo[i])==0)         {         set_zero_size_fraction(getAxisFromInformation(string),value);         break;         }   if (stringncmp(string,"all")==0)      {      if (stringncmp(string,"allrot")!=0)         {         set_zero_size_fraction(num_axis_x,value);         set_zero_size_fraction(num_axis_y,value);         set_zero_size_fraction(num_axis_z,value);         }      if (stringncmp(string,"allxyz")!=0)         {         set_zero_size_fraction(num_axis_xrot,value);         set_zero_size_fraction(num_axis_yrot,value);         set_zero_size_fraction(num_axis_zrot,value);             }      }   }int InputDevice::getAxisFromInformation(char* axesinfo)   {   int axis_number;   if      (strcmp(axesinfo,"xrot")==0)      axis_number=num_axis_xrot;   else if (strcmp(axesinfo,"yrot")==0)      axis_number=num_axis_yrot;   else if (strcmp(axesinfo,"zrot")==0)      axis_number=num_axis_zrot;   else if (strcmp(axesinfo,"x")==0)      axis_number=num_axis_x;   else if (strcmp(axesinfo,"y")==0)      axis_number=num_axis_y;   else if (strcmp(axesinfo,"z")==0)      axis_number=num_axis_z;   else       {      fprintf(stderr,"axesinfo %s not understood, using axis 0\n",axesinfo);      axis_number=0;      }   return axis_number;   }void InputDevice::setAxisOfInformation(char* axesinfo,int axis_number)   {   if      (strcmp(axesinfo,"xrot")==0)      num_axis_xrot=axis_number;   else if (strcmp(axesinfo,"yrot")==0)      num_axis_yrot=axis_number;   else if (strcmp(axesinfo,"zrot")==0)      num_axis_zrot=axis_number;   else if (strcmp(axesinfo,"x")==0)      num_axis_x=axis_number;   else if (strcmp(axesinfo,"y")==0)      num_axis_y=axis_number;   else if (strcmp(axesinfo,"z")==0)      num_axis_z=axis_number;   }// destruktive way to cut out the string till next comma// return index to the comma position or -1 if no comma position availablestatic int cut_out_till_next_comma(char* string)   {   char* comma;   // search for comma   comma=strchr(string,',');   if (comma==NULL)      return -1;   else      {      // end of string is placed at comma position                comma[0]=(char)0;      return(comma-string+1);      }   }static bool string2int(int &value,char* string)   {   int characters_read;      sscanf(string,"%d%n",&value,&characters_read);   if (characters_read!=strlen(string))      return(false);   return(true);   }bool InputDevice::argument_is_axis(char* string)   {   for (int i=0;i<sizeof(axesinfo)/sizeof(char*);i++)      if (stringncmp(string,axesinfo[i])==0)         return true;   return false;   }   /* * parse argumentstring  * valid syntax is * * -x|-y|-z|-xrot|-yrot|-zrot=[-][integer_axisnumber][,[factor][,[accel][,[wheel][,ignore]]]] * -all|-allxyz|-allrot=[factor][,[accel][,[wheel][,ignore]]] * * -                  sign of value from axis is swapped * * integer_axisnumber is a integer with the number of the axis, that should  *                    be used for the x y z xrot yrot zrot directions. *                    This number may not be greater than the number of *                    axes of the inputdevice * * factor is a float with a multiplicator for the axes * * accel is a float with a expotential accelerator for the axes * * wheel is the string "wheel" *       wheel means this axis of the inputdevice will not deliver zero  *       if released * * ignore is a float with the value (relative to the maximal value *        from the device) which will be ignored (insensitivity) *   * function returns always true     */bool InputDevice::scan_argument(char *string,char* argument)    {   int next;   bool sign_swap=false;   // skip argument   string=string+strlen(argument);   // skip '='   if (string[0]=='=')      string++;   // read sign of axis   if (string[0]=='-')      {      sign_swap=true;      string++;      }   if (argument_is_axis(argument))      {      // read axisnumber      next=cut_out_till_next_comma(string);      if (next!=1)         {         int characters_read;         int axis_number;         if (!string2int(axis_number,string))            {            fprintf(stderr,"axisnumber %s is not a integer\n",string);            return true;            }         setAxisOfInformation(argument,axis_number);         set_sign_swap(axis_number,sign_swap);         if (next==-1)            return true;         }      // position after comma      string=string+next;      }   // read factor   next=cut_out_till_next_comma(string);   if (next!=1)      {      int characters_read;      float fact;      sscanf(string,"%f%n",&fact,&characters_read);      if (characters_read!=strlen(string))         {         fprintf(stderr,"factor %s is not a float\n",string);         return true;         }      else         {         set_factor(argument,fact);         if (next==-1)            return true;         }      }   // position after comma   string=string+next;   // read acceleration   next=cut_out_till_next_comma(string);   if (next!=1)      {      int characters_read;      float accel;      sscanf(string,"%f%n",&accel,&characters_read);      if (characters_read!=strlen(string))         {         fprintf(stderr,"acceleration %s is not a float\n",string);         return true;         }      else         {         set_acceleration(argument,accel);         if (next==-1)            return true;         }      }   // position after comma   string=string+next;   // wheel/zero_on_release information   next=cut_out_till_next_comma(string);   if (next!=1)      {      if (strcmp(string,"wheel")==0)         set_zero_on_release(argument,false);      else         {         fprintf(stderr,"%s is not the string \"wheel\"\n",string);         return true;         }      if (next==-1)         return true;      }   // position after comma   string=string+next;   // read acceleration   next=cut_out_till_next_comma(string);   if (next!=1)      {      int characters_read;      float ignore;      sscanf(string,"%f%n",&ignore,&characters_read);      if (characters_read!=strlen(string))         {         fprintf(stderr,"zero_size_fraction %s is not a float\n",string);         return true;         }      else         {         set_zero_size_fraction(argument,ignore);         if (next==-1)            return true;         }      }   // position after comma   string=string+next;   fprintf(stderr,"argument %s not understood\n",string);   return true;   }static char* remove_blanks(char* stringIn)   {   char* string=(char*)malloc(strlen(stringIn)+1);   if (string==NULL)       {      perror("in remove_blanks");      return string;      }   // erase whitespaces of ASCII string   string[0]=(char)0;   int stringindex=0;   for (int i=0;i<strlen(stringIn);i++)      {      if ((unsigned char)stringIn[i]>' ')         {         string[stringindex]=stringIn[i];         stringindex++;         string[stringindex]=(char)0;         }      }   return string;   }    void InputDevice::SetAxisInformation(char* info_string)   {   int i;   char* freestring=remove_blanks(info_string);   if (freestring==NULL) return;   char* string=freestring;      if (string[0]=='-')       string++;   bool found=false;   for (i=0;i<sizeof(axesinfo)/sizeof(char*);i++)      {      if (stringncmp(string,axesinfo[i])==0)         {         found=scan_argument(string,axesinfo[i]);         break;         }      }   if (stringncmp(string,"all")==0)      if (stringncmp(string,"allrot")==0)         found=scan_argument(string,"allrot");      else if (stringncmp(string,"allxyz")==0)         found=scan_argument(string,"allxyz");      else          found=scan_argument(string,"all");   if (!found)      {         fprintf(stderr,"%s do not contain",string);      for (i=0;i<sizeof(axesinfo)/sizeof(char*);i++)         fprintf(stderr," -%s or",axesinfo[i]);      fprintf(stderr," -all=wheel\n",info_string);      }   free(freestring);   return;   }// set number of axes// only valid to reduce axes from a bad designed joystickvoid InputDevice::SetNumberAxes(char* argument)   {   char* freestring=remove_blanks(argument);   if (freestring==NULL) return;   char* string=freestring;      if (stringncmp(string,"-axes")!=0)      {      fprintf(stderr,"internal error in InputDevice::SetNumberAxes\n");      return;      }   string=string+strlen("-axes");   if (string[0]=='=')       string++;   int new_number_axes;   if (string2int(new_number_axes,string))         if (new_number_axes<=number_axes)         number_axes=new_number_axes;      else         fprintf(stderr,"only %d axes found, can not decrease to %d\n",                 number_axes,new_number_axes);   free(freestring);   return;         }// Drivers for joystick-like devices// Linux Joystick driver of Linux 2.2 or Linux 2.4/* * based on jstest.c  Version 1.2 * * Copyright (c) 1996-1999 Vojtech Pavlik * * Sponsored by SuSE * * modified by J. "MUFTI" Scheurich 2001-2002 for white_dune * This program can be used to test [..] the features of the Linux * joystick API, including non-blocking [..] access [..].  * It is also intended to * serve as an example implementation for those who wish to learn * how to write their own joystick using applications. */#ifdef LINUX_JOYSTICK # include <sys/ioctl.h># include <sys/time.h># include <sys/types.h># include <fcntl.h># include <unistd.h># include <errno.h>  /* * initialise joystick  * argument "device" is the joystickdevice, eg. "/dev/input/js0" or "/dev/js0"  */ linux_joystick::linux_joystick(char *device) : InputDevice()   {   if ((fd = open(device, O_RDONLY)) < 0)       {      fprintf(stderr,"joystickdevice %s ",device);      perror("joystick initialisation failed: ");      }    factor=0.1;   rotfactor=0.1;   number_axes = 2;   number_buttons = 2;   version = 0x000800;   strcpy(name,"Unknown");   ioctl(fd, JSIOCGVERSION, &version);   ioctl(fd, JSIOCGAXES, &number_axes);   ioctl(fd, JSIOCGBUTTONS, &number_buttons);   ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);   fcntl(fd, F_SETFL, O_NONBLOCK);   if (number_buttons>0)      {       button=new bool[number_buttons];      for (int i=0;i<number_buttons;i++)         button[i]=false;      } # ifdef DEBUG   fprintf(stderr,"Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",           name, number_axes, number_buttons, version >> 16, (version >> 8) & 0xff,            version & 0xff);# endif   }linux_joystick::~linux_joystick()   {   close(fd);   }bool linux_joystick::readInputDevice(void)   {   bool ret=false;   button_pressed=-1;   button_released=-1;   while (read(fd, &js, sizeof(struct js_event)) == sizeof(struct js_event))        {# ifdef DEBUG       fprintf(strerr,"Event: type %d, time %d, number %d, value %d\n", 	     js.type, js.time, js.number, js.value);# endif      switch(js.type & ~JS_EVENT_INIT)          {         case JS_EVENT_BUTTON:            ret=true;            if (js.value)               {               button[js.number] = true;               button_pressed=js.number;               }            else               {               button[js.number] = false;               button_released=js.number;               }            break;         case JS_EVENT_AXIS:            ret=true;            if (js.number==num_axis_x)               value[0] =  js.value/maxvalue(js.number)*factor;            if (js.number==num_axis_y)               value[1] = -js.value/maxvalue(js.number)*factor;            if (js.number==num_axis_z)               value[2] = -js.value/maxvalue(js.number)*factor;            if (js.number==num_axis_xrot)               value[3] = js.value/maxvalue(js.number)*rotfactor;            if (js.number==num_axis_yrot)               value[4] = js.value/maxvalue(js.number)*rotfactor;            if (js.number==num_axis_zrot)               value[5] = js.value/maxvalue(js.number)*rotfactor;            break;         }      if ((button_pressed!=-1) || (button_released!=-1))         break;      }   if (ret==false)      if ((value[0]!=0) || (value[1]!=0) || (value[2]!=0) ||           (value[3]!=0) || (value[4]!=0) || (value[5]!=0))         {         // deliver constantly values         ret=true;               }   return ret;   } # ifdef TEST_JOYSTICKint main(int argc,char** argv)   {   linux_joystick js=linux_joystick("/dev/input/js0");   while(1)      if (js.readInputDevice())         printf("%f %f %f\n",js.get_x(),js.get_y(),js.get_z());   }# endif#endif// Xinput driver #ifdef HAVE_XINPUT /*  * initialise XInput   * argument "device" is the name of the device, eg. "magellan"   */ xinput::xinput(char *device) : InputDevice()   {   int i;   number_buttons=0;   number_axes=0;   factor=1;   rotfactor=1;   max_value=NULL;   swxinput=(swXinput*) malloc(sizeof(swXinput));   swInitXinputDevice(swxinput,device);   if (swxinput->xinput==NULL)      {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -