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 fgPlatformProcessWork ( SFG_Window *window );
58 extern fg_time_t fgPlatformSystemTime ( void );
59 extern void fgPlatformSleepForEvents( fg_time_t msec );
60 extern void fgPlatformProcessSingleEvent ( void );
61 extern void fgPlatformMainLoopPreliminaryWork ( void );
66 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
68 void fghOnReshapeNotify(SFG_Window *window, int width, int height, GLboolean forceNotify)
70 GLboolean notify = GL_FALSE;
72 if( width != window->State.Width ||
73 height != window->State.Height )
75 window->State.Width = width;
76 window->State.Height = height;
81 if (notify || forceNotify)
83 SFG_Window *saved_window = fgStructure.CurrentWindow;
85 INVOKE_WCB( *window, Reshape, ( width, height ) );
88 * Force a window redraw. In Windows at least this is only a partial
89 * solution: if the window is increasing in size in either dimension,
90 * the already-drawn part does not get drawn again and things look funny.
91 * But without this we get this bad behaviour whenever we resize the
93 * DN: Hmm.. the above sounds like a concern only in single buffered mode...
95 window->State.WorkMask |= GLUT_DISPLAY_WORK;
97 fgSetWindow( saved_window );
101 void fghOnPositionNotify(SFG_Window *window, int x, int y, GLboolean forceNotify)
103 GLboolean notify = GL_FALSE;
105 if( x != window->State.Xpos ||
106 y != window->State.Ypos )
108 window->State.Xpos = x;
109 window->State.Ypos = y;
114 if (notify || forceNotify)
116 SFG_Window *saved_window = fgStructure.CurrentWindow;
117 INVOKE_WCB( *window, Position, ( x, y ) );
118 fgSetWindow( saved_window );
123 * Calls a window's redraw method. This is used when
124 * a redraw is forced by the incoming window messages,
125 * or if a redisplay is otherwise pending.
126 * this is lean and mean without checks as it is
127 * currently only called from fghcbDisplayWindow which
128 * only calls this if the window is visible and needs
130 * Note that the fgSetWindow call on Windows makes the
131 * right device context current on windows, allowing
132 * direct drawing without BeginPaint/EndPaint in the
135 void fghRedrawWindow ( SFG_Window *window )
137 SFG_Window *current_window = fgStructure.CurrentWindow;
139 fgSetWindow( window );
140 INVOKE_WCB( *window, Display, ( ) );
142 fgSetWindow( current_window );
145 void fghRedrawWindowAndChildren ( SFG_Window *window )
149 fghRedrawWindow(window);
151 for( child = ( SFG_Window * )window->Children.First;
153 child = ( SFG_Window * )child->Node.Next )
155 fghRedrawWindowAndChildren(child);
160 static void fghcbProcessWork( SFG_Window *window,
161 SFG_Enumerator *enumerator )
163 if( window->State.WorkMask )
164 fgPlatformProcessWork ( window );
166 fgEnumSubWindows( window, fghcbProcessWork, enumerator );
170 * Make all windows process their work list
172 static void fghProcessWork( void )
174 SFG_Enumerator enumerator;
176 enumerator.found = GL_FALSE;
177 enumerator.data = NULL;
179 fgEnumWindows( fghcbProcessWork, &enumerator );
183 * Window enumerator callback to check for the joystick polling code
185 static void fghcbCheckJoystickPolls( SFG_Window *window,
186 SFG_Enumerator *enumerator )
190 if (window->State.JoystickPollRate > 0 && FETCH_WCB( *window, Joystick ))
192 /* This window has a joystick to be polled (if pollrate <= 0, user needs to poll manually with glutForceJoystickFunc */
193 checkTime= fgElapsedTime( );
195 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
198 #if !defined(_WIN32_WCE)
199 fgJoystickPollWindow( window );
200 #endif /* !defined(_WIN32_WCE) */
201 window->State.JoystickLastPoll = checkTime;
205 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
209 * Check all windows for joystick polling
211 * The real way to do this is to make use of the glutTimer() API
212 * to more cleanly re-implement the joystick API. Then, this code
213 * and all other "joystick timer" code can be yanked.
215 static void fghCheckJoystickPolls( void )
217 SFG_Enumerator enumerator;
219 enumerator.found = GL_FALSE;
220 enumerator.data = NULL;
222 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
226 * Check the global timers
228 static void fghCheckTimers( void )
230 fg_time_t checkTime = fgElapsedTime( );
232 while( fgState.Timers.First )
234 SFG_Timer *timer = fgState.Timers.First;
236 if( timer->TriggerTime > checkTime )
237 /* Timers are sorted by triggerTime */
240 fgListRemove( &fgState.Timers, &timer->Node );
241 fgListAppend( &fgState.FreeTimers, &timer->Node );
243 timer->Callback( timer->ID );
248 /* Platform-dependent time in milliseconds, as an unsigned 64-bit integer.
249 * This doesn't overflow in any reasonable time, so no need to worry about
250 * that. The GLUT API return value will however overflow after 49.7 days,
251 * which means you will still get in trouble when running the
252 * application for more than 49.7 days.
254 fg_time_t fgSystemTime(void)
256 return fgPlatformSystemTime();
262 fg_time_t fgElapsedTime( void )
264 return fgSystemTime() - fgState.Time;
270 void fgError( const char *fmt, ... )
274 if (fgState.ErrorFunc) {
278 /* call user set error handler here */
279 fgState.ErrorFunc(fmt, ap);
284 #ifdef FREEGLUT_PRINT_ERRORS
287 fprintf( stderr, "freeglut ");
288 if( fgState.ProgramName )
289 fprintf( stderr, "(%s): ", fgState.ProgramName );
290 VFPRINTF( stderr, fmt, ap );
291 fprintf( stderr, "\n" );
296 if ( fgState.Initialised )
303 void fgWarning( const char *fmt, ... )
307 if (fgState.WarningFunc) {
311 /* call user set warning handler here */
312 fgState.WarningFunc(fmt, ap);
317 #ifdef FREEGLUT_PRINT_WARNINGS
320 fprintf( stderr, "freeglut ");
321 if( fgState.ProgramName )
322 fprintf( stderr, "(%s): ", fgState.ProgramName );
323 VFPRINTF( stderr, fmt, ap );
324 fprintf( stderr, "\n" );
333 * Indicates whether work is pending for ANY window.
335 * The current mechanism is to walk all of the windows and ask if
336 * work is pending. We have a short-circuit early return if we find any.
338 static void fghHavePendingWorkCallback( SFG_Window* w, SFG_Enumerator* e)
340 if( w->State.WorkMask )
346 fgEnumSubWindows( w, fghHavePendingWorkCallback, e );
348 static int fghHavePendingWork (void)
350 SFG_Enumerator enumerator;
352 enumerator.found = GL_FALSE;
353 enumerator.data = NULL;
354 fgEnumWindows( fghHavePendingWorkCallback, &enumerator );
355 return !!enumerator.data;
359 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
361 static fg_time_t fghNextTimer( void )
363 fg_time_t currentTime;
364 SFG_Timer *timer = fgState.Timers.First; /* timers are sorted by trigger time, so only have to check the first */
369 currentTime = fgElapsedTime();
370 if( timer->TriggerTime < currentTime )
373 return timer->TriggerTime - currentTime;
376 static void fghSleepForEvents( void )
380 if( fghHavePendingWork( ) )
383 msec = fghNextTimer( );
384 /* XXX Should use GLUT timers for joysticks... */
385 /* XXX Dumb; forces granularity to .01sec */
386 if( fgState.NumActiveJoysticks>0 && ( msec > 10 ) )
389 fgPlatformSleepForEvents ( msec );
393 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
396 * Executes a single iteration in the freeglut processing loop.
398 void FGAPIENTRY glutMainLoopEvent( void )
401 fgPlatformProcessSingleEvent ();
403 if( fgState.Timers.First )
405 if (fgState.NumActiveJoysticks>0) /* If zero, don't poll joysticks */
406 fghCheckJoystickPolls( );
408 /* Perform work on the window (position, reshape, display, etc) */
415 * Enters the freeglut processing loop.
416 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
418 void FGAPIENTRY glutMainLoop( void )
422 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
424 if (!fgStructure.Windows.First)
425 fgError(" ERROR: glutMainLoop called with no windows created.");
427 fgPlatformMainLoopPreliminaryWork ();
429 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
430 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
434 glutMainLoopEvent( );
436 * Step through the list of windows, seeing if there are any
439 for( window = ( SFG_Window * )fgStructure.Windows.First;
441 window = ( SFG_Window * )window->Node.Next )
442 if ( ! ( window->IsMenu ) )
446 fgState.ExecState = GLUT_EXEC_STATE_STOP;
449 if( fgState.IdleCallback )
451 if( fgStructure.CurrentWindow &&
452 fgStructure.CurrentWindow->IsMenu )
454 fgSetWindow( window );
455 fgState.IdleCallback( );
458 fghSleepForEvents( );
463 * When this loop terminates, destroy the display, state and structure
464 * of a freeglut session, so that another glutInit() call can happen
466 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
468 action = fgState.ActionOnWindowClose;
470 if( action == GLUT_ACTION_EXIT )
475 * Leaves the freeglut processing loop.
477 void FGAPIENTRY glutLeaveMainLoop( void )
479 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
480 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
485 /*** END OF FILE ***/