Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

glut_init.c

Go to the documentation of this file.
00001 
00002 /* Copyright (c) Mark J. Kilgard, 1994. */
00003 
00004 /* This program is freely distributable without licensing fees
00005    and is provided without guarantee or warrantee expressed or
00006    implied. This program is -not- in the public domain. */
00007 
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <stdio.h>
00011 #include <X11/Xlib.h>
00012 
00013 #include <GL/glut.h>
00014 #include "glutint.h"
00015 
00016 /* GLUT inter-file variables */
00017 char *__glutProgramName = NULL;
00018 int __glutArgc = 0;
00019 char **__glutArgv = NULL;
00020 char *__glutGeometry = NULL;
00021 Display *__glutDisplay = NULL;
00022 int __glutScreen;
00023 Window __glutRoot;
00024 int __glutScreenHeight;
00025 int __glutScreenWidth;
00026 GLboolean __glutIconic = GL_FALSE;
00027 GLboolean __glutDebug = GL_FALSE;
00028 unsigned int __glutDisplayMode =
00029   GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH;
00030 int __glutConnectionFD;
00031 XSizeHints __glutSizeHints = {0};
00032 int __glutInitWidth = 300, __glutInitHeight = 300;
00033 int __glutInitX = -1, __glutInitY = -1;
00034 GLboolean __glutForceDirect = GL_FALSE,
00035   __glutTryDirect = GL_TRUE;
00036 Atom __glutWMDeleteWindow;
00037 
00038 static Bool synchronize = False;
00039 
00040 #if defined(__vms)
00041 char *
00042 strdup(const char *string)
00043 {
00044   char *new;
00045 
00046   new = malloc(strlen(string) + 1);
00047   if (new == NULL)
00048     return NULL;
00049   strcpy(new, string);
00050   return new;
00051 }
00052 #endif
00053 
00054 void
00055 __glutOpenXConnection(char *display)
00056 {
00057   int errorBase, eventBase;
00058 
00059   __glutDisplay = XOpenDisplay(display);
00060   if (!__glutDisplay)
00061     __glutFatalError("could not open display: %s",
00062       XDisplayName(display));
00063   if (synchronize)
00064     XSynchronize(__glutDisplay, True);
00065   if (!glXQueryExtension(__glutDisplay, &errorBase, &eventBase))
00066     __glutFatalError(
00067       "OpenGL GLX extension not supported by display: %s",
00068       XDisplayName(display));
00069   __glutScreen = DefaultScreen(__glutDisplay);
00070   __glutRoot = RootWindow(__glutDisplay, __glutScreen);
00071   __glutScreenWidth = DisplayWidth(__glutDisplay, __glutScreen);
00072   __glutScreenHeight = DisplayHeight(__glutDisplay,
00073     __glutScreen);
00074   __glutConnectionFD = ConnectionNumber(__glutDisplay);
00075   __glutWMDeleteWindow = XInternAtom(__glutDisplay,
00076     "WM_DELETE_WINDOW", False);
00077 }
00078 
00079 void
00080 __glutInitTime(struct timeval *beginning)
00081 {
00082   static int beenhere = 0;
00083   static struct timeval genesis;
00084 
00085   if (!beenhere) {
00086     GETTIMEOFDAY(&genesis);
00087     beenhere = 1;
00088   }
00089   *beginning = genesis;
00090 }
00091 
00092 static void
00093 removeArgs(int *argcp, char **argv, int numToRemove)
00094 {
00095   int i, j;
00096 
00097   for (i = 0, j = numToRemove; argv[j]; i++, j++) {
00098     argv[i] = argv[j];
00099   }
00100   argv[i] = NULL;
00101   *argcp -= numToRemove;
00102 }
00103 
00104 void
00105 glutInit(int *argcp, char **argv)
00106 {
00107   char *display = NULL;
00108   char *str;
00109   struct timeval unused;
00110   int i;
00111 
00112   if (__glutDisplay) {
00113     __glutWarning("glutInit being called a second time.");
00114     return;
00115   }
00116   /* determine temporary program name */
00117   str = strrchr(argv[0], '/');
00118   if (str == NULL) {
00119     __glutProgramName = argv[0];
00120   } else {
00121     __glutProgramName = str + 1;
00122   }
00123 
00124   /* make private copy of command line arguments */
00125   __glutArgc = *argcp;
00126   __glutArgv = (char **) malloc(__glutArgc * sizeof(char *));
00127   if (!__glutArgv)
00128     __glutFatalError("out of memory.");
00129   for (i = 0; i < __glutArgc; i++) {
00130     __glutArgv[i] = strdup(argv[i]);
00131     if (!__glutArgv[i])
00132       __glutFatalError("out of memory.");
00133   }
00134 
00135   /* determine permanent program name */
00136   str = strrchr(__glutArgv[0], '/');
00137   if (str == NULL) {
00138     __glutProgramName = __glutArgv[0];
00139   } else {
00140     __glutProgramName = str + 1;
00141   }
00142 
00143   /* parse arguments for standard options */
00144   for (i = 1; i < __glutArgc; i++) {
00145     if (!strcmp(__glutArgv[i], "-display")) {
00146       if (++i >= __glutArgc) {
00147         __glutFatalError(
00148           "follow -display option with X display name.");
00149       }
00150       display = __glutArgv[i];
00151       removeArgs(argcp, &argv[1], 2);
00152     } else if (!strcmp(__glutArgv[i], "-geometry")) {
00153       int flags, x, y, width, height;
00154 
00155       if (++i >= __glutArgc) {
00156         __glutFatalError(
00157           "follow -geometry option with geometry parameter.");
00158       }
00159       /* Fix bogus "{width|height} may be used before set"
00160          warning */
00161       width = 0;
00162       height = 0;
00163 
00164       flags = XParseGeometry(__glutArgv[i], &x, &y,
00165         (unsigned int *) &width, (unsigned int *) &height);
00166       if (WidthValue & flags) {
00167         /* Careful because X does not allow zero or negative
00168            width windows */
00169         if (width > 0)
00170           __glutInitWidth = width;
00171       }
00172       if (HeightValue & flags) {
00173         /* Careful because X does not allow zero or negative
00174            height windows */
00175         if (height > 0)
00176           __glutInitHeight = height;
00177       }
00178       glutInitWindowSize(__glutInitWidth, __glutInitHeight);
00179       if (XValue & flags) {
00180         if (XNegative & flags)
00181           x = DisplayWidth(__glutDisplay, __glutScreen) +
00182             x - __glutSizeHints.width;
00183         /* Play safe: reject negative X locations */
00184         if (x >= 0)
00185           __glutInitX = x;
00186       }
00187       if (YValue & flags) {
00188         if (YNegative & flags)
00189           y = DisplayHeight(__glutDisplay, __glutScreen) +
00190             y - __glutSizeHints.height;
00191         /* Play safe: reject negative Y locations */
00192         if (y >= 0)
00193           __glutInitY = y;
00194       }
00195       glutInitWindowPosition(__glutInitX, __glutInitY);
00196       removeArgs(argcp, &argv[1], 2);
00197     } else if (!strcmp(__glutArgv[i], "-direct")) {
00198       if (!__glutTryDirect)
00199         __glutFatalError(
00200           "cannot force both direct and indirect rendering.");
00201       __glutForceDirect = GL_TRUE;
00202       removeArgs(argcp, &argv[1], 1);
00203     } else if (!strcmp(__glutArgv[i], "-indirect")) {
00204       if (__glutForceDirect)
00205         __glutFatalError(
00206           "cannot force both direct and indirect rendering.");
00207       __glutTryDirect = GL_FALSE;
00208       removeArgs(argcp, &argv[1], 1);
00209     } else if (!strcmp(__glutArgv[i], "-iconic")) {
00210       __glutIconic = GL_TRUE;
00211       removeArgs(argcp, &argv[1], 1);
00212     } else if (!strcmp(__glutArgv[i], "-gldebug")) {
00213       __glutDebug = GL_TRUE;
00214       removeArgs(argcp, &argv[1], 1);
00215     } else if (!strcmp(__glutArgv[i], "-sync")) {
00216       synchronize = GL_TRUE;
00217       removeArgs(argcp, &argv[1], 1);
00218     } else {
00219       /* once unknown option encountered, stop command line
00220          processing */
00221       break;
00222     }
00223   }
00224   __glutOpenXConnection(display);
00225   __glutInitTime(&unused);
00226 }
00227 
00228 void
00229 glutInitWindowPosition(int x, int y)
00230 {
00231   __glutInitX = x;
00232   __glutInitY = y;
00233   if (x >= 0 && y >= 0) {
00234     __glutSizeHints.x = x;
00235     __glutSizeHints.y = y;
00236     __glutSizeHints.flags |= USPosition;
00237   } else {
00238     __glutSizeHints.flags &= ~USPosition;
00239   }
00240 }
00241 
00242 void
00243 glutInitWindowSize(int width, int height)
00244 {
00245   __glutInitWidth = width;
00246   __glutInitHeight = height;
00247   if (width > 0 && height > 0) {
00248     __glutSizeHints.width = width;
00249     __glutSizeHints.height = height;
00250     __glutSizeHints.flags |= USSize;
00251   } else {
00252     __glutSizeHints.flags &= ~USSize;
00253   }
00254 }
00255 
00256 void
00257 glutInitDisplayMode(unsigned int mask)
00258 {
00259   __glutDisplayMode = mask;
00260 }
00261 
 

Powered by Plone

This site conforms to the following standards: