4 * The callbacks setting methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
31 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
37 /* Sets the global idle callback */
38 void FGAPIENTRY glutIdleFuncUcall( FGCBIdleUC callback, FGCBUserData userData )
40 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFuncUcall" );
41 fgState.IdleCallback = callback;
42 fgState.IdleCallbackData = userData;
45 void glutIdleFuncCallback( void* userData )
47 FGCBIdle callback = (FGCBIdle)userData;
51 void FGAPIENTRY glutIdleFunc( FGCBIdle callback )
53 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
54 glutIdleFuncUcall( glutIdleFuncCallback, (FGCBUserData)callback );
57 /* Creates a timer and sets its callback */
58 void FGAPIENTRY glutTimerFuncUcall( unsigned int timeOut, FGCBTimerUC callback, int timerID, FGCBUserData userData )
60 SFG_Timer *timer, *node;
62 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFuncUcall" );
64 if( (timer = fgState.FreeTimers.Last) )
66 fgListRemove( &fgState.FreeTimers, &timer->Node );
70 if( ! (timer = malloc(sizeof(SFG_Timer))) )
71 fgError( "Fatal error: "
72 "Memory allocation failure in glutTimerFunc()" );
75 timer->Callback = callback;
76 timer->CallbackData = userData;
78 timer->TriggerTime = fgElapsedTime() + timeOut;
80 /* Insert such that timers are sorted by end-time */
81 for( node = fgState.Timers.First; node; node = node->Node.Next )
83 if( node->TriggerTime > timer->TriggerTime )
87 fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
90 void glutTimerFuncCallback( int ID, FGCBUserData userData )
92 FGCBTimer callback = (FGCBTimer)userData;
96 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, FGCBTimer callback, int timerID )
98 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFunc" );
99 glutTimerFuncUcall( timeOut, glutTimerFuncCallback, timerID, (FGCBUserData)callback );
102 /* Deprecated version of glutMenuStatusFunc callback setting method */
103 void FGAPIENTRY glutMenuStateFunc( FGCBMenuState callback )
105 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
106 fgState.MenuStateCallback = callback;
109 /* Sets the global menu status callback for the current window */
110 void FGAPIENTRY glutMenuStatusFunc( FGCBMenuStatus callback )
112 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
113 fgState.MenuStatusCallback = callback;
118 * Menu specific callbacks.
120 /* Callback upon menu destruction */
121 void FGAPIENTRY glutMenuDestroyFunc( FGCBDestroy callback )
123 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
124 if( fgStructure.CurrentMenu )
125 fgStructure.CurrentMenu->Destroy = callback;
130 * All of the window-specific callbacks setting methods can be generalized to this:
132 #define SET_CALLBACK(a) \
135 if( fgStructure.CurrentWindow == NULL ) \
137 SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
140 * And almost every time the callback setter function can be implemented like this:
142 #define IMPLEMENT_CALLBACK_FUNC_2NAME(a,b) \
143 void FGAPIENTRY glut##a##Func( FGCB##b callback ) \
145 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glut"#a"Func" ); \
148 #define IMPLEMENT_CALLBACK_FUNC(a) IMPLEMENT_CALLBACK_FUNC_2NAME(a,a)
150 /* Implement all these callback setter functions... */
151 IMPLEMENT_CALLBACK_FUNC(Position)
152 IMPLEMENT_CALLBACK_FUNC(Keyboard)
153 IMPLEMENT_CALLBACK_FUNC(KeyboardUp)
154 IMPLEMENT_CALLBACK_FUNC(Special)
155 IMPLEMENT_CALLBACK_FUNC(SpecialUp)
156 IMPLEMENT_CALLBACK_FUNC(Mouse)
157 IMPLEMENT_CALLBACK_FUNC(MouseWheel)
158 IMPLEMENT_CALLBACK_FUNC(Motion)
159 IMPLEMENT_CALLBACK_FUNC_2NAME(PassiveMotion,Passive)
160 IMPLEMENT_CALLBACK_FUNC(Entry)
161 /* glutWMCloseFunc is an alias for glutCloseFunc; both set the window's Destroy callback */
162 IMPLEMENT_CALLBACK_FUNC_2NAME(Close,Destroy)
163 IMPLEMENT_CALLBACK_FUNC_2NAME(WMClose,Destroy)
164 IMPLEMENT_CALLBACK_FUNC(OverlayDisplay)
165 IMPLEMENT_CALLBACK_FUNC(WindowStatus)
166 IMPLEMENT_CALLBACK_FUNC(ButtonBox)
167 IMPLEMENT_CALLBACK_FUNC(Dials)
168 IMPLEMENT_CALLBACK_FUNC(TabletMotion)
169 IMPLEMENT_CALLBACK_FUNC(TabletButton)
170 IMPLEMENT_CALLBACK_FUNC(MultiEntry)
171 IMPLEMENT_CALLBACK_FUNC(MultiButton)
172 IMPLEMENT_CALLBACK_FUNC(MultiMotion)
173 IMPLEMENT_CALLBACK_FUNC(MultiPassive)
174 IMPLEMENT_CALLBACK_FUNC(InitContext)
175 IMPLEMENT_CALLBACK_FUNC(AppStatus)
180 * Sets the Display callback for the current window
182 void FGAPIENTRY glutDisplayFunc( FGCBDisplay callback )
184 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
186 fgError( "Fatal error in program. NULL display callback not "
187 "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
188 SET_CALLBACK( Display );
191 void fghDefaultReshape(int width, int height)
193 glViewport( 0, 0, width, height );
196 void FGAPIENTRY glutReshapeFunc( FGCBReshape callback )
198 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
201 callback = fghDefaultReshape;
203 SET_CALLBACK( Reshape );
207 * Sets the Visibility callback for the current window.
208 * NB: the Visibility func is deprecated in favor of the WindowStatus func,
209 * which provides more detail. The visibility func callback is implemented
210 * as a translation step from the windowStatus func. When the user sets the
211 * windowStatus func, any visibility func is overwritten.
212 * DEVELOPER NOTE: in the library, only invoke the window status func, this
213 * gets automatically translated to the visibility func if thats what the
215 * window status is kind of anemic on win32 as there are no window messages
216 * to notify us that the window is covered by other windows or not.
217 * Should one want to query this, see
218 * http://stackoverflow.com/questions/5445889/get-which-process-window-is-actually-visible-in-c-sharp
219 * for an implementation outline (but it would be polling based, not push based).
221 static void fghVisibility( int status )
225 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
226 freeglut_return_if_fail( fgStructure.CurrentWindow );
228 /* Translate window status func states to visibility states */
229 if( ( GLUT_HIDDEN == status ) || ( GLUT_FULLY_COVERED == status ) )
230 vis_status = GLUT_NOT_VISIBLE;
231 else /* GLUT_FULLY_RETAINED, GLUT_PARTIALLY_RETAINED */
232 vis_status = GLUT_VISIBLE;
234 INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( vis_status ) );
237 void FGAPIENTRY glutVisibilityFunc( FGCBVisibility callback )
239 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
240 SET_CALLBACK( Visibility );
243 glutWindowStatusFunc( fghVisibility );
245 glutWindowStatusFunc( NULL );
249 * Sets the joystick callback and polling rate for the current window
251 void FGAPIENTRY glutJoystickFunc( FGCBJoystick callback, int pollInterval )
253 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
254 fgInitialiseJoysticks ();
257 fgStructure.CurrentWindow->State.JoystickPollRate <= 0 || /* Joystick callback was disabled */
258 !FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
261 callback && ( pollInterval > 0 ) /* but is now enabled */
263 ++fgState.NumActiveJoysticks;
265 fgStructure.CurrentWindow->State.JoystickPollRate > 0 && /* Joystick callback was enabled */
266 FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
269 !callback || ( pollInterval <= 0 ) /* but is now disabled */
271 --fgState.NumActiveJoysticks;
273 SET_CALLBACK( Joystick );
274 fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
276 /* set last poll time such that joystick will be polled asap */
277 fgStructure.CurrentWindow->State.JoystickLastPoll = fgElapsedTime();
278 if (fgStructure.CurrentWindow->State.JoystickLastPoll < pollInterval)
279 fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
281 fgStructure.CurrentWindow->State.JoystickLastPoll -= pollInterval;
287 * Sets the spaceball motion callback for the current window
289 void FGAPIENTRY glutSpaceballMotionFunc( FGCBSpaceMotion callback )
291 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
292 fgInitialiseSpaceball();
294 SET_CALLBACK( SpaceMotion );
298 * Sets the spaceball rotate callback for the current window
300 void FGAPIENTRY glutSpaceballRotateFunc( FGCBSpaceRotation callback )
302 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
303 fgInitialiseSpaceball();
305 SET_CALLBACK( SpaceRotation );
309 * Sets the spaceball button callback for the current window
311 void FGAPIENTRY glutSpaceballButtonFunc( FGCBSpaceButton callback )
313 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
314 fgInitialiseSpaceball();
316 SET_CALLBACK( SpaceButton );
319 /*** END OF FILE ***/