00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00032 #include "class_gamepad.h"
00033 
00034 class_gamepad::class_gamepad()
00035 {
00036         ret=0;
00037         fd=0;
00038         n_buttons=0;
00039         n_axes=0;
00040 }
00041 
00042 class_gamepad::~class_gamepad()
00043 {
00044         close(fd);
00045 }
00046 
00047 int class_gamepad::StartComm(const char*device)
00048 {
00049         fd = open(device, O_RDONLY | O_NONBLOCK);
00050         if(fd<0)
00051         {
00052                 sprintf(err,"Unable to open device: %s",device);
00053                 return -3;
00054         }
00055         
00056         ret=ioctl(fd, JSIOCGAXES, &n_axes);
00057         if(ret<0)
00058         {
00059                 strcpy(err,"Unable to read number of axes");
00060                 return -3;
00061         }
00062         
00063         ret=ioctl(fd, JSIOCGBUTTONS, &n_buttons);
00064         if(ret<0)
00065         {
00066                 strcpy(err,"Unable to read number of buttons");
00067                 return -3;
00068         }
00069         
00070         ret=ioctl(fd, JSIOCGNAME(sizeof(name)), name);
00071         if(ret<0)
00072                 strcpy(name,"Unknown");
00073         
00074         printf("\33[1m\33[36mGamepad\33[0m %s\n",name);
00075         
00076         buttons=(t_button*)malloc(n_buttons*sizeof(t_button));
00077         
00078         axes=(t_button*)malloc(n_axes*sizeof(t_button));
00079         
00080         for(int i=0;i<n_buttons;i++)
00081                 buttons[i].callback=NULL;
00082         
00083         for(int i=0;i<n_axes;i++)
00084                 axes[i].callback=NULL;
00085         
00086         return 0;
00087 }
00088 
00089 int class_gamepad::GetButtonMapping(void)
00090 {
00091         ret=ioctl(fd,JSIOCGAXMAP,&m_axes);
00092         if(ret<0)
00093         {
00094                 strcpy(err,"Unable to read axes mapping");
00095                 return -3;
00096         }
00097         
00098         ret=ioctl(fd,JSIOCGBTNMAP,&m_buttons);
00099         if(ret<0)
00100         {
00101                 strcpy(err,"Unable to read buttons mapping");
00102                 return -3;
00103         }
00104         
00105         return 0;       
00106 }
00107 
00108 int class_gamepad::SetButtonMapping(void)
00109 {
00110         ret=ioctl(fd,JSIOCSAXMAP,&m_axes);
00111         if(ret<0)
00112         {
00113                 strcpy(err,"Unable to set axes mapping");
00114                 return -3;
00115         }
00116         
00117         ret=ioctl(fd,JSIOCSBTNMAP,&m_buttons);
00118         if(ret<0)
00119         {
00120                 strcpy(err,"Unable to set buttons mapping");
00121                 return -3;
00122         }
00123         
00124         return 0;       
00125 }
00126 
00127 int class_gamepad::RegisterCallback(e_type type,int id,void (*callback)(int value,void*data),void*data)
00128 {
00129         if(type==AXIS)
00130         {
00131                 if(id>n_axes)
00132                 {
00133                         sprintf(err,"This device does not have this axis: Axis %d",id);
00134                         return -2;
00135                 }
00136                 
00137                 axes[id].callback=callback;
00138                 axes[id].userdata=data;
00139                 
00140         }else if(type==BUTTON)
00141         {
00142                 if(id>n_buttons)
00143                 {
00144                         sprintf(err,"This device does not have this Button: Button %d",id);
00145                         return -2;
00146                 }
00147                 
00148                 buttons[id].callback=callback;
00149                 buttons[id].userdata=data;
00150         }
00151         
00152         
00153         return 0;
00154 }
00155 
00156 int class_gamepad::Dispatch(bool debug)
00157 {
00158         struct js_event buffer[64];
00159         
00160         ret = read (fd, buffer, sizeof(struct js_event)*64);
00161         if(ret<0)
00162                 return 0;
00163         
00164         for(int i=0; i<ret/(signed int)sizeof(struct js_event);i++)
00165         {
00166                 if(buffer[i].type & JS_EVENT_BUTTON & ~JS_EVENT_INIT)
00167                 {
00168                         if(debug)printf("Button %d Value %d\n",buffer[i].number,buffer[i].value);
00169                         if(buttons[buffer[i].number].callback==NULL)
00170                                 continue;
00171                         
00172                         buttons[buffer[i].number].value = buffer[i].value;
00173                         buttons[buffer[i].number].callback(buttons[buffer[i].number].value,buttons[buffer[i].number].userdata);
00174                 }else if(buffer[i].type == JS_EVENT_AXIS)
00175                 {
00176                         if(debug)printf("Axis %d Value %d\n",buffer[i].number,buffer[i].value);
00177                         if(axes[buffer[i].number].callback==NULL)
00178                                 continue;
00179                         
00180                         axes[buffer[i].number].value = buffer[i].value;
00181                         axes[buffer[i].number].callback(axes[buffer[i].number].value,axes[buffer[i].number].userdata);
00182                 }
00183         }
00184         
00185         return 0;
00186 }
00187 
00188 void class_gamepad::plerr(int ret)
00189 {
00190         if(ret==-1)
00191         {
00192                 printf("Error!!   ");
00193                 printf("%s (raising SIGINT)\n",err);
00194                 raise(SIGINT);
00195         }else if(ret==-2)
00196         {
00197                 printf("Warning!! ");
00198                 printf("%s\n",err);
00199         }else if(ret==-3)
00200         {
00201                 printf("Error!!   ");
00202                 printf("%s, ",err);fflush(stdout);
00203                 perror(NULL);
00204                 raise(SIGINT);
00205         }
00206 }