4 * The windows message processing methods.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
35 # define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
36 #elif defined(HAVE__DOPRNT)
37 # define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
39 # define VFPRINTF(s,f,a)
43 * Try to get the maximum value allowed for ints, falling back to the minimum
44 * guaranteed by ISO C99 if there is no suitable header.
50 # define INT_MAX 32767
54 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
57 extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );
58 extern void fgPlatformDisplayWindow ( SFG_Window *window );
59 extern fg_time_t fgPlatformSystemTime ( void );
60 extern void fgPlatformSleepForEvents( fg_time_t msec );
61 extern void fgPlatformProcessSingleEvent ( void );
62 extern void fgPlatformMainLoopPreliminaryWork ( void );
67 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
69 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
71 SFG_Window *current_window = fgStructure.CurrentWindow;
73 freeglut_return_if_fail( window != NULL );
75 fgPlatformReshapeWindow ( window, width, height );
77 if( FETCH_WCB( *window, Reshape ) )
78 INVOKE_WCB( *window, Reshape, ( width, height ) );
81 fgSetWindow( window );
82 glViewport( 0, 0, width, height );
86 * Force a window redraw. In Windows at least this is only a partial
87 * solution: if the window is increasing in size in either dimension,
88 * the already-drawn part does not get drawn again and things look funny.
89 * But without this we get this bad behaviour whenever we resize the
91 * DN: Hmm.. the above sounds like a concern only in single buffered mode...
93 window->State.Redisplay = GL_TRUE;
96 fgSetWindow( current_window );
100 * Calls a window's redraw method. This is used when
101 * a redraw is forced by the incoming window messages.
103 void fghRedrawWindow ( SFG_Window *window )
105 SFG_Window *current_window = fgStructure.CurrentWindow;
107 freeglut_return_if_fail( window );
109 if( window->State.NeedToInitContext ) {
110 INVOKE_WCB( *window, InitContext, ());
111 window->State.NeedToInitContext = GL_FALSE;
114 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
116 window->State.Redisplay = GL_FALSE;
118 freeglut_return_if_fail( window->State.Visible );
120 fgSetWindow( window );
122 if( window->State.NeedToResize )
124 /* Set need to resize to false before calling fghReshapeWindow, otherwise
125 in the case the user's reshape callback calls glutReshapeWindow,
126 his request would get canceled after fghReshapeWindow gets called.
128 window->State.NeedToResize = GL_FALSE;
137 INVOKE_WCB( *window, Display, ( ) );
139 fgSetWindow( current_window );
143 static void fghcbDisplayWindow( SFG_Window *window,
144 SFG_Enumerator *enumerator )
146 if( window->State.Redisplay &&
147 window->State.Visible )
149 window->State.Redisplay = GL_FALSE;
150 fgPlatformDisplayWindow ( window );
153 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
157 * Make all windows perform a display call
159 static void fghDisplayAll( void )
161 SFG_Enumerator enumerator;
163 enumerator.found = GL_FALSE;
164 enumerator.data = NULL;
166 fgEnumWindows( fghcbDisplayWindow, &enumerator );
170 * Window enumerator callback to check for the joystick polling code
172 static void fghcbCheckJoystickPolls( SFG_Window *window,
173 SFG_Enumerator *enumerator )
177 if (window->State.JoystickPollRate > 0 && FETCH_WCB( *window, Joystick ))
179 /* This window has a joystick to be polled (if pollrate <= 0, user needs to poll manually with glutForceJoystickFunc */
180 checkTime= fgElapsedTime( );
182 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
185 #if !defined(_WIN32_WCE)
186 fgJoystickPollWindow( window );
187 #endif /* !defined(_WIN32_WCE) */
188 window->State.JoystickLastPoll = checkTime;
192 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
196 * Check all windows for joystick polling
198 * The real way to do this is to make use of the glutTimer() API
199 * to more cleanly re-implement the joystick API. Then, this code
200 * and all other "joystick timer" code can be yanked.
202 static void fghCheckJoystickPolls( void )
204 SFG_Enumerator enumerator;
206 enumerator.found = GL_FALSE;
207 enumerator.data = NULL;
209 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
213 * Check the global timers
215 static void fghCheckTimers( void )
217 fg_time_t checkTime = fgElapsedTime( );
219 while( fgState.Timers.First )
221 SFG_Timer *timer = fgState.Timers.First;
223 if( timer->TriggerTime > checkTime )
226 fgListRemove( &fgState.Timers, &timer->Node );
227 fgListAppend( &fgState.FreeTimers, &timer->Node );
229 timer->Callback( timer->ID );
234 /* Platform-dependent time in milliseconds, as an unsigned 64-bit integer.
235 * This doesn't overflow in any reasonable time, so no need to worry about
236 * that. The GLUT API return value will however overflow after 49.7 days,
237 * which means you will still get in trouble when running the
238 * application for more than 49.7 days.
240 fg_time_t fgSystemTime(void)
242 return fgPlatformSystemTime();
248 fg_time_t fgElapsedTime( void )
250 return fgSystemTime() - fgState.Time;
256 void fgError( const char *fmt, ... )
260 if (fgState.ErrorFunc) {
264 /* call user set error handler here */
265 fgState.ErrorFunc(fmt, ap);
273 fprintf( stderr, "freeglut ");
274 if( fgState.ProgramName )
275 fprintf( stderr, "(%s): ", fgState.ProgramName );
276 VFPRINTF( stderr, fmt, ap );
277 fprintf( stderr, "\n" );
282 if ( fgState.Initialised )
289 void fgWarning( const char *fmt, ... )
293 if (fgState.WarningFunc) {
297 /* call user set warning handler here */
298 fgState.WarningFunc(fmt, ap);
303 #if FREEGLUT_WARNINGS
306 fprintf( stderr, "freeglut ");
307 if( fgState.ProgramName )
308 fprintf( stderr, "(%s): ", fgState.ProgramName );
309 VFPRINTF( stderr, fmt, ap );
310 fprintf( stderr, "\n" );
319 * Indicates whether Joystick events are being used by ANY window.
321 * The current mechanism is to walk all of the windows and ask if
322 * there is a joystick callback. We have a short-circuit early
323 * return if we find any joystick handler registered.
327 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
329 if( w->State.Redisplay && w->State.Visible )
334 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
336 static int fghHavePendingRedisplays (void)
338 SFG_Enumerator enumerator;
340 enumerator.found = GL_FALSE;
341 enumerator.data = NULL;
342 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
343 return !!enumerator.data;
347 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
349 static fg_time_t fghNextTimer( void )
351 fg_time_t currentTime = fgElapsedTime();
352 SFG_Timer *timer = fgState.Timers.First;
357 if( timer->TriggerTime < currentTime )
360 return timer->TriggerTime - currentTime;
363 static void fghSleepForEvents( void )
367 if( fghHavePendingRedisplays( ) )
370 msec = fghNextTimer( );
371 /* XXX Should use GLUT timers for joysticks... */
372 /* XXX Dumb; forces granularity to .01sec */
373 if( fgState.NumActiveJoysticks>0 && ( msec > 10 ) )
376 fgPlatformSleepForEvents ( msec );
380 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
383 * Executes a single iteration in the freeglut processing loop.
385 void FGAPIENTRY glutMainLoopEvent( void )
387 fgPlatformProcessSingleEvent ();
389 if( fgState.Timers.First )
391 if (fgState.NumActiveJoysticks>0) /* If zero, don't poll joysticks */
392 fghCheckJoystickPolls( );
399 * Enters the freeglut processing loop.
400 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
402 void FGAPIENTRY glutMainLoop( void )
406 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
408 if (!fgStructure.Windows.First)
409 fgError(" ERROR: glutMainLoop called with no windows created.");
411 fgPlatformMainLoopPreliminaryWork ();
413 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
414 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
418 glutMainLoopEvent( );
420 * Step through the list of windows, seeing if there are any
423 for( window = ( SFG_Window * )fgStructure.Windows.First;
425 window = ( SFG_Window * )window->Node.Next )
426 if ( ! ( window->IsMenu ) )
430 fgState.ExecState = GLUT_EXEC_STATE_STOP;
433 if( fgState.IdleCallback )
435 if( fgStructure.CurrentWindow &&
436 fgStructure.CurrentWindow->IsMenu )
438 fgSetWindow( window );
439 fgState.IdleCallback( );
442 fghSleepForEvents( );
447 * When this loop terminates, destroy the display, state and structure
448 * of a freeglut session, so that another glutInit() call can happen
450 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
452 action = fgState.ActionOnWindowClose;
454 if( action == GLUT_ACTION_EXIT )
459 * Leaves the freeglut processing loop.
461 void FGAPIENTRY glutLeaveMainLoop( void )
463 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
464 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
469 /*** END OF FILE ***/