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 glutIdleFunc( FGCBIdle callback )
40 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
41 fgState.IdleCallback = callback;
44 /* Creates a timer and sets its callback */
45 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, FGCBTimer callback, int timerID )
47 SFG_Timer *timer, *node;
49 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFunc" );
51 if( (timer = fgState.FreeTimers.Last) )
53 fgListRemove( &fgState.FreeTimers, &timer->Node );
57 if( ! (timer = malloc(sizeof(SFG_Timer))) )
58 fgError( "Fatal error: "
59 "Memory allocation failure in glutTimerFunc()" );
62 timer->Callback = callback;
64 timer->TriggerTime = fgElapsedTime() + timeOut;
66 for( node = fgState.Timers.First; node; node = node->Node.Next )
68 if( node->TriggerTime > timer->TriggerTime )
72 fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
75 /* Deprecated version of glutMenuStatusFunc callback setting method */
76 void FGAPIENTRY glutMenuStateFunc( FGCBMenuState callback )
78 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
79 fgState.MenuStateCallback = callback;
82 /* Sets the global menu status callback for the current window */
83 void FGAPIENTRY glutMenuStatusFunc( FGCBMenuStatus callback )
85 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
86 fgState.MenuStatusCallback = callback;
91 * Menu specific callbacks.
93 /* Callback upon menu destruction */
94 void FGAPIENTRY glutMenuDestroyFunc( FGCBDestroy callback )
96 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
97 if( fgStructure.CurrentMenu )
98 fgStructure.CurrentMenu->Destroy = callback;
103 * All of the window-specific callbacks setting methods can be generalized to this:
105 #define SET_CALLBACK(a) \
108 if( fgStructure.CurrentWindow == NULL ) \
110 SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
113 * And almost every time the callback setter function can be implemented like this:
115 #define IMPLEMENT_CALLBACK_FUNC_2NAME(a,b) \
116 void FGAPIENTRY glut##a##Func( FGCB##b callback ) \
118 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glut"#a"Func" ); \
121 #define IMPLEMENT_CALLBACK_FUNC(a) IMPLEMENT_CALLBACK_FUNC_2NAME(a,a)
123 /* Implement all these callback setter functions... */
124 IMPLEMENT_CALLBACK_FUNC(Position);
125 IMPLEMENT_CALLBACK_FUNC(Keyboard);
126 IMPLEMENT_CALLBACK_FUNC(KeyboardUp);
127 IMPLEMENT_CALLBACK_FUNC(Special);
128 IMPLEMENT_CALLBACK_FUNC(SpecialUp);
129 IMPLEMENT_CALLBACK_FUNC(Mouse);
130 IMPLEMENT_CALLBACK_FUNC(MouseWheel);
131 IMPLEMENT_CALLBACK_FUNC(Motion);
132 IMPLEMENT_CALLBACK_FUNC_2NAME(PassiveMotion,Passive);
133 IMPLEMENT_CALLBACK_FUNC(Entry);
134 /* glutWMCloseFunc is an alias for glutCloseFunc; both set the window's Destroy callback */
135 IMPLEMENT_CALLBACK_FUNC_2NAME(Close,Destroy);
136 IMPLEMENT_CALLBACK_FUNC_2NAME(WMClose,Destroy);
137 IMPLEMENT_CALLBACK_FUNC(OverlayDisplay);
138 IMPLEMENT_CALLBACK_FUNC(WindowStatus);
139 IMPLEMENT_CALLBACK_FUNC(ButtonBox);
140 IMPLEMENT_CALLBACK_FUNC(Dials);
141 IMPLEMENT_CALLBACK_FUNC(TabletMotion);
142 IMPLEMENT_CALLBACK_FUNC(TabletButton);
143 IMPLEMENT_CALLBACK_FUNC(MultiEntry);
144 IMPLEMENT_CALLBACK_FUNC(MultiButton);
145 IMPLEMENT_CALLBACK_FUNC(MultiMotion);
146 IMPLEMENT_CALLBACK_FUNC(MultiPassive);
147 IMPLEMENT_CALLBACK_FUNC(InitContext);
148 IMPLEMENT_CALLBACK_FUNC(AppStatus);
153 * Sets the Display callback for the current window
155 void FGAPIENTRY glutDisplayFunc( FGCBDisplay callback )
157 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
159 fgError( "Fatal error in program. NULL display callback not "
160 "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
161 SET_CALLBACK( Display );
164 void fghDefaultReshape(int width, int height)
166 glViewport( 0, 0, width, height );
169 void FGAPIENTRY glutReshapeFunc( FGCBReshape callback )
171 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
174 callback = fghDefaultReshape;
176 SET_CALLBACK( Reshape );
180 * Sets the Visibility callback for the current window.
181 * NB: the Visibility func is deprecated in favor of the WindowStatus func,
182 * which provides more detail. The visibility func callback is implemented
183 * as a translation step from the windowStatus func. When the user sets the
184 * windowStatus func, any visibility func is overwritten.
185 * DEVELOPER NOTE: in the library, only invoke the window status func, this
186 * gets automatically translated to the visibility func if thats what the
188 * window status is kind of anemic on win32 as there are no window messages
189 * to notify us that the window is covered by other windows or not.
190 * Should one want to query this, see
191 * http://stackoverflow.com/questions/5445889/get-which-process-window-is-actually-visible-in-c-sharp
192 * for an implementation outline (but it would be polling based, not push based).
194 static void fghVisibility( int status )
198 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
199 freeglut_return_if_fail( fgStructure.CurrentWindow );
201 /* Translate window status func states to visibility states */
202 if( ( GLUT_HIDDEN == status ) || ( GLUT_FULLY_COVERED == status ) )
203 vis_status = GLUT_NOT_VISIBLE;
204 else /* GLUT_FULLY_RETAINED, GLUT_PARTIALLY_RETAINED */
205 vis_status = GLUT_VISIBLE;
207 INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( vis_status ) );
210 void FGAPIENTRY glutVisibilityFunc( FGCBVisibility callback )
212 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
213 SET_CALLBACK( Visibility );
216 glutWindowStatusFunc( fghVisibility );
218 glutWindowStatusFunc( NULL );
222 * Sets the joystick callback and polling rate for the current window
224 void FGAPIENTRY glutJoystickFunc( FGCBJoystick callback, int pollInterval )
226 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
227 fgInitialiseJoysticks ();
230 fgStructure.CurrentWindow->State.JoystickPollRate <= 0 || /* Joystick callback was disabled */
231 !FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
234 callback && ( pollInterval > 0 ) /* but is now enabled */
236 ++fgState.NumActiveJoysticks;
238 fgStructure.CurrentWindow->State.JoystickPollRate > 0 && /* Joystick callback was enabled */
239 FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
242 !callback || ( pollInterval <= 0 ) /* but is now disabled */
244 --fgState.NumActiveJoysticks;
246 SET_CALLBACK( Joystick );
247 fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
249 fgStructure.CurrentWindow->State.JoystickLastPoll =
250 fgElapsedTime() - fgStructure.CurrentWindow->State.JoystickPollRate;
252 if( fgStructure.CurrentWindow->State.JoystickLastPoll < 0 )
253 fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
259 * Sets the spaceball motion callback for the current window
261 void FGAPIENTRY glutSpaceballMotionFunc( FGCBSpaceMotion callback )
263 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
264 fgInitialiseSpaceball();
266 SET_CALLBACK( SpaceMotion );
270 * Sets the spaceball rotate callback for the current window
272 void FGAPIENTRY glutSpaceballRotateFunc( FGCBSpaceRotation callback )
274 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
275 fgInitialiseSpaceball();
277 SET_CALLBACK( SpaceRotation );
281 * Sets the spaceball button callback for the current window
283 void FGAPIENTRY glutSpaceballButtonFunc( FGCBSpaceButton callback )
285 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
286 fgInitialiseSpaceball();
288 SET_CALLBACK( SpaceButton );
291 /*** END OF FILE ***/