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 /* pushing attribute/value pairs into an array */
146 #define ATTRIB(a) attributes[where++]=(a)
147 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
150 * TODO BEFORE THE STABLE RELEASE:
152 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
153 * fgSetupPixelFormat -- ignores the display mode settings
154 * fgOpenWindow() -- check the Win32 version, -iconic handling!
155 * fgCloseWindow() -- check the Win32 version
156 * glutCreateWindow() -- Check when default position and size is {-1,-1}
157 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
158 * glutDestroyWindow() -- check the Win32 version
159 * glutSetWindow() -- check the Win32 version
160 * glutGetWindow() -- OK
161 * glutSetWindowTitle() -- check the Win32 version
162 * glutSetIconTitle() -- check the Win32 version
163 * glutShowWindow() -- check the Win32 version
164 * glutHideWindow() -- check the Win32 version
165 * glutIconifyWindow() -- check the Win32 version
166 * glutReshapeWindow() -- check the Win32 version
167 * glutPositionWindow() -- check the Win32 version
168 * glutPushWindow() -- check the Win32 version
169 * glutPopWindow() -- check the Win32 version
172 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
174 static int fghIsLegacyContextVersionRequested( void )
176 return fgState.MajorVersion == 1 && fgState.MinorVersion == 0;
179 static int fghIsLegacyContextRequested( void )
181 return fghIsLegacyContextVersionRequested() &&
182 fgState.ContextFlags == 0 &&
183 fgState.ContextProfile == 0;
186 static int fghNumberOfAuxBuffersRequested( void )
188 if ( fgState.DisplayMode & GLUT_AUX4 ) {
191 if ( fgState.DisplayMode & GLUT_AUX3 ) {
194 if ( fgState.DisplayMode & GLUT_AUX2 ) {
197 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
198 return fgState.AuxiliaryBufferNumber;
203 static int fghMapBit( int mask, int from, int to )
205 return ( mask & from ) ? to : 0;
209 static void fghContextCreationError( void )
211 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
212 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
213 fgState.ContextProfile );
217 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
219 #if TARGET_HOST_POSIX_X11
221 * Chooses a visual basing on the current display mode settings
224 GLXFBConfig* fgChooseFBConfig( int *numcfgs )
226 GLboolean wantIndexedMode = GL_FALSE;
227 int attributes[ 100 ];
228 int where = 0, numAuxBuffers;
230 /* First we have to process the display mode settings... */
231 if( fgState.DisplayMode & GLUT_INDEX ) {
232 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
233 /* Buffer size is selected later. */
235 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
236 wantIndexedMode = GL_TRUE;
238 ATTRIB_VAL( GLX_RED_SIZE, 1 );
239 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
240 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
241 if( fgState.DisplayMode & GLUT_ALPHA ) {
242 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
246 if( fgState.DisplayMode & GLUT_DOUBLE ) {
247 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
250 if( fgState.DisplayMode & GLUT_STEREO ) {
251 ATTRIB_VAL( GLX_STEREO, True );
254 if( fgState.DisplayMode & GLUT_DEPTH ) {
255 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
258 if( fgState.DisplayMode & GLUT_STENCIL ) {
259 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
262 if( fgState.DisplayMode & GLUT_ACCUM ) {
263 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
264 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
265 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
266 if( fgState.DisplayMode & GLUT_ALPHA ) {
267 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
271 numAuxBuffers = fghNumberOfAuxBuffersRequested();
272 if ( numAuxBuffers > 0 ) {
273 ATTRIB_VAL( GLX_AUX_BUFFERS, numAuxBuffers );
276 if( fgState.DisplayMode & GLUT_SRGB ) {
277 ATTRIB_VAL( GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, True );
280 if (fgState.DisplayMode & GLUT_MULTISAMPLE) {
281 ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1);
282 ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber);
285 /* Push a terminator at the end of the list */
289 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
290 GLXFBConfig * fbconfig; /* The FBConfig we want */
291 int fbconfigArraySize; /* Number of FBConfigs in the array */
294 /* Get all FBConfigs that match "attributes". */
295 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
298 &fbconfigArraySize );
300 if (fbconfigArray != NULL)
302 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
305 if( wantIndexedMode )
308 * In index mode, we want the largest buffer size, i.e. visual
309 * depth. Here, FBConfigs are sorted by increasing buffer size
310 * first, so FBConfigs with the largest size come last.
313 int bufferSizeMin, bufferSizeMax;
315 /* Get bufferSizeMin. */
317 glXGetFBConfigAttrib( fgDisplay.Display,
321 /* Get bufferSizeMax. */
323 glXGetFBConfigAttrib( fgDisplay.Display,
324 fbconfigArray[fbconfigArraySize - 1],
328 if (bufferSizeMax > bufferSizeMin)
331 * Free and reallocate fbconfigArray, keeping only FBConfigs
332 * with the largest buffer size.
334 XFree(fbconfigArray);
336 /* Add buffer size token at the end of the list. */
338 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
341 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
344 &fbconfigArraySize );
349 * We now have an array of FBConfigs, the first one being the "best"
350 * one. So we should return only this FBConfig:
354 * - pick the XID of the FBConfig we want
355 * result = glXGetFBConfigAttrib( fgDisplay.Display,
361 * XFree(fbconfigArray);
363 * - reset "attributes" with the XID
365 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
368 * - get our FBConfig only
369 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
372 * &fbconfigArraySize );
374 * However, for some configurations (for instance multisampling with
375 * Mesa 6.5.2 and ATI drivers), this does not work:
376 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
377 * XID. Further investigation is needed.
379 * So, for now, we return the whole array of FBConfigs. This should
380 * not produce any side effects elsewhere.
382 fbconfig = fbconfigArray;
390 *numcfgs = fbconfigArraySize;
397 static void fghFillContextAttributes( int *attributes ) {
398 int where = 0, contextFlags, contextProfile;
400 if ( !fghIsLegacyContextVersionRequested() ) {
401 ATTRIB_VAL( GLX_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
402 ATTRIB_VAL( GLX_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
406 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, GLX_CONTEXT_DEBUG_BIT_ARB ) |
407 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
408 if ( contextFlags != 0 ) {
409 ATTRIB_VAL( GLX_CONTEXT_FLAGS_ARB, contextFlags );
413 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, GLX_CONTEXT_CORE_PROFILE_BIT_ARB ) |
414 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
415 if ( contextProfile != 0 ) {
416 ATTRIB_VAL( GLX_CONTEXT_PROFILE_MASK_ARB, contextProfile );
422 typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
423 GLXContext share_list, Bool direct,
424 const int *attrib_list);
426 static GLXContext fghCreateNewContext( SFG_Window* window )
428 /* for color model calculation */
429 int menu = ( window->IsMenu && !fgStructure.MenuContext );
430 int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
432 /* "classic" context creation */
433 Display *dpy = fgDisplay.Display;
434 GLXFBConfig config = *(window->Window.FBConfig);
435 int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
436 GLXContext share_list = NULL;
437 Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
440 /* new context creation */
442 CreateContextAttribsProc createContextAttribs;
444 /* If nothing fancy has been required, simply use the old context creation GLX API entry */
445 if ( fghIsLegacyContextRequested() )
447 context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
448 if ( context == NULL ) {
449 fghContextCreationError();
454 /* color index mode is not available anymore with OpenGL 3.0 */
455 if ( render_type == GLX_COLOR_INDEX_TYPE ) {
456 fgWarning( "color index mode is deprecated, using RGBA mode" );
459 fghFillContextAttributes( attributes );
461 createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
462 if ( createContextAttribs == NULL ) {
463 fgError( "glXCreateContextAttribsARB not found" );
466 context = createContextAttribs( dpy, config, share_list, direct, attributes );
467 if ( context == NULL ) {
468 fghContextCreationError();
474 #define _NET_WM_STATE_TOGGLE 2
475 static int fghResizeFullscrToggle(void)
477 XWindowAttributes attributes;
479 if(glutGet(GLUT_FULL_SCREEN)) {
480 /* restore original window size */
481 SFG_Window *win = fgStructure.CurrentWindow;
482 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
483 fgStructure.CurrentWindow->State.Width = win->State.OldWidth;
484 fgStructure.CurrentWindow->State.Height = win->State.OldHeight;
487 /* resize the window to cover the entire screen */
488 XGetWindowAttributes(fgDisplay.Display,
489 fgStructure.CurrentWindow->Window.Handle,
493 * The "x" and "y" members of "attributes" are the window's coordinates
494 * relative to its parent, i.e. to the decoration window.
496 XMoveResizeWindow(fgDisplay.Display,
497 fgStructure.CurrentWindow->Window.Handle,
500 fgDisplay.ScreenWidth,
501 fgDisplay.ScreenHeight);
506 static int fghEwmhFullscrToggle(void)
509 long evmask = SubstructureRedirectMask | SubstructureNotifyMask;
511 if(!fgDisplay.State || !fgDisplay.StateFullScreen) {
515 xev.type = ClientMessage;
516 xev.xclient.window = fgStructure.CurrentWindow->Window.Handle;
517 xev.xclient.message_type = fgDisplay.State;
518 xev.xclient.format = 32;
519 xev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
520 xev.xclient.data.l[1] = fgDisplay.StateFullScreen;
521 xev.xclient.data.l[2] = 0; /* no second property to toggle */
522 xev.xclient.data.l[3] = 1; /* source indication: application */
523 xev.xclient.data.l[4] = 0; /* unused */
525 if(!XSendEvent(fgDisplay.Display, fgDisplay.RootWindow, 0, evmask, &xev)) {
531 static int fghToggleFullscreen(void)
533 /* first try the EWMH (_NET_WM_STATE) method ... */
534 if(fghEwmhFullscrToggle() != -1) {
538 /* fall back to resizing the window */
539 if(fghResizeFullscrToggle() != -1) {
546 #endif /* TARGET_HOST_POSIX_X11 */
549 #if TARGET_HOST_MS_WINDOWS
551 * Setup the pixel format for a Win32 window
554 #if defined(_WIN32_WCE)
555 static wchar_t* fghWstrFromStr(const char* str)
557 int i,len=strlen(str);
558 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
564 #endif /* defined(_WIN32_WCE) */
567 static void fghFillContextAttributes( int *attributes ) {
568 int where = 0, contextFlags, contextProfile;
570 if ( !fghIsLegacyContextVersionRequested() ) {
571 ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
572 ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
576 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |
577 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
578 if ( contextFlags != 0 ) {
579 ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );
583 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |
584 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
585 if ( contextProfile != 0 ) {
586 ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );
592 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {
593 const char *pWglExtString;
594 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =
595 (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
596 if ( wglGetEntensionsStringARB == NULL )
600 pWglExtString = wglGetEntensionsStringARB( hdc );
601 return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );
604 void fgNewWGLCreateContext( SFG_Window* window )
608 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
610 /* If nothing fancy has been required, leave the context as it is */
611 if ( fghIsLegacyContextRequested() )
616 wglMakeCurrent( window->Window.Device, window->Window.Context );
618 if ( !fghIsExtensionSupported( window->Window.Device, "WGL_ARB_create_context" ) )
623 /* new context creation */
624 fghFillContextAttributes( attributes );
626 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
627 if ( wglCreateContextAttribsARB == NULL )
629 fgError( "wglCreateContextAttribsARB not found" );
632 context = wglCreateContextAttribsARB( window->Window.Device, 0, attributes );
633 if ( context == NULL )
635 fghContextCreationError();
638 wglMakeCurrent( NULL, NULL );
639 wglDeleteContext( window->Window.Context );
640 window->Window.Context = context;
643 #if !defined(_WIN32_WCE)
645 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )
647 int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
648 if ( fgState.DisplayMode & GLUT_DOUBLE ) {
649 flags |= PFD_DOUBLEBUFFER;
651 if ( fgState.DisplayMode & GLUT_STEREO ) {
655 #if defined(_MSC_VER)
656 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
659 /* Specify which pixel format do we opt for... */
660 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
662 ppfd->dwFlags = flags;
664 if( fgState.DisplayMode & GLUT_INDEX ) {
665 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
667 ppfd->cGreenBits = 0;
669 ppfd->cAlphaBits = 0;
671 ppfd->iPixelType = PFD_TYPE_RGBA;
673 ppfd->cGreenBits = 8;
675 ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;
678 ppfd->cColorBits = 24;
680 ppfd->cGreenShift = 0;
681 ppfd->cBlueShift = 0;
682 ppfd->cAlphaShift = 0;
683 ppfd->cAccumBits = ( fgState.DisplayMode & GLUT_ACCUM ) ? 1 : 0;
684 ppfd->cAccumRedBits = 0;
685 ppfd->cAccumGreenBits = 0;
686 ppfd->cAccumBlueBits = 0;
687 ppfd->cAccumAlphaBits = 0;
689 /* Hmmm, or 32/0 instead of 24/8? */
690 ppfd->cDepthBits = 24;
691 ppfd->cStencilBits = 8;
693 ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();
694 ppfd->iLayerType = layer_type;
696 ppfd->dwLayerMask = 0;
697 ppfd->dwVisibleMask = 0;
698 ppfd->dwDamageMask = 0;
700 ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );
703 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )
707 ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );
708 ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );
709 ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );
711 ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );
712 ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );
713 ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );
714 ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );
716 ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );
718 if ( fgState.DisplayMode & GLUT_SRGB ) {
719 ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );
722 ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );
723 ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );
728 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
729 unsigned char layer_type )
731 #if defined(_WIN32_WCE)
734 PIXELFORMATDESCRIPTOR pfd;
735 PIXELFORMATDESCRIPTOR* ppfd = &pfd;
741 current_hDC = CreateDC(TEXT("DISPLAY"), NULL ,NULL ,NULL);
743 current_hDC = window->Window.Device;
745 fghFillPFD( ppfd, current_hDC, layer_type );
746 pixelformat = ChoosePixelFormat( current_hDC, ppfd );
748 /* windows hack for multismapling/sRGB */
749 if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||
750 ( fgState.DisplayMode & GLUT_SRGB ) )
752 HGLRC rc, rc_before=wglGetCurrentContext();
754 HDC hDC, hDC_before=wglGetCurrentDC();
757 /* create a dummy window */
758 ZeroMemory(&wndCls, sizeof(wndCls));
759 wndCls.lpfnWndProc = DefWindowProc;
760 wndCls.hInstance = fgDisplay.Instance;
761 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
762 wndCls.lpszClassName = _T("FREEGLUT_dummy");
763 RegisterClass( &wndCls );
765 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
767 SetPixelFormat( hDC, pixelformat, ppfd );
769 rc = wglCreateContext( hDC );
770 wglMakeCurrent(hDC, rc);
772 if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )
774 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =
775 (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
776 if ( wglChoosePixelFormatARBProc )
781 float fAttributes[] = { 0, 0 };
783 fghFillPixelFormatAttributes( attributes, ppfd );
784 bValid = wglChoosePixelFormatARBProc(hDC, attributes, fAttributes, 1, &iPixelFormat, &numFormats);
786 if ( bValid && numFormats > 0 )
788 pixelformat = iPixelFormat;
793 wglMakeCurrent( hDC_before, rc_before);
794 wglDeleteContext(rc);
795 ReleaseDC(hWnd, hDC);
797 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
800 success = ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( current_hDC, pixelformat, ppfd ) );
803 DeleteDC(current_hDC);
806 #endif /* defined(_WIN32_WCE) */
809 #endif /* TARGET_HOST_MS_WINDOWS */
812 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
813 * the window structure passed in.
815 void fgSetWindow ( SFG_Window *window )
817 #if TARGET_HOST_POSIX_X11
820 glXMakeContextCurrent(
822 window->Window.Handle,
823 window->Window.Handle,
824 window->Window.Context
827 #elif TARGET_HOST_MS_WINDOWS
828 if ( window != fgStructure.CurrentWindow )
830 if( fgStructure.CurrentWindow )
831 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
832 fgStructure.CurrentWindow->Window.Device );
836 window->Window.Device = GetDC( window->Window.Handle );
838 window->Window.Device,
839 window->Window.Context
844 fgStructure.CurrentWindow = window;
847 #if TARGET_HOST_MS_WINDOWS
856 static BOOL CALLBACK m_proc(HMONITOR mon,
861 m_proc_t *dp=(m_proc_t *)data;
864 info.cbSize=sizeof(info);
865 res=GetMonitorInfo(mon,(LPMONITORINFO)&info);
868 if( !strcmp(dp->name,info.szDevice) )
870 *(dp->x)=info.rcMonitor.left;
871 *(dp->y)=info.rcMonitor.top;
879 * this function is only used in fgOpenWindow. Currently it only sets
880 * its output parameters, if the DisplayName is set in fgDisplay
881 * (and if it is able to recognize the display)
884 static void get_display_origin(int *xp,int *yp)
886 if( fgDisplay.DisplayName )
891 st.name=fgDisplay.DisplayName;
892 EnumDisplayMonitors(0,0,m_proc,(LPARAM)&st);
896 #pragma message( "-display parameter only works if compiled with WINVER >= 0x0500")
898 static void get_display_origin(int *xp,int *yp)
900 if( fgDisplay.DisplayName )
902 fgWarning( "for working -display support FreeGLUT must be compiled with WINVER >= 0x0500");
909 #if TARGET_HOST_POSIX_X11
910 static Bool fghWindowIsVisible( Display *display, XEvent *event, XPointer arg)
913 return (event->type == MapNotify) && (event->xmap.window == window);
919 * Opens a window. Requires a SFG_Window object created and attached
920 * to the freeglut structure. OpenGL context is created here.
922 void fgOpenWindow( SFG_Window* window, const char* title,
923 GLboolean positionUse, int x, int y,
924 GLboolean sizeUse, int w, int h,
925 GLboolean gameMode, GLboolean isSubWindow )
927 #if TARGET_HOST_POSIX_X11
928 XVisualInfo * visualInfo;
929 XSetWindowAttributes winAttr;
930 XTextProperty textProperty;
931 XSizeHints sizeHints;
933 XEvent eventReturnBuffer; /* return buffer required for a call */
935 int num_FBConfigs, i;
936 unsigned int current_DisplayMode = fgState.DisplayMode ;
938 /* Save the display mode if we are creating a menu window */
939 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
940 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
942 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
944 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
945 fgState.DisplayMode = current_DisplayMode ;
947 if( ! window->Window.FBConfig )
950 * The "fgChooseFBConfig" returned a null meaning that the visual
951 * context is not available.
952 * Try a couple of variations to see if they will work.
954 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
956 fgState.DisplayMode |= GLUT_DOUBLE ;
957 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
958 fgState.DisplayMode &= ~GLUT_DOUBLE;
961 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
963 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
964 window->Window.FBConfig = fgChooseFBConfig( &num_FBConfigs );
965 fgState.DisplayMode |= GLUT_MULTISAMPLE;
969 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
970 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
972 /* Get the X visual. */
973 for (i = 0; i < num_FBConfigs; i++) {
974 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
975 window->Window.FBConfig[i] );
980 FREEGLUT_INTERNAL_ERROR_EXIT( visualInfo != NULL,
981 "visualInfo could not be retrieved from FBConfig", "fgOpenWindow" );
984 * XXX HINT: the masks should be updated when adding/removing callbacks.
985 * XXX This might speed up message processing. Is that true?
987 * XXX A: Not appreciably, but it WILL make it easier to debug.
988 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
989 * XXX turns off events that it doesn't need and is a whole lot
990 * XXX more pleasant to trace. (Think mouse-motion! Tons of
991 * XXX ``bonus'' GUI events stream in.)
994 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
995 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
996 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
997 PointerMotionMask | ButtonMotionMask;
998 winAttr.background_pixmap = None;
999 winAttr.background_pixel = 0;
1000 winAttr.border_pixel = 0;
1002 winAttr.colormap = XCreateColormap(
1003 fgDisplay.Display, fgDisplay.RootWindow,
1004 visualInfo->visual, AllocNone
1007 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
1009 if( window->IsMenu || ( gameMode == GL_TRUE ) )
1011 winAttr.override_redirect = True;
1012 mask |= CWOverrideRedirect;
1016 x = y = -1; /* default window position */
1018 w = h = 300; /* default window size */
1020 window->Window.Handle = XCreateWindow(
1022 window->Parent == NULL ? fgDisplay.RootWindow :
1023 window->Parent->Window.Handle,
1025 visualInfo->depth, InputOutput,
1026 visualInfo->visual, mask,
1031 * The GLX context creation, possibly trying the direct context rendering
1032 * or else use the current context if the user has so specified
1035 if( window->IsMenu )
1038 * If there isn't already an OpenGL rendering context for menu
1041 if( !fgStructure.MenuContext )
1043 fgStructure.MenuContext =
1044 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1045 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
1048 /* window->Window.Context = fgStructure.MenuContext->MContext; */
1049 window->Window.Context = fghCreateNewContext( window );
1051 else if( fgState.UseCurrentContext )
1053 window->Window.Context = glXGetCurrentContext( );
1055 if( ! window->Window.Context )
1056 window->Window.Context = fghCreateNewContext( window );
1059 window->Window.Context = fghCreateNewContext( window );
1061 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
1062 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
1064 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
1065 fgError( "Unable to force direct context rendering for window '%s'",
1071 * XXX Assume the new window is visible by default
1072 * XXX Is this a safe assumption?
1074 window->State.Visible = GL_TRUE;
1076 sizeHints.flags = 0;
1078 sizeHints.flags |= USPosition;
1080 sizeHints.flags |= USSize;
1083 * Fill in the size hints values now (the x, y, width and height
1084 * settings are obsolete, are there any more WMs that support them?)
1085 * Unless the X servers actually stop supporting these, we should
1086 * continue to fill them in. It is *not* our place to tell the user
1087 * that they should replace a window manager that they like, and which
1088 * works, just because *we* think that it's not "modern" enough.
1092 sizeHints.width = w;
1093 sizeHints.height = h;
1095 wmHints.flags = StateHint;
1096 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
1097 /* Prepare the window and iconified window names... */
1098 XStringListToTextProperty( (char **) &title, 1, &textProperty );
1102 window->Window.Handle,
1111 XFree( textProperty.value );
1113 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
1114 &fgDisplay.DeleteWindow, 1 );
1116 glXMakeContextCurrent(
1118 window->Window.Handle,
1119 window->Window.Handle,
1120 window->Window.Context
1123 XMapWindow( fgDisplay.Display, window->Window.Handle );
1128 XPeekIfEvent( fgDisplay.Display, &eventReturnBuffer, &fghWindowIsVisible, window->Window.Handle );
1130 #elif TARGET_HOST_MS_WINDOWS
1136 int WindowStyle = 0;
1138 /* Grab the window class we have registered on glutInit(): */
1139 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
1140 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
1145 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
1146 "Game mode being invoked on a subwindow",
1150 * Set the window creation flags appropriately to make the window
1153 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
1157 int worig = w, horig = h;
1159 #if !defined(_WIN32_WCE)
1160 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
1163 * Update the window dimensions, taking account of window
1164 * decorations. "freeglut" is to create the window with the
1165 * outside of its border at (x,y) and with dimensions (w,h).
1167 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
1168 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
1169 GetSystemMetrics( SM_CYCAPTION );
1171 #endif /* defined(_WIN32_WCE) */
1178 /* setting State.Width/Height to call resize callback later */
1181 if( ! window->IsMenu )
1186 else /* fail safe - Windows can make a window of size (0, 0) */
1187 w = h = 300; /* default window size */
1188 window->State.Width = window->State.Height = -1;
1192 window->State.Width = worig;
1193 window->State.Height = horig;
1197 * There's a small difference between creating the top, child and
1200 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
1202 if ( window->IsMenu )
1205 exFlags |= WS_EX_TOOLWINDOW;
1207 #if !defined(_WIN32_WCE)
1208 else if( window->Parent == NULL )
1209 flags |= WS_OVERLAPPEDWINDOW;
1215 #if defined(_WIN32_WCE)
1217 wchar_t* wstr = fghWstrFromStr(title);
1219 window->Window.Handle = CreateWindow(
1222 WS_VISIBLE | WS_POPUP,
1232 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
1233 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
1234 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
1235 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
1236 ShowWindow(window->Window.Handle, SW_SHOW);
1237 UpdateWindow(window->Window.Handle);
1241 /* xoff and yoff are used to place window relative to current display */
1242 /* The operation of gamemode also depends on this */
1244 get_display_origin(&xoff,&yoff);
1246 window->Window.Handle = CreateWindowEx(
1251 x+xoff, y+yoff, w, h,
1252 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
1258 #endif /* defined(_WIN32_WCE) */
1260 if( !( window->Window.Handle ) )
1261 fgError( "Failed to create a window (%s)!", title );
1263 /* Make a menu window always on top - fix Feature Request 947118 */
1264 if( window->IsMenu || gameMode )
1266 window->Window.Handle,
1269 SWP_NOMOVE | SWP_NOSIZE
1272 /* Hack to remove the caption (title bar) and/or border
1273 * and all the system menu controls.
1275 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
1276 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
1278 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1279 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
1281 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
1283 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1284 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
1286 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
1287 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
1290 #if defined(_WIN32_WCE)
1291 ShowWindow( window->Window.Handle, SW_SHOW );
1293 ShowWindow( window->Window.Handle,
1294 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
1295 #endif /* defined(_WIN32_WCE) */
1297 UpdateWindow( window->Window.Handle );
1298 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
1302 fgSetWindow( window );
1304 window->Window.DoubleBuffered =
1305 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
1307 if ( ! window->Window.DoubleBuffered )
1309 glDrawBuffer ( GL_FRONT );
1310 glReadBuffer ( GL_FRONT );
1315 * Closes a window, destroying the frame and OpenGL context
1317 void fgCloseWindow( SFG_Window* window )
1319 /* if we're in gamemode, call glutLeaveGameMode first to make sure the
1320 * gamemode is properly closed before closing the window
1322 if (fgStructure.GameModeWindow != NULL)
1323 glutLeaveGameMode();
1325 #if TARGET_HOST_POSIX_X11
1327 if( window->Window.Context )
1328 glXDestroyContext( fgDisplay.Display, window->Window.Context );
1329 XFree( window->Window.FBConfig );
1331 if( window->Window.Handle ) {
1332 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
1334 /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
1336 #elif TARGET_HOST_MS_WINDOWS
1338 /* Make sure we don't close a window with current context active */
1339 if( fgStructure.CurrentWindow == window )
1340 wglMakeCurrent( NULL, NULL );
1343 * Step through the list of windows. If the rendering context
1344 * is not being used by another window, then we delete it.
1350 for( iter = (SFG_Window *)fgStructure.Windows.First;
1352 iter = (SFG_Window *)iter->Node.Next )
1354 if( ( iter->Window.Context == window->Window.Context ) &&
1355 ( iter != window ) )
1360 wglDeleteContext( window->Window.Context );
1363 DestroyWindow( window->Window.Handle );
1368 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1371 * Creates a new top-level freeglut window
1373 int FGAPIENTRY glutCreateWindow( const char* title )
1375 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
1376 * XXX application has not already done so. The "freeglut" community
1377 * XXX decided not to go this route (freeglut-developer e-mail from
1378 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
1379 * XXX Desired 'freeglut' behaviour when there is no current window"
1381 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
1383 return fgCreateWindow( NULL, title, fgState.Position.Use,
1384 fgState.Position.X, fgState.Position.Y,
1385 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
1386 GL_FALSE, GL_FALSE )->ID;
1389 #if TARGET_HOST_MS_WINDOWS
1390 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )
1392 __glutExitFunc = exit_function;
1393 return glutCreateWindow( title );
1398 * This function creates a sub window.
1400 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
1403 SFG_Window* window = NULL;
1404 SFG_Window* parent = NULL;
1406 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
1407 parent = fgWindowByID( parentID );
1408 freeglut_return_val_if_fail( parent != NULL, 0 );
1411 x = parent->State.Width + x ;
1412 if ( w >= 0 ) x -= w ;
1415 if ( w < 0 ) w = parent->State.Width - x + w ;
1424 y = parent->State.Height + y ;
1425 if ( h >= 0 ) y -= h ;
1428 if ( h < 0 ) h = parent->State.Height - y + h ;
1435 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
1442 * Destroys a window and all of its subwindows
1444 void FGAPIENTRY glutDestroyWindow( int windowID )
1447 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1448 window = fgWindowByID( windowID );
1449 freeglut_return_if_fail( window != NULL );
1451 fgExecutionState ExecState = fgState.ExecState;
1452 fgAddToWindowDestroyList( window );
1453 fgState.ExecState = ExecState;
1458 * This function selects the current window
1460 void FGAPIENTRY glutSetWindow( int ID )
1462 SFG_Window* window = NULL;
1464 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1465 if( fgStructure.CurrentWindow != NULL )
1466 if( fgStructure.CurrentWindow->ID == ID )
1469 window = fgWindowByID( ID );
1470 if( window == NULL )
1472 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1476 fgSetWindow( window );
1480 * This function returns the ID number of the current window, 0 if none exists
1482 int FGAPIENTRY glutGetWindow( void )
1484 SFG_Window *win = fgStructure.CurrentWindow;
1486 * Since GLUT did not throw an error if this function was called without a prior call to
1487 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1488 * See Feature Request "[ 1307049 ] glutInit check".
1490 if ( ! fgState.Initialised )
1493 while ( win && win->IsMenu )
1495 return win ? win->ID : 0;
1499 * This function makes the current window visible
1501 void FGAPIENTRY glutShowWindow( void )
1503 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1504 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1506 #if TARGET_HOST_POSIX_X11
1508 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1509 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1511 #elif TARGET_HOST_MS_WINDOWS
1513 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1517 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1521 * This function hides the current window
1523 void FGAPIENTRY glutHideWindow( void )
1525 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1526 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1528 #if TARGET_HOST_POSIX_X11
1530 if( fgStructure.CurrentWindow->Parent == NULL )
1531 XWithdrawWindow( fgDisplay.Display,
1532 fgStructure.CurrentWindow->Window.Handle,
1535 XUnmapWindow( fgDisplay.Display,
1536 fgStructure.CurrentWindow->Window.Handle );
1537 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1539 #elif TARGET_HOST_MS_WINDOWS
1541 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1545 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1549 * Iconify the current window (top-level windows only)
1551 void FGAPIENTRY glutIconifyWindow( void )
1553 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1554 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1556 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1557 #if TARGET_HOST_POSIX_X11
1559 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1561 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1563 #elif TARGET_HOST_MS_WINDOWS
1565 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1569 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1573 * Set the current window's title
1575 void FGAPIENTRY glutSetWindowTitle( const char* title )
1577 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1578 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1579 if( ! fgStructure.CurrentWindow->Parent )
1581 #if TARGET_HOST_POSIX_X11
1585 text.value = (unsigned char *) title;
1586 text.encoding = XA_STRING;
1588 text.nitems = strlen( title );
1592 fgStructure.CurrentWindow->Window.Handle,
1596 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1598 #elif TARGET_HOST_MS_WINDOWS
1601 wchar_t* wstr = fghWstrFromStr(title);
1602 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1606 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1614 * Set the current window's iconified title
1616 void FGAPIENTRY glutSetIconTitle( const char* title )
1618 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1619 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1621 if( ! fgStructure.CurrentWindow->Parent )
1623 #if TARGET_HOST_POSIX_X11
1627 text.value = (unsigned char *) title;
1628 text.encoding = XA_STRING;
1630 text.nitems = strlen( title );
1634 fgStructure.CurrentWindow->Window.Handle,
1638 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1640 #elif TARGET_HOST_MS_WINDOWS
1643 wchar_t* wstr = fghWstrFromStr(title);
1644 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1648 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1656 * Change the current window's size
1658 void FGAPIENTRY glutReshapeWindow( int width, int height )
1660 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1661 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1663 if (glutGet(GLUT_FULL_SCREEN))
1665 /* Leave full screen state before resizing. */
1666 glutFullScreenToggle();
1669 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1670 fgStructure.CurrentWindow->State.Width = width ;
1671 fgStructure.CurrentWindow->State.Height = height;
1675 * Change the current window's position
1677 void FGAPIENTRY glutPositionWindow( int x, int y )
1679 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1680 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1682 if (glutGet(GLUT_FULL_SCREEN))
1684 /* Leave full screen state before moving. */
1685 glutFullScreenToggle();
1688 #if TARGET_HOST_POSIX_X11
1690 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1692 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1694 #elif TARGET_HOST_MS_WINDOWS
1699 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1700 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1702 fgStructure.CurrentWindow->Window.Handle,
1705 winRect.right - winRect.left,
1706 winRect.bottom - winRect.top,
1715 * Lowers the current window (by Z order change)
1717 void FGAPIENTRY glutPushWindow( void )
1719 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1720 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1722 #if TARGET_HOST_POSIX_X11
1724 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1726 #elif TARGET_HOST_MS_WINDOWS
1729 fgStructure.CurrentWindow->Window.Handle,
1732 SWP_NOSIZE | SWP_NOMOVE
1739 * Raises the current window (by Z order change)
1741 void FGAPIENTRY glutPopWindow( void )
1743 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1744 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1746 #if TARGET_HOST_POSIX_X11
1748 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1750 #elif TARGET_HOST_MS_WINDOWS
1753 fgStructure.CurrentWindow->Window.Handle,
1756 SWP_NOSIZE | SWP_NOMOVE
1763 * Resize the current window so that it fits the whole screen
1765 void FGAPIENTRY glutFullScreen( void )
1769 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1770 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1772 win = fgStructure.CurrentWindow;
1774 #if TARGET_HOST_POSIX_X11
1775 if(!glutGet(GLUT_FULL_SCREEN)) {
1776 if(fghToggleFullscreen() != -1) {
1777 win->State.IsFullscreen = GL_TRUE;
1781 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1783 if (glutGet(GLUT_FULL_SCREEN))
1785 /* Leave full screen state before resizing. */
1786 glutFullScreenToggle();
1792 /* For fullscreen mode, force the top-left corner to 0,0
1793 * and adjust the window rectangle so that the client area
1794 * covers the whole screen.
1799 get_display_origin(&rect.left,&rect.top);
1800 rect.right = fgDisplay.ScreenWidth+rect.left;
1801 rect.bottom = fgDisplay.ScreenHeight+rect.top;
1803 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1804 WS_CLIPCHILDREN, FALSE );
1807 * SWP_NOACTIVATE Do not activate the window
1808 * SWP_NOOWNERZORDER Do not change position in z-order
1809 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1810 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1813 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1817 rect.right - rect.left,
1818 rect.bottom - rect.top,
1819 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1823 win->State.IsFullscreen = GL_TRUE;
1829 * Toggle the window's full screen state.
1831 void FGAPIENTRY glutFullScreenToggle( void )
1835 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1836 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1838 win = fgStructure.CurrentWindow;
1840 #if TARGET_HOST_POSIX_X11
1841 if(fghToggleFullscreen() != -1) {
1842 win->State.IsFullscreen = !win->State.IsFullscreen;
1844 #elif TARGET_HOST_MS_WINDOWS
1846 win->State.IsFullscreen = !win->State.IsFullscreen;
1851 * A.Donev: Set and retrieve the window's user data
1853 void* FGAPIENTRY glutGetWindowData( void )
1855 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1856 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1857 return fgStructure.CurrentWindow->UserData;
1860 void FGAPIENTRY glutSetWindowData(void* data)
1862 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1863 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1864 fgStructure.CurrentWindow->UserData = data;
1867 /*** END OF FILE ***/