fgState.FPSInterval is unsigned int (GLuint), environment variable GLUT_FPS can be...
[freeglut] / src / freeglut_init.c
1 /*
2  * freeglut_init.c
3  *
4  * Various freeglut initialization functions.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 2 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
34
35 /*
36  * TODO BEFORE THE STABLE RELEASE:
37  *
38  *  fgDeinitialize()        -- Win32's OK, X11 needs the OS-specific
39  *                             deinitialization done
40  *  glutInitDisplayString() -- display mode string parsing
41  *
42  * Wouldn't it be cool to use gettext() for error messages? I just love
43  * bash saying  "nie znaleziono pliku" instead of "file not found" :)
44  * Is gettext easily portable?
45  */
46
47 /* -- GLOBAL VARIABLES ----------------------------------------------------- */
48
49 /*
50  * A structure pointed by g_pDisplay holds all information
51  * regarding the display, screen, root window etc.
52  */
53 SFG_Display fgDisplay;
54
55 /*
56  * The settings for the current freeglut session
57  */
58 SFG_State fgState = { { -1, -1, GL_FALSE },  /* Position */
59                       { 300, 300, GL_TRUE }, /* Size */
60                       GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH,  /* DisplayMode */
61                       GL_FALSE,              /* Initialised */
62                       GL_FALSE,              /* ForceDirectContext */
63                       GL_TRUE,               /* TryDirectContext */
64                       GL_FALSE,              /* ForceIconic */
65                       GL_FALSE,              /* UseCurrentContext */
66                       GL_FALSE,              /* GLDebugSwitch */
67                       GL_FALSE,              /* XSyncSwitch */
68                       GL_TRUE,               /* IgnoreKeyRepeat */
69                       0xffffffff,            /* Modifiers */
70                       0,                     /* FPSInterval */
71                       0,                     /* SwapCount */
72                       0,                     /* SwapTime */
73 #if TARGET_HOST_WIN32
74                       { 0, GL_FALSE },       /* Time */
75 #else
76                       { { 0, 0 }, GL_FALSE },
77 #endif
78                       { NULL, NULL },         /* Timers */
79                       { NULL, NULL },         /* FreeTimers */
80                       NULL,                   /* IdleCallback */
81                       0,                      /* ActiveMenus */
82                       NULL,                   /* MenuStateCallback */
83                       NULL,                   /* MenuStatusCallback */
84                       { 640, 480, GL_TRUE },  /* GameModeSize */
85                       16,                     /* GameModeDepth */
86                       72,                     /* GameModeRefresh */
87                       GLUT_ACTION_EXIT,       /* ActionOnWindowClose */
88                       GLUT_EXEC_STATE_INIT,   /* ExecState */
89                       NULL                    /* ProgramName */
90 };
91
92
93 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
94
95 /*
96  * A call to this function should initialize all the display stuff...
97  */
98 void fgInitialize( const char* displayName )
99 {
100 #if TARGET_HOST_UNIX_X11
101     fgDisplay.Display = XOpenDisplay( displayName );
102
103     if( fgDisplay.Display == NULL )
104         fgError( "failed to open display '%s'", XDisplayName( displayName ) );
105
106     if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) )
107         fgError( "OpenGL GLX extension not supported by display '%s'",
108             XDisplayName( displayName ) );
109
110     fgDisplay.Screen = DefaultScreen( fgDisplay.Display );
111     fgDisplay.RootWindow = RootWindow(
112         fgDisplay.Display,
113         fgDisplay.Screen
114     );
115
116     fgDisplay.ScreenWidth  = DisplayWidth(
117         fgDisplay.Display,
118         fgDisplay.Screen
119     );
120     fgDisplay.ScreenHeight = DisplayHeight(
121         fgDisplay.Display,
122         fgDisplay.Screen
123     );
124
125     fgDisplay.ScreenWidthMM = DisplayWidthMM(
126         fgDisplay.Display,
127         fgDisplay.Screen
128     );
129     fgDisplay.ScreenHeightMM = DisplayHeightMM(
130         fgDisplay.Display,
131         fgDisplay.Screen
132     );
133
134     fgDisplay.Connection = ConnectionNumber( fgDisplay.Display );
135
136     /*
137      * Create the window deletion atom
138      */
139     fgDisplay.DeleteWindow = XInternAtom(
140         fgDisplay.Display,
141         "WM_DELETE_WINDOW",
142         FALSE
143     );
144
145 #elif TARGET_HOST_WIN32
146
147     WNDCLASS wc;
148     ATOM atom;
149
150     /*
151      * What we need to do is to initialize the fgDisplay global structure here.
152      */
153     fgDisplay.Instance = GetModuleHandle( NULL );
154
155     atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
156     if( atom == 0 )
157     {
158         ZeroMemory( &wc, sizeof(WNDCLASS) );
159
160         /*
161          * Each of the windows should have its own device context, and we
162          * want redraw events during Vertical and Horizontal Resizes by
163          * the user.
164          *
165          * XXX Old code had "| CS_DBCLCKS" commented out.  Plans for the
166          * XXX future?  Dead-end idea?
167          */
168         wc.style          = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
169         wc.lpfnWndProc    = fgWindowProc;
170         wc.cbClsExtra     = 0;
171         wc.cbWndExtra     = 0;
172         wc.hInstance      = fgDisplay.Instance;
173         wc.hIcon          = LoadIcon( fgDisplay.Instance, "GLUT_ICON" );
174         if (!wc.hIcon)
175           wc.hIcon        = LoadIcon( NULL, IDI_WINLOGO );
176
177         wc.hCursor        = LoadCursor( NULL, IDC_ARROW );
178         wc.hbrBackground  = NULL;
179         wc.lpszMenuName   = NULL;
180         wc.lpszClassName  = "FREEGLUT";
181
182         /*
183          * Register the window class
184          */
185         atom = RegisterClass( &wc );
186         assert( atom );
187     }
188
189     /*
190      * The screen dimensions can be obtained via GetSystemMetrics() calls
191      */
192     fgDisplay.ScreenWidth  = GetSystemMetrics( SM_CXSCREEN );
193     fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
194
195     {
196         HWND desktop = GetDesktopWindow( );
197         HDC  context = GetDC( desktop );
198
199         fgDisplay.ScreenWidthMM  = GetDeviceCaps( context, HORZSIZE );
200         fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
201
202         ReleaseDC( desktop, context );
203     }
204
205 #endif
206
207     fgJoystickInit( 0 );
208
209     fgState.Initialised = GL_TRUE;
210 }
211
212 /*
213  * Perform the freeglut deinitialization...
214  */
215 void fgDeinitialize( void )
216 {
217     SFG_Timer *timer;
218
219     if( !fgState.Initialised )
220     {
221         fgWarning( "fgDeinitialize(): "
222                    "no valid initialization has been performed" );
223         return;
224     }
225
226     /* fgState.Initialised = GL_FALSE; */
227
228     /*
229      * If there was a menu created, destroy the rendering context
230      */
231     if( fgStructure.MenuContext )
232     {
233         free( fgStructure.MenuContext );
234         fgStructure.MenuContext = NULL;
235     }
236
237     fgDestroyStructure( );
238
239     while( (timer = fgState.Timers.First) )
240     {
241         fgListRemove( &fgState.Timers, &timer->Node );
242         free( timer );
243     }
244
245     while( (timer = fgState.FreeTimers.First) )
246     {
247         fgListRemove( &fgState.FreeTimers, &timer->Node );
248         free( timer );
249     }
250
251     fgJoystickClose( );
252
253     fgState.Initialised = GL_FALSE;
254
255     fgState.Position.X = -1;
256     fgState.Position.Y = -1;
257     fgState.Position.Use = GL_FALSE;
258
259     fgState.Size.X = 300;
260     fgState.Size.Y = 300;
261     fgState.Size.Use = GL_TRUE;
262
263     fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH;
264
265     fgState.ForceDirectContext  = GL_FALSE;
266     fgState.TryDirectContext    = GL_TRUE;
267     fgState.ForceIconic         = GL_FALSE;
268     fgState.UseCurrentContext   = GL_FALSE;
269     fgState.GLDebugSwitch       = GL_FALSE;
270     fgState.XSyncSwitch         = GL_FALSE;
271     fgState.ActionOnWindowClose = GLUT_ACTION_EXIT;
272     fgState.ExecState           = GLUT_EXEC_STATE_INIT;
273
274     fgState.IgnoreKeyRepeat = GL_TRUE;
275     fgState.Modifiers       = 0xffffffff;
276
277     fgState.GameModeSize.X  = 640;
278     fgState.GameModeSize.Y  = 480;
279     fgState.GameModeDepth   =  16;
280     fgState.GameModeRefresh =  72;
281
282     fgState.Time.Set = GL_FALSE;
283
284     fgListInit( &fgState.Timers );
285     fgListInit( &fgState.FreeTimers );
286
287     fgState.IdleCallback = NULL;
288     fgState.MenuStateCallback = ( FGCBMenuState )NULL;
289     fgState.MenuStatusCallback = ( FGCBMenuStatus )NULL;
290
291     fgState.SwapCount   = 0;
292     fgState.SwapTime    = 0;
293     fgState.FPSInterval = 0;
294
295     if( fgState.ProgramName )
296     {
297         free( fgState.ProgramName );
298         fgState.ProgramName = NULL;
299     }
300     
301
302 #if TARGET_HOST_UNIX_X11
303
304     /*
305      * Make sure all X-client data we have created will be destroyed on
306      * display closing
307      */
308     XSetCloseDownMode( fgDisplay.Display, DestroyAll );
309
310     /*
311      * Close the display connection, destroying all windows we have
312      * created so far
313      */
314     XCloseDisplay( fgDisplay.Display );
315
316 #endif
317 }
318
319 /*
320  * Everything inside the following #ifndef is copied from the X sources.
321  */
322
323 #if TARGET_HOST_WIN32
324
325 /*
326
327 Copyright 1985, 1986, 1987,1998  The Open Group
328
329 Permission to use, copy, modify, distribute, and sell this software and its
330 documentation for any purpose is hereby granted without fee, provided that
331 the above copyright notice appear in all copies and that both that
332 copyright notice and this permission notice appear in supporting
333 documentation.
334
335 The above copyright notice and this permission notice shall be included
336 in all copies or substantial portions of the Software.
337
338 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
339 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
340 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
341 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
342 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
343 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
344 OTHER DEALINGS IN THE SOFTWARE.
345
346 Except as contained in this notice, the name of The Open Group shall
347 not be used in advertising or otherwise to promote the sale, use or
348 other dealings in this Software without prior written authorization
349 from The Open Group.
350
351 */
352
353 #define NoValue         0x0000
354 #define XValue          0x0001
355 #define YValue          0x0002
356 #define WidthValue      0x0004
357 #define HeightValue     0x0008
358 #define AllValues       0x000F
359 #define XNegative       0x0010
360 #define YNegative       0x0020
361
362 /*
363  *    XParseGeometry parses strings of the form
364  *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
365  *   width, height, xoffset, and yoffset are unsigned integers.
366  *   Example:  "=80x24+300-49"
367  *   The equal sign is optional.
368  *   It returns a bitmask that indicates which of the four values
369  *   were actually found in the string.  For each value found,
370  *   the corresponding argument is updated;  for each value
371  *   not found, the corresponding argument is left unchanged. 
372  */
373
374 static int
375 ReadInteger(char *string, char **NextString)
376 {
377     register int Result = 0;
378     int Sign = 1;
379     
380     if (*string == '+')
381         string++;
382     else if (*string == '-')
383     {
384         string++;
385         Sign = -1;
386     }
387     for (; (*string >= '0') && (*string <= '9'); string++)
388     {
389         Result = (Result * 10) + (*string - '0');
390     }
391     *NextString = string;
392     if (Sign >= 0)
393         return Result;
394     else
395         return -Result;
396 }
397
398 static int XParseGeometry (
399     const char *string,
400     int *x,
401     int *y,
402     unsigned int *width,    /* RETURN */
403     unsigned int *height)    /* RETURN */
404 {
405     int mask = NoValue;
406     register char *strind;
407     unsigned int tempWidth = 0, tempHeight = 0;
408     int tempX = 0, tempY = 0;
409     char *nextCharacter;
410
411     if ( (string == NULL) || (*string == '\0'))
412       return mask;
413     if (*string == '=')
414         string++;  /* ignore possible '=' at beg of geometry spec */
415
416     strind = (char *)string;
417     if (*strind != '+' && *strind != '-' && *strind != 'x') {
418         tempWidth = ReadInteger(strind, &nextCharacter);
419         if (strind == nextCharacter) 
420             return 0;
421         strind = nextCharacter;
422         mask |= WidthValue;
423     }
424
425     if (*strind == 'x' || *strind == 'X') {
426         strind++;
427         tempHeight = ReadInteger(strind, &nextCharacter);
428         if (strind == nextCharacter)
429             return 0;
430         strind = nextCharacter;
431         mask |= HeightValue;
432     }
433
434     if ((*strind == '+') || (*strind == '-')) {
435         if (*strind == '-') {
436             strind++;
437             tempX = -ReadInteger(strind, &nextCharacter);
438             if (strind == nextCharacter)
439                 return 0;
440             strind = nextCharacter;
441             mask |= XNegative;
442         }
443         else
444         {
445             strind++;
446             tempX = ReadInteger(strind, &nextCharacter);
447             if (strind == nextCharacter)
448                 return 0;
449             strind = nextCharacter;
450         }
451         mask |= XValue;
452         if ((*strind == '+') || (*strind == '-')) {
453             if (*strind == '-') {
454                 strind++;
455                 tempY = -ReadInteger(strind, &nextCharacter);
456                 if (strind == nextCharacter)
457                     return 0;
458                 strind = nextCharacter;
459                 mask |= YNegative;
460             }
461             else
462             {
463                 strind++;
464                 tempY = ReadInteger(strind, &nextCharacter);
465                 if (strind == nextCharacter)
466                     return 0;
467                 strind = nextCharacter;
468             }
469             mask |= YValue;
470         }
471     }
472
473     /* If strind isn't at the end of the string the it's an invalid
474        geometry specification. */
475
476     if (*strind != '\0') return 0;
477
478     if (mask & XValue)
479         *x = tempX;
480     if (mask & YValue)
481         *y = tempY;
482     if (mask & WidthValue)
483         *width = tempWidth;
484     if (mask & HeightValue)
485         *height = tempHeight;
486     return mask;
487 }
488 #endif
489
490 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
491
492 /*
493  * Perform initialization. This usually happens on the program startup
494  * and restarting after glutMainLoop termination...
495  */
496 void FGAPIENTRY glutInit( int* pargc, char** argv )
497 {
498     char* displayName = NULL;
499     char* geometry = NULL;
500     int i, j, argc = *pargc;
501
502     if( fgState.Initialised )
503         fgError( "illegal glutInit() reinitialization attempt" );
504
505     if (pargc && *pargc && argv && *argv && **argv)
506     {
507         fgState.ProgramName = strdup (*argv);
508
509         if( !fgState.ProgramName )
510             fgError ("Could not allocate space for the program's name.");
511     }
512
513     fgCreateStructure( );
514
515     fgElapsedTime( );
516
517     /* check if GLUT_FPS env var is set */
518     {
519         const char *fps = getenv( "GLUT_FPS" );
520         if( fps )
521         {
522             int interval;
523             sscanf( fps, "%d", &interval );
524             
525             if( interval <= 0 )
526                 fgState.FPSInterval = 5000;  /* 5000 millisecond default */
527             else
528                 fgState.FPSInterval = interval;
529         }
530     }
531
532     displayName = getenv( "DISPLAY");
533
534     for( i = 1; i < argc; i++ )
535     {
536         if( strcmp( argv[ i ], "-display" ) == 0 )
537         {
538             if( ++i >= argc )
539                 fgError( "-display parameter must be followed by display name" );
540
541             displayName = argv[ i ];
542
543             argv[ i - 1 ] = NULL;
544             argv[ i     ] = NULL;
545             ( *pargc ) -= 2;
546         }
547         else if( strcmp( argv[ i ], "-geometry" ) == 0 )
548         {
549             if( ++i >= argc )
550                 fgError( "-geometry parameter must be followed by window "
551                          "geometry settings" );
552
553             geometry = argv[ i ];
554
555             argv[ i - 1 ] = NULL;
556             argv[ i     ] = NULL;
557             ( *pargc ) -= 2;
558         }
559         else if( strcmp( argv[ i ], "-direct" ) == 0)
560         {
561             if( ! fgState.TryDirectContext )
562                 fgError( "parameters ambiguity, -direct and -indirect "
563                     "cannot be both specified" );
564
565             fgState.ForceDirectContext = GL_TRUE;
566             argv[ i ] = NULL;
567             ( *pargc )--;
568         }
569         else if( strcmp( argv[ i ], "-indirect" ) == 0 )
570         {
571             if( fgState.ForceDirectContext )
572                 fgError( "parameters ambiguity, -direct and -indirect "
573                     "cannot be both specified" );
574
575             fgState.TryDirectContext = GL_FALSE;
576             argv[ i ] = NULL;
577             (*pargc)--;
578         }
579         else if( strcmp( argv[ i ], "-iconic" ) == 0 )
580         {
581             fgState.ForceIconic = GL_TRUE;
582             argv[ i ] = NULL;
583             ( *pargc )--;
584         }
585         else if( strcmp( argv[ i ], "-gldebug" ) == 0 )
586         {
587             fgState.GLDebugSwitch = GL_TRUE;
588             argv[ i ] = NULL;
589             ( *pargc )--;
590         }
591         else if( strcmp( argv[ i ], "-sync" ) == 0 )
592         {
593             fgState.XSyncSwitch = GL_TRUE;
594             argv[ i ] = NULL;
595             ( *pargc )--;
596         }
597     }
598
599     /*
600      * Compact {argv}.
601      */
602     j = 2;
603     for( i = 1; i < *pargc; i++, j++ )
604     {
605         if( argv[ i ] == NULL )
606         {
607             /* Guaranteed to end because there are "*pargc" arguments left */
608             while ( argv[ j ] == NULL )
609                 j++;
610             argv[ i ] = argv[ j ];
611         }
612     }
613
614     /*
615      * Have the display created now. As I am too lazy to implement
616      * the program arguments parsing, we will have the DISPLAY
617      * environment variable used for opening the X display:
618      *
619      * XXX The above comment is rather unclear.  We have just
620      * XXX completed parsing of the program arguments for GLUT
621      * XXX parameters.  We obviously canNOT parse the application-
622      * XXX specific parameters.  Can someone re-write the above
623      * XXX more clearly?
624      */
625     fgInitialize( displayName );
626
627     /*
628      * Geometry parsing deffered until here because we may need the screen
629      * size.
630      */
631
632     if (geometry )
633     {
634         int mask = XParseGeometry( geometry,
635                                    &fgState.Position.X, &fgState.Position.Y,
636                                    &fgState.Size.X, &fgState.Size.Y );
637
638         if( (mask & (WidthValue|HeightValue)) == (WidthValue|HeightValue) )
639             fgState.Size.Use = GL_TRUE;
640
641         if( mask & XNegative )
642             fgState.Position.X += fgDisplay.ScreenWidth - fgState.Size.X;
643
644         if( mask & YNegative )
645             fgState.Position.Y += fgDisplay.ScreenHeight - fgState.Size.Y;
646
647         if( (mask & (XValue|YValue)) == (XValue|YValue) )
648             fgState.Position.Use = GL_TRUE;
649     }
650 }
651
652 /*
653  * Sets the default initial window position for new windows
654  */
655 void FGAPIENTRY glutInitWindowPosition( int x, int y )
656 {
657     fgState.Position.X = x;
658     fgState.Position.Y = y;
659
660     if( ( x >= 0 ) && ( y >= 0 ) )
661         fgState.Position.Use = GL_TRUE;
662     else
663         fgState.Position.Use = GL_FALSE;
664 }
665
666 /*
667  * Sets the default initial window size for new windows
668  */
669 void FGAPIENTRY glutInitWindowSize( int width, int height )
670 {
671     fgState.Size.X = width;
672     fgState.Size.Y = height;
673
674     if( ( width > 0 ) && ( height > 0 ) )
675         fgState.Size.Use = GL_TRUE;
676     else
677         fgState.Size.Use = GL_FALSE;
678 }
679
680 /*
681  * Sets the default display mode for all new windows
682  */
683 void FGAPIENTRY glutInitDisplayMode( unsigned int displayMode )
684 {
685     /*
686      * We will make use of this value when creating a new OpenGL context...
687      */
688     fgState.DisplayMode = displayMode;
689 }
690
691
692 /* -- INIT DISPLAY STRING PARSING ------------------------------------------ */
693
694 #define NUM_TOKENS             28
695 static char* Tokens[] =
696 {
697     "alpha", "acca", "acc", "blue", "buffer", "conformant", "depth", "double",
698     "green", "index", "num", "red", "rgba", "rgb", "luminance", "stencil",
699     "single", "stereo", "samples", "slow", "win32pdf", "xvisual",
700     "xstaticgray", "xgrayscale", "xstaticcolor", "xpseudocolor",
701     "xtruecolor", "xdirectcolor"
702 };
703
704 static int TokenLengths[] =
705 {
706     5,       4,      3,     4,      6,        10,           5,       6,
707     5,       5,       3,     3,     4,      3,     9,           7,
708     6,        6,        7,         4,      8,          7,
709     11,            10,           12,             12,
710     10,           12
711 };
712
713 void FGAPIENTRY glutInitDisplayString( const char* displayMode )
714 {
715     int glut_state_flag = 0 ;
716     /*
717      * Unpack a lot of options from a character string.  The options are
718      * delimited by blanks or tabs.
719      */
720     char *token ;
721     int len = strlen ( displayMode );
722     char *buffer = (char *)malloc ( (len+1) * sizeof(char) );
723     memcpy ( buffer, displayMode, len );
724     buffer[len] = '\0';
725
726     token = strtok ( buffer, " \t" );
727     while ( token )
728     {
729         /*
730          * Process this token
731          */
732         int i ;
733         for ( i = 0; i < NUM_TOKENS; i++ )
734         {
735             if ( strncmp ( token, Tokens[i], TokenLengths[i] ) == 0 ) break ;
736         }
737
738         switch ( i )
739         {
740         case 0 :  /* "alpha":  Alpha color buffer precision in bits */
741             glut_state_flag |= GLUT_ALPHA ;  /* Somebody fix this for me! */
742             break ;
743
744         case 1 :  /* "acca":  Red, green, blue, and alpha accumulation buffer
745                      precision in bits */
746             break ;
747
748         case 2 :  /* "acc":  Red, green, and blue accumulation buffer precision
749                      in bits with zero bits alpha */
750             glut_state_flag |= GLUT_ACCUM ;  /* Somebody fix this for me! */
751             break ;
752
753         case 3 :  /* "blue":  Blue color buffer precision in bits */
754             break ;
755
756         case 4 :  /* "buffer":  Number of bits in the color index color buffer
757                    */
758             break ;
759
760         case 5 :  /* "conformant":  Boolean indicating if the frame buffer
761                      configuration is conformant or not */
762             break ;
763
764         case 6 : /* "depth":  Number of bits of precsion in the depth buffer */
765             glut_state_flag |= GLUT_DEPTH ;  /* Somebody fix this for me! */
766             break ;
767
768         case 7 :  /* "double":  Boolean indicating if the color buffer is
769                      double buffered */
770             glut_state_flag |= GLUT_DOUBLE ;
771             break ;
772
773         case 8 :  /* "green":  Green color buffer precision in bits */
774             break ;
775
776         case 9 :  /* "index":  Boolean if the color model is color index or not
777                    */
778             glut_state_flag |= GLUT_INDEX ;
779             break ;
780
781         case 10 :  /* "num":  A special capability  name indicating where the
782                       value represents the Nth frame buffer configuration
783                       matching the description string */
784             break ;
785
786         case 11 :  /* "red":  Red color buffer precision in bits */
787             break ;
788
789         case 12 :  /* "rgba":  Number of bits of red, green, blue, and alpha in
790                       the RGBA color buffer */
791             glut_state_flag |= GLUT_RGBA ;  /* Somebody fix this for me! */
792             break ;
793
794         case 13 :  /* "rgb":  Number of bits of red, green, and blue in the
795                       RGBA color buffer with zero bits alpha */
796             glut_state_flag |= GLUT_RGB ;  /* Somebody fix this for me! */
797             break ;
798
799         case 14 :  /* "luminance":  Number of bits of red in the RGBA and zero
800                       bits of green, blue (alpha not specified) of color buffer
801                       precision */
802             glut_state_flag |= GLUT_LUMINANCE ; /* Somebody fix this for me! */
803             break ;
804
805         case 15 :  /* "stencil":  Number of bits in the stencil buffer */
806             glut_state_flag |= GLUT_STENCIL;  /* Somebody fix this for me! */
807             break ;
808
809         case 16 :  /* "single":  Boolean indicate the color buffer is single
810                       buffered */
811             glut_state_flag |= GLUT_SINGLE ;
812             break ;
813
814         case 17 :  /* "stereo":  Boolean indicating the color buffer supports
815                       OpenGL-style stereo */
816             glut_state_flag |= GLUT_STEREO ;
817             break ;
818
819         case 18 :  /* "samples":  Indicates the number of multisamples to use
820                       based on GLX's SGIS_multisample extension (for
821                       antialiasing) */
822             glut_state_flag |= GLUT_MULTISAMPLE ; /*Somebody fix this for me!*/
823             break ;
824
825         case 19 :  /* "slow":  Boolean indicating if the frame buffer
826                       configuration is slow or not */
827             break ;
828
829         case 20 :  /* "win32pdf":  matches the Win32 Pixel Format Descriptor by
830                       number */
831 #if TARGET_HOST_WIN32
832 #endif
833             break ;
834
835         case 21 :  /* "xvisual":  matches the X visual ID by number */
836 #if TARGET_HOST_UNIX_X11
837 #endif
838             break ;
839
840         case 22 :  /* "xstaticgray":  boolean indicating if the frame buffer
841                       configuration's X visual is of type StaticGray */
842 #if TARGET_HOST_UNIX_X11
843 #endif
844             break ;
845
846         case 23 :  /* "xgrayscale":  boolean indicating if the frame buffer
847                       configuration's X visual is of type GrayScale */
848 #if TARGET_HOST_UNIX_X11
849 #endif
850             break ;
851
852         case 24 :  /* "xstaticcolor":  boolean indicating if the frame buffer
853                       configuration's X visual is of type StaticColor */
854 #if TARGET_HOST_UNIX_X11
855 #endif
856             break ;
857
858         case 25 :  /* "xpseudocolor":  boolean indicating if the frame buffer
859                       configuration's X visual is of type PseudoColor */
860 #if TARGET_HOST_UNIX_X11
861 #endif
862             break ;
863
864         case 26 :  /* "xtruecolor":  boolean indicating if the frame buffer
865                       configuration's X visual is of type TrueColor */
866 #if TARGET_HOST_UNIX_X11
867 #endif
868             break ;
869
870         case 27 :  /* "xdirectcolor":  boolean indicating if the frame buffer
871                       configuration's X visual is of type DirectColor */
872 #if TARGET_HOST_UNIX_X11
873 #endif
874             break ;
875
876         case 28 :  /* Unrecognized */
877             printf ( "WARNING - Display string token not recognized:  %s\n",
878                      token );
879             break ;
880         }
881
882         token = strtok ( NULL, " \t" );
883     }
884
885     free ( buffer );
886
887     /*
888      * We will make use of this value when creating a new OpenGL context...
889      */
890     fgState.DisplayMode = glut_state_flag;
891 }
892
893 /*** END OF FILE ***/