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