Partially (re-?)unified some of the glutMainLoopEvent() code. The only
[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)( 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 ), int timerID )
95 {
96     SFG_Timer* timer;
97
98     freeglut_assert_ready;
99
100     timer = (SFG_Timer *)calloc( sizeof(SFG_Timer), 1 );
101     if (!timer)
102         fgError ("Fatal error: "
103             "Memory allocation failure in glutTimerFunc()\n");
104
105     timer->Callback  = callback;
106     timer->ID        = timerID;
107     timer->TriggerTime = fgElapsedTime() + timeOut;
108     fgListAppend( &fgState.Timers, &timer->Node );
109 }
110
111 /*
112  * Sets the Visibility callback for the current window.
113  */
114 static void fghVisibility( int status )
115 {
116     freeglut_assert_ready;
117     freeglut_return_if_fail( fgStructure.Window != NULL );
118     freeglut_return_if_fail( fgStructure.Window->Callbacks.Visibility != NULL );
119
120     if( status == GLUT_HIDDEN  || status == GLUT_FULLY_COVERED )
121         fgStructure.Window->Callbacks.Visibility( GLUT_NOT_VISIBLE );
122     else
123         fgStructure.Window->Callbacks.Visibility( GLUT_VISIBLE );
124 }
125
126 void FGAPIENTRY glutVisibilityFunc( void (* callback)( int ) )
127 {
128     SET_CALLBACK( Visibility );
129
130     if( callback )
131         glutWindowStatusFunc( fghVisibility );
132     else
133         glutWindowStatusFunc( NULL );
134 }
135
136 /*
137  * Sets the keyboard key release callback for the current window
138  */
139 void FGAPIENTRY glutKeyboardUpFunc( void (* callback)( unsigned char, int, int ) )
140 {
141     SET_CALLBACK( KeyboardUp );
142 }
143
144 /*
145  * Sets the special key release callback for the current window
146  */
147 void FGAPIENTRY glutSpecialUpFunc( void (* callback)( int, int, int ) )
148 {
149     SET_CALLBACK( SpecialUp );
150 }
151
152 /*
153  * Sets the joystick callback and polling rate for the current window
154  */
155 void FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval )
156 {
157     SET_CALLBACK( Joystick );
158     fgStructure.Window->State.JoystickPollRate = pollInterval;
159
160     fgStructure.Window->State.JoystickLastPoll =
161         fgElapsedTime() - fgStructure.Window->State.JoystickPollRate;
162
163     if( fgStructure.Window->State.JoystickLastPoll < 0 )
164         fgStructure.Window->State.JoystickLastPoll = 0;
165 }
166
167 /*
168  * Sets the mouse callback for the current window
169  */
170 void FGAPIENTRY glutMouseFunc( void (* callback)( int, int, int, int ) )
171 {
172     SET_CALLBACK( Mouse );
173 }
174
175 /*
176  * Sets the mouse motion callback for the current window (one or more buttons
177  * are pressed)
178  */
179 void FGAPIENTRY glutMotionFunc( void (* callback)( int, int ) )
180 {
181     SET_CALLBACK( Motion );
182 }
183
184 /*
185  * Sets the passive mouse motion callback for the current window (no mouse
186  * buttons are pressed)
187  */
188 void FGAPIENTRY glutPassiveMotionFunc( void (* callback)( int, int ) )
189 {
190     SET_CALLBACK( Passive );
191 }
192
193 /*
194  * Window mouse entry/leave callback
195  */
196 void FGAPIENTRY glutEntryFunc( void (* callback)( int ) )
197 {
198     SET_CALLBACK( Entry );
199 }
200
201 /*
202  * Window destruction callbacks
203  */
204 void FGAPIENTRY glutCloseFunc( void (* callback)( void ) )
205 {
206     SET_CALLBACK( Destroy );
207 }
208
209 void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) )
210 {
211     glutCloseFunc( callback );
212 }
213
214 /* A. Donev: Destruction callback for menus */
215 void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) )
216 {
217    if( fgStructure.Menu == NULL )
218        return;
219    fgStructure.Menu->Destroy = callback;
220 }
221
222 /*
223  * Deprecated version of glutMenuStatusFunc callback setting method
224  */
225 void FGAPIENTRY glutMenuStateFunc( void (* callback)( int ) )
226 {
227     freeglut_assert_ready;
228     fgState.MenuStateCallback = callback;
229 }
230
231 /*
232  * Sets the global menu status callback for the current window
233  */
234 void FGAPIENTRY glutMenuStatusFunc( void (* callback)( int, int, int ) )
235 {
236     freeglut_assert_ready;
237     fgState.MenuStatusCallback = callback;
238 }
239
240 /*
241  * Sets the overlay display callback for the current window
242  */
243 void FGAPIENTRY glutOverlayDisplayFunc( void (* callback)( void ) )
244 {
245     SET_CALLBACK( OverlayDisplay );
246 }
247
248 /*
249  * Sets the window status callback for the current window
250  */
251 void FGAPIENTRY glutWindowStatusFunc( void (* callback)( int ) )
252 {
253     SET_CALLBACK( WindowStatus );
254 }
255
256 /*
257  * Sets the spaceball motion callback for the current window
258  */
259 void FGAPIENTRY glutSpaceballMotionFunc( void (* callback)( int, int, int ) )
260 {
261     SET_CALLBACK( SpaceMotion );
262 }
263
264 /*
265  * Sets the spaceball rotate callback for the current window
266  */
267 void FGAPIENTRY glutSpaceballRotateFunc( void (* callback)( int, int, int ) )
268 {
269     SET_CALLBACK( SpaceRotation );
270 }
271
272 /*
273  * Sets the spaceball button callback for the current window
274  */
275 void FGAPIENTRY glutSpaceballButtonFunc( void (* callback)( int, int ) )
276 {
277     SET_CALLBACK( SpaceButton );
278 }
279
280 /*
281  * Sets the button box callback for the current window
282  */
283 void FGAPIENTRY glutButtonBoxFunc( void (* callback)( int, int ) )
284 {
285     SET_CALLBACK( ButtonBox );
286 }
287
288 /*
289  * Sets the dials box callback for the current window
290  */
291 void FGAPIENTRY glutDialsFunc( void (* callback)( int, int ) )
292 {
293     SET_CALLBACK( Dials );
294 }
295
296 /*
297  * Sets the tablet motion callback for the current window
298  */
299 void FGAPIENTRY glutTabletMotionFunc( void (* callback)( int, int ) )
300 {
301     SET_CALLBACK( TabletMotion );
302 }
303
304 /*
305  * Sets the tablet buttons callback for the current window
306  */
307 void FGAPIENTRY glutTabletButtonFunc( void (* callback)( int, int, int, int ) )
308 {
309     SET_CALLBACK( TabletButton );
310 }
311
312 /*** END OF FILE ***/