00001 #include "OSDLClassicalJoystick.h" 00002 00003 #include "OSDLController.h" // for joystickAxisChanged, etc. 00004 00005 00006 using std::string ; 00007 00008 00009 using namespace Ceylan::Log ; 00010 00011 using namespace OSDL::Events ; 00012 00013 00014 const AxisPosition ClassicalJoystick::DefaultDeadZoneExtent = 100 ; 00015 00016 00017 #ifdef OSDL_USES_CONFIG_H 00018 #include <OSDLConfig.h> // for OSDL_VERBOSE_JOYSTICK and al 00019 #endif // OSDL_USES_CONFIG_H 00020 00021 00022 #if OSDL_ARCH_NINTENDO_DS 00023 #include "OSDLConfigForNintendoDS.h" // for OSDL_USES_SDL and al 00024 #endif // OSDL_ARCH_NINTENDO_DS 00025 00026 00027 00028 #if OSDL_USES_SDL 00029 00030 #include "SDL.h" // for CD-ROM primitives 00031 00032 #endif // OSDL_USES_SDL 00033 00034 00035 00036 #if OSDL_VERBOSE_JOYSTICK 00037 00038 #include <iostream> 00039 #define OSDL_JOYSTICK_LOG( message ) std::cout << "[OSDL Joystick] " << message << std::endl ; 00040 00041 #else // OSDL_VERBOSE_JOYSTICK 00042 00043 #define OSDL_JOYSTICK_LOG( message ) 00044 00045 #endif // OSDL_VERBOSE_JOYSTICK 00046 00047 00048 00049 /* 00050 * Not used currently since event loop is prefered to polling: 00051 * - SDL_JoystickUpdate 00052 * - SDL_JoystickEventState (used in OSDLJoysticksHandler) 00053 * 00054 */ 00055 00056 00057 ClassicalJoystick::ClassicalJoystick( 00058 JoystickNumber index, AxisPosition deadZoneExtent ) 00059 throw( JoystickException ): 00060 Joystick( index ), 00061 _deadZoneExtentFirstAxis( deadZoneExtent ), 00062 _deadZoneExtentSecondAxis( deadZoneExtent ) 00063 { 00064 00065 #if ! OSDL_USES_SDL 00066 00067 throw JoystickException( "ClassicalJoystick constructor failed: " 00068 "no SDL support available" ) ; 00069 00070 #endif // OSDL_USES_SDL 00071 00072 } 00073 00074 00075 ClassicalJoystick::~ClassicalJoystick() throw() 00076 { 00077 00078 } 00079 00080 00081 void ClassicalJoystick::getDeadZoneValues( AxisPosition & firstAxisExtent, 00082 AxisPosition & secondAxisExtent ) const throw() 00083 { 00084 00085 firstAxisExtent = _deadZoneExtentFirstAxis ; 00086 secondAxisExtent = _deadZoneExtentSecondAxis ; 00087 00088 } 00089 00090 00091 void ClassicalJoystick::setDeadZoneValues( AxisPosition firstAxisExtent, 00092 AxisPosition secondAxisExtent ) throw() 00093 { 00094 00095 _deadZoneExtentFirstAxis = firstAxisExtent ; 00096 _deadZoneExtentSecondAxis = secondAxisExtent ; 00097 00098 } 00099 00100 00101 const string ClassicalJoystick::toString( Ceylan::VerbosityLevels level ) 00102 const throw() 00103 { 00104 00105 return "Classical joystick: " + Joystick::toString( level ) 00106 + ". The first axis deadzone extent is " 00107 + Ceylan::toString( _deadZoneExtentFirstAxis ) 00108 + ", the second axis deadzone extent is " 00109 + Ceylan::toString( _deadZoneExtentSecondAxis ) ; 00110 00111 } 00112 00113 00114 00115 00116 // Protected section. 00117 00118 00119 void ClassicalJoystick::axisChanged( const JoystickAxisEvent & joystickEvent ) 00120 throw() 00121 { 00122 00123 #if OSDL_USES_SDL 00124 00125 if ( isLinkedToController() ) 00126 { 00127 00128 // If in deadzone, do not notify the controller. 00129 00130 if ( joystickEvent.axis == 0 ) 00131 { 00132 if ( joystickEvent.value > _deadZoneExtentFirstAxis ) 00133 getActualController().joystickRight( joystickEvent.value ) ; 00134 else 00135 { 00136 // -1 offset to avoid overflow (range: -32768 to 32767) 00137 AxisPosition pos = -1 - joystickEvent.value ; 00138 if ( pos > _deadZoneExtentFirstAxis ) 00139 getActualController().joystickLeft( pos ) ; 00140 } 00141 00142 return ; 00143 } 00144 00145 00146 if ( joystickEvent.axis == 1 ) 00147 { 00148 if ( joystickEvent.value > _deadZoneExtentSecondAxis ) 00149 getActualController().joystickDown( joystickEvent.value ) ; 00150 else 00151 { 00152 // -1 offset to avoid overflow (range: -32768 to 32767) 00153 AxisPosition pos = -1 - joystickEvent.value ; 00154 if ( pos > _deadZoneExtentSecondAxis ) 00155 getActualController().joystickUp( pos ) ; 00156 } 00157 00158 return ; 00159 } 00160 00161 } 00162 else 00163 { 00164 OSDL_JOYSTICK_LOG( 00165 "Axis changed for controller-less classical joystick #" 00166 + Ceylan::toNumericalString( joystickEvent.which ) + ": " 00167 + EventsModule::DescribeEvent( joystickEvent ) ) ; 00168 } 00169 00170 #endif // OSDL_USES_SDL 00171 00172 } 00173 00174 00175 00176 void ClassicalJoystick::buttonPressed( 00177 const JoystickButtonEvent & joystickEvent ) throw() 00178 { 00179 00180 #if OSDL_USES_SDL 00181 00182 if ( isLinkedToController() ) 00183 { 00184 00185 switch( joystickEvent.button ) 00186 { 00187 00188 case 0: 00189 getActualController().joystickFirstButtonPressed() ; 00190 break ; 00191 00192 case 1: 00193 getActualController().joystickSecondButtonPressed() ; 00194 break ; 00195 00196 default: 00197 OSDL_JOYSTICK_LOG( 00198 "Button (neither first or second, hence ignored) " 00199 "pressed for classical joystick #" 00200 + Ceylan::toNumericalString( joystickEvent.which ) + ": " 00201 + EventsModule::DescribeEvent( joystickEvent ) ) ; 00202 break ; 00203 } 00204 00205 00206 } 00207 else 00208 { 00209 OSDL_JOYSTICK_LOG( 00210 "Button pressed for controller-less classical joystick #" 00211 + Ceylan::toNumericalString( joystickEvent.which ) + ": " 00212 + EventsModule::DescribeEvent( joystickEvent ) ) ; 00213 } 00214 00215 #endif // OSDL_USES_SDL 00216 00217 } 00218 00219 00220 00221 void ClassicalJoystick::buttonReleased( 00222 const JoystickButtonEvent & joystickEvent ) throw() 00223 { 00224 00225 #if OSDL_USES_SDL 00226 00227 if ( isLinkedToController() ) 00228 { 00229 00230 switch( joystickEvent.button ) 00231 { 00232 00233 case 0: 00234 getActualController().joystickFirstButtonReleased() ; 00235 break ; 00236 00237 case 1: 00238 getActualController().joystickSecondButtonReleased() ; 00239 break ; 00240 00241 default: 00242 OSDL_JOYSTICK_LOG( 00243 "Button (neither first or second, hence ignored) " 00244 "released for classical joystick #" 00245 + Ceylan::toNumericalString( joystickEvent.which ) + ": " 00246 + EventsModule::DescribeEvent( joystickEvent ) ) ; 00247 break ; 00248 } 00249 00250 } 00251 else 00252 { 00253 00254 OSDL_JOYSTICK_LOG( 00255 "Button released for controller-less classical joystick #" 00256 + Ceylan::toNumericalString( joystickEvent.which ) + ": " 00257 + EventsModule::DescribeEvent( joystickEvent ) ) ; 00258 } 00259 00260 #endif // OSDL_USES_SDL 00261 00262 } 00263
1.5.5