timers internally now use 64bit unsigned int, if available
[freeglut] / src / Common / freeglut_main.c
index b1654a5..8aa02b2 100644 (file)
-/*\r
- * freeglut_main.c\r
- *\r
- * The windows message processing methods.\r
- *\r
- * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.\r
- * Written by Pawel W. Olszta, <olszta@sourceforge.net>\r
- * Creation date: Fri Dec 3 1999\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a\r
- * copy of this software and associated documentation files (the "Software"),\r
- * to deal in the Software without restriction, including without limitation\r
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
- * and/or sell copies of the Software, and to permit persons to whom the\r
- * Software is furnished to do so, subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
- * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- */\r
-\r
-#include <GL/freeglut.h>\r
-#include "freeglut_internal.h"\r
-#ifdef HAVE_ERRNO_H\r
-#    include <errno.h>\r
-#endif\r
-#include <stdarg.h>\r
-#ifdef  HAVE_VFPRINTF\r
-#    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))\r
-#elif defined(HAVE__DOPRNT)\r
-#    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))\r
-#else\r
-#    define VFPRINTF(s,f,a)\r
-#endif\r
-\r
-#ifdef _WIN32_WCE\r
-\r
-typedef struct GXDisplayProperties GXDisplayProperties;\r
-typedef struct GXKeyList GXKeyList;\r
-#include <gx.h>\r
-\r
-typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);\r
-typedef int (*GXOPENINPUT)();\r
-\r
-GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;\r
-GXOPENINPUT GXOpenInput_ = NULL;\r
-\r
-struct GXKeyList gxKeyList;\r
-\r
-#endif /* _WIN32_WCE */\r
-\r
-/*\r
- * Try to get the maximum value allowed for ints, falling back to the minimum\r
- * guaranteed by ISO C99 if there is no suitable header.\r
- */\r
-#ifdef HAVE_LIMITS_H\r
-#    include <limits.h>\r
-#endif\r
-#ifndef INT_MAX\r
-#    define INT_MAX 32767\r
-#endif\r
-\r
-#ifndef MIN\r
-#    define MIN(a,b) (((a)<(b)) ? (a) : (b))\r
-#endif\r
-\r
-extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );\r
-extern void fgPlatformDisplayWindow ( SFG_Window *window );\r
-extern void fgPlatformSleepForEvents( long msec );\r
-extern void fgPlatformProcessSingleEvent ( void );\r
-extern void fgPlatformMainLoopPreliminaryWork ( void );\r
-\r
-\r
-/*\r
- * TODO BEFORE THE STABLE RELEASE:\r
- *\r
- * There are some issues concerning window redrawing under X11, and maybe\r
- * some events are not handled. The Win32 version lacks some more features,\r
- * but seems acceptable for not demanding purposes.\r
- *\r
- * Need to investigate why the X11 version breaks out with an error when\r
- * closing a window (using the window manager, not glutDestroyWindow)...\r
- */\r
-\r
-/* -- PRIVATE FUNCTIONS ---------------------------------------------------- */\r
-\r
-/*\r
- * Handle a window configuration change. When no reshape\r
- * callback is hooked, the viewport size is updated to\r
- * match the new window size.\r
- */\r
-#if TARGET_HOST_POSIX_X11\r
-static void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )\r
-{\r
-    XResizeWindow( fgDisplay.Display, window->Window.Handle,\r
-                   width, height );\r
-    XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */\r
-}\r
-#endif\r
-\r
-static void fghReshapeWindow ( SFG_Window *window, int width, int height )\r
-{\r
-    SFG_Window *current_window = fgStructure.CurrentWindow;\r
-\r
-    freeglut_return_if_fail( window != NULL );\r
-\r
-       fgPlatformReshapeWindow ( window, width, height );\r
-\r
-    if( FETCH_WCB( *window, Reshape ) )\r
-        INVOKE_WCB( *window, Reshape, ( width, height ) );\r
-    else\r
-    {\r
-        fgSetWindow( window );\r
-        glViewport( 0, 0, width, height );\r
-    }\r
-\r
-    /*\r
-     * Force a window redraw.  In Windows at least this is only a partial\r
-     * solution:  if the window is increasing in size in either dimension,\r
-     * the already-drawn part does not get drawn again and things look funny.\r
-     * But without this we get this bad behaviour whenever we resize the\r
-     * window.\r
-     */\r
-    window->State.Redisplay = GL_TRUE;\r
-\r
-    if( window->IsMenu )\r
-        fgSetWindow( current_window );\r
-}\r
-\r
-/*\r
- * Calls a window's redraw method. This is used when\r
- * a redraw is forced by the incoming window messages.\r
- */\r
-void fghRedrawWindow ( SFG_Window *window )\r
-{\r
-    SFG_Window *current_window = fgStructure.CurrentWindow;\r
-\r
-    freeglut_return_if_fail( window );\r
-    freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );\r
-\r
-    window->State.Redisplay = GL_FALSE;\r
-\r
-    freeglut_return_if_fail( window->State.Visible );\r
-\r
-    fgSetWindow( window );\r
-\r
-    if( window->State.NeedToResize )\r
-    {\r
-        fghReshapeWindow(\r
-            window,\r
-            window->State.Width,\r
-            window->State.Height\r
-        );\r
-\r
-        window->State.NeedToResize = GL_FALSE;\r
-    }\r
-\r
-    INVOKE_WCB( *window, Display, ( ) );\r
-\r
-    fgSetWindow( current_window );\r
-}\r
-\r
-/*\r
- * A static helper function to execute display callback for a window\r
- */\r
-#if TARGET_HOST_POSIX_X11\r
-static void fgPlatformDisplayWindow ( SFG_Window *window )\r
-{\r
-        fghRedrawWindow ( window ) ;\r
-}\r
-#endif\r
-\r
-static void fghcbDisplayWindow( SFG_Window *window,\r
-                                SFG_Enumerator *enumerator )\r
-{\r
-    if( window->State.Redisplay &&\r
-        window->State.Visible )\r
-    {\r
-        window->State.Redisplay = GL_FALSE;\r
-               fgPlatformDisplayWindow ( window );\r
-    }\r
-\r
-    fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );\r
-}\r
-\r
-/*\r
- * Make all windows perform a display call\r
- */\r
-static void fghDisplayAll( void )\r
-{\r
-    SFG_Enumerator enumerator;\r
-\r
-    enumerator.found = GL_FALSE;\r
-    enumerator.data  =  NULL;\r
-\r
-    fgEnumWindows( fghcbDisplayWindow, &enumerator );\r
-}\r
-\r
-/*\r
- * Window enumerator callback to check for the joystick polling code\r
- */\r
-static void fghcbCheckJoystickPolls( SFG_Window *window,\r
-                                     SFG_Enumerator *enumerator )\r
-{\r
-    long int checkTime = fgElapsedTime( );\r
-\r
-    if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=\r
-        checkTime )\r
-    {\r
-#if !defined(_WIN32_WCE)\r
-        fgJoystickPollWindow( window );\r
-#endif /* !defined(_WIN32_WCE) */\r
-        window->State.JoystickLastPoll = checkTime;\r
-    }\r
-\r
-    fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );\r
-}\r
-\r
-/*\r
- * Check all windows for joystick polling\r
- */\r
-static void fghCheckJoystickPolls( void )\r
-{\r
-    SFG_Enumerator enumerator;\r
-\r
-    enumerator.found = GL_FALSE;\r
-    enumerator.data  =  NULL;\r
-\r
-    fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );\r
-}\r
-\r
-/*\r
- * Check the global timers\r
- */\r
-static void fghCheckTimers( void )\r
-{\r
-    long checkTime = fgElapsedTime( );\r
-\r
-    while( fgState.Timers.First )\r
-    {\r
-        SFG_Timer *timer = fgState.Timers.First;\r
-\r
-        if( timer->TriggerTime > checkTime )\r
-            break;\r
-\r
-        fgListRemove( &fgState.Timers, &timer->Node );\r
-        fgListAppend( &fgState.FreeTimers, &timer->Node );\r
-\r
-        timer->Callback( timer->ID );\r
-    }\r
-}\r
-\r
\r
-/* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.\r
- * This value wraps every 49.7 days, but integer overflows cancel\r
- * when subtracting an initial start time, unless the total time exceeds\r
- * 32-bit, where the GLUT API return value is also overflowed.\r
- */  \r
-unsigned long fgSystemTime(void) {\r
-#if TARGET_HOST_SOLARIS || HAVE_GETTIMEOFDAY\r
-    struct timeval now;\r
-    gettimeofday( &now, NULL );\r
-    return now.tv_usec/1000 + now.tv_sec*1000;\r
-#elif TARGET_HOST_MS_WINDOWS\r
-#    if defined(_WIN32_WCE)\r
-    return GetTickCount();\r
-#    else\r
-    return timeGetTime();\r
-#    endif\r
-#endif\r
-}\r
-  \r
-/*\r
- * Elapsed Time\r
- */\r
-long fgElapsedTime( void )\r
-{\r
-    return (long) (fgSystemTime() - fgState.Time);\r
-}\r
-\r
-/*\r
- * Error Messages.\r
- */\r
-void fgError( const char *fmt, ... )\r
-{\r
-    va_list ap;\r
-\r
-    if (fgState.ErrorFunc) {\r
-\r
-        va_start( ap, fmt );\r
-\r
-        /* call user set error handler here */\r
-        fgState.ErrorFunc(fmt, ap);\r
-\r
-        va_end( ap );\r
-\r
-    } else {\r
-\r
-        va_start( ap, fmt );\r
-\r
-        fprintf( stderr, "freeglut ");\r
-        if( fgState.ProgramName )\r
-            fprintf( stderr, "(%s): ", fgState.ProgramName );\r
-        VFPRINTF( stderr, fmt, ap );\r
-        fprintf( stderr, "\n" );\r
-\r
-        va_end( ap );\r
-\r
-        if ( fgState.Initialised )\r
-            fgDeinitialize ();\r
-\r
-        exit( 1 );\r
-    }\r
-}\r
-\r
-void fgWarning( const char *fmt, ... )\r
-{\r
-    va_list ap;\r
-\r
-    if (fgState.WarningFunc) {\r
-\r
-        va_start( ap, fmt );\r
-\r
-        /* call user set warning handler here */\r
-        fgState.WarningFunc(fmt, ap);\r
-\r
-        va_end( ap );\r
-\r
-    } else {\r
-\r
-        va_start( ap, fmt );\r
-\r
-        fprintf( stderr, "freeglut ");\r
-        if( fgState.ProgramName )\r
-            fprintf( stderr, "(%s): ", fgState.ProgramName );\r
-        VFPRINTF( stderr, fmt, ap );\r
-        fprintf( stderr, "\n" );\r
-\r
-        va_end( ap );\r
-    }\r
-}\r
-\r
-\r
-/*\r
- * Indicates whether Joystick events are being used by ANY window.\r
- *\r
- * The current mechanism is to walk all of the windows and ask if\r
- * there is a joystick callback.  We have a short-circuit early\r
- * return if we find any joystick handler registered.\r
- *\r
- * The real way to do this is to make use of the glutTimer() API\r
- * to more cleanly re-implement the joystick API.  Then, this code\r
- * and all other "joystick timer" code can be yanked.\r
- *\r
- */\r
-static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)\r
-{\r
-    if( FETCH_WCB( *w, Joystick ) )\r
-    {\r
-        e->found = GL_TRUE;\r
-        e->data = w;\r
-    }\r
-    fgEnumSubWindows( w, fghCheckJoystickCallback, e );\r
-}\r
-static int fghHaveJoystick( void )\r
-{\r
-    SFG_Enumerator enumerator;\r
-\r
-    enumerator.found = GL_FALSE;\r
-    enumerator.data = NULL;\r
-    fgEnumWindows( fghCheckJoystickCallback, &enumerator );\r
-    return !!enumerator.data;\r
-}\r
-static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)\r
-{\r
-    if( w->State.Redisplay && w->State.Visible )\r
-    {\r
-        e->found = GL_TRUE;\r
-        e->data = w;\r
-    }\r
-    fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );\r
-}\r
-static int fghHavePendingRedisplays (void)\r
-{\r
-    SFG_Enumerator enumerator;\r
-\r
-    enumerator.found = GL_FALSE;\r
-    enumerator.data = NULL;\r
-    fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );\r
-    return !!enumerator.data;\r
-}\r
-/*\r
- * Returns the number of GLUT ticks (milliseconds) till the next timer event.\r
- */\r
-static long fghNextTimer( void )\r
-{\r
-    long ret = INT_MAX;\r
-    SFG_Timer *timer = fgState.Timers.First;\r
-\r
-    if( timer )\r
-        ret = timer->TriggerTime - fgElapsedTime();\r
-    if( ret < 0 )\r
-        ret = 0;\r
-\r
-    return ret;\r
-}\r
-/*\r
- * Does the magic required to relinquish the CPU until something interesting\r
- * happens.\r
- */\r
-\r
-#if TARGET_HOST_POSIX_X11\r
-static void fgPlatformSleepForEvents( long msec )\r
-{\r
-    /*\r
-     * Possibly due to aggressive use of XFlush() and friends,\r
-     * it is possible to have our socket drained but still have\r
-     * unprocessed events.  (Or, this may just be normal with\r
-     * X, anyway?)  We do non-trivial processing of X events\r
-     * after the event-reading loop, in any case, so we\r
-     * need to allow that we may have an empty socket but non-\r
-     * empty event queue.\r
-     */\r
-    if( ! XPending( fgDisplay.Display ) )\r
-    {\r
-        fd_set fdset;\r
-        int err;\r
-        int socket;\r
-        struct timeval wait;\r
-\r
-        socket = ConnectionNumber( fgDisplay.Display );\r
-        FD_ZERO( &fdset );\r
-        FD_SET( socket, &fdset );\r
-        wait.tv_sec = msec / 1000;\r
-        wait.tv_usec = (msec % 1000) * 1000;\r
-        err = select( socket+1, &fdset, NULL, NULL, &wait );\r
-\r
-#ifdef HAVE_ERRNO_H\r
-        if( ( -1 == err ) && ( errno != EINTR ) )\r
-            fgWarning ( "freeglut select() error: %d", errno );\r
-#endif\r
-    }\r
-}\r
-#endif\r
-\r
-static void fghSleepForEvents( void )\r
-{\r
-    long msec;\r
-\r
-    if( fgState.IdleCallback || fghHavePendingRedisplays( ) )\r
-        return;\r
-\r
-    msec = fghNextTimer( );\r
-    /* XXX Use GLUT timers for joysticks... */\r
-    /* XXX Dumb; forces granularity to .01sec */\r
-    if( fghHaveJoystick( ) && ( msec > 10 ) )     \r
-        msec = 10;\r
-\r
-       fgPlatformSleepForEvents ( msec );\r
-}\r
-\r
-#if TARGET_HOST_POSIX_X11\r
-/*\r
- * Returns GLUT modifier mask for the state field of an X11 event.\r
- */\r
-int fgPlatformGetModifiers( int state )\r
-{\r
-    int ret = 0;\r
-\r
-    if( state & ( ShiftMask | LockMask ) )\r
-        ret |= GLUT_ACTIVE_SHIFT;\r
-    if( state & ControlMask )\r
-        ret |= GLUT_ACTIVE_CTRL;\r
-    if( state & Mod1Mask )\r
-        ret |= GLUT_ACTIVE_ALT;\r
-\r
-    return ret;\r
-}\r
-\r
-\r
-\r
-static const char* fghTypeToString( int type )\r
-{\r
-    switch( type ) {\r
-    case KeyPress: return "KeyPress";\r
-    case KeyRelease: return "KeyRelease";\r
-    case ButtonPress: return "ButtonPress";\r
-    case ButtonRelease: return "ButtonRelease";\r
-    case MotionNotify: return "MotionNotify";\r
-    case EnterNotify: return "EnterNotify";\r
-    case LeaveNotify: return "LeaveNotify";\r
-    case FocusIn: return "FocusIn";\r
-    case FocusOut: return "FocusOut";\r
-    case KeymapNotify: return "KeymapNotify";\r
-    case Expose: return "Expose";\r
-    case GraphicsExpose: return "GraphicsExpose";\r
-    case NoExpose: return "NoExpose";\r
-    case VisibilityNotify: return "VisibilityNotify";\r
-    case CreateNotify: return "CreateNotify";\r
-    case DestroyNotify: return "DestroyNotify";\r
-    case UnmapNotify: return "UnmapNotify";\r
-    case MapNotify: return "MapNotify";\r
-    case MapRequest: return "MapRequest";\r
-    case ReparentNotify: return "ReparentNotify";\r
-    case ConfigureNotify: return "ConfigureNotify";\r
-    case ConfigureRequest: return "ConfigureRequest";\r
-    case GravityNotify: return "GravityNotify";\r
-    case ResizeRequest: return "ResizeRequest";\r
-    case CirculateNotify: return "CirculateNotify";\r
-    case CirculateRequest: return "CirculateRequest";\r
-    case PropertyNotify: return "PropertyNotify";\r
-    case SelectionClear: return "SelectionClear";\r
-    case SelectionRequest: return "SelectionRequest";\r
-    case SelectionNotify: return "SelectionNotify";\r
-    case ColormapNotify: return "ColormapNotify";\r
-    case ClientMessage: return "ClientMessage";\r
-    case MappingNotify: return "MappingNotify";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghBoolToString( Bool b )\r
-{\r
-    return b == False ? "False" : "True";\r
-}\r
-\r
-static const char* fghNotifyHintToString( char is_hint )\r
-{\r
-    switch( is_hint ) {\r
-    case NotifyNormal: return "NotifyNormal";\r
-    case NotifyHint: return "NotifyHint";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghNotifyModeToString( int mode )\r
-{\r
-    switch( mode ) {\r
-    case NotifyNormal: return "NotifyNormal";\r
-    case NotifyGrab: return "NotifyGrab";\r
-    case NotifyUngrab: return "NotifyUngrab";\r
-    case NotifyWhileGrabbed: return "NotifyWhileGrabbed";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghNotifyDetailToString( int detail )\r
-{\r
-    switch( detail ) {\r
-    case NotifyAncestor: return "NotifyAncestor";\r
-    case NotifyVirtual: return "NotifyVirtual";\r
-    case NotifyInferior: return "NotifyInferior";\r
-    case NotifyNonlinear: return "NotifyNonlinear";\r
-    case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";\r
-    case NotifyPointer: return "NotifyPointer";\r
-    case NotifyPointerRoot: return "NotifyPointerRoot";\r
-    case NotifyDetailNone: return "NotifyDetailNone";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghVisibilityToString( int state ) {\r
-    switch( state ) {\r
-    case VisibilityUnobscured: return "VisibilityUnobscured";\r
-    case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";\r
-    case VisibilityFullyObscured: return "VisibilityFullyObscured";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghConfigureDetailToString( int detail )\r
-{\r
-    switch( detail ) {\r
-    case Above: return "Above";\r
-    case Below: return "Below";\r
-    case TopIf: return "TopIf";\r
-    case BottomIf: return "BottomIf";\r
-    case Opposite: return "Opposite";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghPlaceToString( int place )\r
-{\r
-    switch( place ) {\r
-    case PlaceOnTop: return "PlaceOnTop";\r
-    case PlaceOnBottom: return "PlaceOnBottom";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghMappingRequestToString( int request )\r
-{\r
-    switch( request ) {\r
-    case MappingModifier: return "MappingModifier";\r
-    case MappingKeyboard: return "MappingKeyboard";\r
-    case MappingPointer: return "MappingPointer";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghPropertyStateToString( int state )\r
-{\r
-    switch( state ) {\r
-    case PropertyNewValue: return "PropertyNewValue";\r
-    case PropertyDelete: return "PropertyDelete";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static const char* fghColormapStateToString( int state )\r
-{\r
-    switch( state ) {\r
-    case ColormapUninstalled: return "ColormapUninstalled";\r
-    case ColormapInstalled: return "ColormapInstalled";\r
-    default: return "UNKNOWN";\r
-    }\r
-}\r
-\r
-static void fghPrintEvent( XEvent *event )\r
-{\r
-    switch( event->type ) {\r
-\r
-    case KeyPress:\r
-    case KeyRelease: {\r
-        XKeyEvent *e = &event->xkey;\r
-        fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
-                   "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
-                   "keycode=%u, same_screen=%s", fghTypeToString( e->type ),\r
-                   e->window, e->root, e->subwindow, (unsigned long)e->time,\r
-                   e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,\r
-                   fghBoolToString( e->same_screen ) );\r
-        break;\r
-    }\r
-\r
-    case ButtonPress:\r
-    case ButtonRelease: {\r
-        XButtonEvent *e = &event->xbutton;\r
-        fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
-                   "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
-                   "button=%u, same_screen=%d", fghTypeToString( e->type ),\r
-                   e->window, e->root, e->subwindow, (unsigned long)e->time,\r
-                   e->x, e->y, e->x_root, e->y_root, e->state, e->button,\r
-                   fghBoolToString( e->same_screen ) );\r
-        break;\r
-    }\r
-\r
-    case MotionNotify: {\r
-        XMotionEvent *e = &event->xmotion;\r
-        fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
-                   "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
-                   "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),\r
-                   e->window, e->root, e->subwindow, (unsigned long)e->time,\r
-                   e->x, e->y, e->x_root, e->y_root, e->state,\r
-                   fghNotifyHintToString( e->is_hint ),\r
-                   fghBoolToString( e->same_screen ) );\r
-        break;\r
-    }\r
-\r
-    case EnterNotify:\r
-    case LeaveNotify: {\r
-        XCrossingEvent *e = &event->xcrossing;\r
-        fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
-                   "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "\r
-                   "focus=%d, state=0x%x", fghTypeToString( e->type ),\r
-                   e->window, e->root, e->subwindow, (unsigned long)e->time,\r
-                   e->x, e->y, fghNotifyModeToString( e->mode ),\r
-                   fghNotifyDetailToString( e->detail ), (int)e->same_screen,\r
-                   (int)e->focus, e->state );\r
-        break;\r
-    }\r
-\r
-    case FocusIn:\r
-    case FocusOut: {\r
-        XFocusChangeEvent *e = &event->xfocus;\r
-        fgWarning( "%s: window=0x%x, mode=%s, detail=%s",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   fghNotifyModeToString( e->mode ),\r
-                   fghNotifyDetailToString( e->detail ) );\r
-        break;\r
-    }\r
-\r
-    case KeymapNotify: {\r
-        XKeymapEvent *e = &event->xkeymap;\r
-        char buf[32 * 2 + 1];\r
-        int i;\r
-        for ( i = 0; i < 32; i++ ) {\r
-            snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,\r
-                      "%02x", e->key_vector[ i ] );\r
-        }\r
-        buf[ i ] = '\0';\r
-        fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,\r
-                   buf );\r
-        break;\r
-    }\r
-\r
-    case Expose: {\r
-        XExposeEvent *e = &event->xexpose;\r
-        fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
-                   "count=%d", fghTypeToString( e->type ), e->window, e->x,\r
-                   e->y, e->width, e->height, e->count );\r
-        break;\r
-    }\r
-\r
-    case GraphicsExpose: {\r
-        XGraphicsExposeEvent *e = &event->xgraphicsexpose;\r
-        fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
-                   "count=%d, (major_code,minor_code)=(%d,%d)",\r
-                   fghTypeToString( e->type ), e->drawable, e->x, e->y,\r
-                   e->width, e->height, e->count, e->major_code,\r
-                   e->minor_code );\r
-        break;\r
-    }\r
-\r
-    case NoExpose: {\r
-        XNoExposeEvent *e = &event->xnoexpose;\r
-        fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",\r
-                   fghTypeToString( e->type ), e->drawable, e->major_code,\r
-                   e->minor_code );\r
-        break;\r
-    }\r
-\r
-    case VisibilityNotify: {\r
-        XVisibilityEvent *e = &event->xvisibility;\r
-        fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),\r
-                   e->window, fghVisibilityToString( e->state) );\r
-        break;\r
-    }\r
-\r
-    case CreateNotify: {\r
-        XCreateWindowEvent *e = &event->xcreatewindow;\r
-        fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "\r
-                   "window=0x%x, override_redirect=%s",\r
-                   fghTypeToString( e->type ), e->x, e->y, e->width, e->height,\r
-                   e->border_width, e->window,\r
-                   fghBoolToString( e->override_redirect ) );\r
-        break;\r
-    }\r
-\r
-    case DestroyNotify: {\r
-        XDestroyWindowEvent *e = &event->xdestroywindow;\r
-        fgWarning( "%s: event=0x%x, window=0x%x",\r
-                   fghTypeToString( e->type ), e->event, e->window );\r
-        break;\r
-    }\r
-\r
-    case UnmapNotify: {\r
-        XUnmapEvent *e = &event->xunmap;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",\r
-                   fghTypeToString( e->type ), e->event, e->window,\r
-                   fghBoolToString( e->from_configure ) );\r
-        break;\r
-    }\r
-\r
-    case MapNotify: {\r
-        XMapEvent *e = &event->xmap;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",\r
-                   fghTypeToString( e->type ), e->event, e->window,\r
-                   fghBoolToString( e->override_redirect ) );\r
-        break;\r
-    }\r
-\r
-    case MapRequest: {\r
-        XMapRequestEvent *e = &event->xmaprequest;\r
-        fgWarning( "%s: parent=0x%x, window=0x%x",\r
-                   fghTypeToString( event->type ), e->parent, e->window );\r
-        break;\r
-    }\r
-\r
-    case ReparentNotify: {\r
-        XReparentEvent *e = &event->xreparent;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "\r
-                   "override_redirect=%s", fghTypeToString( e->type ),\r
-                   e->event, e->window, e->parent, e->x, e->y,\r
-                   fghBoolToString( e->override_redirect ) );\r
-        break;\r
-    }\r
-\r
-    case ConfigureNotify: {\r
-        XConfigureEvent *e = &event->xconfigure;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
-                   "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
-                   "override_redirect=%s", fghTypeToString( e->type ), e->event,\r
-                   e->window, e->x, e->y, e->width, e->height, e->border_width,\r
-                   e->above, fghBoolToString( e->override_redirect ) );\r
-        break;\r
-    }\r
-\r
-    case ConfigureRequest: {\r
-        XConfigureRequestEvent *e = &event->xconfigurerequest;\r
-        fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
-                   "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
-                   "detail=%s, value_mask=%lx", fghTypeToString( e->type ),\r
-                   e->parent, e->window, e->x, e->y, e->width, e->height,\r
-                   e->border_width, e->above,\r
-                   fghConfigureDetailToString( e->detail ), e->value_mask );\r
-        break;\r
-    }\r
-\r
-    case GravityNotify: {\r
-        XGravityEvent *e = &event->xgravity;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",\r
-                   fghTypeToString( e->type ), e->event, e->window, e->x, e->y );\r
-        break;\r
-    }\r
-\r
-    case ResizeRequest: {\r
-        XResizeRequestEvent *e = &event->xresizerequest;\r
-        fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",\r
-                   fghTypeToString( e->type ), e->window, e->width, e->height );\r
-        break;\r
-    }\r
-\r
-    case CirculateNotify: {\r
-        XCirculateEvent *e = &event->xcirculate;\r
-        fgWarning( "%s: event=0x%x, window=0x%x, place=%s",\r
-                   fghTypeToString( e->type ), e->event, e->window,\r
-                   fghPlaceToString( e->place ) );\r
-        break;\r
-    }\r
-\r
-    case CirculateRequest: {\r
-        XCirculateRequestEvent *e = &event->xcirculaterequest;\r
-        fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",\r
-                   fghTypeToString( e->type ), e->parent, e->window,\r
-                   fghPlaceToString( e->place ) );\r
-        break;\r
-    }\r
-\r
-    case PropertyNotify: {\r
-        XPropertyEvent *e = &event->xproperty;\r
-        fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   (unsigned long)e->atom, (unsigned long)e->time,\r
-                   fghPropertyStateToString( e->state ) );\r
-        break;\r
-    }\r
-\r
-    case SelectionClear: {\r
-        XSelectionClearEvent *e = &event->xselectionclear;\r
-        fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   (unsigned long)e->selection, (unsigned long)e->time );\r
-        break;\r
-    }\r
-\r
-    case SelectionRequest: {\r
-        XSelectionRequestEvent *e = &event->xselectionrequest;\r
-        fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "\r
-                   "target=0x%x, property=%lu, time=%lu",\r
-                   fghTypeToString( e->type ), e->owner, e->requestor,\r
-                   (unsigned long)e->selection, (unsigned long)e->target,\r
-                   (unsigned long)e->property, (unsigned long)e->time );\r
-        break;\r
-    }\r
-\r
-    case SelectionNotify: {\r
-        XSelectionEvent *e = &event->xselection;\r
-        fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "\r
-                   "property=%lu, time=%lu", fghTypeToString( e->type ),\r
-                   e->requestor, (unsigned long)e->selection,\r
-                   (unsigned long)e->target, (unsigned long)e->property,\r
-                   (unsigned long)e->time );\r
-        break;\r
-    }\r
-\r
-    case ColormapNotify: {\r
-        XColormapEvent *e = &event->xcolormap;\r
-        fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   (unsigned long)e->colormap, fghBoolToString( e->new ),\r
-                   fghColormapStateToString( e->state ) );\r
-        break;\r
-    }\r
-\r
-    case ClientMessage: {\r
-        XClientMessageEvent *e = &event->xclient;\r
-        char buf[ 61 ];\r
-        char* p = buf;\r
-        char* end = buf + sizeof( buf );\r
-        int i;\r
-        switch( e->format ) {\r
-        case 8:\r
-          for ( i = 0; i < 20; i++, p += 3 ) {\r
-                snprintf( p, end - p, " %02x", e->data.b[ i ] );\r
-            }\r
-            break;\r
-        case 16:\r
-            for ( i = 0; i < 10; i++, p += 5 ) {\r
-                snprintf( p, end - p, " %04x", e->data.s[ i ] );\r
-            }\r
-            break;\r
-        case 32:\r
-            for ( i = 0; i < 5; i++, p += 9 ) {\r
-                snprintf( p, end - p, " %08lx", e->data.l[ i ] );\r
-            }\r
-            break;\r
-        }\r
-        *p = '\0';\r
-        fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   (unsigned long)e->message_type, e->format, buf );\r
-        break;\r
-    }\r
-\r
-    case MappingNotify: {\r
-        XMappingEvent *e = &event->xmapping;\r
-        fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",\r
-                   fghTypeToString( e->type ), e->window,\r
-                   fghMappingRequestToString( e->request ), e->first_keycode,\r
-                   e->count );\r
-        break;\r
-    }\r
-\r
-    default: {\r
-        fgWarning( "%s", fghTypeToString( event->type ) );\r
-        break;\r
-    }\r
-    }\r
-}\r
-\r
-\r
-void fgPlatformProcessSingleEvent ( void )\r
-{\r
-    SFG_Window* window;\r
-    XEvent event;\r
-\r
-    /* This code was repeated constantly, so here it goes into a definition: */\r
-#define GETWINDOW(a)                             \\r
-    window = fgWindowByHandle( event.a.window ); \\r
-    if( window == NULL )                         \\r
-        break;\r
-\r
-#define GETMOUSE(a)                              \\r
-    window->State.MouseX = event.a.x;            \\r
-    window->State.MouseY = event.a.y;\r
-\r
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );\r
-\r
-    while( XPending( fgDisplay.Display ) )\r
-    {\r
-        XNextEvent( fgDisplay.Display, &event );\r
-#if _DEBUG\r
-        fghPrintEvent( &event );\r
-#endif\r
-\r
-        switch( event.type )\r
-        {\r
-        case ClientMessage:\r
-            if(fgIsSpaceballXEvent(&event)) {\r
-                fgSpaceballHandleXEvent(&event);\r
-                break;\r
-            }\r
-            /* Destroy the window when the WM_DELETE_WINDOW message arrives */\r
-            if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )\r
-            {\r
-                GETWINDOW( xclient );\r
-\r
-                fgDestroyWindow ( window );\r
-\r
-                if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )\r
-                {\r
-                    fgDeinitialize( );\r
-                    exit( 0 );\r
-                }\r
-                else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )\r
-                    fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
-\r
-                return;\r
-            }\r
-            break;\r
-\r
-            /*\r
-             * CreateNotify causes a configure-event so that sub-windows are\r
-             * handled compatibly with GLUT.  Otherwise, your sub-windows\r
-             * (in freeglut only) will not get an initial reshape event,\r
-             * which can break things.\r
-             *\r
-             * GLUT presumably does this because it generally tries to treat\r
-             * sub-windows the same as windows.\r
-             */\r
-        case CreateNotify:\r
-        case ConfigureNotify:\r
-            {\r
-                int width, height;\r
-                if( event.type == CreateNotify ) {\r
-                    GETWINDOW( xcreatewindow );\r
-                    width = event.xcreatewindow.width;\r
-                    height = event.xcreatewindow.height;\r
-                } else {\r
-                    GETWINDOW( xconfigure );\r
-                    width = event.xconfigure.width;\r
-                    height = event.xconfigure.height;\r
-                }\r
-\r
-                if( ( width != window->State.OldWidth ) ||\r
-                    ( height != window->State.OldHeight ) )\r
-                {\r
-                    SFG_Window *current_window = fgStructure.CurrentWindow;\r
-\r
-                    window->State.OldWidth = width;\r
-                    window->State.OldHeight = height;\r
-                    if( FETCH_WCB( *window, Reshape ) )\r
-                        INVOKE_WCB( *window, Reshape, ( width, height ) );\r
-                    else\r
-                    {\r
-                        fgSetWindow( window );\r
-                        glViewport( 0, 0, width, height );\r
-                    }\r
-                    glutPostRedisplay( );\r
-                    if( window->IsMenu )\r
-                        fgSetWindow( current_window );\r
-                }\r
-            }\r
-            break;\r
-\r
-        case DestroyNotify:\r
-            /*\r
-             * This is sent to confirm the XDestroyWindow call.\r
-             *\r
-             * XXX WHY is this commented out?  Should we re-enable it?\r
-             */\r
-            /* fgAddToWindowDestroyList ( window ); */\r
-            break;\r
-\r
-        case Expose:\r
-            /*\r
-             * We are too dumb to process partial exposes...\r
-             *\r
-             * XXX Well, we could do it.  However, it seems to only\r
-             * XXX be potentially useful for single-buffered (since\r
-             * XXX double-buffered does not respect viewport when we\r
-             * XXX do a buffer-swap).\r
-             *\r
-             */\r
-            if( event.xexpose.count == 0 )\r
-            {\r
-                GETWINDOW( xexpose );\r
-                window->State.Redisplay = GL_TRUE;\r
-            }\r
-            break;\r
-\r
-        case MapNotify:\r
-            break;\r
-\r
-        case UnmapNotify:\r
-            /* We get this when iconifying a window. */ \r
-            GETWINDOW( xunmap );\r
-            INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );\r
-            window->State.Visible = GL_FALSE;\r
-            break;\r
-\r
-        case MappingNotify:\r
-            /*\r
-             * Have the client's keyboard knowledge updated (xlib.ps,\r
-             * page 206, says that's a good thing to do)\r
-             */\r
-            XRefreshKeyboardMapping( (XMappingEvent *) &event );\r
-            break;\r
-\r
-        case VisibilityNotify:\r
-        {\r
-            /*\r
-             * Sending this event, the X server can notify us that the window\r
-             * has just acquired one of the three possible visibility states:\r
-             * VisibilityUnobscured, VisibilityPartiallyObscured or\r
-             * VisibilityFullyObscured. Note that we DO NOT receive a\r
-             * VisibilityNotify event when iconifying a window, we only get an\r
-             * UnmapNotify then.\r
-             */\r
-            GETWINDOW( xvisibility );\r
-            switch( event.xvisibility.state )\r
-            {\r
-            case VisibilityUnobscured:\r
-                INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );\r
-                window->State.Visible = GL_TRUE;\r
-                break;\r
-\r
-            case VisibilityPartiallyObscured:\r
-                INVOKE_WCB( *window, WindowStatus,\r
-                            ( GLUT_PARTIALLY_RETAINED ) );\r
-                window->State.Visible = GL_TRUE;\r
-                break;\r
-\r
-            case VisibilityFullyObscured:\r
-                INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );\r
-                window->State.Visible = GL_FALSE;\r
-                break;\r
-\r
-            default:\r
-                fgWarning( "Unknown X visibility state: %d",\r
-                           event.xvisibility.state );\r
-                break;\r
-            }\r
-        }\r
-        break;\r
-\r
-        case EnterNotify:\r
-        case LeaveNotify:\r
-            GETWINDOW( xcrossing );\r
-            GETMOUSE( xcrossing );\r
-            if( ( event.type == LeaveNotify ) && window->IsMenu &&\r
-                window->ActiveMenu && window->ActiveMenu->IsActive )\r
-                fgUpdateMenuHighlight( window->ActiveMenu );\r
-\r
-            INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?\r
-                                          GLUT_ENTERED :\r
-                                          GLUT_LEFT ) );\r
-            break;\r
-\r
-        case MotionNotify:\r
-        {\r
-            GETWINDOW( xmotion );\r
-            GETMOUSE( xmotion );\r
-\r
-            if( window->ActiveMenu )\r
-            {\r
-                if( window == window->ActiveMenu->ParentWindow )\r
-                {\r
-                    window->ActiveMenu->Window->State.MouseX =\r
-                        event.xmotion.x_root - window->ActiveMenu->X;\r
-                    window->ActiveMenu->Window->State.MouseY =\r
-                        event.xmotion.y_root - window->ActiveMenu->Y;\r
-                }\r
-\r
-                fgUpdateMenuHighlight( window->ActiveMenu );\r
-\r
-                break;\r
-            }\r
-\r
-            /*\r
-             * XXX For more than 5 buttons, just check {event.xmotion.state},\r
-             * XXX rather than a host of bit-masks?  Or maybe we need to\r
-             * XXX track ButtonPress/ButtonRelease events in our own\r
-             * XXX bit-mask?\r
-             */\r
-            fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state );\r
-            if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {\r
-                INVOKE_WCB( *window, Motion, ( event.xmotion.x,\r
-                                               event.xmotion.y ) );\r
-            } else {\r
-                INVOKE_WCB( *window, Passive, ( event.xmotion.x,\r
-                                                event.xmotion.y ) );\r
-            }\r
-            fgState.Modifiers = INVALID_MODIFIERS;\r
-        }\r
-        break;\r
-\r
-        case ButtonRelease:\r
-        case ButtonPress:\r
-        {\r
-            GLboolean pressed = GL_TRUE;\r
-            int button;\r
-\r
-            if( event.type == ButtonRelease )\r
-                pressed = GL_FALSE ;\r
-\r
-            /*\r
-             * A mouse button has been pressed or released. Traditionally,\r
-             * break if the window was found within the freeglut structures.\r
-             */\r
-            GETWINDOW( xbutton );\r
-            GETMOUSE( xbutton );\r
-\r
-            /*\r
-             * An X button (at least in XFree86) is numbered from 1.\r
-             * A GLUT button is numbered from 0.\r
-             * Old GLUT passed through buttons other than just the first\r
-             * three, though it only gave symbolic names and official\r
-             * support to the first three.\r
-             */\r
-            button = event.xbutton.button - 1;\r
-\r
-            /*\r
-             * Do not execute the application's mouse callback if a menu\r
-             * is hooked to this button.  In that case an appropriate\r
-             * private call should be generated.\r
-             */\r
-            if( fgCheckActiveMenu( window, button, pressed,\r
-                                   event.xbutton.x_root, event.xbutton.y_root ) )\r
-                break;\r
-\r
-            /*\r
-             * Check if there is a mouse or mouse wheel callback hooked to the\r
-             * window\r
-             */\r
-            if( ! FETCH_WCB( *window, Mouse ) &&\r
-                ! FETCH_WCB( *window, MouseWheel ) )\r
-                break;\r
-\r
-            fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );\r
-\r
-            /* Finally execute the mouse or mouse wheel callback */\r
-            if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )\r
-                INVOKE_WCB( *window, Mouse, ( button,\r
-                                              pressed ? GLUT_DOWN : GLUT_UP,\r
-                                              event.xbutton.x,\r
-                                              event.xbutton.y )\r
-                );\r
-            else\r
-            {\r
-                /*\r
-                 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1\r
-                 *  "  6 and 7 "    "   one; ...\r
-                 *\r
-                 * XXX This *should* be behind some variables/macros,\r
-                 * XXX since the order and numbering isn't certain\r
-                 * XXX See XFree86 configuration docs (even back in the\r
-                 * XXX 3.x days, and especially with 4.x).\r
-                 *\r
-                 * XXX Note that {button} has already been decremented\r
-                 * XXX in mapping from X button numbering to GLUT.\r
-                                *\r
-                                * XXX Should add support for partial wheel turns as Windows does -- 5/27/11\r
-                 */\r
-                int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;\r
-                int direction = -1;\r
-                if( button % 2 )\r
-                    direction = 1;\r
-\r
-                if( pressed )\r
-                    INVOKE_WCB( *window, MouseWheel, ( wheel_number,\r
-                                                       direction,\r
-                                                       event.xbutton.x,\r
-                                                       event.xbutton.y )\r
-                    );\r
-            }\r
-            fgState.Modifiers = INVALID_MODIFIERS;\r
-        }\r
-        break;\r
-\r
-        case KeyRelease:\r
-        case KeyPress:\r
-        {\r
-            FGCBKeyboard keyboard_cb;\r
-            FGCBSpecial special_cb;\r
-\r
-            GETWINDOW( xkey );\r
-            GETMOUSE( xkey );\r
-\r
-            /* Detect auto repeated keys, if configured globally or per-window */\r
-\r
-            if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )\r
-            {\r
-                if (event.type==KeyRelease)\r
-                {\r
-                    /*\r
-                     * Look at X11 keystate to detect repeat mode.\r
-                     * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.\r
-                     */\r
-\r
-                    char keys[32];\r
-                    XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */\r
-\r
-                    if ( event.xkey.keycode<256 )            /* XQueryKeymap is limited to 256 keycodes    */\r
-                    {\r
-                        if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )\r
-                            window->State.KeyRepeating = GL_TRUE;\r
-                        else\r
-                            window->State.KeyRepeating = GL_FALSE;\r
-                    }\r
-                }\r
-            }\r
-            else\r
-                window->State.KeyRepeating = GL_FALSE;\r
-\r
-            /* Cease processing this event if it is auto repeated */\r
-\r
-            if (window->State.KeyRepeating)\r
-            {\r
-                if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;\r
-                break;\r
-            }\r
-\r
-            if( event.type == KeyPress )\r
-            {\r
-                keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));\r
-                special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, Special  ));\r
-            }\r
-            else\r
-            {\r
-                keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));\r
-                special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp  ));\r
-            }\r
-\r
-            /* Is there a keyboard/special callback hooked for this window? */\r
-            if( keyboard_cb || special_cb )\r
-            {\r
-                XComposeStatus composeStatus;\r
-                char asciiCode[ 32 ];\r
-                KeySym keySym;\r
-                int len;\r
-\r
-                /* Check for the ASCII/KeySym codes associated with the event: */\r
-                len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),\r
-                                     &keySym, &composeStatus\r
-                );\r
-\r
-                /* GLUT API tells us to have two separate callbacks... */\r
-                if( len > 0 )\r
-                {\r
-                    /* ...one for the ASCII translateable keypresses... */\r
-                    if( keyboard_cb )\r
-                    {\r
-                        fgSetWindow( window );\r
-                        fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );\r
-                        keyboard_cb( asciiCode[ 0 ],\r
-                                     event.xkey.x, event.xkey.y\r
-                        );\r
-                        fgState.Modifiers = INVALID_MODIFIERS;\r
-                    }\r
-                }\r
-                else\r
-                {\r
-                    int special = -1;\r
-\r
-                    /*\r
-                     * ...and one for all the others, which need to be\r
-                     * translated to GLUT_KEY_Xs...\r
-                     */\r
-                    switch( keySym )\r
-                    {\r
-                    case XK_F1:     special = GLUT_KEY_F1;     break;\r
-                    case XK_F2:     special = GLUT_KEY_F2;     break;\r
-                    case XK_F3:     special = GLUT_KEY_F3;     break;\r
-                    case XK_F4:     special = GLUT_KEY_F4;     break;\r
-                    case XK_F5:     special = GLUT_KEY_F5;     break;\r
-                    case XK_F6:     special = GLUT_KEY_F6;     break;\r
-                    case XK_F7:     special = GLUT_KEY_F7;     break;\r
-                    case XK_F8:     special = GLUT_KEY_F8;     break;\r
-                    case XK_F9:     special = GLUT_KEY_F9;     break;\r
-                    case XK_F10:    special = GLUT_KEY_F10;    break;\r
-                    case XK_F11:    special = GLUT_KEY_F11;    break;\r
-                    case XK_F12:    special = GLUT_KEY_F12;    break;\r
-\r
-                    case XK_KP_Left:\r
-                    case XK_Left:   special = GLUT_KEY_LEFT;   break;\r
-                    case XK_KP_Right:\r
-                    case XK_Right:  special = GLUT_KEY_RIGHT;  break;\r
-                    case XK_KP_Up:\r
-                    case XK_Up:     special = GLUT_KEY_UP;     break;\r
-                    case XK_KP_Down:\r
-                    case XK_Down:   special = GLUT_KEY_DOWN;   break;\r
-\r
-                    case XK_KP_Prior:\r
-                    case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;\r
-                    case XK_KP_Next:\r
-                    case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;\r
-                    case XK_KP_Home:\r
-                    case XK_Home:   special = GLUT_KEY_HOME;   break;\r
-                    case XK_KP_End:\r
-                    case XK_End:    special = GLUT_KEY_END;    break;\r
-                    case XK_KP_Insert:\r
-                    case XK_Insert: special = GLUT_KEY_INSERT; break;\r
-\r
-                    case XK_Num_Lock :  special = GLUT_KEY_NUM_LOCK;  break;\r
-                    case XK_KP_Begin :  special = GLUT_KEY_BEGIN;     break;\r
-                    case XK_KP_Delete:  special = GLUT_KEY_DELETE;    break;\r
-\r
-                    case XK_Shift_L:   special = GLUT_KEY_SHIFT_L;    break;\r
-                    case XK_Shift_R:   special = GLUT_KEY_SHIFT_R;    break;\r
-                    case XK_Control_L: special = GLUT_KEY_CTRL_L;     break;\r
-                    case XK_Control_R: special = GLUT_KEY_CTRL_R;     break;\r
-                    case XK_Alt_L:     special = GLUT_KEY_ALT_L;      break;\r
-                    case XK_Alt_R:     special = GLUT_KEY_ALT_R;      break;\r
-                    }\r
-\r
-                    /*\r
-                     * Execute the callback (if one has been specified),\r
-                     * given that the special code seems to be valid...\r
-                     */\r
-                    if( special_cb && (special != -1) )\r
-                    {\r
-                        fgSetWindow( window );\r
-                        fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );\r
-                        special_cb( special, event.xkey.x, event.xkey.y );\r
-                        fgState.Modifiers = INVALID_MODIFIERS;\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        break;\r
-\r
-        case ReparentNotify:\r
-            break; /* XXX Should disable this event */\r
-\r
-        /* Not handled */\r
-        case GravityNotify:\r
-            break;\r
-\r
-        default:\r
-            /* enter handling of Extension Events here */\r
-            #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H\r
-                fgHandleExtensionEvents( &event );\r
-            #endif\r
-            break;\r
-        }\r
-    }\r
-}\r
-\r
-\r
-static void fgPlatformMainLoopPreliminaryWork ( void )\r
-{\r
-}\r
-#endif\r
-\r
-/* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
-\r
-/*\r
- * Executes a single iteration in the freeglut processing loop.\r
- */\r
-void FGAPIENTRY glutMainLoopEvent( void )\r
-{\r
-       fgPlatformProcessSingleEvent ();\r
-\r
-    if( fgState.Timers.First )\r
-        fghCheckTimers( );\r
-    fghCheckJoystickPolls( );\r
-    fghDisplayAll( );\r
-\r
-    fgCloseWindows( );\r
-}\r
-\r
-/*\r
- * Enters the freeglut processing loop.\r
- * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".\r
- */\r
-void FGAPIENTRY glutMainLoop( void )\r
-{\r
-    int action;\r
-\r
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );\r
-\r
-       fgPlatformMainLoopPreliminaryWork ();\r
-\r
-    fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;\r
-    while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )\r
-    {\r
-        SFG_Window *window;\r
-\r
-        glutMainLoopEvent( );\r
-        /*\r
-         * Step through the list of windows, seeing if there are any\r
-         * that are not menus\r
-         */\r
-        for( window = ( SFG_Window * )fgStructure.Windows.First;\r
-             window;\r
-             window = ( SFG_Window * )window->Node.Next )\r
-            if ( ! ( window->IsMenu ) )\r
-                break;\r
-\r
-        if( ! window )\r
-            fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
-        else\r
-        {\r
-            if( fgState.IdleCallback )\r
-            {\r
-                if( fgStructure.CurrentWindow &&\r
-                    fgStructure.CurrentWindow->IsMenu )\r
-                    /* fail safe */\r
-                    fgSetWindow( window );\r
-                fgState.IdleCallback( );\r
-            }\r
-\r
-            fghSleepForEvents( );\r
-        }\r
-    }\r
-\r
-    /*\r
-     * When this loop terminates, destroy the display, state and structure\r
-     * of a freeglut session, so that another glutInit() call can happen\r
-     *\r
-     * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.\r
-     */\r
-    action = fgState.ActionOnWindowClose;\r
-    fgDeinitialize( );\r
-    if( action == GLUT_ACTION_EXIT )\r
-        exit( 0 );\r
-}\r
-\r
-/*\r
- * Leaves the freeglut processing loop.\r
- */\r
-void FGAPIENTRY glutLeaveMainLoop( void )\r
-{\r
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );\r
-    fgState.ExecState = GLUT_EXEC_STATE_STOP ;\r
-}\r
-\r
-\r
-\r
-/*** END OF FILE ***/\r
+/*
+ * freeglut_main.c
+ *
+ * The windows message processing methods.
+ *
+ * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
+ * Written by Pawel W. Olszta, <olszta@sourceforge.net>
+ * Creation date: Fri Dec 3 1999
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <GL/freeglut.h>
+#include "freeglut_internal.h"
+#ifdef HAVE_ERRNO_H
+#    include <errno.h>
+#endif
+#include <stdarg.h>
+#ifdef  HAVE_VFPRINTF
+#    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
+#elif defined(HAVE__DOPRNT)
+#    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
+#else
+#    define VFPRINTF(s,f,a)
+#endif
+
+/*
+ * Try to get the maximum value allowed for ints, falling back to the minimum
+ * guaranteed by ISO C99 if there is no suitable header.
+ */
+#ifdef HAVE_LIMITS_H
+#    include <limits.h>
+#endif
+#ifndef INT_MAX
+#    define INT_MAX 32767
+#endif
+
+#ifndef MIN
+#    define MIN(a,b) (((a)<(b)) ? (a) : (b))
+#endif
+
+extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );
+extern void fgPlatformDisplayWindow ( SFG_Window *window );
+extern fg_time_t fgPlatformSystemTime ( void );
+extern void fgPlatformSleepForEvents( fg_time_t msec );
+extern void fgPlatformProcessSingleEvent ( void );
+extern void fgPlatformMainLoopPreliminaryWork ( void );
+
+
+
+
+/* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
+
+static void fghReshapeWindow ( SFG_Window *window, int width, int height )
+{
+    SFG_Window *current_window = fgStructure.CurrentWindow;
+
+    freeglut_return_if_fail( window != NULL );
+
+       fgPlatformReshapeWindow ( window, width, height );
+
+    if( FETCH_WCB( *window, Reshape ) )
+        INVOKE_WCB( *window, Reshape, ( width, height ) );
+    else
+    {
+        fgSetWindow( window );
+        glViewport( 0, 0, width, height );
+    }
+
+    /*
+     * Force a window redraw.  In Windows at least this is only a partial
+     * solution:  if the window is increasing in size in either dimension,
+     * the already-drawn part does not get drawn again and things look funny.
+     * But without this we get this bad behaviour whenever we resize the
+     * window.
+     */
+    window->State.Redisplay = GL_TRUE;
+
+    if( window->IsMenu )
+        fgSetWindow( current_window );
+}
+
+/*
+ * Calls a window's redraw method. This is used when
+ * a redraw is forced by the incoming window messages.
+ */
+void fghRedrawWindow ( SFG_Window *window )
+{
+    SFG_Window *current_window = fgStructure.CurrentWindow;
+
+    freeglut_return_if_fail( window );
+    freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
+
+    window->State.Redisplay = GL_FALSE;
+
+    freeglut_return_if_fail( window->State.Visible );
+
+    fgSetWindow( window );
+
+    if( window->State.NeedToResize )
+    {
+        fghReshapeWindow(
+            window,
+            window->State.Width,
+            window->State.Height
+        );
+
+        window->State.NeedToResize = GL_FALSE;
+    }
+
+    INVOKE_WCB( *window, Display, ( ) );
+
+    fgSetWindow( current_window );
+}
+
+
+static void fghcbDisplayWindow( SFG_Window *window,
+                                SFG_Enumerator *enumerator )
+{
+    if( window->State.Redisplay &&
+        window->State.Visible )
+    {
+        window->State.Redisplay = GL_FALSE;
+               fgPlatformDisplayWindow ( window );
+    }
+
+    fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
+}
+
+/*
+ * Make all windows perform a display call
+ */
+static void fghDisplayAll( void )
+{
+    SFG_Enumerator enumerator;
+
+    enumerator.found = GL_FALSE;
+    enumerator.data  =  NULL;
+
+    fgEnumWindows( fghcbDisplayWindow, &enumerator );
+}
+
+/*
+ * Window enumerator callback to check for the joystick polling code
+ */
+static void fghcbCheckJoystickPolls( SFG_Window *window,
+                                     SFG_Enumerator *enumerator )
+{
+    fg_time_t checkTime = fgElapsedTime( );
+
+    if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
+        checkTime )
+    {
+#if !defined(_WIN32_WCE)
+        fgJoystickPollWindow( window );
+#endif /* !defined(_WIN32_WCE) */
+        window->State.JoystickLastPoll = checkTime;
+    }
+
+    fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
+}
+
+/*
+ * Check all windows for joystick polling
+ */
+static void fghCheckJoystickPolls( void )
+{
+    SFG_Enumerator enumerator;
+
+    enumerator.found = GL_FALSE;
+    enumerator.data  =  NULL;
+
+    fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
+}
+
+/*
+ * Check the global timers
+ */
+static void fghCheckTimers( void )
+{
+    fg_time_t checkTime = fgElapsedTime( );
+
+    while( fgState.Timers.First )
+    {
+        SFG_Timer *timer = fgState.Timers.First;
+
+        if( timer->TriggerTime > checkTime )
+            break;
+
+        fgListRemove( &fgState.Timers, &timer->Node );
+        fgListAppend( &fgState.FreeTimers, &timer->Node );
+
+        timer->Callback( timer->ID );
+    }
+}
+
+/* Platform-dependent time in milliseconds, as an unsigned 64-bit integer.
+ * This doesn't overflow in any reasonable time, so no need to worry about
+ * that. The GLUT API return value will however overflow after 49.7 days,
+ * and on Windows we (currently) do not have access to a 64-bit timestamp,
+ * which means internal time will still get in trouble when running the
+ * application for more than 49.7 days.
+ * This value wraps every 49.7 days, but integer overflows cancel
+ * when subtracting an initial start time, unless the total time exceeds
+ * 32-bit, where the GLUT API return value is also overflowed.
+ */  
+fg_time_t fgSystemTime(void)
+{
+       return fgPlatformSystemTime();
+}
+  
+/*
+ * Elapsed Time
+ */
+fg_time_t fgElapsedTime( void )
+{
+    return fgSystemTime() - fgState.Time;
+}
+
+/*
+ * Error Messages.
+ */
+void fgError( const char *fmt, ... )
+{
+    va_list ap;
+
+    if (fgState.ErrorFunc) {
+
+        va_start( ap, fmt );
+
+        /* call user set error handler here */
+        fgState.ErrorFunc(fmt, ap);
+
+        va_end( ap );
+
+    } else {
+
+        va_start( ap, fmt );
+
+        fprintf( stderr, "freeglut ");
+        if( fgState.ProgramName )
+            fprintf( stderr, "(%s): ", fgState.ProgramName );
+        VFPRINTF( stderr, fmt, ap );
+        fprintf( stderr, "\n" );
+
+        va_end( ap );
+
+        if ( fgState.Initialised )
+            fgDeinitialize ();
+
+        exit( 1 );
+    }
+}
+
+void fgWarning( const char *fmt, ... )
+{
+    va_list ap;
+
+    if (fgState.WarningFunc) {
+
+        va_start( ap, fmt );
+
+        /* call user set warning handler here */
+        fgState.WarningFunc(fmt, ap);
+
+        va_end( ap );
+
+    } else {
+
+        va_start( ap, fmt );
+
+        fprintf( stderr, "freeglut ");
+        if( fgState.ProgramName )
+            fprintf( stderr, "(%s): ", fgState.ProgramName );
+        VFPRINTF( stderr, fmt, ap );
+        fprintf( stderr, "\n" );
+
+        va_end( ap );
+    }
+}
+
+
+/*
+ * Indicates whether Joystick events are being used by ANY window.
+ *
+ * The current mechanism is to walk all of the windows and ask if
+ * there is a joystick callback.  We have a short-circuit early
+ * return if we find any joystick handler registered.
+ *
+ * The real way to do this is to make use of the glutTimer() API
+ * to more cleanly re-implement the joystick API.  Then, this code
+ * and all other "joystick timer" code can be yanked.
+ *
+ */
+static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
+{
+    if( FETCH_WCB( *w, Joystick ) )
+    {
+        e->found = GL_TRUE;
+        e->data = w;
+    }
+    fgEnumSubWindows( w, fghCheckJoystickCallback, e );
+}
+static int fghHaveJoystick( void )
+{
+    SFG_Enumerator enumerator;
+
+    enumerator.found = GL_FALSE;
+    enumerator.data = NULL;
+    fgEnumWindows( fghCheckJoystickCallback, &enumerator );
+    return !!enumerator.data;
+}
+static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
+{
+    if( w->State.Redisplay && w->State.Visible )
+    {
+        e->found = GL_TRUE;
+        e->data = w;
+    }
+    fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
+}
+static int fghHavePendingRedisplays (void)
+{
+    SFG_Enumerator enumerator;
+
+    enumerator.found = GL_FALSE;
+    enumerator.data = NULL;
+    fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
+    return !!enumerator.data;
+}
+/*
+ * Returns the number of GLUT ticks (milliseconds) till the next timer event.
+ */
+static fg_time_t fghNextTimer( void )
+{
+    fg_time_t currentTime = fgElapsedTime();
+    SFG_Timer *timer = fgState.Timers.First;
+
+    if( !timer )
+        return INT_MAX;
+
+    if( timer->TriggerTime < currentTime )
+        return 0;
+    else
+        return timer->TriggerTime - currentTime;
+}
+
+static void fghSleepForEvents( void )
+{
+    fg_time_t msec;
+
+    if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
+        return;
+
+    msec = fghNextTimer( );
+    /* XXX Use GLUT timers for joysticks... */
+    /* XXX Dumb; forces granularity to .01sec */
+    if( fghHaveJoystick( ) && ( msec > 10 ) )     
+        msec = 10;
+
+       fgPlatformSleepForEvents ( msec );
+}
+
+
+/* -- INTERFACE FUNCTIONS -------------------------------------------------- */
+
+/*
+ * Executes a single iteration in the freeglut processing loop.
+ */
+void FGAPIENTRY glutMainLoopEvent( void )
+{
+       fgPlatformProcessSingleEvent ();
+
+    if( fgState.Timers.First )
+        fghCheckTimers( );
+    fghCheckJoystickPolls( );
+    fghDisplayAll( );
+
+    fgCloseWindows( );
+}
+
+/*
+ * Enters the freeglut processing loop.
+ * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
+ */
+void FGAPIENTRY glutMainLoop( void )
+{
+    int action;
+
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
+
+       fgPlatformMainLoopPreliminaryWork ();
+
+    fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
+    while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
+    {
+        SFG_Window *window;
+
+        glutMainLoopEvent( );
+        /*
+         * Step through the list of windows, seeing if there are any
+         * that are not menus
+         */
+        for( window = ( SFG_Window * )fgStructure.Windows.First;
+             window;
+             window = ( SFG_Window * )window->Node.Next )
+            if ( ! ( window->IsMenu ) )
+                break;
+
+        if( ! window )
+            fgState.ExecState = GLUT_EXEC_STATE_STOP;
+        else
+        {
+            if( fgState.IdleCallback )
+            {
+                if( fgStructure.CurrentWindow &&
+                    fgStructure.CurrentWindow->IsMenu )
+                    /* fail safe */
+                    fgSetWindow( window );
+                fgState.IdleCallback( );
+            }
+
+            fghSleepForEvents( );
+        }
+    }
+
+    /*
+     * When this loop terminates, destroy the display, state and structure
+     * of a freeglut session, so that another glutInit() call can happen
+     *
+     * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
+     */
+    action = fgState.ActionOnWindowClose;
+    fgDeinitialize( );
+    if( action == GLUT_ACTION_EXIT )
+        exit( 0 );
+}
+
+/*
+ * Leaves the freeglut processing loop.
+ */
+void FGAPIENTRY glutLeaveMainLoop( void )
+{
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
+    fgState.ExecState = GLUT_EXEC_STATE_STOP ;
+}
+
+
+
+/*** END OF FILE ***/