b09f421021d9a5bd3214bf574976d00fca852c4f
[freeglut] / src / fg_callbacks.c
1 /*
2  * fg_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 #include <GL/freeglut.h>
29 #include "fg_internal.h"
30
31 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
32
33
34 /*
35  * Global callbacks.
36  */
37 /* Sets the global idle callback */
38 void FGAPIENTRY glutIdleFuncUcall( FGCBIdleUC callback, FGCBUserData userData )
39 {
40     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFuncUcall" );
41     fgState.IdleCallback = callback;
42     fgState.IdleCallbackData = userData;
43 }
44
45 void glutIdleFuncCallback( void* userData )
46 {
47     FGCBIdle callback = (FGCBIdle)userData;
48     callback();
49 }
50
51 void FGAPIENTRY glutIdleFunc( FGCBIdle callback )
52 {
53     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
54     glutIdleFuncUcall( glutIdleFuncCallback, (FGCBUserData)callback );
55 }
56
57 /* Creates a timer and sets its callback */
58 void FGAPIENTRY glutTimerFuncUcall( unsigned int timeOut, FGCBTimerUC callback, int timerID, FGCBUserData userData )
59 {
60     SFG_Timer *timer, *node;
61
62     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFuncUcall" );
63
64     if( (timer = fgState.FreeTimers.Last) )
65     {
66         fgListRemove( &fgState.FreeTimers, &timer->Node );
67     }
68     else
69     {
70         if( ! (timer = malloc(sizeof(SFG_Timer))) )
71             fgError( "Fatal error: "
72                      "Memory allocation failure in glutTimerFunc()" );
73     }
74
75     timer->Callback     = callback;
76     timer->CallbackData = userData;
77     timer->ID           = timerID;
78     timer->TriggerTime  = fgElapsedTime() + timeOut;
79
80     /* Insert such that timers are sorted by end-time */
81     for( node = fgState.Timers.First; node; node = node->Node.Next )
82     {
83         if( node->TriggerTime > timer->TriggerTime )
84             break;
85     }
86
87     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
88 }
89
90 void glutTimerFuncCallback( int ID, FGCBUserData userData )
91 {
92     FGCBTimer callback = (FGCBTimer)userData;
93     callback( ID );
94 }
95
96 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, FGCBTimer callback, int timerID )
97 {
98     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFunc" );
99     glutTimerFuncUcall( timeOut, glutTimerFuncCallback, timerID, (FGCBUserData)callback );
100 }
101
102 /* Deprecated version of glutMenuStatusFunc callback setting method */
103 void FGAPIENTRY glutMenuStateFunc( FGCBMenuState callback )
104 {
105     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
106     fgState.MenuStateCallback = callback;
107 }
108
109 /* Sets the global menu status callback for the current window */
110 void FGAPIENTRY glutMenuStatusFunc( FGCBMenuStatus callback )
111 {
112     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
113     fgState.MenuStatusCallback = callback;
114 }
115
116
117 /*
118  * Menu specific callbacks.
119  */
120 /* Callback upon menu destruction */
121 void FGAPIENTRY glutMenuDestroyFunc( FGCBDestroy callback )
122 {
123     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
124     if( fgStructure.CurrentMenu )
125         fgStructure.CurrentMenu->Destroy = callback;
126 }
127
128
129 /*
130  * All of the window-specific callbacks setting methods can be generalized to this:
131  */
132 #define SET_CALLBACK(a)                                         \
133 do                                                              \
134 {                                                               \
135     if( fgStructure.CurrentWindow == NULL )                     \
136         return;                                                 \
137     SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
138 } while( 0 )
139 /*
140  * And almost every time the callback setter function can be implemented like this:
141  */
142 #define IMPLEMENT_CALLBACK_FUNC_2NAME(a,b)                      \
143 void FGAPIENTRY glut##a##Func( FGCB##b callback )               \
144 {                                                               \
145     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glut"#a"Func" );    \
146     SET_CALLBACK( b );                                          \
147 }
148 #define IMPLEMENT_CALLBACK_FUNC(a) IMPLEMENT_CALLBACK_FUNC_2NAME(a,a)
149
150 /* Implement all these callback setter functions... */
151 IMPLEMENT_CALLBACK_FUNC(Position)
152 IMPLEMENT_CALLBACK_FUNC(Keyboard)
153 IMPLEMENT_CALLBACK_FUNC(KeyboardUp)
154 IMPLEMENT_CALLBACK_FUNC(Special)
155 IMPLEMENT_CALLBACK_FUNC(SpecialUp)
156 IMPLEMENT_CALLBACK_FUNC(Mouse)
157 IMPLEMENT_CALLBACK_FUNC(MouseWheel)
158 IMPLEMENT_CALLBACK_FUNC(Motion)
159 IMPLEMENT_CALLBACK_FUNC_2NAME(PassiveMotion,Passive)
160 IMPLEMENT_CALLBACK_FUNC(Entry)
161 /* glutWMCloseFunc is an alias for glutCloseFunc; both set the window's Destroy callback */
162 IMPLEMENT_CALLBACK_FUNC_2NAME(Close,Destroy)
163 IMPLEMENT_CALLBACK_FUNC_2NAME(WMClose,Destroy)
164 IMPLEMENT_CALLBACK_FUNC(OverlayDisplay)
165 IMPLEMENT_CALLBACK_FUNC(WindowStatus)
166 IMPLEMENT_CALLBACK_FUNC(ButtonBox)
167 IMPLEMENT_CALLBACK_FUNC(Dials)
168 IMPLEMENT_CALLBACK_FUNC(TabletMotion)
169 IMPLEMENT_CALLBACK_FUNC(TabletButton)
170 IMPLEMENT_CALLBACK_FUNC(MultiEntry)
171 IMPLEMENT_CALLBACK_FUNC(MultiButton)
172 IMPLEMENT_CALLBACK_FUNC(MultiMotion)
173 IMPLEMENT_CALLBACK_FUNC(MultiPassive)
174 IMPLEMENT_CALLBACK_FUNC(InitContext)
175 IMPLEMENT_CALLBACK_FUNC(AppStatus)
176
177
178
179 /*
180  * Sets the Display callback for the current window
181  */
182 void FGAPIENTRY glutDisplayFunc( FGCBDisplay callback )
183 {
184     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
185     if( !callback )
186         fgError( "Fatal error in program.  NULL display callback not "
187                  "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
188     SET_CALLBACK( Display );
189 }
190
191 void fghDefaultReshape(int width, int height)
192 {
193     glViewport( 0, 0, width, height );
194 }
195
196 void FGAPIENTRY glutReshapeFunc( FGCBReshape callback )
197 {
198     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeFunc" );
199     
200     if( !callback )
201         callback = fghDefaultReshape;
202
203     SET_CALLBACK( Reshape );
204 }
205
206 /*
207  * Sets the Visibility callback for the current window.
208  * NB: the Visibility func is deprecated in favor of the WindowStatus func,
209  * which provides more detail. The visibility func callback is implemented
210  * as a translation step from the windowStatus func. When the user sets the
211  * windowStatus func, any visibility func is overwritten.
212  * DEVELOPER NOTE: in the library, only invoke the window status func, this
213  * gets automatically translated to the visibility func if thats what the
214  * user has set.
215  * window status is kind of anemic on win32 as there are no window messages
216  * to notify us that the window is covered by other windows or not.
217  * Should one want to query this, see
218  * http://stackoverflow.com/questions/5445889/get-which-process-window-is-actually-visible-in-c-sharp
219  * for an implementation outline (but it would be polling based, not push based).
220  */
221 static void fghVisibility( int status )
222 {
223     int vis_status;
224
225     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
226     freeglut_return_if_fail( fgStructure.CurrentWindow );
227
228     /* Translate window status func states to visibility states */
229     if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
230         vis_status = GLUT_NOT_VISIBLE;
231     else    /* GLUT_FULLY_RETAINED, GLUT_PARTIALLY_RETAINED */
232         vis_status = GLUT_VISIBLE;
233
234     INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( vis_status ) );
235 }
236
237 void FGAPIENTRY glutVisibilityFunc( FGCBVisibility callback )
238 {
239     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
240     SET_CALLBACK( Visibility );
241
242     if( callback )
243         glutWindowStatusFunc( fghVisibility );
244     else
245         glutWindowStatusFunc( NULL );
246 }
247
248 /*
249  * Sets the joystick callback and polling rate for the current window
250  */
251 void FGAPIENTRY glutJoystickFunc( FGCBJoystick callback, int pollInterval )
252 {
253     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
254     fgInitialiseJoysticks ();
255
256     if ( (
257            fgStructure.CurrentWindow->State.JoystickPollRate <= 0 ||        /* Joystick callback was disabled */
258            !FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
259          ) &&
260          ( 
261            callback && ( pollInterval > 0 )                                 /* but is now enabled */
262          ) )
263         ++fgState.NumActiveJoysticks;
264     else if ( ( 
265                 fgStructure.CurrentWindow->State.JoystickPollRate > 0 &&    /* Joystick callback was enabled */
266                 FETCH_WCB(*fgStructure.CurrentWindow,Joystick)
267               ) &&  
268               ( 
269                 !callback || ( pollInterval <= 0 )                          /* but is now disabled */
270               ) )
271         --fgState.NumActiveJoysticks;
272
273     SET_CALLBACK( Joystick );
274     fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
275
276     /* set last poll time such that joystick will be polled asap */
277     fgStructure.CurrentWindow->State.JoystickLastPoll = fgElapsedTime();
278     if (fgStructure.CurrentWindow->State.JoystickLastPoll < pollInterval)
279         fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
280     else
281         fgStructure.CurrentWindow->State.JoystickLastPoll -= pollInterval;
282 }
283
284
285
286 /*
287  * Sets the spaceball motion callback for the current window
288  */
289 void FGAPIENTRY glutSpaceballMotionFunc( FGCBSpaceMotion callback )
290 {
291     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
292     fgInitialiseSpaceball();
293
294     SET_CALLBACK( SpaceMotion );
295 }
296
297 /*
298  * Sets the spaceball rotate callback for the current window
299  */
300 void FGAPIENTRY glutSpaceballRotateFunc( FGCBSpaceRotation callback )
301 {
302     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
303     fgInitialiseSpaceball();
304
305     SET_CALLBACK( SpaceRotation );
306 }
307
308 /*
309  * Sets the spaceball button callback for the current window
310  */
311 void FGAPIENTRY glutSpaceballButtonFunc( FGCBSpaceButton callback )
312 {
313     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
314     fgInitialiseSpaceball();
315
316     SET_CALLBACK( SpaceButton );
317 }
318
319 /*** END OF FILE ***/