f61bfb83aab7af649bf1a5702fddeb05d8abd73e
[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 "../include/GL/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 = g_new0( 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 =
115         g_timer_elapsed( fgState.Timer, NULL ) + (((double) timeOut) / 1000.0);
116
117     /*
118      * Have the new hook attached to the current window
119      */
120     fgState.Timers = g_list_append( fgState.Timers, timer );
121 }
122
123 /*
124  * Sets the Visibility callback for the current window.
125  *
126  * I had to peer to GLUT sources to clean up the mess.
127  * Can anyone please explain me what is going on here?!?
128  */
129 static void fghVisibility( gint status )
130 {
131     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Window != NULL );
132     freeglut_return_if_fail( fgStructure.Window->Callbacks.Visibility != NULL );
133
134     if( status == GLUT_HIDDEN  || status == GLUT_FULLY_COVERED )
135         fgStructure.Window->Callbacks.Visibility( GLUT_NOT_VISIBLE );
136     else
137         fgStructure.Window->Callbacks.Visibility( GLUT_VISIBLE );
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)( unsigned char, int, int ) )
154 {
155     SET_CALLBACK( KeyboardUp );
156 }
157
158 /*
159  * Sets the special key release callback for the current window
160  */
161 void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
162 {
163     SET_CALLBACK( SpecialUp );
164 }
165
166 /*
167  * Sets the joystick callback and polling rate for the current window
168  */
169 void FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval )
170 {
171     SET_CALLBACK( Joystick );
172
173     freeglut_return_if_fail( fgStructure.Window != NULL );
174
175     /*
176      * Do not forget setting the joystick poll rate
177      */
178     fgStructure.Window->State.JoystickPollRate = ((double) pollInterval) / 1000.0;
179
180     /*
181      * Make sure the joystick polling routine gets called as early as possible:
182      */
183     fgStructure.Window->State.JoystickLastPoll =
184         g_timer_elapsed( fgState.Timer, NULL ) - fgStructure.Window->State.JoystickPollRate;
185
186     if( fgStructure.Window->State.JoystickLastPoll < 0.0 )
187         fgStructure.Window->State.JoystickLastPoll = 0.0;
188 }
189
190 /*
191  * Sets the mouse callback for the current window
192  */
193 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
194 {
195     SET_CALLBACK( Mouse );
196 }
197
198 /*
199  * Sets the mouse motion callback for the current window (one or more buttons are pressed)
200  */
201 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
202 {
203     SET_CALLBACK( Motion );
204 }
205
206 /*
207  * Sets the passive mouse motion callback for the current window (no mouse buttons are pressed)
208  */
209 void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
210 {
211     SET_CALLBACK( Passive );
212 }
213
214 /*
215  * Window mouse entry/leave callback
216  */
217 void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
218 {
219     SET_CALLBACK( Entry );
220 }
221
222 /*
223  * Deprecated version of glutMenuStatusFunc callback setting method
224  */
225 void FGAPIENTRY glutMenuStateFunc( void (* callback)( int ) )
226 {
227     SET_CALLBACK( MenuState );
228 }
229
230 /*
231  * Sets the global menu status callback for the current window
232  */
233 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
234 {
235     SET_CALLBACK( MenuStatus );
236 }
237
238 /*
239  * Sets the overlay display callback for the current window
240  */
241 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
242 {
243     SET_CALLBACK( OverlayDisplay );
244 }
245
246 /*
247  * Sets the window status callback for the current window
248  */
249 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
250 {
251     SET_CALLBACK( WindowStatus );
252 }
253
254 /*
255  * Sets the spaceball motion callback for the current window
256  */
257 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
258 {
259     SET_CALLBACK( SpaceMotion );
260 }
261
262 /*
263  * Sets the spaceball rotate callback for the current window
264  */
265 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
266 {
267     SET_CALLBACK( SpaceRotation );
268 }
269
270 /*
271  * Sets the spaceball button callback for the current window
272  */
273 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
274 {
275     SET_CALLBACK( SpaceButton );
276 }
277
278 /*
279  * Sets the button box callback for the current window
280  */
281 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
282 {
283     SET_CALLBACK( ButtonBox );
284 }
285
286 /*
287  * Sets the dials box callback for the current window
288  */
289 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
290 {
291     SET_CALLBACK( Dials );
292 }
293
294 /*
295  * Sets the tablet motion callback for the current window
296  */
297 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
298 {
299     SET_CALLBACK( TabletMotion );
300 }
301
302 /*
303  * Sets the tablet buttons callback for the current window
304  */
305 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
306 {
307     SET_CALLBACK( TabletButton );
308 }
309
310 /*** END OF FILE ***/