Converted the "Callbacks" structure (with named members holding individual
[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 "../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     FETCH_WCB( ( *( fgStructure.Window ) ), a ) = callback;
45     /* fgStructure.Window->Callbacks.a = callback; */
46
47 /*
48  * Sets the Display callback for the current window
49  */
50 void FGAPIENTRY glutDisplayFunc( void (* callback)( void ) )
51 {
52     if( !callback )
53         fgError( "Fatal error in program.  NULL display callback not "
54                  "permitted in GLUT 3.0+ or freeglut 2.0.1+\n" );
55     SET_CALLBACK( Display );
56     fgStructure.Window->State.Redisplay = TRUE;
57 }
58
59 /*
60  * Sets the Reshape callback for the current window
61  */
62 void FGAPIENTRY glutReshapeFunc( void (* callback)( int, int ) )
63 {
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     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     fgState.IdleCallback = callback;
91 }
92
93 /*
94  * Sets the Timer callback for the current window
95  */
96 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
97                                int timerID )
98 {
99     SFG_Timer* timer;
100
101     freeglut_assert_ready;
102
103     timer = (SFG_Timer *)calloc( sizeof(SFG_Timer), 1 );
104     if( !timer )
105         fgError( "Fatal error: "
106                  "Memory allocation failure in glutTimerFunc()\n" );
107
108     timer->Callback  = callback;
109     timer->ID        = timerID;
110     timer->TriggerTime = fgElapsedTime() + timeOut;
111     fgListAppend( &fgState.Timers, &timer->Node );
112 }
113
114 /*
115  * Sets the Visibility callback for the current window.
116  */
117 static void fghVisibility( int status )
118 {
119     FGCBVisibility vis;
120     int glut_status;
121     
122     freeglut_assert_ready;
123     freeglut_return_if_fail( fgStructure.Window );
124     vis = FETCH_WCB( ( *( fgStructure.Window ) ), Visibility );
125     freeglut_return_if_fail( vis );
126     /* Callbacks.Visibility ); */
127
128     if( status == GLUT_HIDDEN  || status == GLUT_FULLY_COVERED )
129         glut_status = GLUT_NOT_VISIBLE;
130     else
131         glut_status = GLUT_VISIBLE;
132     vis( glut_status );
133     /*
134         fgStructure.Window->Callbacks.Visibility( GLUT_NOT_VISIBLE );
135     else
136         fgStructure.Window->Callbacks.Visibility( GLUT_VISIBLE );
137     */
138 }
139
140 void FGAPIENTRY glutVisibilityFunc( void (* callback)( int ) )
141 {
142     SET_CALLBACK( Visibility );
143
144     if( callback )
145         glutWindowStatusFunc( fghVisibility );
146     else
147         glutWindowStatusFunc( NULL );
148 }
149
150 /*
151  * Sets the keyboard key release callback for the current window
152  */
153 void FGAPIENTRY glutKeyboardUpFunc( void (* callback)
154                                     ( unsigned char, int, int ) )
155 {
156     SET_CALLBACK( KeyboardUp );
157 }
158
159 /*
160  * Sets the special key release callback for the current window
161  */
162 void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
163 {
164     SET_CALLBACK( SpecialUp );
165 }
166
167 /*
168  * Sets the joystick callback and polling rate for the current window
169  */
170 void FGAPIENTRY glutJoystickFunc( void (* callback)
171                                   ( unsigned int, int, int, int ),
172                                   int pollInterval )
173 {
174     SET_CALLBACK( Joystick );
175     fgStructure.Window->State.JoystickPollRate = pollInterval;
176
177     fgStructure.Window->State.JoystickLastPoll =
178         fgElapsedTime() - fgStructure.Window->State.JoystickPollRate;
179
180     if( fgStructure.Window->State.JoystickLastPoll < 0 )
181         fgStructure.Window->State.JoystickLastPoll = 0;
182 }
183
184 /*
185  * Sets the mouse callback for the current window
186  */
187 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
188 {
189     SET_CALLBACK( Mouse );
190 }
191
192 /*
193  * Sets the mouse wheel callback for the current window
194  */
195 void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) )
196 {
197     SET_CALLBACK( MouseWheel );
198 }
199
200 /*
201  * Sets the mouse motion callback for the current window (one or more buttons
202  * are pressed)
203  */
204 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
205 {
206     SET_CALLBACK( Motion );
207 }
208
209 /*
210  * Sets the passive mouse motion callback for the current window (no mouse
211  * buttons are pressed)
212  */
213 void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
214 {
215     SET_CALLBACK( Passive );
216 }
217
218 /*
219  * Window mouse entry/leave callback
220  */
221 void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
222 {
223     SET_CALLBACK( Entry );
224 }
225
226 /*
227  * Window destruction callbacks
228  */
229 void FGAPIENTRY glutCloseFunc( void (* callback)( void ) )
230 {
231     SET_CALLBACK( Destroy );
232 }
233
234 void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) )
235 {
236     glutCloseFunc( callback );
237 }
238
239 /* A. Donev: Destruction callback for menus */
240 void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) )
241 {
242     if( fgStructure.Menu == NULL )
243         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     fgState.MenuStateCallback = callback;
254 }
255
256 /*
257  * Sets the global menu status callback for the current window
258  */
259 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
260 {
261     freeglut_assert_ready;
262     fgState.MenuStatusCallback = callback;
263 }
264
265 /*
266  * Sets the overlay display callback for the current window
267  */
268 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
269 {
270     SET_CALLBACK( OverlayDisplay );
271 }
272
273 /*
274  * Sets the window status callback for the current window
275  */
276 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
277 {
278     SET_CALLBACK( WindowStatus );
279 }
280
281 /*
282  * Sets the spaceball motion callback for the current window
283  */
284 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
285 {
286     SET_CALLBACK( SpaceMotion );
287 }
288
289 /*
290  * Sets the spaceball rotate callback for the current window
291  */
292 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
293 {
294     SET_CALLBACK( SpaceRotation );
295 }
296
297 /*
298  * Sets the spaceball button callback for the current window
299  */
300 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
301 {
302     SET_CALLBACK( SpaceButton );
303 }
304
305 /*
306  * Sets the button box callback for the current window
307  */
308 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
309 {
310     SET_CALLBACK( ButtonBox );
311 }
312
313 /*
314  * Sets the dials box callback for the current window
315  */
316 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
317 {
318     SET_CALLBACK( Dials );
319 }
320
321 /*
322  * Sets the tablet motion callback for the current window
323  */
324 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
325 {
326     SET_CALLBACK( TabletMotion );
327 }
328
329 /*
330  * Sets the tablet buttons callback for the current window
331  */
332 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
333 {
334     SET_CALLBACK( TabletButton );
335 }
336
337 /*** END OF FILE ***/