Added some error-checking (particularly w.r.t. strdup()).

Deleted numerous 4-line "padding" sequences of C-as-English trans-
literation comments.

Re-indented some (but not all) code that is using 2-space indentation.
(Most of the freeglut code seems to be using 4-space indentation.)


I did not touch the "INIT DISPLAY STRING PARSING" code, since it is
filled with a ton of over-long lines and also appears to be in more
flux than the rest of the code.  (Well, I added one error-check
to a strdup().)


git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@240 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
rkrolib 2003-10-26 05:19:02 +00:00
parent 6f7dc9821f
commit 14801c8908

View File

@ -95,72 +95,39 @@ SFG_State fgState = { { -1, -1, FALSE }, /* Position */
void fgInitialize( const char* displayName ) void fgInitialize( const char* displayName )
{ {
#if TARGET_HOST_UNIX_X11 #if TARGET_HOST_UNIX_X11
/*
* Have the display created
*/
fgDisplay.Display = XOpenDisplay( displayName ); fgDisplay.Display = XOpenDisplay( displayName );
if( fgDisplay.Display == NULL ) if( fgDisplay.Display == NULL )
{
/*
* Failed to open a display. That's no good.
*/
fgError( "failed to open display '%s'", XDisplayName( displayName ) ); fgError( "failed to open display '%s'", XDisplayName( displayName ) );
}
/*
* Check for the OpenGL GLX extension availability:
*/
if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) ) if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) )
{ fgError( "OpenGL GLX extension not supported by display '%s'",
/* XDisplayName( displayName ) );
* GLX extensions have not been found...
*/
fgError( "OpenGL GLX extension not supported by display '%s'", XDisplayName( displayName ) );
}
/*
* Grab the default screen for the display we have just opened
*/
fgDisplay.Screen = DefaultScreen( fgDisplay.Display ); fgDisplay.Screen = DefaultScreen( fgDisplay.Display );
/*
* The same applying to the root window
*/
fgDisplay.RootWindow = RootWindow( fgDisplay.RootWindow = RootWindow(
fgDisplay.Display, fgDisplay.Display,
fgDisplay.Screen fgDisplay.Screen
); );
/*
* Grab the logical screen's geometry
*/
fgDisplay.ScreenWidth = DisplayWidth( fgDisplay.ScreenWidth = DisplayWidth(
fgDisplay.Display, fgDisplay.Display,
fgDisplay.Screen fgDisplay.Screen
); );
fgDisplay.ScreenHeight = DisplayHeight( fgDisplay.ScreenHeight = DisplayHeight(
fgDisplay.Display, fgDisplay.Display,
fgDisplay.Screen fgDisplay.Screen
); );
/*
* Grab the physical screen's geometry
*/
fgDisplay.ScreenWidthMM = DisplayWidthMM( fgDisplay.ScreenWidthMM = DisplayWidthMM(
fgDisplay.Display, fgDisplay.Display,
fgDisplay.Screen fgDisplay.Screen
); );
fgDisplay.ScreenHeightMM = DisplayHeightMM( fgDisplay.ScreenHeightMM = DisplayHeightMM(
fgDisplay.Display, fgDisplay.Display,
fgDisplay.Screen fgDisplay.Screen
); );
/*
* The display's connection number
*/
fgDisplay.Connection = ConnectionNumber( fgDisplay.Display ); fgDisplay.Connection = ConnectionNumber( fgDisplay.Display );
/* /*
@ -182,19 +149,9 @@ void fgInitialize( const char* displayName )
*/ */
fgDisplay.Instance = GetModuleHandle( NULL ); fgDisplay.Instance = GetModuleHandle( NULL );
/*
* Check if the freeglut window class has been registered before...
*/
atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc ); atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
/*
* ...nope, it has not, and we have to do it right now:
*/
if( atom == 0 ) if( atom == 0 )
{ {
/*
* Make sure the unitialized fields are reset to zero
*/
ZeroMemory( &wc, sizeof(WNDCLASS) ); ZeroMemory( &wc, sizeof(WNDCLASS) );
/* /*
@ -228,29 +185,17 @@ void fgInitialize( const char* displayName )
fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN ); fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
{ {
/*
* Checking the display's size in millimeters isn't too hard, too:
*/
HWND desktop = GetDesktopWindow(); HWND desktop = GetDesktopWindow();
HDC context = GetDC( desktop ); HDC context = GetDC( desktop );
/*
* Grab the appropriate values now (HORZSIZE and VERTSIZE respectably):
*/
fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE ); fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE ); fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
/*
* Whoops, forgot to release the device context :)
*/
ReleaseDC( desktop, context ); ReleaseDC( desktop, context );
} }
#endif #endif
/*
* Have the joystick device initialized now
*/
fgJoystickInit( 0 ); fgJoystickInit( 0 );
} }
@ -261,12 +206,10 @@ void fgDeinitialize( void )
{ {
SFG_Timer *timer; SFG_Timer *timer;
/*
* Check if initialization has been performed before
*/
if( !fgState.Time.Set ) if( !fgState.Time.Set )
{ {
fgWarning( "fgDeinitialize(): fgState.Timer is null => no valid initialization has been performed" ); fgWarning( "fgDeinitialize(): fgState.Timer is null => "
"no valid initialization has been performed" );
return; return;
} }
@ -279,29 +222,16 @@ void fgDeinitialize( void )
fgStructure.MenuContext = NULL ; fgStructure.MenuContext = NULL ;
} }
/*
* Perform the freeglut structure deinitialization
*/
fgDestroyStructure(); fgDestroyStructure();
/*
* Delete all the timers and their storage list
*/
while ( (timer = (SFG_Timer *)fgState.Timers.First) != NULL ) while ( (timer = (SFG_Timer *)fgState.Timers.First) != NULL )
{ {
fgListRemove ( &fgState.Timers, &timer->Node ) ; fgListRemove ( &fgState.Timers, &timer->Node ) ;
free ( timer ) ; free ( timer ) ;
} }
/*
* Deinitialize the joystick device
*/
fgJoystickClose(); fgJoystickClose();
/*
* Reset the state structure
*/
fgState.Position.X = -1 ; fgState.Position.X = -1 ;
fgState.Position.Y = -1 ; fgState.Position.Y = -1 ;
fgState.Position.Use = FALSE ; fgState.Position.Use = FALSE ;
@ -310,9 +240,6 @@ void fgDeinitialize( void )
fgState.Size.Y = 300 ; fgState.Size.Y = 300 ;
fgState.Size.Use = TRUE ; fgState.Size.Use = TRUE ;
/*
* The default display mode to be used
*/
fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH; fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH;
fgState.ForceDirectContext = FALSE; fgState.ForceDirectContext = FALSE;
@ -324,14 +251,8 @@ void fgDeinitialize( void )
fgState.ActionOnWindowClose = GLUT_ACTION_EXIT ; fgState.ActionOnWindowClose = GLUT_ACTION_EXIT ;
fgState.ExecState = GLUT_EXEC_STATE_INIT ; fgState.ExecState = GLUT_EXEC_STATE_INIT ;
/*
* Assume we want to ignore the automatic key repeat
*/
fgState.IgnoreKeyRepeat = TRUE; fgState.IgnoreKeyRepeat = TRUE;
/*
* Set the default game mode settings
*/
fgState.GameModeSize.X = 640; fgState.GameModeSize.X = 640;
fgState.GameModeSize.Y = 480; fgState.GameModeSize.Y = 480;
fgState.GameModeDepth = 16; fgState.GameModeDepth = 16;
@ -344,14 +265,10 @@ void fgDeinitialize( void )
fgState.MenuStateCallback = (FGCBmenuState)NULL ; fgState.MenuStateCallback = (FGCBmenuState)NULL ;
fgState.MenuStatusCallback = (FGCBmenuStatus)NULL ; fgState.MenuStatusCallback = (FGCBmenuStatus)NULL ;
/*
* FPS display
*/
fgState.SwapCount = 0; fgState.SwapCount = 0;
fgState.SwapTime = 0; fgState.SwapTime = 0;
fgState.FPSInterval = 0; fgState.FPSInterval = 0;
#if TARGET_HOST_UNIX_X11 #if TARGET_HOST_UNIX_X11
/* /*
@ -383,25 +300,14 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
fgState.ProgramName = strdup (*argv); fgState.ProgramName = strdup (*argv);
else else
fgState.ProgramName = strdup (""); fgState.ProgramName = strdup ("");
/* if (!fgState.ProgramName)
* Do not allow multiple initialization of the library fgError ("Could not allocate space for the program's name.");
*/
if( fgState.Time.Set ) if( fgState.Time.Set )
{ fgError( "illegal glutInit() reinitialization attemp" );
/*
* We can't have multiple initialization performed
*/
fgError( "illegal glutInit() reinitialization attemp" );
}
/*
* Have the internal freeglut structure initialized now
*/
fgCreateStructure(); fgCreateStructure();
/*
* Remember the function's call time
*/
#if TARGET_HOST_UNIX_X11 #if TARGET_HOST_UNIX_X11
gettimeofday(&fgState.Time.Value, NULL); gettimeofday(&fgState.Time.Value, NULL);
#elif TARGET_HOST_WIN32 #elif TARGET_HOST_WIN32
@ -420,83 +326,53 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
} }
} }
/*
* Grab the environment variable indicating the X display to use.
* This is harmless under Win32, so let's let it stay here...
*/
#if TARGET_HOST_WIN32 #if TARGET_HOST_WIN32
if ( !getenv ( "DISPLAY" ) ) if ( !getenv ( "DISPLAY" ) )
displayName = strdup ( "" ) ; displayName = strdup ( "" ) ;
else else
#endif #endif
displayName = strdup( getenv( "DISPLAY" ) ); displayName = strdup( getenv( "DISPLAY" ) );
if (!displayName)
fgError ("Could not allocate space for display name.");
/*
* Have the program arguments parsed.
*/
for( i=1; i<argc; i++ ) for( i=1; i<argc; i++ )
{ {
/*
* The X display name settings
*/
if( strcmp( argv[ i ], "-display" ) == 0 ) if( strcmp( argv[ i ], "-display" ) == 0 )
{ {
/*
* Check for possible lack of the next argument
*/
if( ++i >= argc ) if( ++i >= argc )
fgError( "-display parameter must be followed by display name" ); fgError( "-display parameter must be followed by display name" );
/* if( displayName )
* Release the previous display name (the one from app's environment)
*/
free( displayName ); free( displayName );
/*
* Make a working copy of the name for us to use
*/
displayName = strdup( argv[ i ] ); displayName = strdup( argv[ i ] );
if (!displayName)
fgError( "Could not allocate space for display name (%s)",
argv [i]);
/*
* Have both arguments removed
*/
argv[ i - 1 ] = NULL; argv[ i - 1 ] = NULL;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc) -= 2; (*pargc) -= 2;
} }
/*
* The geometry settings
*/
else if( strcmp( argv[ i ], "-geometry" ) == 0 ) else if( strcmp( argv[ i ], "-geometry" ) == 0 )
{ {
int result, x, y; int result, x, y;
unsigned int w, h; unsigned int w, h;
/*
* Again, check if there is at least one more argument
*/
if ( ++i >= argc ) if ( ++i >= argc )
fgError( "-geometry parameter must be followed by window geometry settings" ); fgError( "-geometry parameter must be followed by window "
"geometry settings" );
/*
* Otherwise scan the geometry settings...
*/
result = sscanf ( argv[i], "%dx%d+%d+%d", &x, &y, &w, &h ); result = sscanf ( argv[i], "%dx%d+%d+%d", &x, &y, &w, &h );
/*
* Check what we have been supplied with...
*/
if ( result > 3 ) if ( result > 3 )
fgState.Size.Y = h ; fgState.Size.Y = h;
if ( result > 2 ) if ( result > 2 )
fgState.Size.X = w ; fgState.Size.X = w;
if( result > 1 ) if( result > 1 )
{ {
if( y < 0 ) if( y < 0 )
fgState.Position.Y = fgDisplay.ScreenHeight + y - fgState.Size.Y; fgState.Position.Y =
fgDisplay.ScreenHeight + y - fgState.Size.Y;
else else
fgState.Position.Y = y; fgState.Position.Y = y;
} }
@ -504,88 +380,67 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
if( result > 0 ) if( result > 0 )
{ {
if( x < 0 ) if( x < 0 )
fgState.Position.X = fgDisplay.ScreenWidth + x - fgState.Size.X; fgState.Position.X =
fgDisplay.ScreenWidth + x - fgState.Size.X;
else else
fgState.Position.X = x; fgState.Position.X = x;
} }
/*
* Have both arguments removed
*/
argv[ i - 1 ] = NULL; argv[ i - 1 ] = NULL;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc) -= 2; (*pargc) -= 2;
} }
/*
* The direct/indirect OpenGL contexts settings
*/
else if( strcmp( argv[ i ], "-direct" ) == 0) else if( strcmp( argv[ i ], "-direct" ) == 0)
{ {
/*
* We try to force direct rendering...
*/
if( fgState.TryDirectContext == FALSE ) if( fgState.TryDirectContext == FALSE )
fgError( "parameters ambiguity, -direct and -indirect cannot be both specified" ); fgError( "parameters ambiguity, -direct and -indirect "
"cannot be both specified" );
fgState.ForceDirectContext = TRUE; fgState.ForceDirectContext = TRUE;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc)--; (*pargc)--;
} }
else if( strcmp( argv[ i ], "-indirect" ) == 0 ) else if( strcmp( argv[ i ], "-indirect" ) == 0 )
{ {
/*
* We try to force indirect rendering...
*/
if( fgState.ForceDirectContext == TRUE ) if( fgState.ForceDirectContext == TRUE )
fgError( "parameters ambiguity, -direct and -indirect cannot be both specified" ); fgError( "parameters ambiguity, -direct and -indirect "
"cannot be both specified" );
fgState.TryDirectContext = FALSE; fgState.TryDirectContext = FALSE;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc)--; (*pargc)--;
} }
/*
* The '-iconic' parameter makes all new top-level
* windows created in iconified state...
*/
else if( strcmp( argv[ i ], "-iconic" ) == 0 ) else if( strcmp( argv[ i ], "-iconic" ) == 0 )
{ {
fgState.ForceIconic = TRUE; fgState.ForceIconic = TRUE;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc)--; (*pargc)--;
} }
/*
* The '-gldebug' option activates some OpenGL state debugging features
*/
else if( strcmp( argv[ i ], "-gldebug" ) == 0 ) else if( strcmp( argv[ i ], "-gldebug" ) == 0 )
{ {
fgState.GLDebugSwitch = TRUE; fgState.GLDebugSwitch = TRUE;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc)--; (*pargc)--;
} }
/*
* The '-sync' option activates X protocol synchronization (for debugging purposes)
*/
else if( strcmp( argv[ i ], "-sync" ) == 0 ) else if( strcmp( argv[ i ], "-sync" ) == 0 )
{ {
fgState.XSyncSwitch = TRUE; fgState.XSyncSwitch = TRUE;
argv[ i ] = NULL; argv[ i ] = NULL;
(* pargc)--; (*pargc)--;
} }
} }
/* /*
* Have the arguments list compacted now * Compact {argv}.
*/ */
j = 2 ; j = 2 ;
for( i = 1; i < *pargc; i++, j++ ) for( i = 1; i < *pargc; i++, j++ )
{ {
if( argv[ i ] == NULL ) if( argv[ i ] == NULL )
{ {
while ( argv[j] == NULL ) j++ ; /* Guaranteed to end because there are "*pargc" arguments left */ /* Guaranteed to end because there are "*pargc" arguments left */
while ( argv[j] == NULL )
j++;
argv[i] = argv[j] ; argv[i] = argv[j] ;
} }
} }
@ -594,6 +449,12 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
* Have the display created now. As I am too lazy to implement * Have the display created now. As I am too lazy to implement
* the program arguments parsing, we will have the DISPLAY * the program arguments parsing, we will have the DISPLAY
* environment variable used for opening the X display: * environment variable used for opening the X display:
*
* XXX The above comment is rather unclear. We have just
* XXX completed parsing of the program arguments for GLUT
* XXX parameters. We obviously canNOT parse the application-
* XXX specific parameters. Can someone re-write the above
* XXX more clearly?
*/ */
fgInitialize( displayName ); fgInitialize( displayName );
@ -606,9 +467,7 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
if( fgState.Size.X < 0 || fgState.Size.Y < 0 ) if( fgState.Size.X < 0 || fgState.Size.Y < 0 )
fgState.Size.Use = FALSE; fgState.Size.Use = FALSE;
/* if( displayName )
* Do not forget about releasing the display name string
*/
free( displayName ); free( displayName );
} }
@ -617,23 +476,15 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
*/ */
void FGAPIENTRY glutInitWindowPosition( int x, int y ) void FGAPIENTRY glutInitWindowPosition( int x, int y )
{ {
/*
* The settings can be disables when both coordinates are negative
*/
if( (x >= 0) && (y >= 0) ) if( (x >= 0) && (y >= 0) )
{ {
/*
* We want to specify the initial position of each of the windows
*/
fgState.Position.X = x; fgState.Position.X = x;
fgState.Position.Y = y; fgState.Position.Y = y;
fgState.Position.Use = TRUE; fgState.Position.Use = TRUE;
} }
else else
{ {
/*
* The initial position of each of the windows is specified by the wm
*/
fgState.Position.X = -1; fgState.Position.X = -1;
fgState.Position.Y = -1; fgState.Position.Y = -1;
fgState.Position.Use = FALSE; fgState.Position.Use = FALSE;
@ -645,23 +496,14 @@ void FGAPIENTRY glutInitWindowPosition( int x, int y )
*/ */
void FGAPIENTRY glutInitWindowSize( int width, int height ) void FGAPIENTRY glutInitWindowSize( int width, int height )
{ {
/*
* The settings can be disables when both values are negative
*/
if( (width > 0) && (height > 0) ) if( (width > 0) && (height > 0) )
{ {
/*
* We want to specify the initial size of each of the windows
*/
fgState.Size.X = width; fgState.Size.X = width;
fgState.Size.Y = height; fgState.Size.Y = height;
fgState.Size.Use = TRUE; fgState.Size.Use = TRUE;
} }
else else
{ {
/*
* The initial size of each of the windows is specified by the wm (officially this is an error condition)
*/
fgState.Size.X = -1; fgState.Size.X = -1;
fgState.Size.Y = -1; fgState.Size.Y = -1;
fgState.Size.Use = FALSE; fgState.Size.Use = FALSE;
@ -879,7 +721,11 @@ void FGAPIENTRY glutInitDisplayString( char* displayMode )
* Grab the value string that must follow the comparison operator... * Grab the value string that must follow the comparison operator...
*/ */
if( comparison != FG_NONE && i < (gint) strlen( scanner->value.v_identifier ) ) if( comparison != FG_NONE && i < (gint) strlen( scanner->value.v_identifier ) )
{
valueString = strdup( scanner->value.v_identifier + i ); valueString = strdup( scanner->value.v_identifier + i );
if (!valueString)
fgError ("Could not allocate an internal string.");
}
/* /*
* If there was a value string, convert it to integer... * If there was a value string, convert it to integer...