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 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 #if TARGET_HOST_POSIX_X11
32 #include <limits.h> /* LONG_MAX */
35 #if defined(_WIN32_WCE)
36 # include <Aygshell.h>
37 # ifdef FREEGLUT_LIB_PRAGMAS
38 # pragma comment( lib, "Aygshell.lib" )
41 static wchar_t* fghWstrFromStr(const char* str)
43 int i,len=strlen(str);
44 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
51 #endif /* defined(_WIN32_WCE) */
54 * TODO BEFORE THE STABLE RELEASE:
56 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
57 * fgSetupPixelFormat -- ignores the display mode settings
58 * fgOpenWindow() -- check the Win32 version, -iconic handling!
59 * fgCloseWindow() -- check the Win32 version
60 * glutCreateWindow() -- Check when default position and size is {-1,-1}
61 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
62 * glutDestroyWindow() -- check the Win32 version
63 * glutSetWindow() -- check the Win32 version
64 * glutGetWindow() -- OK
65 * glutSetWindowTitle() -- check the Win32 version
66 * glutSetIconTitle() -- check the Win32 version
67 * glutShowWindow() -- check the Win32 version
68 * glutHideWindow() -- check the Win32 version
69 * glutIconifyWindow() -- check the Win32 version
70 * glutReshapeWindow() -- check the Win32 version
71 * glutPositionWindow() -- check the Win32 version
72 * glutPushWindow() -- check the Win32 version
73 * glutPopWindow() -- check the Win32 version
76 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
79 * Chooses a visual basing on the current display mode settings
81 #if TARGET_HOST_POSIX_X11
83 GLXFBConfig* fgChooseFBConfig( void )
85 GLboolean wantIndexedMode = GL_FALSE;
89 /* First we have to process the display mode settings... */
91 * XXX Why is there a semi-colon in this #define? The code
92 * XXX that uses the macro seems to always add more semicolons...
94 #define ATTRIB(a) attributes[where++]=a;
95 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
97 if( fgState.DisplayMode & GLUT_INDEX )
99 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
100 /* Buffer size is selected later. */
102 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
103 wantIndexedMode = GL_TRUE;
107 ATTRIB_VAL( GLX_RED_SIZE, 1 );
108 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
109 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
110 if( fgState.DisplayMode & GLUT_ALPHA )
111 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
114 if( fgState.DisplayMode & GLUT_DOUBLE )
115 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
117 if( fgState.DisplayMode & GLUT_STEREO )
118 ATTRIB_VAL( GLX_STEREO, True );
120 if( fgState.DisplayMode & GLUT_DEPTH )
121 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
123 if( fgState.DisplayMode & GLUT_STENCIL )
124 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
126 if( fgState.DisplayMode & GLUT_ACCUM )
128 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
129 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
130 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
131 if( fgState.DisplayMode & GLUT_ALPHA )
132 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
135 if ((fgState.DisplayMode & GLUT_AUX)
136 || (fgState.DisplayMode & GLUT_AUX1)
137 || (fgState.DisplayMode & GLUT_AUX2)
138 || (fgState.DisplayMode & GLUT_AUX3)
139 || (fgState.DisplayMode & GLUT_AUX4))
141 ATTRIB_VAL(GLX_AUX_BUFFERS, fgState.AuxiliaryBufferNumber)
144 if (fgState.DisplayMode & GLUT_MULTISAMPLE)
146 ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1)
147 ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber)
150 /* Push a null at the end of the list */
154 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
155 GLXFBConfig * fbconfig; /* The FBConfig we want */
156 int fbconfigArraySize; /* Number of FBConfigs in the array */
159 /* Get all FBConfigs that match "attributes". */
160 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
163 &fbconfigArraySize );
165 if (fbconfigArray != NULL)
167 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
170 if( wantIndexedMode )
173 * In index mode, we want the largest buffer size, i.e. visual
174 * depth. Here, FBConfigs are sorted by increasing buffer size
175 * first, so FBConfigs with the largest size come last.
178 int bufferSizeMin, bufferSizeMax;
180 /* Get bufferSizeMin. */
182 glXGetFBConfigAttrib( fgDisplay.Display,
186 /* Get bufferSizeMax. */
188 glXGetFBConfigAttrib( fgDisplay.Display,
189 fbconfigArray[fbconfigArraySize - 1],
193 if (bufferSizeMax > bufferSizeMin)
196 * Free and reallocate fbconfigArray, keeping only FBConfigs
197 * with the largest buffer size.
199 XFree(fbconfigArray);
201 /* Add buffer size token at the end of the list. */
203 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
206 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
209 &fbconfigArraySize );
214 * We now have an array of FBConfigs, the first one being the "best"
215 * one. So we should return only this FBConfig:
219 * - pick the XID of the FBConfig we want
220 * result = glXGetFBConfigAttrib( fgDisplay.Display,
226 * XFree(fbconfigArray);
228 * - reset "attributes" with the XID
230 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
233 * - get our FBConfig only
234 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
237 * &fbconfigArraySize );
239 * However, for some configurations (for instance multisampling with
240 * Mesa 6.5.2 and ATI drivers), this does not work:
241 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
242 * XID. Further investigation is needed.
244 * So, for now, we return the whole array of FBConfigs. This should
245 * not produce any side effects elsewhere.
247 fbconfig = fbconfigArray;
257 #endif /* TARGET_HOST_POSIX_X11 */
260 * Setup the pixel format for a Win32 window
262 #if TARGET_HOST_MS_WINDOWS
263 /* The following include file is available from SGI but is not standard:
264 * #include <GL/wglext.h>
265 * So we copy the necessary parts out of it.
266 * XXX: should local definitions for extensions be put in a separate include file?
268 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
270 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
272 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
273 #define WGL_ACCELERATION_ARB 0x2003
274 #define WGL_SUPPORT_OPENGL_ARB 0x2010
275 #define WGL_DOUBLE_BUFFER_ARB 0x2011
276 #define WGL_COLOR_BITS_ARB 0x2014
277 #define WGL_ALPHA_BITS_ARB 0x201B
278 #define WGL_DEPTH_BITS_ARB 0x2022
279 #define WGL_STENCIL_BITS_ARB 0x2023
280 #define WGL_FULL_ACCELERATION_ARB 0x2027
282 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
283 #define WGL_SAMPLES_ARB 0x2042
286 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
287 unsigned char layer_type )
289 #if defined(_WIN32_WCE)
292 PIXELFORMATDESCRIPTOR* ppfd, pfd;
293 int flags, pixelformat;
295 freeglut_return_val_if_fail( window != NULL, 0 );
296 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
297 if( fgState.DisplayMode & GLUT_DOUBLE )
298 flags |= PFD_DOUBLEBUFFER;
300 if( fgState.DisplayMode & GLUT_STEREO )
303 #if defined(_MSC_VER)
304 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
307 /* Specify which pixel format do we opt for... */
308 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
312 if( fgState.DisplayMode & GLUT_INDEX )
314 pfd.iPixelType = PFD_TYPE_COLORINDEX;
322 pfd.iPixelType = PFD_TYPE_RGBA;
326 if ( fgState.DisplayMode & GLUT_ALPHA )
338 pfd.cAccumRedBits = 0;
339 pfd.cAccumGreenBits = 0;
340 pfd.cAccumBlueBits = 0;
341 pfd.cAccumAlphaBits = 0;
344 pfd.cStencilBits = 0;
347 pfd.cStencilBits = 8;
349 if( fgState.DisplayMode & GLUT_AUX4 )
351 else if( fgState.DisplayMode & GLUT_AUX3 )
353 else if( fgState.DisplayMode & GLUT_AUX2 )
355 else if( fgState.DisplayMode & GLUT_AUX1 )
360 pfd.iLayerType = layer_type;
363 pfd.dwVisibleMask = 0;
364 pfd.dwDamageMask = 0;
366 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
369 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
371 /* windows hack for multismapling */
372 if (fgState.DisplayMode&GLUT_MULTISAMPLE)
374 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
375 HGLRC rc, rc_before=wglGetCurrentContext();
377 HDC hDC, hDC_before=wglGetCurrentDC();
380 /* create a dummy window */
381 ZeroMemory(&wndCls, sizeof(wndCls));
382 wndCls.lpfnWndProc = DefWindowProc;
383 wndCls.hInstance = fgDisplay.Instance;
384 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
385 wndCls.lpszClassName = _T("FREEGLUT_dummy");
386 RegisterClass( &wndCls );
388 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
390 SetPixelFormat( hDC, pixelformat, ppfd );
392 rc = wglCreateContext( hDC );
393 wglMakeCurrent(hDC, rc);
395 wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
396 if (wglGetEntensionsStringARB)
398 const char * pWglExtString=wglGetEntensionsStringARB(hDC);
401 if (strstr(pWglExtString, "WGL_ARB_multisample"))
403 int pAttributes[100];
407 float fAttributes[] = {0,0};
409 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
411 wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
412 if ( wglChoosePixelFormatARBProc )
414 pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB; pAttributes[iCounter++]=GL_TRUE;
415 pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB; pAttributes[iCounter++]=GL_TRUE;
416 pAttributes[iCounter++]=WGL_ACCELERATION_ARB; pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
418 pAttributes[iCounter++]=WGL_COLOR_BITS_ARB; pAttributes[iCounter++]=pfd.cColorBits ;
419 pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB; pAttributes[iCounter++]=pfd.cAlphaBits;
420 pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB; pAttributes[iCounter++]=pfd.cDepthBits;
421 pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB; pAttributes[iCounter++]=pfd.cStencilBits;
423 pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB; pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
424 pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB; pAttributes[iCounter++]=GL_TRUE;
425 pAttributes[iCounter++]=WGL_SAMPLES_ARB; pAttributes[iCounter++]=4;
426 pAttributes[iCounter++]=0; pAttributes[iCounter++]=0; /* terminator */
428 bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
430 if (bValid && numFormats>0)
431 pixelformat=iPixelFormat;
434 wglMakeCurrent( hDC_before, rc_before);
435 wglDeleteContext(rc);
436 ReleaseDC(hWnd, hDC);
438 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
443 if( pixelformat == 0 )
448 return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
449 #endif /* defined(_WIN32_WCE) */
451 #endif /* TARGET_HOST_MS_WINDOWS */
454 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
455 * the window structure passed in.
457 void fgSetWindow ( SFG_Window *window )
459 #if TARGET_HOST_POSIX_X11
461 glXMakeContextCurrent(
463 window->Window.Handle,
464 window->Window.Handle,
465 window->Window.Context
467 #elif TARGET_HOST_MS_WINDOWS
468 if( fgStructure.CurrentWindow )
469 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
470 fgStructure.CurrentWindow->Window.Device );
474 window->Window.Device = GetDC( window->Window.Handle );
476 window->Window.Device,
477 window->Window.Context
481 fgStructure.CurrentWindow = window;
486 #if TARGET_HOST_POSIX_X11
488 #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
489 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
492 #ifndef GLX_CONTEXT_MINOR_VERSION_ARB
493 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
496 #ifndef GLX_CONTEXT_FLAGS_ARB
497 #define GLX_CONTEXT_FLAGS_ARB 0x2094
500 #ifndef GLX_CONTEXT_DEBUG_BIT_ARB
501 #define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001
504 #ifndef GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
505 #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
508 typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
509 GLXContext share_list, Bool direct,
510 const int *attrib_list);
512 static GLXContext fghCreateNewContext( SFG_Window* window )
514 /* for color model calculation */
515 int menu = ( window->IsMenu && !fgStructure.MenuContext );
516 int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
518 /* "classic" context creation */
519 Display *dpy = fgDisplay.Display;
520 GLXFBConfig config = *(window->Window.FBConfig);
521 int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
522 GLXContext share_list = NULL;
523 Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
526 /* new context creation */
528 CreateContextAttribsProc createContextAttribs;
530 /* If nothing fancy has been required, simply use the old context creation GLX API entry */
531 if ( fgState.MajorVersion == 1 && fgState.MinorVersion == 0 && fgState.ContextFlags == 0)
533 context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
534 if ( context == NULL ) {
535 fgError( "could not create new OpenGL context" );
540 /* color index mode is not available anymore with OpenGL 3.0 */
541 if ( render_type == GLX_COLOR_INDEX_TYPE ) {
542 fgWarning( "color index mode is deprecated, using RGBA mode" );
545 attribs[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
546 attribs[1] = fgState.MajorVersion;
547 attribs[2] = GLX_CONTEXT_MINOR_VERSION_ARB;
548 attribs[3] = fgState.MinorVersion;
549 attribs[4] = GLX_CONTEXT_FLAGS_ARB;
550 attribs[5] = ((fgState.ContextFlags & GLUT_DEBUG) ? GLX_CONTEXT_DEBUG_BIT_ARB : 0) |
551 ((fgState.ContextFlags & GLUT_FORWARD_COMPATIBLE) ? GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB : 0);
554 createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
555 if ( createContextAttribs == NULL ) {
556 fgError( "glXCreateContextAttribsARB not found" );
559 context = createContextAttribs( dpy, config, share_list, direct, attribs );
560 if ( context == NULL ) {
561 fgError( "could not create new OpenGL %d.%d context (flags %x)",
562 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags );
570 * Opens a window. Requires a SFG_Window object created and attached
571 * to the freeglut structure. OpenGL context is created here.
573 void fgOpenWindow( SFG_Window* window, const char* title,
574 GLboolean positionUse, int x, int y,
575 GLboolean sizeUse, int w, int h,
576 GLboolean gameMode, GLboolean isSubWindow )
578 #if TARGET_HOST_POSIX_X11
579 XVisualInfo * visualInfo;
580 XSetWindowAttributes winAttr;
581 XTextProperty textProperty;
582 XSizeHints sizeHints;
585 unsigned int current_DisplayMode = fgState.DisplayMode ;
587 /* Save the display mode if we are creating a menu window */
588 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
589 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
591 window->Window.FBConfig = fgChooseFBConfig( );
593 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
594 fgState.DisplayMode = current_DisplayMode ;
596 if( ! window->Window.FBConfig )
599 * The "fgChooseFBConfig" returned a null meaning that the visual
600 * context is not available.
601 * Try a couple of variations to see if they will work.
603 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
605 fgState.DisplayMode |= GLUT_DOUBLE ;
606 window->Window.FBConfig = fgChooseFBConfig( );
607 fgState.DisplayMode &= ~GLUT_DOUBLE;
610 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
612 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
613 window->Window.FBConfig = fgChooseFBConfig( );
614 fgState.DisplayMode |= GLUT_MULTISAMPLE;
618 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
619 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
621 /* Get the X visual. */
622 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
623 *(window->Window.FBConfig) );
626 * XXX HINT: the masks should be updated when adding/removing callbacks.
627 * XXX This might speed up message processing. Is that true?
629 * XXX A: Not appreciably, but it WILL make it easier to debug.
630 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
631 * XXX turns off events that it doesn't need and is a whole lot
632 * XXX more pleasant to trace. (Think mouse-motion! Tons of
633 * XXX ``bonus'' GUI events stream in.)
636 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
637 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
638 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
639 PointerMotionMask | ButtonMotionMask;
640 winAttr.background_pixmap = None;
641 winAttr.background_pixel = 0;
642 winAttr.border_pixel = 0;
644 winAttr.colormap = XCreateColormap(
645 fgDisplay.Display, fgDisplay.RootWindow,
646 visualInfo->visual, AllocNone
649 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
651 if( window->IsMenu || ( gameMode == GL_TRUE ) )
653 winAttr.override_redirect = True;
654 mask |= CWOverrideRedirect;
658 x = y = -1; /* default window position */
660 w = h = 300; /* default window size */
662 window->Window.Handle = XCreateWindow(
664 window->Parent == NULL ? fgDisplay.RootWindow :
665 window->Parent->Window.Handle,
667 visualInfo->depth, InputOutput,
668 visualInfo->visual, mask,
673 * The GLX context creation, possibly trying the direct context rendering
674 * or else use the current context if the user has so specified
680 * If there isn't already an OpenGL rendering context for menu
683 if( !fgStructure.MenuContext )
685 fgStructure.MenuContext =
686 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
687 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
690 /* window->Window.Context = fgStructure.MenuContext->MContext; */
691 window->Window.Context = fghCreateNewContext( window );
693 else if( fgState.UseCurrentContext )
695 window->Window.Context = glXGetCurrentContext( );
697 if( ! window->Window.Context )
698 window->Window.Context = fghCreateNewContext( window );
701 window->Window.Context = fghCreateNewContext( window );
703 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
704 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
706 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
707 fgError( "Unable to force direct context rendering for window '%s'",
713 * XXX Assume the new window is visible by default
714 * XXX Is this a safe assumption?
716 window->State.Visible = GL_TRUE;
720 sizeHints.flags |= USPosition;
722 sizeHints.flags |= USSize;
725 * Fill in the size hints values now (the x, y, width and height
726 * settings are obsolete, are there any more WMs that support them?)
727 * Unless the X servers actually stop supporting these, we should
728 * continue to fill them in. It is *not* our place to tell the user
729 * that they should replace a window manager that they like, and which
730 * works, just because *we* think that it's not "modern" enough.
735 sizeHints.height = h;
737 wmHints.flags = StateHint;
738 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
739 /* Prepare the window and iconified window names... */
740 XStringListToTextProperty( (char **) &title, 1, &textProperty );
744 window->Window.Handle,
753 XFree( textProperty.value );
755 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
756 &fgDisplay.DeleteWindow, 1 );
758 glXMakeContextCurrent(
760 window->Window.Handle,
761 window->Window.Handle,
762 window->Window.Context
765 XMapWindow( fgDisplay.Display, window->Window.Handle );
769 #elif TARGET_HOST_MS_WINDOWS
777 /* Grab the window class we have registered on glutInit(): */
778 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
779 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
784 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
785 "Game mode being invoked on a subwindow",
789 * Set the window creation flags appropriately to make the window
792 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
796 int worig = w, horig = h;
798 #if !defined(_WIN32_WCE)
799 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
802 * Update the window dimensions, taking account of window
803 * decorations. "freeglut" is to create the window with the
804 * outside of its border at (x,y) and with dimensions (w,h).
806 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
807 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
808 GetSystemMetrics( SM_CYCAPTION );
810 #endif /* defined(_WIN32_WCE) */
817 /* setting State.Width/Height to call resize callback later */
820 if( ! window->IsMenu )
825 else /* fail safe - Windows can make a window of size (0, 0) */
826 w = h = 300; /* default window size */
827 window->State.Width = window->State.Height = -1;
831 window->State.Width = worig;
832 window->State.Height = horig;
836 * There's a small difference between creating the top, child and
839 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
841 if ( window->IsMenu )
844 exFlags |= WS_EX_TOOLWINDOW;
846 #if !defined(_WIN32_WCE)
847 else if( window->Parent == NULL )
848 flags |= WS_OVERLAPPEDWINDOW;
854 #if defined(_WIN32_WCE)
856 wchar_t* wstr = fghWstrFromStr(title);
858 window->Window.Handle = CreateWindow(
861 WS_VISIBLE | WS_POPUP,
871 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
872 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
873 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
874 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
875 ShowWindow(window->Window.Handle, SW_SHOW);
876 UpdateWindow(window->Window.Handle);
879 window->Window.Handle = CreateWindowEx(
885 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
890 #endif /* defined(_WIN32_WCE) */
892 if( !( window->Window.Handle ) )
893 fgError( "Failed to create a window (%s)!", title );
895 /* Make a menu window always on top - fix Feature Request 947118 */
896 if( window->IsMenu || gameMode )
898 window->Window.Handle,
901 SWP_NOMOVE | SWP_NOSIZE
904 /* Hack to remove the caption (title bar) and/or border
905 * and all the system menu controls.
907 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
908 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
910 SetWindowLong ( window->Window.Handle, GWL_STYLE,
911 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
913 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
915 SetWindowLong ( window->Window.Handle, GWL_STYLE,
916 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
918 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
919 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
922 #if defined(_WIN32_WCE)
923 ShowWindow( window->Window.Handle, SW_SHOW );
925 ShowWindow( window->Window.Handle,
926 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
927 #endif /* defined(_WIN32_WCE) */
929 UpdateWindow( window->Window.Handle );
930 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
934 fgSetWindow( window );
936 window->Window.DoubleBuffered =
937 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
939 if ( ! window->Window.DoubleBuffered )
941 glDrawBuffer ( GL_FRONT );
942 glReadBuffer ( GL_FRONT );
947 * Closes a window, destroying the frame and OpenGL context
949 void fgCloseWindow( SFG_Window* window )
951 #if TARGET_HOST_POSIX_X11
953 glXDestroyContext( fgDisplay.Display, window->Window.Context );
954 XFree( window->Window.FBConfig );
955 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
956 /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
958 #elif TARGET_HOST_MS_WINDOWS
960 /* Make sure we don't close a window with current context active */
961 if( fgStructure.CurrentWindow == window )
962 wglMakeCurrent( NULL, NULL );
965 * Step through the list of windows. If the rendering context
966 * is not being used by another window, then we delete it.
972 for( iter = (SFG_Window *)fgStructure.Windows.First;
974 iter = (SFG_Window *)iter->Node.Next )
976 if( ( iter->Window.Context == window->Window.Context ) &&
982 wglDeleteContext( window->Window.Context );
985 DestroyWindow( window->Window.Handle );
990 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
993 * Creates a new top-level freeglut window
995 int FGAPIENTRY glutCreateWindow( const char* title )
997 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
998 * XXX application has not already done so. The "freeglut" community
999 * XXX decided not to go this route (freeglut-developer e-mail from
1000 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
1001 * XXX Desired 'freeglut' behaviour when there is no current window"
1003 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
1005 return fgCreateWindow( NULL, title, fgState.Position.Use,
1006 fgState.Position.X, fgState.Position.Y,
1007 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
1008 GL_FALSE, GL_FALSE )->ID;
1012 * This function creates a sub window.
1014 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
1017 SFG_Window* window = NULL;
1018 SFG_Window* parent = NULL;
1020 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
1021 parent = fgWindowByID( parentID );
1022 freeglut_return_val_if_fail( parent != NULL, 0 );
1025 x = parent->State.Width + x ;
1026 if ( w >= 0 ) x -= w ;
1029 if ( w < 0 ) w = parent->State.Width - x + w ;
1038 y = parent->State.Height + y ;
1039 if ( h >= 0 ) y -= h ;
1042 if ( h < 0 ) h = parent->State.Height - y + h ;
1049 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
1056 * Destroys a window and all of its subwindows
1058 void FGAPIENTRY glutDestroyWindow( int windowID )
1061 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1062 window = fgWindowByID( windowID );
1063 freeglut_return_if_fail( window != NULL );
1065 fgExecutionState ExecState = fgState.ExecState;
1066 fgAddToWindowDestroyList( window );
1067 fgState.ExecState = ExecState;
1072 * This function selects the current window
1074 void FGAPIENTRY glutSetWindow( int ID )
1076 SFG_Window* window = NULL;
1078 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1079 if( fgStructure.CurrentWindow != NULL )
1080 if( fgStructure.CurrentWindow->ID == ID )
1083 window = fgWindowByID( ID );
1084 if( window == NULL )
1086 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1090 fgSetWindow( window );
1094 * This function returns the ID number of the current window, 0 if none exists
1096 int FGAPIENTRY glutGetWindow( void )
1098 SFG_Window *win = fgStructure.CurrentWindow;
1100 * Since GLUT did not throw an error if this function was called without a prior call to
1101 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1102 * See Feature Request "[ 1307049 ] glutInit check".
1104 if ( ! fgState.Initialised )
1107 while ( win && win->IsMenu )
1109 return win ? win->ID : 0;
1113 * This function makes the current window visible
1115 void FGAPIENTRY glutShowWindow( void )
1117 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1118 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1120 #if TARGET_HOST_POSIX_X11
1122 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1123 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1125 #elif TARGET_HOST_MS_WINDOWS
1127 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1131 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1135 * This function hides the current window
1137 void FGAPIENTRY glutHideWindow( void )
1139 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1140 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1142 #if TARGET_HOST_POSIX_X11
1144 if( fgStructure.CurrentWindow->Parent == NULL )
1145 XWithdrawWindow( fgDisplay.Display,
1146 fgStructure.CurrentWindow->Window.Handle,
1149 XUnmapWindow( fgDisplay.Display,
1150 fgStructure.CurrentWindow->Window.Handle );
1151 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1153 #elif TARGET_HOST_MS_WINDOWS
1155 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1159 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1163 * Iconify the current window (top-level windows only)
1165 void FGAPIENTRY glutIconifyWindow( void )
1167 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1168 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1170 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1171 #if TARGET_HOST_POSIX_X11
1173 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1175 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1177 #elif TARGET_HOST_MS_WINDOWS
1179 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1183 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1187 * Set the current window's title
1189 void FGAPIENTRY glutSetWindowTitle( const char* title )
1191 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1192 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1193 if( ! fgStructure.CurrentWindow->Parent )
1195 #if TARGET_HOST_POSIX_X11
1199 text.value = (unsigned char *) title;
1200 text.encoding = XA_STRING;
1202 text.nitems = strlen( title );
1206 fgStructure.CurrentWindow->Window.Handle,
1210 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1212 #elif TARGET_HOST_MS_WINDOWS
1215 wchar_t* wstr = fghWstrFromStr(title);
1216 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1220 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1228 * Set the current window's iconified title
1230 void FGAPIENTRY glutSetIconTitle( const char* title )
1232 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1233 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1235 if( ! fgStructure.CurrentWindow->Parent )
1237 #if TARGET_HOST_POSIX_X11
1241 text.value = (unsigned char *) title;
1242 text.encoding = XA_STRING;
1244 text.nitems = strlen( title );
1248 fgStructure.CurrentWindow->Window.Handle,
1252 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1254 #elif TARGET_HOST_MS_WINDOWS
1257 wchar_t* wstr = fghWstrFromStr(title);
1258 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1262 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1270 * Change the current window's size
1272 void FGAPIENTRY glutReshapeWindow( int width, int height )
1274 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1275 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1277 if (glutGet(GLUT_FULL_SCREEN))
1279 /* Leave full screen state before resizing. */
1280 glutFullScreenToggle();
1283 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1284 fgStructure.CurrentWindow->State.Width = width ;
1285 fgStructure.CurrentWindow->State.Height = height;
1289 * Change the current window's position
1291 void FGAPIENTRY glutPositionWindow( int x, int y )
1293 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1294 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1296 if (glutGet(GLUT_FULL_SCREEN))
1298 /* Leave full screen state before moving. */
1299 glutFullScreenToggle();
1302 #if TARGET_HOST_POSIX_X11
1304 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1306 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1308 #elif TARGET_HOST_MS_WINDOWS
1313 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1314 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1316 fgStructure.CurrentWindow->Window.Handle,
1319 winRect.right - winRect.left,
1320 winRect.bottom - winRect.top,
1329 * Lowers the current window (by Z order change)
1331 void FGAPIENTRY glutPushWindow( void )
1333 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1334 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1336 #if TARGET_HOST_POSIX_X11
1338 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1340 #elif TARGET_HOST_MS_WINDOWS
1343 fgStructure.CurrentWindow->Window.Handle,
1346 SWP_NOSIZE | SWP_NOMOVE
1353 * Raises the current window (by Z order change)
1355 void FGAPIENTRY glutPopWindow( void )
1357 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1358 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1360 #if TARGET_HOST_POSIX_X11
1362 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1364 #elif TARGET_HOST_MS_WINDOWS
1367 fgStructure.CurrentWindow->Window.Handle,
1370 SWP_NOSIZE | SWP_NOMOVE
1377 * Resize the current window so that it fits the whole screen
1379 void FGAPIENTRY glutFullScreen( void )
1381 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1382 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1384 if (glutGet(GLUT_FULL_SCREEN))
1386 /* Leave full screen state before resizing. */
1387 glutFullScreenToggle();
1391 #if TARGET_HOST_POSIX_X11
1393 Status status; /* Returned by XGetWindowAttributes(), not checked. */
1394 XWindowAttributes attributes;
1396 status = XGetWindowAttributes(fgDisplay.Display,
1397 fgStructure.CurrentWindow->Window.Handle,
1400 * The "x" and "y" members of "attributes" are the window's coordinates
1401 * relative to its parent, i.e. to the decoration window.
1403 XMoveResizeWindow(fgDisplay.Display,
1404 fgStructure.CurrentWindow->Window.Handle,
1407 fgDisplay.ScreenWidth,
1408 fgDisplay.ScreenHeight);
1410 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1413 /* For fullscreen mode, force the top-left corner to 0,0
1414 * and adjust the window rectangle so that the client area
1415 * covers the whole screen.
1420 rect.right = fgDisplay.ScreenWidth;
1421 rect.bottom = fgDisplay.ScreenHeight;
1423 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1424 WS_CLIPCHILDREN, FALSE );
1427 * SWP_NOACTIVATE Do not activate the window
1428 * SWP_NOOWNERZORDER Do not change position in z-order
1429 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1430 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1433 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1437 rect.right - rect.left,
1438 rect.bottom - rect.top,
1439 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1447 * Toggle the window's full screen state.
1449 void FGAPIENTRY glutFullScreenToggle( void )
1451 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1452 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1455 #if TARGET_HOST_POSIX_X11
1457 if (fgDisplay.StateFullScreen != None)
1463 xevent.type = ClientMessage;
1464 xevent.xclient.type = ClientMessage;
1465 xevent.xclient.serial = 0;
1466 xevent.xclient.send_event = True;
1467 xevent.xclient.display = fgDisplay.Display;
1468 xevent.xclient.window = fgStructure.CurrentWindow->Window.Handle;
1469 xevent.xclient.message_type = fgDisplay.State;
1470 xevent.xclient.format = 32;
1471 xevent.xclient.data.l[0] = 2; /* _NET_WM_STATE_TOGGLE */
1472 xevent.xclient.data.l[1] = fgDisplay.StateFullScreen;
1473 xevent.xclient.data.l[2] = 0;
1474 xevent.xclient.data.l[3] = 0;
1475 xevent.xclient.data.l[4] = 0;
1477 /*** Don't really understand how event masks work... ***/
1478 event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
1480 status = XSendEvent(fgDisplay.Display,
1481 fgDisplay.RootWindow,
1485 FREEGLUT_INTERNAL_ERROR_EXIT(status != 0,
1486 "XSendEvent failed",
1487 "glutFullScreenToggle");
1493 * If the window manager is not Net WM compliant, fall back to legacy
1502 * A.Donev: Set and retrieve the window's user data
1504 void* FGAPIENTRY glutGetWindowData( void )
1506 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1507 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1508 return fgStructure.CurrentWindow->UserData;
1511 void FGAPIENTRY glutSetWindowData(void* data)
1513 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1514 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1515 fgStructure.CurrentWindow->UserData = data;
1518 /*** END OF FILE ***/