Checking in the 'build patch' from Jeremy Huddleston vintage 11/19/09 -- everything...
[freeglut] / src / freeglut_main.c
index b4cabb3..19f584e 100644 (file)
 
 #include <GL/freeglut.h>
 #include "freeglut_internal.h"
-#include <errno.h>
+#ifdef HAVE_ERRNO_H
+#    include <errno.h>
+#endif
 #include <stdarg.h>
-#if  HAVE_VPRINTF
+#ifdef  HAVE_VFPRINTF
 #    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
-#elif HAVE_DOPRNT
+#elif defined(HAVE__DOPRNT)
 #    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
 #else
 #    define VFPRINTF(s,f,a)
@@ -57,7 +59,7 @@ struct GXKeyList gxKeyList;
  * Try to get the maximum value allowed for ints, falling back to the minimum
  * guaranteed by ISO C99 if there is no suitable header.
  */
-#if HAVE_LIMITS_H
+#ifdef HAVE_LIMITS_H
 #    include <limits.h>
 #endif
 #ifndef INT_MAX
@@ -120,7 +122,7 @@ static void fghReshapeWindow ( SFG_Window *window, int width, int height )
 
         if ( window->Parent == NULL )
         {
-            if ( ! window->IsMenu && !window->State.IsGameMode )
+            if ( ! window->IsMenu && (window != fgStructure.GameModeWindow) )
             {
                 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
                 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
@@ -301,46 +303,32 @@ static void fghCheckTimers( void )
     }
 }
 
-/*
- * Elapsed Time
- */
-long fgElapsedTime( void )
-{
-    if ( fgState.Time.Set )
-    {
-#if TARGET_HOST_POSIX_X11
-        struct timeval now;
-        long elapsed;
-
-        gettimeofday( &now, NULL );
-
-        elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
-        elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
-
-        return elapsed;
+/* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
+ * 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.
+ */  
+unsigned long fgSystemTime(void) {
+#if TARGET_HOST_SOLARIS || HAVE_GETTIMEOFDAY
+    struct timeval now;
+    gettimeofday( &now, NULL );
+    return now.tv_usec/1000 + now.tv_sec*1000;
 #elif TARGET_HOST_MS_WINDOWS
 #    if defined(_WIN32_WCE)
-        return GetTickCount() - fgState.Time.Value;
+    return GetTickCount();
 #    else
-        return timeGetTime() - fgState.Time.Value;
+    return timeGetTime();
 #    endif
 #endif
-    }
-    else
-    {
-#if TARGET_HOST_POSIX_X11
-        gettimeofday( &fgState.Time.Value, NULL );
-#elif TARGET_HOST_MS_WINDOWS
-#    if defined(_WIN32_WCE)
-        fgState.Time.Value = GetTickCount();
-#    else
-        fgState.Time.Value = timeGetTime ();
-#    endif
-#endif
-        fgState.Time.Set = GL_TRUE ;
-
-        return 0 ;
-    }
+}
+  
+/*
+ * Elapsed Time
+ */
+long fgElapsedTime( void )
+{
+    return (long) (fgSystemTime() - fgState.Time);
 }
 
 /*
@@ -485,8 +473,10 @@ static void fghSleepForEvents( void )
         wait.tv_usec = (msec % 1000) * 1000;
         err = select( socket+1, &fdset, NULL, NULL, &wait );
 
+#ifdef HAVE_ERRNO_H
         if( ( -1 == err ) && ( errno != EINTR ) )
             fgWarning ( "freeglut select() error: %d", errno );
+#endif
     }
 #elif TARGET_HOST_MS_WINDOWS
     MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLINPUT );
@@ -990,6 +980,10 @@ void FGAPIENTRY glutMainLoopEvent( void )
         switch( event.type )
         {
         case ClientMessage:
+            if(fgIsSpaceballXEvent(&event)) {
+                fgSpaceballHandleXEvent(&event);
+                break;
+            }
             /* Destroy the window when the WM_DELETE_WINDOW message arrives */
             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
             {
@@ -1173,15 +1167,15 @@ void FGAPIENTRY glutMainLoopEvent( void )
              * XXX track ButtonPress/ButtonRelease events in our own
              * XXX bit-mask?
              */
-           fgState.Modifiers = fghGetXModifiers( event.xmotion.state );
+            fgState.Modifiers = fghGetXModifiers( event.xmotion.state );
             if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
                                                event.xmotion.y ) );
             } else {
                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
                                                 event.xmotion.y ) );
-           }
-           fgState.Modifiers = INVALID_MODIFIERS;
+            }
+            fgState.Modifiers = INVALID_MODIFIERS;
         }
         break;
 
@@ -1304,7 +1298,10 @@ void FGAPIENTRY glutMainLoopEvent( void )
             /* Cease processing this event if it is auto repeated */
 
             if (window->State.KeyRepeating)
+            {
+                if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
                 break;
+            }
 
             if( event.type == KeyPress )
             {
@@ -1367,9 +1364,13 @@ void FGAPIENTRY glutMainLoopEvent( void )
                     case XK_F11:    special = GLUT_KEY_F11;    break;
                     case XK_F12:    special = GLUT_KEY_F12;    break;
 
+                    case XK_KP_Left:
                     case XK_Left:   special = GLUT_KEY_LEFT;   break;
+                    case XK_KP_Right:
                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;
+                    case XK_KP_Up:
                     case XK_Up:     special = GLUT_KEY_UP;     break;
+                    case XK_KP_Down:
                     case XK_Down:   special = GLUT_KEY_DOWN;   break;
 
                     case XK_KP_Prior:
@@ -1382,6 +1383,10 @@ void FGAPIENTRY glutMainLoopEvent( void )
                     case XK_End:    special = GLUT_KEY_END;    break;
                     case XK_KP_Insert:
                     case XK_Insert: special = GLUT_KEY_INSERT; break;
+
+                    case XK_Num_Lock :  special = GLUT_KEY_NUM_LOCK;  break;
+                    case XK_KP_Begin :  special = GLUT_KEY_BEGIN;     break;
+                    case XK_KP_Delete:  special = GLUT_KEY_DELETE;    break;
                     }
 
                     /*
@@ -1403,6 +1408,10 @@ void FGAPIENTRY glutMainLoopEvent( void )
         case ReparentNotify:
             break; /* XXX Should disable this event */
 
+        /* Not handled */
+        case GravityNotify:
+            break;
+
         default:
             fgWarning ("Unknown X event type: %d\n", event.type);
             break;
@@ -1559,7 +1568,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
 {
     SFG_Window* window;
     PAINTSTRUCT ps;
-    LONG lRet = 1;
+    LRESULT lRet = 1;
 
     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
 
@@ -1620,11 +1629,22 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                     window->Window.Context =
                         wglCreateContext( window->Window.Device );
             }
+
+#if !defined(_WIN32_WCE)
+            fgNewWGLCreateContext( window );
+#endif
         }
 
         window->State.NeedToResize = GL_TRUE;
-        window->State.Width  = fgState.Size.X;
-        window->State.Height = fgState.Size.Y;
+        if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
+        {
+            SFG_Window *current_window = fgStructure.CurrentWindow;
+
+            fgSetWindow( window );
+            window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
+            window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
+            fgSetWindow( current_window );
+        }
 
         ReleaseDC( window->Window.Handle, window->Window.Device );
 
@@ -1666,12 +1686,25 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
         }
 
         break;
-#if 0
+
     case WM_SETFOCUS:
 /*        printf("WM_SETFOCUS: %p\n", window ); */
         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
+        INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
         break;
 
+    case WM_KILLFOCUS:
+/*        printf("WM_KILLFOCUS: %p\n", window ); */
+        lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
+        INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
+
+        if( window->IsMenu &&
+            window->ActiveMenu && window->ActiveMenu->IsActive )
+            fgUpdateMenuHighlight( window->ActiveMenu );
+
+        break;
+
+#if 0
     case WM_ACTIVATE:
         if (LOWORD(wParam) != WA_INACTIVE)
         {
@@ -1717,20 +1750,6 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
          */
         return 0;
 
-    /* XXX For a future patch:  we need a mouse entry event.  Unfortunately Windows
-     * XXX doesn't give us one, so we will probably need a "MouseInWindow" flag in
-     * XXX the SFG_Window structure.  Set it to true to begin with and then have the
-     * XXX WM_MOUSELEAVE code set it to false.  Then when we get a WM_MOUSEMOVE event,
-     * XXX if the flag is false we invoke the Entry callback and set the flag to true.
-     */
-    case 0x02a2:  /* This is the message we get when the mouse is leaving the window */
-        if( window->IsMenu &&
-            window->ActiveMenu && window->ActiveMenu->IsActive )
-            fgUpdateMenuHighlight( window->ActiveMenu );
-
-        INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
-        break ;
-
     case WM_MOUSEMOVE:
     {
 #if defined(_WIN32_WCE)
@@ -2092,7 +2111,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
 
             GetKeyboardState( state );
 
-            if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
+            if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
                 wParam=code[ 0 ];
 
             INVOKE_WCB( *window, KeyboardUp,
@@ -2240,6 +2259,17 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
             case SC_HOTKEY     :
                 break ;
 
+#if(WINVER >= 0x0400)
+            case SC_DEFAULT    :
+                break ;
+
+            case SC_MONITORPOWER    :
+                break ;
+
+            case SC_CONTEXTHELP    :
+                break ;
+#endif /* WINVER >= 0x0400 */
+
             default:
 #if _DEBUG
                 fgWarning( "Unknown wParam type 0x%x", wParam );