Adding initialization checking to all GLUT interface functions and removing asserts...
[freeglut] / src / freeglut_callbacks.c
1 /*
2  * freeglut_callbacks.c
3  *
4  * The callbacks setting methods.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
34
35
36 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
37
38 /*
39  * All of the callbacks setting methods can be generalized to this:
40  */
41 #define SET_CALLBACK(a)              \
42     if( fgStructure.Window == NULL ) \
43         return;                      \
44     SET_WCB( ( *( fgStructure.Window ) ), a, callback );
45
46 /*
47  * Sets the Display callback for the current window
48  */
49 void FGAPIENTRY glutDisplayFunc( void (* callback)( void ) )
50 {
51     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
52     if( !callback )
53         fgError( "Fatal error in program.  NULL display callback not "
54                  "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
55     SET_CALLBACK( Display );
56 }
57
58 /*
59  * Sets the Reshape callback for the current window
60  */
61 void FGAPIENTRY glutReshapeFunc( void (* callback)( int, int ) )
62 {
63     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
64     SET_CALLBACK( Reshape );
65 }
66
67 /*
68  * Sets the Keyboard callback for the current window
69  */
70 void FGAPIENTRY glutKeyboardFunc( void (* callback)
71                                   ( unsigned char, int, int ) )
72 {
73     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutKeyboardFunc" );
74     SET_CALLBACK( Keyboard );
75 }
76
77 /*
78  * Sets the Special callback for the current window
79  */
80 void FGAPIENTRY glutSpecialFunc( void (* callback)( int, int, int ) )
81 {
82     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpecialFunc" );
83     SET_CALLBACK( Special );
84 }
85
86 /*
87  * Sets the global idle callback
88  */
89 void FGAPIENTRY glutIdleFunc( void (* callback)( void ) )
90 {
91     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
92     fgState.IdleCallback = callback;
93 }
94
95 /*
96  * Sets the Timer callback for the current window
97  */
98 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
99                                int timerID )
100 {
101     SFG_Timer *timer, *node;
102
103     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFunc" );
104
105     if( (timer = fgState.FreeTimers.Last) )
106     {
107         fgListRemove( &fgState.FreeTimers, &timer->Node );
108     }
109     else
110     {
111         if( ! (timer = malloc(sizeof(SFG_Timer))) )
112             fgError( "Fatal error: "
113                      "Memory allocation failure in glutTimerFunc()" );
114     }
115
116     timer->Callback  = callback;
117     timer->ID        = timerID;
118     timer->TriggerTime = fgElapsedTime() + timeOut;
119
120     for( node = fgState.Timers.First; node; node = node->Node.Next )
121     {
122         if( node->TriggerTime > timer->TriggerTime )
123             break;
124     }
125
126     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
127 }
128
129 /*
130  * Sets the Visibility callback for the current window.
131  */
132 static void fghVisibility( int status )
133 {
134     int glut_status = GLUT_VISIBLE;
135
136     freeglut_return_if_fail( fgStructure.Window );
137
138     if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
139         glut_status = GLUT_NOT_VISIBLE;
140     INVOKE_WCB( *( fgStructure.Window ), Visibility, ( glut_status ) );
141 }
142
143 void FGAPIENTRY glutVisibilityFunc( void (* callback)( int ) )
144 {
145     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
146     SET_CALLBACK( Visibility );
147
148     if( callback )
149         glutWindowStatusFunc( fghVisibility );
150     else
151         glutWindowStatusFunc( NULL );
152 }
153
154 /*
155  * Sets the keyboard key release callback for the current window
156  */
157 void FGAPIENTRY glutKeyboardUpFunc( void (* callback)
158                                     ( unsigned char, int, int ) )
159 {
160     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutKeyboardUpFunc" );
161     SET_CALLBACK( KeyboardUp );
162 }
163
164 /*
165  * Sets the special key release callback for the current window
166  */
167 void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
168 {
169     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpecialUpFunc" );
170     SET_CALLBACK( SpecialUp );
171 }
172
173 /*
174  * Sets the joystick callback and polling rate for the current window
175  */
176 void FGAPIENTRY glutJoystickFunc( void (* callback)
177                                   ( unsigned int, int, int, int ),
178                                   int pollInterval )
179 {
180     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
181     fgInitialiseJoysticks ();
182
183     SET_CALLBACK( Joystick );
184     fgStructure.Window->State.JoystickPollRate = pollInterval;
185
186     fgStructure.Window->State.JoystickLastPoll =
187         fgElapsedTime() - fgStructure.Window->State.JoystickPollRate;
188
189     if( fgStructure.Window->State.JoystickLastPoll < 0 )
190         fgStructure.Window->State.JoystickLastPoll = 0;
191 }
192
193 /*
194  * Sets the mouse callback for the current window
195  */
196 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
197 {
198     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMouseFunc" );
199     SET_CALLBACK( Mouse );
200 }
201
202 /*
203  * Sets the mouse wheel callback for the current window
204  */
205 void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) )
206 {
207     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMouseWheelFunc" );
208     SET_CALLBACK( MouseWheel );
209 }
210
211 /*
212  * Sets the mouse motion callback for the current window (one or more buttons
213  * are pressed)
214  */
215 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
216 {
217     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMotionFunc" );
218     SET_CALLBACK( Motion );
219 }
220
221 /*
222  * Sets the passive mouse motion callback for the current window (no mouse
223  * buttons are pressed)
224  */
225 void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
226 {
227     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPassiveMotionFunc" );
228     SET_CALLBACK( Passive );
229 }
230
231 /*
232  * Window mouse entry/leave callback
233  */
234 void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
235 {
236     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEntryFunc" );
237     SET_CALLBACK( Entry );
238 }
239
240 /*
241  * Window destruction callbacks
242  */
243 void FGAPIENTRY glutCloseFunc( void (* callback)( void ) )
244 {
245     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCloseFunc" );
246     SET_CALLBACK( Destroy );
247 }
248
249 void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) )
250 {
251     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWMCloseFunc" );
252     glutCloseFunc( callback );
253 }
254
255 /* A. Donev: Destruction callback for menus */
256 void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) )
257 {
258     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
259     if( fgStructure.Menu )
260         fgStructure.Menu->Destroy = callback;
261 }
262
263 /*
264  * Deprecated version of glutMenuStatusFunc callback setting method
265  */
266 void FGAPIENTRY glutMenuStateFunc( void (* callback)( int ) )
267 {
268     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
269     fgState.MenuStateCallback = callback;
270 }
271
272 /*
273  * Sets the global menu status callback for the current window
274  */
275 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
276 {
277     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
278     fgState.MenuStatusCallback = callback;
279 }
280
281 /*
282  * Sets the overlay display callback for the current window
283  */
284 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
285 {
286     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutOverlayDisplayFunc" );
287     SET_CALLBACK( OverlayDisplay );
288 }
289
290 /*
291  * Sets the window status callback for the current window
292  */
293 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
294 {
295     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWindowStatusFunc" );
296     SET_CALLBACK( WindowStatus );
297 }
298
299 /*
300  * Sets the spaceball motion callback for the current window
301  */
302 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
303 {
304     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
305     SET_CALLBACK( SpaceMotion );
306 }
307
308 /*
309  * Sets the spaceball rotate callback for the current window
310  */
311 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
312 {
313     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
314     SET_CALLBACK( SpaceRotation );
315 }
316
317 /*
318  * Sets the spaceball button callback for the current window
319  */
320 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
321 {
322     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
323     SET_CALLBACK( SpaceButton );
324 }
325
326 /*
327  * Sets the button box callback for the current window
328  */
329 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
330 {
331     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutButtonBoxFunc" );
332     SET_CALLBACK( ButtonBox );
333 }
334
335 /*
336  * Sets the dials box callback for the current window
337  */
338 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
339 {
340     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDialsFunc" );
341     SET_CALLBACK( Dials );
342 }
343
344 /*
345  * Sets the tablet motion callback for the current window
346  */
347 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
348 {
349     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTabletMotionFunc" );
350     SET_CALLBACK( TabletMotion );
351 }
352
353 /*
354  * Sets the tablet buttons callback for the current window
355  */
356 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
357 {
358     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTabletButtonFunc" );
359     SET_CALLBACK( TabletButton );
360 }
361
362 /*** END OF FILE ***/