minor fix on CMakeLists for checking MSVC_VERSION
[freeglut] / src / fg_callbacks.c
index 7e399a9..11924b3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * freeglut_callbacks.c
+ * fg_callbacks.c
  *
  * The callbacks setting methods.
  *
 
 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
 
-/*
- * All of the callbacks setting methods can be generalized to this:
- */
-#define SET_CALLBACK(a)                                         \
-do                                                              \
-{                                                               \
-    if( fgStructure.CurrentWindow == NULL )                     \
-        return;                                                 \
-    SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
-} while( 0 )
 
 /*
- * Sets the Display callback for the current window
+ * Global callbacks.
  */
-void FGAPIENTRY glutDisplayFunc( void (* callback)( void ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
-    if( !callback )
-        fgError( "Fatal error in program.  NULL display callback not "
-                 "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
-    SET_CALLBACK( Display );
-}
-
-/*
- * Sets the Reshape callback for the current window
- */
-void FGAPIENTRY glutReshapeFunc( void (* callback)( int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
-    SET_CALLBACK( Reshape );
-}
-
-/*
- * Sets the Keyboard callback for the current window
- */
-void FGAPIENTRY glutKeyboardFunc( void (* callback)
-                                  ( unsigned char, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutKeyboardFunc" );
-    SET_CALLBACK( Keyboard );
-}
-
-/*
- * Sets the Special callback for the current window
- */
-void FGAPIENTRY glutSpecialFunc( void (* callback)( int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpecialFunc" );
-    SET_CALLBACK( Special );
-}
-
-/*
- * Sets the global idle callback
- */
-void FGAPIENTRY glutIdleFunc( void (* callback)( void ) )
+/* Sets the global idle callback */
+void FGAPIENTRY glutIdleFunc( FGCBIdle callback )
 {
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
     fgState.IdleCallback = callback;
 }
 
-/*
- * Sets the Timer callback for the current window
- */
-void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
-                               int timerID )
+/* Creates a timer and sets its callback */
+void FGAPIENTRY glutTimerFunc( unsigned int timeOut, FGCBTimer callback, int timerID )
 {
     SFG_Timer *timer, *node;
 
@@ -115,6 +63,7 @@ void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
     timer->ID        = timerID;
     timer->TriggerTime = fgElapsedTime() + timeOut;
 
+    /* Insert such that timers are sorted by end-time */
     for( node = fgState.Timers.First; node; node = node->Node.Next )
     {
         if( node->TriggerTime > timer->TriggerTime )
@@ -124,190 +73,194 @@ void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
 }
 
-/*
- * Sets the Visibility callback for the current window.
- */
-static void fghVisibility( int status )
+/* Deprecated version of glutMenuStatusFunc callback setting method */
+void FGAPIENTRY glutMenuStateFunc( FGCBMenuState callback )
 {
-    int glut_status = GLUT_VISIBLE;
-
-    FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
-    freeglut_return_if_fail( fgStructure.CurrentWindow );
-
-    if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
-        glut_status = GLUT_NOT_VISIBLE;
-    INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( glut_status ) );
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
+    fgState.MenuStateCallback = callback;
 }
 
-void FGAPIENTRY glutVisibilityFunc( void (* callback)( int ) )
+/* Sets the global menu status callback for the current window */
+void FGAPIENTRY glutMenuStatusFunc( FGCBMenuStatus callback )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
-    SET_CALLBACK( Visibility );
-
-    if( callback )
-        glutWindowStatusFunc( fghVisibility );
-    else
-        glutWindowStatusFunc( NULL );
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
+    fgState.MenuStatusCallback = callback;
 }
 
+
 /*
- * Sets the keyboard key release callback for the current window
+ * Menu specific callbacks.
  */
-void FGAPIENTRY glutKeyboardUpFunc( void (* callback)
-                                    ( unsigned char, int, int ) )
+/* Callback upon menu destruction */
+void FGAPIENTRY glutMenuDestroyFunc( FGCBDestroy callback )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutKeyboardUpFunc" );
-    SET_CALLBACK( KeyboardUp );
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
+    if( fgStructure.CurrentMenu )
+        fgStructure.CurrentMenu->Destroy = callback;
 }
 
+
 /*
- * Sets the special key release callback for the current window
+ * All of the window-specific callbacks setting methods can be generalized to this:
  */
-void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpecialUpFunc" );
-    SET_CALLBACK( SpecialUp );
-}
-
+#define SET_CALLBACK(a)                                         \
+do                                                              \
+{                                                               \
+    if( fgStructure.CurrentWindow == NULL )                     \
+        return;                                                 \
+    SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
+} while( 0 )
 /*
- * Sets the joystick callback and polling rate for the current window
+ * And almost every time the callback setter function can be implemented like this:
  */
-void FGAPIENTRY glutJoystickFunc( void (* callback)
-                                  ( unsigned int, int, int, int ),
-                                  int pollInterval )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
-    fgInitialiseJoysticks ();
-
-    if ( ( ( fgStructure.CurrentWindow->State.JoystickPollRate < 0 ) ||
-           !FETCH_WCB(*fgStructure.CurrentWindow,Joystick) ) &&  /* Joystick callback was disabled */
-         ( callback && ( pollInterval >= 0 ) ) )               /* but is now enabled */
-        ++fgState.NumActiveJoysticks;
-    else if ( ( ( fgStructure.CurrentWindow->State.JoystickPollRate >= 0 ) &&
-                FETCH_WCB(*fgStructure.CurrentWindow,Joystick) ) &&  /* Joystick callback was enabled */
-              ( !callback || ( pollInterval < 0 ) ) )              /* but is now disabled */
-        --fgState.NumActiveJoysticks;
-
-    SET_CALLBACK( Joystick );
-    fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
+#define IMPLEMENT_CALLBACK_FUNC_2NAME(a,b)                      \
+void FGAPIENTRY glut##a##Func( FGCB##b callback )               \
+{                                                               \
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glut"#a"Func" );    \
+    SET_CALLBACK( b );                                          \
+}
+#define IMPLEMENT_CALLBACK_FUNC(a) IMPLEMENT_CALLBACK_FUNC_2NAME(a,a)
+
+/* Implement all these callback setter functions... */
+IMPLEMENT_CALLBACK_FUNC(Position)
+IMPLEMENT_CALLBACK_FUNC(Keyboard)
+IMPLEMENT_CALLBACK_FUNC(KeyboardUp)
+IMPLEMENT_CALLBACK_FUNC(Special)
+IMPLEMENT_CALLBACK_FUNC(SpecialUp)
+IMPLEMENT_CALLBACK_FUNC(Mouse)
+IMPLEMENT_CALLBACK_FUNC(MouseWheel)
+IMPLEMENT_CALLBACK_FUNC(Motion)
+IMPLEMENT_CALLBACK_FUNC_2NAME(PassiveMotion,Passive)
+IMPLEMENT_CALLBACK_FUNC(Entry)
+/* glutWMCloseFunc is an alias for glutCloseFunc; both set the window's Destroy callback */
+IMPLEMENT_CALLBACK_FUNC_2NAME(Close,Destroy)
+IMPLEMENT_CALLBACK_FUNC_2NAME(WMClose,Destroy)
+IMPLEMENT_CALLBACK_FUNC(OverlayDisplay)
+IMPLEMENT_CALLBACK_FUNC(WindowStatus)
+IMPLEMENT_CALLBACK_FUNC(ButtonBox)
+IMPLEMENT_CALLBACK_FUNC(Dials)
+IMPLEMENT_CALLBACK_FUNC(TabletMotion)
+IMPLEMENT_CALLBACK_FUNC(TabletButton)
+IMPLEMENT_CALLBACK_FUNC(MultiEntry)
+IMPLEMENT_CALLBACK_FUNC(MultiButton)
+IMPLEMENT_CALLBACK_FUNC(MultiMotion)
+IMPLEMENT_CALLBACK_FUNC(MultiPassive)
+IMPLEMENT_CALLBACK_FUNC(InitContext)
+IMPLEMENT_CALLBACK_FUNC(AppStatus)
 
-    fgStructure.CurrentWindow->State.JoystickLastPoll =
-        fgElapsedTime() - fgStructure.CurrentWindow->State.JoystickPollRate;
 
-    if( fgStructure.CurrentWindow->State.JoystickLastPoll < 0 )
-        fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
-}
 
 /*
- * Sets the mouse callback for the current window
+ * Sets the Display callback for the current window
  */
-void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
+void FGAPIENTRY glutDisplayFunc( FGCBDisplay callback )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMouseFunc" );
-    SET_CALLBACK( Mouse );
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
+    if( !callback )
+        fgError( "Fatal error in program.  NULL display callback not "
+                 "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
+    SET_CALLBACK( Display );
 }
 
-/*
- * Sets the mouse wheel callback for the current window
- */
-void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) )
+void fghDefaultReshape(int width, int height)
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMouseWheelFunc" );
-    SET_CALLBACK( MouseWheel );
+    glViewport( 0, 0, width, height );
 }
 
-/*
- * Sets the mouse motion callback for the current window (one or more buttons
- * are pressed)
- */
-void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
+void FGAPIENTRY glutReshapeFunc( FGCBReshape callback )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMotionFunc" );
-    SET_CALLBACK( Motion );
-}
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
+    
+    if( !callback )
+        callback = fghDefaultReshape;
 
-/*
- * Sets the passive mouse motion callback for the current window (no mouse
- * buttons are pressed)
- */
-void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPassiveMotionFunc" );
-    SET_CALLBACK( Passive );
+    SET_CALLBACK( Reshape );
 }
 
 /*
- * Window mouse entry/leave callback
+ * Sets the Visibility callback for the current window.
+ * NB: the Visibility func is deprecated in favor of the WindowStatus func,
+ * which provides more detail. The visibility func callback is implemented
+ * as a translation step from the windowStatus func. When the user sets the
+ * windowStatus func, any visibility func is overwritten.
+ * DEVELOPER NOTE: in the library, only invoke the window status func, this
+ * gets automatically translated to the visibility func if thats what the
+ * user has set.
+ * window status is kind of anemic on win32 as there are no window messages
+ * to notify us that the window is covered by other windows or not.
+ * Should one want to query this, see
+ * http://stackoverflow.com/questions/5445889/get-which-process-window-is-actually-visible-in-c-sharp
+ * for an implementation outline (but it would be polling based, not push based).
  */
-void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
+static void fghVisibility( int status )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEntryFunc" );
-    SET_CALLBACK( Entry );
-}
+    int vis_status;
 
-/*
- * Window destruction callbacks
- */
-void FGAPIENTRY glutCloseFunc( void (* callback)( void ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCloseFunc" );
-    SET_CALLBACK( Destroy );
-}
+    FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
+    freeglut_return_if_fail( fgStructure.CurrentWindow );
 
-void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWMCloseFunc" );
-    glutCloseFunc( callback );
-}
+    /* Translate window status func states to visibility states */
+    if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
+        vis_status = GLUT_NOT_VISIBLE;
+    else    /* GLUT_FULLY_RETAINED, GLUT_PARTIALLY_RETAINED */
+        vis_status = GLUT_VISIBLE;
 
-/* A. Donev: Destruction callback for menus */
-void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
-    if( fgStructure.CurrentMenu )
-        fgStructure.CurrentMenu->Destroy = callback;
+    INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( vis_status ) );
 }
 
-/*
- * Deprecated version of glutMenuStatusFunc callback setting method
- */
-void FGAPIENTRY glutMenuStateFunc( void (* callback)( int ) )
+void FGAPIENTRY glutVisibilityFunc( FGCBVisibility callback )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
-    fgState.MenuStateCallback = callback;
-}
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
+    SET_CALLBACK( Visibility );
 
-/*
- * Sets the global menu status callback for the current window
- */
-void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
-    fgState.MenuStatusCallback = callback;
+    if( callback )
+        glutWindowStatusFunc( fghVisibility );
+    else
+        glutWindowStatusFunc( NULL );
 }
 
 /*
- * Sets the overlay display callback for the current window
+ * Sets the joystick callback and polling rate for the current window
  */
-void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
+void FGAPIENTRY glutJoystickFunc( FGCBJoystick callback, int pollInterval )
 {
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutOverlayDisplayFunc" );
-    SET_CALLBACK( OverlayDisplay );
-}
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
+    fgInitialiseJoysticks ();
 
-/*
- * Sets the window status callback for the current window
- */
-void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWindowStatusFunc" );
-    SET_CALLBACK( WindowStatus );
+    if ( (
+           fgStructure.CurrentWindow->State.JoystickPollRate <= 0 ||        /* Joystick callback was disabled */
+           !FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
+         ) &&
+         ( 
+           callback && ( pollInterval > 0 )                                 /* but is now enabled */
+         ) )
+        ++fgState.NumActiveJoysticks;
+    else if ( ( 
+                fgStructure.CurrentWindow->State.JoystickPollRate > 0 &&    /* Joystick callback was enabled */
+                FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
+              ) &&  
+              ( 
+                !callback || ( pollInterval <= 0 )                          /* but is now disabled */
+              ) )
+        --fgState.NumActiveJoysticks;
+
+    SET_CALLBACK( Joystick );
+    fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
+
+    /* set last poll time such that joystick will be polled asap */
+    fgStructure.CurrentWindow->State.JoystickLastPoll = fgElapsedTime();
+    if (fgStructure.CurrentWindow->State.JoystickLastPoll < pollInterval)
+        fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
+    else
+        fgStructure.CurrentWindow->State.JoystickLastPoll -= pollInterval;
 }
 
+
+
 /*
  * Sets the spaceball motion callback for the current window
  */
-void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
+void FGAPIENTRY glutSpaceballMotionFunc( FGCBSpaceMotion callback )
 {
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
     fgInitialiseSpaceball();
@@ -318,7 +271,7 @@ void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
 /*
  * Sets the spaceball rotate callback for the current window
  */
-void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
+void FGAPIENTRY glutSpaceballRotateFunc( FGCBSpaceRotation callback )
 {
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
     fgInitialiseSpaceball();
@@ -329,7 +282,7 @@ void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
 /*
  * Sets the spaceball button callback for the current window
  */
-void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
+void FGAPIENTRY glutSpaceballButtonFunc( FGCBSpaceButton callback )
 {
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
     fgInitialiseSpaceball();
@@ -337,76 +290,4 @@ void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
     SET_CALLBACK( SpaceButton );
 }
 
-/*
- * Sets the button box callback for the current window
- */
-void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutButtonBoxFunc" );
-    SET_CALLBACK( ButtonBox );
-}
-
-/*
- * Sets the dials box callback for the current window
- */
-void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDialsFunc" );
-    SET_CALLBACK( Dials );
-}
-
-/*
- * Sets the tablet motion callback for the current window
- */
-void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTabletMotionFunc" );
-    SET_CALLBACK( TabletMotion );
-}
-
-/*
- * Sets the tablet buttons callback for the current window
- */
-void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTabletButtonFunc" );
-    SET_CALLBACK( TabletButton );
-}
-
-/*
- * Sets the multi-pointer entry callback for the current window
- */
-void FGAPIENTRY glutMultiEntryFunc( void (* callback)(int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMultiEntryFunc" );
-    SET_CALLBACK( MultiEntry );
-}
-
-/*
- * Sets the multi-pointer button callback for the current window
- */
-void FGAPIENTRY glutMultiButtonFunc( void (* callback)(int, int, int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMultiButtonFunc" );
-    SET_CALLBACK( MultiButton );
-}
-
-/*
- * Sets the multi-pointer motion callback for the current window
- */
-void FGAPIENTRY glutMultiMotionFunc( void (* callback)(int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMultiMotionFunc" );
-    SET_CALLBACK( MultiMotion );
-}
-
-/*
- * Sets the multi-pointer passive motion callback for the current window
- */
-void FGAPIENTRY glutMultiPassiveFunc( void (* callback)(int, int, int ) )
-{
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMultiPassiveFunc" );
-    SET_CALLBACK( MultiPassive );
-}
-
 /*** END OF FILE ***/