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