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