Added a safety-check on a memory allocation. (Timers were using
[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 #define  G_LOG_DOMAIN  "freeglut-callbacks"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37
38 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
39
40 /*
41  * All of the callbacks setting methods can be generalized to this:
42  */
43 #define SET_CALLBACK(a) if( fgStructure.Window == NULL ) return;\
44                             fgStructure.Window->Callbacks.a = callback;
45
46 /*
47  * Sets the Display callback for the current window
48  */
49 void FGAPIENTRY glutDisplayFunc( void (* callback)( void ) )
50 {
51     if( !callback )
52         fgError ("Fatal error in program.  NULL display callback not "
53             "permitted in GLUT 3.0+ or freeglut 2.0.1+\n");
54     SET_CALLBACK( Display );
55
56     /*
57      * Force a redisplay with the new callback
58      */
59     fgStructure.Window->State.Redisplay = TRUE;
60
61 }
62
63 /*
64  * Sets the Reshape callback for the current window
65  */
66 void FGAPIENTRY glutReshapeFunc( void (* callback)( int, int ) )
67 {
68     SET_CALLBACK( Reshape );
69 }
70
71 /*
72  * Sets the Keyboard callback for the current window
73  */
74 void FGAPIENTRY glutKeyboardFunc( void (* callback)( unsigned char, int, int ) )
75 {
76     SET_CALLBACK( Keyboard );
77 }
78
79 /*
80  * Sets the Special callback for the current window
81  */
82 void FGAPIENTRY glutSpecialFunc( void (* callback)( int, int, int ) )
83 {
84     SET_CALLBACK( Special );
85 }
86
87 /*
88  * Sets the global idle callback
89  */
90 void FGAPIENTRY glutIdleFunc( void (* callback)( void ) )
91 {
92     freeglut_assert_ready;
93
94     /*
95      * The global idle callback pointer is stored in fgState structure
96      */
97     fgState.IdleCallback = callback;
98 }
99
100 /*
101  * Sets the Timer callback for the current window
102  */
103 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ), int timerID )
104 {
105     SFG_Timer* timer;
106
107     freeglut_assert_ready;
108
109     /*
110      * Create a new freeglut timer hook structure
111      */
112     timer = (SFG_Timer *)calloc( sizeof(SFG_Timer), 1 );
113     if (!timer)
114         fgError ("Fatal error: "
115             "Memory allocation failure in glutTimerFunc()\n");
116
117     /*
118      * Remember the callback address and timer hook's ID
119      */
120     timer->Callback  = callback;
121     timer->ID        = timerID;
122
123     /*
124      * When will the time out happen (in terms of window's timer)
125      */
126     timer->TriggerTime = fgElapsedTime() + timeOut;
127
128     /*
129      * Have the new hook attached to the current window
130      */
131     fgListAppend( &fgState.Timers, &timer->Node );
132 }
133
134 /*
135  * Sets the Visibility callback for the current window.
136  *
137  * I had to peer to GLUT sources to clean up the mess.
138  * Can anyone please explain me what is going on here?!?
139  */
140 static void fghVisibility( int status )
141 {
142     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Window != NULL );
143     freeglut_return_if_fail( fgStructure.Window->Callbacks.Visibility != NULL );
144
145     if( status == GLUT_HIDDEN  || status == GLUT_FULLY_COVERED )
146         fgStructure.Window->Callbacks.Visibility( GLUT_NOT_VISIBLE );
147     else
148         fgStructure.Window->Callbacks.Visibility( GLUT_VISIBLE );
149 }
150
151 void FGAPIENTRY glutVisibilityFunc( void (* callback)( int ) )
152 {
153     SET_CALLBACK( Visibility );
154
155     if( callback )
156         glutWindowStatusFunc( fghVisibility );
157     else
158         glutWindowStatusFunc( NULL );
159 }
160
161 /*
162  * Sets the keyboard key release callback for the current window
163  */
164 void FGAPIENTRY glutKeyboardUpFunc( void (* callback)( unsigned char, int, int ) )
165 {
166     SET_CALLBACK( KeyboardUp );
167 }
168
169 /*
170  * Sets the special key release callback for the current window
171  */
172 void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
173 {
174     SET_CALLBACK( SpecialUp );
175 }
176
177 /*
178  * Sets the joystick callback and polling rate for the current window
179  */
180 void FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval )
181 {
182     SET_CALLBACK( Joystick );
183
184     freeglut_return_if_fail( fgStructure.Window != NULL );
185
186     /*
187      * Do not forget setting the joystick poll rate
188      */
189     fgStructure.Window->State.JoystickPollRate = pollInterval;
190
191     /*
192      * Make sure the joystick polling routine gets called as early as possible:
193      */
194     fgStructure.Window->State.JoystickLastPoll =
195         fgElapsedTime() - fgStructure.Window->State.JoystickPollRate;
196
197     if( fgStructure.Window->State.JoystickLastPoll < 0 )
198         fgStructure.Window->State.JoystickLastPoll = 0;
199 }
200
201 /*
202  * Sets the mouse callback for the current window
203  */
204 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
205 {
206     SET_CALLBACK( Mouse );
207 }
208
209 /*
210  * Sets the mouse motion callback for the current window (one or more buttons are pressed)
211  */
212 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
213 {
214     SET_CALLBACK( Motion );
215 }
216
217 /*
218  * Sets the passive mouse motion callback for the current window (no mouse buttons are pressed)
219  */
220 void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
221 {
222     SET_CALLBACK( Passive );
223 }
224
225 /*
226  * Window mouse entry/leave callback
227  */
228 void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
229 {
230     SET_CALLBACK( Entry );
231 }
232
233 /*
234  * Window destruction callbacks
235  */
236 void FGAPIENTRY glutCloseFunc( void (* callback)( void ) )
237 {
238     SET_CALLBACK( Destroy );
239 }
240
241 void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) )
242 {
243     glutCloseFunc( callback );
244 }
245
246 /* A. Donev: Destruction callback for menus */
247 void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) )
248 {
249    if( fgStructure.Menu == NULL ) return;
250    fgStructure.Menu->Destroy = callback;
251 }
252
253 /*
254  * Deprecated version of glutMenuStatusFunc callback setting method
255  */
256 void FGAPIENTRY glutMenuStateFunc( void (* callback)( int ) )
257 {
258     freeglut_assert_ready;
259
260     fgState.MenuStateCallback = callback;
261 }
262
263 /*
264  * Sets the global menu status callback for the current window
265  */
266 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
267 {
268     freeglut_assert_ready;
269
270     fgState.MenuStatusCallback = callback;
271 }
272
273 /*
274  * Sets the overlay display callback for the current window
275  */
276 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
277 {
278     SET_CALLBACK( OverlayDisplay );
279 }
280
281 /*
282  * Sets the window status callback for the current window
283  */
284 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
285 {
286     SET_CALLBACK( WindowStatus );
287 }
288
289 /*
290  * Sets the spaceball motion callback for the current window
291  */
292 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
293 {
294     SET_CALLBACK( SpaceMotion );
295 }
296
297 /*
298  * Sets the spaceball rotate callback for the current window
299  */
300 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
301 {
302     SET_CALLBACK( SpaceRotation );
303 }
304
305 /*
306  * Sets the spaceball button callback for the current window
307  */
308 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
309 {
310     SET_CALLBACK( SpaceButton );
311 }
312
313 /*
314  * Sets the button box callback for the current window
315  */
316 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
317 {
318     SET_CALLBACK( ButtonBox );
319 }
320
321 /*
322  * Sets the dials box callback for the current window
323  */
324 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
325 {
326     SET_CALLBACK( Dials );
327 }
328
329 /*
330  * Sets the tablet motion callback for the current window
331  */
332 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
333 {
334     SET_CALLBACK( TabletMotion );
335 }
336
337 /*
338  * Sets the tablet buttons callback for the current window
339  */
340 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
341 {
342     SET_CALLBACK( TabletButton );
343 }
344
345 /*** END OF FILE ***/