94c4dbd88094f103779eb41e37223646802e01eb
[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     if( !callback )
52         fgError( "Fatal error in program.  NULL display callback not "
53                  "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
54     SET_CALLBACK( Display );
55 }
56
57 /*
58  * Sets the Reshape callback for the current window
59  */
60 void FGAPIENTRY glutReshapeFunc( void (* callback)( int, int ) )
61 {
62     SET_CALLBACK( Reshape );
63 }
64
65 /*
66  * Sets the Keyboard callback for the current window
67  */
68 void FGAPIENTRY glutKeyboardFunc( void (* callback)
69                                   ( unsigned char, int, int ) )
70 {
71     SET_CALLBACK( Keyboard );
72 }
73
74 /*
75  * Sets the Special callback for the current window
76  */
77 void FGAPIENTRY glutSpecialFunc( void (* callback)( int, int, int ) )
78 {
79     SET_CALLBACK( Special );
80 }
81
82 /*
83  * Sets the global idle callback
84  */
85 void FGAPIENTRY glutIdleFunc( void (* callback)( void ) )
86 {
87     freeglut_assert_ready;
88     fgState.IdleCallback = callback;
89 }
90
91 /*
92  * Sets the Timer callback for the current window
93  */
94 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
95                                int timerID )
96 {
97     SFG_Timer *timer, *node;
98
99     freeglut_assert_ready;
100
101     if( (timer = fgState.FreeTimers.Last) )
102     {
103         fgListRemove( &fgState.FreeTimers, &timer->Node );
104     }
105     else
106     {
107         if( ! (timer = malloc(sizeof(SFG_Timer))) )
108             fgError( "Fatal error: "
109                      "Memory allocation failure in glutTimerFunc()" );
110     }
111
112     timer->Callback  = callback;
113     timer->ID        = timerID;
114     timer->TriggerTime = fgElapsedTime() + timeOut;
115
116     for( node = fgState.Timers.First; node; node = node->Node.Next )
117     {
118         if( node->TriggerTime > timer->TriggerTime )
119             break;
120     }
121
122     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
123 }
124
125 /*
126  * Sets the Visibility callback for the current window.
127  */
128 static void fghVisibility( int status )
129 {
130     int glut_status = GLUT_VISIBLE;
131
132     freeglut_assert_ready;
133     freeglut_return_if_fail( fgStructure.Window );
134
135     if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
136         glut_status = GLUT_NOT_VISIBLE;
137     INVOKE_WCB( *( fgStructure.Window ), Visibility, ( glut_status ) );
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     if( !fgState.JoysticksInitialised )
175     {
176         fgJoystickInit( 0 );
177         fgJoystickInit( 1 );
178         fgState.JoysticksInitialised = GL_TRUE;
179     }
180
181     SET_CALLBACK( Joystick );
182     fgStructure.Window->State.JoystickPollRate = pollInterval;
183
184     fgStructure.Window->State.JoystickLastPoll =
185         fgElapsedTime() - fgStructure.Window->State.JoystickPollRate;
186
187     if( fgStructure.Window->State.JoystickLastPoll < 0 )
188         fgStructure.Window->State.JoystickLastPoll = 0;
189 }
190
191 /*
192  * Sets the mouse callback for the current window
193  */
194 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
195 {
196     SET_CALLBACK( Mouse );
197 }
198
199 /*
200  * Sets the mouse wheel callback for the current window
201  */
202 void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) )
203 {
204     SET_CALLBACK( MouseWheel );
205 }
206
207 /*
208  * Sets the mouse motion callback for the current window (one or more buttons
209  * are pressed)
210  */
211 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
212 {
213     SET_CALLBACK( Motion );
214 }
215
216 /*
217  * Sets the passive mouse motion callback for the current window (no mouse
218  * 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 )
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     fgState.MenuStateCallback = callback;
260 }
261
262 /*
263  * Sets the global menu status callback for the current window
264  */
265 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
266 {
267     freeglut_assert_ready;
268     fgState.MenuStatusCallback = callback;
269 }
270
271 /*
272  * Sets the overlay display callback for the current window
273  */
274 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
275 {
276     SET_CALLBACK( OverlayDisplay );
277 }
278
279 /*
280  * Sets the window status callback for the current window
281  */
282 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
283 {
284     SET_CALLBACK( WindowStatus );
285 }
286
287 /*
288  * Sets the spaceball motion callback for the current window
289  */
290 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
291 {
292     SET_CALLBACK( SpaceMotion );
293 }
294
295 /*
296  * Sets the spaceball rotate callback for the current window
297  */
298 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
299 {
300     SET_CALLBACK( SpaceRotation );
301 }
302
303 /*
304  * Sets the spaceball button callback for the current window
305  */
306 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
307 {
308     SET_CALLBACK( SpaceButton );
309 }
310
311 /*
312  * Sets the button box callback for the current window
313  */
314 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
315 {
316     SET_CALLBACK( ButtonBox );
317 }
318
319 /*
320  * Sets the dials box callback for the current window
321  */
322 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
323 {
324     SET_CALLBACK( Dials );
325 }
326
327 /*
328  * Sets the tablet motion callback for the current window
329  */
330 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
331 {
332     SET_CALLBACK( TabletMotion );
333 }
334
335 /*
336  * Sets the tablet buttons callback for the current window
337  */
338 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
339 {
340     SET_CALLBACK( TabletButton );
341 }
342
343 /*** END OF FILE ***/