used a macro to remove all the code repetition in fg_callbacks.c
[freeglut] / src / fg_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 #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 glutIdleFunc( FGCBIdle callback )
39 {
40     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIdleFunc" );
41     fgState.IdleCallback = callback;
42 }
43
44 /* Creates a timer and sets its callback */
45 void FGAPIENTRY glutTimerFunc( unsigned int timeOut, FGCBTimer callback, int timerID )
46 {
47     SFG_Timer *timer, *node;
48
49     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutTimerFunc" );
50
51     if( (timer = fgState.FreeTimers.Last) )
52     {
53         fgListRemove( &fgState.FreeTimers, &timer->Node );
54     }
55     else
56     {
57         if( ! (timer = malloc(sizeof(SFG_Timer))) )
58             fgError( "Fatal error: "
59                      "Memory allocation failure in glutTimerFunc()" );
60     }
61
62     timer->Callback  = callback;
63     timer->ID        = timerID;
64     timer->TriggerTime = fgElapsedTime() + timeOut;
65
66     for( node = fgState.Timers.First; node; node = node->Node.Next )
67     {
68         if( node->TriggerTime > timer->TriggerTime )
69             break;
70     }
71
72     fgListInsert( &fgState.Timers, &node->Node, &timer->Node );
73 }
74
75 /* Deprecated version of glutMenuStatusFunc callback setting method */
76 void FGAPIENTRY glutMenuStateFunc( FGCBMenuState callback )
77 {
78     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStateFunc" );
79     fgState.MenuStateCallback = callback;
80 }
81
82 /* Sets the global menu status callback for the current window */
83 void FGAPIENTRY glutMenuStatusFunc( FGCBMenuStatus callback )
84 {
85     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuStatusFunc" );
86     fgState.MenuStatusCallback = callback;
87 }
88
89
90 /*
91  * Menu specific callbacks.
92  */
93 /* Callback upon menu destruction */
94 void FGAPIENTRY glutMenuDestroyFunc( FGCBDestroy callback )
95 {
96     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMenuDestroyFunc" );
97     if( fgStructure.CurrentMenu )
98         fgStructure.CurrentMenu->Destroy = callback;
99 }
100
101
102 /*
103  * All of the window-specific callbacks setting methods can be generalized to this:
104  */
105 #define SET_CALLBACK(a)                                         \
106 do                                                              \
107 {                                                               \
108     if( fgStructure.CurrentWindow == NULL )                     \
109         return;                                                 \
110     SET_WCB( ( *( fgStructure.CurrentWindow ) ), a, callback ); \
111 } while( 0 )
112 /*
113  * And almost every time the callback setter function can be implemented like this:
114  */
115 #define IMPLEMENT_CALLBACK_FUNC_2NAME(a,b)                      \
116 void FGAPIENTRY glut##a##Func( FGCB##b callback )               \
117 {                                                               \
118     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glut"###a##"Func" );    \
119     SET_CALLBACK( b );                                          \
120 }
121 #define IMPLEMENT_CALLBACK_FUNC(a) IMPLEMENT_CALLBACK_FUNC_2NAME(a,a)
122
123 /* Implement all these callback setter functions... */
124 IMPLEMENT_CALLBACK_FUNC(Reshape);
125 IMPLEMENT_CALLBACK_FUNC(Position);
126 IMPLEMENT_CALLBACK_FUNC(Keyboard);
127 IMPLEMENT_CALLBACK_FUNC(KeyboardUp);
128 IMPLEMENT_CALLBACK_FUNC(Special);
129 IMPLEMENT_CALLBACK_FUNC(SpecialUp);
130 IMPLEMENT_CALLBACK_FUNC(Mouse);
131 IMPLEMENT_CALLBACK_FUNC(MouseWheel);
132 IMPLEMENT_CALLBACK_FUNC(Motion);
133 IMPLEMENT_CALLBACK_FUNC_2NAME(PassiveMotion,Passive);
134 IMPLEMENT_CALLBACK_FUNC(Entry);
135 /* glutWMCloseFunc is an alias for glutCloseFunc; both set the window's Destroy callback */
136 IMPLEMENT_CALLBACK_FUNC_2NAME(Close,Destroy);
137 IMPLEMENT_CALLBACK_FUNC_2NAME(WMClose,Destroy);
138 IMPLEMENT_CALLBACK_FUNC(OverlayDisplay);
139 IMPLEMENT_CALLBACK_FUNC(WindowStatus);
140 IMPLEMENT_CALLBACK_FUNC(ButtonBox);
141 IMPLEMENT_CALLBACK_FUNC(Dials);
142 IMPLEMENT_CALLBACK_FUNC(TabletMotion);
143 IMPLEMENT_CALLBACK_FUNC(TabletButton);
144 IMPLEMENT_CALLBACK_FUNC(MultiEntry);
145 IMPLEMENT_CALLBACK_FUNC(MultiButton);
146 IMPLEMENT_CALLBACK_FUNC(MultiMotion);
147 IMPLEMENT_CALLBACK_FUNC(MultiPassive);
148 IMPLEMENT_CALLBACK_FUNC(InitContext);
149 IMPLEMENT_CALLBACK_FUNC(Pause);
150 IMPLEMENT_CALLBACK_FUNC(Resume);
151
152
153
154 /*
155  * Sets the Display callback for the current window
156  */
157 void FGAPIENTRY glutDisplayFunc( FGCBDisplay callback )
158 {
159     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDisplayFunc" );
160     if( !callback )
161         fgError( "Fatal error in program.  NULL display callback not "
162                  "permitted in GLUT 3.0+ or freeglut 2.0.1+" );
163     SET_CALLBACK( Display );
164 }
165
166 /*
167  * Sets the Visibility callback for the current window.
168  */
169 static void fghVisibility( int status )
170 {
171     int glut_status = GLUT_VISIBLE;
172
173     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Visibility Callback" );
174     freeglut_return_if_fail( fgStructure.CurrentWindow );
175
176     if( ( GLUT_HIDDEN == status )  || ( GLUT_FULLY_COVERED == status ) )
177         glut_status = GLUT_NOT_VISIBLE;
178     INVOKE_WCB( *( fgStructure.CurrentWindow ), Visibility, ( glut_status ) );
179 }
180
181 void FGAPIENTRY glutVisibilityFunc( FGCBVisibility callback )
182 {
183     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutVisibilityFunc" );
184     SET_CALLBACK( Visibility );
185
186     if( callback )
187         glutWindowStatusFunc( fghVisibility );
188     else
189         glutWindowStatusFunc( NULL );
190 }
191
192 /*
193  * Sets the joystick callback and polling rate for the current window
194  */
195 void FGAPIENTRY glutJoystickFunc( FGCBJoystick callback, int pollInterval )
196 {
197     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutJoystickFunc" );
198     fgInitialiseJoysticks ();
199
200     if ( ( ( fgStructure.CurrentWindow->State.JoystickPollRate < 0 ) ||
201            !FETCH_WCB(*fgStructure.CurrentWindow,Joystick) ) &&  /* Joystick callback was disabled */
202          ( callback && ( pollInterval >= 0 ) ) )               /* but is now enabled */
203         ++fgState.NumActiveJoysticks;
204     else if ( ( ( fgStructure.CurrentWindow->State.JoystickPollRate >= 0 ) &&
205                 FETCH_WCB(*fgStructure.CurrentWindow,Joystick) ) &&  /* Joystick callback was enabled */
206               ( !callback || ( pollInterval < 0 ) ) )              /* but is now disabled */
207         --fgState.NumActiveJoysticks;
208
209     SET_CALLBACK( Joystick );
210     fgStructure.CurrentWindow->State.JoystickPollRate = pollInterval;
211
212     fgStructure.CurrentWindow->State.JoystickLastPoll =
213         fgElapsedTime() - fgStructure.CurrentWindow->State.JoystickPollRate;
214
215     if( fgStructure.CurrentWindow->State.JoystickLastPoll < 0 )
216         fgStructure.CurrentWindow->State.JoystickLastPoll = 0;
217 }
218
219
220
221 /*
222  * Sets the spaceball motion callback for the current window
223  */
224 void FGAPIENTRY glutSpaceballMotionFunc( FGCBSpaceMotion callback )
225 {
226     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballMotionFunc" );
227     fgInitialiseSpaceball();
228
229     SET_CALLBACK( SpaceMotion );
230 }
231
232 /*
233  * Sets the spaceball rotate callback for the current window
234  */
235 void FGAPIENTRY glutSpaceballRotateFunc( FGCBSpaceRotation callback )
236 {
237     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballRotateFunc" );
238     fgInitialiseSpaceball();
239
240     SET_CALLBACK( SpaceRotation );
241 }
242
243 /*
244  * Sets the spaceball button callback for the current window
245  */
246 void FGAPIENTRY glutSpaceballButtonFunc( FGCBSpaceButton callback )
247 {
248     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSpaceballButtonFunc" );
249     fgInitialiseSpaceball();
250
251     SET_CALLBACK( SpaceButton );
252 }
253
254 /*** END OF FILE ***/