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
00027
00028 #include "OSDLEvents.h"
00029
00030 #include "OSDLBasic.h"
00031 #include "OSDLVideo.h"
00032 #include "OSDLScheduler.h"
00033 #include "OSDLRenderer.h"
00034 #include "OSDLJoystickHandler.h"
00035 #include "OSDLKeyboardHandler.h"
00036 #include "OSDLMouseHandler.h"
00037 #include "OSDLMouseCommon.h"
00038
00039
00040
00041 #include "Ceylan.h"
00042
00043 #include <list>
00044
00045
00046
00047 #ifdef OSDL_USES_CONFIG_H
00048 #include <OSDLConfig.h>
00049 #endif // OSDL_USES_CONFIG_H
00050
00051 #if OSDL_ARCH_NINTENDO_DS
00052 #include "OSDLConfigForNintendoDS.h"
00053 #endif // OSDL_ARCH_NINTENDO_DS
00054
00055
00056
00057 #if OSDL_USES_SDL
00058
00059 #include "SDL.h"
00060
00061 #endif // OSDL_USES_SDL
00062
00063
00064
00065 #if OSDL_USES_AGAR
00066
00067 #include <agar/config/have_opengl.h>
00068
00069 #include <agar/core.h>
00070 #include <agar/gui.h>
00071 #include <agar/gui/opengl.h>
00072
00073 #include <agar/core/types.h>
00074 #include <agar/gui/cursors.h>
00075 #include <agar/gui/drv_sdl_common.h>
00076 #include <agar/core/close_types.h>
00077
00078 #endif // OSDL_USES_AGAR
00079
00080
00081
00082 using std::string ;
00083 using Ceylan::Maths::Hertz ;
00084
00085
00086 using namespace Ceylan ;
00087 using namespace Ceylan::Log ;
00088
00089
00090
00091 using namespace Ceylan::System ;
00092
00093
00094 using namespace OSDL::Events ;
00095 using namespace OSDL::Engine ;
00096
00097
00098
00099
00100
00101
00102 #if OSDL_VERBOSE_EVENTS_MODULE
00103
00104 #define LOG_DEBUG_EVENTS(message) LogPlug::debug(message)
00105 #define LOG_TRACE_EVENTS(message) LogPlug::trace(message)
00106 #define LOG_WARNING_EVENTS(message) LogPlug::warning(message)
00107
00108 #else // OSDL_DEBUG_AUDIO_PLAYBACK
00109
00110 #define LOG_DEBUG_EVENTS(message)
00111 #define LOG_TRACE_EVENTS(message)
00112 #define LOG_WARNING_EVENTS(message)
00113
00114 #endif // OSDL_DEBUG_AUDIO_PLAYBACK
00115
00116
00117
00118
00119 const std::string NoSDLSupportAvailable = "(no SDL support available)" ;
00120
00121
00122 bool EventsModule::_EventsInitialized = false ;
00123
00124
00125
00126
00127 #if OSDL_USES_SDL
00128
00129
00130
00131
00132
00133 const BasicEventType EventsModule::ApplicationFocusChanged =
00134 SDL_ACTIVEEVENT ;
00135
00136
00137 const BasicEventType EventsModule::KeyPressed =
00138 SDL_KEYDOWN ;
00139
00140
00141 const BasicEventType EventsModule::KeyReleased =
00142 SDL_KEYUP ;
00143
00144
00145 const BasicEventType EventsModule::MouseMoved =
00146 SDL_MOUSEMOTION ;
00147
00148
00149 const BasicEventType EventsModule::MouseButtonPressed =
00150 SDL_MOUSEBUTTONDOWN ;
00151
00152
00153 const BasicEventType EventsModule::MouseButtonReleased =
00154 SDL_MOUSEBUTTONUP ;
00155
00156
00157 const BasicEventType EventsModule::JoystickAxisChanged =
00158 SDL_JOYAXISMOTION ;
00159
00160 const BasicEventType EventsModule::JoystickTrackballChanged =
00161 SDL_JOYBALLMOTION ;
00162
00163
00164 const BasicEventType EventsModule::JoystickHatPositionChanged =
00165 SDL_JOYHATMOTION ;
00166
00167
00168 const BasicEventType EventsModule::JoystickButtonPressed =
00169 SDL_JOYBUTTONDOWN ;
00170
00171
00172 const BasicEventType EventsModule::JoystickButtonReleased =
00173 SDL_JOYBUTTONUP ;
00174
00175
00176 const BasicEventType EventsModule::UserRequestedQuit =
00177 SDL_QUIT ;
00178
00179
00180 const BasicEventType EventsModule::SystemSpecificTriggered =
00181 SDL_SYSWMEVENT ;
00182
00183
00184 const BasicEventType EventsModule::UserResizedVideoMode =
00185 SDL_VIDEORESIZE ;
00186
00187
00188 const BasicEventType EventsModule::ScreenNeedsRedraw =
00189 SDL_VIDEOEXPOSE ;
00190
00191
00192 const BasicEventType EventsModule::FirstUserEventTriggered =
00193 SDL_USEREVENT ;
00194
00195
00196
00197 const BasicEventType EventsModule::LastUserEventTriggered =
00198 SDL_NUMEVENTS - 1 ;
00199
00200
00201
00202
00203 const Ceylan::Sint16 EventsModule::_MouseFocus = SDL_APPMOUSEFOCUS ;
00204 const Ceylan::Sint16 EventsModule::_KeyboardFocus = SDL_APPINPUTFOCUS ;
00205 const Ceylan::Sint16 EventsModule::_ApplicationFocus = SDL_APPACTIVE ;
00206
00207
00208
00209
00211 const string EventsModule::_SDLEnvironmentVariables[] =
00212 {
00213 "SDL_MOUSE_RELATIVE",
00214 "SDL_MOUSEDEV",
00215 "SDL_MOUSEDEV_IMPS2",
00216 "SDL_MOUSEDRV",
00217 "SDL_NO_RAWKBD",
00218 "SDL_NOMOUSE",
00219 "SDL_JOYSTICK_DEVICE",
00220 "SDL_LINUX_JOYSTICK"
00221 } ;
00222
00223
00224
00225
00226 #else // OSDL_USES_SDL
00227
00228
00229
00230
00231
00232 const BasicEventType EventsModule::ApplicationFocusChanged = 1 ;
00233 const BasicEventType EventsModule::KeyPressed = 2 ;
00234 const BasicEventType EventsModule::KeyReleased = 3 ;
00235 const BasicEventType EventsModule::MouseMoved = 4 ;
00236 const BasicEventType EventsModule::MouseButtonPressed = 5 ;
00237 const BasicEventType EventsModule::MouseButtonReleased = 6 ;
00238 const BasicEventType EventsModule::JoystickAxisChanged = 7 ;
00239 const BasicEventType EventsModule::JoystickTrackballChanged = 8 ;
00240 const BasicEventType EventsModule::JoystickHatPositionChanged = 9 ;
00241 const BasicEventType EventsModule::JoystickButtonPressed = 10 ;
00242 const BasicEventType EventsModule::JoystickButtonReleased = 11 ;
00243 const BasicEventType EventsModule::UserRequestedQuit = 12 ;
00244 const BasicEventType EventsModule::SystemSpecificTriggered = 13 ;
00245
00246
00247 const BasicEventType EventsModule::UserResizedVideoMode = 16 ;
00248 const BasicEventType EventsModule::ScreenNeedsRedraw = 17 ;
00249
00250 const BasicEventType EventsModule::FirstUserEventTriggered = 24 ;
00251
00252
00253 const BasicEventType EventsModule::LastUserEventTriggered = 31 ;
00254
00255
00256
00257 const Ceylan::Sint16 EventsModule::_MouseFocus = 0x01 ;
00258 const Ceylan::Sint16 EventsModule::_KeyboardFocus = 0x02 ;
00259 const Ceylan::Sint16 EventsModule::_ApplicationFocus = 0x04 ;
00260
00262 const string EventsModule::_SDLEnvironmentVariables[] = {} ;
00263
00264
00265 #endif // OSDL_USES_SDL
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 const UserEventType EventsModule::NoEvent = 0 ;
00280 const UserEventType EventsModule::QuitRequested = 1 ;
00281
00282
00283
00284
00285 const string EventsModule::_MessageHeader = "[OSDL event] " ;
00286
00287
00288
00289
00290
00292 const Ceylan::Uint32 EventsModule::_MouseButtonCount = 5 ;
00293
00294
00295
00296 EventsModule::EventsModule( Flags eventsFlag ) :
00297 Ceylan::Module(
00298 "OSDL events module",
00299 "This is the module of OSDL dedicated to events management",
00300 "http://osdl.sourceforge.net",
00301 "Olivier Boudeville",
00302 "olivier.boudeville@online.fr",
00303 OSDL::GetVersion(),
00304 "disjunctive LGPL/GPL" ),
00305 _useScheduler( false ),
00306 _isGuiEnabled( false ),
00307 _keyboardHandler( 0 ),
00308 _joystickHandler( 0 ),
00309 _mouseHandler( 0 ),
00310 _quitRequested( false ),
00311 _loopTargetedFrequency( DefaultEventLoopTargetedFrequency ),
00312 _idleCallsCount( 0 ),
00313 _loopIdleCallback( 0 ),
00314 _loopIdleCallbackData( 0 )
00315 {
00316
00317 send( "Initializing events subsystem." ) ;
00318
00319
00320
00321
00322 if ( eventsFlag & CommonModule::UseJoystick )
00323 {
00324 _joystickHandler = new JoystickHandler(
00325 true ) ;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 _keyboardHandler = new KeyboardHandler( Events::rawInput,
00338 false ) ;
00339
00340
00341
00342
00343
00344
00345
00346
00347 _mouseHandler = new MouseHandler( true ) ;
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 send( "Events subsystem initialized." ) ;
00358
00359 dropIdentifier() ;
00360
00361 }
00362
00363
00364
00365 EventsModule::~EventsModule() throw()
00366 {
00367
00368 send( "Stopping events subsystem." ) ;
00369
00370 if ( _joystickHandler != 0 )
00371 {
00372 delete _joystickHandler ;
00373 _joystickHandler = 0 ;
00374 }
00375
00376 if ( _keyboardHandler != 0 )
00377 {
00378 delete _keyboardHandler ;
00379 _keyboardHandler = 0 ;
00380 }
00381
00382 if ( _mouseHandler != 0 )
00383 {
00384 delete _mouseHandler ;
00385 _mouseHandler = 0 ;
00386 }
00387
00388 send( "Stopping and deleting any existing scheduler" ) ;
00389 Scheduler::DeleteScheduler() ;
00390
00391 send( "Events subsystem stopped." ) ;
00392
00393 }
00394
00395
00396
00397 void EventsModule::waitForAnyKey( bool displayWaitingMessage ) const
00398 {
00399
00400 #if OSDL_USES_SDL
00401
00402
00403
00404
00405
00406
00407
00408 BasicEvent currentEvent ;
00409
00410 if ( displayWaitingMessage )
00411 {
00412 Ceylan::display( "< Press any key on OSDL window to continue >" ) ;
00413 }
00414
00415
00416
00417
00418
00419
00420
00421 do
00422 {
00423
00424
00425 SDL_WaitEvent( & currentEvent ) ;
00426
00427 }
00428 while ( currentEvent.type != KeyPressed ) ;
00429
00430 #else // OSDL_USES_SDL
00431
00432
00433
00434
00435 try
00436 {
00437
00438 if ( displayWaitingMessage )
00439 Ceylan::waitForKey( "< Press any key to continue >" ) ;
00440 else
00441 Ceylan::waitForKey( "" ) ;
00442
00443 }
00444 catch( const UtilsException & e )
00445 {
00446
00447 throw EventsException( "EventsModule::waitForAnyKey failed: "
00448 + e.toString() ) ;
00449
00450 }
00451
00452
00453 #endif // OSDL_USES_SDL
00454
00455 }
00456
00457
00458
00459 void EventsModule::waitForAnyUserInput() const
00460 {
00461
00462 #if OSDL_USES_SDL
00463
00464
00465
00466
00467
00468
00469
00470 BasicEvent currentEvent ;
00471
00472
00473
00474
00475
00476
00477
00478 do
00479 {
00480
00481
00482 ::SDL_WaitEvent( & currentEvent ) ;
00483
00484 }
00485 while ( currentEvent.type != KeyPressed
00486 && currentEvent.type != MouseButtonPressed
00487 && currentEvent.type != JoystickButtonPressed
00488 && currentEvent.type != UserRequestedQuit ) ;
00489
00490 #else // OSDL_USES_SDL
00491
00492
00493
00494
00495 try
00496 {
00497
00498 Ceylan::waitForKey( "" ) ;
00499
00500 }
00501 catch( const UtilsException & e )
00502 {
00503
00504 throw EventsException( "EventsModule::waitForAnyUserInput failed: "
00505 + e.toString() ) ;
00506
00507 }
00508
00509
00510 #endif // OSDL_USES_SDL
00511
00512 }
00513
00514
00515
00516 bool EventsModule::hasPendingUserInput() const
00517 {
00518
00519 #if OSDL_USES_SDL
00520
00521
00522
00523
00524
00525
00526
00527 BasicEvent currentEvent ;
00528
00529 bool userInputFound = false ;
00530
00531
00532
00533
00534
00535
00536 while ( ! userInputFound && ::SDL_PollEvent( & currentEvent ) == 1 )
00537 {
00538
00539 switch( currentEvent.type )
00540 {
00541
00542
00543 case KeyPressed:
00544 case MouseButtonPressed:
00545 case JoystickButtonPressed:
00546 case UserRequestedQuit:
00547 userInputFound = true ;
00548 break ;
00549
00550 default:
00551 break ;
00552
00553 }
00554 }
00555
00556 return userInputFound ;
00557
00558
00559 #else // OSDL_USES_SDL
00560
00561 return false ;
00562
00563 #endif // OSDL_USES_SDL
00564
00565 }
00566
00567
00568
00569 bool EventsModule::getGrabInputMode() const
00570 {
00571
00572 #if OSDL_USES_SDL
00573
00574 return ( ::SDL_WM_GrabInput( SDL_GRAB_QUERY ) == SDL_GRAB_ON ) ;
00575
00576
00577 #else // OSDL_USES_SDL
00578
00579 throw EventsException( "EventsModule::getGrabInputMode failed: "
00580 "no SDL support available." ) ;
00581
00582 #endif // OSDL_USES_SDL
00583
00584 }
00585
00586
00587
00588 void EventsModule::setGrabInputMode( bool newMode )
00589 {
00590
00591 #if OSDL_USES_SDL
00592
00593 SDL_GrabMode newGrabMode = ( newMode ? SDL_GRAB_ON : SDL_GRAB_OFF ) ;
00594
00595 ::SDL_WM_GrabInput( newGrabMode ) ;
00596
00597 #else // OSDL_USES_SDL
00598
00599 throw EventsException( "EventsModule::setGrabInputMode failed: "
00600 "no SDL support available." ) ;
00601
00602 #endif // OSDL_USES_SDL
00603
00604 }
00605
00606
00607
00608 bool EventsModule::sleepFor( Second seconds, Microsecond micros ) const
00609 {
00610
00611
00612
00613
00614
00615
00616
00617 try
00618 {
00619
00620
00621 while ( seconds > 4100 )
00622 {
00623 Ceylan::System::smartSleep( 4100, 0 ) ;
00624 seconds -= 4100 ;
00625 }
00626
00627 return Ceylan::System::smartSleep( seconds, micros ) ;
00628
00629 }
00630 catch( const Ceylan::System::SystemException & e )
00631 {
00632 throw EventsException( "EventsModule::sleepFor failed: "
00633 + e.toString() ) ;
00634 }
00635
00636 }
00637
00638
00639
00640 void EventsModule::useScheduler( bool on )
00641 {
00642
00643 if ( on && ! _useScheduler )
00644 {
00645
00646 Scheduler::GetScheduler() ;
00647 }
00648
00649 if ( ! on && _useScheduler )
00650 {
00651
00652 Scheduler::DeleteScheduler() ;
00653 }
00654
00655 _useScheduler = on ;
00656
00657 }
00658
00659
00660
00661
00662 bool EventsModule::isGUIEnabled() const
00663 {
00664
00665 return _isGuiEnabled ;
00666
00667 }
00668
00669
00670
00671 void EventsModule::setGUIEnableStatus( bool newStatus )
00672 {
00673
00674 _isGuiEnabled = newStatus ;
00675
00676 }
00677
00678
00679
00680
00681 void EventsModule::setIdleCallback(
00682 Ceylan::System::Callback idleCallback,
00683 void * callbackData,
00684 Ceylan::System::Microsecond callbackExpectedMaxDuration )
00685 {
00686
00687 if ( _useScheduler )
00688 {
00689
00690
00691 Scheduler::GetScheduler().setIdleCallback( idleCallback,
00692 callbackData, callbackExpectedMaxDuration ) ;
00693
00694 }
00695 else
00696 {
00697
00698 _loopIdleCallback = idleCallback ;
00699 _loopIdleCallbackData = callbackData ;
00700
00701 if ( callbackExpectedMaxDuration != 0 )
00702 _loopIdleCallbackMaxDuration = callbackExpectedMaxDuration ;
00703 else
00704 _loopIdleCallbackMaxDuration = EvaluateCallbackduration(
00705 idleCallback, callbackData ) ;
00706
00707 }
00708
00709 }
00710
00711
00712
00713 void EventsModule::setEventLoopTargetFrequency( Hertz targetFrequency )
00714 {
00715
00716 _loopTargetedFrequency = targetFrequency ;
00717
00718 }
00719
00720
00721
00722 void EventsModule::enterMainLoop()
00723 {
00724
00725 if ( ! OSDL::CommonModule::IsBackendInitialized() )
00726 throw EventsException( "EventsModule::enterMainLoop called "
00727 "whereas back-end not initialized." ) ;
00728
00729
00730
00731 if ( _useScheduler )
00732 {
00733
00734 LOG_DEBUG_EVENTS(
00735 "EventsModule::enterMainLoop: delegating to scheduler." ) ;
00736
00737 try
00738 {
00739
00740
00741 Scheduler::GetScheduler().schedule() ;
00742
00743 }
00744 catch( const SchedulingException & e )
00745 {
00746 throw EventsException(
00747 "EventsModule::enterMainLoop: scheduler stopped on failure: "
00748 + e.toString() ) ;
00749 }
00750
00751 LOG_DEBUG_EVENTS( "EventsModule::enterMainLoop: scheduler returned." ) ;
00752
00753 }
00754 else
00755 {
00756
00757 LOG_DEBUG_EVENTS( "EventsModule::enterMainLoop: "
00758 "no scheduler requested, using basic event loop." ) ;
00759
00760 enterBasicMainLoop() ;
00761
00762 LOG_DEBUG_EVENTS(
00763 "EventsModule::enterMainLoop: exiting now from main loop." ) ;
00764
00765 }
00766
00767 }
00768
00769
00770
00771 void EventsModule::requestQuit()
00772 {
00773
00774 send( "Quit has been requested, exiting from main event loop." ) ;
00775
00776 _quitRequested = true ;
00777
00778 if ( _useScheduler )
00779 {
00780
00781 try
00782 {
00783 Scheduler::GetExistingScheduler().stop() ;
00784 }
00785 catch ( const SchedulingException & e )
00786 {
00787 LogPlug::error( "EventsModule::requestQuit: "
00788 "no scheduler found, none stopped, or stop failed: "
00789 + e.toString() ) ;
00790 }
00791
00792 }
00793
00794 }
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 bool EventsModule::hasKeyboardHandler() const
00805 {
00806
00807 return ( _keyboardHandler != 0 ) ;
00808
00809 }
00810
00811
00812
00813 KeyboardHandler & EventsModule::getKeyboardHandler() const
00814 {
00815
00816 if ( _keyboardHandler == 0 )
00817 throw EventsException(
00818 "EventsModule::getKeyboardHandler: no handler available: "
00819 "did you specify 'CommonModule::UseKeyboard' "
00820 "at the initialization of the common module?" ) ;
00821
00822 return * _keyboardHandler ;
00823
00824 }
00825
00826
00827
00828 void EventsModule::setKeyboardHandler( KeyboardHandler & newHandler )
00829 {
00830
00831 if ( _keyboardHandler != 0 )
00832 delete _keyboardHandler ;
00833
00834 _keyboardHandler = & newHandler ;
00835
00836 }
00837
00838
00839
00840 bool EventsModule::hasJoystickHandler() const
00841 {
00842
00843 return ( _joystickHandler != 0 ) ;
00844
00845 }
00846
00847
00848
00849 JoystickHandler & EventsModule::getJoystickHandler() const
00850
00851 {
00852
00853 if ( _joystickHandler == 0 )
00854 throw EventsException(
00855 "EventsModule::getJoystickHandler: no handler available: "
00856 "did you specify 'CommonModule::UseJoystick' "
00857 "at the initialization of the common module?" ) ;
00858
00859 return * _joystickHandler ;
00860
00861 }
00862
00863
00864
00865 void EventsModule::setJoystickHandler( JoystickHandler & newHandler )
00866 {
00867
00868 if ( _joystickHandler != 0 )
00869 delete _joystickHandler ;
00870
00871 _joystickHandler = & newHandler ;
00872
00873 }
00874
00875
00876
00877
00878 bool EventsModule::hasMouseHandler() const
00879 {
00880
00881 return ( _mouseHandler != 0 ) ;
00882
00883 }
00884
00885
00886
00887 MouseHandler & EventsModule::getMouseHandler() const
00888 {
00889
00890 if ( _mouseHandler == 0 )
00891 throw EventsException(
00892 "EventsModule::getMouseHandler: no handler available: "
00893 "did you specify 'CommonModule::UseMouse' "
00894 "at the initialization of the common module?" ) ;
00895
00896 return * _mouseHandler ;
00897
00898 }
00899
00900
00901
00902 void EventsModule::setMouseHandler( MouseHandler & newHandler )
00903 {
00904
00905 if ( _mouseHandler != 0 )
00906 delete _mouseHandler ;
00907
00908 _mouseHandler = & newHandler ;
00909
00910 }
00911
00912
00913
00914 void EventsModule::updateInputState()
00915 {
00916
00917 #if OSDL_USES_SDL
00918
00919
00920 BasicEvent currentEvent ;
00921
00922
00923
00924 while ( SDL_PollEvent( & currentEvent ) )
00925 {
00926
00927 if ( _isGuiEnabled )
00928 {
00929
00930 #if OSDL_USES_AGAR
00931
00932 AG_DriverEvent driverEvent ;
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945 AG_SDL_TranslateEvent( 0,
00946 & currentEvent,
00947 & driverEvent ) ;
00948
00949
00950 if ( AG_ProcessEvent( 0, &driverEvent ) == -1)
00951 {
00952
00953
00954
00955
00956 if( agTerminating == 1 )
00957 onQuitRequested() ;
00958 else
00959 LogPlug::error( "AG_ProcessEvent returned -1, "
00960 "but we are not known to be terminating." ) ;
00961
00962 }
00963
00964 #else // OSDL_USES_AGAR
00965
00966 throw EventsException( "EventsModule::updateInputState: "
00967 "GUI enabled but cannot be managed." ) ;
00968
00969 #endif // OSDL_USES_AGAR
00970
00971 }
00972 else
00973 {
00974
00975
00976
00977 switch ( currentEvent.type )
00978 {
00979
00980
00981
00982 case ApplicationFocusChanged:
00983 onApplicationFocusChanged( currentEvent.active ) ;
00984 break ;
00985
00986
00987 case KeyPressed:
00988 onKeyPressed( currentEvent.key ) ;
00989 break ;
00990
00991 case KeyReleased:
00992 onKeyReleased( currentEvent.key ) ;
00993 break ;
00994
00995
00996 case MouseMoved:
00997 onMouseMotion( currentEvent.motion ) ;
00998 break ;
00999
01000 case MouseButtonPressed:
01001 onMouseButtonPressed( currentEvent.button ) ;
01002 break ;
01003
01004 case MouseButtonReleased:
01005 onMouseButtonReleased( currentEvent.button ) ;
01006 break ;
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 case JoystickAxisChanged:
01018 onJoystickAxisChanged( currentEvent.jaxis ) ;
01019 break ;
01020
01021 case JoystickTrackballChanged:
01022 onJoystickTrackballChanged( currentEvent.jball ) ;
01023 break ;
01024
01025 case JoystickHatPositionChanged:
01026 onJoystickHatChanged( currentEvent.jhat ) ;
01027 break ;
01028
01029 case JoystickButtonPressed:
01030 onJoystickButtonPressed( currentEvent.jbutton ) ;
01031 break ;
01032
01033 case JoystickButtonReleased:
01034 onJoystickButtonReleased( currentEvent.jbutton ) ;
01035 break ;
01036
01037
01038
01039
01040 case UserRequestedQuit:
01041 onQuitRequested() ;
01042 break ;
01043
01044 case SystemSpecificTriggered:
01045 onSystemSpecificWindowManagerEvent( currentEvent.syswm ) ;
01046 break ;
01047
01048 case UserResizedVideoMode:
01049 onResizedWindow( currentEvent.resize ) ;
01050 break ;
01051
01052 case ScreenNeedsRedraw:
01053 onScreenNeedsRedraw() ;
01054 break ;
01055
01056
01057 default:
01058 if ( currentEvent.type >= FirstUserEventTriggered
01059 || currentEvent.type <= LastUserEventTriggered )
01060 onUserEvent( currentEvent.user ) ;
01061 else
01062 onUnknownEventType( currentEvent ) ;
01063 break ;
01064
01065 }
01066
01067 }
01068 }
01069
01070 #endif // OSDL_USES_SDL
01071
01072 }
01073
01074
01075
01076 const string EventsModule::toString( Ceylan::VerbosityLevels level ) const
01077 {
01078
01079 string res = "Event module, " ;
01080
01081 if ( _useScheduler )
01082 res += "using a scheduler, " ;
01083 else
01084 res += "not using any scheduler, " ;
01085
01086 if ( _keyboardHandler != 0 )
01087 res += "using a keyboard handler, " ;
01088 else
01089 res += "not using any keyboard handler, " ;
01090
01091 if ( _joystickHandler != 0 )
01092 res += "using a joystick handler, " ;
01093 else
01094 res += "not using any joystick handler, " ;
01095
01096 if ( _mouseHandler != 0 )
01097 res += "using a mouse handler, " ;
01098 else
01099 res += "not using any mouse handler, " ;
01100
01101 if ( _isGuiEnabled )
01102 res += "with GUI support enabled, " ;
01103 else
01104 res += "with no GUI support enabled, " ;
01105
01106
01107 if ( _loopIdleCallback == 0 )
01108 res += "using micro-sleep idle callback" ;
01109 else
01110 res += "using user-specified idle callback" ;
01111
01112 if ( level == Ceylan::low )
01113 return res ;
01114
01115 std::list<string> returned ;
01116
01117 returned.push_back( res ) ;
01118
01119 returned.push_back( Ceylan::Module::toString() ) ;
01120
01121 if ( level == Ceylan::medium)
01122 return Ceylan::formatStringList( returned ) ;
01123
01124 if ( _keyboardHandler != 0 )
01125 returned.push_back( _keyboardHandler->toString() ) ;
01126
01127 if ( _joystickHandler != 0 )
01128 returned.push_back( _joystickHandler->toString() ) ;
01129
01130 if ( _mouseHandler != 0 )
01131 returned.push_back( _mouseHandler->toString() ) ;
01132
01133 return Ceylan::formatStringList( returned ) ;
01134
01135 }
01136
01137
01138
01139
01140
01141
01142
01143
01144 string EventsModule::DescribeEnvironmentVariables()
01145 {
01146
01147 #if OSDL_USES_SDL
01148
01149 Ceylan::Uint16 varCount =
01150 sizeof( _SDLEnvironmentVariables ) / sizeof (string) ;
01151
01152 string result = "Examining the " + Ceylan::toString( varCount )
01153 + " events-related environment variables for SDL backend:" ;
01154
01155 std::list<string> variables ;
01156
01157 string var, value ;
01158
01159 TextOutputFormat htmlFormat = TextDisplayable::GetOutputFormat() ;
01160
01161 for ( Ceylan::Uint16 i = 0; i < varCount; i++ )
01162 {
01163
01164 var = _SDLEnvironmentVariables[ i ] ;
01165 value = Ceylan::System::getEnvironmentVariable( var ) ;
01166
01167 if ( value.empty() )
01168 {
01169 if ( htmlFormat )
01170 {
01171 variables.push_back( "<em>" + var + "</em> is not set." ) ;
01172 }
01173 else
01174 {
01175 variables.push_back( var + " is not set." ) ;
01176 }
01177 }
01178 else
01179 {
01180 if ( htmlFormat )
01181 {
01182 variables.push_back( "<b>" + var + "</b> set to ["
01183 + value + "]." ) ;
01184 }
01185 else
01186 {
01187 variables.push_back( var + " set to [" + value + "]." ) ;
01188 }
01189 }
01190
01191 }
01192
01193 return result + Ceylan::formatStringList( variables ) ;
01194
01195 #else // OSDL_USES_SDL
01196
01197 return "(not using SDL)" ;
01198
01199 #endif // OSDL_USES_SDL
01200
01201 }
01202
01203
01204
01205 string EventsModule::DescribeEvent( BasicEvent anEvent )
01206 {
01207
01208 #if OSDL_USES_SDL
01209
01210 switch( anEvent.type )
01211 {
01212
01213 case ApplicationFocusChanged:
01214 return DescribeEvent( anEvent.active ) ;
01215 break ;
01216
01217 case KeyPressed:
01218 return DescribeEvent( anEvent.key ) ;
01219 break ;
01220
01221 case KeyReleased:
01222 return DescribeEvent( anEvent.key ) ;
01223 break ;
01224
01225 case MouseMoved:
01226 return DescribeEvent( anEvent.motion ) ;
01227 break ;
01228
01229 case MouseButtonPressed:
01230 return DescribeEvent( anEvent.button ) ;
01231 break ;
01232
01233 case MouseButtonReleased:
01234 return DescribeEvent( anEvent.button ) ;
01235 break ;
01236
01237 case JoystickAxisChanged:
01238 return DescribeEvent( anEvent.jaxis ) ;
01239 break ;
01240
01241 case JoystickTrackballChanged:
01242 return DescribeEvent( anEvent.jball ) ;
01243 break ;
01244
01245 case JoystickHatPositionChanged:
01246 return DescribeEvent( anEvent.jhat ) ;
01247 break ;
01248
01249 case JoystickButtonPressed:
01250 return DescribeEvent( anEvent.jbutton ) ;
01251 break ;
01252
01253 case JoystickButtonReleased:
01254 return DescribeEvent( anEvent.jbutton ) ;
01255 break ;
01256
01257 case UserRequestedQuit:
01258 return DescribeEvent( anEvent.quit ) ;
01259 break ;
01260
01261 case SystemSpecificTriggered:
01262 return DescribeEvent( anEvent.syswm ) ;
01263 break ;
01264
01265 case UserResizedVideoMode:
01266 return DescribeEvent( anEvent.resize ) ;
01267 break ;
01268
01269 case ScreenNeedsRedraw:
01270 return DescribeEvent( anEvent.expose ) ;
01271 break ;
01272
01273 default:
01274 return "Unknow event type." ;
01275 break ;
01276 }
01277
01278 return "Unexpected event type." ;
01279
01280 #else // OSDL_USES_SDL
01281
01282 return "(not using SDL)" ;
01283
01284 #endif // OSDL_USES_SDL
01285
01286 }
01287
01288
01289
01290 Millisecond EventsModule::GetMillisecondsSinceStartup()
01291 {
01292
01293 #if OSDL_USES_SDL
01294
01295
01296
01297 #if OSDL_DEBUG
01298 if ( ! OSDL::CommonModule::IsBackendInitialized() )
01299 throw EventsException(
01300 "EventsModule::getMillisecondsSinceStartup() called "
01301 "whereas back-end not initialized." ) ;
01302 #endif // OSDL_DEBUG
01303
01304 return static_cast<Millisecond>( SDL_GetTicks() ) ;
01305
01306 #else // OSDL_USES_SDL
01307
01308 throw EventsException( "EventsModule::GetMillisecondsSinceStartup failed: "
01309 "no SDL support available" ) ;
01310
01311 #endif // OSDL_USES_SDL
01312
01313 }
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323 void EventsModule::enterBasicMainLoop()
01324 {
01325
01326
01327
01328 Ceylan::Uint32 frameCount = 0 ;
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345 Video::VideoModule * video ;
01346
01347 try
01348 {
01349 video = & OSDL::getExistingCommonModule().getVideoModule() ;
01350 }
01351 catch ( const OSDL::Exception & e )
01352 {
01353 throw EventsException(
01354 "EventsModule::enterBasicMainLoop: no video module available ("
01355 + e.toString() ) ;
01356 }
01357
01358 Rendering::Renderer * renderer = 0 ;
01359
01360 if ( Rendering::Renderer::HasExistingRootRenderer() )
01361 {
01362 renderer = & Rendering::Renderer::GetExistingRootRenderer() ;
01363 LOG_DEBUG_EVENTS(
01364 "EventsModule::enterBasicMainLoop: using root renderer ("
01365 + renderer->toString() + ")" ) ;
01366 }
01367 else
01368 {
01369 LOG_DEBUG_EVENTS( "EventsModule::enterBasicMainLoop: "
01370 "not using any root renderer." ) ;
01371 }
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382 _idleCallsCount = 0 ;
01383
01384 Microsecond startedMicrosec ;
01385 Second startedSec ;
01386
01387 Microsecond lastMicrosec ;
01388 Second lastSec ;
01389
01390 Microsecond nowMicrosec ;
01391 Second nowSec ;
01392
01393
01394 try
01395 {
01396
01397 if ( _loopIdleCallback == 0 )
01398 {
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408 _loopIdleCallbackMaxDuration =
01409 static_cast<Microsecond>( 1.1 * getSchedulingGranularity() ) ;
01410
01411 }
01412
01413
01414
01415 Microsecond loopExpectedDuration
01416 = static_cast<Microsecond>( 1000000.0f / _loopTargetedFrequency ) ;
01417
01418
01419 LOG_DEBUG_EVENTS( "Loop expected duration is "
01420 + Ceylan::toString( loopExpectedDuration ) + " microseconds, "
01421 "and idle callback expected duration is "
01422 + Ceylan::toString( _loopIdleCallbackMaxDuration )
01423 + " microseconds." ) ;
01424
01425 getPreciseTime( lastSec, lastMicrosec ) ;
01426
01427 startedSec = lastSec ;
01428 startedMicrosec = lastMicrosec ;
01429
01430
01431 while ( ! _quitRequested )
01432 {
01433
01434
01435 updateInputState() ;
01436
01437
01438
01439
01440
01441
01442
01443 if ( renderer != 0 )
01444 renderer->render() ;
01445 else
01446 video->redraw() ;
01447
01448 frameCount++ ;
01449
01450 getPreciseTime( nowSec, nowMicrosec ) ;
01451
01452
01453 while ( getDurationBetween( lastSec, lastMicrosec,
01454 nowSec, nowMicrosec ) + _loopIdleCallbackMaxDuration
01455 < loopExpectedDuration )
01456 {
01457
01458 onIdle() ;
01459 getPreciseTime( nowSec, nowMicrosec ) ;
01460
01461 }
01462
01463
01464
01465
01466
01467
01468 while ( getDurationBetween( lastSec, lastMicrosec, nowSec,
01469 nowMicrosec ) < loopExpectedDuration )
01470 getPreciseTime( nowSec, nowMicrosec ) ;
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481 lastSec = nowSec ;
01482 lastMicrosec = nowMicrosec ;
01483
01484 }
01485
01486 }
01487 catch ( const SystemException & e )
01488 {
01489 throw EventsException( "EventsModule::enterBasicMainLoop: "
01490 + e.toString() ) ;
01491 }
01492
01493 if ( ( lastSec - startedSec ) >
01494 Ceylan::System::MaximumDurationWithMicrosecondAccuracy )
01495 {
01496
01497
01498 LOG_DEBUG_EVENTS( "Exited from main loop after "
01499 + Ceylan::toString( frameCount ) + " frames and about "
01500 + Ceylan::toString( lastSec - startedSec )
01501 + " seconds, an average of "
01502 + Ceylan::toString(
01503 static_cast<Ceylan::Float64>( _idleCallsCount ) / frameCount,
01504 3 )
01505 + " idle calls per frame have been performed." ) ;
01506
01507 }
01508 else
01509 {
01510
01511 LOG_DEBUG_EVENTS( "Exited from main loop after "
01512 + Ceylan::toString( frameCount ) + " frames and about "
01513 + Ceylan::toString( lastSec - startedSec )
01514 + " seconds, on average there were "
01515 + Ceylan::toString(
01516 1000000.0f * frameCount /
01517 getDurationBetween( startedSec, startedMicrosec, lastSec,
01518 lastMicrosec ),
01519 3 )
01520 + " frames per second, an average of "
01521 + Ceylan::toString(
01522 static_cast<Ceylan::Float64>( _idleCallsCount ) / frameCount,
01523 3 )
01524 + " idle calls per frame have been performed." ) ;
01525
01526 }
01527
01528 }
01529
01530
01531
01532
01533
01534
01535
01536
01537 void EventsModule::onKeyboardFocusGained(
01538 const FocusEvent & keyboardFocusEvent )
01539 {
01540
01541 #if OSDL_USES_SDL
01542
01543 LOG_DEBUG_EVENTS( "Application gained keyboard focus." ) ;
01544
01545 #if OSDL_DEBUG
01546 if ( _keyboardHandler == 0 )
01547 Ceylan::emergencyShutdown( "EventsModule::onKeyboardFocusGained called "
01548 "whereas no handler is available." ) ;
01549 #endif // OSDL_DEBUG
01550
01551 _keyboardHandler->focusGained( keyboardFocusEvent ) ;
01552
01553 #endif // OSDL_USES_SDL
01554
01555 }
01556
01557
01558
01559 void EventsModule::onKeyboardFocusLost(
01560 const FocusEvent & keyboardFocusEvent )
01561 {
01562
01563 #if OSDL_USES_SDL
01564
01565 LOG_DEBUG_EVENTS( "Application lost keyboard focus." ) ;
01566
01567 #if OSDL_DEBUG
01568 if ( _keyboardHandler == 0 )
01569 Ceylan::emergencyShutdown( "EventsModule::onKeyboardFocusLost called "
01570 "whereas no handler is available." ) ;
01571 #endif // OSDL_DEBUG
01572
01573 _keyboardHandler->focusLost( keyboardFocusEvent ) ;
01574
01575 #endif // OSDL_USES_SDL
01576
01577 }
01578
01579
01580
01581 void EventsModule::onKeyPressed( const KeyboardEvent & keyboardEvent )
01582 {
01583
01584 #if OSDL_USES_SDL
01585
01586 LOG_DEBUG_EVENTS( "Key pressed." ) ;
01587
01588 #if OSDL_DEBUG
01589 if ( _keyboardHandler == 0 )
01590 Ceylan::emergencyShutdown( "EventsModule::onKeyPressed called "
01591 "whereas no handler is available." ) ;
01592 #endif // OSDL_DEBUG
01593
01594 _keyboardHandler->keyPressed( keyboardEvent ) ;
01595
01596 #endif // OSDL_USES_SDL
01597
01598 }
01599
01600
01601
01602 void EventsModule::onKeyReleased( const KeyboardEvent & keyboardEvent )
01603 {
01604
01605 #if OSDL_USES_SDL
01606
01607 LOG_DEBUG_EVENTS( "Key released." ) ;
01608
01609 #if OSDL_DEBUG
01610 if ( _keyboardHandler == 0 )
01611 Ceylan::emergencyShutdown( "EventsModule::onKeyReleased called "
01612 "whereas no handler is available." ) ;
01613 #endif // OSDL_DEBUG
01614
01615 _keyboardHandler->keyReleased( keyboardEvent ) ;
01616
01617 #endif // OSDL_USES_SDL
01618
01619 }
01620
01621
01622
01623
01624
01625
01626
01627
01628 void EventsModule::onMouseFocusGained( const FocusEvent & mouseFocusEvent )
01629 {
01630
01631 #if OSDL_USES_SDL
01632
01633 LOG_DEBUG_EVENTS( "Application gained mouse focus." ) ;
01634
01635 #if OSDL_DEBUG
01636 if ( _mouseHandler == 0 )
01637 Ceylan::emergencyShutdown( "EventsModule::onMouseFocusGained called "
01638 "whereas no handler is available." ) ;
01639 #endif // OSDL_DEBUG
01640
01641 _mouseHandler->focusGained( mouseFocusEvent ) ;
01642
01643 #endif // OSDL_USES_SDL
01644
01645 }
01646
01647
01648
01649 void EventsModule::onMouseFocusLost( const FocusEvent & mouseFocusEvent )
01650 {
01651
01652 #if OSDL_USES_SDL
01653
01654 LOG_DEBUG_EVENTS( "Application lost mouse focus." ) ;
01655
01656 #if OSDL_DEBUG
01657 if ( _mouseHandler == 0 )
01658 Ceylan::emergencyShutdown( "EventsModule::onMouseFocusLost called "
01659 "whereas no handler is available." ) ;
01660 #endif // OSDL_DEBUG
01661
01662 _mouseHandler->focusLost( mouseFocusEvent ) ;
01663
01664 #endif // OSDL_USES_SDL
01665
01666 }
01667
01668
01669
01670 void EventsModule::onMouseMotion( const MouseMotionEvent & mouseEvent )
01671 {
01672
01673 #if OSDL_USES_SDL
01674
01675 LOG_DEBUG_EVENTS( "Mouse motion." ) ;
01676
01677 #if OSDL_DEBUG
01678 if ( _mouseHandler == 0 )
01679 Ceylan::emergencyShutdown( "EventsModule::onMouseMotion called "
01680 "whereas no handler is available." ) ;
01681 #endif // OSDL_DEBUG
01682
01683 _mouseHandler->mouseMoved( mouseEvent ) ;
01684
01685 #endif // OSDL_USES_SDL
01686
01687 }
01688
01689
01690
01691 void EventsModule::onMouseButtonPressed( const MouseButtonEvent & mouseEvent )
01692 {
01693
01694 #if OSDL_USES_SDL
01695
01696 LOG_DEBUG_EVENTS( "Mouse button pressed." ) ;
01697
01698 #if OSDL_DEBUG
01699 if ( _mouseHandler == 0 )
01700 Ceylan::emergencyShutdown( "EventsModule::onMouseButtonPressed called "
01701 "whereas no handler is available." ) ;
01702 #endif // OSDL_DEBUG
01703
01704 _mouseHandler->buttonPressed( mouseEvent ) ;
01705
01706 #endif // OSDL_USES_SDL
01707
01708 }
01709
01710
01711
01712 void EventsModule::onMouseButtonReleased( const MouseButtonEvent & mouseEvent )
01713 {
01714
01715 #if OSDL_USES_SDL
01716
01717 LOG_DEBUG_EVENTS( "Mouse button released." ) ;
01718
01719 #if OSDL_DEBUG
01720 if ( _mouseHandler == 0 )
01721 Ceylan::emergencyShutdown( "EventsModule::onMouseButtonReleased called "
01722 "whereas no handler is available." ) ;
01723 #endif // OSDL_DEBUG
01724
01725 _mouseHandler->buttonReleased( mouseEvent ) ;
01726
01727 #endif // OSDL_USES_SDL
01728
01729 }
01730
01731
01732
01733
01734
01735
01736
01737
01738 void EventsModule::onJoystickAxisChanged(
01739 const JoystickAxisEvent & joystickEvent )
01740 {
01741
01742 #if OSDL_USES_SDL
01743
01744 LOG_DEBUG_EVENTS( "Joystick axis moved." ) ;
01745
01746 #if OSDL_DEBUG
01747 if ( _joystickHandler == 0 )
01748 Ceylan::emergencyShutdown( "EventsModule::onJoystickAxisChanged called "
01749 "whereas no handler is available." ) ;
01750 #endif // OSDL_DEBUG
01751
01752 _joystickHandler->axisChanged( joystickEvent ) ;
01753
01754 #endif // OSDL_USES_SDL
01755
01756 }
01757
01758
01759
01760 void EventsModule::onJoystickTrackballChanged(
01761 const JoystickTrackballEvent & joystickEvent )
01762 {
01763
01764 #if OSDL_USES_SDL
01765
01766 LOG_DEBUG_EVENTS( "Joystick trackball moved." ) ;
01767
01768 #if OSDL_DEBUG
01769 if ( _joystickHandler == 0 )
01770 Ceylan::emergencyShutdown(
01771 "EventsModule::onJoystickTrackballChanged called "
01772 "whereas no handler is available." ) ;
01773 #endif // OSDL_DEBUG
01774
01775 _joystickHandler->trackballChanged( joystickEvent ) ;
01776
01777 #endif // OSDL_USES_SDL
01778
01779 }
01780
01781
01782
01783 void EventsModule::onJoystickHatChanged(
01784 const JoystickHatEvent & joystickEvent )
01785 {
01786
01787 #if OSDL_USES_SDL
01788
01789 LOG_DEBUG_EVENTS( "Joystick hat moved." ) ;
01790
01791 #if OSDL_DEBUG
01792 if ( _joystickHandler == 0 )
01793 Ceylan::emergencyShutdown( "EventsModule::onJoystickHatChanged called "
01794 "whereas no handler is available." ) ;
01795 #endif // OSDL_DEBUG
01796
01797 _joystickHandler->hatChanged( joystickEvent ) ;
01798
01799 #endif // OSDL_USES_SDL
01800
01801 }
01802
01803
01804
01805 void EventsModule::onJoystickButtonPressed(
01806 const JoystickButtonEvent & joystickEvent )
01807 {
01808
01809 #if OSDL_USES_SDL
01810
01811 LOG_DEBUG_EVENTS( "Joystick button pressed." ) ;
01812
01813 #if OSDL_DEBUG
01814 if ( _joystickHandler == 0 )
01815 Ceylan::emergencyShutdown(
01816 "EventsModule::onJoystickButtonPressed called "
01817 "whereas no handler is available." ) ;
01818 #endif // OSDL_DEBUG
01819
01820 _joystickHandler->buttonPressed( joystickEvent ) ;
01821
01822 #endif // OSDL_USES_SDL
01823
01824 }
01825
01826
01827
01828 void EventsModule::onJoystickButtonReleased(
01829 const JoystickButtonEvent & joystickEvent )
01830 {
01831
01832 #if OSDL_USES_SDL
01833
01834 LOG_DEBUG_EVENTS( "Joystick button released." ) ;
01835
01836 #if OSDL_DEBUG
01837 if ( _joystickHandler == 0 )
01838 Ceylan::emergencyShutdown(
01839 "EventsModule::onJoystickButtonReleased called "
01840 "whereas no handler is available." ) ;
01841 #endif
01842
01843 _joystickHandler->buttonReleased( joystickEvent ) ;
01844
01845 #endif // OSDL_USES_SDL
01846
01847 }
01848
01849
01850
01851
01852
01853
01854
01855
01856 void EventsModule::onIdle()
01857 {
01858
01859 #if OSDL_USES_SDL
01860
01861 _idleCallsCount++ ;
01862
01863 if ( _loopIdleCallback != 0 )
01864 {
01865 LOG_DEBUG_EVENTS(
01866 "EventsModule::onIdle: calling now idle call-back." ) ;
01867
01868 (*_loopIdleCallback)( _loopIdleCallbackData ) ;
01869
01870 LOG_DEBUG_EVENTS(
01871 "EventsModule::onIdle: returned from idle call-back." ) ;
01872
01873 }
01874 else
01875 {
01876
01877
01878
01879
01880
01881
01882 Ceylan::System::atomicSleep() ;
01883
01884 }
01885
01886 #endif // OSDL_USES_SDL
01887
01888 }
01889
01890
01891
01892 void EventsModule::onApplicationFocusChanged( const FocusEvent & focusEvent )
01893 {
01894
01895 #if OSDL_USES_SDL
01896
01897 LOG_DEBUG_EVENTS( "Application focus changed." ) ;
01898
01899
01900
01901 if ( focusEvent.state & _MouseFocus )
01902 {
01903
01904
01905
01906 if ( focusEvent.gain == 1 )
01907 onMouseFocusGained( focusEvent ) ;
01908 else
01909 onMouseFocusLost( focusEvent ) ;
01910 }
01911
01912 if ( focusEvent.state & _KeyboardFocus )
01913 {
01914 if ( focusEvent.gain == 1 )
01915 onKeyboardFocusGained( focusEvent ) ;
01916 else
01917 onKeyboardFocusLost( focusEvent ) ;
01918 }
01919
01920 if ( focusEvent.state & _ApplicationFocus )
01921 {
01922 if ( focusEvent.gain == 1 )
01923 onApplicationRestored( focusEvent ) ;
01924 else
01925 onApplicationIconified( focusEvent ) ;
01926 }
01927
01928 #endif // OSDL_USES_SDL
01929
01930 }
01931
01932
01933
01934 void EventsModule::onApplicationIconified( const FocusEvent & focusEvent )
01935 {
01936
01937 #if OSDL_USES_SDL
01938
01939 LOG_DEBUG_EVENTS( "Application is iconified (no handler registered): "
01940 + DescribeEvent( focusEvent ) ) ;
01941
01942 #endif // OSDL_USES_SDL
01943
01944 }
01945
01946
01947
01948 void EventsModule::onApplicationRestored( const FocusEvent & focusEvent )
01949 {
01950
01951 #if OSDL_USES_SDL
01952
01953 LOG_DEBUG_EVENTS( "Application is restored (no handler registered): "
01954 + DescribeEvent( focusEvent ) ) ;
01955 #endif // OSDL_USES_SDL
01956
01957 }
01958
01959
01960
01961 void EventsModule::onQuitRequested()
01962 {
01963
01964 #if OSDL_USES_SDL
01965
01966 LOG_DEBUG_EVENTS( "Application is requested to stop." ) ;
01967
01968 requestQuit() ;
01969
01970 #endif // OSDL_USES_SDL
01971
01972 }
01973
01974
01975
01976 void EventsModule::onSystemSpecificWindowManagerEvent(
01977 const SystemSpecificWindowManagerEvent & wmEvent )
01978 {
01979
01980 #if OSDL_USES_SDL
01981
01982
01983
01984
01985
01986
01987
01988 LOG_DEBUG_EVENTS( "System specific window manager event received "
01989 "(no handler registered)." ) ;
01990
01991 #endif // OSDL_USES_SDL
01992
01993 }
01994
01995
01996
01997 void EventsModule::onResizedWindow( const WindowResizedEvent & resizeEvent )
01998
01999 {
02000
02001 #if OSDL_USES_SDL
02002
02003 LOG_DEBUG_EVENTS( "Resizing, new width is "
02004 + Ceylan::toString( resizeEvent.w ) + ", new height is "
02005 + Ceylan::toString( resizeEvent.h ) ) ;
02006
02007 try
02008 {
02009 OSDL::getExistingCommonModule().getVideoModule().resize(
02010 resizeEvent.w, resizeEvent.h ) ;
02011 }
02012 catch( const Video::VideoException & e )
02013 {
02014
02015 LogPlug::error(
02016 "EventsModule::onResizedWindow: error when resizing to "
02017 + Ceylan::toString( resizeEvent.w ) + ", "
02018 + Ceylan::toString( resizeEvent.h ) + "): "
02019 + e.toString() ) ;
02020 }
02021
02022 #endif // OSDL_USES_SDL
02023
02024 }
02025
02026
02027
02028 void EventsModule::onScreenNeedsRedraw()
02029 {
02030
02031 LOG_DEBUG_EVENTS( "Screen needs redraw." ) ;
02032
02033 try
02034 {
02035 OSDL::getExistingCommonModule().getVideoModule().redraw() ;
02036 }
02037 catch( const Video::VideoException & e )
02038 {
02039
02040 LogPlug::error( "EventsModule::onScreenNeedsRedraw: "
02041 "error when requesting a redraw: " + e.toString() ) ;
02042 }
02043
02044 }
02045
02046
02047
02048 void EventsModule::onUserEvent( const UserEvent & userEvent )
02049 {
02050
02051 #if OSDL_USES_SDL
02052
02053 LOG_DEBUG_EVENTS( "User event received, whose type is "
02054 + Ceylan::toNumericalString( userEvent.type )
02055 + " (no handler registered)." ) ;
02056
02057 #endif // OSDL_USES_SDL
02058
02059 }
02060
02061
02062
02063 void EventsModule::onUnknownEventType( const BasicEvent & unknownEvent )
02064 {
02065
02066 #if OSDL_USES_SDL
02067
02068 LOG_DEBUG_EVENTS( "Unknown event, type is "
02069 + Ceylan::toNumericalString( unknownEvent.type )
02070 + " (no handler registered)." ) ;
02071
02072 #endif // OSDL_USES_SDL
02073
02074 }
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084 string EventsModule::DescribeEvent( const FocusEvent & focusEvent )
02085 {
02086
02087 #if OSDL_USES_SDL
02088
02089 string res = "Focus changed" ;
02090
02091 if ( focusEvent.state & _MouseFocus )
02092 {
02093 if ( focusEvent.gain == 1 )
02094 res += ", mouse gained focus" ;
02095 else
02096 res += ", mouse lost focus" ;
02097 }
02098
02099 if ( focusEvent.state & _KeyboardFocus )
02100 {
02101 if ( focusEvent.gain == 1 )
02102 res += ", keyboard gained focus" ;
02103 else
02104 res += ", keyboard lost focus" ;
02105 }
02106
02107 if ( focusEvent.state & _ApplicationFocus )
02108 {
02109 if ( focusEvent.gain == 1 )
02110 res += ", application restored." ;
02111 else
02112 res += ", application iconified." ;
02113 }
02114
02115 return res ;
02116
02117 #else // OSDL_USES_SDL
02118
02119 return NoSDLSupportAvailable ;
02120
02121 #endif // OSDL_USES_SDL
02122
02123 }
02124
02125
02126
02127 string EventsModule::DescribeEvent( const KeyboardEvent & keyboardEvent )
02128 {
02129
02130 #if OSDL_USES_SDL
02131
02132 string res = "Key " + KeyboardHandler::DescribeKey(
02133 static_cast<KeyboardHandler::KeyIdentifier>(
02134 keyboardEvent.keysym.sym ) )
02135 + ", with modifier(s): "
02136 + KeyboardHandler::DescribeModifier(
02137 static_cast<KeyboardHandler::KeyModifier>(
02138 keyboardEvent.keysym.mod ) )
02139 + " (scancode: "
02140 + Ceylan::toNumericalString( keyboardEvent.keysym.scancode )
02141 + ") was " ;
02142
02143 if ( keyboardEvent.type == KeyPressed )
02144 res += "pressed" ;
02145 else
02146 res += "released" ;
02147
02148 if ( KeyboardHandler::GetMode() == textInput )
02149 res += ". " + KeyboardHandler::DescribeUnicode(
02150 keyboardEvent.keysym.unicode ) ;
02151
02152 return res + "." ;
02153
02154 #else // OSDL_USES_SDL
02155
02156 return NoSDLSupportAvailable ;
02157
02158 #endif // OSDL_USES_SDL
02159
02160 }
02161
02162
02163
02164 string EventsModule::DescribeEvent( const MouseMotionEvent & mouseMotionEvent )
02165 {
02166
02167 #if OSDL_USES_SDL
02168
02169 string res = "Mouse moved to (" + Ceylan::toString( mouseMotionEvent.x )
02170 + ";"
02171 + Ceylan::toString( mouseMotionEvent.y )
02172 + ") after a relative movement of ("
02173 + Ceylan::toString( mouseMotionEvent.xrel ) + ";"
02174 + Ceylan::toString( mouseMotionEvent.yrel ) + ")." ;
02175
02176 for ( Ceylan::Uint8 i = 1; i <= _MouseButtonCount; i++ )
02177 if ( mouseMotionEvent.state & ( SDL_BUTTON( i ) ) )
02178 res += " Button #" + Ceylan::toNumericalString( i )
02179 + " is pressed." ;
02180
02181 return res ;
02182
02183 #else // OSDL_USES_SDL
02184
02185 return NoSDLSupportAvailable ;
02186
02187 #endif // OSDL_USES_SDL
02188
02189 }
02190
02191
02192
02193 string EventsModule::DescribeEvent( const MouseButtonEvent & mouseButtonEvent )
02194 {
02195
02196 #if OSDL_USES_SDL
02197
02198 string res ;
02199
02200 switch( mouseButtonEvent.button )
02201 {
02202
02203 case SDL_BUTTON_LEFT:
02204 res = "Left mouse button "
02205 + ( mouseButtonEvent.state == SDL_PRESSED ) ?
02206 "pressed": "released" ;
02207 break ;
02208
02209 case SDL_BUTTON_MIDDLE:
02210 res = "Middle mouse button "
02211 + ( mouseButtonEvent.state == SDL_PRESSED ) ?
02212 "pressed": "released" ;
02213 break ;
02214
02215 case SDL_BUTTON_RIGHT:
02216 res = "Right mouse button "
02217 + ( mouseButtonEvent.state == SDL_PRESSED ) ?
02218 "pressed": "released" ;
02219 break ;
02220
02221 case SDL_BUTTON_WHEELUP:
02222 res = "Mouse wheel up" ;
02223 break ;
02224
02225 case SDL_BUTTON_WHEELDOWN:
02226 res = "Mouse wheel down" ;
02227 break ;
02228
02229 default:
02230 res = "Unexpected mouse event" ;
02231 break ;
02232
02233 }
02234
02235
02236 res += " while mouse was located at ("
02237 + Ceylan::toString( mouseButtonEvent.x ) + ";"
02238 + Ceylan::toString( mouseButtonEvent.y ) + ")." ;
02239
02240 return res ;
02241
02242 #else // OSDL_USES_SDL
02243
02244 return NoSDLSupportAvailable ;
02245
02246 #endif // OSDL_USES_SDL
02247
02248 }
02249
02250
02251
02252 string EventsModule::DescribeEvent( const JoystickAxisEvent & axisEvent )
02253 {
02254
02255 #if OSDL_USES_SDL
02256
02257 return "Joystick #" + Ceylan::toNumericalString( axisEvent.which )
02258 + " had axis #" + Ceylan::toNumericalString( axisEvent.axis )
02259 + " moved to " + Ceylan::toString( axisEvent.value ) + "." ;
02260
02261 #else // OSDL_USES_SDL
02262
02263 return NoSDLSupportAvailable ;
02264
02265 #endif // OSDL_USES_SDL
02266
02267 }
02268
02269
02270
02271 string EventsModule::DescribeEvent( const JoystickTrackballEvent & ballEvent )
02272 {
02273
02274 #if OSDL_USES_SDL
02275
02276 return "Joystick #" + Ceylan::toNumericalString( ballEvent.which )
02277 + " had trackball #" + Ceylan::toNumericalString( ballEvent.ball )
02278 + " moved of ("
02279 + Ceylan::toString( ballEvent.xrel ) + ";"
02280 + Ceylan::toString( ballEvent.yrel ) + ")." ;
02281
02282 #else // OSDL_USES_SDL
02283
02284 return NoSDLSupportAvailable ;
02285
02286 #endif // OSDL_USES_SDL
02287
02288 }
02289
02290
02291
02292 string EventsModule::DescribeEvent( const JoystickHatEvent & hatEvent )
02293 {
02294
02295 #if OSDL_USES_SDL
02296
02297 return "Joystick #" + Ceylan::toString( hatEvent.which ) + " had hat #"
02298 + Ceylan::toNumericalString( hatEvent.hat ) + " set to position "
02299 + Ceylan::toString( hatEvent.value ) + "." ;
02300
02301 #else // OSDL_USES_SDL
02302
02303 return NoSDLSupportAvailable ;
02304
02305 #endif // OSDL_USES_SDL
02306
02307 }
02308
02309
02310
02311 string EventsModule::DescribeEvent( const JoystickButtonEvent & buttonEvent )
02312 {
02313
02314 #if OSDL_USES_SDL
02315
02316 return "Joystick #" + Ceylan::toNumericalString( buttonEvent.which )
02317 + " had button #" + Ceylan::toNumericalString( buttonEvent.button )
02318 + ( ( buttonEvent.state == SDL_PRESSED ) ?
02319 " pressed.": " released." ) ;
02320
02321 #else // OSDL_USES_SDL
02322
02323 return NoSDLSupportAvailable ;
02324
02325 #endif // OSDL_USES_SDL
02326
02327 }
02328
02329
02330
02331 string EventsModule::DescribeEvent( const UserRequestedQuitEvent & quitEvent )
02332 {
02333
02334 #if OSDL_USES_SDL
02335
02336 return "User requested to quit." ;
02337
02338 #else // OSDL_USES_SDL
02339
02340 return NoSDLSupportAvailable ;
02341
02342 #endif // OSDL_USES_SDL
02343
02344 }
02345
02346
02347
02348 string EventsModule::DescribeEvent(
02349 const SystemSpecificWindowManagerEvent & windowManagerEvent )
02350 {
02351
02352 #if OSDL_USES_SDL
02353
02354
02355
02356
02357
02358
02359
02360 return "System-specific window manager event occured." ;
02361
02362 #else // OSDL_USES_SDL
02363
02364 return NoSDLSupportAvailable ;
02365
02366 #endif // OSDL_USES_SDL
02367
02368 }
02369
02370
02371
02372 string EventsModule::DescribeEvent( const WindowResizedEvent & resizeEvent )
02373 {
02374
02375 #if OSDL_USES_SDL
02376
02377 return "Window resized to (" + Ceylan::toString( resizeEvent.w )
02378 + ";" + Ceylan::toString( resizeEvent.h ) + ")." ;
02379
02380 #else // OSDL_USES_SDL
02381
02382 return NoSDLSupportAvailable ;
02383
02384 #endif // OSDL_USES_SDL
02385
02386 }
02387
02388
02389
02390 string EventsModule::DescribeEvent( const ScreenExposedEvent & redrawEvent )
02391 {
02392
02393 #if OSDL_USES_SDL
02394
02395 return "Window exposed, needs to be redrawn." ;
02396
02397 #else // OSDL_USES_SDL
02398
02399 return NoSDLSupportAvailable ;
02400
02401 #endif // OSDL_USES_SDL
02402
02403 }
02404
02405
02406
02407 string EventsModule::DescribeEvent( const UserEvent & userEvent )
02408 {
02409
02410 #if OSDL_USES_SDL
02411
02412 return "User-defined event occured (code #"
02413 + Ceylan::toString( userEvent.code ) + ")." ;
02414
02415 #else // OSDL_USES_SDL
02416
02417 return NoSDLSupportAvailable ;
02418
02419 #endif // OSDL_USES_SDL
02420
02421 }
02422
02423
02424
02425 bool EventsModule::IsEventsInitialized()
02426 {
02427
02428 return _EventsInitialized ;
02429
02430 }
02431
02432
02433
02434 Ceylan::System::Microsecond EventsModule::EvaluateCallbackduration(
02435 Ceylan::System::Callback callback, void * callbackData )
02436 {
02437
02438
02439 Microsecond startedMicrosec ;
02440 Second startedSec ;
02441
02442 Microsecond endedMicrosec ;
02443 Second endedSec ;
02444
02445 getPreciseTime( startedSec, startedMicrosec ) ;
02446
02447 (*callback)( callbackData ) ;
02448
02449 getPreciseTime( endedSec, endedMicrosec ) ;
02450
02451 Microsecond callbackExpectedMaxDuration =
02452 static_cast<Microsecond>( 1.2 *
02453 getDurationBetween( startedSec, startedMicrosec,
02454 endedSec, endedMicrosec ) ) ;
02455
02456 LOG_DEBUG_EVENTS( "EventsModule::EvaluateCallbackduration: duration for "
02457 "idle callback evaluated to "
02458 + Ceylan::toString( callbackExpectedMaxDuration )
02459 + " microseconds." ) ;
02460
02461 return callbackExpectedMaxDuration ;
02462
02463 }