/*
 * Copyright (C) 2001-2005, J. Dean Brederson.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
 * or visit http://www.gnu.org/copyleft/gpl.html.
 *
 * Contributor(s):
 *   J. Dean Brederson (jdb@cs.utah.edu)
 *
 * The OpenRM project is located at http://openrm.sourceforge.net/.
 */
/*
 * $Id: glxinfo.c,v 1.7 2005/06/08 18:34:16 wes Exp $
 * $Revision: 1.7 $
 * $Name: OpenRM-1-6-0-2-c $
 * $Log: glxinfo.c,v $
 * Revision 1.7  2005/06/08 18:34:16  wes
 * Code cleanup to eliminate compiler warnings.
 *
 * Revision 1.6  2003/04/13 18:13:23  wes
 * Updated copyright dates.
 *
 * Revision 1.5  2003/02/02 17:51:34  wes
 * Minor tweak to RMpipe init sequence.
 *
 * Revision 1.4  2003/01/27 05:07:07  wes
 * Changes to RMpipe initialization sequence APIs. Tested for GLX, but not WGL.
 *
 * Revision 1.3  2003/01/16 22:22:45  wes
 * Updated all source files to reflect new organization of header files: all
 * headers that were formerly located in include/rmaux, include/rmv and
 * include/rmi are now located in include/rm.
 *
 * Revision 1.2  2002/06/17 00:37:16  wes
 * Updated copyright line.
 *
 * Revision 1.1  2001/05/26 14:24:22  wes
 * Initial entry.
 *
 *
 */

/*
 * file:     glxinfo.c
 * 
 * purpose:  simple GL/GLU/GLX info report using OpenRM
 *
 * authors:  J. Dean Brederson (jdb@cs.utah.edu)
 *
 * created:  09/28/00 jdb
 * modified: 02/11/01 jdb
 *
 * notes:    since OpenGL and friends typically return a static string of info,
 *	     this must be copied to a buffer before parsing with strtok() since
 *	     strtok() modifies the string!
 * 
 * TODO: can we do this offscreen, so no visible window pops up?
 */

#include <string.h>
#include <rm/rm.h>   
#include <rm/rmaux.h>


/*
 * function: printExtensions
 *
 * purpose:  parses GL/GLU/GLX extension string into formatted column output
 */         
void
printExtensions (char *s)
{
    char * ext;
    char   buffer[2048];	/* should be sufficient buffer size for current OpenGL implementations */

    if (strlen(s) > 0)
    {
	memcpy (buffer, s, strlen(s) + 1);
	printf ("%s\n", strtok (buffer, " "));
	while ((ext = strtok (NULL, " ")) != NULL)
	    printf ("               %s\n", ext);
    }
    else
	printf ("no extensions defined.\n");
    return;
}

/*
 * GLINFO
 */
int
main ()
{
    RMpipe *  pipe = NULL;
    Display * dpy;

    /* setup minimal OpenGL context using OpenRM */
    rmInit ();
#ifdef RM_WIN
    pipe = rmPipeNew(RM_PIPE_WGL);
#endif
#ifdef RM_X
    pipe = rmPipeNew(RM_PIPE_GLX);
#endif
    rmPipeSetWindow (pipe, rmauxCreateXWindow(pipe, (Window)NULL, 0, 0, 1, 1, NULL, NULL, RM_TRUE), 1, 1);
    rmPipeMakeCurrent (pipe);

    /* GL info */
    printf ("\nOpenGL Details:\n");
    printf ("   vendor:     %s\n", glGetString (GL_VENDOR));
    printf ("   version:    %s\n", glGetString (GL_VERSION));
    printf ("   renderer:   %s\n", glGetString (GL_RENDERER));
    printf ("   extensions: ");
    printExtensions ((char *)glGetString (GL_EXTENSIONS));

    /* GLU info */
    printf ("\nGLU Details:\n");
    printf ("   version:    %s\n", (char *)gluGetString (GLU_VERSION));
    printf ("   extensions: ");
    printExtensions ((char *)gluGetString (GLU_EXTENSIONS));

    /* GLX server info */
    dpy = rmxPipeGetDisplay (pipe);
    printf ("\nGLX Server Details:\n");
    printf ("   vendor:     %s\n", glXQueryServerString (dpy, 0, GLX_VENDOR));
    printf ("   version:    %s\n", glXQueryServerString (dpy, 0, GLX_VERSION));
    printf ("   extensions: ");
    printExtensions ((char *)glXQueryServerString (dpy, 0, GLX_EXTENSIONS));

    /* printf (" extensions 2: "); */
    /* printExtensions ((char *)glXQueryExtensionsString (dpy, 0)); */

    /* GLX client info */
    printf ("\nGLX Client Details:\n");
    printf ("   vendor:     %s\n", glXGetClientString (dpy, GLX_VENDOR));
    printf ("   version:    %s\n", glXGetClientString (dpy, GLX_VERSION));
    printf ("   extensions: ");
    printExtensions ((char *)glXGetClientString (dpy, GLX_EXTENSIONS));
    printf ("\n");

    /* c'est tout! */
    rmFinish ();
    exit (1);
}
