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