00001 /******************************************************************** 00002 Description: joystick plug-in 00003 part of the 3Dsia project 00004 created: StonedBones, 26.2.2000 00005 00006 History: 00007 date, name, changes, in funtion 00008 260200 StonedBones created source 00009 00010 ********************************************************************/ 00011 00012 00013 00014 #include "inputDeviceJoystick.h" 00015 00016 #include <unistd.h> 00017 #include <stdio.h> 00018 #include <linux/joystick.h> 00019 #include <sys/ioctl.h> 00020 #include <fcntl.h> 00021 #include <errno.h> 00022 #include <string.h> 00023 00024 int joyHandle; 00025 bool isPresent; 00026 struct js_event jsEvent; 00027 int intAxes; 00028 int intButtons; 00029 long intAxis[MAXAXES]; 00030 long maxAxis[MAXAXES]; 00031 bool intButton[MAXBUTTONS]; 00032 string intName; 00033 00034 /*................................................................... 00035 Description: returns the actual plugin 00036 Args: <none> 00037 Returns: plug-in pointer 00038 Created: StonedBones, 26.2.2000 00039 [ToDo:] 00040 [Comments:] 00041 Changes: 00042 -------------------------------------------------------------------*/ 00043 00044 extern "C" {DevicePlugIn* GetDevicePlugIn (void); }; 00045 00046 DevicePlugIn* GetDevicePlugIn () { 00047 return &inputPlugIn; 00048 }; 00049 00050 /*................................................................... 00051 Description: opens a joystick device 00052 Args: char* device: device-filename 00053 Returns: true on succes 00054 Created: StonedBones, 26.2.2000 00055 [ToDo:] 00056 [Comments:] 00057 Changes: 00058 -------------------------------------------------------------------*/ 00059 00060 bool openDevice () { 00061 for (int i = 1; i <= MAXAXES; i++) { 00062 intAxis[i]=0; 00063 maxAxis[i]=1; 00064 }; 00065 00066 joyHandle = open ("/dev/js0", O_RDONLY | O_NONBLOCK); 00067 00068 if (joyHandle) { 00069 isPresent = true; 00070 } else { 00071 isPresent = false; 00072 return false; 00073 }; 00074 00075 char tmp[128]; 00076 00077 ioctl (joyHandle, JSIOCGAXES, &intAxes); 00078 ioctl (joyHandle, JSIOCGBUTTONS, &intButtons); 00079 ioctl (joyHandle, JSIOCGNAME(128), tmp); 00080 00081 intName.assign (tmp); 00082 00083 for (int i=0; i < intAxes; i++) { 00084 intAxis[i]=0; 00085 }; 00086 for (int i=0; i < intButtons; i++) { 00087 intButton[i]=false; 00088 }; 00089 00090 update (); 00091 return true; 00092 }; 00093 00094 /*................................................................... 00095 Description: closes device if open 00096 Args: <none> 00097 Returns: <none> 00098 Created: StonedBones, 26.2.2000 00099 [ToDo:] 00100 [Comments:] 00101 Changes: 00102 -------------------------------------------------------------------*/ 00103 00104 void closeDevice () { 00105 close (joyHandle); 00106 return; 00107 }; 00108 00109 /*................................................................... 00110 Description: updates axis(int nr) and buttonPressed (int nr) 00111 Args: <none> 00112 Returns: true on success 00113 Created: StonedBones, 26.2.2000 00114 [ToDo:] 00115 [Comments:] 00116 Changes: 00117 -------------------------------------------------------------------*/ 00118 00119 bool update () { 00120 if (isPresent) { 00121 00122 read (joyHandle, &jsEvent, sizeof (struct js_event)); 00123 00124 switch (jsEvent.type) { 00125 case 1 : { if (jsEvent.value == 1) { 00126 intButton[jsEvent.number]=true; 00127 } else { 00128 intButton[jsEvent.number]=false; 00129 }; 00130 break; 00131 }; 00132 case 2 : { intAxis[jsEvent.number] = jsEvent.value; 00133 break; 00134 }; 00135 case 129: 00136 case 130: break; 00137 default: { printf ("unhandled InputDeviceJoystick event: type=%i, number=%i, value=%i\n", jsEvent.type, jsEvent.number, jsEvent.value); 00138 break; 00139 }; 00140 }; 00141 return true; 00142 } else return false; 00143 }; 00144 00145 /*................................................................... 00146 Description: returns the joystick name (given by kernel-module) 00147 Args: <none> 00148 Returns: NULL if no joystick was opened 00149 Created: StonedBones, 26.2.2000 00150 [ToDo:] 00151 [Comments:] 00152 Changes: 00153 -------------------------------------------------------------------*/ 00154 00155 string name () { 00156 if (isPresent) { 00157 return intName; 00158 } else return "none found"; 00159 }; 00160 00161 /*................................................................... 00162 Description: returns the amount of axes 00163 Args: <none> 00164 Returns: 0 if no joystick present 00165 Created: StonedBones, 26.2.2000 00166 [ToDo:] 00167 [Comments:] 00168 Changes: 00169 -------------------------------------------------------------------*/ 00170 00171 int axes () { 00172 if (isPresent) { 00173 return intAxes; 00174 } else return 0; 00175 }; 00176 00177 /*................................................................... 00178 Description: returns the amount of buttons 00179 Args: <none> 00180 Returns: 0 if no joystick present 00181 Created: StonedBones, 26.2.2000 00182 [ToDo:] 00183 [Comments:] 00184 Changes: 00185 -------------------------------------------------------------------*/ 00186 00187 int buttons () { 00188 if (isPresent) { 00189 return intButtons; 00190 } else return 0; 00191 }; 00192 00193 /*................................................................... 00194 Description: returns the value of axis 'nr' 00195 Args: int nr: axis-number 00196 Returns: axis-value 00197 Created: StonedBones, 26.2.2000 00198 [ToDo:] 00199 [Comments:] 00200 Changes: 00201 -------------------------------------------------------------------*/ 00202 00203 long axis (int nr) { 00204 if (isPresent) { 00205 if (nr < intAxes) { 00206 return (intAxis[nr]); 00207 } else { 00208 return 0; 00209 }; 00210 } else { 00211 return 0; 00212 }; 00213 }; 00214 00215 /*................................................................... 00216 Description: returns if a certain button is pressed 00217 Args: int nr: button-number 00218 Returns: true if pressed 00219 Created: StonedBones, 26.2.2000 00220 [ToDo:] 00221 [Comments:] 00222 Changes: 00223 -------------------------------------------------------------------*/ 00224 00225 bool buttonPressed (int nr) { 00226 if (isPresent) { 00227 if (nr < intButtons) { 00228 return intButton[nr]; 00229 } else { 00230 return false; 00231 }; 00232 } else { 00233 return false; 00234 }; 00235 }; 00236