4 * Freeglut state query methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
32 * TODO BEFORE THE STABLE RELEASE:
34 * glutGet() -- X11 tests passed, but check if all enums
35 * handled (what about Win32?)
36 * glutDeviceGet() -- X11 tests passed, but check if all enums
37 * handled (what about Win32?)
38 * glutGetModifiers() -- OK, but could also remove the limitation
39 * glutLayerGet() -- what about GLUT_NORMAL_DAMAGED?
41 * The fail-on-call policy will help adding the most needed things imho.
44 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
46 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
48 #if TARGET_HOST_POSIX_X11
50 * Queries the GL context about some attributes
52 static int fghGetConfig( int attribute )
56 if( fgStructure.CurrentWindow )
57 glXGetConfig( fgDisplay.Display, fgStructure.CurrentWindow->Window.VisualInfo,
58 attribute, &returnValue );
64 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
67 * General settings assignment method
69 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
71 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
74 * XXX In chronological code add order. (WHY in that order?)
78 case GLUT_INIT_WINDOW_X:
79 fgState.Position.X = (GLint)value;
82 case GLUT_INIT_WINDOW_Y:
83 fgState.Position.Y = (GLint)value;
86 case GLUT_INIT_WINDOW_WIDTH:
87 fgState.Size.X = (GLint)value;
90 case GLUT_INIT_WINDOW_HEIGHT:
91 fgState.Size.Y = (GLint)value;
94 case GLUT_INIT_DISPLAY_MODE:
95 fgState.DisplayMode = (unsigned int)value;
98 case GLUT_ACTION_ON_WINDOW_CLOSE:
99 fgState.ActionOnWindowClose = value;
102 case GLUT_RENDERING_CONTEXT:
103 fgState.UseCurrentContext =
104 ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
107 case GLUT_DIRECT_RENDERING:
108 fgState.DirectContext = value;
111 case GLUT_WINDOW_CURSOR:
112 if( fgStructure.CurrentWindow != NULL )
113 fgStructure.CurrentWindow->State.Cursor = value;
117 fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
122 #if TARGET_HOST_MS_WINDOWS
123 /* The following include file is available from SGI but is not standard:
124 * #include <GL/wglext.h>
125 * So we copy the necessary parts out of it to support the multisampling query
127 #define WGL_SAMPLES_ARB 0x2042
132 * General settings query method
134 int FGAPIENTRY glutGet( GLenum eWhat )
136 #if TARGET_HOST_MS_WINDOWS
138 GLboolean boolValue ;
145 case GLUT_INIT_STATE:
146 return fgState.Initialised;
148 case GLUT_ELAPSED_TIME:
149 return fgElapsedTime();
152 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
154 /* XXX In chronological code add order. (WHY in that order?) */
157 /* Following values are stored in fgState and fgDisplay global structures */
158 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
159 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
160 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
161 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
162 case GLUT_INIT_WINDOW_X: return fgState.Position.X ;
163 case GLUT_INIT_WINDOW_Y: return fgState.Position.Y ;
164 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.X ;
165 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Y ;
166 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
168 #if TARGET_HOST_POSIX_X11
170 * The window/context specific queries are handled mostly by
173 case GLUT_WINDOW_NUM_SAMPLES:
174 #ifdef GLX_VERSION_1_3
175 glGetIntegerv(GL_SAMPLES, &nsamples);
180 * The rest of GLX queries under X are general enough to use a macro to
183 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
185 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
186 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
187 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
188 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
189 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
190 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
191 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
192 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
193 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
194 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
195 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
196 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
197 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
198 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
202 /* Colormap size is handled in a bit different way than all the rest */
203 case GLUT_WINDOW_COLORMAP_SIZE:
204 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
207 * We've got a RGBA visual, so there is no colormap at all.
208 * The other possibility is that we have no current window set.
212 return fgStructure.CurrentWindow->Window.VisualInfo->visual->map_entries;
215 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
220 case GLUT_WINDOW_BORDER_WIDTH:
221 case GLUT_WINDOW_HEADER_HEIGHT:
226 if( fgStructure.CurrentWindow == NULL )
229 XTranslateCoordinates(
231 fgStructure.CurrentWindow->Window.Handle,
232 fgDisplay.RootWindow,
237 case GLUT_WINDOW_X: return x;
238 case GLUT_WINDOW_Y: return y;
243 XTranslateCoordinates(
245 fgStructure.CurrentWindow->Window.Handle,
246 w, 0, 0, &x, &y, &w);
250 case GLUT_WINDOW_BORDER_WIDTH: return x;
251 case GLUT_WINDOW_HEADER_HEIGHT: return y;
255 case GLUT_WINDOW_WIDTH:
256 case GLUT_WINDOW_HEIGHT:
258 XWindowAttributes winAttributes;
260 if( fgStructure.CurrentWindow == NULL )
262 XGetWindowAttributes(
264 fgStructure.CurrentWindow->Window.Handle,
269 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
270 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
274 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
275 case GLUT_DISPLAY_MODE_POSSIBLE:
277 XVisualInfo* visualInfo = fgChooseVisual();
278 if ( visualInfo == NULL ) {
286 /* This is system-dependant */
287 case GLUT_WINDOW_FORMAT_ID:
288 if( fgStructure.CurrentWindow == NULL )
291 return fgStructure.CurrentWindow->Window.VisualInfo->visualid;
293 #elif TARGET_HOST_MS_WINDOWS
295 case GLUT_WINDOW_NUM_SAMPLES:
296 glGetIntegerv(WGL_SAMPLES_ARB, &nsamples);
299 /* Handle the OpenGL inquiries */
300 case GLUT_WINDOW_RGBA:
301 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
302 returnValue = boolValue ? 1 : 0;
304 case GLUT_WINDOW_DOUBLEBUFFER:
305 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
306 returnValue = boolValue ? 1 : 0;
308 case GLUT_WINDOW_STEREO:
309 glGetBooleanv ( GL_STEREO, &boolValue );
310 returnValue = boolValue ? 1 : 0;
313 case GLUT_WINDOW_RED_SIZE:
314 glGetIntegerv ( GL_RED_BITS, &returnValue );
316 case GLUT_WINDOW_GREEN_SIZE:
317 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
319 case GLUT_WINDOW_BLUE_SIZE:
320 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
322 case GLUT_WINDOW_ALPHA_SIZE:
323 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
325 case GLUT_WINDOW_ACCUM_RED_SIZE:
326 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
328 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
329 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
331 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
332 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
334 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
335 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
337 case GLUT_WINDOW_DEPTH_SIZE:
338 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
341 case GLUT_WINDOW_BUFFER_SIZE:
342 returnValue = 1 ; /* ????? */
344 case GLUT_WINDOW_STENCIL_SIZE:
345 returnValue = 0 ; /* ????? */
350 case GLUT_WINDOW_WIDTH:
351 case GLUT_WINDOW_HEIGHT:
354 * There is considerable confusion about the "right thing to
355 * do" concerning window size and position. GLUT itself is
356 * not consistent between Windows and UNIX/X11; since
357 * platform independence is a virtue for "freeglut", we
358 * decided to break with GLUT's behaviour.
360 * Under UNIX/X11, it is apparently not possible to get the
361 * window border sizes in order to subtract them off the
362 * window's initial position until some time after the window
363 * has been created. Therefore we decided on the following
364 * behaviour, both under Windows and under UNIX/X11:
365 * - When you create a window with position (x,y) and size
366 * (w,h), the upper left hand corner of the outside of the
367 * window is at (x,y) and the size of the drawable area is
369 * - When you query the size and position of the window--as
370 * is happening here for Windows--"freeglut" will return
371 * the size of the drawable area--the (w,h) that you
372 * specified when you created the window--and the coordinates
373 * of the upper left hand corner of the drawable
374 * area--which is NOT the (x,y) you specified.
379 freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
382 * We need to call GetWindowRect() first...
383 * (this returns the pixel coordinates of the outside of the window)
385 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
387 /* ...then we've got to correct the results we've just received... */
389 #if !defined(_WIN32_WCE)
390 if ( ( fgStructure.GameModeWindow != fgStructure.CurrentWindow ) && ( fgStructure.CurrentWindow->Parent == NULL ) &&
391 ( ! fgStructure.CurrentWindow->IsMenu ) )
393 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
394 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
395 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
396 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
398 #endif /* !defined(_WIN32_WCE) */
402 case GLUT_WINDOW_X: return winRect.left ;
403 case GLUT_WINDOW_Y: return winRect.top ;
404 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
405 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
410 case GLUT_WINDOW_BORDER_WIDTH :
411 #if defined(_WIN32_WCE)
414 return GetSystemMetrics( SM_CXSIZEFRAME );
415 #endif /* !defined(_WIN32_WCE) */
417 case GLUT_WINDOW_HEADER_HEIGHT :
418 #if defined(_WIN32_WCE)
421 return GetSystemMetrics( SM_CYCAPTION );
422 #endif /* defined(_WIN32_WCE) */
424 case GLUT_DISPLAY_MODE_POSSIBLE:
425 #if defined(_WIN32_WCE)
428 return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
430 #endif /* defined(_WIN32_WCE) */
433 case GLUT_WINDOW_FORMAT_ID:
434 #if !defined(_WIN32_WCE)
435 if( fgStructure.CurrentWindow != NULL )
436 return GetPixelFormat( fgStructure.CurrentWindow->Window.Device );
437 #endif /* defined(_WIN32_WCE) */
442 /* The window structure queries */
443 case GLUT_WINDOW_PARENT:
444 if( fgStructure.CurrentWindow == NULL ) return 0;
445 if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
446 return fgStructure.CurrentWindow->Parent->ID;
448 case GLUT_WINDOW_NUM_CHILDREN:
449 if( fgStructure.CurrentWindow == NULL )
451 return fgListLength( &fgStructure.CurrentWindow->Children );
453 case GLUT_WINDOW_CURSOR:
454 if( fgStructure.CurrentWindow == NULL )
456 return fgStructure.CurrentWindow->State.Cursor;
458 case GLUT_MENU_NUM_ITEMS:
459 if( fgStructure.CurrentMenu == NULL )
461 return fgListLength( &fgStructure.CurrentMenu->Entries );
463 case GLUT_ACTION_ON_WINDOW_CLOSE:
464 return fgState.ActionOnWindowClose;
467 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
469 case GLUT_RENDERING_CONTEXT:
470 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
471 : GLUT_CREATE_NEW_CONTEXT;
473 case GLUT_DIRECT_RENDERING:
474 return fgState.DirectContext;
478 fgWarning( "glutGet(): missing enum handle %d", eWhat );
485 * Returns various device information.
487 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
489 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
491 /* XXX WARNING: we are mostly lying in this function. */
494 case GLUT_HAS_KEYBOARD:
496 * Win32 is assumed a keyboard, and this cannot be queried,
497 * except for WindowsCE.
499 * X11 has a core keyboard by definition, although it can
500 * be present as a virtual/dummy keyboard. For now, there
501 * is no reliable way to tell if a real keyboard is present.
503 #if defined(_WIN32_CE)
504 return ( GetKeyboardStatus() & KBDI_KEYBOARD_PRESENT ) ? 1 : 0;
505 # if FREEGLUT_LIB_PRAGMAS
506 # pragma comment (lib,"Kbdui.lib")
513 #if TARGET_HOST_POSIX_X11
515 /* X11 has a mouse by definition */
519 case GLUT_NUM_MOUSE_BUTTONS:
520 /* We should be able to pass NULL when the last argument is zero,
521 * but at least one X server has a bug where this causes a segfault.
523 * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
524 * rather than an Axis; "freeglut_main.c" expects this when
525 * checking for a wheel event.
529 int nbuttons = XGetPointerMapping(fgDisplay.Display, &map,0);
533 #elif TARGET_HOST_MS_WINDOWS
537 * MS Windows can be booted without a mouse.
539 return GetSystemMetrics( SM_MOUSEPRESENT );
541 case GLUT_NUM_MOUSE_BUTTONS:
542 # if defined(_WIN32_WCE)
545 return GetSystemMetrics( SM_CMOUSEBUTTONS );
549 case GLUT_HAS_JOYSTICK:
550 return fgJoystickDetect ();
552 case GLUT_OWNS_JOYSTICK:
553 return fgState.JoysticksInitialised;
555 case GLUT_JOYSTICK_POLL_RATE:
556 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
558 /* XXX The following two are only for Joystick 0 but this is an improvement */
559 case GLUT_JOYSTICK_BUTTONS:
560 return glutJoystickGetNumButtons ( 0 );
562 case GLUT_JOYSTICK_AXES:
563 return glutJoystickGetNumAxes ( 0 );
565 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
566 return fgInputDeviceDetect ();
569 if ( fgState.InputDevsInitialised ) return 8;
572 case GLUT_NUM_BUTTON_BOX_BUTTONS:
575 case GLUT_HAS_SPACEBALL:
576 case GLUT_HAS_TABLET:
579 case GLUT_NUM_SPACEBALL_BUTTONS:
580 case GLUT_NUM_TABLET_BUTTONS:
583 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
584 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
586 case GLUT_DEVICE_KEY_REPEAT:
587 return fgState.KeyRepeat;
590 fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
594 /* And now -- the failure. */
599 * This should return the current state of ALT, SHIFT and CTRL keys.
601 int FGAPIENTRY glutGetModifiers( void )
603 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
604 if( fgState.Modifiers == INVALID_MODIFIERS )
606 fgWarning( "glutGetModifiers() called outside an input callback" );
610 return fgState.Modifiers;
614 * Return the state of the GLUT API overlay subsystem. A misery ;-)
616 int FGAPIENTRY glutLayerGet( GLenum eWhat )
618 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
621 * This is easy as layers are not implemented ;-)
623 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
624 * XXX is overlay support planned?
629 #if TARGET_HOST_POSIX_X11
631 case GLUT_OVERLAY_POSSIBLE:
634 case GLUT_LAYER_IN_USE:
637 case GLUT_HAS_OVERLAY:
640 case GLUT_TRANSPARENT_INDEX:
642 * Return just anything, which is always defined as zero
648 case GLUT_NORMAL_DAMAGED:
649 /* XXX Actually I do not know. Maybe. */
652 case GLUT_OVERLAY_DAMAGED:
655 #elif TARGET_HOST_MS_WINDOWS
657 case GLUT_OVERLAY_POSSIBLE:
658 /* return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
659 PFD_OVERLAY_PLANE ); */
662 case GLUT_LAYER_IN_USE:
665 case GLUT_HAS_OVERLAY:
668 case GLUT_TRANSPARENT_INDEX:
670 * Return just anything, which is always defined as zero
676 case GLUT_NORMAL_DAMAGED:
677 /* XXX Actually I do not know. Maybe. */
680 case GLUT_OVERLAY_DAMAGED:
685 fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
689 /* And fail. That's good. Programs do love failing. */
693 /*** END OF FILE ***/