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 #define FREEGLUT_BUILDING_LIB
29 #include "freeglut.h"
30 #include "freeglut_internal.h"
31
32 #if TARGET_HOST_POSIX_X11
33 #include <limits.h> /* LONG_MAX */
34 #endif
35
36 #if defined(_WIN32_WCE)
37 # include <Aygshell.h>
38 # ifdef FREEGLUT_LIB_PRAGMAS
39 # pragma comment( lib, "Aygshell.lib" )
40 # endif
41
fghWstrFromStr(const char * str)42 static wchar_t* fghWstrFromStr(const char* str)
43 {
44 int i,len=strlen(str);
45 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
46 for(i=0; i<len; i++)
47 wstr[i] = str[i];
48 wstr[len] = 0;
49 return wstr;
50 }
51
52 #endif /* defined(_WIN32_WCE) */
53
54 /* pushing attribute/value pairs into an array */
55 #define ATTRIB(a) attributes[where++]=(a)
56 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
57
58 /*
59 * TODO BEFORE THE STABLE RELEASE:
60 *
61 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
62 * fgSetupPixelFormat -- ignores the display mode settings
63 * fgOpenWindow() -- check the Win32 version, -iconic handling!
64 * fgCloseWindow() -- check the Win32 version
65 * glutCreateWindow() -- Check when default position and size is {-1,-1}
66 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
67 * glutDestroyWindow() -- check the Win32 version
68 * glutSetWindow() -- check the Win32 version
69 * glutGetWindow() -- OK
70 * glutSetWindowTitle() -- check the Win32 version
71 * glutSetIconTitle() -- check the Win32 version
72 * glutShowWindow() -- check the Win32 version
73 * glutHideWindow() -- check the Win32 version
74 * glutIconifyWindow() -- check the Win32 version
75 * glutReshapeWindow() -- check the Win32 version
76 * glutPositionWindow() -- check the Win32 version
77 * glutPushWindow() -- check the Win32 version
78 * glutPopWindow() -- check the Win32 version
79 */
80
81 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
82
fghIsLegacyContextVersionRequested(void)83 static int fghIsLegacyContextVersionRequested( void )
84 {
85 return fgState.MajorVersion == 1 && fgState.MinorVersion == 0;
86 }
87
fghIsLegacyContextRequested(void)88 static int fghIsLegacyContextRequested( void )
89 {
90 return fghIsLegacyContextVersionRequested() &&
91 fgState.ContextFlags == 0 &&
92 fgState.ContextProfile == 0;
93 }
94
fghNumberOfAuxBuffersRequested(void)95 static int fghNumberOfAuxBuffersRequested( void )
96 {
97 if ( fgState.DisplayMode & GLUT_AUX4 ) {
98 return 4;
99 }
100 if ( fgState.DisplayMode & GLUT_AUX3 ) {
101 return 3;
102 }
103 if ( fgState.DisplayMode & GLUT_AUX2 ) {
104 return 2;
105 }
106 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
107 return fgState.AuxiliaryBufferNumber;
108 }
109 return 0;
110 }
111
fghMapBit(int mask,int from,int to)112 static int fghMapBit( int mask, int from, int to )
113 {
114 return ( mask & from ) ? to : 0;
115
116 }
117
fghContextCreationError(void)118 static void fghContextCreationError( void )
119 {
120 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
121 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
122 fgState.ContextProfile );
123 }
124
125 /*
126 * Chooses a visual basing on the current display mode settings
127 */
128 #if TARGET_HOST_POSIX_X11
129
130 #ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
131 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20B2
132 #endif
133
fgChooseFBConfig(void)134 GLXFBConfig* fgChooseFBConfig( void )
135 {
136 GLboolean wantIndexedMode = GL_FALSE;
137 int attributes[ 100 ];
138 int where = 0, numAuxBuffers;
139
140 /* First we have to process the display mode settings... */
141 if( fgState.DisplayMode & GLUT_INDEX ) {
142 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
143 /* Buffer size is selected later. */
144
145 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
146 wantIndexedMode = GL_TRUE;
147 } else {
148 ATTRIB_VAL( GLX_RED_SIZE, 1 );
149 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
150 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
151 if( fgState.DisplayMode & GLUT_ALPHA ) {
152 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
153 }
154 }
155
156 if( fgState.DisplayMode & GLUT_DOUBLE ) {
157 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
158 }
159
160 if( fgState.DisplayMode & GLUT_STEREO ) {
161 ATTRIB_VAL( GLX_STEREO, True );
162 }
163
164 if( fgState.DisplayMode & GLUT_DEPTH ) {
165 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
166 }
167
168 if( fgState.DisplayMode & GLUT_STENCIL ) {
169 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
170 }
171
172 if( fgState.DisplayMode & GLUT_ACCUM ) {
173 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
174 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
175 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
176 if( fgState.DisplayMode & GLUT_ALPHA ) {
177 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
178 }
179 }
180
181 numAuxBuffers = fghNumberOfAuxBuffersRequested();
182 if ( numAuxBuffers > 0 ) {
183 ATTRIB_VAL( GLX_AUX_BUFFERS, numAuxBuffers );
184 }
185
186 if( fgState.DisplayMode & GLUT_SRGB ) {
187 ATTRIB_VAL( GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, True );
188 }
189
190 if (fgState.DisplayMode & GLUT_MULTISAMPLE) {
191 ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1);
192 ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber);
193 }
194
195 /* Push a terminator at the end of the list */
196 ATTRIB( None );
197
198 {
199 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
200 GLXFBConfig * fbconfig; /* The FBConfig we want */
201 int fbconfigArraySize; /* Number of FBConfigs in the array */
202
203
204 /* Get all FBConfigs that match "attributes". */
205 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
206 fgDisplay.Screen,
207 attributes,
208 &fbconfigArraySize );
209
210 if (fbconfigArray != NULL)
211 {
212 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
213
214
215 if( wantIndexedMode )
216 {
217 /*
218 * In index mode, we want the largest buffer size, i.e. visual
219 * depth. Here, FBConfigs are sorted by increasing buffer size
220 * first, so FBConfigs with the largest size come last.
221 */
222
223 int bufferSizeMin, bufferSizeMax;
224
225 /* Get bufferSizeMin. */
226 result =
227 glXGetFBConfigAttrib( fgDisplay.Display,
228 fbconfigArray[0],
229 GLX_BUFFER_SIZE,
230 &bufferSizeMin );
231 /* Get bufferSizeMax. */
232 result =
233 glXGetFBConfigAttrib( fgDisplay.Display,
234 fbconfigArray[fbconfigArraySize - 1],
235 GLX_BUFFER_SIZE,
236 &bufferSizeMax );
237
238 if (bufferSizeMax > bufferSizeMin)
239 {
240 /*
241 * Free and reallocate fbconfigArray, keeping only FBConfigs
242 * with the largest buffer size.
243 */
244 XFree(fbconfigArray);
245
246 /* Add buffer size token at the end of the list. */
247 where--;
248 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
249 ATTRIB( None );
250
251 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
252 fgDisplay.Screen,
253 attributes,
254 &fbconfigArraySize );
255 }
256 }
257
258 /*
259 * We now have an array of FBConfigs, the first one being the "best"
260 * one. So we should return only this FBConfig:
261 *
262 * int fbconfigXID;
263 *
264 * - pick the XID of the FBConfig we want
265 * result = glXGetFBConfigAttrib( fgDisplay.Display,
266 * fbconfigArray[0],
267 * GLX_FBCONFIG_ID,
268 * &fbconfigXID );
269 *
270 * - free the array
271 * XFree(fbconfigArray);
272 *
273 * - reset "attributes" with the XID
274 * where = 0;
275 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
276 * ATTRIB( None );
277 *
278 * - get our FBConfig only
279 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
280 * fgDisplay.Screen,
281 * attributes,
282 * &fbconfigArraySize );
283 *
284 * However, for some configurations (for instance multisampling with
285 * Mesa 6.5.2 and ATI drivers), this does not work:
286 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
287 * XID. Further investigation is needed.
288 *
289 * So, for now, we return the whole array of FBConfigs. This should
290 * not produce any side effects elsewhere.
291 */
292 fbconfig = fbconfigArray;
293 }
294 else
295 {
296 fbconfig = NULL;
297 }
298
299 return fbconfig;
300 }
301 }
302 #endif /* TARGET_HOST_POSIX_X11 */
303
304 /*
305 * Setup the pixel format for a Win32 window
306 */
307 #if TARGET_HOST_MS_WINDOWS
308 /* The following include file is available from SGI but is not standard:
309 * #include <GL/wglext.h>
310 * So we copy the necessary parts out of it.
311 * XXX: should local definitions for extensions be put in a separate include file?
312 */
313 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
314
315 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
316
317 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
318 #define WGL_ACCELERATION_ARB 0x2003
319 #define WGL_SUPPORT_OPENGL_ARB 0x2010
320 #define WGL_DOUBLE_BUFFER_ARB 0x2011
321 #define WGL_COLOR_BITS_ARB 0x2014
322 #define WGL_ALPHA_BITS_ARB 0x201B
323 #define WGL_DEPTH_BITS_ARB 0x2022
324 #define WGL_STENCIL_BITS_ARB 0x2023
325 #define WGL_FULL_ACCELERATION_ARB 0x2027
326
327 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
328 #define WGL_SAMPLES_ARB 0x2042
329
330 #define WGL_TYPE_RGBA_FLOAT_ARB 0x21A0
331
332 #define WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20A9
333
334 #ifndef WGL_ARB_create_context
335 #define WGL_ARB_create_context 1
336 #ifdef WGL_WGLEXT_PROTOTYPES
337 extern HGLRC WINAPI wglCreateContextAttribsARB (HDC, HGLRC, const int *);
338 #endif /* WGL_WGLEXT_PROTOTYPES */
339 typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);
340
341 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
342 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
343 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
344 #define WGL_CONTEXT_FLAGS_ARB 0x2094
345 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
346
347 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
348 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
349
350 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
351 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
352
353 #define ERROR_INVALID_VERSION_ARB 0x2095
354 #define ERROR_INVALID_PROFILE_ARB 0x2096
355 #endif
356
fghFillContextAttributes(int * attributes)357 static void fghFillContextAttributes( int *attributes ) {
358 int where = 0, contextFlags, contextProfile;
359
360 if ( !fghIsLegacyContextVersionRequested() ) {
361 ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
362 ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
363 }
364
365 contextFlags =
366 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |
367 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
368 if ( contextFlags != 0 ) {
369 ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );
370 }
371
372 contextProfile =
373 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |
374 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
375 if ( contextProfile != 0 ) {
376 ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );
377 }
378
379 ATTRIB( 0 );
380 }
381
fghIsExtensionSupported(HDC hdc,const char * extension)382 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {
383 const char *pWglExtString;
384 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =
385 (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
386 if ( wglGetEntensionsStringARB == NULL )
387 {
388 return FALSE;
389 }
390 pWglExtString = wglGetEntensionsStringARB( hdc );
391 return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );
392 }
393
fgNewWGLCreateContext(SFG_Window * window)394 void fgNewWGLCreateContext( SFG_Window* window )
395 {
396 HGLRC context;
397 int attributes[9];
398 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
399
400 /* If nothing fancy has been required, leave the context as it is */
401 if ( fghIsLegacyContextRequested() )
402 {
403 return;
404 }
405
406 wglMakeCurrent( window->Window.Device, window->Window.Context );
407
408 if ( !fghIsExtensionSupported( window->Window.Device, "WGL_ARB_create_context" ) )
409 {
410 return;
411 }
412
413 /* new context creation */
414 fghFillContextAttributes( attributes );
415
416 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
417 if ( wglCreateContextAttribsARB == NULL )
418 {
419 fgError( "wglCreateContextAttribsARB not found" );
420 }
421
422 context = wglCreateContextAttribsARB( window->Window.Device, 0, attributes );
423 if ( context == NULL )
424 {
425 fghContextCreationError();
426 }
427
428 wglMakeCurrent( NULL, NULL );
429 wglDeleteContext( window->Window.Context );
430 window->Window.Context = context;
431 }
432
433 #if !defined(_WIN32_WCE)
434
fghFillPFD(PIXELFORMATDESCRIPTOR * ppfd,HDC hdc,unsigned char layer_type)435 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )
436 {
437 int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
438 if ( fgState.DisplayMode & GLUT_DOUBLE ) {
439 flags |= PFD_DOUBLEBUFFER;
440 }
441 if ( fgState.DisplayMode & GLUT_STEREO ) {
442 flags |= PFD_STEREO;
443 }
444
445 //#if defined(_MSC_VER)
446 //#pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
447 //#endif
448
449 /* Specify which pixel format do we opt for... */
450 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
451 ppfd->nVersion = 1;
452 ppfd->dwFlags = flags;
453
454 if( fgState.DisplayMode & GLUT_INDEX ) {
455 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
456 ppfd->cRedBits = 0;
457 ppfd->cGreenBits = 0;
458 ppfd->cBlueBits = 0;
459 ppfd->cAlphaBits = 0;
460 } else {
461 ppfd->iPixelType = PFD_TYPE_RGBA;
462 ppfd->cRedBits = 8;
463 ppfd->cGreenBits = 8;
464 ppfd->cBlueBits = 8;
465 ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;
466 }
467
468 ppfd->cColorBits = 24;
469 ppfd->cRedShift = 0;
470 ppfd->cGreenShift = 0;
471 ppfd->cBlueShift = 0;
472 ppfd->cAlphaShift = 0;
473 ppfd->cAccumBits = 0;
474 ppfd->cAccumRedBits = 0;
475 ppfd->cAccumGreenBits = 0;
476 ppfd->cAccumBlueBits = 0;
477 ppfd->cAccumAlphaBits = 0;
478
479 /* Hmmm, or 32/0 instead of 24/8? */
480 ppfd->cDepthBits = 24;
481 ppfd->cStencilBits = 8;
482
483 ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();
484 ppfd->iLayerType = layer_type;
485 ppfd->bReserved = 0;
486 ppfd->dwLayerMask = 0;
487 ppfd->dwVisibleMask = 0;
488 ppfd->dwDamageMask = 0;
489
490 ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );
491 }
492
fghFillPixelFormatAttributes(int * attributes,const PIXELFORMATDESCRIPTOR * ppfd)493 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )
494 {
495 int where = 0;
496
497 ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );
498 ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );
499 ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );
500
501 ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );
502 ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );
503 ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );
504 ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );
505
506 ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );
507
508 if ( fgState.DisplayMode & GLUT_SRGB ) {
509 ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );
510 }
511
512 ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );
513 ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );
514 ATTRIB( 0 );
515 }
516 #endif
517
fgSetupPixelFormat(SFG_Window * window,GLboolean checkOnly,unsigned char layer_type)518 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
519 unsigned char layer_type )
520 {
521 #if defined(_WIN32_WCE)
522 return GL_TRUE;
523 #else
524 PIXELFORMATDESCRIPTOR pfd;
525 PIXELFORMATDESCRIPTOR* ppfd = &pfd;
526 int pixelformat;
527
528 fghFillPFD( ppfd, window->Window.Device, layer_type );
529 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
530
531 /* windows hack for multismapling/sRGB */
532 if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||
533 ( fgState.DisplayMode & GLUT_SRGB ) )
534 {
535 HGLRC rc, rc_before=wglGetCurrentContext();
536 HWND hWnd;
537 HDC hDC, hDC_before=wglGetCurrentDC();
538 WNDCLASS wndCls;
539
540 /* create a dummy window */
541 ZeroMemory(&wndCls, sizeof(wndCls));
542 wndCls.lpfnWndProc = DefWindowProc;
543 wndCls.hInstance = fgDisplay.Instance;
544 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
545 wndCls.lpszClassName = _T("FREEGLUT_dummy");
546 RegisterClass( &wndCls );
547
548 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
549 hDC=GetDC(hWnd);
550 SetPixelFormat( hDC, pixelformat, ppfd );
551
552 rc = wglCreateContext( hDC );
553 wglMakeCurrent(hDC, rc);
554
555 if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )
556 {
557 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =
558 (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
559 if ( wglChoosePixelFormatARBProc )
560 {
561 int attributes[100];
562 int iPixelFormat;
563 BOOL bValid;
564 float fAttributes[] = { 0, 0 };
565 UINT numFormats;
566 fghFillPixelFormatAttributes( attributes, ppfd );
567 bValid = wglChoosePixelFormatARBProc(window->Window.Device, attributes, fAttributes, 1, &iPixelFormat, &numFormats);
568
569 if ( bValid && numFormats > 0 )
570 {
571 pixelformat = iPixelFormat;
572 }
573 }
574 }
575
576 wglMakeCurrent( hDC_before, rc_before);
577 wglDeleteContext(rc);
578 ReleaseDC(hWnd, hDC);
579 DestroyWindow(hWnd);
580 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
581 }
582
583 return ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( window->Window.Device, pixelformat, ppfd ) );
584 #endif /* defined(_WIN32_WCE) */
585 }
586 #endif /* TARGET_HOST_MS_WINDOWS */
587
588 /*
589 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
590 * the window structure passed in.
591 */
fgSetWindow(SFG_Window * window)592 void fgSetWindow ( SFG_Window *window )
593 {
594 #if TARGET_HOST_POSIX_X11
595 if ( window )
596 {
597 glXMakeContextCurrent(
598 fgDisplay.Display,
599 window->Window.Handle,
600 window->Window.Handle,
601 window->Window.Context
602 );
603
604 /* also register this window to receive spaceball events */
605 fgSpaceballSetWindow(window);
606 }
607 #elif TARGET_HOST_MS_WINDOWS
608 if( fgStructure.CurrentWindow )
609 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
610 fgStructure.CurrentWindow->Window.Device );
611
612 if ( window )
613 {
614 window->Window.Device = GetDC( window->Window.Handle );
615 wglMakeCurrent(
616 window->Window.Device,
617 window->Window.Context
618 );
619 }
620 #endif
621 fgStructure.CurrentWindow = window;
622 }
623
624
625
626 #if TARGET_HOST_POSIX_X11
627
628 #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
629 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
630 #endif
631
632 #ifndef GLX_CONTEXT_MINOR_VERSION_ARB
633 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
634 #endif
635
636 #ifndef GLX_CONTEXT_FLAGS_ARB
637 #define GLX_CONTEXT_FLAGS_ARB 0x2094
638 #endif
639
640 #ifndef GLX_CONTEXT_PROFILE_MASK_ARB
641 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
642 #endif
643
644 #ifndef GLX_CONTEXT_DEBUG_BIT_ARB
645 #define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001
646 #endif
647
648 #ifndef GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
649 #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
650 #endif
651
652 #ifndef GLX_CONTEXT_CORE_PROFILE_BIT_ARB
653 #define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
654 #endif
655
656 #ifndef GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
657 #define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
658 #endif
659
660 #ifndef GLX_RGBA_FLOAT_TYPE
661 #define GLX_RGBA_FLOAT_TYPE 0x20B9
662 #endif
663
664 #ifndef GLX_RGBA_FLOAT_BIT
665 #define GLX_RGBA_FLOAT_BIT 0x00000004
666 #endif
667
fghFillContextAttributes(int * attributes)668 static void fghFillContextAttributes( int *attributes ) {
669 int where = 0, contextFlags, contextProfile;
670
671 if ( !fghIsLegacyContextVersionRequested() ) {
672 ATTRIB_VAL( GLX_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
673 ATTRIB_VAL( GLX_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
674 }
675
676 contextFlags =
677 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, GLX_CONTEXT_DEBUG_BIT_ARB ) |
678 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
679 if ( contextFlags != 0 ) {
680 ATTRIB_VAL( GLX_CONTEXT_FLAGS_ARB, contextFlags );
681 }
682
683 contextProfile =
684 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, GLX_CONTEXT_CORE_PROFILE_BIT_ARB ) |
685 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
686 if ( contextProfile != 0 ) {
687 ATTRIB_VAL( GLX_CONTEXT_PROFILE_MASK_ARB, contextProfile );
688 }
689
690 ATTRIB( 0 );
691 }
692
693 typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
694 GLXContext share_list, Bool direct,
695 const int *attrib_list);
696
fghCreateNewContext(SFG_Window * window)697 static GLXContext fghCreateNewContext( SFG_Window* window )
698 {
699 /* for color model calculation */
700 int menu = ( window->IsMenu && !fgStructure.MenuContext );
701 int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
702
703 /* "classic" context creation */
704 Display *dpy = fgDisplay.Display;
705 GLXFBConfig config = *(window->Window.FBConfig);
706 int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
707 GLXContext share_list = NULL;
708 Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
709 GLXContext context;
710
711 /* new context creation */
712 int attributes[9];
713 CreateContextAttribsProc createContextAttribs;
714
715 /* If nothing fancy has been required, simply use the old context creation GLX API entry */
716 if ( fghIsLegacyContextRequested() )
717 {
718 context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
719 if ( context == NULL ) {
720 fghContextCreationError();
721 }
722 return context;
723 }
724
725 /* color index mode is not available anymore with OpenGL 3.0 */
726 if ( render_type == GLX_COLOR_INDEX_TYPE ) {
727 fgWarning( "color index mode is deprecated, using RGBA mode" );
728 }
729
730 fghFillContextAttributes( attributes );
731
732 createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
733 if ( createContextAttribs == NULL ) {
734 fgError( "glXCreateContextAttribsARB not found" );
735 }
736
737 context = createContextAttribs( dpy, config, share_list, direct, attributes );
738 if ( context == NULL ) {
739 fghContextCreationError();
740 }
741 return context;
742 }
743 #endif
744
745
746 /*
747 * Opens a window. Requires a SFG_Window object created and attached
748 * to the freeglut structure. OpenGL context is created here.
749 */
fgOpenWindow(SFG_Window * window,const char * title,GLboolean positionUse,int x,int y,GLboolean sizeUse,int w,int h,GLboolean gameMode,GLboolean isSubWindow)750 void fgOpenWindow( SFG_Window* window, const char* title,
751 GLboolean positionUse, int x, int y,
752 GLboolean sizeUse, int w, int h,
753 GLboolean gameMode, GLboolean isSubWindow )
754 {
755 #if TARGET_HOST_POSIX_X11
756 XVisualInfo * visualInfo;
757 XSetWindowAttributes winAttr;
758 XTextProperty textProperty;
759 XSizeHints sizeHints;
760 XWMHints wmHints;
761 unsigned long mask;
762 unsigned int current_DisplayMode = fgState.DisplayMode ;
763
764 /* Save the display mode if we are creating a menu window */
765 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
766 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
767
768 window->Window.FBConfig = fgChooseFBConfig( );
769
770 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
771 fgState.DisplayMode = current_DisplayMode ;
772
773 if( ! window->Window.FBConfig )
774 {
775 /*
776 * The "fgChooseFBConfig" returned a null meaning that the visual
777 * context is not available.
778 * Try a couple of variations to see if they will work.
779 */
780 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
781 {
782 fgState.DisplayMode |= GLUT_DOUBLE ;
783 window->Window.FBConfig = fgChooseFBConfig( );
784 fgState.DisplayMode &= ~GLUT_DOUBLE;
785 }
786
787 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
788 {
789 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
790 window->Window.FBConfig = fgChooseFBConfig( );
791 fgState.DisplayMode |= GLUT_MULTISAMPLE;
792 }
793 }
794
795 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
796 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
797
798 /* Get the X visual. */
799 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
800 *(window->Window.FBConfig) );
801
802 /*
803 * XXX HINT: the masks should be updated when adding/removing callbacks.
804 * XXX This might speed up message processing. Is that true?
805 * XXX
806 * XXX A: Not appreciably, but it WILL make it easier to debug.
807 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
808 * XXX turns off events that it doesn't need and is a whole lot
809 * XXX more pleasant to trace. (Think mouse-motion! Tons of
810 * XXX ``bonus'' GUI events stream in.)
811 */
812 winAttr.event_mask =
813 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
814 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
815 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
816 PointerMotionMask | ButtonMotionMask;
817 winAttr.background_pixmap = None;
818 winAttr.background_pixel = 0;
819 winAttr.border_pixel = 0;
820
821 winAttr.colormap = XCreateColormap(
822 fgDisplay.Display, fgDisplay.RootWindow,
823 visualInfo->visual, AllocNone
824 );
825
826 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
827
828 if( window->IsMenu || ( gameMode == GL_TRUE ) )
829 {
830 winAttr.override_redirect = True;
831 mask |= CWOverrideRedirect;
832 }
833
834 if( ! positionUse )
835 x = y = -1; /* default window position */
836 if( ! sizeUse )
837 w = h = 300; /* default window size */
838
839 window->Window.Handle = XCreateWindow(
840 fgDisplay.Display,
841 window->Parent == NULL ? fgDisplay.RootWindow :
842 window->Parent->Window.Handle,
843 x, y, w, h, 0,
844 visualInfo->depth, InputOutput,
845 visualInfo->visual, mask,
846 &winAttr
847 );
848
849 /*
850 * The GLX context creation, possibly trying the direct context rendering
851 * or else use the current context if the user has so specified
852 */
853
854 if( window->IsMenu )
855 {
856 /*
857 * If there isn't already an OpenGL rendering context for menu
858 * windows, make one
859 */
860 if( !fgStructure.MenuContext )
861 {
862 fgStructure.MenuContext =
863 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
864 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
865 }
866
867 /* window->Window.Context = fgStructure.MenuContext->MContext; */
868 window->Window.Context = fghCreateNewContext( window );
869 }
870 else if( fgState.UseCurrentContext )
871 {
872 window->Window.Context = glXGetCurrentContext( );
873
874 if( ! window->Window.Context )
875 window->Window.Context = fghCreateNewContext( window );
876 }
877 else
878 window->Window.Context = fghCreateNewContext( window );
879
880 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
881 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
882 {
883 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
884 fgError( "Unable to force direct context rendering for window '%s'",
885 title );
886 }
887 #endif
888
889 /*
890 * XXX Assume the new window is visible by default
891 * XXX Is this a safe assumption?
892 */
893 window->State.Visible = GL_TRUE;
894
895 sizeHints.flags = 0;
896 if ( positionUse )
897 sizeHints.flags |= USPosition;
898 if ( sizeUse )
899 sizeHints.flags |= USSize;
900
901 /*
902 * Fill in the size hints values now (the x, y, width and height
903 * settings are obsolete, are there any more WMs that support them?)
904 * Unless the X servers actually stop supporting these, we should
905 * continue to fill them in. It is *not* our place to tell the user
906 * that they should replace a window manager that they like, and which
907 * works, just because *we* think that it's not "modern" enough.
908 */
909 sizeHints.x = x;
910 sizeHints.y = y;
911 sizeHints.width = w;
912 sizeHints.height = h;
913
914 wmHints.flags = StateHint;
915 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
916 /* Prepare the window and iconified window names... */
917 XStringListToTextProperty( (char **) &title, 1, &textProperty );
918
919 XSetWMProperties(
920 fgDisplay.Display,
921 window->Window.Handle,
922 &textProperty,
923 &textProperty,
924 0,
925 0,
926 &sizeHints,
927 &wmHints,
928 NULL
929 );
930 XFree( textProperty.value );
931
932 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
933 &fgDisplay.DeleteWindow, 1 );
934
935 glXMakeContextCurrent(
936 fgDisplay.Display,
937 window->Window.Handle,
938 window->Window.Handle,
939 window->Window.Context
940 );
941
942 XMapWindow( fgDisplay.Display, window->Window.Handle );
943
944 XFree(visualInfo);
945
946 #elif TARGET_HOST_MS_WINDOWS
947
948 WNDCLASS wc;
949 DWORD flags;
950 DWORD exFlags = 0;
951 ATOM atom;
952 int WindowStyle = 0;
953
954 /* Grab the window class we have registered on glutInit(): */
955 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
956 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
957 "fgOpenWindow" );
958
959 if( gameMode )
960 {
961 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
962 "Game mode being invoked on a subwindow",
963 "fgOpenWindow" );
964
965 /*
966 * Set the window creation flags appropriately to make the window
967 * entirely visible:
968 */
969 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
970 }
971 else
972 {
973 int worig = w, horig = h;
974
975 #if !defined(_WIN32_WCE)
976 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
977 {
978 /*
979 * Update the window dimensions, taking account of window
980 * decorations. "freeglut" is to create the window with the
981 * outside of its border at (x,y) and with dimensions (w,h).
982 */
983 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
984 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
985 GetSystemMetrics( SM_CYCAPTION );
986 }
987 #endif /* defined(_WIN32_WCE) */
988
989 if( ! positionUse )
990 {
991 x = CW_USEDEFAULT;
992 y = CW_USEDEFAULT;
993 }
994 /* setting State.Width/Height to call resize callback later */
995 if( ! sizeUse )
996 {
997 if( ! window->IsMenu )
998 {
999 w = CW_USEDEFAULT;
1000 h = CW_USEDEFAULT;
1001 }
1002 else /* fail safe - Windows can make a window of size (0, 0) */
1003 w = h = 300; /* default window size */
1004 window->State.Width = window->State.Height = -1;
1005 }
1006 else
1007 {
1008 window->State.Width = worig;
1009 window->State.Height = horig;
1010 }
1011
1012 /*
1013 * There's a small difference between creating the top, child and
1014 * game mode windows
1015 */
1016 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
1017
1018 if ( window->IsMenu )
1019 {
1020 flags |= WS_POPUP;
1021 exFlags |= WS_EX_TOOLWINDOW;
1022 }
1023 #if !defined(_WIN32_WCE)
1024 else if( window->Parent == NULL )
1025 flags |= WS_OVERLAPPEDWINDOW;
1026 #endif
1027 else
1028 flags |= WS_CHILD;
1029 }
1030
1031 #if defined(_WIN32_WCE)
1032 {
1033 wchar_t* wstr = fghWstrFromStr(title);
1034
1035 window->Window.Handle = CreateWindow(
1036 _T("FREEGLUT"),
1037 wstr,
1038 WS_VISIBLE | WS_POPUP,
1039 0,0, 240,320,
1040 NULL,
1041 NULL,
1042 fgDisplay.Instance,
1043 (LPVOID) window
1044 );
1045
1046 free(wstr);
1047
1048 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
1049 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
1050 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
1051 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
1052 ShowWindow(window->Window.Handle, SW_SHOW);
1053 UpdateWindow(window->Window.Handle);
1054 }
1055 #else
1056 window->Window.Handle = CreateWindowEx(
1057 exFlags,
1058 _T("FREEGLUT"),
1059 title,
1060 flags,
1061 x, y, w, h,
1062 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
1063 (HMENU) NULL,
1064 fgDisplay.Instance,
1065 (LPVOID) window
1066 );
1067 #endif /* defined(_WIN32_WCE) */
1068
1069 if( !( window->Window.Handle ) )
1070 fgError( "Failed to create a window (%s)!", title );
1071
1072 /* Make a menu window always on top - fix Feature Request 947118 */
1073 if( window->IsMenu || gameMode )
1074 SetWindowPos(
1075 window->Window.Handle,
1076 HWND_TOPMOST,
1077 0, 0, 0, 0,
1078 SWP_NOMOVE | SWP_NOSIZE
1079 );
1080
1081 /* Hack to remove the caption (title bar) and/or border
1082 * and all the system menu controls.
1083 */
1084 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
1085 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
1086 {
1087 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1088 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
1089 }
1090 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
1091 {
1092 SetWindowLong ( window->Window.Handle, GWL_STYLE,
1093 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
1094 }
1095 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
1096 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
1097
1098
1099 #if defined(_WIN32_WCE)
1100 ShowWindow( window->Window.Handle, SW_SHOW );
1101 #else
1102 ShowWindow( window->Window.Handle,
1103 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
1104 #endif /* defined(_WIN32_WCE) */
1105
1106 UpdateWindow( window->Window.Handle );
1107 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
1108
1109 #endif
1110
1111 fgSetWindow( window );
1112
1113 window->Window.DoubleBuffered =
1114 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
1115
1116 if ( ! window->Window.DoubleBuffered )
1117 {
1118 glDrawBuffer ( GL_FRONT );
1119 glReadBuffer ( GL_FRONT );
1120 }
1121 }
1122
1123 /*
1124 * Closes a window, destroying the frame and OpenGL context
1125 */
fgCloseWindow(SFG_Window * window)1126 void fgCloseWindow( SFG_Window* window )
1127 {
1128 #if TARGET_HOST_POSIX_X11
1129
1130 if( window->Window.Context )
1131 glXDestroyContext( fgDisplay.Display, window->Window.Context );
1132 XFree( window->Window.FBConfig );
1133 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
1134 /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
1135
1136 #elif TARGET_HOST_MS_WINDOWS
1137
1138 /* Make sure we don't close a window with current context active */
1139 if( fgStructure.CurrentWindow == window )
1140 wglMakeCurrent( NULL, NULL );
1141
1142 /*
1143 * Step through the list of windows. If the rendering context
1144 * is not being used by another window, then we delete it.
1145 */
1146 {
1147 int used = FALSE ;
1148 SFG_Window *iter ;
1149
1150 for( iter = (SFG_Window *)fgStructure.Windows.First;
1151 iter;
1152 iter = (SFG_Window *)iter->Node.Next )
1153 {
1154 if( ( iter->Window.Context == window->Window.Context ) &&
1155 ( iter != window ) )
1156 used = TRUE;
1157 }
1158
1159 if( ! used )
1160 wglDeleteContext( window->Window.Context );
1161 }
1162
1163 DestroyWindow( window->Window.Handle );
1164 #endif
1165 }
1166
1167
1168 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1169
1170 /*
1171 * Creates a new top-level freeglut window
1172 */
glutCreateWindow(const char * title)1173 int FGAPIENTRY glutCreateWindow( const char* title )
1174 {
1175 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
1176 * XXX application has not already done so. The "freeglut" community
1177 * XXX decided not to go this route (freeglut-developer e-mail from
1178 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
1179 * XXX Desired 'freeglut' behaviour when there is no current window"
1180 */
1181 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
1182
1183 return fgCreateWindow( NULL, title, fgState.Position.Use,
1184 fgState.Position.X, fgState.Position.Y,
1185 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
1186 GL_FALSE, GL_FALSE )->ID;
1187 }
1188
1189 #if TARGET_HOST_MS_WINDOWS
__glutCreateWindowWithExit(const char * title,void (__cdecl * exit_function)(int))1190 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )
1191 {
1192 __glutExitFunc = exit_function;
1193 return glutCreateWindow( title );
1194 }
1195 #endif
1196
1197 /*
1198 * This function creates a sub window.
1199 */
glutCreateSubWindow(int parentID,int x,int y,int w,int h)1200 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
1201 {
1202 int ret = 0;
1203 SFG_Window* window = NULL;
1204 SFG_Window* parent = NULL;
1205
1206 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
1207 parent = fgWindowByID( parentID );
1208 freeglut_return_val_if_fail( parent != NULL, 0 );
1209 if ( x < 0 )
1210 {
1211 x = parent->State.Width + x ;
1212 if ( w >= 0 ) x -= w ;
1213 }
1214
1215 if ( w < 0 ) w = parent->State.Width - x + w ;
1216 if ( w < 0 )
1217 {
1218 x += w ;
1219 w = -w ;
1220 }
1221
1222 if ( y < 0 )
1223 {
1224 y = parent->State.Height + y ;
1225 if ( h >= 0 ) y -= h ;
1226 }
1227
1228 if ( h < 0 ) h = parent->State.Height - y + h ;
1229 if ( h < 0 )
1230 {
1231 y += h ;
1232 h = -h ;
1233 }
1234
1235 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
1236 ret = window->ID;
1237
1238 return ret;
1239 }
1240
1241 /*
1242 * Destroys a window and all of its subwindows
1243 */
glutDestroyWindow(int windowID)1244 void FGAPIENTRY glutDestroyWindow( int windowID )
1245 {
1246 SFG_Window* window;
1247 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1248 window = fgWindowByID( windowID );
1249 freeglut_return_if_fail( window != NULL );
1250 {
1251 fgExecutionState ExecState = fgState.ExecState;
1252 fgAddToWindowDestroyList( window );
1253 fgState.ExecState = ExecState;
1254 }
1255 }
1256
1257 /*
1258 * This function selects the current window
1259 */
glutSetWindow(int ID)1260 void FGAPIENTRY glutSetWindow( int ID )
1261 {
1262 SFG_Window* window = NULL;
1263
1264 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1265 if( fgStructure.CurrentWindow != NULL )
1266 if( fgStructure.CurrentWindow->ID == ID )
1267 return;
1268
1269 window = fgWindowByID( ID );
1270 if( window == NULL )
1271 {
1272 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1273 return;
1274 }
1275
1276 fgSetWindow( window );
1277 }
1278
1279 /*
1280 * This function returns the ID number of the current window, 0 if none exists
1281 */
glutGetWindow(void)1282 int FGAPIENTRY glutGetWindow( void )
1283 {
1284 SFG_Window *win = fgStructure.CurrentWindow;
1285 /*
1286 * Since GLUT did not throw an error if this function was called without a prior call to
1287 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1288 * See Feature Request "[ 1307049 ] glutInit check".
1289 */
1290 if ( ! fgState.Initialised )
1291 return 0;
1292
1293 while ( win && win->IsMenu )
1294 win = win->Parent;
1295 return win ? win->ID : 0;
1296 }
1297
1298 /*
1299 * This function makes the current window visible
1300 */
glutShowWindow(void)1301 void FGAPIENTRY glutShowWindow( void )
1302 {
1303 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1304 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1305
1306 #if TARGET_HOST_POSIX_X11
1307
1308 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1309 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1310
1311 #elif TARGET_HOST_MS_WINDOWS
1312
1313 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1314
1315 #endif
1316
1317 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1318 }
1319
1320 /*
1321 * This function hides the current window
1322 */
glutHideWindow(void)1323 void FGAPIENTRY glutHideWindow( void )
1324 {
1325 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1326 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1327
1328 #if TARGET_HOST_POSIX_X11
1329
1330 if( fgStructure.CurrentWindow->Parent == NULL )
1331 XWithdrawWindow( fgDisplay.Display,
1332 fgStructure.CurrentWindow->Window.Handle,
1333 fgDisplay.Screen );
1334 else
1335 XUnmapWindow( fgDisplay.Display,
1336 fgStructure.CurrentWindow->Window.Handle );
1337 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1338
1339 #elif TARGET_HOST_MS_WINDOWS
1340
1341 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1342
1343 #endif
1344
1345 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1346 }
1347
1348 /*
1349 * Iconify the current window (top-level windows only)
1350 */
glutIconifyWindow(void)1351 void FGAPIENTRY glutIconifyWindow( void )
1352 {
1353 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1354 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1355
1356 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1357 #if TARGET_HOST_POSIX_X11
1358
1359 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1360 fgDisplay.Screen );
1361 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1362
1363 #elif TARGET_HOST_MS_WINDOWS
1364
1365 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1366
1367 #endif
1368
1369 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1370 }
1371
1372 /*
1373 * Set the current window's title
1374 */
glutSetWindowTitle(const char * title)1375 void FGAPIENTRY glutSetWindowTitle( const char* title )
1376 {
1377 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1378 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1379 if( ! fgStructure.CurrentWindow->Parent )
1380 {
1381 #if TARGET_HOST_POSIX_X11
1382
1383 XTextProperty text;
1384
1385 text.value = (unsigned char *) title;
1386 text.encoding = XA_STRING;
1387 text.format = 8;
1388 text.nitems = strlen( title );
1389
1390 XSetWMName(
1391 fgDisplay.Display,
1392 fgStructure.CurrentWindow->Window.Handle,
1393 &text
1394 );
1395
1396 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1397
1398 #elif TARGET_HOST_MS_WINDOWS
1399 # ifdef _WIN32_WCE
1400 {
1401 wchar_t* wstr = fghWstrFromStr(title);
1402 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1403 free(wstr);
1404 }
1405 # else
1406 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1407 # endif
1408
1409 #endif
1410 }
1411 }
1412
1413 /*
1414 * Set the current window's iconified title
1415 */
glutSetIconTitle(const char * title)1416 void FGAPIENTRY glutSetIconTitle( const char* title )
1417 {
1418 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1419 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1420
1421 if( ! fgStructure.CurrentWindow->Parent )
1422 {
1423 #if TARGET_HOST_POSIX_X11
1424
1425 XTextProperty text;
1426
1427 text.value = (unsigned char *) title;
1428 text.encoding = XA_STRING;
1429 text.format = 8;
1430 text.nitems = strlen( title );
1431
1432 XSetWMIconName(
1433 fgDisplay.Display,
1434 fgStructure.CurrentWindow->Window.Handle,
1435 &text
1436 );
1437
1438 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1439
1440 #elif TARGET_HOST_MS_WINDOWS
1441 # ifdef _WIN32_WCE
1442 {
1443 wchar_t* wstr = fghWstrFromStr(title);
1444 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1445 free(wstr);
1446 }
1447 # else
1448 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1449 # endif
1450
1451 #endif
1452 }
1453 }
1454
1455 /*
1456 * Change the current window's size
1457 */
glutReshapeWindow(int width,int height)1458 void FGAPIENTRY glutReshapeWindow( int width, int height )
1459 {
1460 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1461 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1462
1463 if (glutGet(GLUT_FULL_SCREEN))
1464 {
1465 /* Leave full screen state before resizing. */
1466 glutFullScreenToggle();
1467 }
1468
1469 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1470 fgStructure.CurrentWindow->State.Width = width ;
1471 fgStructure.CurrentWindow->State.Height = height;
1472 }
1473
1474 /*
1475 * Change the current window's position
1476 */
glutPositionWindow(int x,int y)1477 void FGAPIENTRY glutPositionWindow( int x, int y )
1478 {
1479 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1480 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1481
1482 if (glutGet(GLUT_FULL_SCREEN))
1483 {
1484 /* Leave full screen state before moving. */
1485 glutFullScreenToggle();
1486 }
1487
1488 #if TARGET_HOST_POSIX_X11
1489
1490 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1491 x, y );
1492 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1493
1494 #elif TARGET_HOST_MS_WINDOWS
1495
1496 {
1497 RECT winRect;
1498
1499 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1500 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1501 MoveWindow(
1502 fgStructure.CurrentWindow->Window.Handle,
1503 x,
1504 y,
1505 winRect.right - winRect.left,
1506 winRect.bottom - winRect.top,
1507 TRUE
1508 );
1509 }
1510
1511 #endif
1512 }
1513
1514 /*
1515 * Lowers the current window (by Z order change)
1516 */
glutPushWindow(void)1517 void FGAPIENTRY glutPushWindow( void )
1518 {
1519 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1520 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1521
1522 #if TARGET_HOST_POSIX_X11
1523
1524 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1525
1526 #elif TARGET_HOST_MS_WINDOWS
1527
1528 SetWindowPos(
1529 fgStructure.CurrentWindow->Window.Handle,
1530 HWND_BOTTOM,
1531 0, 0, 0, 0,
1532 SWP_NOSIZE | SWP_NOMOVE
1533 );
1534
1535 #endif
1536 }
1537
1538 /*
1539 * Raises the current window (by Z order change)
1540 */
glutPopWindow(void)1541 void FGAPIENTRY glutPopWindow( void )
1542 {
1543 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1544 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1545
1546 #if TARGET_HOST_POSIX_X11
1547
1548 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1549
1550 #elif TARGET_HOST_MS_WINDOWS
1551
1552 SetWindowPos(
1553 fgStructure.CurrentWindow->Window.Handle,
1554 HWND_TOP,
1555 0, 0, 0, 0,
1556 SWP_NOSIZE | SWP_NOMOVE
1557 );
1558
1559 #endif
1560 }
1561
1562 #if TARGET_HOST_POSIX_X11
1563 static int ewmh_fullscr_toggle(void);
1564 static int resize_fullscr_toogle(void);
1565
toggle_fullscreen(void)1566 static int toggle_fullscreen(void)
1567 {
1568 /* first try the EWMH (_NET_WM_STATE) method ... */
1569 if(ewmh_fullscr_toggle() != -1) {
1570 return 0;
1571 }
1572
1573 /* fall back to resizing the window */
1574 if(resize_fullscr_toogle() != -1) {
1575 return 0;
1576 }
1577 return -1;
1578 }
1579
1580 #define _NET_WM_STATE_TOGGLE 2
ewmh_fullscr_toggle(void)1581 static int ewmh_fullscr_toggle(void)
1582 {
1583 XEvent xev;
1584 long evmask = SubstructureRedirectMask | SubstructureNotifyMask;
1585
1586 if(!fgDisplay.State || !fgDisplay.StateFullScreen) {
1587 return -1;
1588 }
1589
1590 xev.type = ClientMessage;
1591 xev.xclient.window = fgStructure.CurrentWindow->Window.Handle;
1592 xev.xclient.message_type = fgDisplay.State;
1593 xev.xclient.format = 32;
1594 xev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
1595 xev.xclient.data.l[1] = fgDisplay.StateFullScreen;
1596 xev.xclient.data.l[2] = 0; /* no second property to toggle */
1597 xev.xclient.data.l[3] = 1; /* source indication: application */
1598 xev.xclient.data.l[4] = 0; /* unused */
1599
1600 if(!XSendEvent(fgDisplay.Display, fgDisplay.RootWindow, 0, evmask, &xev)) {
1601 return -1;
1602 }
1603 return 0;
1604 }
1605
resize_fullscr_toogle(void)1606 static int resize_fullscr_toogle(void)
1607 {
1608 XWindowAttributes attributes;
1609
1610 if(glutGet(GLUT_FULL_SCREEN)) {
1611 /* restore original window size */
1612 SFG_Window *win = fgStructure.CurrentWindow;
1613 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1614 fgStructure.CurrentWindow->State.Width = win->State.OldWidth;
1615 fgStructure.CurrentWindow->State.Height = win->State.OldHeight;
1616
1617 } else {
1618 /* resize the window to cover the entire screen */
1619 XGetWindowAttributes(fgDisplay.Display,
1620 fgStructure.CurrentWindow->Window.Handle,
1621 &attributes);
1622
1623 /*
1624 * The "x" and "y" members of "attributes" are the window's coordinates
1625 * relative to its parent, i.e. to the decoration window.
1626 */
1627 XMoveResizeWindow(fgDisplay.Display,
1628 fgStructure.CurrentWindow->Window.Handle,
1629 -attributes.x,
1630 -attributes.y,
1631 fgDisplay.ScreenWidth,
1632 fgDisplay.ScreenHeight);
1633 }
1634 return 0;
1635 }
1636 #endif /* TARGET_HOST_POSIX_X11 */
1637
1638
1639 /*
1640 * Resize the current window so that it fits the whole screen
1641 */
glutFullScreen(void)1642 void FGAPIENTRY glutFullScreen( void )
1643 {
1644 SFG_Window *win;
1645
1646 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1647 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1648
1649 win = fgStructure.CurrentWindow;
1650
1651 #if TARGET_HOST_POSIX_X11
1652 if(!glutGet(GLUT_FULL_SCREEN)) {
1653 if(toggle_fullscreen() != -1) {
1654 win->State.IsFullscreen = GL_TRUE;
1655 }
1656 }
1657
1658 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1659
1660 if (glutGet(GLUT_FULL_SCREEN))
1661 {
1662 /* Leave full screen state before resizing. */
1663 glutFullScreenToggle();
1664 }
1665
1666 {
1667 RECT rect;
1668
1669 /* For fullscreen mode, force the top-left corner to 0,0
1670 * and adjust the window rectangle so that the client area
1671 * covers the whole screen.
1672 */
1673
1674 rect.left = 0;
1675 rect.top = 0;
1676 rect.right = fgDisplay.ScreenWidth;
1677 rect.bottom = fgDisplay.ScreenHeight;
1678
1679 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1680 WS_CLIPCHILDREN, FALSE );
1681
1682 /*
1683 * SWP_NOACTIVATE Do not activate the window
1684 * SWP_NOOWNERZORDER Do not change position in z-order
1685 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1686 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1687 */
1688
1689 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1690 HWND_TOP,
1691 rect.left,
1692 rect.top,
1693 rect.right - rect.left,
1694 rect.bottom - rect.top,
1695 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1696 SWP_NOZORDER
1697 );
1698
1699 win->State.IsFullscreen = GL_TRUE;
1700 }
1701 #endif
1702 }
1703
1704 /*
1705 * Toggle the window's full screen state.
1706 */
glutFullScreenToggle(void)1707 void FGAPIENTRY glutFullScreenToggle( void )
1708 {
1709 SFG_Window *win;
1710
1711 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1712 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1713
1714 win = fgStructure.CurrentWindow;
1715
1716 #if TARGET_HOST_POSIX_X11
1717 if(toggle_fullscreen() != -1) {
1718 win->State.IsFullscreen = !win->State.IsFullscreen;
1719 }
1720 #elif TARGET_HOST_MS_WINDOWS
1721 glutFullScreen();
1722 win->State.IsFullscreen = !win->State.IsFullscreen;
1723 #endif
1724 }
1725
1726 /*
1727 * A.Donev: Set and retrieve the window's user data
1728 */
glutGetWindowData(void)1729 void* FGAPIENTRY glutGetWindowData( void )
1730 {
1731 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1732 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1733 return fgStructure.CurrentWindow->UserData;
1734 }
1735
glutSetWindowData(void * data)1736 void FGAPIENTRY glutSetWindowData(void* data)
1737 {
1738 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1739 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1740 fgStructure.CurrentWindow->UserData = data;
1741 }
1742
1743 /*** END OF FILE ***/
1744