OSDLMouseHandler.cc

Go to the documentation of this file.
00001 #include "OSDLMouseHandler.h"
00002 
00003 #include "OSDLMouse.h"             // for Mouse
00004 #include "OSDLController.h"        // for Controller
00005 
00006 #include "SDL.h"                   // for SDL_Getmicetate, etc.
00007 
00008 
00009 
00010 using std::string ;
00011 using std::list ;
00012 
00013 
00014 using namespace Ceylan::Log ;      // for LogPlug
00015 using namespace OSDL::Events ;
00016 
00017 
00018 #ifdef OSDL_USES_CONFIG_H
00019 #include <OSDLConfig.h>            // for OSDL_VERBOSE_MOUSE_HANDLER and al
00020 #endif // OSDL_USES_CONFIG_H
00021 
00022 
00023 
00024 #if OSDL_VERBOSE_MOUSE_HANDLER
00025 
00026 #include <iostream>
00027 #define OSDL_MOUSE_HANDLER_LOG( message ) std::cout << "[OSDL Mouse Handler] " << message << std::endl ;
00028 
00029 #else // OSDL_VERBOSE_MOUSE_HANDLER
00030 
00031 #define OSDL_MOUSE_HANDLER_LOG( message )
00032 
00033 #endif // OSDL_VERBOSE_MOUSE_HANDLER
00034 
00035 
00036 
00037 
00038 MouseHandler::MouseHandler( bool useClassicalMice )
00039         throw( InputDeviceHandlerException ) :
00040     InputDeviceHandler(),
00041     _miceCount( 0 ),
00042     _mice( 0 ),
00043     _useClassicalMice( useClassicalMice )
00044 {
00045 
00046     send( "Initializing Mouse subsystem." ) ;
00047 
00048     // No SDL_InitSubSystem for mice.
00049 
00050     _miceCount = GetAvailableMiceCount() ;
00051     
00052     _mice = new Mouse *[ _miceCount ] ;
00053     
00054     for ( MouseNumber i = 0; i < _miceCount ; i++ )
00055         _mice[i] = new Mouse( _useClassicalMice, i ) ;
00056     
00057     send( "Mouse subsystem initialized." ) ;
00058 
00059     dropIdentifier() ;
00060     
00061 }
00062 
00063 
00064 MouseHandler::~MouseHandler() throw()
00065 {
00066 
00067     send( "Stopping Mouse subsystem." ) ;
00068 
00069     // Mouse instances are owned by the handler :
00070     blank() ;
00071     
00072     // No SDL_QuitSubSystem for Mouse.
00073     
00074     send( "Mouse subsystem stopped." ) ;
00075         
00076 }
00077 
00078 
00079 void MouseHandler::linkToController( OSDL::MVC::Controller & controller )
00080     throw( MouseException )
00081 {
00082     
00083     linkToController( DefaultMouse, controller ) ;
00084             
00085 }
00086 
00087                     
00088 void MouseHandler::linkToController( MouseNumber mouseIndex,
00089     OSDL::MVC::Controller & controller ) throw( MouseException )
00090 {
00091     
00092     /*
00093      * Beware, mouse list might be out of synch, test is not exactly
00094      * equivalent to _miceCount.
00095      *
00096      */ 
00097     if ( mouseIndex >= static_cast<MouseNumber>( GetAvailableMiceCount() ) )
00098         throw MouseException( "MouseHandler::linkToController : index " 
00099             + Ceylan::toString( mouseIndex ) + " out of bounds (" 
00100             + Ceylan::toString( GetAvailableMiceCount() )
00101             + " mice attached)." ) ;
00102             
00103     if ( mouseIndex >= _miceCount )
00104         throw MouseException( "MouseHandler::linkToController : index " 
00105             + Ceylan::toString( mouseIndex ) + " out of bounds (" 
00106             + Ceylan::toString( _miceCount )
00107             + " mice attached according to internal mouse list)." ) ;
00108     
00109 #if OSDL_DEBUG
00110     if ( _mice[ mouseIndex ] == 0 )
00111         throw MouseException( "MouseHandler::linkToController : "
00112             "no known mouse for index " 
00113             + Ceylan::toString( mouseIndex ) + "." ) ;
00114 #endif // OSDL_DEBUG
00115 
00116     _mice[ mouseIndex ]->setController( controller ) ;
00117 
00118 }
00119     
00120     
00121 const string MouseHandler::toString( Ceylan::VerbosityLevels level ) 
00122     const throw()
00123 {
00124 
00125     string res = "Mouse handler " ;
00126     
00127     switch( _miceCount )
00128     {
00129     
00130         case 0:
00131             res += "does not manage any mouse" ;
00132             break ;
00133             
00134         case 1:
00135             res += "managing one mouse" ;
00136             break ;
00137         
00138         default:    
00139             res += "managing " + Ceylan::toNumericalString( _miceCount ) 
00140             + " mice" ;
00141             
00142     }
00143     
00144     if ( level == Ceylan::low )     
00145         return res ;
00146     
00147     res += ". Listing detected mice : " ;
00148         
00149     list<string> mice ;
00150         
00151     for ( MouseNumber i = 0; i < _miceCount; i++ )
00152         if ( _mice[i] != 0 )
00153             mice.push_back( _mice[i]->toString( level ) ) ;
00154         else
00155             mice.push_back( "no mouse at index " + Ceylan::toString( i ) 
00156                 + " (abnormal)" ) ;
00157             
00158     return res + Ceylan::formatStringList( mice ) ; 
00159         
00160 }
00161 
00162 
00163 MouseNumber MouseHandler::GetAvailableMiceCount() throw() 
00164 {
00165 
00166     /*
00167      * No way of guessing with SDL 1.2.x :
00168      *
00169      */
00170     return static_cast<MouseNumber>( 1 ) ;
00171     
00172 }
00173 
00174 
00175 
00176 
00177 // Protected section.
00178 
00179 
00180 
00181 void MouseHandler::focusGained( const FocusEvent & mouseFocusEvent ) 
00182     const throw()
00183 {
00184      
00185 #if OSDL_DEBUG
00186     checkMouseAt( DefaultMouse ) ;
00187 #endif // OSDL_DEBUG
00188     
00189     _mice[ DefaultMouse ]->focusGained( mouseFocusEvent ) ;
00190     
00191 }
00192 
00193 
00194 void MouseHandler::focusLost( const FocusEvent & mouseFocusEvent ) const throw()
00195 {
00196      
00197 #if OSDL_DEBUG
00198     checkMouseAt( DefaultMouse ) ;
00199 #endif // OSDL_DEBUG
00200     
00201     _mice[ DefaultMouse ]->focusLost( mouseFocusEvent ) ;
00202     
00203 }
00204 
00205 
00206 void MouseHandler::mouseMoved( const MouseMotionEvent & mouseMovedEvent ) 
00207     const throw()
00208 {
00209      
00210 #if OSDL_DEBUG
00211     checkMouseAt( mouseMovedEvent.which ) ;
00212 #endif // OSDL_DEBUG
00213     
00214     _mice[ mouseMovedEvent.which ]->mouseMoved( mouseMovedEvent ) ;
00215     
00216 }
00217 
00218 
00219 void MouseHandler::buttonPressed( 
00220         const MouseButtonEvent & mouseButtonPressedEvent ) 
00221     const throw()
00222 {
00223      
00224 #if OSDL_DEBUG
00225     checkMouseAt( mouseButtonPressedEvent.which ) ;
00226 #endif // OSDL_DEBUG
00227     
00228     _mice[ mouseButtonPressedEvent.which ]->buttonPressed(
00229         mouseButtonPressedEvent ) ;
00230     
00231 }
00232 
00233 
00234 void MouseHandler::buttonReleased( 
00235         const MouseButtonEvent & mouseButtonReleasedEvent ) 
00236     const throw()
00237 {
00238      
00239 #if OSDL_DEBUG
00240     checkMouseAt( mouseButtonReleasedEvent.which ) ;
00241 #endif // OSDL_DEBUG
00242     
00243     _mice[ mouseButtonReleasedEvent.which ]->buttonReleased(
00244         mouseButtonReleasedEvent ) ;
00245     
00246 }
00247 
00248 
00249 
00250 void MouseHandler::blank() throw()
00251 {
00252 
00253     if ( _mice != 0 )
00254     {
00255 
00256         for ( MouseNumber c = 0; c < _miceCount; c++ )
00257         {
00258             delete _mice[c] ;
00259         }
00260 
00261         delete [] _mice ;
00262         _mice = 0 ;
00263     }
00264     
00265     _miceCount = 0 ;
00266     
00267 }
00268 
00269 
00270 void MouseHandler::checkMouseAt( MouseNumber index ) const throw()
00271 {
00272 
00273     if ( index >= _miceCount )
00274         Ceylan::emergencyShutdown( 
00275             "MouseHandler::checkMouseAt : index "
00276             + Ceylan::toNumericalString( index ) 
00277             + " out of bounds (maximum value is "
00278             + Ceylan::toNumericalString( _miceCount - 1 ) + ")." ) ; 
00279         
00280     if ( _mice[ index ] == 0 )
00281         Ceylan::emergencyShutdown( "MouseHandler::checkMouseAt : "
00282             "no mouse intance at index "
00283             + Ceylan::toNumericalString( index ) + "." ) ;
00284     
00285 }
00286 

Generated on Fri Mar 30 14:46:59 2007 for OSDL by  doxygen 1.5.1