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 
glutDropFilesFunc(void (* callback)(int,int,const char *,int))46 void  glutDropFilesFunc(void (*callback)(int, int, const char*, int))
47 {
48 	SET_CALLBACK( DropFiles );
49 
50 }
51 
52 /*
53  * Sets the Display callback for the current window
54  */
glutDisplayFunc(void (* callback)(void))55 void  glutDisplayFunc( void (* callback)( void ) )
56 {
57     if( !callback )
58         fgError( "Fatal error in program.  NULL display callback not "
59                  "permitted in GLUT 3.0+ or freeglut 2.0.1+\n" );
60     SET_CALLBACK( Display );
61     fgStructure.Window->State.Redisplay = GL_TRUE;
62 }
63 
64 /*
65  * Sets the Reshape callback for the current window
66  */
glutReshapeFunc(void (* callback)(int,int))67 void  glutReshapeFunc( void (* callback)( int, int ) )
68 {
69     SET_CALLBACK( Reshape );
70 }
71 
72 /*
73  * Sets the Keyboard callback for the current window
74  */
glutKeyboardFunc(void (* callback)(unsigned char,int,int))75 void  glutKeyboardFunc( void (* callback)
76                                   ( unsigned char, int, int ) )
77 {
78     SET_CALLBACK( Keyboard );
79 }
80 
81 /*
82  * Sets the Special callback for the current window
83  */
glutSpecialFunc(void (* callback)(int,int,int))84 void  glutSpecialFunc( void (* callback)( int, int, int ) )
85 {
86     SET_CALLBACK( Special );
87 }
88 
89 /*
90  * Sets the global idle callback
91  */
glutIdleFunc(void (* callback)(void))92 void  glutIdleFunc( void (* callback)( void ) )
93 {
94     freeglut_assert_ready;
95     fgState.IdleCallback = callback;
96 }
97 
98 /*
99  * Sets the Timer callback for the current window
100  */
glutTimerFunc(unsigned int timeOut,void (* callback)(int),int timerID)101 void  glutTimerFunc( unsigned int timeOut, void (* callback)( int ),
102                                int timerID )
103 {
104     SFG_Timer *timer, *node;
105 
106     freeglut_assert_ready;
107 
108     if( (timer = fgState.FreeTimers.Last) )
109     {
110         fgListRemove( &fgState.FreeTimers, &timer->Node );
111     }
112     else
113     {
114         if( ! (timer = malloc(sizeof(SFG_Timer))) )
115             fgError( "Fatal error: "
116                      "Memory allocation failure in glutTimerFunc()\n" );
117     }
118 
119     timer->Callback  = callback;
120     timer->ID        = timerID;
121     timer->TriggerTime = fgElapsedTime() + timeOut;
122 
123     for( node = fgState.Timers.First; node; node = node->Node.Next )
124     {
125         if( node->TriggerTime > timer->TriggerTime )
126             break;
127     }
128 
129     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
130 }
131 
132 /*
133  * Sets the Visibility callback for the current window.
134  */
fghVisibility(int status)135 static void fghVisibility( int status )
136 {
137     int glut_status = GLUT_VISIBLE;
138 
139     freeglut_assert_ready;
140     freeglut_return_if_fail( fgStructure.Window );
141 
142     if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
143         glut_status = GLUT_NOT_VISIBLE;
144     INVOKE_WCB( *( fgStructure.Window ), Visibility, ( glut_status ) );
145 }
146 
glutVisibilityFunc(void (* callback)(int))147 void  glutVisibilityFunc( void (* callback)( int ) )
148 {
149     SET_CALLBACK( Visibility );
150 
151     if( callback )
152         glutWindowStatusFunc( fghVisibility );
153     else
154         glutWindowStatusFunc( NULL );
155 }
156 
157 /*
158  * Sets the keyboard key release callback for the current window
159  */
glutKeyboardUpFunc(void (* callback)(unsigned char,int,int))160 void  glutKeyboardUpFunc( void (* callback)
161                                     ( unsigned char, int, int ) )
162 {
163     SET_CALLBACK( KeyboardUp );
164 }
165 
166 /*
167  * Sets the special key release callback for the current window
168  */
glutSpecialUpFunc(void (* callback)(int,int,int))169 void  glutSpecialUpFunc( void (* callback)( int, int, int ) )
170 {
171     SET_CALLBACK( SpecialUp );
172 }
173 
174 /*
175  * Sets the joystick callback and polling rate for the current window
176  */
glutJoystickFunc(void (* callback)(unsigned int,int,int,int),int pollInterval)177 void  glutJoystickFunc( void (* callback)
178                                   ( unsigned int, int, int, int ),
179                                   int pollInterval )
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  */
glutMouseFunc(void (* callback)(int,int,int,int))194 void  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  */
glutMouseWheelFunc(void (* callback)(int,int,int,int))202 void  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  */
glutMotionFunc(void (* callback)(int,int))211 void  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  */
glutPassiveMotionFunc(void (* callback)(int,int))220 void  glutPassiveMotionFunc( void (* callback)( int, int ) )
221 {
222     SET_CALLBACK( Passive );
223 }
224 
225 /*
226  * Window mouse entry/leave callback
227  */
glutEntryFunc(void (* callback)(int))228 void  glutEntryFunc( void (* callback)( int ) )
229 {
230     SET_CALLBACK( Entry );
231 }
232 
233 /*
234  * Window destruction callbacks
235  */
glutCloseFunc(void (* callback)(void))236 void  glutCloseFunc( void (* callback)( void ) )
237 {
238     SET_CALLBACK( Destroy );
239 }
240 
glutWMCloseFunc(void (* callback)(void))241 void  glutWMCloseFunc( void (* callback)( void ) )
242 {
243     glutCloseFunc( callback );
244 }
245 
246 /* A. Donev: Destruction callback for menus */
glutMenuDestroyFunc(void (* callback)(void))247 void  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  */
glutMenuStateFunc(void (* callback)(int))256 void  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  */
glutMenuStatusFunc(void (* callback)(int,int,int))265 void  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  */
glutOverlayDisplayFunc(void (* callback)(void))274 void  glutOverlayDisplayFunc( void (* callback)( void ) )
275 {
276     SET_CALLBACK( OverlayDisplay );
277 }
278 
279 /*
280  * Sets the window status callback for the current window
281  */
glutWindowStatusFunc(void (* callback)(int))282 void  glutWindowStatusFunc( void (* callback)( int ) )
283 {
284     SET_CALLBACK( WindowStatus );
285 }
286 
287 /*
288  * Sets the spaceball motion callback for the current window
289  */
glutSpaceballMotionFunc(void (* callback)(int,int,int))290 void  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  */
glutSpaceballRotateFunc(void (* callback)(int,int,int))298 void  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  */
glutSpaceballButtonFunc(void (* callback)(int,int))306 void  glutSpaceballButtonFunc( void (* callback)( int, int ) )
307 {
308     SET_CALLBACK( SpaceButton );
309 }
310 
311 /*
312  * Sets the button box callback for the current window
313  */
glutButtonBoxFunc(void (* callback)(int,int))314 void  glutButtonBoxFunc( void (* callback)( int, int ) )
315 {
316     SET_CALLBACK( ButtonBox );
317 }
318 
319 /*
320  * Sets the dials box callback for the current window
321  */
glutDialsFunc(void (* callback)(int,int))322 void  glutDialsFunc( void (* callback)( int, int ) )
323 {
324     SET_CALLBACK( Dials );
325 }
326 
327 /*
328  * Sets the tablet motion callback for the current window
329  */
glutTabletMotionFunc(void (* callback)(int,int))330 void  glutTabletMotionFunc( void (* callback)( int, int ) )
331 {
332     SET_CALLBACK( TabletMotion );
333 }
334 
335 /*
336  * Sets the tablet buttons callback for the current window
337  */
glutTabletButtonFunc(void (* callback)(int,int,int,int))338 void  glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
339 {
340     SET_CALLBACK( TabletButton );
341 }
342 
343 /*** END OF FILE ***/
344