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_UNIX_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 );
123 * General settings query method
125 int FGAPIENTRY glutGet( GLenum eWhat )
127 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
129 GLboolean boolValue ;
134 case GLUT_INIT_STATE:
135 return fgState.Initialised;
137 case GLUT_ELAPSED_TIME:
138 return fgElapsedTime();
141 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
143 /* XXX In chronological code add order. (WHY in that order?) */
146 /* Following values are stored in fgState and fgDisplay global structures */
147 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
148 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
149 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
150 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
151 case GLUT_INIT_WINDOW_X: return fgState.Position.X ;
152 case GLUT_INIT_WINDOW_Y: return fgState.Position.Y ;
153 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.X ;
154 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Y ;
155 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
158 * The window/context specific queries are handled mostly by
161 case GLUT_WINDOW_NUM_SAMPLES:
162 /* XXX Multisampling. Return what I know about multisampling. */
165 #if TARGET_HOST_UNIX_X11
167 * The rest of GLX queries under X are general enough to use a macro to
170 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
172 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
173 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
174 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
175 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
176 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
177 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
178 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
179 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
180 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
181 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
182 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
183 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
184 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
185 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
189 /* Colormap size is handled in a bit different way than all the rest */
190 case GLUT_WINDOW_COLORMAP_SIZE:
191 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
194 * We've got a RGBA visual, so there is no colormap at all.
195 * The other possibility is that we have no current window set.
199 return fgStructure.CurrentWindow->Window.VisualInfo->visual->map_entries;
202 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
207 case GLUT_WINDOW_BORDER_WIDTH:
208 case GLUT_WINDOW_HEADER_HEIGHT:
213 if( fgStructure.CurrentWindow == NULL )
216 XTranslateCoordinates(
218 fgStructure.CurrentWindow->Window.Handle,
219 fgDisplay.RootWindow,
224 case GLUT_WINDOW_X: return x;
225 case GLUT_WINDOW_Y: return y;
230 XTranslateCoordinates(
232 fgStructure.CurrentWindow->Window.Handle,
233 w, 0, 0, &x, &y, &w);
237 case GLUT_WINDOW_BORDER_WIDTH: return x;
238 case GLUT_WINDOW_HEADER_HEIGHT: return y;
242 case GLUT_WINDOW_WIDTH:
243 case GLUT_WINDOW_HEIGHT:
245 XWindowAttributes winAttributes;
247 if( fgStructure.CurrentWindow == NULL )
249 XGetWindowAttributes(
251 fgStructure.CurrentWindow->Window.Handle,
256 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
257 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
261 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
262 case GLUT_DISPLAY_MODE_POSSIBLE:
263 return( fgChooseVisual() == NULL ? 0 : 1 );
265 /* This is system-dependant */
266 case GLUT_WINDOW_FORMAT_ID:
267 if( fgStructure.CurrentWindow == NULL )
270 return fgStructure.CurrentWindow->Window.VisualInfo->visualid;
272 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
274 /* Handle the OpenGL inquiries */
275 case GLUT_WINDOW_RGBA:
276 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
277 returnValue = boolValue ? 1 : 0;
279 case GLUT_WINDOW_DOUBLEBUFFER:
280 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
281 returnValue = boolValue ? 1 : 0;
283 case GLUT_WINDOW_STEREO:
284 glGetBooleanv ( GL_STEREO, &boolValue );
285 returnValue = boolValue ? 1 : 0;
288 case GLUT_WINDOW_RED_SIZE:
289 glGetIntegerv ( GL_RED_BITS, &returnValue );
291 case GLUT_WINDOW_GREEN_SIZE:
292 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
294 case GLUT_WINDOW_BLUE_SIZE:
295 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
297 case GLUT_WINDOW_ALPHA_SIZE:
298 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
300 case GLUT_WINDOW_ACCUM_RED_SIZE:
301 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
303 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
304 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
306 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
307 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
309 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
310 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
312 case GLUT_WINDOW_DEPTH_SIZE:
313 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
316 case GLUT_WINDOW_BUFFER_SIZE:
317 returnValue = 1 ; /* ????? */
319 case GLUT_WINDOW_STENCIL_SIZE:
320 returnValue = 0 ; /* ????? */
325 case GLUT_WINDOW_WIDTH:
326 case GLUT_WINDOW_HEIGHT:
329 * There is considerable confusion about the "right thing to
330 * do" concerning window size and position. GLUT itself is
331 * not consistent between Windows and UNIX/X11; since
332 * platform independence is a virtue for "freeglut", we
333 * decided to break with GLUT's behaviour.
335 * Under UNIX/X11, it is apparently not possible to get the
336 * window border sizes in order to subtract them off the
337 * window's initial position until some time after the window
338 * has been created. Therefore we decided on the following
339 * behaviour, both under Windows and under UNIX/X11:
340 * - When you create a window with position (x,y) and size
341 * (w,h), the upper left hand corner of the outside of the
342 * window is at (x,y) and the size of the drawable area is
344 * - When you query the size and position of the window--as
345 * is happening here for Windows--"freeglut" will return
346 * the size of the drawable area--the (w,h) that you
347 * specified when you created the window--and the coordinates
348 * of the upper left hand corner of the drawable
349 * area--which is NOT the (x,y) you specified.
354 freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
357 * We need to call GetWindowRect() first...
358 * (this returns the pixel coordinates of the outside of the window)
360 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
362 /* ...then we've got to correct the results we've just received... */
364 #if !TARGET_HOST_WINCE
365 if ( ( fgStructure.GameMode != fgStructure.CurrentWindow ) && ( fgStructure.CurrentWindow->Parent == NULL ) &&
366 ( ! fgStructure.CurrentWindow->IsMenu ) )
368 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
369 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
370 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
371 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
373 #endif /* !TARGET_HOST_WINCE */
377 case GLUT_WINDOW_X: return winRect.left ;
378 case GLUT_WINDOW_Y: return winRect.top ;
379 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
380 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
385 case GLUT_WINDOW_BORDER_WIDTH :
386 #if TARGET_HOST_WINCE
389 return GetSystemMetrics( SM_CXSIZEFRAME );
390 #endif /* !TARGET_HOST_WINCE */
392 case GLUT_WINDOW_HEADER_HEIGHT :
393 #if TARGET_HOST_WINCE
396 return GetSystemMetrics( SM_CYCAPTION );
397 #endif /* TARGET_HOST_WINCE */
399 case GLUT_DISPLAY_MODE_POSSIBLE:
400 #if TARGET_HOST_WINCE
403 return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
405 #endif /* TARGET_HOST_WINCE */
408 case GLUT_WINDOW_FORMAT_ID:
409 #if !TARGET_HOST_WINCE
410 if( fgStructure.CurrentWindow != NULL )
411 return GetPixelFormat( fgStructure.CurrentWindow->Window.Device );
412 #endif /* TARGET_HOST_WINCE */
417 /* The window structure queries */
418 case GLUT_WINDOW_PARENT:
419 if( fgStructure.CurrentWindow == NULL ) return 0;
420 if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
421 return fgStructure.CurrentWindow->Parent->ID;
423 case GLUT_WINDOW_NUM_CHILDREN:
424 if( fgStructure.CurrentWindow == NULL )
426 return fgListLength( &fgStructure.CurrentWindow->Children );
428 case GLUT_WINDOW_CURSOR:
429 if( fgStructure.CurrentWindow == NULL )
431 return fgStructure.CurrentWindow->State.Cursor;
433 case GLUT_MENU_NUM_ITEMS:
434 if( fgStructure.CurrentMenu == NULL )
436 return fgListLength( &fgStructure.CurrentMenu->Entries );
438 case GLUT_ACTION_ON_WINDOW_CLOSE:
439 return fgState.ActionOnWindowClose;
442 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
444 case GLUT_RENDERING_CONTEXT:
445 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
446 : GLUT_CREATE_NEW_CONTEXT;
448 case GLUT_DIRECT_RENDERING:
449 return fgState.DirectContext;
453 fgWarning( "glutGet(): missing enum handle %d", eWhat );
460 * Returns various device information.
462 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
464 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
466 /* XXX WARNING: we are mostly lying in this function. */
469 case GLUT_HAS_KEYBOARD:
471 * We always have a keyboard present on PC machines...
473 * XXX I think that some of my PCs will boot without a keyboard.
474 * XXX Also, who says that we are running on a PC? UNIX/X11
475 * XXX is much more generic, and X11 can go over a network.
476 * XXX Though in actuality, we can probably assume BOTH a
477 * XXX mouse and keyboard for most/all of our users.
481 #if TARGET_HOST_UNIX_X11
486 case GLUT_NUM_MOUSE_BUTTONS:
488 * Return the number of mouse buttons available. This is a big guess.
490 * XXX We can probe /var/run/dmesg.boot which is world-readable.
491 * XXX This would be somewhat system-dependant, but is doable.
492 * XXX E.g., on NetBSD, my USB mouse registers:
493 * XXX ums0 at uhidev0: 3 buttons and Z dir.
494 * XXX We can also probe /var/log/XFree86\..*\.log to get
496 * XXX (**) Option "Buttons" "5"
497 * XXX (**) Option "ZAxisMapping" "4 5"
498 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
499 * XXX (**) Mouse0: Buttons: 5
500 * XXX ...which tells us even more, and is a bit less
501 * XXX system-dependant. (Other than MS-WINDOWS, all
502 * XXX target hosts with actual users are probably running
503 * XXX XFree86...) It is at least worth taking a look at
508 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
512 * The Windows can be booted without a mouse.
513 * It would be nice to have this reported.
515 return GetSystemMetrics( SM_MOUSEPRESENT );
517 case GLUT_NUM_MOUSE_BUTTONS:
518 /* We are much more fortunate under Win32 about this... */
519 #if TARGET_HOST_WINCE
522 return GetSystemMetrics( SM_CMOUSEBUTTONS );
523 #endif /* TARGET_HOST_WINCE */
527 case GLUT_HAS_JOYSTICK:
528 return fgJoystickDetect ();
530 case GLUT_OWNS_JOYSTICK:
531 return fgState.JoysticksInitialised;
533 case GLUT_JOYSTICK_POLL_RATE:
534 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
536 /* XXX The following two are only for Joystick 0 but this is an improvement */
537 case GLUT_JOYSTICK_BUTTONS:
538 return glutJoystickGetNumButtons ( 0 );
540 case GLUT_JOYSTICK_AXES:
541 return glutJoystickGetNumAxes ( 0 );
543 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
544 return fgInputDeviceDetect ();
547 if ( fgState.InputDevsInitialised ) return 8;
550 case GLUT_NUM_BUTTON_BOX_BUTTONS:
553 case GLUT_HAS_SPACEBALL:
554 case GLUT_HAS_TABLET:
557 case GLUT_NUM_SPACEBALL_BUTTONS:
558 case GLUT_NUM_TABLET_BUTTONS:
561 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
562 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
564 case GLUT_DEVICE_KEY_REPEAT:
565 return fgState.KeyRepeat;
568 fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
572 /* And now -- the failure. */
577 * This should return the current state of ALT, SHIFT and CTRL keys.
579 int FGAPIENTRY glutGetModifiers( void )
581 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
582 if( fgState.Modifiers == 0xffffffff )
584 fgWarning( "glutGetModifiers() called outside an input callback" );
588 return fgState.Modifiers;
592 * Return the state of the GLUT API overlay subsystem. A misery ;-)
594 int FGAPIENTRY glutLayerGet( GLenum eWhat )
596 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
599 * This is easy as layers are not implemented ;-)
601 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
602 * XXX is overlay support planned?
607 #if TARGET_HOST_UNIX_X11
609 case GLUT_OVERLAY_POSSIBLE:
612 case GLUT_LAYER_IN_USE:
615 case GLUT_HAS_OVERLAY:
618 case GLUT_TRANSPARENT_INDEX:
620 * Return just anything, which is always defined as zero
626 case GLUT_NORMAL_DAMAGED:
627 /* XXX Actually I do not know. Maybe. */
630 case GLUT_OVERLAY_DAMAGED:
633 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
635 case GLUT_OVERLAY_POSSIBLE:
636 /* return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
637 PFD_OVERLAY_PLANE ); */
640 case GLUT_LAYER_IN_USE:
643 case GLUT_HAS_OVERLAY:
646 case GLUT_TRANSPARENT_INDEX:
648 * Return just anything, which is always defined as zero
654 case GLUT_NORMAL_DAMAGED:
655 /* XXX Actually I do not know. Maybe. */
658 case GLUT_OVERLAY_DAMAGED:
663 fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
667 /* And fail. That's good. Programs do love failing. */
671 /*** END OF FILE ***/