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