4 * Window management methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 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.
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "freeglut_internal.h"
32 #if TARGET_HOST_POSIX_X11
33 #include <limits.h> /* LONG_MAX */
34 #include <unistd.h> /* usleep */
37 #if defined(_WIN32_WCE)
38 # include <Aygshell.h>
39 # ifdef FREEGLUT_LIB_PRAGMAS
40 # pragma comment( lib, "Aygshell.lib" )
42 #endif /* defined(_WIN32_WCE) */
45 #if TARGET_HOST_POSIX_X11
46 #ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
47 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20B2
50 #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
51 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
54 #ifndef GLX_CONTEXT_MINOR_VERSION_ARB
55 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
58 #ifndef GLX_CONTEXT_FLAGS_ARB
59 #define GLX_CONTEXT_FLAGS_ARB 0x2094
62 #ifndef GLX_CONTEXT_PROFILE_MASK_ARB
63 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
66 #ifndef GLX_CONTEXT_DEBUG_BIT_ARB
67 #define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001
70 #ifndef GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
71 #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
74 #ifndef GLX_CONTEXT_CORE_PROFILE_BIT_ARB
75 #define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
78 #ifndef GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
79 #define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
82 #ifndef GLX_RGBA_FLOAT_TYPE
83 #define GLX_RGBA_FLOAT_TYPE 0x20B9
86 #ifndef GLX_RGBA_FLOAT_BIT
87 #define GLX_RGBA_FLOAT_BIT 0x00000004
89 #endif /* TARGET_HOST_POSIX_X11 */
92 #if TARGET_HOST_MS_WINDOWS
93 /* The following include file is available from SGI but is not standard:
94 * #include <GL/wglext.h>
95 * So we copy the necessary parts out of it.
96 * XXX: should local definitions for extensions be put in a separate include file?
98 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
100 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
102 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
103 #define WGL_ACCELERATION_ARB 0x2003
104 #define WGL_SUPPORT_OPENGL_ARB 0x2010
105 #define WGL_DOUBLE_BUFFER_ARB 0x2011
106 #define WGL_COLOR_BITS_ARB 0x2014
107 #define WGL_ALPHA_BITS_ARB 0x201B
108 #define WGL_DEPTH_BITS_ARB 0x2022
109 #define WGL_STENCIL_BITS_ARB 0x2023
110 #define WGL_FULL_ACCELERATION_ARB 0x2027
112 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
113 #define WGL_SAMPLES_ARB 0x2042
115 #define WGL_TYPE_RGBA_FLOAT_ARB 0x21A0
117 #define WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20A9
119 #ifndef WGL_ARB_create_context
120 #define WGL_ARB_create_context 1
121 #ifdef WGL_WGLEXT_PROTOTYPES
122 extern HGLRC WINAPI wglCreateContextAttribsARB (HDC, HGLRC, const int *);
123 #endif /* WGL_WGLEXT_PROTOTYPES */
124 typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);
126 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
127 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
128 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
129 #define WGL_CONTEXT_FLAGS_ARB 0x2094
130 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
132 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
133 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
135 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
136 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
138 #define ERROR_INVALID_VERSION_ARB 0x2095
139 #define ERROR_INVALID_PROFILE_ARB 0x2096
142 #endif /* TARGET_HOST_MS_WINDOWS */
145 typedef BOOL (*pRegisterTouchWindow)(HWND,ULONG);
146 static pRegisterTouchWindow fghRegisterTouchWindow = (pRegisterTouchWindow)0xDEADBEEF;
149 /* pushing attribute/value pairs into an array */
150 #define ATTRIB(a) attributes[where++]=(a)
151 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
154 * TODO BEFORE THE STABLE RELEASE:
156 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
157 * fgSetupPixelFormat -- ignores the display mode settings
158 * fgOpenWindow() -- check the Win32 version, -iconic handling!
159 * fgCloseWindow() -- check the Win32 version
160 * glutCreateWindow() -- Check when default position and size is {-1,-1}
161 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
162 * glutDestroyWindow() -- check the Win32 version
163 * glutSetWindow() -- check the Win32 version
164 * glutGetWindow() -- OK
165 * glutSetWindowTitle() -- check the Win32 version
166 * glutSetIconTitle() -- check the Win32 version
167 * glutShowWindow() -- check the Win32 version
168 * glutHideWindow() -- check the Win32 version
169 * glutIconifyWindow() -- check the Win32 version
170 * glutReshapeWindow() -- check the Win32 version
171 * glutPositionWindow() -- check the Win32 version
172 * glutPushWindow() -- check the Win32 version
173 * glutPopWindow() -- check the Win32 version
176 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
178 static int fghIsLegacyContextVersionRequested( void )
180 return fgState.MajorVersion == 1 && fgState.MinorVersion == 0;
183 static int fghIsLegacyContextRequested( void )
185 return fghIsLegacyContextVersionRequested() &&
186 fgState.ContextFlags == 0 &&
187 fgState.ContextProfile == 0;
190 static int fghNumberOfAuxBuffersRequested( void )
192 if ( fgState.DisplayMode & GLUT_AUX4 ) {
195 if ( fgState.DisplayMode & GLUT_AUX3 ) {
198 if ( fgState.DisplayMode & GLUT_AUX2 ) {
201 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
202 return fgState.AuxiliaryBufferNumber;
207 static int fghMapBit( int mask, int from, int to )
209 return ( mask & from ) ? to : 0;
213 static void fghContextCreationError( void )
215 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
216 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
217 fgState.ContextProfile );
221 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
223 #if TARGET_HOST_POSIX_X11
225 * Chooses a visual basing on the current display mode settings
228 GLXFBConfig* fgChooseFBConfig( int *numcfgs )
230 GLboolean wantIndexedMode = GL_FALSE;
231 int attributes[ 100 ];
232 int where = 0, numAuxBuffers;
234 /* First we have to process the display mode settings... */
235 if( fgState.DisplayMode & GLUT_INDEX ) {
236 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
237 /* Buffer size is selected later. */
239 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
240 wantIndexedMode = GL_TRUE;
242 ATTRIB_VAL( GLX_RED_SIZE, 1 );
243 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
244 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
245 if( fgState.DisplayMode & GLUT_ALPHA ) {
246 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
250 if( fgState.DisplayMode & GLUT_DOUBLE ) {
251 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
254 if( fgState.DisplayMode & GLUT_STEREO ) {
255 ATTRIB_VAL( GLX_STEREO, True );
258 if( fgState.DisplayMode & GLUT_DEPTH ) {
259 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
262 if( fgState.DisplayMode & GLUT_STENCIL ) {
263 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
266 if( fgState.DisplayMode & GLUT_ACCUM ) {
267 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
268 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
269 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
270 if( fgState.DisplayMode & GLUT_ALPHA ) {
271 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
275 numAuxBuffers = fghNumberOfAuxBuffersRequested();
276 if ( numAuxBuffers > 0 ) {
277 ATTRIB_VAL( GLX_AUX_BUFFERS, numAuxBuffers );
280 if( fgState.DisplayMode & GLUT_SRGB ) {
281 ATTRIB_VAL( GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, True );
284 if (fgState.DisplayMode & GLUT_MULTISAMPLE) {
285 ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1);
286 ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber);
289 /* Push a terminator at the end of the list */
293 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
294 GLXFBConfig * fbconfig; /* The FBConfig we want */
295 int fbconfigArraySize; /* Number of FBConfigs in the array */
298 /* Get all FBConfigs that match "attributes". */
299 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
302 &fbconfigArraySize );
304 if (fbconfigArray != NULL)
306 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
309 if( wantIndexedMode )
312 * In index mode, we want the largest buffer size, i.e. visual
313 * depth. Here, FBConfigs are sorted by increasing buffer size
314 * first, so FBConfigs with the largest size come last.
317 int bufferSizeMin, bufferSizeMax;
319 /* Get bufferSizeMin. */
321 glXGetFBConfigAttrib( fgDisplay.Display,
325 /* Get bufferSizeMax. */
327 glXGetFBConfigAttrib( fgDisplay.Display,
328 fbconfigArray[fbconfigArraySize - 1],
332 if (bufferSizeMax > bufferSizeMin)
335 * Free and reallocate fbconfigArray, keeping only FBConfigs
336 * with the largest buffer size.
338 XFree(fbconfigArray);
340 /* Add buffer size token at the end of the list. */
342 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
345 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
348 &fbconfigArraySize );
353 * We now have an array of FBConfigs, the first one being the "best"
354 * one. So we should return only this FBConfig:
358 * - pick the XID of the FBConfig we want
359 * result = glXGetFBConfigAttrib( fgDisplay.Display,
365 * XFree(fbconfigArray);
367 * - reset "attributes" with the XID
369 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
372 * - get our FBConfig only
373 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
376 * &fbconfigArraySize );
378 * However, for some configurations (for instance multisampling with
379 * Mesa 6.5.2 and ATI drivers), this does not work:
380 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
381 * XID. Further investigation is needed.
383 * So, for now, we return the whole array of FBConfigs. This should
384 * not produce any side effects elsewhere.
386 fbconfig = fbconfigArray;
394 *numcfgs = fbconfigArraySize;
401 static void fghFillContextAttributes( int *attributes ) {
402 int where = 0, contextFlags, contextProfile;
404 if ( !fghIsLegacyContextVersionRequested() ) {
405 ATTRIB_VAL( GLX_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
406 ATTRIB_VAL( GLX_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
410 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, GLX_CONTEXT_DEBUG_BIT_ARB ) |
411 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
412 if ( contextFlags != 0 ) {
413 ATTRIB_VAL( GLX_CONTEXT_FLAGS_ARB, contextFlags );
417 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, GLX_CONTEXT_CORE_PROFILE_BIT_ARB ) |
418 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
419 if ( contextProfile != 0 ) {
420 ATTRIB_VAL( GLX_CONTEXT_PROFILE_MASK_ARB, contextProfile );
426 typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
427 GLXContext share_list, Bool direct,
428 const int *attrib_list);
430 static GLXContext fghCreateNewContext( SFG_Window* window )
432 /* for color model calculation */
433 int menu = ( window->IsMenu && !fgStructure.MenuContext );
434 int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
436 /* "classic" context creation */
437 Display *dpy = fgDisplay.Display;
438 GLXFBConfig config = *(window->Window.FBConfig);
439 int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
440 GLXContext share_list = NULL;
441 Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
444 /* new context creation */
446 CreateContextAttribsProc createContextAttribs;
448 /* If nothing fancy has been required, simply use the old context creation GLX API entry */
449 if ( fghIsLegacyContextRequested() )
451 context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
452 if ( context == NULL ) {
453 fghContextCreationError();
458 /* color index mode is not available anymore with OpenGL 3.0 */
459 if ( render_type == GLX_COLOR_INDEX_TYPE ) {
460 fgWarning( "color index mode is deprecated, using RGBA mode" );
463 fghFillContextAttributes( attributes );
465 createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
466 if ( createContextAttribs == NULL ) {
467 fgError( "glXCreateContextAttribsARB not found" );
470 context = createContextAttribs( dpy, config, share_list, direct, attributes );
471 if ( context == NULL ) {
472 fghContextCreationError();
478 #define _NET_WM_STATE_TOGGLE 2
479 static int fghResizeFullscrToggle(void)
481 XWindowAttributes attributes;
483 if(glutGet(GLUT_FULL_SCREEN)) {
484 /* restore original window size */
485 SFG_Window *win = fgStructure.CurrentWindow;
486 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
487 fgStructure.CurrentWindow->State.Width = win->State.OldWidth;
488 fgStructure.CurrentWindow->State.Height = win->State.OldHeight;
491 /* resize the window to cover the entire screen */
492 XGetWindowAttributes(fgDisplay.Display,
493 fgStructure.CurrentWindow->Window.Handle,
497 * The "x" and "y" members of "attributes" are the window's coordinates
498 * relative to its parent, i.e. to the decoration window.
500 XMoveResizeWindow(fgDisplay.Display,
501 fgStructure.CurrentWindow->Window.Handle,
504 fgDisplay.ScreenWidth,
505 fgDisplay.ScreenHeight);
510 static int fghEwmhFullscrToggle(void)
513 long evmask = SubstructureRedirectMask | SubstructureNotifyMask;
515 if(!fgDisplay.State || !fgDisplay.StateFullScreen) {
519 xev.type = ClientMessage;
520 xev.xclient.window = fgStructure.CurrentWindow->Window.Handle;
521 xev.xclient.message_type = fgDisplay.State;
522 xev.xclient.format = 32;
523 xev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
524 xev.xclient.data.l[1] = fgDisplay.StateFullScreen;
525 xev.xclient.data.l[2] = 0; /* no second property to toggle */
526 xev.xclient.data.l[3] = 1; /* source indication: application */
527 xev.xclient.data.l[4] = 0; /* unused */
529 if(!XSendEvent(fgDisplay.Display, fgDisplay.RootWindow, 0, evmask, &xev)) {
535 static int fghToggleFullscreen(void)
537 /* first try the EWMH (_NET_WM_STATE) method ... */
538 if(fghEwmhFullscrToggle() != -1) {
542 /* fall back to resizing the window */
543 if(fghResizeFullscrToggle() != -1) {
550 #endif /* TARGET_HOST_POSIX_X11 */
553 #if TARGET_HOST_MS_WINDOWS
555 * Setup the pixel format for a Win32 window
558 #if defined(_WIN32_WCE)
559 static wchar_t* fghWstrFromStr(const char* str)
561 int i,len=strlen(str);
562 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
568 #endif /* defined(_WIN32_WCE) */
571 static void fghFillContextAttributes( int *attributes ) {
572 int where = 0, contextFlags, contextProfile;
574 if ( !fghIsLegacyContextVersionRequested() ) {
575 ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
576 ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
580 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |
581 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
582 if ( contextFlags != 0 ) {
583 ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );
587 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |
588 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
589 if ( contextProfile != 0 ) {
590 ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );
596 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {
597 const char *pWglExtString;
598 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =
599 (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
600 if ( wglGetEntensionsStringARB == NULL )
604 pWglExtString = wglGetEntensionsStringARB( hdc );
605 return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );
608 void fgNewWGLCreateContext( SFG_Window* window )
612 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
614 /* If nothing fancy has been required, leave the context as it is */
615 if ( fghIsLegacyContextRequested() )
620 wglMakeCurrent( window->Window.Device, window->Window.Context );
622 if ( !fghIsExtensionSupported( window->Window.Device, "WGL_ARB_create_context" ) )
627 /* new context creation */
628 fghFillContextAttributes( attributes );
630 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
631 if ( wglCreateContextAttribsARB == NULL )
633 fgError( "wglCreateContextAttribsARB not found" );
636 context = wglCreateContextAttribsARB( window->Window.Device, 0, attributes );
637 if ( context == NULL )
639 fghContextCreationError();
642 wglMakeCurrent( NULL, NULL );
643 wglDeleteContext( window->Window.Context );
644 window->Window.Context = context;
647 #if !defined(_WIN32_WCE)
649 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )
651 int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
652 if ( fgState.DisplayMode & GLUT_DOUBLE ) {
653 flags |= PFD_DOUBLEBUFFER;
655 if ( fgState.DisplayMode & GLUT_STEREO ) {
659 #if defined(_MSC_VER)
660 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
663 /* Specify which pixel format do we opt for... */
664 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
666 ppfd->dwFlags = flags;
668 if( fgState.DisplayMode & GLUT_INDEX ) {
669 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
671 ppfd->cGreenBits = 0;
673 ppfd->cAlphaBits = 0;
675 ppfd->iPixelType = PFD_TYPE_RGBA;
677 ppfd->cGreenBits = 8;
679 ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;
682 ppfd->cColorBits = 24;
684 ppfd->cGreenShift = 0;
685 ppfd->cBlueShift = 0;
686 ppfd->cAlphaShift = 0;
687 ppfd->cAccumBits = ( fgState.DisplayMode & GLUT_ACCUM ) ? 1 : 0;
688 ppfd->cAccumRedBits = 0;
689 ppfd->cAccumGreenBits = 0;
690 ppfd->cAccumBlueBits = 0;
691 ppfd->cAccumAlphaBits = 0;
693 /* Hmmm, or 32/0 instead of 24/8? */
694 ppfd->cDepthBits = 24;
695 ppfd->cStencilBits = 8;
697 ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();
698 ppfd->iLayerType = layer_type;
700 ppfd->dwLayerMask = 0;
701 ppfd->dwVisibleMask = 0;
702 ppfd->dwDamageMask = 0;
704 ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );
707 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )
711 ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );
712 ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );
713 ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );
715 ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );
716 ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );
717 ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );
718 ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );
720 ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );
722 if ( fgState.DisplayMode & GLUT_SRGB ) {
723 ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );
726 ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );
727 ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );
732 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
733 unsigned char layer_type )
735 #if defined(_WIN32_WCE)
738 PIXELFORMATDESCRIPTOR pfd;
739 PIXELFORMATDESCRIPTOR* ppfd = &pfd;
745 current_hDC = CreateDC(TEXT("DISPLAY"), NULL ,NULL ,NULL);
747 current_hDC = window->Window.Device;
749 fghFillPFD( ppfd, current_hDC, layer_type );
750 pixelformat = ChoosePixelFormat( current_hDC, ppfd );
752 /* windows hack for multismapling/sRGB */
753 if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||
754 ( fgState.DisplayMode & GLUT_SRGB ) )
756 HGLRC rc, rc_before=wglGetCurrentContext();
758 HDC hDC, hDC_before=wglGetCurrentDC();
761 /* create a dummy window */
762 ZeroMemory(&wndCls, sizeof(wndCls));
763 wndCls.lpfnWndProc = DefWindowProc;
764 wndCls.hInstance = fgDisplay.Instance;
765 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
766 wndCls.lpszClassName = _T("FREEGLUT_dummy");
767 RegisterClass( &wndCls );
769 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
771 SetPixelFormat( hDC, pixelformat, ppfd );
773 rc = wglCreateContext( hDC );
774 wglMakeCurrent(hDC, rc);
776 if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )
778 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =
779 (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
780 if ( wglChoosePixelFormatARBProc )
785 float fAttributes[] = { 0, 0 };
787 fghFillPixelFormatAttributes( attributes, ppfd );
788 bValid = wglChoosePixelFormatARBProc(hDC, attributes, fAttributes, 1, &iPixelFormat, &numFormats);
790 if ( bValid && numFormats > 0 )
792 pixelformat = iPixelFormat;
797 wglMakeCurrent( hDC_before, rc_before);
798 wglDeleteContext(rc);
799 ReleaseDC(hWnd, hDC);
801 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
804 success = ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( current_hDC, pixelformat, ppfd ) );
807 DeleteDC(current_hDC);
810 #endif /* defined(_WIN32_WCE) */
813 #endif /* TARGET_HOST_MS_WINDOWS */
816 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
817 * the window structure passed in.
819 void fgSetWindow ( SFG_Window *window )
821 #if TARGET_HOST_POSIX_X11
824 glXMakeContextCurrent(
826 window->Window.Handle,
827 window->Window.Handle,
828 window->Window.Context
831 #elif TARGET_HOST_MS_WINDOWS
832 if ( window != fgStructure.CurrentWindow )
834 if( fgStructure.CurrentWindow )
835 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
836 fgStructure.CurrentWindow->Window.Device );
840 window->Window.Device = GetDC( window->Window.Handle );
842 window->Window.Device,
843 window->Window.Context
848 fgStructure.CurrentWindow = window;
851 #if TARGET_HOST_MS_WINDOWS
860 static BOOL CALLBACK m_proc(HMONITOR mon,
865 m_proc_t *dp=(m_proc_t *)data;
868 info.cbSize=sizeof(info);
869 res=GetMonitorInfo(mon,(LPMONITORINFO)&info);
872 if( !strcmp(dp->name,info.szDevice) )
874 *(dp->x)=info.rcMonitor.left;
875 *(dp->y)=info.rcMonitor.top;
883 * this function is only used in fgOpenWindow. Currently it only sets
884 * its output parameters, if the DisplayName is set in fgDisplay
885 * (and if it is able to recognize the display)
888 static void get_display_origin(int *xp,int *yp)
890 if( fgDisplay.DisplayName )
895 st.name=fgDisplay.DisplayName;
896 EnumDisplayMonitors(0,0,m_proc,(LPARAM)&st);
900 #pragma message( "-display parameter only works if compiled with WINVER >= 0x0500")
902 static void get_display_origin(int *xp,int *yp)
904 if( fgDisplay.DisplayName )
906 fgWarning( "for working -display support FreeGLUT must be compiled with WINVER >= 0x0500");
913 #if TARGET_HOST_POSIX_X11
914 static Bool fghWindowIsVisible( Display *display, XEvent *event, XPointer arg)
917 return (event->type == MapNotify) && (event->xmap.window == window);
923 * Opens a window. Requires a SFG_Window object created and attached
924 * to the freeglut structure. OpenGL context is created here.
926 void fgOpenWindow( SFG_Window* window, const char* title,
927 GLboolean positionUse, int x, int y,
928 GLboolean sizeUse, int w, int h,
929 GLboolean gameMode, GLboolean isSubWindow )
931 #if TARGET_HOST_POSIX_X11
932 XVisualInfo * visualInfo;
933 XSetWindowAttributes winAttr;
934 XTextProperty textProperty;
935 XSizeHints sizeHints;
937 XEvent eventReturnBuffer; /* return buffer required for a call */
939 int num_FBConfigs, i;
940 unsigned int current_DisplayMode = fgState.DisplayMode ;
942 /* Save the display mode if we are creating a menu window */
943 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
944 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
946 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
948 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
949 fgState.DisplayMode = current_DisplayMode ;
951 if( ! window->Window.FBConfig )
954 * The "fgChooseFBConfig" returned a null meaning that the visual
955 * context is not available.
956 * Try a couple of variations to see if they will work.
958 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
960 fgState.DisplayMode |= GLUT_DOUBLE ;
961 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
962 fgState.DisplayMode &= ~GLUT_DOUBLE;
965 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
967 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
968 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
969 fgState.DisplayMode |= GLUT_MULTISAMPLE;
973 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
974 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
976 /* Get the X visual. */
977 for (i = 0; i < num_FBConfigs; i++) {
978 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
979 window->Window.FBConfig[i] );
984 FREEGLUT_INTERNAL_ERROR_EXIT( visualInfo != NULL,
985 "visualInfo could not be retrieved from FBConfig", "fgOpenWindow" );
988 * XXX HINT: the masks should be updated when adding/removing callbacks.
989 * XXX This might speed up message processing. Is that true?
991 * XXX A: Not appreciably, but it WILL make it easier to debug.
992 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
993 * XXX turns off events that it doesn't need and is a whole lot
994 * XXX more pleasant to trace. (Think mouse-motion! Tons of
995 * XXX ``bonus'' GUI events stream in.)
998 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
999 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
1000 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
1001 PointerMotionMask | ButtonMotionMask;
1002 winAttr.background_pixmap = None;
1003 winAttr.background_pixel = 0;
1004 winAttr.border_pixel = 0;
1006 winAttr.colormap = XCreateColormap(
1007 fgDisplay.Display, fgDisplay.RootWindow,
1008 visualInfo->visual, AllocNone
1011 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
1013 if( window->IsMenu || ( gameMode == GL_TRUE ) )
1015 winAttr.override_redirect = True;
1016 mask |= CWOverrideRedirect;
1020 x = y = -1; /* default window position */
1022 w = h = 300; /* default window size */
1024 window->Window.Handle = XCreateWindow(
1026 window->Parent == NULL ? fgDisplay.RootWindow :
1027 window->Parent->Window.Handle,
1029 visualInfo->depth, InputOutput,
1030 visualInfo->visual, mask,
1035 * The GLX context creation, possibly trying the direct context rendering
1036 * or else use the current context if the user has so specified
1039 if( window->IsMenu )
1042 * If there isn't already an OpenGL rendering context for menu
1045 if( !fgStructure.MenuContext )
1047 fgStructure.MenuContext =
1048 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1049 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
1052 /* window->Window.Context = fgStructure.MenuContext->MContext; */
1053 window->Window.Context = fghCreateNewContext( window );
1055 else if( fgState.UseCurrentContext )
1057 window->Window.Context = glXGetCurrentContext( );
1059 if( ! window->Window.Context )
1060 window->Window.Context = fghCreateNewContext( window );
1063 window->Window.Context = fghCreateNewContext( window );
1065 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
1066 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
1068 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
1069 fgError( "Unable to force direct context rendering for window '%s'",
1075 * XXX Assume the new window is visible by default
1076 * XXX Is this a safe assumption?
1078 window->State.Visible = GL_TRUE;
1080 sizeHints.flags = 0;
1082 sizeHints.flags |= USPosition;
1084 sizeHints.flags |= USSize;
1087 * Fill in the size hints values now (the x, y, width and height
1088 * settings are obsolete, are there any more WMs that support them?)
1089 * Unless the X servers actually stop supporting these, we should
1090 * continue to fill them in. It is *not* our place to tell the user
1091 * that they should replace a window manager that they like, and which
1092 * works, just because *we* think that it's not "modern" enough.
1096 sizeHints.width = w;
1097 sizeHints.height = h;
1099 wmHints.flags = StateHint;
1100 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
1101 /* Prepare the window and iconified window names... */
1102 XStringListToTextProperty( (char **) &title, 1, &textProperty );
1106 window->Window.Handle,
1115 XFree( textProperty.value );
1117 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
1118 &fgDisplay.DeleteWindow, 1 );
1120 glXMakeContextCurrent(
1122 window->Window.Handle,
1123 window->Window.Handle,
1124 window->Window.Context
1127 /* register extension events _before_ window is mapped */
1128 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1129 fgRegisterDevices( fgDisplay.Display, &(window->Window.Handle) );
1132 XMapWindow( fgDisplay.Display, window->Window.Handle );
1137 XPeekIfEvent( fgDisplay.Display, &eventReturnBuffer, &fghWindowIsVisible, window->Window.Handle );
1139 #elif TARGET_HOST_MS_WINDOWS
1145 int WindowStyle = 0;
1147 /* Grab the window class we have registered on glutInit(): */
1148 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
1149 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
1154 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
1155 "Game mode being invoked on a subwindow",
1159 * Set the window creation flags appropriately to make the window
1162 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
1166 int worig = w, horig = h;
1168 #if !defined(_WIN32_WCE)
1169 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
1172 * Update the window dimensions, taking account of window
1173 * decorations. "freeglut" is to create the window with the
1174 * outside of its border at (x,y) and with dimensions (w,h).
1176 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
1177 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
1178 GetSystemMetrics( SM_CYCAPTION );
1180 #endif /* defined(_WIN32_WCE) */
1187 /* setting State.Width/Height to call resize callback later */
1190 if( ! window->IsMenu )
1195 else /* fail safe - Windows can make a window of size (0, 0) */
1196 w = h = 300; /* default window size */
1197 window->State.Width = window->State.Height = -1;
1201 window->State.Width = worig;
1202 window->State.Height = horig;
1206 * There's a small difference between creating the top, child and
1209 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
1211 if ( window->IsMenu )
1214 exFlags |= WS_EX_TOOLWINDOW;
1216 #if !defined(_WIN32_WCE)
1217 else if( window->Parent == NULL )
1218 flags |= WS_OVERLAPPEDWINDOW;
1224 #if defined(_WIN32_WCE)
1226 wchar_t* wstr = fghWstrFromStr(title);
1228 window->Window.Handle = CreateWindow(
1231 WS_VISIBLE | WS_POPUP,
1241 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
1242 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
1243 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
1244 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
1245 ShowWindow(window->Window.Handle, SW_SHOW);
1246 UpdateWindow(window->Window.Handle);
1250 /* xoff and yoff are used to place window relative to current display */
1251 /* The operation of gamemode also depends on this */
1253 get_display_origin(&xoff,&yoff);
1255 window->Window.Handle = CreateWindowEx(
1260 x+xoff, y+yoff, w, h,
1261 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
1267 #endif /* defined(_WIN32_WCE) */
1269 if( !( window->Window.Handle ) )
1270 fgError( "Failed to create a window (%s)!", title );
1272 /* Make a menu window always on top - fix Feature Request 947118 */
1273 if( window->IsMenu || gameMode )
1275 window->Window.Handle,
1278 SWP_NOMOVE | SWP_NOSIZE
1281 /* Hack to remove the caption (title bar) and/or border
1282 * and all the system menu controls.
1284 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
1285 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
1287 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1288 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
1290 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
1292 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1293 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_DLGFRAME | WS_SIZEBOX));
1295 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
1296 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
1298 /* Enable multitouch: additional flag TWF_FINETOUCH, TWF_WANTPALM */
1300 if (fghRegisterTouchWindow == (pRegisterTouchWindow)0xDEADBEEF)
1301 fghRegisterTouchWindow = (pRegisterTouchWindow)GetProcAddress(GetModuleHandle("user32"),"RegisterTouchWindow");
1302 if (fghRegisterTouchWindow)
1303 fghRegisterTouchWindow( window->Window.Handle, TWF_FINETOUCH | TWF_WANTPALM );
1306 #if defined(_WIN32_WCE)
1307 ShowWindow( window->Window.Handle, SW_SHOW );
1309 ShowWindow( window->Window.Handle,
1310 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
1311 #endif /* defined(_WIN32_WCE) */
1313 UpdateWindow( window->Window.Handle );
1314 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
1318 fgSetWindow( window );
1320 window->Window.DoubleBuffered =
1321 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
1323 if ( ! window->Window.DoubleBuffered )
1325 glDrawBuffer ( GL_FRONT );
1326 glReadBuffer ( GL_FRONT );
1331 * Closes a window, destroying the frame and OpenGL context
1333 void fgCloseWindow( SFG_Window* window )
1335 /* if we're in gamemode, call glutLeaveGameMode first to make sure the
1336 * gamemode is properly closed before closing the window
1338 if (fgStructure.GameModeWindow != NULL)
1339 glutLeaveGameMode();
1341 #if TARGET_HOST_POSIX_X11
1343 if( window->Window.Context )
1344 glXDestroyContext( fgDisplay.Display, window->Window.Context );
1345 XFree( window->Window.FBConfig );
1347 if( window->Window.Handle ) {
1348 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
1350 /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
1352 #elif TARGET_HOST_MS_WINDOWS
1354 /* Make sure we don't close a window with current context active */
1355 if( fgStructure.CurrentWindow == window )
1356 wglMakeCurrent( NULL, NULL );
1359 * Step through the list of windows. If the rendering context
1360 * is not being used by another window, then we delete it.
1366 for( iter = (SFG_Window *)fgStructure.Windows.First;
1368 iter = (SFG_Window *)iter->Node.Next )
1370 if( ( iter->Window.Context == window->Window.Context ) &&
1371 ( iter != window ) )
1376 wglDeleteContext( window->Window.Context );
1379 DestroyWindow( window->Window.Handle );
1384 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1387 * Creates a new top-level freeglut window
1389 int FGAPIENTRY glutCreateWindow( const char* title )
1391 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
1392 * XXX application has not already done so. The "freeglut" community
1393 * XXX decided not to go this route (freeglut-developer e-mail from
1394 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
1395 * XXX Desired 'freeglut' behaviour when there is no current window"
1397 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
1399 return fgCreateWindow( NULL, title, fgState.Position.Use,
1400 fgState.Position.X, fgState.Position.Y,
1401 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
1402 GL_FALSE, GL_FALSE )->ID;
1405 #if TARGET_HOST_MS_WINDOWS
1406 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )
1408 __glutExitFunc = exit_function;
1409 return glutCreateWindow( title );
1414 * This function creates a sub window.
1416 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
1419 SFG_Window* window = NULL;
1420 SFG_Window* parent = NULL;
1422 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
1423 parent = fgWindowByID( parentID );
1424 freeglut_return_val_if_fail( parent != NULL, 0 );
1427 x = parent->State.Width + x ;
1428 if ( w >= 0 ) x -= w ;
1431 if ( w < 0 ) w = parent->State.Width - x + w ;
1440 y = parent->State.Height + y ;
1441 if ( h >= 0 ) y -= h ;
1444 if ( h < 0 ) h = parent->State.Height - y + h ;
1451 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
1458 * Destroys a window and all of its subwindows
1460 void FGAPIENTRY glutDestroyWindow( int windowID )
1463 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1464 window = fgWindowByID( windowID );
1465 freeglut_return_if_fail( window != NULL );
1467 fgExecutionState ExecState = fgState.ExecState;
1468 fgAddToWindowDestroyList( window );
1469 fgState.ExecState = ExecState;
1474 * This function selects the current window
1476 void FGAPIENTRY glutSetWindow( int ID )
1478 SFG_Window* window = NULL;
1480 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1481 if( fgStructure.CurrentWindow != NULL )
1482 if( fgStructure.CurrentWindow->ID == ID )
1485 window = fgWindowByID( ID );
1486 if( window == NULL )
1488 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1492 fgSetWindow( window );
1496 * This function returns the ID number of the current window, 0 if none exists
1498 int FGAPIENTRY glutGetWindow( void )
1500 SFG_Window *win = fgStructure.CurrentWindow;
1502 * Since GLUT did not throw an error if this function was called without a prior call to
1503 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1504 * See Feature Request "[ 1307049 ] glutInit check".
1506 if ( ! fgState.Initialised )
1509 while ( win && win->IsMenu )
1511 return win ? win->ID : 0;
1515 * This function makes the current window visible
1517 void FGAPIENTRY glutShowWindow( void )
1519 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1520 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1522 #if TARGET_HOST_POSIX_X11
1524 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1525 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1527 #elif TARGET_HOST_MS_WINDOWS
1529 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1533 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1537 * This function hides the current window
1539 void FGAPIENTRY glutHideWindow( void )
1541 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1542 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1544 #if TARGET_HOST_POSIX_X11
1546 if( fgStructure.CurrentWindow->Parent == NULL )
1547 XWithdrawWindow( fgDisplay.Display,
1548 fgStructure.CurrentWindow->Window.Handle,
1551 XUnmapWindow( fgDisplay.Display,
1552 fgStructure.CurrentWindow->Window.Handle );
1553 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1555 #elif TARGET_HOST_MS_WINDOWS
1557 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1561 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1565 * Iconify the current window (top-level windows only)
1567 void FGAPIENTRY glutIconifyWindow( void )
1569 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1570 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1572 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1573 #if TARGET_HOST_POSIX_X11
1575 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1577 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1579 #elif TARGET_HOST_MS_WINDOWS
1581 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1585 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1589 * Set the current window's title
1591 void FGAPIENTRY glutSetWindowTitle( const char* title )
1593 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1594 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1595 if( ! fgStructure.CurrentWindow->Parent )
1597 #if TARGET_HOST_POSIX_X11
1601 text.value = (unsigned char *) title;
1602 text.encoding = XA_STRING;
1604 text.nitems = strlen( title );
1608 fgStructure.CurrentWindow->Window.Handle,
1612 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1614 #elif TARGET_HOST_MS_WINDOWS
1617 wchar_t* wstr = fghWstrFromStr(title);
1618 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1622 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1630 * Set the current window's iconified title
1632 void FGAPIENTRY glutSetIconTitle( const char* title )
1634 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1635 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1637 if( ! fgStructure.CurrentWindow->Parent )
1639 #if TARGET_HOST_POSIX_X11
1643 text.value = (unsigned char *) title;
1644 text.encoding = XA_STRING;
1646 text.nitems = strlen( title );
1650 fgStructure.CurrentWindow->Window.Handle,
1654 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1656 #elif TARGET_HOST_MS_WINDOWS
1659 wchar_t* wstr = fghWstrFromStr(title);
1660 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1664 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1672 * Change the current window's size
1674 void FGAPIENTRY glutReshapeWindow( int width, int height )
1676 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1677 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1679 if (glutGet(GLUT_FULL_SCREEN))
1681 /* Leave full screen state before resizing. */
1682 glutFullScreenToggle();
1685 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1686 fgStructure.CurrentWindow->State.Width = width ;
1687 fgStructure.CurrentWindow->State.Height = height;
1691 * Change the current window's position
1693 void FGAPIENTRY glutPositionWindow( int x, int y )
1695 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1696 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1698 if (glutGet(GLUT_FULL_SCREEN))
1700 /* Leave full screen state before moving. */
1701 glutFullScreenToggle();
1704 #if TARGET_HOST_POSIX_X11
1706 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1708 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1710 #elif TARGET_HOST_MS_WINDOWS
1715 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1716 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1718 fgStructure.CurrentWindow->Window.Handle,
1721 winRect.right - winRect.left,
1722 winRect.bottom - winRect.top,
1731 * Lowers the current window (by Z order change)
1733 void FGAPIENTRY glutPushWindow( void )
1735 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1736 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1738 #if TARGET_HOST_POSIX_X11
1740 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1742 #elif TARGET_HOST_MS_WINDOWS
1745 fgStructure.CurrentWindow->Window.Handle,
1748 SWP_NOSIZE | SWP_NOMOVE
1755 * Raises the current window (by Z order change)
1757 void FGAPIENTRY glutPopWindow( void )
1759 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1760 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1762 #if TARGET_HOST_POSIX_X11
1764 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1766 #elif TARGET_HOST_MS_WINDOWS
1769 fgStructure.CurrentWindow->Window.Handle,
1772 SWP_NOSIZE | SWP_NOMOVE
1779 * Resize the current window so that it fits the whole screen
1781 void FGAPIENTRY glutFullScreen( void )
1785 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1786 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1788 win = fgStructure.CurrentWindow;
1790 #if TARGET_HOST_POSIX_X11
1791 if(!glutGet(GLUT_FULL_SCREEN)) {
1792 if(fghToggleFullscreen() != -1) {
1793 win->State.IsFullscreen = GL_TRUE;
1797 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1799 if (glutGet(GLUT_FULL_SCREEN))
1801 /* Leave full screen state before resizing. */
1802 glutFullScreenToggle();
1808 /* For fullscreen mode, force the top-left corner to 0,0
1809 * and adjust the window rectangle so that the client area
1810 * covers the whole screen.
1815 get_display_origin(&rect.left,&rect.top);
1816 rect.right = fgDisplay.ScreenWidth+rect.left;
1817 rect.bottom = fgDisplay.ScreenHeight+rect.top;
1819 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1820 WS_CLIPCHILDREN, FALSE );
1823 * SWP_NOACTIVATE Do not activate the window
1824 * SWP_NOOWNERZORDER Do not change position in z-order
1825 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1826 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1829 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1833 rect.right - rect.left,
1834 rect.bottom - rect.top,
1835 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1839 win->State.IsFullscreen = GL_TRUE;
1845 * Toggle the window's full screen state.
1847 void FGAPIENTRY glutFullScreenToggle( void )
1851 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1852 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1854 win = fgStructure.CurrentWindow;
1856 #if TARGET_HOST_POSIX_X11
1857 if(fghToggleFullscreen() != -1) {
1858 win->State.IsFullscreen = !win->State.IsFullscreen;
1860 #elif TARGET_HOST_MS_WINDOWS
1862 win->State.IsFullscreen = !win->State.IsFullscreen;
1867 * A.Donev: Set and retrieve the window's user data
1869 void* FGAPIENTRY glutGetWindowData( void )
1871 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1872 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1873 return fgStructure.CurrentWindow->UserData;
1876 void FGAPIENTRY glutSetWindowData(void* data)
1878 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1879 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1880 fgStructure.CurrentWindow->UserData = data;
1883 /*** END OF FILE ***/