00001 /******************************************************************** 00002 Description: keyboard plug-in 00003 part of the 3Dsia project 00004 created: StonedBones, 24.4.2000 00005 00006 History: 00007 date, name, changes, in funtion 00008 240400 StonedBones created source 00009 00010 ********************************************************************/ 00011 00012 00013 00014 #include "deviceKeyboard.h" 00015 00016 #include <string.h> 00017 #include <iostream> 00018 #include <iomanip> 00019 00020 bool isPresent; 00021 int intAxes; 00022 int intButtons; 00023 long intAxis[MAXAXES]; 00024 long maxAxis[MAXAXES]; 00025 bool intButton[MAXBUTTONS]; 00026 string intName; 00027 bool chatmode; 00028 bool keyunderway; 00029 bool keypassed; 00030 00031 /*................................................................... 00032 Description: returns the actual plugin 00033 Args: <none> 00034 Returns: plug-in pointer 00035 Created: StonedBones, 26.2.2000 00036 [ToDo:] 00037 [Comments:] 00038 Changes: 00039 -------------------------------------------------------------------*/ 00040 00041 extern "C" {DevicePlugIn* GetDevicePlugIn (void); }; 00042 00043 DevicePlugIn* GetDevicePlugIn () { 00044 return &inputPlugIn; 00045 }; 00046 00047 /*................................................................... 00048 Description: opens a joystick device 00049 Args: char* device: device-filename 00050 Returns: true on succes 00051 Created: StonedBones, 26.2.2000 00052 [ToDo:] 00053 [Comments:] 00054 Changes: 00055 -------------------------------------------------------------------*/ 00056 00057 bool openDevice () { 00058 intAxes =3; 00059 intButtons=6; 00060 intName ="glut-keyboard"; 00061 isPresent =true; 00062 chatmode = false; 00063 keyunderway = false; 00064 keypassed = false; 00065 00066 for (int i=0; i < intAxes; i++) { 00067 intAxis[i]=0; 00068 }; 00069 for (int i=0; i < intButtons; i++) { 00070 intButton[i]=false; 00071 }; 00072 00073 glutKeyboardFunc (keyboard); 00074 glutKeyboardUpFunc (keyboardUp); 00075 glutSpecialFunc (special); 00076 glutSpecialUpFunc (specialUp); 00077 00078 update (); 00079 00080 return true; 00081 }; 00082 00083 /*................................................................... 00084 Description: closes device if open 00085 Args: <none> 00086 Returns: <none> 00087 Created: StonedBones, 26.2.2000 00088 [ToDo:] 00089 [Comments:] 00090 Changes: 00091 -------------------------------------------------------------------*/ 00092 00093 void closeDevice () { 00094 return; 00095 }; 00096 00097 /*................................................................... 00098 Description: updates axis(int nr) and buttonPressed (int nr) 00099 Args: <none> 00100 Returns: true on success 00101 Created: StonedBones, 26.2.2000 00102 [ToDo:] 00103 [Comments:] 00104 Changes: 00105 -------------------------------------------------------------------*/ 00106 00107 bool update () { 00108 if (isPresent) { 00109 if (keypassed) { 00110 intAxis[2]=0; 00111 keypassed = false; 00112 keyunderway = false; 00113 }; 00114 if (keyunderway) { 00115 keypassed = true; 00116 }; 00117 return true; 00118 } else return false; 00119 }; 00120 00121 /*................................................................... 00122 Description: returns the joystick name (given by kernel-module) 00123 Args: <none> 00124 Returns: NULL if no joystick was opened 00125 Created: StonedBones, 26.2.2000 00126 [ToDo:] 00127 [Comments:] 00128 Changes: 00129 -------------------------------------------------------------------*/ 00130 00131 string name () { 00132 if (isPresent) { 00133 return intName; 00134 } else return "none found"; 00135 }; 00136 00137 /*................................................................... 00138 Description: returns the amount of axes 00139 Args: <none> 00140 Returns: 0 if no joystick present 00141 Created: StonedBones, 26.2.2000 00142 [ToDo:] 00143 [Comments:] 00144 Changes: 00145 -------------------------------------------------------------------*/ 00146 00147 int axes () { 00148 if (isPresent) { 00149 return intAxes; 00150 } else return 0; 00151 }; 00152 00153 /*................................................................... 00154 Description: returns the amount of buttons 00155 Args: <none> 00156 Returns: 0 if no joystick present 00157 Created: StonedBones, 26.2.2000 00158 [ToDo:] 00159 [Comments:] 00160 Changes: 00161 -------------------------------------------------------------------*/ 00162 00163 int buttons () { 00164 if (isPresent) { 00165 return intButtons; 00166 } else return 0; 00167 }; 00168 00169 /*................................................................... 00170 Description: returns the value of axis 'nr' 00171 Args: int nr: axis-number 00172 Returns: axis-value 00173 Created: StonedBones, 26.2.2000 00174 [ToDo:] 00175 [Comments:] 00176 Changes: 00177 -------------------------------------------------------------------*/ 00178 00179 long axis (int nr) { 00180 if (isPresent) { 00181 if (nr < intAxes) { 00182 return (intAxis[nr]); 00183 } else { 00184 return 0; 00185 }; 00186 } else { 00187 return 0; 00188 }; 00189 }; 00190 00191 /*................................................................... 00192 Description: returns if a certain button is pressed 00193 Args: int nr: button-number 00194 Returns: true if pressed 00195 Created: StonedBones, 26.2.2000 00196 [ToDo:] 00197 [Comments:] 00198 Changes: 00199 -------------------------------------------------------------------*/ 00200 00201 bool buttonPressed (int nr) { 00202 if (isPresent) { 00203 if (nr < intButtons) { 00204 return intButton[nr]; 00205 } else { 00206 return false; 00207 }; 00208 } else { 00209 return false; 00210 }; 00211 }; 00212 00213 00214 void keyboard (unsigned char key, int x, int y) { 00215 if (!chatmode) { 00216 intButton[0]=(key == 'f'); 00217 intButton[1]=(key == 'b'); 00218 intButton[2]=((key == 27) || (key == 'q') || (key == 'x')); 00219 intButton[3]=(key == '+'); 00220 intButton[4]=(key == '-'); 00221 } else { 00222 intAxis[2]=(long) key; 00223 keyunderway = true; 00224 }; 00225 }; 00226 00227 void keyboardUp (unsigned char key, int x, int y) { 00228 if (!chatmode) { 00229 switch (key) { 00230 case 'f' : { intButton[0] = false; 00231 break; 00232 }; 00233 case 'b' : { intButton[1] = false; 00234 break; 00235 }; 00236 case 27 : 00237 case 'q' : 00238 case 'x' : { intButton[2] = false; 00239 break; 00240 }; 00241 case '+' : { intButton[3] = false; 00242 break; 00243 }; 00244 case '-' : { intButton[4] = false; 00245 break; 00246 }; 00247 default : break; 00248 }; 00249 } else { 00250 intAxis[2]=0; 00251 }; 00252 }; 00253 00254 void special (int key, int x, int y) { 00255 if (key == GLUT_KEY_UP) { 00256 intAxis[0]=32767; 00257 } else if (key == GLUT_KEY_DOWN) { 00258 intAxis[0]=-32767; 00259 } else { 00260 intAxis[0]=0; 00261 }; 00262 00263 if (key == GLUT_KEY_LEFT) { 00264 intAxis[1]=32767; 00265 } else if (key == GLUT_KEY_RIGHT) { 00266 intAxis[1]=-32767; 00267 } else { 00268 intAxis[1]=0; 00269 }; 00270 00271 if (key == GLUT_KEY_F1) { 00272 intButton[5]=true; 00273 chatmode = !chatmode; 00274 } else { 00275 intButton[5]=false; 00276 }; 00277 }; 00278 00279 void specialUp (int key, int x, int y) { 00280 switch (key) { 00281 case GLUT_KEY_UP : 00282 case GLUT_KEY_DOWN: { intAxis[0] = 0; 00283 break; 00284 }; 00285 case GLUT_KEY_LEFT : 00286 case GLUT_KEY_RIGHT:{ intAxis[1] = 0; 00287 break; 00288 }; 00289 case GLUT_KEY_F1 : { intButton[5] = false; 00290 break; 00291 }; 00292 default : break; 00293 }; 00294 };