-/*\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
-#ifdef WM_TOUCH\r
- typedef BOOL (WINAPI *pGetTouchInputInfo)(HTOUCHINPUT,UINT,PTOUCHINPUT,int);\r
- typedef BOOL (WINAPI *pCloseTouchInputHandle)(HTOUCHINPUT);\r
- static pGetTouchInputInfo fghGetTouchInputInfo = (pGetTouchInputInfo)0xDEADBEEF;\r
- static pCloseTouchInputHandle fghCloseTouchInputHandle = (pCloseTouchInputHandle)0xDEADBEEF;\r
-#endif\r
-\r
-extern void fghPlatformReshapeWindow ( SFG_Window *window, int width, int height );\r
-extern void fghcbPlatformDisplayWindow ( SFG_Window *window );\r
-extern void fghPlatformSleepForEvents( long msec );\r
-extern void fghProcessSingleEvent ( 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 fghPlatformReshapeWindow ( 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
- fghPlatformReshapeWindow ( 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
-static 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 fghcbPlatformDisplayWindow ( 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
- fghcbPlatformDisplayWindow ( 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 fghPlatformSleepForEvents( 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
- fghPlatformSleepForEvents ( 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 fghGetXModifiers( 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 fghProcessSingleEvent ( 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 = fghGetXModifiers( 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 = fghGetXModifiers( 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 = fghGetXModifiers( 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 = fghGetXModifiers( 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
-#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
- fghProcessSingleEvent ();\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
-#if TARGET_HOST_MS_WINDOWS\r
- SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;\r
-#endif\r
-\r
- FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );\r
-\r
-#if TARGET_HOST_MS_WINDOWS\r
- /*\r
- * Processing before the main loop: If there is a window which is open and\r
- * which has a visibility callback, call it. I know this is an ugly hack,\r
- * but I'm not sure what else to do about it. Ideally we should leave\r
- * something uninitialized in the create window code and initialize it in\r
- * the main loop, and have that initialization create a "WM_ACTIVATE"\r
- * message. Then we would put the visibility callback code in the\r
- * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02\r
- */\r
- while( window )\r
- {\r
- if ( FETCH_WCB( *window, Visibility ) )\r
- {\r
- SFG_Window *current_window = fgStructure.CurrentWindow ;\r
-\r
- INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );\r
- fgSetWindow( current_window );\r
- }\r
-\r
- window = (SFG_Window *)window->Node.Next ;\r
- }\r
-#endif\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
-#if TARGET_HOST_MS_WINDOWS\r
-/*\r
- * Determine a GLUT modifer mask based on MS-WINDOWS system info.\r
- */\r
-static int fghGetWin32Modifiers (void)\r
-{\r
- return\r
- ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||\r
- ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |\r
- ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||\r
- ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |\r
- ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||\r
- ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );\r
-}\r
-\r
-/*\r
- * The window procedure for handling Win32 events\r
- */\r
-LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,\r
- LPARAM lParam )\r
-{\r
- static unsigned char lControl = 0, rControl = 0, lShift = 0,\r
- rShift = 0, lAlt = 0, rAlt = 0;\r
-\r
- SFG_Window* window;\r
- PAINTSTRUCT ps;\r
- LRESULT lRet = 1;\r
-\r
- FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;\r
-\r
- window = fgWindowByHandle( hWnd );\r
-\r
- if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )\r
- return DefWindowProc( hWnd, uMsg, wParam, lParam );\r
-\r
- /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,\r
- uMsg, wParam, lParam ); */\r
-\r
- if ( window )\r
- {\r
- /* Checking for CTRL, ALT, and SHIFT key positions: Key Down! */\r
- if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lControl = 1;\r
- }\r
-\r
- if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rControl = 1;\r
- }\r
-\r
- if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lShift = 1;\r
- }\r
-\r
- if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rShift = 1;\r
- }\r
-\r
- if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lAlt = 1;\r
- }\r
-\r
- if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )\r
- {\r
- INVOKE_WCB ( *window, Special,\r
- ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rAlt = 1;\r
- }\r
-\r
- /* Checking for CTRL, ALT, and SHIFT key positions: Key Up! */\r
- if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lControl = 0;\r
- }\r
-\r
- if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rControl = 0;\r
- }\r
-\r
- if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lShift = 0;\r
- }\r
-\r
- if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rShift = 0;\r
- }\r
-\r
- if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- lAlt = 0;\r
- }\r
-\r
- if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )\r
- {\r
- INVOKE_WCB ( *window, SpecialUp,\r
- ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- rAlt = 0;\r
- }\r
- }\r
-\r
- switch( uMsg )\r
- {\r
- case WM_CREATE:\r
- /* The window structure is passed as the creation structure parameter... */\r
- window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);\r
- FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",\r
- "fgWindowProc" );\r
-\r
- window->Window.Handle = hWnd;\r
- window->Window.Device = GetDC( hWnd );\r
- if( window->IsMenu )\r
- {\r
- unsigned int current_DisplayMode = fgState.DisplayMode;\r
- fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;\r
-#if !defined(_WIN32_WCE)\r
- fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );\r
-#endif\r
- fgState.DisplayMode = current_DisplayMode;\r
-\r
- if( fgStructure.MenuContext )\r
- wglMakeCurrent( window->Window.Device,\r
- fgStructure.MenuContext->MContext\r
- );\r
- else\r
- {\r
- fgStructure.MenuContext =\r
- (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );\r
- fgStructure.MenuContext->MContext =\r
- wglCreateContext( window->Window.Device );\r
- }\r
-\r
- /* window->Window.Context = wglGetCurrentContext (); */\r
- window->Window.Context = wglCreateContext( window->Window.Device );\r
- }\r
- else\r
- {\r
-#if !defined(_WIN32_WCE)\r
- fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );\r
-#endif\r
-\r
- if( ! fgState.UseCurrentContext )\r
- window->Window.Context =\r
- wglCreateContext( window->Window.Device );\r
- else\r
- {\r
- window->Window.Context = wglGetCurrentContext( );\r
- if( ! window->Window.Context )\r
- window->Window.Context =\r
- wglCreateContext( window->Window.Device );\r
- }\r
-\r
-#if !defined(_WIN32_WCE)\r
- fgNewWGLCreateContext( window );\r
-#endif\r
- }\r
-\r
- window->State.NeedToResize = GL_TRUE;\r
- /* if we used CW_USEDEFAULT (thats a negative value) for the size\r
- * of the window, query the window now for the size at which it\r
- * was created.\r
- */\r
- if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )\r
- {\r
- SFG_Window *current_window = fgStructure.CurrentWindow;\r
-\r
- fgSetWindow( window );\r
- window->State.Width = glutGet( GLUT_WINDOW_WIDTH );\r
- window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );\r
- fgSetWindow( current_window );\r
- }\r
-\r
- ReleaseDC( window->Window.Handle, window->Window.Device );\r
-\r
-#if defined(_WIN32_WCE)\r
- /* Take over button handling */\r
- {\r
- HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));\r
- if (dxDllLib)\r
- {\r
- GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));\r
- GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));\r
- }\r
-\r
- if(GXOpenInput_)\r
- (*GXOpenInput_)();\r
- if(GXGetDefaultKeys_)\r
- gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);\r
- }\r
-\r
-#endif /* defined(_WIN32_WCE) */\r
- break;\r
-\r
- case WM_SIZE:\r
- /*\r
- * If the window is visible, then it is the user manually resizing it.\r
- * If it is not, then it is the system sending us a dummy resize with\r
- * zero dimensions on a "glutIconifyWindow" call.\r
- */\r
- if( window->State.Visible )\r
- {\r
- window->State.NeedToResize = GL_TRUE;\r
-#if defined(_WIN32_WCE)\r
- window->State.Width = HIWORD(lParam);\r
- window->State.Height = LOWORD(lParam);\r
-#else\r
- window->State.Width = LOWORD(lParam);\r
- window->State.Height = HIWORD(lParam);\r
-#endif /* defined(_WIN32_WCE) */\r
- }\r
-\r
- break;\r
-\r
- case WM_SETFOCUS:\r
-/* printf("WM_SETFOCUS: %p\n", window ); */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );\r
- break;\r
-\r
- case WM_KILLFOCUS:\r
-/* printf("WM_KILLFOCUS: %p\n", window ); */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );\r
-\r
- if( window->IsMenu &&\r
- window->ActiveMenu && window->ActiveMenu->IsActive )\r
- fgUpdateMenuHighlight( window->ActiveMenu );\r
-\r
- break;\r
-\r
-#if 0\r
- case WM_ACTIVATE:\r
- if (LOWORD(wParam) != WA_INACTIVE)\r
- {\r
-/* printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,\r
- window->State.Cursor ); */\r
- fgSetCursor( window, window->State.Cursor );\r
- }\r
-\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
-#endif\r
-\r
- case WM_SETCURSOR:\r
-/* printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */\r
- if( LOWORD( lParam ) == HTCLIENT )\r
- fgSetCursor ( window, window->State.Cursor ) ;\r
- else\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
-\r
- case WM_SHOWWINDOW:\r
- window->State.Visible = GL_TRUE;\r
- window->State.Redisplay = GL_TRUE;\r
- break;\r
-\r
- case WM_PAINT:\r
- /* Turn on the visibility in case it was turned off somehow */\r
- window->State.Visible = GL_TRUE;\r
- BeginPaint( hWnd, &ps );\r
- fghRedrawWindow( window );\r
- EndPaint( hWnd, &ps );\r
- break;\r
-\r
- case WM_CLOSE:\r
- fgDestroyWindow ( window );\r
- if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )\r
- PostQuitMessage(0);\r
- break;\r
-\r
- case WM_DESTROY:\r
- /*\r
- * The window already got destroyed, so don't bother with it.\r
- */\r
- return 0;\r
-\r
- case WM_MOUSEMOVE:\r
- {\r
-#if defined(_WIN32_WCE)\r
- window->State.MouseX = 320-HIWORD( lParam );\r
- window->State.MouseY = LOWORD( lParam );\r
-#else\r
- window->State.MouseX = LOWORD( lParam );\r
- window->State.MouseY = HIWORD( lParam );\r
-#endif /* defined(_WIN32_WCE) */\r
- /* Restrict to [-32768, 32767] to match X11 behaviour */\r
- /* See comment in "freeglut_developer" mailing list 10/4/04 */\r
- if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;\r
- if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;\r
-\r
- if ( window->ActiveMenu )\r
- {\r
- fgUpdateMenuHighlight( window->ActiveMenu );\r
- break;\r
- }\r
- SetFocus(window->Window.Handle);\r
-\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
-\r
- if( ( wParam & MK_LBUTTON ) ||\r
- ( wParam & MK_MBUTTON ) ||\r
- ( wParam & MK_RBUTTON ) )\r
- INVOKE_WCB( *window, Motion, ( window->State.MouseX,\r
- window->State.MouseY ) );\r
- else\r
- INVOKE_WCB( *window, Passive, ( window->State.MouseX,\r
- window->State.MouseY ) );\r
-\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- break;\r
-\r
- case WM_LBUTTONDOWN:\r
- case WM_MBUTTONDOWN:\r
- case WM_RBUTTONDOWN:\r
- case WM_LBUTTONUP:\r
- case WM_MBUTTONUP:\r
- case WM_RBUTTONUP:\r
- {\r
- GLboolean pressed = GL_TRUE;\r
- int button;\r
-\r
-#if defined(_WIN32_WCE)\r
- window->State.MouseX = 320-HIWORD( lParam );\r
- window->State.MouseY = LOWORD( lParam );\r
-#else\r
- window->State.MouseX = LOWORD( lParam );\r
- window->State.MouseY = HIWORD( lParam );\r
-#endif /* defined(_WIN32_WCE) */\r
-\r
- /* Restrict to [-32768, 32767] to match X11 behaviour */\r
- /* See comment in "freeglut_developer" mailing list 10/4/04 */\r
- if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;\r
- if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;\r
-\r
- switch( uMsg )\r
- {\r
- case WM_LBUTTONDOWN:\r
- pressed = GL_TRUE;\r
- button = GLUT_LEFT_BUTTON;\r
- break;\r
- case WM_MBUTTONDOWN:\r
- pressed = GL_TRUE;\r
- button = GLUT_MIDDLE_BUTTON;\r
- break;\r
- case WM_RBUTTONDOWN:\r
- pressed = GL_TRUE;\r
- button = GLUT_RIGHT_BUTTON;\r
- break;\r
- case WM_LBUTTONUP:\r
- pressed = GL_FALSE;\r
- button = GLUT_LEFT_BUTTON;\r
- break;\r
- case WM_MBUTTONUP:\r
- pressed = GL_FALSE;\r
- button = GLUT_MIDDLE_BUTTON;\r
- break;\r
- case WM_RBUTTONUP:\r
- pressed = GL_FALSE;\r
- button = GLUT_RIGHT_BUTTON;\r
- break;\r
- default:\r
- pressed = GL_FALSE;\r
- button = -1;\r
- break;\r
- }\r
-\r
-#if !defined(_WIN32_WCE)\r
- if( GetSystemMetrics( SM_SWAPBUTTON ) )\r
- {\r
- if( button == GLUT_LEFT_BUTTON )\r
- button = GLUT_RIGHT_BUTTON;\r
- else\r
- if( button == GLUT_RIGHT_BUTTON )\r
- button = GLUT_LEFT_BUTTON;\r
- }\r
-#endif /* !defined(_WIN32_WCE) */\r
-\r
- if( button == -1 )\r
- return DefWindowProc( hWnd, uMsg, lParam, wParam );\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
- window->State.MouseX, window->State.MouseY ) )\r
- break;\r
-\r
- /* Set capture so that the window captures all the mouse messages */\r
- /*\r
- * XXX - Multiple button support: Under X11, the mouse is not released\r
- * XXX - from the window until all buttons have been released, even if the\r
- * XXX - user presses a button in another window. This will take more\r
- * XXX - code changes than I am up to at the moment (10/5/04). The present\r
- * XXX - is a 90 percent solution.\r
- */\r
- if ( pressed == GL_TRUE )\r
- SetCapture ( window->Window.Handle ) ;\r
- else\r
- ReleaseCapture () ;\r
-\r
- if( ! FETCH_WCB( *window, Mouse ) )\r
- break;\r
-\r
- fgSetWindow( window );\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
-\r
- INVOKE_WCB(\r
- *window, Mouse,\r
- ( button,\r
- pressed ? GLUT_DOWN : GLUT_UP,\r
- window->State.MouseX,\r
- window->State.MouseY\r
- )\r
- );\r
-\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- break;\r
-\r
- case 0x020a:\r
- /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */\r
- {\r
- int wheel_number = LOWORD( wParam );\r
- short ticks = ( short )HIWORD( wParam );\r
- fgState.MouseWheelTicks += ticks;\r
-\r
- /*\r
- * XXX Should use WHEEL_DELTA instead of 120\r
- */\r
- if ( abs ( fgState.MouseWheelTicks ) > 120 )\r
- {\r
- int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;\r
-\r
- if( ! FETCH_WCB( *window, MouseWheel ) &&\r
- ! FETCH_WCB( *window, Mouse ) )\r
- break;\r
-\r
- fgSetWindow( window );\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
-\r
- /*\r
- * XXX Should use WHEEL_DELTA instead of 120\r
- */\r
- while( abs ( fgState.MouseWheelTicks ) > 120 )\r
- {\r
- if( FETCH_WCB( *window, MouseWheel ) )\r
- INVOKE_WCB( *window, MouseWheel,\r
- ( wheel_number,\r
- direction,\r
- window->State.MouseX,\r
- window->State.MouseY\r
- )\r
- );\r
- else /* No mouse wheel, call the mouse button callback twice */\r
- {\r
- /*\r
- * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4\r
- * " " one +1 to 5, -1 to 6, ...\r
- *\r
- * XXX The below assumes that you have no more than 3 mouse\r
- * XXX buttons. Sorry.\r
- */\r
- int button = wheel_number * 2 + 3;\r
- if( direction < 0 )\r
- ++button;\r
- INVOKE_WCB( *window, Mouse,\r
- ( button, GLUT_DOWN,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
- INVOKE_WCB( *window, Mouse,\r
- ( button, GLUT_UP,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
- }\r
-\r
- /*\r
- * XXX Should use WHEEL_DELTA instead of 120\r
- */\r
- fgState.MouseWheelTicks -= 120 * direction;\r
- }\r
-\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- }\r
- break ;\r
-\r
- case WM_SYSKEYDOWN:\r
- case WM_KEYDOWN:\r
- {\r
- int keypress = -1;\r
- POINT mouse_pos ;\r
-\r
- if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )\r
- break;\r
-\r
- /*\r
- * Remember the current modifiers state. This is done here in order\r
- * to make sure the VK_DELETE keyboard callback is executed properly.\r
- */\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
-\r
- GetCursorPos( &mouse_pos );\r
- ScreenToClient( window->Window.Handle, &mouse_pos );\r
-\r
- window->State.MouseX = mouse_pos.x;\r
- window->State.MouseY = mouse_pos.y;\r
-\r
- /* Convert the Win32 keystroke codes to GLUTtish way */\r
-# define KEY(a,b) case a: keypress = b; break;\r
-\r
- switch( wParam )\r
- {\r
- KEY( VK_F1, GLUT_KEY_F1 );\r
- KEY( VK_F2, GLUT_KEY_F2 );\r
- KEY( VK_F3, GLUT_KEY_F3 );\r
- KEY( VK_F4, GLUT_KEY_F4 );\r
- KEY( VK_F5, GLUT_KEY_F5 );\r
- KEY( VK_F6, GLUT_KEY_F6 );\r
- KEY( VK_F7, GLUT_KEY_F7 );\r
- KEY( VK_F8, GLUT_KEY_F8 );\r
- KEY( VK_F9, GLUT_KEY_F9 );\r
- KEY( VK_F10, GLUT_KEY_F10 );\r
- KEY( VK_F11, GLUT_KEY_F11 );\r
- KEY( VK_F12, GLUT_KEY_F12 );\r
- KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );\r
- KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );\r
- KEY( VK_HOME, GLUT_KEY_HOME );\r
- KEY( VK_END, GLUT_KEY_END );\r
- KEY( VK_LEFT, GLUT_KEY_LEFT );\r
- KEY( VK_UP, GLUT_KEY_UP );\r
- KEY( VK_RIGHT, GLUT_KEY_RIGHT );\r
- KEY( VK_DOWN, GLUT_KEY_DOWN );\r
- KEY( VK_INSERT, GLUT_KEY_INSERT );\r
- KEY( VK_LCONTROL, GLUT_KEY_CTRL_L );\r
- KEY( VK_RCONTROL, GLUT_KEY_CTRL_R );\r
- KEY( VK_LSHIFT, GLUT_KEY_SHIFT_L );\r
- KEY( VK_RSHIFT, GLUT_KEY_SHIFT_R );\r
- KEY( VK_LMENU, GLUT_KEY_ALT_L );\r
- KEY( VK_RMENU, GLUT_KEY_ALT_R );\r
-\r
- case VK_DELETE:\r
- /* The delete key should be treated as an ASCII keypress: */\r
- INVOKE_WCB( *window, Keyboard,\r
- ( 127, window->State.MouseX, window->State.MouseY )\r
- );\r
- }\r
-\r
-#if defined(_WIN32_WCE)\r
- if(!(lParam & 0x40000000)) /* Prevent auto-repeat */\r
- {\r
- if(wParam==(unsigned)gxKeyList.vkRight)\r
- keypress = GLUT_KEY_RIGHT;\r
- else if(wParam==(unsigned)gxKeyList.vkLeft)\r
- keypress = GLUT_KEY_LEFT;\r
- else if(wParam==(unsigned)gxKeyList.vkUp)\r
- keypress = GLUT_KEY_UP;\r
- else if(wParam==(unsigned)gxKeyList.vkDown)\r
- keypress = GLUT_KEY_DOWN;\r
- else if(wParam==(unsigned)gxKeyList.vkA)\r
- keypress = GLUT_KEY_F1;\r
- else if(wParam==(unsigned)gxKeyList.vkB)\r
- keypress = GLUT_KEY_F2;\r
- else if(wParam==(unsigned)gxKeyList.vkC)\r
- keypress = GLUT_KEY_F3;\r
- else if(wParam==(unsigned)gxKeyList.vkStart)\r
- keypress = GLUT_KEY_F4;\r
- }\r
-#endif\r
-\r
- if( keypress != -1 )\r
- INVOKE_WCB( *window, Special,\r
- ( keypress,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- break;\r
-\r
- case WM_SYSKEYUP:\r
- case WM_KEYUP:\r
- {\r
- int keypress = -1;\r
- POINT mouse_pos;\r
-\r
- /*\r
- * Remember the current modifiers state. This is done here in order\r
- * to make sure the VK_DELETE keyboard callback is executed properly.\r
- */\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
-\r
- GetCursorPos( &mouse_pos );\r
- ScreenToClient( window->Window.Handle, &mouse_pos );\r
-\r
- window->State.MouseX = mouse_pos.x;\r
- window->State.MouseY = mouse_pos.y;\r
-\r
- /*\r
- * Convert the Win32 keystroke codes to GLUTtish way.\r
- * "KEY(a,b)" was defined under "WM_KEYDOWN"\r
- */\r
-\r
- switch( wParam )\r
- {\r
- KEY( VK_F1, GLUT_KEY_F1 );\r
- KEY( VK_F2, GLUT_KEY_F2 );\r
- KEY( VK_F3, GLUT_KEY_F3 );\r
- KEY( VK_F4, GLUT_KEY_F4 );\r
- KEY( VK_F5, GLUT_KEY_F5 );\r
- KEY( VK_F6, GLUT_KEY_F6 );\r
- KEY( VK_F7, GLUT_KEY_F7 );\r
- KEY( VK_F8, GLUT_KEY_F8 );\r
- KEY( VK_F9, GLUT_KEY_F9 );\r
- KEY( VK_F10, GLUT_KEY_F10 );\r
- KEY( VK_F11, GLUT_KEY_F11 );\r
- KEY( VK_F12, GLUT_KEY_F12 );\r
- KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );\r
- KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );\r
- KEY( VK_HOME, GLUT_KEY_HOME );\r
- KEY( VK_END, GLUT_KEY_END );\r
- KEY( VK_LEFT, GLUT_KEY_LEFT );\r
- KEY( VK_UP, GLUT_KEY_UP );\r
- KEY( VK_RIGHT, GLUT_KEY_RIGHT );\r
- KEY( VK_DOWN, GLUT_KEY_DOWN );\r
- KEY( VK_INSERT, GLUT_KEY_INSERT );\r
- KEY( VK_LCONTROL, GLUT_KEY_CTRL_L );\r
- KEY( VK_RCONTROL, GLUT_KEY_CTRL_R );\r
- KEY( VK_LSHIFT, GLUT_KEY_SHIFT_L );\r
- KEY( VK_RSHIFT, GLUT_KEY_SHIFT_R );\r
- KEY( VK_LMENU, GLUT_KEY_ALT_L );\r
- KEY( VK_RMENU, GLUT_KEY_ALT_R );\r
-\r
- case VK_DELETE:\r
- /* The delete key should be treated as an ASCII keypress: */\r
- INVOKE_WCB( *window, KeyboardUp,\r
- ( 127, window->State.MouseX, window->State.MouseY )\r
- );\r
- break;\r
-\r
- default:\r
- {\r
-#if !defined(_WIN32_WCE)\r
- BYTE state[ 256 ];\r
- WORD code[ 2 ];\r
-\r
- GetKeyboardState( state );\r
-\r
- if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )\r
- wParam=code[ 0 ];\r
-\r
- INVOKE_WCB( *window, KeyboardUp,\r
- ( (char)wParam,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
-#endif /* !defined(_WIN32_WCE) */\r
- }\r
- }\r
-\r
- if( keypress != -1 )\r
- INVOKE_WCB( *window, SpecialUp,\r
- ( keypress,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
-\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- break;\r
-\r
- case WM_SYSCHAR:\r
- case WM_CHAR:\r
- {\r
- if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )\r
- break;\r
-\r
- fgState.Modifiers = fghGetWin32Modifiers( );\r
- INVOKE_WCB( *window, Keyboard,\r
- ( (char)wParam,\r
- window->State.MouseX, window->State.MouseY )\r
- );\r
- fgState.Modifiers = INVALID_MODIFIERS;\r
- }\r
- break;\r
-\r
- case WM_CAPTURECHANGED:\r
- /* User has finished resizing the window, force a redraw */\r
- INVOKE_WCB( *window, Display, ( ) );\r
-\r
- /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */\r
- break;\r
-\r
- /* Other messages that I have seen and which are not handled already */\r
- case WM_SETTEXT: /* 0x000c */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- /* Pass it on to "DefWindowProc" to set the window text */\r
- break;\r
-\r
- case WM_GETTEXT: /* 0x000d */\r
- /* Ideally we would copy the title of the window into "lParam" */\r
- /* strncpy ( (char *)lParam, "Window Title", wParam );\r
- lRet = ( wParam > 12 ) ? 12 : wParam; */\r
- /* the number of characters copied */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
-\r
- case WM_GETTEXTLENGTH: /* 0x000e */\r
- /* Ideally we would get the length of the title of the window */\r
- lRet = 12;\r
- /* the number of characters in "Window Title\0" (see above) */\r
- break;\r
-\r
- case WM_ERASEBKGND: /* 0x0014 */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
-\r
-#if !defined(_WIN32_WCE)\r
- case WM_SYNCPAINT: /* 0x0088 */\r
- /* Another window has moved, need to update this one */\r
- window->State.Redisplay = GL_TRUE;\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- /* Help screen says this message must be passed to "DefWindowProc" */\r
- break;\r
-\r
- case WM_NCPAINT: /* 0x0085 */\r
- /* Need to update the border of this window */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- /* Pass it on to "DefWindowProc" to repaint a standard border */\r
- break;\r
-\r
- case WM_SYSCOMMAND : /* 0x0112 */\r
- {\r
- /*\r
- * We have received a system command message. Try to act on it.\r
- * The commands are passed in through the "wParam" parameter:\r
- * The least significant digit seems to be which edge of the window\r
- * is being used for a resize event:\r
- * 4 3 5\r
- * 1 2\r
- * 7 6 8\r
- * Congratulations and thanks to Richard Rauch for figuring this out..\r
- */\r
- switch ( wParam & 0xfff0 )\r
- {\r
- case SC_SIZE :\r
- break ;\r
-\r
- case SC_MOVE :\r
- break ;\r
-\r
- case SC_MINIMIZE :\r
- /* User has clicked on the "-" to minimize the window */\r
- /* Turn off the visibility */\r
- window->State.Visible = GL_FALSE ;\r
-\r
- break ;\r
-\r
- case SC_MAXIMIZE :\r
- break ;\r
-\r
- case SC_NEXTWINDOW :\r
- break ;\r
-\r
- case SC_PREVWINDOW :\r
- break ;\r
-\r
- case SC_CLOSE :\r
- /* Followed very closely by a WM_CLOSE message */\r
- break ;\r
-\r
- case SC_VSCROLL :\r
- break ;\r
-\r
- case SC_HSCROLL :\r
- break ;\r
-\r
- case SC_MOUSEMENU :\r
- break ;\r
-\r
- case SC_KEYMENU :\r
- break ;\r
-\r
- case SC_ARRANGE :\r
- break ;\r
-\r
- case SC_RESTORE :\r
- break ;\r
-\r
- case SC_TASKLIST :\r
- break ;\r
-\r
- case SC_SCREENSAVE :\r
- break ;\r
-\r
- case SC_HOTKEY :\r
- break ;\r
-\r
-#if(WINVER >= 0x0400)\r
- case SC_DEFAULT :\r
- break ;\r
-\r
- case SC_MONITORPOWER :\r
- break ;\r
-\r
- case SC_CONTEXTHELP :\r
- break ;\r
-#endif /* WINVER >= 0x0400 */\r
-\r
- default:\r
-#if _DEBUG\r
- fgWarning( "Unknown wParam type 0x%x", wParam );\r
-#endif\r
- break;\r
- }\r
- }\r
-#endif /* !defined(_WIN32_WCE) */\r
-\r
- /* We need to pass the message on to the operating system as well */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
-\r
-#ifdef WM_TOUCH\r
- /* handle multi-touch messages */\r
- case WM_TOUCH:\r
- {\r
- unsigned int numInputs = (unsigned int)wParam;\r
- unsigned int i = 0;\r
- TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);\r
-\r
- if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {\r
- fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");\r
- fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");\r
- }\r
-\r
- if (!fghGetTouchInputInfo) { \r
- free( (void*)ti );\r
- break;\r
- }\r
-\r
- if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {\r
- /* Handle each contact point */\r
- for (i = 0; i < numInputs; ++i ) {\r
-\r
- POINT tp;\r
- tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);\r
- tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);\r
- ScreenToClient( hWnd, &tp );\r
-\r
- ti[i].dwID = ti[i].dwID * 2;\r
-\r
- if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {\r
- INVOKE_WCB( *window, MultiEntry, ( ti[i].dwID, GLUT_ENTERED ) );\r
- INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );\r
- } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {\r
- INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );\r
- } else if (ti[i].dwFlags & TOUCHEVENTF_UP) { \r
- INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );\r
- INVOKE_WCB( *window, MultiEntry, ( ti[i].dwID, GLUT_LEFT ) );\r
- }\r
- }\r
- }\r
- fghCloseTouchInputHandle((HTOUCHINPUT)lParam);\r
- free( (void*)ti );\r
- lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/\r
- break;\r
- }\r
-#endif\r
- default:\r
- /* Handle unhandled messages */\r
- lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
- break;\r
- }\r
-\r
- return lRet;\r
-}\r
-#endif\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 ***/