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 "freeglut_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)
44 typedef struct GXDisplayProperties GXDisplayProperties;
45 typedef struct GXKeyList GXKeyList;
48 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
49 typedef int (*GXOPENINPUT)();
51 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
52 GXOPENINPUT GXOpenInput_ = NULL;
54 struct GXKeyList gxKeyList;
56 #endif /* _WIN32_WCE */
59 * Try to get the maximum value allowed for ints, falling back to the minimum
60 * guaranteed by ISO C99 if there is no suitable header.
66 # define INT_MAX 32767
70 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
73 extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );
74 extern void fgPlatformDisplayWindow ( SFG_Window *window );
75 extern unsigned long fgPlatformSystemTime ( void );
76 extern void fgPlatformSleepForEvents( long msec );
77 extern void fgPlatformProcessSingleEvent ( void );
78 extern void fgPlatformMainLoopPreliminaryWork ( void );
83 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
85 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
87 SFG_Window *current_window = fgStructure.CurrentWindow;
89 freeglut_return_if_fail( window != NULL );
91 fgPlatformReshapeWindow ( window, width, height );
93 if( FETCH_WCB( *window, Reshape ) )
94 INVOKE_WCB( *window, Reshape, ( width, height ) );
97 fgSetWindow( window );
98 glViewport( 0, 0, width, height );
102 * Force a window redraw. In Windows at least this is only a partial
103 * solution: if the window is increasing in size in either dimension,
104 * the already-drawn part does not get drawn again and things look funny.
105 * But without this we get this bad behaviour whenever we resize the
108 window->State.Redisplay = GL_TRUE;
111 fgSetWindow( current_window );
115 * Calls a window's redraw method. This is used when
116 * a redraw is forced by the incoming window messages.
118 void fghRedrawWindow ( SFG_Window *window )
120 SFG_Window *current_window = fgStructure.CurrentWindow;
122 freeglut_return_if_fail( window );
123 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
125 window->State.Redisplay = GL_FALSE;
127 freeglut_return_if_fail( window->State.Visible );
129 fgSetWindow( window );
131 if( window->State.NeedToResize )
139 window->State.NeedToResize = GL_FALSE;
142 INVOKE_WCB( *window, Display, ( ) );
144 fgSetWindow( current_window );
148 static void fghcbDisplayWindow( SFG_Window *window,
149 SFG_Enumerator *enumerator )
151 if( window->State.Redisplay &&
152 window->State.Visible )
154 window->State.Redisplay = GL_FALSE;
155 fgPlatformDisplayWindow ( window );
158 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
162 * Make all windows perform a display call
164 static void fghDisplayAll( void )
166 SFG_Enumerator enumerator;
168 enumerator.found = GL_FALSE;
169 enumerator.data = NULL;
171 fgEnumWindows( fghcbDisplayWindow, &enumerator );
175 * Window enumerator callback to check for the joystick polling code
177 static void fghcbCheckJoystickPolls( SFG_Window *window,
178 SFG_Enumerator *enumerator )
180 long int 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;
191 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
195 * Check all windows for joystick polling
197 static void fghCheckJoystickPolls( void )
199 SFG_Enumerator enumerator;
201 enumerator.found = GL_FALSE;
202 enumerator.data = NULL;
204 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
208 * Check the global timers
210 static void fghCheckTimers( void )
212 long checkTime = fgElapsedTime( );
214 while( fgState.Timers.First )
216 SFG_Timer *timer = fgState.Timers.First;
218 if( timer->TriggerTime > checkTime )
221 fgListRemove( &fgState.Timers, &timer->Node );
222 fgListAppend( &fgState.FreeTimers, &timer->Node );
224 timer->Callback( timer->ID );
229 /* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
230 * This value wraps every 49.7 days, but integer overflows cancel
231 * when subtracting an initial start time, unless the total time exceeds
232 * 32-bit, where the GLUT API return value is also overflowed.
234 unsigned long fgSystemTime(void)
236 return fgPlatformSystemTime ();
242 long fgElapsedTime( void )
244 return (long) (fgSystemTime() - fgState.Time);
250 void fgError( const char *fmt, ... )
254 if (fgState.ErrorFunc) {
258 /* call user set error handler here */
259 fgState.ErrorFunc(fmt, ap);
267 fprintf( stderr, "freeglut ");
268 if( fgState.ProgramName )
269 fprintf( stderr, "(%s): ", fgState.ProgramName );
270 VFPRINTF( stderr, fmt, ap );
271 fprintf( stderr, "\n" );
275 if ( fgState.Initialised )
282 void fgWarning( const char *fmt, ... )
286 if (fgState.WarningFunc) {
290 /* call user set warning handler here */
291 fgState.WarningFunc(fmt, ap);
299 fprintf( stderr, "freeglut ");
300 if( fgState.ProgramName )
301 fprintf( stderr, "(%s): ", fgState.ProgramName );
302 VFPRINTF( stderr, fmt, ap );
303 fprintf( stderr, "\n" );
311 * Indicates whether Joystick events are being used by ANY window.
313 * The current mechanism is to walk all of the windows and ask if
314 * there is a joystick callback. We have a short-circuit early
315 * return if we find any joystick handler registered.
317 * The real way to do this is to make use of the glutTimer() API
318 * to more cleanly re-implement the joystick API. Then, this code
319 * and all other "joystick timer" code can be yanked.
322 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
324 if( FETCH_WCB( *w, Joystick ) )
329 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
331 static int fghHaveJoystick( void )
333 SFG_Enumerator enumerator;
335 enumerator.found = GL_FALSE;
336 enumerator.data = NULL;
337 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
338 return !!enumerator.data;
340 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
342 if( w->State.Redisplay && w->State.Visible )
347 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
349 static int fghHavePendingRedisplays (void)
351 SFG_Enumerator enumerator;
353 enumerator.found = GL_FALSE;
354 enumerator.data = NULL;
355 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
356 return !!enumerator.data;
359 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
361 static long fghNextTimer( void )
364 SFG_Timer *timer = fgState.Timers.First;
367 ret = timer->TriggerTime - fgElapsedTime();
374 static void fghSleepForEvents( void )
378 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
381 msec = fghNextTimer( );
382 /* XXX Use GLUT timers for joysticks... */
383 /* XXX Dumb; forces granularity to .01sec */
384 if( fghHaveJoystick( ) && ( msec > 10 ) )
387 fgPlatformSleepForEvents ( msec );
391 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
394 * Executes a single iteration in the freeglut processing loop.
396 void FGAPIENTRY glutMainLoopEvent( void )
398 fgPlatformProcessSingleEvent ();
400 if( fgState.Timers.First )
402 fghCheckJoystickPolls( );
409 * Enters the freeglut processing loop.
410 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
412 void FGAPIENTRY glutMainLoop( void )
416 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
418 fgPlatformMainLoopPreliminaryWork ();
420 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
421 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
425 glutMainLoopEvent( );
427 * Step through the list of windows, seeing if there are any
430 for( window = ( SFG_Window * )fgStructure.Windows.First;
432 window = ( SFG_Window * )window->Node.Next )
433 if ( ! ( window->IsMenu ) )
437 fgState.ExecState = GLUT_EXEC_STATE_STOP;
440 if( fgState.IdleCallback )
442 if( fgStructure.CurrentWindow &&
443 fgStructure.CurrentWindow->IsMenu )
445 fgSetWindow( window );
446 fgState.IdleCallback( );
449 fghSleepForEvents( );
454 * When this loop terminates, destroy the display, state and structure
455 * of a freeglut session, so that another glutInit() call can happen
457 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
459 action = fgState.ActionOnWindowClose;
461 if( action == GLUT_ACTION_EXIT )
466 * Leaves the freeglut processing loop.
468 void FGAPIENTRY glutLeaveMainLoop( void )
470 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
471 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
476 /*** END OF FILE ***/