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 "../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 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 */
74 { 0, GL_FALSE }, /* Time */
76 { { 0, 0 }, GL_FALSE },
78 { NULL, NULL }, /* Timers */
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 */
92 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
95 * A call to this function should initialize all the display stuff...
97 void fgInitialize( const char* displayName )
99 #if TARGET_HOST_UNIX_X11
100 fgDisplay.Display = XOpenDisplay( displayName );
102 if( fgDisplay.Display == NULL )
103 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
105 if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) )
106 fgError( "OpenGL GLX extension not supported by display '%s'",
107 XDisplayName( displayName ) );
109 fgDisplay.Screen = DefaultScreen( fgDisplay.Display );
110 fgDisplay.RootWindow = RootWindow(
115 fgDisplay.ScreenWidth = DisplayWidth(
119 fgDisplay.ScreenHeight = DisplayHeight(
124 fgDisplay.ScreenWidthMM = DisplayWidthMM(
128 fgDisplay.ScreenHeightMM = DisplayHeightMM(
133 fgDisplay.Connection = ConnectionNumber( fgDisplay.Display );
136 * Create the window deletion atom
138 fgDisplay.DeleteWindow = XInternAtom(
144 #elif TARGET_HOST_WIN32
150 * What we need to do is to initialize the fgDisplay global structure here...
152 fgDisplay.Instance = GetModuleHandle( NULL );
154 atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
157 ZeroMemory( &wc, sizeof(WNDCLASS) );
160 * Each of the windows should have its own device context...
162 wc.style = CS_OWNDC /* | CS_DBLCLKS */;
163 wc.lpfnWndProc = fgWindowProc;
166 wc.hInstance = fgDisplay.Instance;
167 wc.hIcon = LoadIcon( fgDisplay.Instance, "GLUT_ICON" );
169 wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
171 wc.hCursor = LoadCursor( NULL, IDC_ARROW );
172 wc.hbrBackground = NULL;
173 wc.lpszMenuName = NULL;
174 wc.lpszClassName = "FREEGLUT";
177 * Register the window class
179 atom = RegisterClass( &wc );
184 * The screen dimensions can be obtained via GetSystemMetrics() calls
186 fgDisplay.ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
187 fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
190 HWND desktop = GetDesktopWindow( );
191 HDC context = GetDC( desktop );
193 fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
194 fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
196 ReleaseDC( desktop, context );
203 fgState.Initialised = GL_TRUE;
207 * Perform the freeglut deinitialization...
209 void fgDeinitialize( void )
213 if( !fgState.Initialised )
215 fgWarning( "fgDeinitialize(): "
216 "no valid initialization has been performed" );
220 /* fgState.Initialised = GL_FALSE; */
223 * 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 = ( SFG_Timer * )fgState.Timers.First )
235 fgListRemove ( &fgState.Timers, &timer->Node );
241 fgState.Initialised = GL_FALSE;
243 fgState.Position.X = -1;
244 fgState.Position.Y = -1;
245 fgState.Position.Use = GL_FALSE;
247 fgState.Size.X = 300;
248 fgState.Size.Y = 300;
249 fgState.Size.Use = GL_TRUE;
251 fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH;
253 fgState.ForceDirectContext = GL_FALSE;
254 fgState.TryDirectContext = GL_TRUE;
255 fgState.ForceIconic = GL_FALSE;
256 fgState.UseCurrentContext = GL_FALSE;
257 fgState.GLDebugSwitch = GL_FALSE;
258 fgState.XSyncSwitch = GL_FALSE;
259 fgState.ActionOnWindowClose = GLUT_ACTION_EXIT;
260 fgState.ExecState = GLUT_EXEC_STATE_INIT;
262 fgState.IgnoreKeyRepeat = GL_TRUE;
263 fgState.Modifiers = 0xffffffff;
265 fgState.GameModeSize.X = 640;
266 fgState.GameModeSize.Y = 480;
267 fgState.GameModeDepth = 16;
268 fgState.GameModeRefresh = 72;
270 fgState.Time.Set = GL_FALSE;
272 fgState.Timers.First = fgState.Timers.Last = NULL;
273 fgState.IdleCallback = NULL;
274 fgState.MenuStateCallback = ( FGCBMenuState )NULL;
275 fgState.MenuStatusCallback = ( FGCBMenuStatus )NULL;
277 fgState.SwapCount = 0;
278 fgState.SwapTime = 0;
279 fgState.FPSInterval = 0;
281 if( fgState.ProgramName )
283 free( fgState.ProgramName );
284 fgState.ProgramName = NULL;
288 #if TARGET_HOST_UNIX_X11
291 * Make sure all X-client data we have created will be destroyed on
294 XSetCloseDownMode( fgDisplay.Display, DestroyAll );
297 * Close the display connection, destroying all windows we have
300 XCloseDisplay( fgDisplay.Display );
306 * Everything inside the following #ifndef is copied from the X sources.
309 #if TARGET_HOST_WIN32
313 Copyright 1985, 1986, 1987,1998 The Open Group
315 Permission to use, copy, modify, distribute, and sell this software and its
316 documentation for any purpose is hereby granted without fee, provided that
317 the above copyright notice appear in all copies and that both that
318 copyright notice and this permission notice appear in supporting
321 The above copyright notice and this permission notice shall be included
322 in all copies or substantial portions of the Software.
324 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
325 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
326 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
327 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
328 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
329 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
330 OTHER DEALINGS IN THE SOFTWARE.
332 Except as contained in this notice, the name of The Open Group shall
333 not be used in advertising or otherwise to promote the sale, use or
334 other dealings in this Software without prior written authorization
339 #define NoValue 0x0000
340 #define XValue 0x0001
341 #define YValue 0x0002
342 #define WidthValue 0x0004
343 #define HeightValue 0x0008
344 #define AllValues 0x000F
345 #define XNegative 0x0010
346 #define YNegative 0x0020
349 * XParseGeometry parses strings of the form
350 * "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
351 * width, height, xoffset, and yoffset are unsigned integers.
352 * Example: "=80x24+300-49"
353 * The equal sign is optional.
354 * It returns a bitmask that indicates which of the four values
355 * were actually found in the string. For each value found,
356 * the corresponding argument is updated; for each value
357 * not found, the corresponding argument is left unchanged.
361 ReadInteger(char *string, char **NextString)
363 register int Result = 0;
368 else if (*string == '-')
373 for (; (*string >= '0') && (*string <= '9'); string++)
375 Result = (Result * 10) + (*string - '0');
377 *NextString = string;
384 static int XParseGeometry (
388 unsigned int *width, /* RETURN */
389 unsigned int *height) /* RETURN */
392 register char *strind;
393 unsigned int tempWidth = 0, tempHeight = 0;
394 int tempX = 0, tempY = 0;
397 if ( (string == NULL) || (*string == '\0'))
400 string++; /* ignore possible '=' at beg of geometry spec */
402 strind = (char *)string;
403 if (*strind != '+' && *strind != '-' && *strind != 'x') {
404 tempWidth = ReadInteger(strind, &nextCharacter);
405 if (strind == nextCharacter)
407 strind = nextCharacter;
411 if (*strind == 'x' || *strind == 'X') {
413 tempHeight = ReadInteger(strind, &nextCharacter);
414 if (strind == nextCharacter)
416 strind = nextCharacter;
420 if ((*strind == '+') || (*strind == '-')) {
421 if (*strind == '-') {
423 tempX = -ReadInteger(strind, &nextCharacter);
424 if (strind == nextCharacter)
426 strind = nextCharacter;
432 tempX = ReadInteger(strind, &nextCharacter);
433 if (strind == nextCharacter)
435 strind = nextCharacter;
438 if ((*strind == '+') || (*strind == '-')) {
439 if (*strind == '-') {
441 tempY = -ReadInteger(strind, &nextCharacter);
442 if (strind == nextCharacter)
444 strind = nextCharacter;
450 tempY = ReadInteger(strind, &nextCharacter);
451 if (strind == nextCharacter)
453 strind = nextCharacter;
459 /* If strind isn't at the end of the string the it's an invalid
460 geometry specification. */
462 if (*strind != '\0') return 0;
468 if (mask & WidthValue)
470 if (mask & HeightValue)
471 *height = tempHeight;
476 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
479 * Perform initialization. This usually happens on the program startup
480 * and restarting after glutMainLoop termination...
482 void FGAPIENTRY glutInit( int* pargc, char** argv )
484 char* displayName = NULL;
485 char* geometry = NULL;
486 int i, j, argc = *pargc;
488 if( fgState.Initialised )
489 fgError( "illegal glutInit() reinitialization attempt" );
491 if (pargc && *pargc && argv && *argv && **argv)
493 fgState.ProgramName = strdup (*argv);
495 if( !fgState.ProgramName )
496 fgError ("Could not allocate space for the program's name.");
499 fgCreateStructure( );
503 /* check if GLUT_FPS env var is set */
505 const char *fps = getenv( "GLUT_FPS" );
508 sscanf( fps, "%d", &fgState.FPSInterval );
509 if( fgState.FPSInterval <= 0 )
510 fgState.FPSInterval = 5000; /* 5000 milliseconds */
514 displayName = getenv( "DISPLAY");
516 for( i = 1; i < argc; i++ )
518 if( strcmp( argv[ i ], "-display" ) == 0 )
521 fgError( "-display parameter must be followed by display name" );
523 displayName = argv[ i ];
525 argv[ i - 1 ] = NULL;
529 else if( strcmp( argv[ i ], "-geometry" ) == 0 )
532 fgError( "-geometry parameter must be followed by window "
533 "geometry settings" );
535 geometry = argv[ i ];
537 argv[ i - 1 ] = NULL;
541 else if( strcmp( argv[ i ], "-direct" ) == 0)
543 if( ! fgState.TryDirectContext )
544 fgError( "parameters ambiguity, -direct and -indirect "
545 "cannot be both specified" );
547 fgState.ForceDirectContext = GL_TRUE;
551 else if( strcmp( argv[ i ], "-indirect" ) == 0 )
553 if( fgState.ForceDirectContext )
554 fgError( "parameters ambiguity, -direct and -indirect "
555 "cannot be both specified" );
557 fgState.TryDirectContext = GL_FALSE;
561 else if( strcmp( argv[ i ], "-iconic" ) == 0 )
563 fgState.ForceIconic = GL_TRUE;
567 else if( strcmp( argv[ i ], "-gldebug" ) == 0 )
569 fgState.GLDebugSwitch = GL_TRUE;
573 else if( strcmp( argv[ i ], "-sync" ) == 0 )
575 fgState.XSyncSwitch = GL_TRUE;
585 for( i = 1; i < *pargc; i++, j++ )
587 if( argv[ i ] == NULL )
589 /* Guaranteed to end because there are "*pargc" arguments left */
590 while ( argv[ j ] == NULL )
592 argv[ i ] = argv[ j ];
597 * Have the display created now. As I am too lazy to implement
598 * the program arguments parsing, we will have the DISPLAY
599 * environment variable used for opening the X display:
601 * XXX The above comment is rather unclear. We have just
602 * XXX completed parsing of the program arguments for GLUT
603 * XXX parameters. We obviously canNOT parse the application-
604 * XXX specific parameters. Can someone re-write the above
607 fgInitialize( displayName );
610 * Geometry parsing deffered until here because we may need the screen
616 int mask = XParseGeometry( geometry,
617 &fgState.Position.X, &fgState.Position.Y,
618 &fgState.Size.X, &fgState.Size.Y );
620 if( (mask & (WidthValue|HeightValue)) == (WidthValue|HeightValue) )
621 fgState.Size.Use = GL_TRUE;
623 if( mask & XNegative )
624 fgState.Position.X += fgDisplay.ScreenWidth - fgState.Size.X;
626 if( mask & YNegative )
627 fgState.Position.Y += fgDisplay.ScreenHeight - fgState.Size.Y;
629 if( (mask & (XValue|YValue)) == (XValue|YValue) )
630 fgState.Position.Use = GL_TRUE;
635 * Sets the default initial window position for new windows
637 void FGAPIENTRY glutInitWindowPosition( int x, int y )
639 fgState.Position.X = x;
640 fgState.Position.Y = y;
642 if( ( x >= 0 ) && ( y >= 0 ) )
643 fgState.Position.Use = GL_TRUE;
645 fgState.Position.Use = GL_FALSE;
649 * Sets the default initial window size for new windows
651 void FGAPIENTRY glutInitWindowSize( int width, int height )
653 fgState.Size.X = width;
654 fgState.Size.Y = height;
656 if( ( width > 0 ) && ( height > 0 ) )
657 fgState.Size.Use = GL_TRUE;
659 fgState.Size.Use = GL_FALSE;
663 * Sets the default display mode for all new windows
665 void FGAPIENTRY glutInitDisplayMode( unsigned int displayMode )
668 * We will make use of this value when creating a new OpenGL context...
670 fgState.DisplayMode = displayMode;
674 /* -- INIT DISPLAY STRING PARSING ------------------------------------------ */
676 #define NUM_TOKENS 28
677 static char* Tokens[] =
679 "alpha", "acca", "acc", "blue", "buffer", "conformant", "depth", "double",
680 "green", "index", "num", "red", "rgba", "rgb", "luminance", "stencil",
681 "single", "stereo", "samples", "slow", "win32pdf", "xvisual",
682 "xstaticgray", "xgrayscale", "xstaticcolor", "xpseudocolor",
683 "xtruecolor", "xdirectcolor"
686 static int TokenLengths[] =
688 5, 4, 3, 4, 6, 10, 5, 6,
689 5, 5, 3, 3, 4, 3, 9, 7,
695 void FGAPIENTRY glutInitDisplayString( const char* displayMode )
697 int glut_state_flag = 0 ;
699 * Unpack a lot of options from a character string. The options are
700 * delimited by blanks or tabs.
703 int len = strlen ( displayMode );
704 char *buffer = (char *)malloc ( (len+1) * sizeof(char) );
705 memcpy ( buffer, displayMode, len );
708 token = strtok ( buffer, " \t" );
715 for ( i = 0; i < NUM_TOKENS; i++ )
717 if ( strncmp ( token, Tokens[i], TokenLengths[i] ) == 0 ) break ;
722 case 0 : /* "alpha": Alpha color buffer precision in bits */
723 glut_state_flag |= GLUT_ALPHA ; /* Somebody fix this for me! */
726 case 1 : /* "acca": Red, green, blue, and alpha accumulation buffer
730 case 2 : /* "acc": Red, green, and blue accumulation buffer precision
731 in bits with zero bits alpha */
732 glut_state_flag |= GLUT_ACCUM ; /* Somebody fix this for me! */
735 case 3 : /* "blue": Blue color buffer precision in bits */
738 case 4 : /* "buffer": Number of bits in the color index color buffer
742 case 5 : /* "conformant": Boolean indicating if the frame buffer
743 configuration is conformant or not */
746 case 6 : /* "depth": Number of bits of precsion in the depth buffer */
747 glut_state_flag |= GLUT_DEPTH ; /* Somebody fix this for me! */
750 case 7 : /* "double": Boolean indicating if the color buffer is
752 glut_state_flag |= GLUT_DOUBLE ;
755 case 8 : /* "green": Green color buffer precision in bits */
758 case 9 : /* "index": Boolean if the color model is color index or not
760 glut_state_flag |= GLUT_INDEX ;
763 case 10 : /* "num": A special capability name indicating where the
764 value represents the Nth frame buffer configuration
765 matching the description string */
768 case 11 : /* "red": Red color buffer precision in bits */
771 case 12 : /* "rgba": Number of bits of red, green, blue, and alpha in
772 the RGBA color buffer */
773 glut_state_flag |= GLUT_RGBA ; /* Somebody fix this for me! */
776 case 13 : /* "rgb": Number of bits of red, green, and blue in the
777 RGBA color buffer with zero bits alpha */
778 glut_state_flag |= GLUT_RGB ; /* Somebody fix this for me! */
781 case 14 : /* "luminance": Number of bits of red in the RGBA and zero
782 bits of green, blue (alpha not specified) of color buffer
784 glut_state_flag |= GLUT_LUMINANCE ; /* Somebody fix this for me! */
787 case 15 : /* "stencil": Number of bits in the stencil buffer */
788 glut_state_flag |= GLUT_STENCIL; /* Somebody fix this for me! */
791 case 16 : /* "single": Boolean indicate the color buffer is single
793 glut_state_flag |= GLUT_SINGLE ;
796 case 17 : /* "stereo": Boolean indicating the color buffer supports
797 OpenGL-style stereo */
798 glut_state_flag |= GLUT_STEREO ;
801 case 18 : /* "samples": Indicates the number of multisamples to use
802 based on GLX's SGIS_multisample extension (for
804 glut_state_flag |= GLUT_MULTISAMPLE ; /*Somebody fix this for me!*/
807 case 19 : /* "slow": Boolean indicating if the frame buffer
808 configuration is slow or not */
811 case 20 : /* "win32pdf": matches the Win32 Pixel Format Descriptor by
813 #if TARGET_HOST_WIN32
817 case 21 : /* "xvisual": matches the X visual ID by number */
818 #if TARGET_HOST_UNIX_X11
822 case 22 : /* "xstaticgray": boolean indicating if the frame buffer
823 configuration's X visual is of type StaticGray */
824 #if TARGET_HOST_UNIX_X11
828 case 23 : /* "xgrayscale": boolean indicating if the frame buffer
829 configuration's X visual is of type GrayScale */
830 #if TARGET_HOST_UNIX_X11
834 case 24 : /* "xstaticcolor": boolean indicating if the frame buffer
835 configuration's X visual is of type StaticColor */
836 #if TARGET_HOST_UNIX_X11
840 case 25 : /* "xpseudocolor": boolean indicating if the frame buffer
841 configuration's X visual is of type PseudoColor */
842 #if TARGET_HOST_UNIX_X11
846 case 26 : /* "xtruecolor": boolean indicating if the frame buffer
847 configuration's X visual is of type TrueColor */
848 #if TARGET_HOST_UNIX_X11
852 case 27 : /* "xdirectcolor": boolean indicating if the frame buffer
853 configuration's X visual is of type DirectColor */
854 #if TARGET_HOST_UNIX_X11
858 case 28 : /* Unrecognized */
859 printf ( "WARNING - Display string token not recognized: %s\n",
864 token = strtok ( NULL, " \t" );
870 * We will make use of this value when creating a new OpenGL context...
872 fgState.DisplayMode = glut_state_flag;
875 /*** END OF FILE ***/