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 #define G_LOG_DOMAIN "freeglut-init"
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * fgDeinitialize() -- Win32's OK, X11 needs the OS-specific deinitialization done
41 * glutInitDisplayString() -- display mode string parsing
43 * Wouldn't it be cool to use gettext() for error messages? I just love bash saying
44 * "nie znaleziono pliku" instead of "file not found" :) 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
61 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
64 * A call to this function should initialize all the display stuff...
66 void fgInitialize( const char* displayName )
68 #if TARGET_HOST_UNIX_X11
70 * Have the display created
72 fgDisplay.Display = XOpenDisplay( displayName );
74 if( fgDisplay.Display == NULL )
77 * Failed to open a display. That's no good.
79 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
83 * Check for the OpenGL GLX extension availability:
85 if( !glXQueryExtension( fgDisplay.Display, NULL, NULL ) )
88 * GLX extensions have not been found...
90 fgError( "OpenGL GLX extension not supported by display '%s'", XDisplayName( displayName ) );
94 * Grab the default screen for the display we have just opened
96 fgDisplay.Screen = DefaultScreen( fgDisplay.Display );
99 * The same applying to the root window
101 fgDisplay.RootWindow = RootWindow(
107 * Grab the logical screen's geometry
109 fgDisplay.ScreenWidth = DisplayWidth(
114 fgDisplay.ScreenHeight = DisplayHeight(
120 * Grab the physical screen's geometry
122 fgDisplay.ScreenWidthMM = DisplayWidthMM(
127 fgDisplay.ScreenHeightMM = DisplayHeightMM(
133 * The display's connection number
135 fgDisplay.Connection = ConnectionNumber( fgDisplay.Display );
138 * Create the window deletion atom
140 fgDisplay.DeleteWindow = XInternAtom(
146 #elif TARGET_HOST_WIN32
152 * What we need to do is to initialize the fgDisplay global structure here...
154 fgDisplay.Instance = GetModuleHandle( NULL );
157 * Check if the freeglut window class has been registered before...
159 atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
162 * ...nope, it has not, and we have to do it right now:
169 * Make sure the unitialized fields are reset to zero
171 ZeroMemory( &wc, sizeof(WNDCLASS) );
174 * Each of the windows should have it's own device context...
177 wc.lpfnWndProc = fgWindowProc;
180 wc.hInstance = fgDisplay.Instance;
181 wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
182 wc.hCursor = LoadCursor( NULL, IDC_ARROW );
183 wc.hbrBackground = NULL;
184 wc.lpszMenuName = NULL;
185 wc.lpszClassName = "FREEGLUT";
188 * Register the window class
190 retval = RegisterClass( &wc );
191 g_assert( retval != FALSE );
195 * The screen dimensions can be obtained via GetSystemMetrics() calls
197 fgDisplay.ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
198 fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
202 * Checking the display's size in millimeters isn't too hard, too:
204 HWND desktop = GetDesktopWindow();
205 HDC context = GetDC( desktop );
208 * Grab the appropriate values now (HORZSIZE and VERTSIZE respectably):
210 fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
211 fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
214 * Whoops, forgot to release the device context :)
216 ReleaseDC( desktop, context );
222 * Have the joystick device initialized now
228 * Perform the freeglut deinitialization...
230 void fgDeinitialize( void )
235 * Check if initialization has been performed before
237 if( !fgState.Time.Set )
239 fgWarning( "fgDeinitialize(): fgState.Timer is null => no valid initialization has been performed" );
244 * Perform the freeglut structure deinitialization
246 fgDestroyStructure();
249 * Delete all the timers and their storage list
251 while ( (timer = fgState.Timers.First) != NULL )
253 fgListRemove(&fgState.Timers, &timer->Node);
258 * Deinitialize the joystick device
262 #if TARGET_HOST_UNIX_X11
265 * Make sure all X-client data we have created will be destroyed on display closing
267 XSetCloseDownMode( fgDisplay.Display, DestroyAll );
270 * Close the display connection, destroying all windows we have created so far
272 XCloseDisplay( fgDisplay.Display );
278 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
281 * Perform initialization. This usually happens on the program startup
282 * and restarting after glutMainLoop termination...
284 void FGAPIENTRY glutInit( int* pargc, char** argv )
286 char* geometrySettings = NULL;
287 char* displayName = NULL;
288 int i, j, argc = *pargc;
291 * Do not allow multiple initialization of the library
293 if( fgState.Time.Set )
296 * We can't have multiple initialization performed
298 fgError( "illegal glutInit() reinitialization attemp" );
302 * Have the internal freeglut structure initialized now
307 * Fill in the default values that have not been passed in yet.
309 if( fgState.Position.Use == FALSE )
311 fgState.Position.X = -1;
312 fgState.Position.Y = -1;
315 if( fgState.Size.Use == FALSE )
317 fgState.Size.X = 300;
318 fgState.Size.Y = 300;
319 fgState.Size.Use = TRUE;
323 * Some more settings to come
325 fgState.ForceDirectContext = FALSE;
326 fgState.TryDirectContext = TRUE;
327 fgState.ForceIconic = FALSE;
328 fgState.GLDebugSwitch = FALSE;
329 fgState.XSyncSwitch = FALSE;
332 * Assume we want to ignore the automatic key repeat
334 fgState.IgnoreKeyRepeat = TRUE;
337 * The default display mode to be used
339 fgState.DisplayMode = GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH;
342 * Set the default game mode settings
344 fgState.GameModeSize.X = 640;
345 fgState.GameModeSize.Y = 480;
346 fgState.GameModeDepth = 16;
347 fgState.GameModeRefresh = 72;
350 * Remember the function's call time
352 gettimeofday(&fgState.Time.Value, NULL);
353 fgState.Time.Set = TRUE;
356 * Grab the environment variable indicating the X display to use.
357 * This is harmless under Win32, so let's let it stay here...
359 displayName = strdup( getenv( "DISPLAY" ) );
362 * Have the program arguments parsed.
364 for( i=1; i<argc; i++ )
367 * The X display name settings
369 if( strcmp( argv[ i ], "-display" ) == 0 )
372 * Check for possible lack of the next argument
375 fgError( "-display parameter must be followed by display name" );
378 * Release the previous display name (the one from app's environment)
383 * Make a working copy of the name for us to use
385 displayName = strdup( argv[ i ] );
388 * Have both arguments removed
390 argv[ i - 1 ] = NULL;
396 * The geometry settings
398 else if( strcmp( argv[ i ], "-geometry" ) == 0 )
401 * Again, check if there is at least one more argument
404 fgError( "-geometry parameter must be followed by window geometry settings" );
407 * Otherwise make a duplicate of the geometry settings...
409 geometrySettings = strdup( argv[ i ] );
412 * Have both arguments removed
414 argv[ i - 1 ] = NULL;
420 * The direct/indirect OpenGL contexts settings
422 else if( strcmp( argv[ i ], "-direct" ) == 0)
425 * We try to force direct rendering...
427 if( fgState.TryDirectContext == FALSE )
428 fgError( "parameters ambiguity, -direct and -indirect cannot be both specified" );
430 fgState.ForceDirectContext = TRUE;
434 else if( strcmp( argv[ i ], "-indirect" ) == 0 )
437 * We try to force indirect rendering...
439 if( fgState.ForceDirectContext == TRUE )
440 fgError( "parameters ambiguity, -direct and -indirect cannot be both specified" );
442 fgState.TryDirectContext = FALSE;
448 * The '-iconic' parameter makes all new top-level
449 * windows created in iconified state...
451 else if( strcmp( argv[ i ], "-iconic" ) == 0 )
453 fgState.ForceIconic = TRUE;
459 * The '-gldebug' option activates some OpenGL state debugging features
461 else if( strcmp( argv[ i ], "-gldebug" ) == 0 )
463 fgState.GLDebugSwitch = TRUE;
469 * The '-sync' option activates X protocol synchronization (for debugging purposes)
471 else if( strcmp( argv[ i ], "-sync" ) == 0 )
473 fgState.XSyncSwitch = TRUE;
480 * Have the arguments list compacted now
482 for( i=1; i<argc; i++ )
484 if( argv[ i ] == NULL )
486 for( j=i; j<argc; j++ )
488 if( argv[ j ] != NULL )
490 argv[ i ] = argv[ j ];
498 * Have the display created now. As I am too lazy to implement
499 * the program arguments parsing, we will have the DISPLAY
500 * environment variable used for opening the X display:
502 fgInitialize( displayName );
504 #if TARGET_HOST_UNIX_X11
506 * We can process the default window geometry settings safely now
508 * WARNING: have this rewritten to be portable. That won't be hard.
510 if( geometrySettings != NULL )
516 * Have the settings parsed now. This is easy.
517 * We will use the XParseGeometry function.
519 result = XParseGeometry( geometrySettings, &x, &y, &w, &h );
522 * Check what we have been supplied with...
524 if( (result & WidthValue) && (w >= 0) )
527 if( (result & HeightValue) && (h >= 0) )
530 if( result & XValue )
532 if( result & XNegative )
533 fgState.Position.X = fgDisplay.ScreenWidth + x - fgState.Size.X;
535 fgState.Position.X = x;
538 if( result & YValue )
540 if( result & YNegative )
541 fgState.Position.Y = fgDisplay.ScreenHeight + y - fgState.Size.Y;
543 fgState.Position.Y = y;
547 * Free the geometry settings string
549 free( geometrySettings );
554 * Check for the minus one settings for both position and size...
556 if( fgState.Position.X < 0 || fgState.Position.Y < 0 )
557 fgState.Position.Use = FALSE;
559 if( fgState.Size.X < 0 || fgState.Size.Y < 0 )
560 fgState.Size.Use = FALSE;
563 * Do not forget about releasing the display name string
569 * Sets the default initial window position for new windows
571 void FGAPIENTRY glutInitWindowPosition( int x, int y )
574 * The settings can be disables when both coordinates are negative
576 if( (x >= 0) && (y >= 0) )
579 * We want to specify the initial position of each of the windows
581 fgState.Position.X = x;
582 fgState.Position.Y = y;
583 fgState.Position.Use = TRUE;
588 * The initial position of each of the windows is specified by the wm
590 fgState.Position.X = -1;
591 fgState.Position.Y = -1;
592 fgState.Position.Use = FALSE;
597 * Sets the default initial window size for new windows
599 void FGAPIENTRY glutInitWindowSize( int width, int height )
602 * The settings can be disables when both values are negative
604 if( (width >= 0) && (height >= 0) )
607 * We want to specify the initial size of each of the windows
609 fgState.Size.X = width;
610 fgState.Size.Y = height;
611 fgState.Size.Use = TRUE;
616 * The initial size of each of the windows is specified by the wm
620 fgState.Size.Use = FALSE;
625 * Sets the default display mode for all new windows
627 void FGAPIENTRY glutInitDisplayMode( int displayMode )
630 * We will make use of this value when creating a new OpenGL context...
632 fgState.DisplayMode = displayMode;
636 /* -- INIT DISPLAY STRING PARSING ------------------------------------------ */
638 #if 0 /* FIXME: CJP */
640 * There is a discrete number of comparison operators we can encounter:
642 * comparison ::= "=" | "!=" | "<" | ">" | "<=" | ">=" | "~"
644 #define FG_NONE 0x0000
645 #define FG_EQUAL 0x0001
646 #define FG_NOT_EQUAL 0x0002
647 #define FG_LESS 0x0003
648 #define FG_MORE 0x0004
649 #define FG_LESS_OR_EQUAL 0x0005
650 #define FG_MORE_OR_EQUAL 0x0006
651 #define FG_CLOSEST 0x0007
654 * The caller can feed us with a number of capability tokens:
656 * capability ::= "alpha" | "acca" | "acc" | "blue" | "buffer" | "conformant" | "depth" | "double" |
657 * "green" | "index" | "num" | "red" | "rgba" | "rgb" | "luminance" | "stencil" |
658 * "single" | "stereo" | "samples" | "slow" | "win32pdf" | "xvisual" | "xstaticgray" |
659 * "xgrayscale" | "xstaticcolor" | "xpseudocolor" | "xtruecolor" | "xdirectcolor"
661 static gchar* g_Tokens[] =
663 "none", "alpha", "acca", "acc", "blue", "buffer", "conformant", "depth", "double", "green",
664 "index", "num", "red", "rgba", "rgb", "luminance", "stencil", "single", "stereo", "samples",
665 "slow", "win32pdf", "xvisual", "xstaticgray", "xgrayscale", "xstaticcolor", "xpseudocolor",
666 "xtruecolor", "xdirectcolor", NULL
670 * The structure to hold the parsed display string tokens
672 typedef struct tagSFG_Capability SFG_Capability;
673 struct tagSFG_Capability
675 gint capability; /* the capability token enumerator */
676 gint comparison; /* the comparison operator used */
677 gint value; /* the value we're comparing to */
681 * The scanner configuration for the init display string
683 static GScannerConfig fgInitDisplayStringScannerConfig =
685 ( " \t\r\n" ) /* cset_skip_characters */,
690 ) /* cset_identifier_first */,
698 ) /* cset_identifier_nth */,
699 ( "#\n" ) /* cpair_comment_single */,
700 FALSE /* case_sensitive */,
701 TRUE /* skip_comment_multi */,
702 TRUE /* skip_comment_single */,
703 TRUE /* scan_comment_multi */,
704 TRUE /* scan_identifier */,
705 FALSE /* scan_identifier_1char */,
706 FALSE /* scan_identifier_NULL */,
707 TRUE /* scan_symbols */,
708 FALSE /* scan_binary */,
709 TRUE /* scan_octal */,
710 TRUE /* scan_float */,
712 FALSE /* scan_hex_dollar */,
713 TRUE /* scan_string_sq */,
714 TRUE /* scan_string_dq */,
715 TRUE /* numbers_2_int */,
716 FALSE /* int_2_float */,
717 FALSE /* identifier_2_string */,
718 TRUE /* char_2_token */,
719 FALSE /* symbol_2_token */,
720 FALSE /* scope_0_fallback */,
724 * Sets the default display mode for all new windows using a string
726 void FGAPIENTRY glutInitDisplayString( char* displayMode )
729 * display_string ::= (switch)
730 * switch ::= capability [comparison value]
731 * comparison ::= "=" | "!=" | "<" | ">" | "<=" | ">=" | "~"
732 * capability ::= "alpha" | "acca" | "acc" | "blue" | "buffer" | "conformant" |
733 * "depth" | "double" | "green" | "index" | "num" | "red" | "rgba" |
734 * "rgb" | "luminance" | "stencil" | "single" | "stereo" |
735 * "samples" | "slow" | "win32pdf" | "xvisual" | "xstaticgray" |
736 * "xgrayscale" | "xstaticcolor" | "xpseudocolor" |
737 * "xtruecolor" | "xdirectcolor"
738 * value ::= 0..9 [value]
740 * The display string grammar. This should be EBNF, but I couldn't find the definitions so, to
741 * clarify: (expr) means 0 or more times the expression, [expr] means 0 or 1 times expr.
743 * Create a new GLib lexical analyzer to process the display mode string
745 GScanner* scanner = g_scanner_new( &fgInitDisplayStringScannerConfig );
750 * Fail if the display mode string is empty or the scanner failed to initialize
752 freeglut_return_if_fail( (scanner != NULL) && (strlen( displayMode ) > 0) );
755 * Set the scanner's input name (for debugging)
757 scanner->input_name = "glutInitDisplayString";
760 * Start the lexical analysis of the extensions string
762 g_scanner_input_text( scanner, displayMode, strlen( displayMode ) );
765 * While there are any more tokens to be checked...
767 while( !g_scanner_eof( scanner ) )
770 * Actually we're expecting only string tokens
772 GTokenType tokenType = g_scanner_get_next_token( scanner );
775 * We are looking for identifiers
777 if( tokenType == G_TOKEN_IDENTIFIER )
779 gchar* capability = NULL; /* the capability identifier string (always present) */
780 gint capID = 0; /* the capability identifier value (from g_Tokens) */
781 gint comparison = 0; /* the comparison operator value, see definitions */
782 gchar* valueString = NULL; /* if the previous one is present, this is needed */
783 gint value = 0; /* the integer value converted using a strtol call */
784 SFG_Capability* capStruct; /* the capability description structure */
787 * OK. The general rule of thumb that we always should be getting a capability identifier
788 * string (see the grammar description). If it is followed by a comparison identifier, then
789 * there must follow an integer value we're comparing the capability to...
791 * Have the current token analyzed with that in mind...
793 for( i=0; i<(gint) strlen( scanner->value.v_identifier ); i++ )
795 gchar c = scanner->value.v_identifier[ i ];
797 if( (c == '=') || (c == '!') || (c == '<') || (c == '>') || (c == '~') )
802 * Here we go with the length of the capability identifier string.
803 * In the worst of cases, it is as long as the token identifier.
805 capability = g_strndup( scanner->value.v_identifier, i );
808 * OK. Is there a chance for comparison and value identifiers to follow?
809 * Note: checking against i+1 as this handles two cases: single character
810 * comparison operator and first of value's digits, which must always be
811 * there, or the two-character comparison operators.
813 if( (i + 1) < (gint) strlen( scanner->value.v_identifier ) )
816 * Yeah, indeed, it is the i-th character to start the identifier, then.
818 gchar c1 = scanner->value.v_identifier[ i + 0 ];
819 gchar c2 = scanner->value.v_identifier[ i + 1 ];
821 if( (c1 == '=') ) { i += 1; comparison = FG_EQUAL; } else
822 if( (c1 == '!') && (c2 == '=') ) { i += 2; comparison = FG_NOT_EQUAL; } else
823 if( (c1 == '<') && (c2 == '=') ) { i += 2; comparison = FG_LESS_OR_EQUAL; } else
824 if( (c1 == '>') && (c2 == '=') ) { i += 2; comparison = FG_MORE_OR_EQUAL; } else
825 if( (c1 == '<') ) { i += 1; comparison = FG_LESS; } else
826 if( (c1 == '>') ) { i += 1; comparison = FG_MORE; } else
827 if( (c1 == '~') ) { i += 1; comparison = FG_CLOSEST; } else
828 g_warning( "invalid comparison operator in token `%s'", scanner->value.v_identifier );
832 * Grab the value string that must follow the comparison operator...
834 if( comparison != FG_NONE && i < (gint) strlen( scanner->value.v_identifier ) )
835 valueString = g_strdup( scanner->value.v_identifier + i );
838 * If there was a value string, convert it to integer...
840 if( comparison != FG_NONE && strlen( valueString ) > 0 )
841 value = strtol( valueString, NULL, 0 );
844 * Now we need to match the capability string and it's ID
846 for( i=0; g_Tokens[ i ]!=NULL; i++ )
848 if( strcmp( capability, g_Tokens[ i ] ) == 0 )
851 * Looks like we've found the one we were looking for
859 * Create a new capability description structure
861 capStruct = g_new0( SFG_Capability, 1 );
864 * Fill in the cap's values, as we have parsed it:
866 capStruct->capability = capID;
867 capStruct->comparison = comparison;
868 capStruct->value = value;
871 * Add the new capabaility to the caps list
873 caps = g_list_append( caps, capStruct );
876 * Clean up the local mess and keep rolling on
878 g_free( valueString );
879 g_free( capability );
884 * Now that we have converted the string into somewhat more machine-friendly
885 * form, proceed with matching the frame buffer configuration...
887 * The caps list could be passed to a function that would try finding the closest
888 * matching pixel format, visual, frame buffer configuration or whatever. It would
889 * be good to convert the glutInitDisplayMode() to use the same method.
892 g_message( "found %i capability preferences", g_list_length( caps ) );
894 g_message( "token `%s': cap: %i, com: %i, val: %i",
895 scanner->value.v_identifier,
896 capStruct->capability,
897 capStruct->comparison,
903 * Free the capabilities we have parsed
905 for( i=0; i<(gint) g_list_length( caps ); i++ )
906 g_free( g_list_nth( caps, i )->data );
909 * Destroy the capabilities list itself
914 * Free the lexical scanner now...
916 g_scanner_destroy( scanner );
920 void FGAPIENTRY glutInitDisplayString( char* displayMode )
924 /*** END OF FILE ***/