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"
34 * Try to get the maximum value allowed for ints, falling back to the minimum
35 * guaranteed by ISO C99 if there is no suitable header.
41 # define INT_MAX 32767
45 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
48 extern void fgProcessWork ( SFG_Window *window );
49 extern fg_time_t fgPlatformSystemTime ( void );
50 extern void fgPlatformSleepForEvents( fg_time_t msec );
51 extern void fgPlatformProcessSingleEvent ( void );
52 extern void fgPlatformMainLoopPreliminaryWork ( void );
54 extern void fgPlatformInitWork(SFG_Window* window);
55 extern void fgPlatformPosResZordWork(SFG_Window* window, unsigned int workMask);
56 extern void fgPlatformVisibilityWork(SFG_Window* window);
59 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
61 void fghOnReshapeNotify(SFG_Window *window, int width, int height, GLboolean forceNotify)
63 GLboolean notify = GL_FALSE;
65 if( width != window->State.Width ||
66 height != window->State.Height )
68 window->State.Width = width;
69 window->State.Height = height;
74 if (notify || forceNotify)
76 SFG_Window *saved_window = fgStructure.CurrentWindow;
78 INVOKE_WCB( *window, Reshape, ( width, height ) );
81 * Force a window redraw. In Windows at least this is only a partial
82 * solution: if the window is increasing in size in either dimension,
83 * the already-drawn part does not get drawn again and things look funny.
84 * But without this we get this bad behaviour whenever we resize the
86 * DN: Hmm.. the above sounds like a concern only in single buffered mode...
88 window->State.WorkMask |= GLUT_DISPLAY_WORK;
90 fgSetWindow( saved_window );
94 void fghOnPositionNotify(SFG_Window *window, int x, int y, GLboolean forceNotify)
96 GLboolean notify = GL_FALSE;
98 if( x != window->State.Xpos ||
99 y != window->State.Ypos )
101 window->State.Xpos = x;
102 window->State.Ypos = y;
107 if (notify || forceNotify)
109 SFG_Window *saved_window = fgStructure.CurrentWindow;
110 INVOKE_WCB( *window, Position, ( x, y ) );
111 fgSetWindow( saved_window );
116 * Calls a window's redraw method. This is used when
117 * a redraw is forced by the incoming window messages,
118 * or if a redisplay is otherwise pending.
119 * this is lean and mean without checks as it is
120 * currently only called from fghcbDisplayWindow which
121 * only calls this if the window is visible and needs
123 * Note that the fgSetWindow call on Windows makes the
124 * right device context current on windows, allowing
125 * direct drawing without BeginPaint/EndPaint in the
128 void fghRedrawWindow ( SFG_Window *window )
130 SFG_Window *current_window = fgStructure.CurrentWindow;
132 fgSetWindow( window );
133 INVOKE_WCB( *window, Display, ( ) );
135 fgSetWindow( current_window );
138 void fghRedrawWindowAndChildren ( SFG_Window *window )
142 fghRedrawWindow(window);
144 for( child = ( SFG_Window * )window->Children.First;
146 child = ( SFG_Window * )child->Node.Next )
148 fghRedrawWindowAndChildren(child);
153 static void fghcbProcessWork( SFG_Window *window,
154 SFG_Enumerator *enumerator )
156 if( window->State.WorkMask )
157 fgProcessWork ( window );
159 fgEnumSubWindows( window, fghcbProcessWork, enumerator );
163 * Make all windows process their work list
165 static void fghProcessWork( void )
167 SFG_Enumerator enumerator;
169 enumerator.found = GL_FALSE;
170 enumerator.data = NULL;
172 fgEnumWindows( fghcbProcessWork, &enumerator );
176 * Window enumerator callback to check for the joystick polling code
178 static void fghcbCheckJoystickPolls( SFG_Window *window,
179 SFG_Enumerator *enumerator )
183 if (window->State.JoystickPollRate > 0 && FETCH_WCB( *window, Joystick ))
185 /* This window has a joystick to be polled (if pollrate <= 0, user needs to poll manually with glutForceJoystickFunc */
186 checkTime= fgElapsedTime( );
188 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
191 #if !defined(_WIN32_WCE)
192 fgJoystickPollWindow( window );
193 #endif /* !defined(_WIN32_WCE) */
194 window->State.JoystickLastPoll = checkTime;
198 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
202 * Check all windows for joystick polling
204 * The real way to do this is to make use of the glutTimer() API
205 * to more cleanly re-implement the joystick API. Then, this code
206 * and all other "joystick timer" code can be yanked.
208 static void fghCheckJoystickPolls( void )
210 SFG_Enumerator enumerator;
212 enumerator.found = GL_FALSE;
213 enumerator.data = NULL;
215 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
219 * Check the global timers
221 static void fghCheckTimers( void )
223 fg_time_t checkTime = fgElapsedTime( );
225 while( fgState.Timers.First )
227 SFG_Timer *timer = fgState.Timers.First;
229 if( timer->TriggerTime > checkTime )
230 /* Timers are sorted by triggerTime */
233 fgListRemove( &fgState.Timers, &timer->Node );
234 fgListAppend( &fgState.FreeTimers, &timer->Node );
236 timer->Callback( timer->ID, timer->CallbackData );
241 /* Platform-dependent time in milliseconds, as an unsigned 64-bit integer.
242 * This doesn't overflow in any reasonable time, so no need to worry about
243 * that. The GLUT API return value will however overflow after 49.7 days,
244 * which means you will still get in trouble when running the
245 * application for more than 49.7 days.
247 fg_time_t fgSystemTime(void)
249 return fgPlatformSystemTime();
255 fg_time_t fgElapsedTime( void )
257 return fgSystemTime() - fgState.Time;
263 void fgError( const char *fmt, ... )
267 if (fgState.ErrorFunc) {
271 /* call user set error handler here */
272 fgState.ErrorFunc(fmt, ap, fgState.ErrorFuncData);
277 #ifdef FREEGLUT_PRINT_ERRORS
280 fprintf( stderr, "freeglut ");
281 if( fgState.ProgramName )
282 fprintf( stderr, "(%s): ", fgState.ProgramName );
283 vfprintf( stderr, fmt, ap );
284 fprintf( stderr, "\n" );
289 if ( fgState.Initialised )
296 void fgWarning( const char *fmt, ... )
300 if (fgState.WarningFunc) {
304 /* call user set warning handler here */
305 fgState.WarningFunc(fmt, ap, fgState.WarningFuncData);
310 #ifdef FREEGLUT_PRINT_WARNINGS
313 fprintf( stderr, "freeglut ");
314 if( fgState.ProgramName )
315 fprintf( stderr, "(%s): ", fgState.ProgramName );
316 vfprintf( stderr, fmt, ap );
317 fprintf( stderr, "\n" );
326 * Indicates whether work is pending for ANY window.
328 * The current mechanism is to walk all of the windows and ask if
329 * work is pending. We have a short-circuit early return if we find any.
331 static void fghHavePendingWorkCallback( SFG_Window* w, SFG_Enumerator* e)
333 if( w->State.WorkMask )
339 fgEnumSubWindows( w, fghHavePendingWorkCallback, e );
341 static int fghHavePendingWork (void)
343 SFG_Enumerator enumerator;
345 enumerator.found = GL_FALSE;
346 enumerator.data = NULL;
347 fgEnumWindows( fghHavePendingWorkCallback, &enumerator );
348 return !!enumerator.data;
352 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
354 static fg_time_t fghNextTimer( void )
356 fg_time_t currentTime;
357 SFG_Timer *timer = fgState.Timers.First; /* timers are sorted by trigger time, so only have to check the first */
362 currentTime = fgElapsedTime();
363 if( timer->TriggerTime < currentTime )
366 return timer->TriggerTime - currentTime;
369 static void fghSleepForEvents( void )
373 if( fghHavePendingWork( ) )
376 msec = fghNextTimer( );
377 /* XXX Should use GLUT timers for joysticks... */
378 /* XXX Dumb; forces granularity to .01sec */
379 if( fgState.NumActiveJoysticks>0 && ( msec > 10 ) )
382 fgPlatformSleepForEvents ( msec );
386 /* Step through the work list */
387 void fgProcessWork(SFG_Window *window)
389 unsigned int workMask = window->State.WorkMask;
390 /* Now clear it so that any callback generated by the actions below can set work again */
391 window->State.WorkMask = 0;
393 if (workMask&~GLUT_DISPLAY_WORK) /* Display work is the common case, skip all the below at once */
395 if (workMask & GLUT_INIT_WORK)
397 /* This is before the first display callback: if needed for the platform,
398 * call a few callbacks to inform user of window size, position, etc
400 fgPlatformInitWork(window);
402 /* Call init context callback */
403 INVOKE_WCB( *window, InitContext, ( ) );
405 /* Lastly, check if we have a display callback, error out if not
406 * This is the right place to do it, as the redisplay will be
407 * next right after we exit this function, so there is no more
408 * opportunity for the user to register a callback for this window.
410 if (!FETCH_WCB(*window, Display))
411 fgError ( "ERROR: No display callback registered for window %d\n", window->ID );
414 /* On windows we can position, resize and change z order at the same time */
415 if (workMask & (GLUT_POSITION_WORK|GLUT_SIZE_WORK|GLUT_ZORDER_WORK|GLUT_FULL_SCREEN_WORK))
417 fgPlatformPosResZordWork(window,workMask);
420 if (workMask & GLUT_VISIBILITY_WORK)
422 fgPlatformVisibilityWork(window);
426 /* check window state's workmask as well as some of the above callbacks might have generated redisplay requests. We can deal with those right now instead of wait for the next mainloop iteration. */
427 if (workMask & GLUT_DISPLAY_WORK || window->State.WorkMask & GLUT_DISPLAY_WORK)
429 if( window->State.Visible )
431 /* Strip out display work from the work list */
432 /* NB: do this before the display callback is called as user might call postredisplay in his display callback */
433 window->State.WorkMask &= ~GLUT_DISPLAY_WORK;
435 fghRedrawWindow ( window );
441 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
444 * Executes a single iteration in the freeglut processing loop.
446 void FGAPIENTRY glutMainLoopEvent( void )
449 fgPlatformProcessSingleEvent ();
451 if( fgState.Timers.First )
453 if (fgState.NumActiveJoysticks>0) /* If zero, don't poll joysticks */
454 fghCheckJoystickPolls( );
456 /* Perform work on the window (position, reshape, display, etc) */
459 /* Check OpenGL error state if requested.
460 * Don't call if no more open windows (can happen if user closes window from
461 * title bar), would lead to infinite error loop in glutReportErrors
463 if (fgState.GLDebugSwitch && fgStructure.CurrentWindow)
470 * Enters the freeglut processing loop.
471 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
473 void FGAPIENTRY glutMainLoop( void )
477 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
479 if (!fgStructure.Windows.First)
480 fgError(" ERROR: glutMainLoop called with no windows created.");
482 fgPlatformMainLoopPreliminaryWork ();
484 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
489 glutMainLoopEvent( );
490 if( fgState.ExecState != GLUT_EXEC_STATE_RUNNING )
493 * Step through the list of windows, seeing if there are any
496 for( window = ( SFG_Window * )fgStructure.Windows.First;
498 window = ( SFG_Window * )window->Node.Next )
499 if ( ! ( window->IsMenu ) )
503 fgState.ExecState = GLUT_EXEC_STATE_STOP;
506 if( fgState.IdleCallback )
508 if( fgStructure.CurrentWindow &&
509 fgStructure.CurrentWindow->IsMenu )
511 fgSetWindow( window );
512 fgState.IdleCallback( fgState.IdleCallbackData );
515 fghSleepForEvents( );
520 * When this loop terminates, destroy the display, state and structure
521 * of a freeglut session, so that another glutInit() call can happen
523 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
525 action = fgState.ActionOnWindowClose;
527 if( action == GLUT_ACTION_EXIT )
532 * Leaves the freeglut processing loop.
534 void FGAPIENTRY glutLeaveMainLoop( void )
536 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
537 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
542 /*** END OF FILE ***/