4 * Various freeglut initialization functions.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
36 * TODO BEFORE THE STABLE RELEASE:
38 * fgDeinitialize() -- Win32's OK, X11 needs the OS-specific
39 * deinitialization done
40 * glutInitDisplayString() -- display mode string parsing
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?
47 /* -- GLOBAL VARIABLES ----------------------------------------------------- */
50 * A structure pointed by g_pDisplay holds all information
51 * regarding the display, screen, root window etc.
53 SFG_Display fgDisplay;
56 * The settings for the current freeglut session
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 */
72 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
73 { 0, GL_FALSE }, /* Time */
75 { { 0, 0 }, GL_FALSE },
77 { NULL, NULL }, /* Timers */
78 { NULL, NULL }, /* FreeTimers */
79 NULL, /* IdleCallback */
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 */
93 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
96 * A call to this function should initialize all the display stuff...
98 static void fghInitialize( const char* displayName )
100 #if TARGET_HOST_UNIX_X11
101 fgDisplay.Display = XOpenDisplay( displayName );
103 if( fgDisplay.Display == NULL )
104 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
106 if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) )
107 fgError( "OpenGL GLX extension not supported by display '%s'",
108 XDisplayName( displayName ) );
110 fgDisplay.Screen = DefaultScreen( fgDisplay.Display );
111 fgDisplay.RootWindow = RootWindow(
116 fgDisplay.ScreenWidth = DisplayWidth(
120 fgDisplay.ScreenHeight = DisplayHeight(
125 fgDisplay.ScreenWidthMM = DisplayWidthMM(
129 fgDisplay.ScreenHeightMM = DisplayHeightMM(
134 fgDisplay.Connection = ConnectionNumber( fgDisplay.Display );
136 /* Create the window deletion atom */
137 fgDisplay.DeleteWindow = XInternAtom(
143 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
148 /* What we need to do is to initialize the fgDisplay global structure here. */
149 fgDisplay.Instance = GetModuleHandle( NULL );
151 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
155 ZeroMemory( &wc, sizeof(WNDCLASS) );
158 * Each of the windows should have its own device context, and we
159 * want redraw events during Vertical and Horizontal Resizes by
162 * XXX Old code had "| CS_DBCLCKS" commented out. Plans for the
163 * XXX future? Dead-end idea?
165 wc.lpfnWndProc = fgWindowProc;
168 wc.hInstance = fgDisplay.Instance;
169 wc.hIcon = LoadIcon( fgDisplay.Instance, _T("GLUT_ICON") );
171 #if TARGET_HOST_WIN32
172 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
174 wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
175 #else /* TARGET_HOST_WINCE */
176 wc.style = CS_HREDRAW | CS_VREDRAW;
179 wc.hCursor = LoadCursor( NULL, IDC_ARROW );
180 wc.hbrBackground = NULL;
181 wc.lpszMenuName = NULL;
182 wc.lpszClassName = _T("FREEGLUT");
184 /* Register the window class */
185 atom = RegisterClass( &wc );
189 /* The screen dimensions can be obtained via GetSystemMetrics() calls */
190 fgDisplay.ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
191 fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
194 HWND desktop = GetDesktopWindow( );
195 HDC context = GetDC( desktop );
197 fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
198 fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
200 ReleaseDC( desktop, context );
205 fgState.Initialised = GL_TRUE;
209 * Perform the freeglut deinitialization...
211 void fgDeinitialize( void )
215 if( !fgState.Initialised )
217 fgWarning( "fgDeinitialize(): "
218 "no valid initialization has been performed" );
222 /* fgState.Initialised = GL_FALSE; */
224 /* If there was a menu created, destroy the rendering context */
225 if( fgStructure.MenuContext )
227 free( fgStructure.MenuContext );
228 fgStructure.MenuContext = NULL;
231 fgDestroyStructure( );
233 while( timer = fgState.Timers.First )
235 fgListRemove( &fgState.Timers, &timer->Node );
239 while( timer = fgState.FreeTimers.First )
241 fgListRemove( &fgState.FreeTimers, &timer->Node );
245 #if !TARGET_HOST_WINCE
246 if ( fgState.JoysticksInitialised )
248 #endif /* !TARGET_HOST_WINCE */
249 fgState.JoysticksInitialised = GL_FALSE;
251 fgState.Initialised = GL_FALSE;
253 fgState.Position.X = -1;
254 fgState.Position.Y = -1;
255 fgState.Position.Use = GL_FALSE;
257 fgState.Size.X = 300;
258 fgState.Size.Y = 300;
259 fgState.Size.Use = GL_TRUE;
261 fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH;
263 fgState.DirectContext = GLUT_TRY_DIRECT_CONTEXT;
264 fgState.ForceIconic = GL_FALSE;
265 fgState.UseCurrentContext = GL_FALSE;
266 fgState.GLDebugSwitch = GL_FALSE;
267 fgState.XSyncSwitch = GL_FALSE;
268 fgState.ActionOnWindowClose = GLUT_ACTION_EXIT;
269 fgState.ExecState = GLUT_EXEC_STATE_INIT;
271 fgState.KeyRepeat = GL_TRUE;
272 fgState.Modifiers = 0xffffffff;
274 fgState.GameModeSize.X = 640;
275 fgState.GameModeSize.Y = 480;
276 fgState.GameModeDepth = 16;
277 fgState.GameModeRefresh = 72;
279 fgState.Time.Set = GL_FALSE;
281 fgListInit( &fgState.Timers );
282 fgListInit( &fgState.FreeTimers );
284 fgState.IdleCallback = NULL;
285 fgState.MenuStateCallback = ( FGCBMenuState )NULL;
286 fgState.MenuStatusCallback = ( FGCBMenuStatus )NULL;
288 fgState.SwapCount = 0;
289 fgState.SwapTime = 0;
290 fgState.FPSInterval = 0;
292 if( fgState.ProgramName )
294 free( fgState.ProgramName );
295 fgState.ProgramName = NULL;
299 #if TARGET_HOST_UNIX_X11
302 * Make sure all X-client data we have created will be destroyed on
305 XSetCloseDownMode( fgDisplay.Display, DestroyAll );
308 * Close the display connection, destroying all windows we have
311 XCloseDisplay( fgDisplay.Display );
317 * Everything inside the following #ifndef is copied from the X sources.
320 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
324 Copyright 1985, 1986, 1987,1998 The Open Group
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
332 The above copyright notice and this permission notice shall be included
333 in all copies or substantial portions of the Software.
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.
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
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
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.
372 ReadInteger(char *string, char **NextString)
374 register int Result = 0;
379 else if (*string == '-')
384 for (; (*string >= '0') && (*string <= '9'); string++)
386 Result = (Result * 10) + (*string - '0');
388 *NextString = string;
395 static int XParseGeometry (
399 unsigned int *width, /* RETURN */
400 unsigned int *height) /* RETURN */
403 register char *strind;
404 unsigned int tempWidth = 0, tempHeight = 0;
405 int tempX = 0, tempY = 0;
408 if ( (string == NULL) || (*string == '\0'))
411 string++; /* ignore possible '=' at beg of geometry spec */
413 strind = (char *)string;
414 if (*strind != '+' && *strind != '-' && *strind != 'x') {
415 tempWidth = ReadInteger(strind, &nextCharacter);
416 if (strind == nextCharacter)
418 strind = nextCharacter;
422 if (*strind == 'x' || *strind == 'X') {
424 tempHeight = ReadInteger(strind, &nextCharacter);
425 if (strind == nextCharacter)
427 strind = nextCharacter;
431 if ((*strind == '+') || (*strind == '-')) {
432 if (*strind == '-') {
434 tempX = -ReadInteger(strind, &nextCharacter);
435 if (strind == nextCharacter)
437 strind = nextCharacter;
443 tempX = ReadInteger(strind, &nextCharacter);
444 if (strind == nextCharacter)
446 strind = nextCharacter;
449 if ((*strind == '+') || (*strind == '-')) {
450 if (*strind == '-') {
452 tempY = -ReadInteger(strind, &nextCharacter);
453 if (strind == nextCharacter)
455 strind = nextCharacter;
461 tempY = ReadInteger(strind, &nextCharacter);
462 if (strind == nextCharacter)
464 strind = nextCharacter;
470 /* If strind isn't at the end of the string the it's an invalid
471 geometry specification. */
473 if (*strind != '\0') return 0;
479 if (mask & WidthValue)
481 if (mask & HeightValue)
482 *height = tempHeight;
487 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
490 * Perform initialization. This usually happens on the program startup
491 * and restarting after glutMainLoop termination...
493 void FGAPIENTRY glutInit( int* pargc, char** argv )
495 char* displayName = NULL;
496 char* geometry = NULL;
497 int i, j, argc = *pargc;
499 if( fgState.Initialised )
500 fgError( "illegal glutInit() reinitialization attempt" );
502 if (pargc && *pargc && argv && *argv && **argv)
504 fgState.ProgramName = strdup (*argv);
506 if( !fgState.ProgramName )
507 fgError ("Could not allocate space for the program's name.");
510 fgCreateStructure( );
514 /* check if GLUT_FPS env var is set */
515 #if !TARGET_HOST_WINCE
517 const char *fps = getenv( "GLUT_FPS" );
521 sscanf( fps, "%d", &interval );
524 fgState.FPSInterval = 5000; /* 5000 millisecond default */
526 fgState.FPSInterval = interval;
530 displayName = getenv( "DISPLAY");
532 for( i = 1; i < argc; i++ )
534 if( strcmp( argv[ i ], "-display" ) == 0 )
537 fgError( "-display parameter must be followed by display name" );
539 displayName = argv[ i ];
541 argv[ i - 1 ] = NULL;
545 else if( strcmp( argv[ i ], "-geometry" ) == 0 )
548 fgError( "-geometry parameter must be followed by window "
549 "geometry settings" );
551 geometry = argv[ i ];
553 argv[ i - 1 ] = NULL;
557 else if( strcmp( argv[ i ], "-direct" ) == 0)
559 if( fgState.DirectContext == GLUT_FORCE_INDIRECT_CONTEXT )
560 fgError( "parameters ambiguity, -direct and -indirect "
561 "cannot be both specified" );
563 fgState.DirectContext = GLUT_FORCE_DIRECT_CONTEXT;
567 else if( strcmp( argv[ i ], "-indirect" ) == 0 )
569 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
570 fgError( "parameters ambiguity, -direct and -indirect "
571 "cannot be both specified" );
573 fgState.DirectContext = GLUT_FORCE_INDIRECT_CONTEXT;
577 else if( strcmp( argv[ i ], "-iconic" ) == 0 )
579 fgState.ForceIconic = GL_TRUE;
583 else if( strcmp( argv[ i ], "-gldebug" ) == 0 )
585 fgState.GLDebugSwitch = GL_TRUE;
589 else if( strcmp( argv[ i ], "-sync" ) == 0 )
591 fgState.XSyncSwitch = GL_TRUE;
601 for( i = 1; i < *pargc; i++, j++ )
603 /* Guaranteed to end because there are "*pargc" arguments left */
604 while ( argv[ j ] == NULL )
607 argv[ i ] = argv[ j ];
610 #endif /* TARGET_HOST_WINCE */
613 * Have the display created now. If there wasn't a "-display"
614 * in the program arguments, we will use the DISPLAY environment
615 * variable for opening the X display (see code above):
617 fghInitialize( displayName );
620 * Geometry parsing deffered until here because we may need the screen
626 int mask = XParseGeometry( geometry,
627 &fgState.Position.X, &fgState.Position.Y,
628 &fgState.Size.X, &fgState.Size.Y );
630 if( (mask & (WidthValue|HeightValue)) == (WidthValue|HeightValue) )
631 fgState.Size.Use = GL_TRUE;
633 if( mask & XNegative )
634 fgState.Position.X += fgDisplay.ScreenWidth - fgState.Size.X;
636 if( mask & YNegative )
637 fgState.Position.Y += fgDisplay.ScreenHeight - fgState.Size.Y;
639 if( (mask & (XValue|YValue)) == (XValue|YValue) )
640 fgState.Position.Use = GL_TRUE;
645 * Sets the default initial window position for new windows
647 void FGAPIENTRY glutInitWindowPosition( int x, int y )
649 fgState.Position.X = x;
650 fgState.Position.Y = y;
652 if( ( x >= 0 ) && ( y >= 0 ) )
653 fgState.Position.Use = GL_TRUE;
655 fgState.Position.Use = GL_FALSE;
659 * Sets the default initial window size for new windows
661 void FGAPIENTRY glutInitWindowSize( int width, int height )
663 fgState.Size.X = width;
664 fgState.Size.Y = height;
666 if( ( width > 0 ) && ( height > 0 ) )
667 fgState.Size.Use = GL_TRUE;
669 fgState.Size.Use = GL_FALSE;
673 * Sets the default display mode for all new windows
675 void FGAPIENTRY glutInitDisplayMode( unsigned int displayMode )
677 /* We will make use of this value when creating a new OpenGL context... */
678 fgState.DisplayMode = displayMode;
682 /* -- INIT DISPLAY STRING PARSING ------------------------------------------ */
684 #define NUM_TOKENS 28
685 static char* Tokens[] =
687 "alpha", "acca", "acc", "blue", "buffer", "conformant", "depth", "double",
688 "green", "index", "num", "red", "rgba", "rgb", "luminance", "stencil",
689 "single", "stereo", "samples", "slow", "win32pdf", "xvisual",
690 "xstaticgray", "xgrayscale", "xstaticcolor", "xpseudocolor",
691 "xtruecolor", "xdirectcolor"
694 static int TokenLengths[] =
696 5, 4, 3, 4, 6, 10, 5, 6,
697 5, 5, 3, 3, 4, 3, 9, 7,
703 void FGAPIENTRY glutInitDisplayString( const char* displayMode )
705 int glut_state_flag = 0 ;
707 * Unpack a lot of options from a character string. The options are
708 * delimited by blanks or tabs.
711 int len = strlen ( displayMode );
712 char *buffer = (char *)malloc ( (len+1) * sizeof(char) );
713 memcpy ( buffer, displayMode, len );
716 token = strtok ( buffer, " \t" );
719 /* Process this token */
721 for ( i = 0; i < NUM_TOKENS; i++ )
723 if ( strncmp ( token, Tokens[i], TokenLengths[i] ) == 0 ) break ;
728 case 0 : /* "alpha": Alpha color buffer precision in bits */
729 glut_state_flag |= GLUT_ALPHA ; /* Somebody fix this for me! */
732 case 1 : /* "acca": Red, green, blue, and alpha accumulation buffer
736 case 2 : /* "acc": Red, green, and blue accumulation buffer precision
737 in bits with zero bits alpha */
738 glut_state_flag |= GLUT_ACCUM ; /* Somebody fix this for me! */
741 case 3 : /* "blue": Blue color buffer precision in bits */
744 case 4 : /* "buffer": Number of bits in the color index color buffer
748 case 5 : /* "conformant": Boolean indicating if the frame buffer
749 configuration is conformant or not */
752 case 6 : /* "depth": Number of bits of precsion in the depth buffer */
753 glut_state_flag |= GLUT_DEPTH ; /* Somebody fix this for me! */
756 case 7 : /* "double": Boolean indicating if the color buffer is
758 glut_state_flag |= GLUT_DOUBLE ;
761 case 8 : /* "green": Green color buffer precision in bits */
764 case 9 : /* "index": Boolean if the color model is color index or not
766 glut_state_flag |= GLUT_INDEX ;
769 case 10 : /* "num": A special capability name indicating where the
770 value represents the Nth frame buffer configuration
771 matching the description string */
774 case 11 : /* "red": Red color buffer precision in bits */
777 case 12 : /* "rgba": Number of bits of red, green, blue, and alpha in
778 the RGBA color buffer */
779 glut_state_flag |= GLUT_RGBA ; /* Somebody fix this for me! */
782 case 13 : /* "rgb": Number of bits of red, green, and blue in the
783 RGBA color buffer with zero bits alpha */
784 glut_state_flag |= GLUT_RGB ; /* Somebody fix this for me! */
787 case 14 : /* "luminance": Number of bits of red in the RGBA and zero
788 bits of green, blue (alpha not specified) of color buffer
790 glut_state_flag |= GLUT_LUMINANCE ; /* Somebody fix this for me! */
793 case 15 : /* "stencil": Number of bits in the stencil buffer */
794 glut_state_flag |= GLUT_STENCIL; /* Somebody fix this for me! */
797 case 16 : /* "single": Boolean indicate the color buffer is single
799 glut_state_flag |= GLUT_SINGLE ;
802 case 17 : /* "stereo": Boolean indicating the color buffer supports
803 OpenGL-style stereo */
804 glut_state_flag |= GLUT_STEREO ;
807 case 18 : /* "samples": Indicates the number of multisamples to use
808 based on GLX's SGIS_multisample extension (for
810 glut_state_flag |= GLUT_MULTISAMPLE ; /*Somebody fix this for me!*/
813 case 19 : /* "slow": Boolean indicating if the frame buffer
814 configuration is slow or not */
817 case 20 : /* "win32pdf": matches the Win32 Pixel Format Descriptor by
819 #if TARGET_HOST_WIN32
823 case 21 : /* "xvisual": matches the X visual ID by number */
824 #if TARGET_HOST_UNIX_X11
828 case 22 : /* "xstaticgray": boolean indicating if the frame buffer
829 configuration's X visual is of type StaticGray */
830 #if TARGET_HOST_UNIX_X11
834 case 23 : /* "xgrayscale": boolean indicating if the frame buffer
835 configuration's X visual is of type GrayScale */
836 #if TARGET_HOST_UNIX_X11
840 case 24 : /* "xstaticcolor": boolean indicating if the frame buffer
841 configuration's X visual is of type StaticColor */
842 #if TARGET_HOST_UNIX_X11
846 case 25 : /* "xpseudocolor": boolean indicating if the frame buffer
847 configuration's X visual is of type PseudoColor */
848 #if TARGET_HOST_UNIX_X11
852 case 26 : /* "xtruecolor": boolean indicating if the frame buffer
853 configuration's X visual is of type TrueColor */
854 #if TARGET_HOST_UNIX_X11
858 case 27 : /* "xdirectcolor": boolean indicating if the frame buffer
859 configuration's X visual is of type DirectColor */
860 #if TARGET_HOST_UNIX_X11
864 case 28 : /* Unrecognized */
865 fgWarning ( "WARNING - Display string token not recognized: %s",
870 token = strtok ( NULL, " \t" );
875 /* We will make use of this value when creating a new OpenGL context... */
876 fgState.DisplayMode = glut_state_flag;
879 /*** END OF FILE ***/