Adding "glutFullScreenToggle" for X11 -- still needs implementation in Windows (e...
[freeglut] / src / freeglut_window.c
1 /*
2  * freeglut_window.c
3  *
4  * Window management methods.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 #if TARGET_HOST_POSIX_X11
32 #include <limits.h>  /* LONG_MAX */
33 #endif
34
35 #if defined(_WIN32_WCE)
36 #   include <Aygshell.h>
37 #   ifdef FREEGLUT_LIB_PRAGMAS
38 #       pragma comment( lib, "Aygshell.lib" )
39 #   endif
40
41 static wchar_t* fghWstrFromStr(const char* str)
42 {
43     int i,len=strlen(str);
44     wchar_t* wstr = (wchar_t*)malloc(2*len+2);
45     for(i=0; i<len; i++)
46         wstr[i] = str[i];
47     wstr[len] = 0;
48     return wstr;
49 }
50
51 #endif /* defined(_WIN32_WCE) */
52
53 /*
54  * TODO BEFORE THE STABLE RELEASE:
55  *
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
74  */
75
76 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
77
78 /*
79  * Chooses a visual basing on the current display mode settings
80  */
81 #if TARGET_HOST_POSIX_X11
82
83 GLXFBConfig* fgChooseFBConfig( void )
84 {
85     GLboolean wantIndexedMode = GL_FALSE;
86     int attributes[ 32 ];
87     int where = 0;
88
89     /* First we have to process the display mode settings... */
90 /*
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...
93  */
94 #define ATTRIB(a) attributes[where++]=a;
95 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
96
97     if( fgState.DisplayMode & GLUT_INDEX )
98     {
99         ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
100         /*  Buffer size is selected later.  */
101
102         ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
103         wantIndexedMode = GL_TRUE;
104     }
105     else
106     {
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 );
112     }
113
114     if( fgState.DisplayMode & GLUT_DOUBLE )
115         ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
116
117     if( fgState.DisplayMode & GLUT_STEREO )
118         ATTRIB_VAL( GLX_STEREO, True );
119
120     if( fgState.DisplayMode & GLUT_DEPTH )
121         ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
122
123     if( fgState.DisplayMode & GLUT_STENCIL )
124         ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
125
126     if( fgState.DisplayMode & GLUT_ACCUM )
127     {
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 );
133     }
134
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))
140       {
141         ATTRIB_VAL(GLX_AUX_BUFFERS, fgState.AuxiliaryBufferNumber)
142       }
143
144     if (fgState.DisplayMode & GLUT_MULTISAMPLE)
145       {
146         ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1)
147         ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber)
148       }
149
150     /* Push a null at the end of the list */
151     ATTRIB( None );
152
153     {
154         GLXFBConfig * fbconfigArray;  /*  Array of FBConfigs  */
155         GLXFBConfig * fbconfig;       /*  The FBConfig we want  */
156         int fbconfigArraySize;        /*  Number of FBConfigs in the array  */
157
158
159         /*  Get all FBConfigs that match "attributes".  */
160         fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
161                                            fgDisplay.Screen,
162                                            attributes,
163                                            &fbconfigArraySize );
164
165         if (fbconfigArray != NULL)
166         {
167             int result;  /* Returned by glXGetFBConfigAttrib, not checked. */
168
169
170             if( wantIndexedMode )
171             {
172                 /*
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.
176                  */
177
178                 int bufferSizeMin, bufferSizeMax;
179
180                 /*  Get bufferSizeMin.  */
181                 result =
182                   glXGetFBConfigAttrib( fgDisplay.Display,
183                                         fbconfigArray[0],
184                                         GLX_BUFFER_SIZE,
185                                         &bufferSizeMin );
186                 /*  Get bufferSizeMax.  */
187                 result =
188                   glXGetFBConfigAttrib( fgDisplay.Display,
189                                         fbconfigArray[fbconfigArraySize - 1],
190                                         GLX_BUFFER_SIZE,
191                                         &bufferSizeMax );
192
193                 if (bufferSizeMax > bufferSizeMin)
194                 {
195                     /* 
196                      * Free and reallocate fbconfigArray, keeping only FBConfigs
197                      * with the largest buffer size.
198                      */
199                     XFree(fbconfigArray);
200
201                     /*  Add buffer size token at the end of the list.  */
202                     where--;
203                     ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
204                     ATTRIB( None );
205
206                     fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
207                                                        fgDisplay.Screen,
208                                                        attributes,
209                                                        &fbconfigArraySize );
210                 }
211             }
212
213             /*
214              * We now have an array of FBConfigs, the first one being the "best"
215              * one.  So we should return only this FBConfig:
216              *
217              * int fbconfigXID;
218              *
219              *  - pick the XID of the FBConfig we want
220              * result = glXGetFBConfigAttrib( fgDisplay.Display,
221              *                                fbconfigArray[0],
222              *                                GLX_FBCONFIG_ID,
223              *                                &fbconfigXID );
224              *
225              * - free the array
226              * XFree(fbconfigArray);
227              *
228              * - reset "attributes" with the XID
229              * where = 0;
230              * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
231              * ATTRIB( None );
232              *
233              * - get our FBConfig only
234              * fbconfig = glXChooseFBConfig( fgDisplay.Display,
235              *                               fgDisplay.Screen,
236              *                               attributes,
237              *                               &fbconfigArraySize );
238              *
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.
243              *
244              * So, for now, we return the whole array of FBConfigs.  This should
245              * not produce any side effects elsewhere.
246              */
247             fbconfig = fbconfigArray;
248         }
249         else
250         {
251            fbconfig = NULL;
252         }
253
254         return fbconfig;
255     }
256 }
257 #endif /* TARGET_HOST_POSIX_X11 */
258
259 /*
260  * Setup the pixel format for a Win32 window
261  */
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?
267  */
268 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
269
270 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
271
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
281
282 #define WGL_SAMPLE_BUFFERS_ARB         0x2041
283 #define WGL_SAMPLES_ARB                0x2042
284
285
286 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
287                               unsigned char layer_type )
288 {
289 #if defined(_WIN32_WCE)
290     return GL_TRUE;
291 #else
292     PIXELFORMATDESCRIPTOR* ppfd, pfd;
293     int flags, pixelformat;
294
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;
299
300     if( fgState.DisplayMode & GLUT_STEREO )
301         flags |= PFD_STEREO;
302
303 #if defined(_MSC_VER)
304 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
305 #endif
306
307     /* Specify which pixel format do we opt for... */
308     pfd.nSize           = sizeof(PIXELFORMATDESCRIPTOR);
309     pfd.nVersion        = 1;
310     pfd.dwFlags         = flags;
311
312     if( fgState.DisplayMode & GLUT_INDEX )
313     {
314         pfd.iPixelType = PFD_TYPE_COLORINDEX;
315         pfd.cRedBits                = 0;
316         pfd.cGreenBits            = 0;
317         pfd.cBlueBits             = 0;
318         pfd.cAlphaBits            = 0;
319     }
320     else
321     {
322         pfd.iPixelType      = PFD_TYPE_RGBA;
323         pfd.cRedBits                = 8;
324         pfd.cGreenBits            = 8;
325         pfd.cBlueBits             = 8;
326         if ( fgState.DisplayMode & GLUT_ALPHA )
327             pfd.cAlphaBits            = 8;
328         else
329             pfd.cAlphaBits            = 0;
330     }
331
332     pfd.cColorBits      = 24;
333     pfd.cRedShift       = 0;
334     pfd.cGreenShift     = 0;
335     pfd.cBlueShift      = 0;
336     pfd.cAlphaShift     = 0;
337     pfd.cAccumBits      = 0;
338     pfd.cAccumRedBits   = 0;
339     pfd.cAccumGreenBits = 0;
340     pfd.cAccumBlueBits  = 0;
341     pfd.cAccumAlphaBits = 0;
342 #if 0
343     pfd.cDepthBits      = 32;
344     pfd.cStencilBits    = 0;
345 #else
346     pfd.cDepthBits      = 24;
347     pfd.cStencilBits    = 8;
348 #endif
349     if( fgState.DisplayMode & GLUT_AUX4 )
350         pfd.cAuxBuffers = 4;
351     else if( fgState.DisplayMode & GLUT_AUX3 )
352         pfd.cAuxBuffers = 3;
353     else if( fgState.DisplayMode & GLUT_AUX2 )
354         pfd.cAuxBuffers = 2;
355     else if( fgState.DisplayMode & GLUT_AUX1 )
356         pfd.cAuxBuffers = 1;
357     else
358         pfd.cAuxBuffers = 0;
359
360     pfd.iLayerType      = layer_type;
361     pfd.bReserved       = 0;
362     pfd.dwLayerMask     = 0;
363     pfd.dwVisibleMask   = 0;
364     pfd.dwDamageMask    = 0;
365
366     pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
367     ppfd = &pfd;
368
369     pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
370
371     /* windows hack for multismapling */
372     if (fgState.DisplayMode&GLUT_MULTISAMPLE)
373     {        
374         PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
375         HGLRC rc, rc_before=wglGetCurrentContext();
376         HWND hWnd;
377         HDC hDC, hDC_before=wglGetCurrentDC();
378         WNDCLASS wndCls;
379         ATOM atom;
380
381         /* create a dummy window */
382         ZeroMemory(&wndCls, sizeof(wndCls));
383                 wndCls.lpfnWndProc        = DefWindowProc;
384                 wndCls.hInstance            = fgDisplay.Instance;
385                 wndCls.style                    = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
386                 wndCls.lpszClassName    = _T("FREEGLUT_dummy");
387         atom = RegisterClass( &wndCls );
388
389         hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
390         hDC=GetDC(hWnd);
391         SetPixelFormat( hDC, pixelformat, ppfd );
392         
393         rc = wglCreateContext( hDC );
394         wglMakeCurrent(hDC, rc);
395         
396         wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
397         if (wglGetEntensionsStringARB)
398         {
399             const char * pWglExtString=wglGetEntensionsStringARB(hDC);
400             if (pWglExtString)
401             {
402                 if (strstr(pWglExtString, "WGL_ARB_multisample"))
403                 {
404                     int pAttributes[100];
405                     int iCounter=0;
406                     int iPixelFormat;
407                     BOOL bValid;
408                     float fAttributes[] = {0,0};
409                     UINT numFormats;
410                     PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
411
412                     wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
413                     if ( wglChoosePixelFormatARBProc )
414                     {
415                         pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB;        pAttributes[iCounter++]=GL_TRUE;
416                         pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB;        pAttributes[iCounter++]=GL_TRUE;
417                         pAttributes[iCounter++]=WGL_ACCELERATION_ARB;        pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
418
419                         pAttributes[iCounter++]=WGL_COLOR_BITS_ARB;            pAttributes[iCounter++]=pfd.cColorBits ;
420                         pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB;            pAttributes[iCounter++]=pfd.cAlphaBits;
421                         pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB;            pAttributes[iCounter++]=pfd.cDepthBits;
422                         pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB;        pAttributes[iCounter++]=pfd.cStencilBits;
423
424                         pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB;        pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
425                         pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB;        pAttributes[iCounter++]=GL_TRUE;
426                         pAttributes[iCounter++]=WGL_SAMPLES_ARB;            pAttributes[iCounter++]=4;
427                         pAttributes[iCounter++]=0;                            pAttributes[iCounter++]=0;    /* terminator */
428
429                         bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
430
431                         if (bValid && numFormats>0)
432                             pixelformat=iPixelFormat;
433                     }
434                 }
435                 wglMakeCurrent( hDC_before, rc_before);
436                 wglDeleteContext(rc);
437                 ReleaseDC(hWnd, hDC);
438                 DestroyWindow(hWnd);
439                 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
440             }
441         }
442     }
443
444     if( pixelformat == 0 )
445         return GL_FALSE;
446
447     if( checkOnly )
448         return GL_TRUE;
449     return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
450 #endif /* defined(_WIN32_WCE) */
451 }
452 #endif /* TARGET_HOST_MS_WINDOWS */
453
454 /*
455  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
456  * the window structure passed in.
457  */
458 void fgSetWindow ( SFG_Window *window )
459 {
460 #if TARGET_HOST_POSIX_X11
461     if ( window )
462         glXMakeContextCurrent(
463             fgDisplay.Display,
464             window->Window.Handle,
465             window->Window.Handle,
466             window->Window.Context
467         );
468 #elif TARGET_HOST_MS_WINDOWS
469     if( fgStructure.CurrentWindow )
470         ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
471                    fgStructure.CurrentWindow->Window.Device );
472
473     if ( window )
474     {
475         window->Window.Device = GetDC( window->Window.Handle );
476         wglMakeCurrent(
477             window->Window.Device,
478             window->Window.Context
479         );
480     }
481 #endif
482     fgStructure.CurrentWindow = window;
483 }
484
485
486 /*
487  * Opens a window. Requires a SFG_Window object created and attached
488  * to the freeglut structure. OpenGL context is created here.
489  */
490 void fgOpenWindow( SFG_Window* window, const char* title,
491                    GLboolean positionUse, int x, int y,
492                    GLboolean sizeUse, int w, int h,
493                    GLboolean gameMode, GLboolean isSubWindow )
494 {
495 #if TARGET_HOST_POSIX_X11
496     XVisualInfo * visualInfo;
497     XSetWindowAttributes winAttr;
498     XTextProperty textProperty;
499     XSizeHints sizeHints;
500     XWMHints wmHints;
501     unsigned long mask;
502     int renderType;  /*  GLX_RGBA_TYPE or GLX_COLOR_INDEX_TYPE  */
503     unsigned int current_DisplayMode = fgState.DisplayMode ;
504
505     /* Save the display mode if we are creating a menu window */
506     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
507         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
508
509     window->Window.FBConfig = fgChooseFBConfig( );
510
511     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
512         fgState.DisplayMode = current_DisplayMode ;
513
514     if( ! window->Window.FBConfig )
515     {
516         /*
517          * The "fgChooseFBConfig" returned a null meaning that the visual
518          * context is not available.
519          * Try a couple of variations to see if they will work.
520          */
521         if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
522         {
523             fgState.DisplayMode |= GLUT_DOUBLE ;
524             window->Window.FBConfig = fgChooseFBConfig( );
525             fgState.DisplayMode &= ~GLUT_DOUBLE;
526         }
527
528         if( fgState.DisplayMode & GLUT_MULTISAMPLE )
529         {
530             fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
531             window->Window.FBConfig = fgChooseFBConfig( );
532             fgState.DisplayMode |= GLUT_MULTISAMPLE;
533         }
534     }
535
536     FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
537                                   "FBConfig with necessary capabilities not found", "fgOpenWindow" );
538
539     /*  Get the X visual.  */
540     visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
541                                            *(window->Window.FBConfig) );
542
543     /*
544      * XXX HINT: the masks should be updated when adding/removing callbacks.
545      * XXX       This might speed up message processing. Is that true?
546      * XXX
547      * XXX A: Not appreciably, but it WILL make it easier to debug.
548      * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
549      * XXX    turns off events that it doesn't need and is a whole lot
550      * XXX    more pleasant to trace.  (Think mouse-motion!  Tons of
551      * XXX    ``bonus'' GUI events stream in.)
552      */
553     winAttr.event_mask        =
554         StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
555         ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
556         VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
557         PointerMotionMask | ButtonMotionMask;
558     winAttr.background_pixmap = None;
559     winAttr.background_pixel  = 0;
560     winAttr.border_pixel      = 0;
561
562     winAttr.colormap = XCreateColormap(
563         fgDisplay.Display, fgDisplay.RootWindow,
564         visualInfo->visual, AllocNone
565     );
566
567     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
568
569     if( window->IsMenu || ( gameMode == GL_TRUE ) )
570     {
571         winAttr.override_redirect = True;
572         mask |= CWOverrideRedirect;
573     }
574
575     if( ! positionUse )
576         x = y = -1; /* default window position */
577     if( ! sizeUse )
578         w = h = 300; /* default window size */
579
580     window->Window.Handle = XCreateWindow(
581         fgDisplay.Display,
582         window->Parent == NULL ? fgDisplay.RootWindow :
583         window->Parent->Window.Handle,
584         x, y, w, h, 0,
585         visualInfo->depth, InputOutput,
586         visualInfo->visual, mask,
587         &winAttr
588     );
589
590     /*
591      * The GLX context creation, possibly trying the direct context rendering
592      *  or else use the current context if the user has so specified
593      */
594
595     /*  Set renderType.  */
596     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
597     {
598         /*  Display mode has been set to GLUT_RGB.  */
599         renderType = GLX_RGBA_TYPE;
600     }
601     else if (fgState.DisplayMode & GLUT_INDEX)
602     {
603         renderType = GLX_COLOR_INDEX_TYPE;
604     }
605     else
606     {
607         renderType = GLX_RGBA_TYPE;
608     }
609
610     if( window->IsMenu )
611     {
612         /*
613          * If there isn't already an OpenGL rendering context for menu
614          * windows, make one
615          */
616         if( !fgStructure.MenuContext )
617         {
618             fgStructure.MenuContext =
619                 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
620             fgStructure.MenuContext->MContext = glXCreateNewContext(
621                 fgDisplay.Display, *(window->Window.FBConfig), renderType,
622                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
623             );
624         }
625
626         /* window->Window.Context = fgStructure.MenuContext->MContext; */
627         window->Window.Context = glXCreateNewContext(
628             fgDisplay.Display, *(window->Window.FBConfig), renderType,
629             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
630         );
631     }
632     else if( fgState.UseCurrentContext )
633     {
634         window->Window.Context = glXGetCurrentContext( );
635
636         if( ! window->Window.Context )
637             window->Window.Context = glXCreateNewContext(
638                 fgDisplay.Display, *(window->Window.FBConfig), renderType,
639                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
640             );
641     }
642     else
643         window->Window.Context = glXCreateNewContext(
644             fgDisplay.Display, *(window->Window.FBConfig), renderType,
645             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
646         );
647
648 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
649     if(  !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
650     {
651       if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
652         fgError( "Unable to force direct context rendering for window '%s'",
653                  title );
654       else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
655         fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
656                  title );
657     }
658 #endif
659
660     /*
661      * XXX Assume the new window is visible by default
662      * XXX Is this a  safe assumption?
663      */
664     window->State.Visible = GL_TRUE;
665
666     sizeHints.flags = 0;
667     if ( positionUse )
668         sizeHints.flags |= USPosition;
669     if ( sizeUse )
670         sizeHints.flags |= USSize;
671
672     /*
673      * Fill in the size hints values now (the x, y, width and height
674      * settings are obsolete, are there any more WMs that support them?)
675      * Unless the X servers actually stop supporting these, we should
676      * continue to fill them in.  It is *not* our place to tell the user
677      * that they should replace a window manager that they like, and which
678      * works, just because *we* think that it's not "modern" enough.
679      */
680     sizeHints.x      = x;
681     sizeHints.y      = y;
682     sizeHints.width  = w;
683     sizeHints.height = h;
684
685     wmHints.flags = StateHint;
686     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
687     /* Prepare the window and iconified window names... */
688     XStringListToTextProperty( (char **) &title, 1, &textProperty );
689
690     XSetWMProperties(
691         fgDisplay.Display,
692         window->Window.Handle,
693         &textProperty,
694         &textProperty,
695         0,
696         0,
697         &sizeHints,
698         &wmHints,
699         NULL
700     );
701     XFree( textProperty.value );
702
703     XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
704                      &fgDisplay.DeleteWindow, 1 );
705
706     glXMakeContextCurrent(
707         fgDisplay.Display,
708         window->Window.Handle,
709         window->Window.Handle,
710         window->Window.Context
711     );
712
713     XMapWindow( fgDisplay.Display, window->Window.Handle );
714
715     XFree(visualInfo);
716
717 #elif TARGET_HOST_MS_WINDOWS
718
719     WNDCLASS wc;
720     DWORD flags;
721     DWORD exFlags = 0;
722     ATOM atom;
723     int WindowStyle = 0;
724
725     /* Grab the window class we have registered on glutInit(): */
726     atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
727     FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
728                                    "fgOpenWindow" );
729
730     if( gameMode )
731     {
732         FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
733                                        "Game mode being invoked on a subwindow",
734                                        "fgOpenWindow" );
735
736         /*
737          * Set the window creation flags appropriately to make the window
738          * entirely visible:
739          */
740         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
741     }
742     else
743     {
744         int worig = w, horig = h;
745
746 #if !defined(_WIN32_WCE)
747         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
748         {
749             /*
750              * Update the window dimensions, taking account of window
751              * decorations.  "freeglut" is to create the window with the
752              * outside of its border at (x,y) and with dimensions (w,h).
753              */
754             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
755             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
756                 GetSystemMetrics( SM_CYCAPTION );
757         }
758 #endif /* defined(_WIN32_WCE) */
759
760         if( ! positionUse )
761         {
762             x = CW_USEDEFAULT;
763             y = CW_USEDEFAULT;
764         }
765         /* setting State.Width/Height to call resize callback later */
766         if( ! sizeUse )
767         {
768             if( ! window->IsMenu )
769             {
770                 w = CW_USEDEFAULT;
771                 h = CW_USEDEFAULT;
772             }
773             else /* fail safe - Windows can make a window of size (0, 0) */
774                 w = h = 300; /* default window size */
775             window->State.Width = window->State.Height = -1;
776         }
777         else
778         {
779             window->State.Width = worig;
780             window->State.Height = horig;
781         }
782
783         /*
784          * There's a small difference between creating the top, child and
785          * game mode windows
786          */
787         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
788
789         if ( window->IsMenu )
790         {
791             flags |= WS_POPUP;
792             exFlags |= WS_EX_TOOLWINDOW;
793         }
794 #if !defined(_WIN32_WCE)
795         else if( window->Parent == NULL )
796             flags |= WS_OVERLAPPEDWINDOW;
797 #endif
798         else
799             flags |= WS_CHILD;
800     }
801
802 #if defined(_WIN32_WCE)
803     {
804         wchar_t* wstr = fghWstrFromStr(title);
805
806         window->Window.Handle = CreateWindow(
807             _T("FREEGLUT"),
808             wstr,
809             WS_VISIBLE | WS_POPUP,
810             0,0, 240,320,
811             NULL,
812             NULL,
813             fgDisplay.Instance,
814             (LPVOID) window
815         );
816
817         free(wstr);
818
819         SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
820         SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
821         SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
822         MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
823         ShowWindow(window->Window.Handle, SW_SHOW);
824         UpdateWindow(window->Window.Handle);
825     }
826 #else
827     window->Window.Handle = CreateWindowEx(
828         exFlags,
829         _T("FREEGLUT"),
830         title,
831         flags,
832         x, y, w, h,
833         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
834         (HMENU) NULL,
835         fgDisplay.Instance,
836         (LPVOID) window
837     );
838 #endif /* defined(_WIN32_WCE) */
839
840     if( !( window->Window.Handle ) )
841         fgError( "Failed to create a window (%s)!", title );
842
843     /* Make a menu window always on top - fix Feature Request 947118 */
844     if( window->IsMenu || gameMode )
845         SetWindowPos(
846                         window->Window.Handle,
847                         HWND_TOPMOST,
848                         0, 0, 0, 0,
849                         SWP_NOMOVE | SWP_NOSIZE
850                     );
851
852     /* Hack to remove the caption (title bar) and/or border
853      * and all the system menu controls.
854      */
855     WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
856     if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
857     {
858         SetWindowLong ( window->Window.Handle, GWL_STYLE,
859                         WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
860     }
861     else if ( fgState.DisplayMode & GLUT_BORDERLESS )
862     {
863         SetWindowLong ( window->Window.Handle, GWL_STYLE,
864                         WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
865     }
866 /*  SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
867      SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
868
869
870 #if defined(_WIN32_WCE)
871     ShowWindow( window->Window.Handle, SW_SHOW );
872 #else
873     ShowWindow( window->Window.Handle,
874                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
875 #endif /* defined(_WIN32_WCE) */
876
877     UpdateWindow( window->Window.Handle );
878     ShowCursor( TRUE );  /* XXX Old comments say "hide cursor"! */
879
880 #endif
881
882     fgSetWindow( window );
883
884     window->Window.DoubleBuffered =
885         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
886
887     if ( ! window->Window.DoubleBuffered )
888     {
889         glDrawBuffer ( GL_FRONT );
890         glReadBuffer ( GL_FRONT );
891     }
892 }
893
894 /*
895  * Closes a window, destroying the frame and OpenGL context
896  */
897 void fgCloseWindow( SFG_Window* window )
898 {
899 #if TARGET_HOST_POSIX_X11
900
901     glXDestroyContext( fgDisplay.Display, window->Window.Context );
902     XFree( window->Window.FBConfig );
903     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
904     /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
905
906 #elif TARGET_HOST_MS_WINDOWS
907
908     /* Make sure we don't close a window with current context active */
909     if( fgStructure.CurrentWindow == window )
910         wglMakeCurrent( NULL, NULL );
911
912     /*
913      * Step through the list of windows.  If the rendering context
914      * is not being used by another window, then we delete it.
915      */
916     {
917         int used = FALSE ;
918         SFG_Window *iter ;
919
920         for( iter = (SFG_Window *)fgStructure.Windows.First;
921              iter;
922              iter = (SFG_Window *)iter->Node.Next )
923         {
924             if( ( iter->Window.Context == window->Window.Context ) &&
925                 ( iter != window ) )
926                 used = TRUE;
927         }
928
929         if( ! used )
930             wglDeleteContext( window->Window.Context );
931     }
932
933     DestroyWindow( window->Window.Handle );
934 #endif
935 }
936
937
938 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
939
940 /*
941  * Creates a new top-level freeglut window
942  */
943 int FGAPIENTRY glutCreateWindow( const char* title )
944 {
945     /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
946      * XXX application has not already done so.  The "freeglut" community
947      * XXX decided not to go this route (freeglut-developer e-mail from
948      * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
949      * XXX Desired 'freeglut' behaviour when there is no current window"
950      */
951     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
952
953     return fgCreateWindow( NULL, title, fgState.Position.Use,
954                            fgState.Position.X, fgState.Position.Y,
955                            fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
956                            GL_FALSE, GL_FALSE )->ID;
957 }
958
959 /*
960  * This function creates a sub window.
961  */
962 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
963 {
964     int ret = 0;
965     SFG_Window* window = NULL;
966     SFG_Window* parent = NULL;
967
968     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
969     parent = fgWindowByID( parentID );
970     freeglut_return_val_if_fail( parent != NULL, 0 );
971     if ( x < 0 )
972     {
973         x = parent->State.Width + x ;
974         if ( w >= 0 ) x -= w ;
975     }
976
977     if ( w < 0 ) w = parent->State.Width - x + w ;
978     if ( w < 0 )
979     {
980         x += w ;
981         w = -w ;
982     }
983
984     if ( y < 0 )
985     {
986         y = parent->State.Height + y ;
987         if ( h >= 0 ) y -= h ;
988     }
989
990     if ( h < 0 ) h = parent->State.Height - y + h ;
991     if ( h < 0 )
992     {
993         y += h ;
994         h = -h ;
995     }
996
997     window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
998     ret = window->ID;
999
1000     return ret;
1001 }
1002
1003 /*
1004  * Destroys a window and all of its subwindows
1005  */
1006 void FGAPIENTRY glutDestroyWindow( int windowID )
1007 {
1008     SFG_Window* window;
1009     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1010     window = fgWindowByID( windowID );
1011     freeglut_return_if_fail( window != NULL );
1012     {
1013         fgExecutionState ExecState = fgState.ExecState;
1014         fgAddToWindowDestroyList( window );
1015         fgState.ExecState = ExecState;
1016     }
1017 }
1018
1019 /*
1020  * This function selects the current window
1021  */
1022 void FGAPIENTRY glutSetWindow( int ID )
1023 {
1024     SFG_Window* window = NULL;
1025
1026     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1027     if( fgStructure.CurrentWindow != NULL )
1028         if( fgStructure.CurrentWindow->ID == ID )
1029             return;
1030
1031     window = fgWindowByID( ID );
1032     if( window == NULL )
1033     {
1034         fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1035         return;
1036     }
1037
1038     fgSetWindow( window );
1039 }
1040
1041 /*
1042  * This function returns the ID number of the current window, 0 if none exists
1043  */
1044 int FGAPIENTRY glutGetWindow( void )
1045 {
1046     SFG_Window *win = fgStructure.CurrentWindow;
1047     /*
1048      * Since GLUT did not throw an error if this function was called without a prior call to
1049      * "glutInit", this function shouldn't do so here.  Instead let us return a zero.
1050      * See Feature Request "[ 1307049 ] glutInit check".
1051      */
1052     if ( ! fgState.Initialised )
1053         return 0;
1054
1055     while ( win && win->IsMenu )
1056         win = win->Parent;
1057     return win ? win->ID : 0;
1058 }
1059
1060 /*
1061  * This function makes the current window visible
1062  */
1063 void FGAPIENTRY glutShowWindow( void )
1064 {
1065     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1066     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1067
1068 #if TARGET_HOST_POSIX_X11
1069
1070     XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1071     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1072
1073 #elif TARGET_HOST_MS_WINDOWS
1074
1075     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1076
1077 #endif
1078
1079     fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1080 }
1081
1082 /*
1083  * This function hides the current window
1084  */
1085 void FGAPIENTRY glutHideWindow( void )
1086 {
1087     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1088     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1089
1090 #if TARGET_HOST_POSIX_X11
1091
1092     if( fgStructure.CurrentWindow->Parent == NULL )
1093         XWithdrawWindow( fgDisplay.Display,
1094                          fgStructure.CurrentWindow->Window.Handle,
1095                          fgDisplay.Screen );
1096     else
1097         XUnmapWindow( fgDisplay.Display,
1098                       fgStructure.CurrentWindow->Window.Handle );
1099     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1100
1101 #elif TARGET_HOST_MS_WINDOWS
1102
1103     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1104
1105 #endif
1106
1107     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1108 }
1109
1110 /*
1111  * Iconify the current window (top-level windows only)
1112  */
1113 void FGAPIENTRY glutIconifyWindow( void )
1114 {
1115     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1116     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1117
1118     fgStructure.CurrentWindow->State.Visible   = GL_FALSE;
1119 #if TARGET_HOST_POSIX_X11
1120
1121     XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1122                     fgDisplay.Screen );
1123     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1124
1125 #elif TARGET_HOST_MS_WINDOWS
1126
1127     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1128
1129 #endif
1130
1131     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1132 }
1133
1134 /*
1135  * Set the current window's title
1136  */
1137 void FGAPIENTRY glutSetWindowTitle( const char* title )
1138 {
1139     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1140     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1141     if( ! fgStructure.CurrentWindow->Parent )
1142     {
1143 #if TARGET_HOST_POSIX_X11
1144
1145         XTextProperty text;
1146
1147         text.value = (unsigned char *) title;
1148         text.encoding = XA_STRING;
1149         text.format = 8;
1150         text.nitems = strlen( title );
1151
1152         XSetWMName(
1153             fgDisplay.Display,
1154             fgStructure.CurrentWindow->Window.Handle,
1155             &text
1156         );
1157
1158         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1159
1160 #elif TARGET_HOST_MS_WINDOWS
1161 #    ifdef _WIN32_WCE
1162         {
1163             wchar_t* wstr = fghWstrFromStr(title);
1164             SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1165             free(wstr);
1166         }
1167 #    else
1168         SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1169 #    endif
1170
1171 #endif
1172     }
1173 }
1174
1175 /*
1176  * Set the current window's iconified title
1177  */
1178 void FGAPIENTRY glutSetIconTitle( const char* title )
1179 {
1180     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1181     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1182
1183     if( ! fgStructure.CurrentWindow->Parent )
1184     {
1185 #if TARGET_HOST_POSIX_X11
1186
1187         XTextProperty text;
1188
1189         text.value = (unsigned char *) title;
1190         text.encoding = XA_STRING;
1191         text.format = 8;
1192         text.nitems = strlen( title );
1193
1194         XSetWMIconName(
1195             fgDisplay.Display,
1196             fgStructure.CurrentWindow->Window.Handle,
1197             &text
1198         );
1199
1200         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1201
1202 #elif TARGET_HOST_MS_WINDOWS
1203 #    ifdef _WIN32_WCE
1204         {
1205             wchar_t* wstr = fghWstrFromStr(title);
1206             SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1207             free(wstr);
1208         }
1209 #    else
1210         SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1211 #    endif
1212
1213 #endif
1214     }
1215 }
1216
1217 /*
1218  * Change the current window's size
1219  */
1220 void FGAPIENTRY glutReshapeWindow( int width, int height )
1221 {
1222     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1223     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1224
1225     if (glutGet(GLUT_FULL_SCREEN))
1226     {
1227       /*  Leave full screen state before resizing. */
1228       glutFullScreenToggle();
1229     }
1230
1231     fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1232     fgStructure.CurrentWindow->State.Width  = width ;
1233     fgStructure.CurrentWindow->State.Height = height;
1234 }
1235
1236 /*
1237  * Change the current window's position
1238  */
1239 void FGAPIENTRY glutPositionWindow( int x, int y )
1240 {
1241     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1242     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1243
1244     if (glutGet(GLUT_FULL_SCREEN))
1245     {
1246       /*  Leave full screen state before moving. */
1247       glutFullScreenToggle();
1248     }
1249
1250 #if TARGET_HOST_POSIX_X11
1251
1252     XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1253                  x, y );
1254     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1255
1256 #elif TARGET_HOST_MS_WINDOWS
1257
1258     {
1259         RECT winRect;
1260
1261         /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1262         GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1263         MoveWindow(
1264             fgStructure.CurrentWindow->Window.Handle,
1265             x,
1266             y,
1267             winRect.right - winRect.left,
1268             winRect.bottom - winRect.top,
1269             TRUE
1270         );
1271     }
1272
1273 #endif
1274 }
1275
1276 /*
1277  * Lowers the current window (by Z order change)
1278  */
1279 void FGAPIENTRY glutPushWindow( void )
1280 {
1281     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1282     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1283
1284 #if TARGET_HOST_POSIX_X11
1285
1286     XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1287
1288 #elif TARGET_HOST_MS_WINDOWS
1289
1290     SetWindowPos(
1291         fgStructure.CurrentWindow->Window.Handle,
1292         HWND_BOTTOM,
1293         0, 0, 0, 0,
1294         SWP_NOSIZE | SWP_NOMOVE
1295     );
1296
1297 #endif
1298 }
1299
1300 /*
1301  * Raises the current window (by Z order change)
1302  */
1303 void FGAPIENTRY glutPopWindow( void )
1304 {
1305     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1306     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1307
1308 #if TARGET_HOST_POSIX_X11
1309
1310     XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1311
1312 #elif TARGET_HOST_MS_WINDOWS
1313
1314     SetWindowPos(
1315         fgStructure.CurrentWindow->Window.Handle,
1316         HWND_TOP,
1317         0, 0, 0, 0,
1318         SWP_NOSIZE | SWP_NOMOVE
1319     );
1320
1321 #endif
1322 }
1323
1324 /*
1325  * Resize the current window so that it fits the whole screen
1326  */
1327 void FGAPIENTRY glutFullScreen( void )
1328 {
1329     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1330     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1331
1332     if (glutGet(GLUT_FULL_SCREEN))
1333     {
1334       /*  Leave full screen state before resizing. */
1335       glutFullScreenToggle();
1336     }
1337
1338     {
1339 #if TARGET_HOST_POSIX_X11
1340
1341         Status status;  /* Returned by XGetWindowAttributes(), not checked. */
1342         XWindowAttributes attributes;
1343
1344         status = XGetWindowAttributes(fgDisplay.Display,
1345                                       fgStructure.CurrentWindow->Window.Handle,
1346                                       &attributes);
1347         /*
1348          * The "x" and "y" members of "attributes" are the window's coordinates
1349          * relative to its parent, i.e. to the decoration window.
1350          */
1351         XMoveResizeWindow(fgDisplay.Display,
1352                           fgStructure.CurrentWindow->Window.Handle,
1353                           -attributes.x,
1354                           -attributes.y,
1355                           fgDisplay.ScreenWidth,
1356                           fgDisplay.ScreenHeight);
1357
1358 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1359         RECT rect;
1360
1361         /* For fullscreen mode, force the top-left corner to 0,0
1362          * and adjust the window rectangle so that the client area
1363          * covers the whole screen.
1364          */
1365
1366         rect.left   = 0;
1367         rect.top    = 0;
1368         rect.right  = fgDisplay.ScreenWidth;
1369         rect.bottom = fgDisplay.ScreenHeight;
1370
1371         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1372                                   WS_CLIPCHILDREN, FALSE );
1373
1374         /*
1375          * SWP_NOACTIVATE     Do not activate the window
1376          * SWP_NOOWNERZORDER  Do not change position in z-order
1377          * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1378          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)
1379          */
1380
1381         SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1382                       HWND_TOP,
1383                       rect.left,
1384                       rect.top,
1385                       rect.right  - rect.left,
1386                       rect.bottom - rect.top,
1387                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1388                       SWP_NOZORDER
1389                     );
1390 #endif
1391     }
1392 }
1393
1394 /*
1395  * Toggle the window's full screen state.
1396  */
1397 void FGAPIENTRY glutFullScreenToggle( void )
1398 {
1399     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1400     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1401
1402     {
1403 #if TARGET_HOST_POSIX_X11
1404
1405       if (fgDisplay.StateFullScreen != None)
1406       {
1407         XEvent xevent;
1408         long event_mask;
1409         int status;
1410
1411         xevent.type = ClientMessage;
1412         xevent.xclient.type = ClientMessage;
1413         xevent.xclient.serial = 0;
1414         xevent.xclient.send_event = True;
1415         xevent.xclient.display = fgDisplay.Display;
1416         xevent.xclient.window = fgStructure.CurrentWindow->Window.Handle;
1417         xevent.xclient.message_type = fgDisplay.State;
1418         xevent.xclient.format = 32;
1419         xevent.xclient.data.l[0] = 2;  /* _NET_WM_STATE_TOGGLE */
1420         xevent.xclient.data.l[1] = fgDisplay.StateFullScreen;
1421         xevent.xclient.data.l[2] = 0;
1422         xevent.xclient.data.l[3] = 0;
1423         xevent.xclient.data.l[4] = 0;
1424
1425         /*** Don't really understand how event masks work... ***/
1426         event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
1427
1428         status = XSendEvent(fgDisplay.Display,
1429           fgDisplay.RootWindow,
1430           False,
1431           event_mask,
1432           &xevent);
1433         FREEGLUT_INTERNAL_ERROR_EXIT(status != 0,
1434           "XSendEvent failed",
1435           "glutFullScreenToggle");
1436       }
1437       else
1438 #endif
1439       {
1440         /*
1441          * If the window manager is not Net WM compliant, fall back to legacy
1442          * behaviour.
1443          */
1444         glutFullScreen();
1445       }
1446     }
1447 }
1448
1449 /*
1450  * A.Donev: Set and retrieve the window's user data
1451  */
1452 void* FGAPIENTRY glutGetWindowData( void )
1453 {
1454     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1455     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1456     return fgStructure.CurrentWindow->UserData;
1457 }
1458
1459 void FGAPIENTRY glutSetWindowData(void* data)
1460 {
1461     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1462     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1463     fgStructure.CurrentWindow->UserData = data;
1464 }
1465
1466 /*** END OF FILE ***/