From 43db91d700145ae03ae7b83308a869aef5bd6eb0 Mon Sep 17 00:00:00 2001 From: "John F. Fay" Date: Mon, 1 Nov 2010 04:13:54 +0000 Subject: [PATCH] Addressing feature request 2116152 -- adding an fgError exit callback routine -- patch from Chris Marshall in e-mail dated 10/30/2010 2:06 PM git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@877 7f0cb862-5218-0410-a997-914c9d46530a --- configure.ac | 2 +- include/GL/freeglut_ext.h | 6 +++++ progs/demos/Makefile.am | 2 +- src/freeglut_ext.c | 2 ++ src/freeglut_init.c | 24 +++++++++++++++++- src/freeglut_internal.h | 7 ++++++ src/freeglut_main.c | 59 ++++++++++++++++++++++++++++++++------------- src/freeglutdll.def | 2 ++ 8 files changed, 84 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 9ee3c67..6d1c59d 100644 --- a/configure.ac +++ b/configure.ac @@ -95,5 +95,5 @@ if test "x$enable_debug" = xyes; then fi # Generate output. -AC_CONFIG_FILES([Makefile doc/Makefile include/GL/Makefile include/Makefile progs/Makefile progs/demos/CallbackMaker/Makefile progs/demos/Fractals/Makefile progs/demos/Fractals_random/Makefile progs/demos/Lorenz/Makefile progs/demos/Makefile progs/demos/One/Makefile progs/demos/shapes/Makefile progs/demos/smooth_opengl3/Makefile progs/demos/spaceball/Makefile src/Makefile]) +AC_CONFIG_FILES([Makefile doc/Makefile include/GL/Makefile include/Makefile progs/Makefile progs/demos/CallbackMaker/Makefile progs/demos/Error/Makefile progs/demos/Fractals/Makefile progs/demos/Fractals_random/Makefile progs/demos/Lorenz/Makefile progs/demos/Makefile progs/demos/One/Makefile progs/demos/shapes/Makefile progs/demos/smooth_opengl3/Makefile progs/demos/spaceball/Makefile src/Makefile]) AC_OUTPUT diff --git a/include/GL/freeglut_ext.h b/include/GL/freeglut_ext.h index 2b9ad99..52367a6 100644 --- a/include/GL/freeglut_ext.h +++ b/include/GL/freeglut_ext.h @@ -202,6 +202,12 @@ FGAPI void FGAPIENTRY glutInitContextVersion( int majorVersion, int minorVers FGAPI void FGAPIENTRY glutInitContextFlags( int flags ); FGAPI void FGAPIENTRY glutInitContextProfile( int profile ); +/* to get the typedef for va_list */ +#include + +FGAPI void FGAPIENTRY glutInitErrorFunc( void (* vError)( const char *fmt, va_list ap ) ); +FGAPI void FGAPIENTRY glutInitWarningFunc( void (* vWarning)( const char *fmt, va_list ap ) ); + /* * GLUT API macro definitions -- the display mode definitions */ diff --git a/progs/demos/Makefile.am b/progs/demos/Makefile.am index 31c1739..511dc81 100644 --- a/progs/demos/Makefile.am +++ b/progs/demos/Makefile.am @@ -1,2 +1,2 @@ EXTRA_DIST = demos.dsw -SUBDIRS = CallbackMaker Fractals Fractals_random Lorenz One shapes smooth_opengl3 spaceball +SUBDIRS = CallbackMaker Error Fractals Fractals_random Lorenz One shapes smooth_opengl3 spaceball diff --git a/src/freeglut_ext.c b/src/freeglut_ext.c index 972ae78..9470dd7 100644 --- a/src/freeglut_ext.c +++ b/src/freeglut_ext.c @@ -196,6 +196,8 @@ static GLUTproc fghGetGLUTProcAddress( const char* procName ) CHECK_NAME(glutInitContextVersion); CHECK_NAME(glutInitContextFlags); CHECK_NAME(glutInitContextProfile); + CHECK_NAME(glutInitErrorFunc); + CHECK_NAME(glutInitWarningFunc); #undef CHECK_NAME return NULL; diff --git a/src/freeglut_init.c b/src/freeglut_init.c index 7cccc35..d113ab7 100644 --- a/src/freeglut_init.c +++ b/src/freeglut_init.c @@ -91,7 +91,9 @@ SFG_State fgState = { { -1, -1, GL_FALSE }, /* Position */ 1, /* MajorVersion */ 0, /* MajorVersion */ 0, /* ContextFlags */ - 0 /* ContextProfile */ + 0, /* ContextProfile */ + NULL, /* ErrorFunc */ + NULL /* WarningFunc */ }; @@ -1143,4 +1145,24 @@ void FGAPIENTRY glutInitContextProfile( int profile ) fgState.ContextProfile = profile; } +/* -------------- User Defined Error/Warning Handler Support -------------- */ + +/* + * Sets the user error handler (note the use of va_list for the args to the fmt) + */ +void FGAPIENTRY glutInitErrorFunc( void (* vfgError) ( const char *fmt, va_list ap ) ) +{ + /* This allows user programs to handle freeglut errors */ + fgState.ErrorFunc = vfgError; +} + +/* + * Sets the user warning handler (note the use of va_list for the args to the fmt) + */ +void FGAPIENTRY glutInitWarningFunc( void (* vfgWarning) ( const char *fmt, va_list ap ) ) +{ + /* This allows user programs to handle freeglut warnings */ + fgState.ErrorFunc = vfgWarning; +} + /*** END OF FILE ***/ diff --git a/src/freeglut_internal.h b/src/freeglut_internal.h index c3c933f..c8db7fa 100644 --- a/src/freeglut_internal.h +++ b/src/freeglut_internal.h @@ -129,6 +129,7 @@ #include #include #include +#include /* These are included based on autoconf directives. */ #ifdef HAVE_SYS_TYPES_H @@ -243,6 +244,10 @@ typedef void (* FGCBMenuStatus )( int, int, int ); /* The callback used when creating/using menus */ typedef void (* FGCBMenu )( int ); +/* The FreeGLUT error/warning handler type definition */ +typedef void (* FGError ) ( const char *fmt, va_list ap); +typedef void (* FGWarning ) ( const char *fmt, va_list ap); + /* A list structure */ typedef struct tagSFG_List SFG_List; @@ -333,6 +338,8 @@ struct tagSFG_State int MinorVersion; /* Minor OpenGL context version */ int ContextFlags; /* OpenGL context flags */ int ContextProfile; /* OpenGL context profile */ + FGError ErrorFunc; /* User defined error handler */ + FGWarning WarningFunc; /* User defined warning handler */ }; /* The structure used by display initialization in freeglut_init.c */ diff --git a/src/freeglut_main.c b/src/freeglut_main.c index aec6a1d..3926b38 100644 --- a/src/freeglut_main.c +++ b/src/freeglut_main.c @@ -338,37 +338,62 @@ void fgError( const char *fmt, ... ) { va_list ap; - va_start( ap, fmt ); + if (fgState.ErrorFunc) { - fprintf( stderr, "freeglut "); - if( fgState.ProgramName ) - fprintf( stderr, "(%s): ", fgState.ProgramName ); - VFPRINTF( stderr, fmt, ap ); - fprintf( stderr, "\n" ); + va_start( ap, fmt ); - va_end( ap ); + /* call user set error handler here */ + fgState.ErrorFunc(fmt, ap); - if ( fgState.Initialised ) - fgDeinitialize (); + va_end( ap ); - exit( 1 ); + } 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; - va_start( ap, fmt ); + if (fgState.WarningFunc) { + + va_start( ap, fmt ); - fprintf( stderr, "freeglut "); - if( fgState.ProgramName ) - fprintf( stderr, "(%s): ", fgState.ProgramName ); - VFPRINTF( stderr, fmt, ap ); - fprintf( stderr, "\n" ); + /* call user set warning handler here */ + fgState.WarningFunc(fmt, ap); - va_end( 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. * diff --git a/src/freeglutdll.def b/src/freeglutdll.def index 724f0be..c538c0d 100644 --- a/src/freeglutdll.def +++ b/src/freeglutdll.def @@ -143,6 +143,8 @@ EXPORTS glutInitContextFlags glutInitContextVersion glutInitContextProfile + glutInitErrorFunc + glutInitWarningFunc __glutInitWithExit __glutCreateWindowWithExit __glutCreateMenuWithExit -- 1.7.10.4