#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/utsname.h>
#include <math.h>

/*
 * Kludge to get access to the smt ptr in the display struct which is not
 * normally visible to clients. It must be defined before including Xlib.h.
 * This is the only reliable way for us to tell if we are running with SMT or
 * not. Its ugly, but it works, as long as know your library is built with
 * SMT support.
 */
#define SMT 1
#include <X11/Xlibint.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glx.h>
#include <X11/Xatom.h>

#include "Env.h"
#include <malloc.h>
#include "aux.h"

static char *GetShortVendorName(const char *input);
static char *GetHostVendor(void);
static char *GetHostOperatingSystem(void);
static char *GetHostOperatingSystemRelease(void);
static char *GetHostName(void);
static char *GetOpenGLClientVendor(void);
static char *GetOpenGLClientVersion(void);
static char *GetOpenGLClientExtensions(void);

static int GetOpenGLBufferConfig(Display *dpy, XVisualInfo *visInfo,
                                 EnvironmentInfo *info);
static int GetXEnvironInfo(EnvironmentInfo *info);

/******************************************************************************
 * 
 * StringSearch - search for pattern in string
 *  
 * Description:
 *  StringSearch returns a pointer to the first occurance of pattern found in
 *  the subject or NULL if the pattern is not found. It implements the
 *  Knuth-Morris-Pratt pattern matching algorithm. If m is the length of the
 *  pattern and n is the length of the subject, the complexity of the KMP
 *  algorithm is (m+n), much better than the (m*n) complexity of the naive
 *  nested loop algorithm. - John Dennis
 *  
 * Returns:
 *  pointer to the first occurance of pattern found the subject or NULL
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  None
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Thu Aug  4 15:49:59 1994
 *    Initial Release
 *
 *****************************************************************************/
char *
StringSearch(char *subject, char *pattern)
{
#define MAX_FLINK (256)
  int *flink, *dynamicFlink = NULL, staticFlink[MAX_FLINK];
  int patternLen = strlen(pattern);
  int subjectLen = strlen(subject);
  int found = 0;
  int i,j;

  if (patternLen > MAX_FLINK) {      /* -1 for NULL terminator */
    dynamicFlink = malloc(patternLen * sizeof(int));
    if (dynamicFlink == NULL) {
      fprintf(stderr, "malloc failure, line %d, file: %s, exiting...\n",
	      __LINE__, __FILE__);
      exit(1);
    }
    flink = dynamicFlink;
  }
  else {
    flink = staticFlink;
  }


  /* Step 1: Constuct Flowchart */
  flink[0] = -1;		       /* -1 == read next char */
  for(i = 1; i < patternLen; i++) {
    j = flink[i-1];
    while((j >= 0) && (pattern[j] != pattern[i-1])) {
      j = flink[j];
    }
    flink[i] = j+1;
  }

  /* Step 2: Scan Algorithm */
  for (i = j = 0; i < subjectLen; i++, j++) {
    while((j >= 0) && (pattern[j] != subject[i])) {
      j = flink[j];
    }
    if (j == patternLen-1) {
      found = 1;
      goto exit;
    }
  }

 exit:
  if (dynamicFlink != NULL) free(dynamicFlink);
  if (!found)
    return(NULL);
  else
    return(&subject[i-patternLen+1]);
#undef MAX_FLINK
}


/******************************************************************************
 *
 * GetShortVendorName - return short vendor string
 *  
 * Description:
 *  some vendor strings are verbose. This function will return a shortest
 *  vendor name it can given an arbitrary vendor string. The returned string
 *  is allocated with malloc, it should be freed when no longer in use. The
 *  input string is not freed or modified by this function.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetShortVendorName(const char *input)
{
  int i;
  char *s1 = NULL;              /* s1 is temp work string */
  char *s2 = NULL;              /* s2 is string to return */

  /* duplicate input string so that we can modify it */
  s1 = strdup(input);
  /* upcase string */
  for (i = 0; s1[i]; i++)
    if (islower(s1[i])) s1[i] = toupper(s1[i]);

  /* return a short name if possible, some vendor names are verbose */
  if (StringSearch(s1, "DEC") ||
      StringSearch(s1, "DECWINDOWS") ||
      StringSearch(s1, "Digital Equipment Corporation") ||
      StringSearch(s1, "DigitalEquipmentCorporation"))
    s2 = strdup("DEC");
  else if (StringSearch(s1, "SGI") ||
	   StringSearch(s1, "SILCON GRAPHICS"))
    s2 = strdup("SGI");
  else if (StringSearch(s1, "IBM") ||
	   StringSearch(s1, "INTERNATIONAL BUSINESS MACHINES"))
    s2 = strdup("IBM");
  else if (StringSearch(s1, "SUN") ||
	   StringSearch(s1, "SUN MICROSYSTEMS"))
    s2 = strdup("SUN");
  else
    s2 = strdup(s1);

  free(s1);
  return(s2);
}


/******************************************************************************
 *
 * GetHostVendor - return string naming the system vendor
 *  
 * Description:
 *  return the name of the system vendor as a string. This is to identify
 *  manufacturer of the system. The string is allocated with malloc, it should
 *  be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetHostVendor(void)
{
  return(strdup("SUN"));
}



/******************************************************************************
 *
 * GetHostOperatingSystem - return string naming the host CPU operating system
 *  
 * Description:
 *  return the type of the host operating system as a string. This is to
 *  identify what type of operating system this program is running under. The
 *  string is allocated with malloc, it should be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetHostOperatingSystem(void)
{
  struct utsname uName;
  int status;

  status = uname(&uName);
  if (status < 0) {
    return(strdup("unknown"));
  }
  else {
    return(strdup(uName.sysname));
  }
}


/******************************************************************************
 *
 * GetHostOperatingSystemRelease - return string naming operating system release
 *  
 * Description:
 *  return the release/version of the host operating system as a string. This
 *  is to identify what version of the operating system this program is
 *  running under. The string is allocated with malloc, it should be freed
 *  when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetHostOperatingSystemRelease(void)
{
  struct utsname uName;
  int status;
  int releaseLen, versionLen;
  char *s;

  status = uname(&uName);
  if (status < 0) {
    return(strdup("unknown"));
  }
  else {
    releaseLen = strlen(uName.release);
    versionLen = strlen(uName.version);
    /* 1 extra char for space, 1 extra char for null terminator */
    s = malloc(releaseLen + versionLen + 2);
    *s = 0;
    strcpy(s, uName.release);
    strcat(s, " ");
    strcat(s, uName.version);
    return(s);
  }
}


/******************************************************************************
 *
 * GetHostName - return string naming the host
 *  
 * Description:
 *  return the name of this host as a string. This is to identify which
 *  platform this program is running on. The string is allocated with malloc,
 *  it should be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetHostName(void)
{
  struct utsname uName;
  int status;

  status = uname(&uName);
  if (status < 0) {
    return(strdup("unknown"));
  }
  else {
    return(strdup(uName.nodename));
  }
}


/******************************************************************************
 *
 * GetDateTime - get the current month, day, year, hour, minute
 *  
 * Description:
 *  Get the current  day, month, year, hour, minute. Each is a pointer to an
 *  integer. If the pointer is NULL then no value is returned for that
 *  parameter, otherwise an assignment is made to the integer pointed to by
 *  the parameter.
 *
 *  month:  in the range [1-12] 1=January, 12=December
 *  day:    in the range [1-31]
 *  year:   as a 4 digit number, e.g. 1994
 *  hour:   in the range [0-23]
 *  min:    in the range [0-59]
 *  
 * Returns:
 *  0 for success, error code otherwise
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  return non-zero on failure
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Wed Aug 17 13:08:36 1994
 *    Initial Release
 *
 *****************************************************************************/

int
GetDateTime(int *month, int *day, int *year, int *hour, int *minute)
{
  time_t timeT;
  struct tm *timeTm;

  time(&timeT);
  timeTm = localtime(&timeT);
  if (timeTm == NULL) {
    fprintf(stderr, "Error calling localtime: %d\n", errno);
    return(errno);
  }
  if (month)  *month  = timeTm->tm_mon + 1;
  if (day)    *day    = timeTm->tm_mday;
  if (year)   *year   = timeTm->tm_year + 1900;
  if (hour)   *hour   = timeTm->tm_hour;
  if (minute) *minute = timeTm->tm_min;
  return(0);
}


/******************************************************************************
 *
 * GetOpenGLClientVendor - return string naming the client library vendor
 *  
 * Description:
 *  return the name of the vendor supplying the OpenGL client library.  The
 *  string is allocated with malloc,it should be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetOpenGLClientVendor(void)
{
    Display *dpy;

    dpy = auxXDisplay();
#if defined(GLX_VERSION_1_1)
    return(GetShortVendorName(glXGetClientString(dpy, GLX_VENDOR)));
#else
    return(strdup("unknown"));
#endif
}


/******************************************************************************
 *
 * GetOpenGLClientVersion - return string naming the client library version
 *  
 * Description:
 *  return the version of the OpenGL client library.  The string is allocated
 *  with malloc,it should be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetOpenGLClientVersion(void)
{
    Display *dpy;

    dpy = auxXDisplay();
#if defined(GLX_VERSION_1_1)
    return(strdup(glXGetClientString(dpy, GLX_VERSION)));
#else
    return(strdup("1.0"));
#endif
}


/******************************************************************************
 *
 * GetOpenGLClientExtensions - return string naming the client extensions
 *  
 * Description:
 *  return the extensions supported in the OpenGL client library.  The string
 *  is allocated with malloc,it should be freed when no longer in use.
 *  
 * Returns:
 *  pointer to allocated string
 *  
 * Side Effects:
 *  string allocation, string should be freed when no longer needed.
 *  
 * Errors:
 *  no errors, if function fails the string "unknown" is returned
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Mon Aug  8 16:30:53 1994
 *    Initial Release
 *
 *****************************************************************************/
static char *
GetOpenGLClientExtensions(void)
{
    Display *dpy;

    dpy = auxXDisplay();
#if defined(GLX_VERSION_1_1)
    return(strdup(glXGetClientString(dpy, GLX_EXTENSIONS)));
#else
    return(strdup("unknown"));
#endif
}



/******************************************************************************
 *
 * GetOpenGLBufferInfo - inquire the OpenGL buffer configuration
 *  
 * Description:
 *  Given a specfic rendering target and visual description, return the OpenGL
 *  buffer configuration. This information includes:
 *  
 * Returns:
 *  0 for success, error code otherwise
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  None
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Thu Aug 18 10:18:45 1994
 *    Initial Release
 *
 *****************************************************************************/

static int
GetOpenGLBufferConfig(Display *dpy, XVisualInfo *visInfo, EnvironmentInfo *info)
{
  BufferConfig *config = &info->bufConfig;

  glXGetConfig(dpy, visInfo, GLX_DOUBLEBUFFER, &config->doubleBuffer);
  glXGetConfig(dpy, visInfo, GLX_STEREO, &config->stereo);
  glXGetConfig(dpy, visInfo, GLX_RGBA, &config->rgba);

  if (config->rgba) {
    glXGetConfig(dpy, visInfo, GLX_RED_SIZE, &config->redSize);
    glXGetConfig(dpy, visInfo, GLX_GREEN_SIZE, &config->greenSize);
    glXGetConfig(dpy, visInfo, GLX_BLUE_SIZE, &config->blueSize);
    glXGetConfig(dpy, visInfo, GLX_ALPHA_SIZE, &config->alphaSize);

    glXGetConfig(dpy, visInfo, GLX_ACCUM_RED_SIZE, &config->accumRedSize);
    glXGetConfig(dpy, visInfo, GLX_ACCUM_GREEN_SIZE, &config->accumGreenSize);
    glXGetConfig(dpy, visInfo, GLX_ACCUM_BLUE_SIZE, &config->accumBlueSize);
    glXGetConfig(dpy, visInfo, GLX_ACCUM_ALPHA_SIZE, &config->accumAlphaSize);

    config->indexSize = -1;
  }
  else {
    config->redSize = -1;
    config->greenSize = -1;
    config->blueSize = -1;
    config->alphaSize = -1;

    config->accumRedSize = -1;
    config->accumGreenSize = -1;
    config->accumBlueSize = -1;
    config->accumAlphaSize = -1;

    glXGetConfig(dpy, visInfo, GLX_BUFFER_SIZE, &config->indexSize);
  }

  glXGetConfig(dpy, visInfo, GLX_DEPTH_SIZE, &config->depthSize);
  glXGetConfig(dpy, visInfo, GLX_STENCIL_SIZE, &config->stencilSize);
  glXGetConfig(dpy, visInfo, GLX_AUX_BUFFERS, &config->auxBuffers);
  glXGetConfig(dpy, visInfo, GLX_LEVEL, &config->level);

  config->visualId = visInfo->visualid;
#if defined(__cplusplus) || defined(c_plusplus)
  config->visualClass = visInfo->c_class;
#else
  config->visualClass = visInfo->class;
#endif

}


/******************************************************************************
 *
 * GetXEnvironInfo - Get all enironment info specfic to X Windows
 *  
 * Description:
 *  
 *  
 * Returns:
 *  0 success, error code otherwise
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  errors with bad configurations
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Thu Aug 18 16:51:10 1994
 *    Initial Release
 *
 *****************************************************************************/

static int
GetXEnvironInfo(EnvironmentInfo *info)
{
  Display *dpy;
  Window win;
  int i, status;
  int screen;
  Screen *pScreen;
  XWindowAttributes winAttr;
  XVisualInfo visualTemplate, *visInfo;
  VisualID visualId;
  int nVisuals;
  char buf[256];
  int glxMajorVersion, glxMinorVersion;
  
  dpy = auxXDisplay();
  if (!dpy) {
    fprintf(stderr, "GetXEnvironInfo: no display set\n");
    return(1);
  }

  win = auxXWindow();
  if (!win) {
    fprintf(stderr, "GetXEnvironInfo: no window set\n");
    return(1);
  }

  status = XGetWindowAttributes(dpy, win, &winAttr);
  if (!status) {
    fprintf(stderr, "Error calling XGetWindowAttributes\n");
  }

  /* XXX - should we be using aux functions to get screen number, visual id? */

  /* Find the screen number given a screen ptr. */
  pScreen = winAttr.screen;
  for (screen = 0; screen < ScreenCount(dpy); screen++) {
    if (pScreen == &dpy->screens[screen]) {
      break;
    }
  }
  if (screen >= ScreenCount(dpy)) {
    fprintf(stderr, "Error, cannot find screen\n");
  }

  /* Find the visualInfo given a visualId */
  visualId = winAttr.visual->visualid;
  visualTemplate.visualid = visualId;
  visualTemplate.screen = screen;
  visInfo = XGetVisualInfo(dpy,VisualScreenMask|VisualIDMask,
                           &visualTemplate, &nVisuals);
  if (visInfo == NULL || nVisuals != 1) {
    fprintf(stderr, "Error calling XGetVisualInfo\n");
  }
  else {
    GetOpenGLBufferConfig(dpy, visInfo, info);
  }

  info->windowWidth = winAttr.width;
  info->windowHeight = winAttr.height;
  
  info->screenNumber = screen;

  info->display = strdup(DisplayString(dpy));

  if (glXQueryVersion(dpy, &glxMajorVersion, &glxMinorVersion)) {
    sprintf(buf, "%d.%d", glxMajorVersion, glxMinorVersion);
  }
  else {
    sprintf(buf, "GLX not available");
  }
  info->glxVersion = strdup(buf);

#if defined(GLX_VERSION_1_1)
  info->glServerVendor =
    GetShortVendorName(glXQueryServerString(dpy, DefaultScreen(dpy), GLX_VENDOR));
#else
  info->glServerVendor = GetShortVendorName(ServerVendor(dpy));
#endif

#if defined(GLX_VERSION_1_1)
  info->glServerVersion = strdup(glXQueryServerString(dpy, DefaultScreen(dpy), GLX_VERSION));
#else
  info->glServerVersion = strdup(info->glxVersion);
#endif

#if defined(GLX_VERSION_1_1)
  info->glServerExtensions = strdup(glXQueryServerString(dpy, DefaultScreen(dpy), GLX_VERSION));
#else
  info->glServerExtensions = strdup("unknown");
#endif

#if defined(GLX_VERSION_1_1)
  info->glxExtensions = strdup(glXQueryExtensionsString(dpy, screen));
#else
  info->glxExtensions = strdup("unknown");
#endif

  info->screenWidth = DisplayWidth(dpy, screen);
  info->screenHeight = DisplayHeight(dpy, screen);

/*
  if (dpy->shmTrans) {
    info->sharedMemConnection = TRUE;
  }
  else {
    info->sharedMemConnection = FALSE;
  }
*/
    info->sharedMemConnection = FALSE;

  XFree((void *)visInfo);
  return(0);
}

#ifdef NOT_YET
void GetSUNHardwareInfo(EnvironmentInfo *info)
{
  char buffer[256];
  char filename[256];
  FILE *inputPipe;
  char cmd[] = "hinv";
  int status;
  int speed, numprocs;
  char proctype[256];
  char processor[256];
  char numProcs[256];
  char primaryCache[256];
  char secondaryCache[256];
  int icache, dcache, sicache, sdcache;
  int unified = 0;

  inputPipe= popen(cmd, "r");
  if (inputPipe == NULL) {
    fprintf(stderr, "could not open pipe for %s\n", cmd);
    info->hostCPU = strdup("unknown");
    info->hostModel = strdup("unknown");
    return;
  }

  while (fgets(buffer, 256, inputPipe)) {
    if (strstr(buffer, "Main memory size:")) {
      sscanf(buffer, "Main memory size: %d Mbytes\n", &info->hostMemorySize);
    } else if (strstr(buffer, "CPU:")) {
      sscanf(buffer, "CPU: MIPS %s\n", processor);
    } else if (strstr(buffer, "MHZ")) {
      sscanf(buffer, "%d %d MHZ %s\n", &numprocs, &speed, proctype);
    } else if (strstr(buffer, "Data cache size:")) {
      sscanf(buffer, "Data cache size: %d Kbytes\n", &dcache);
    } else if (strstr(buffer, "Instruction cache size:")) {
      sscanf(buffer, "Instruction cache size: %d Kbytes\n", &icache);
    } else if (strstr(buffer, "Secondary instruction cache size:")) {
      if (strstr(buffer, "Kbytes")) {
        sscanf(buffer, "Secondary instruction cache size: %d", &sicache);
      } else {
        sscanf(buffer, "Secondary instruction cache size: %d", &sicache);
	sicache *= 1024;
      }
    } else if (strstr(buffer, "Secondary data cache size:")) {
      if (strstr(buffer, "Kbytes")) {
        sscanf(buffer, "Secondary data cache size: %d", &sdcache);
      } else {
        sscanf(buffer, "Secondary data cache size: %d", &sdcache);
	sdcache *= 1024;
      }
    } else if (strstr(buffer, "Secondary unified instruction/data cache size:")) {
      unified = 1;
      if (strstr(buffer, "Kbytes")) {
        sscanf(buffer, "Secondary unified instruction/data cache size: %d", &sdcache);
      } else {
        sscanf(buffer, "Secondary unified instruction/data cache size: %d", &sdcache);
	sdcache *= 1024;
      }
    }
  }
  status = pclose(inputPipe);
  if (status != 0) {
    fprintf(stderr, "cmd \"%s\" returned error %d\n", cmd, status);
    info->hostCPU = strdup("unknown");
    info->hostCPUCount = strdup("unknown");
    info->hostModel = strdup("unknown");
    info->hostPrimaryCacheSize = strdup("unknown");
    info->hostSecondaryCacheSize = strdup("unknown");
    info->windowSystem = strdup("X11R5");
    info->driverVersion = strdup("N/A");
    return;
  }
  info->hostModel = strdup(proctype);
  sprintf(buffer, "%d MHz %s", speed, processor);
  info->hostCPU = strdup(buffer);
  sprintf(buffer, "%d", numprocs);
  info->hostCPUCount = strdup(buffer);
  sprintf(buffer, "%d/%d (D/I)", dcache, icache);
  info->hostPrimaryCacheSize = strdup(buffer);
  if (unified) {
    sprintf(buffer, "%d (D+I)", sdcache);
  } else {
    sprintf(buffer, "%d/%d (D/I)", sdcache, sicache);
  }
  info->hostSecondaryCacheSize = strdup(buffer);
  info->windowSystem = strdup("X11R5");
  info->driverVersion = strdup("N/A");
}
#else
void GetSUNHardwareInfo(EnvironmentInfo *info)
{
    info->driverVersion = strdup("N/A");
    info->hostCPU = strdup("unknown");
    info->hostCPUCount = strdup("unknown");
    info->hostMemorySize = 0;
    info->hostModel = strdup("unknown");
    info->hostPrimaryCacheSize = strdup("unknown");
    info->hostSecondaryCacheSize = strdup("unknown");
    info->windowSystem = strdup("X11R5");
}
#endif


/******************************************************************************
 *
 * GetEnvironment - return info about environment test was run in
 *  
 * Description:
 *  This function fills in an environment info record with every pertinent
 *  piece of information about the condiditons under which the test was run.
 *  
 * Returns:
 *  0 success, error code otherwise
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  errors generally only with bad configurations, too many to list
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Thu Aug 18 16:52:35 1994
 *    Initial Release
 *
 *****************************************************************************/

int
GetEnvironment(EnvironmentInfo *info)
{
  GLenum windType;

  FreeEnvironmentData(info);

  GetDateTime(&info->month, &info->day, &info->year, NULL, NULL);
  info->host = GetHostName();
  info->hostOperatingSystem = GetHostOperatingSystem();
  info->hostOperatingSystemVersion = GetHostOperatingSystemRelease();
  info->hostVendor = GetHostVendor();
  GetSUNHardwareInfo(info);
  info->glVendor = GetShortVendorName((char *)glGetString(GL_VENDOR));
  info->glVersion = strdup((char *)glGetString(GL_VERSION));
  info->glRenderer = strdup((char *)glGetString(GL_RENDERER));
  info->glExtensions = strdup((char *)glGetString(GL_EXTENSIONS));
  info->glClientVendor = GetOpenGLClientVendor();
  info->glClientVersion = GetOpenGLClientVersion();
  info->glClientExtensions = GetOpenGLClientExtensions();

#ifdef GLX_VERSION_1_1
  info->gluVersion = strdup((char *)gluGetString(GLU_VERSION));;
  info->gluExtensions = strdup((char *)gluGetString(GLU_EXTENSIONS));;
#else
  info->gluVersion = strdup("unknown");
  info->gluExtensions = strdup("unknown");
#endif

  GetXEnvironInfo(info);

  /*
   * Direct rendering is a "request", not a guarantee. We need to check the
   * direct render bit for each test because the GL context is only set up
   * in the above conditional block. If we didn't check it for each test
   * then the value for direct rendering store in the test descriptor would
   * be the "request" value, not the actual value.
   */

  windType = auxGetDisplayMode();
  if (windType & AUX_DIRECT)
    info->directRender = True;
  else
    info->directRender = False;
}



/******************************************************************************
 *
 * FreeEnvironmentData - frees all dynamic data in EnvironmentInfo struct
 *  
 * Description:
 *  Free all the dynamically allocated data in an EnvironmentInfo struct. Each
 *  pointer in the struct is assigned the value of NULL after its data has
 *  been freed. The struct itself is not freed.
 *  
 * Returns:
 *  void
 *  
 * Side Effects:
 *  free dynamically allocated memory
 *  
 * Errors:
 *  None
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Tue Sep 13 17:31:19 1994
 *    Initial Release
 *
 *****************************************************************************/

void
FreeEnvironmentData(EnvironmentInfo *info)
{
  if (info->host != NULL) free(info->host);
  if (info->hostOperatingSystem != NULL) free(info->hostOperatingSystem);
  if (info->hostOperatingSystemVersion != NULL) free(info->hostOperatingSystemVersion);
  if (info->hostVendor != NULL) free(info->hostVendor);
  if (info->hostModel != NULL) free(info->hostModel);
  if (info->hostCPU != NULL) free(info->hostCPU);
  if (info->hostCPUCount != NULL) free(info->hostCPUCount);
  if (info->hostPrimaryCacheSize != NULL) free(info->hostPrimaryCacheSize);
  if (info->hostSecondaryCacheSize != NULL) free(info->hostSecondaryCacheSize);
  if (info->windowSystem != NULL) free(info->windowSystem);
  if (info->driverVersion != NULL) free(info->driverVersion);
  if (info->glVendor != NULL) free(info->glVendor);
  if (info->glVersion != NULL) free(info->glVersion);
  if (info->glRenderer != NULL) free(info->glRenderer);
  if (info->glExtensions != NULL) free(info->glExtensions);
  if (info->glClientVendor != NULL) free(info->glClientVendor);
  if (info->glClientVersion != NULL) free(info->glClientVersion);
  if (info->glClientExtensions != NULL) free(info->glClientExtensions);
  if (info->gluVersion != NULL) free(info->gluVersion);
  if (info->gluExtensions != NULL) free(info->gluExtensions);
  if (info->display != NULL) free(info->display);
  if (info->glServerVendor != NULL) free(info->glServerVendor);
  if (info->glServerVersion != NULL) free(info->glServerVersion);
  if (info->glServerExtensions != NULL) free(info->glServerExtensions);
  if (info->glxVersion != NULL) free(info->glxVersion);
  if (info->glxExtensions != NULL) free(info->glxExtensions);
  NullEnvironmentData(info);
}


/******************************************************************************
 *
 * NullEnvironmentData - sets all dynamic data ptrs in EnvironmentInfo to NULL
 *  
 * Description:
 *  Sets all the pointers to dynamically allocated data in an EnvironmentInfo
 *  struct to NULL. The data pointed to by the pointers are not freed, use the
 *  function FreeEnvironmentData for that purpose.
 *  
 * Returns:
 *  void
 *  
 * Side Effects:
 *  None
 *  
 * Errors:
 *  None
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Tue Sep 13 17:31:19 1994
 *    Initial Release
 *
 *****************************************************************************/

void
NullEnvironmentData(EnvironmentInfo *info)
{
    info->host = NULL;
    info->hostOperatingSystem = NULL;
    info->hostOperatingSystemVersion = NULL;
    info->hostVendor = NULL;
    info->hostModel = NULL;
    info->hostCPU = NULL;
    info->hostCPUCount = NULL;
    info->hostPrimaryCacheSize = NULL;
    info->hostSecondaryCacheSize = NULL;
    info->windowSystem = NULL;
    info->driverVersion = NULL;
    info->glVendor = NULL;
    info->glVersion = NULL;
    info->glRenderer = NULL;
    info->glExtensions = NULL;
    info->glClientVendor = NULL;
    info->glClientVersion = NULL;
    info->glClientExtensions = NULL;
    info->gluVersion = NULL;
    info->gluExtensions = NULL;
    info->display = NULL;
    info->glServerVendor = NULL;
    info->glServerVersion = NULL;
    info->glServerExtensions = NULL;
    info->glxVersion = NULL;
    info->glxExtensions = NULL;
}



/******************************************************************************
 *
 * PrintEnvironment - print contents of EnvironmentInfo struct
 *  
 * Description:
 *  Print the contents of EnvironmentInfo struct. The stream parameter Points
 *  to a FILE structure specifying an open stream to which output will be
 *  written. The title parameter is a pointer to a string which will be output
 *  before any of the EnvironmentInfo data is output, The title string may be
 *  NULL in which case no title string will be written. The leader parameter
 *  is a pointer to a string which will be output before each field in the
 *  EnvironmentInfo struct. The nameWidth parameter is an integer parameter
 *  specifying how pad the name of each field. This can be used to cause the
 *  values to line up in a column. A positive value left justifies, a negative
 *  value right justifies. The suffix parameter is a pointer to a string which
 *  will be output after all of the EnvironmentInfo data is output, The suffix
 *  string may be NULL in which case no suffix string will be written.
 *  
 * Returns:
 *  void
 *  
 * Side Effects:
 *  file stream output
 *  
 * Errors:
 *  None
 *
 * Revision History:
 *  Revision 0: Author: John R. Dennis Date: Wed Sep 14 09:02:57 1994
 *    Initial Release
 *
 *****************************************************************************/

void
PrintEnvironment(FILE *stream, EnvironmentInfo *info, char *title,
                 char *leader, int nameWidth, char *suffix)
{
  if (title) fprintf(stream, "%s", title);

  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Month", info->month);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Day", info->day);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Year", info->year);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host", info->host);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Operating System", info->hostOperatingSystem);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Operating System Version", info->hostOperatingSystemVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host Vendor", info->hostVendor);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host Model", info->hostModel);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host CPU", info->hostCPU);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host CPU Count", info->hostCPUCount);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Host Memory Size (MB)", info->hostMemorySize);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host Primary Cache Size (KB)", info->hostPrimaryCacheSize);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Host Secondary Cache Size (KB)", info->hostSecondaryCacheSize);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Window System", info->windowSystem);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
          "Driver Version", info->driverVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Vendor", info->glVendor);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Version", info->glVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Extensions", info->glExtensions);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Renderer", info->glRenderer);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Client Vendor", info->glClientVendor);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Client Version", info->glClientVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Client Extensions", info->glClientExtensions);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "GLU Version", info->gluVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "GLU Extensions", info->gluExtensions);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Direct Rendering", info->directRender ? "True" : "False");
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Double Buffer", info->bufConfig.doubleBuffer ? "True" : "False");
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Stereo", info->bufConfig.stereo ? "True" : "False");
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "RGBA", info->bufConfig.rgba ? "True" : "False");
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Color Index Size", info->bufConfig.indexSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Red Size", info->bufConfig.redSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Green Size", info->bufConfig.greenSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Blue Size", info->bufConfig.blueSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Alpha Size", info->bufConfig.alphaSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Accum Red Size", info->bufConfig.accumRedSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Accum Green Size", info->bufConfig.accumGreenSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Accum Blue Size", info->bufConfig.accumBlueSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Accum Alpha Size", info->bufConfig.accumAlphaSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Depth Size", info->bufConfig.depthSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Stencil Size", info->bufConfig.stencilSize);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Auxiliary Buffer Count", info->bufConfig.auxBuffers);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Frame BufferLevel", info->bufConfig.level);
  fprintf(stream, "%s%*s%#X\n", leader, nameWidth,
         "Visual ID", info->bufConfig.visualId);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Visual Class", 
         info->bufConfig.visualClass == StaticGray  ? "StaticGray"  :
         info->bufConfig.visualClass == GrayScale   ? "GrayScale"   :
         info->bufConfig.visualClass == StaticColor ? "StaticColor" :
         info->bufConfig.visualClass == PseudoColor ? "PseudoColor" :
         info->bufConfig.visualClass == TrueColor   ? "TrueColor"   :
         info->bufConfig.visualClass == DirectColor ? "DirectColor" :
         "unknown");
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Window Width (pixels)", info->windowWidth);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Window Height (pixels)", info->windowHeight);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Screen Width (pixels)", info->screenWidth);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Screen Height (pixels)", info->screenHeight);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Display", info->display);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Server Vendor", info->glServerVendor);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Server Version", info->glServerVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "OpenGL Server Extensions", info->glServerExtensions);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "GLX Server Version", info->glxVersion);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "GLX Server Extensions", info->glxExtensions);
  fprintf(stream, "%s%*s%d\n", leader, nameWidth,
         "Screen Number", info->screenNumber);
  fprintf(stream, "%s%*s%s\n", leader, nameWidth,
         "Shared Memory Connection",
         info->sharedMemConnection ? "True" : "False");
  if (suffix) fprintf(stream, "%s", suffix);
}

