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
92 window->State.Redisplay = GL_TRUE;
95 fgSetWindow( current_window );
99 * Calls a window's redraw method. This is used when
100 * a redraw is forced by the incoming window messages.
102 void fghRedrawWindow ( SFG_Window *window )
104 SFG_Window *current_window = fgStructure.CurrentWindow;
106 freeglut_return_if_fail( window );
107 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
109 window->State.Redisplay = GL_FALSE;
111 freeglut_return_if_fail( window->State.Visible );
113 fgSetWindow( window );
115 if( window->State.NeedToResize )
123 window->State.NeedToResize = GL_FALSE;
126 INVOKE_WCB( *window, Display, ( ) );
128 fgSetWindow( current_window );
132 static void fghcbDisplayWindow( SFG_Window *window,
133 SFG_Enumerator *enumerator )
135 if( window->State.Redisplay &&
136 window->State.Visible )
138 window->State.Redisplay = GL_FALSE;
139 fgPlatformDisplayWindow ( window );
142 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
146 * Make all windows perform a display call
148 static void fghDisplayAll( void )
150 SFG_Enumerator enumerator;
152 enumerator.found = GL_FALSE;
153 enumerator.data = NULL;
155 fgEnumWindows( fghcbDisplayWindow, &enumerator );
159 * Window enumerator callback to check for the joystick polling code
161 static void fghcbCheckJoystickPolls( SFG_Window *window,
162 SFG_Enumerator *enumerator )
164 fg_time_t checkTime = fgElapsedTime( );
166 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
169 #if !defined(_WIN32_WCE)
170 fgJoystickPollWindow( window );
171 #endif /* !defined(_WIN32_WCE) */
172 window->State.JoystickLastPoll = checkTime;
175 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
179 * Check all windows for joystick polling
181 static void fghCheckJoystickPolls( void )
183 SFG_Enumerator enumerator;
185 enumerator.found = GL_FALSE;
186 enumerator.data = NULL;
188 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
192 * Check the global timers
194 static void fghCheckTimers( void )
196 fg_time_t checkTime = fgElapsedTime( );
198 while( fgState.Timers.First )
200 SFG_Timer *timer = fgState.Timers.First;
202 if( timer->TriggerTime > checkTime )
205 fgListRemove( &fgState.Timers, &timer->Node );
206 fgListAppend( &fgState.FreeTimers, &timer->Node );
208 timer->Callback( timer->ID );
213 /* Platform-dependent time in milliseconds, as an unsigned 64-bit integer.
214 * This doesn't overflow in any reasonable time, so no need to worry about
215 * that. The GLUT API return value will however overflow after 49.7 days,
216 * and on Windows we (currently) do not have access to a 64-bit timestamp,
217 * which means internal time will still get in trouble when running the
218 * application for more than 49.7 days.
219 * This value wraps every 49.7 days, but integer overflows cancel
220 * when subtracting an initial start time, unless the total time exceeds
221 * 32-bit, where the GLUT API return value is also overflowed.
223 fg_time_t fgSystemTime(void)
225 return fgPlatformSystemTime();
231 fg_time_t fgElapsedTime( void )
233 return fgSystemTime() - fgState.Time;
239 void fgError( const char *fmt, ... )
243 if (fgState.ErrorFunc) {
247 /* call user set error handler here */
248 fgState.ErrorFunc(fmt, ap);
256 fprintf( stderr, "freeglut ");
257 if( fgState.ProgramName )
258 fprintf( stderr, "(%s): ", fgState.ProgramName );
259 VFPRINTF( stderr, fmt, ap );
260 fprintf( stderr, "\n" );
264 if ( fgState.Initialised )
271 void fgWarning( const char *fmt, ... )
275 if (fgState.WarningFunc) {
279 /* call user set warning handler here */
280 fgState.WarningFunc(fmt, ap);
288 fprintf( stderr, "freeglut ");
289 if( fgState.ProgramName )
290 fprintf( stderr, "(%s): ", fgState.ProgramName );
291 VFPRINTF( stderr, fmt, ap );
292 fprintf( stderr, "\n" );
300 * Indicates whether Joystick events are being used by ANY window.
302 * The current mechanism is to walk all of the windows and ask if
303 * there is a joystick callback. We have a short-circuit early
304 * return if we find any joystick handler registered.
306 * The real way to do this is to make use of the glutTimer() API
307 * to more cleanly re-implement the joystick API. Then, this code
308 * and all other "joystick timer" code can be yanked.
311 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
313 if( FETCH_WCB( *w, Joystick ) )
318 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
320 static int fghHaveJoystick( void )
322 SFG_Enumerator enumerator;
324 enumerator.found = GL_FALSE;
325 enumerator.data = NULL;
326 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
327 return !!enumerator.data;
329 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
331 if( w->State.Redisplay && w->State.Visible )
336 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
338 static int fghHavePendingRedisplays (void)
340 SFG_Enumerator enumerator;
342 enumerator.found = GL_FALSE;
343 enumerator.data = NULL;
344 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
345 return !!enumerator.data;
348 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
350 static fg_time_t fghNextTimer( void )
352 fg_time_t currentTime = fgElapsedTime();
353 SFG_Timer *timer = fgState.Timers.First;
358 if( timer->TriggerTime < currentTime )
361 return timer->TriggerTime - currentTime;
364 static void fghSleepForEvents( void )
368 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
371 msec = fghNextTimer( );
372 /* XXX Use GLUT timers for joysticks... */
373 /* XXX Dumb; forces granularity to .01sec */
374 if( fghHaveJoystick( ) && ( msec > 10 ) )
377 fgPlatformSleepForEvents ( msec );
381 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
384 * Executes a single iteration in the freeglut processing loop.
386 void FGAPIENTRY glutMainLoopEvent( void )
388 fgPlatformProcessSingleEvent ();
390 if( fgState.Timers.First )
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 fgPlatformMainLoopPreliminaryWork ();
410 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
411 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
415 glutMainLoopEvent( );
417 * Step through the list of windows, seeing if there are any
420 for( window = ( SFG_Window * )fgStructure.Windows.First;
422 window = ( SFG_Window * )window->Node.Next )
423 if ( ! ( window->IsMenu ) )
427 fgState.ExecState = GLUT_EXEC_STATE_STOP;
430 if( fgState.IdleCallback )
432 if( fgStructure.CurrentWindow &&
433 fgStructure.CurrentWindow->IsMenu )
435 fgSetWindow( window );
436 fgState.IdleCallback( );
439 fghSleepForEvents( );
444 * When this loop terminates, destroy the display, state and structure
445 * of a freeglut session, so that another glutInit() call can happen
447 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
449 action = fgState.ActionOnWindowClose;
451 if( action == GLUT_ACTION_EXIT )
456 * Leaves the freeglut processing loop.
458 void FGAPIENTRY glutLeaveMainLoop( void )
460 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
461 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
466 /*** END OF FILE ***/