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.
32 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
36 * TODO BEFORE THE STABLE RELEASE:
38 * glutGet() -- X11 tests passed, but check if all enums
39 * handled (what about Win32?)
40 * glutDeviceGet() -- X11 tests passed, but check if all enums
41 * handled (what about Win32?)
42 * glutGetModifiers() -- OK, but could also remove the limitation
43 * glutLayerGet() -- what about GLUT_NORMAL_DAMAGED?
45 * The fail-on-call policy will help adding the most needed things imho.
48 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
50 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
52 #if TARGET_HOST_UNIX_X11
54 * Queries the GL context about some attributes
56 static int fghGetConfig( int attribute )
60 if( fgStructure.Window )
61 glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo,
62 attribute, &returnValue );
68 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
71 * General settings assignment method
73 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
75 freeglut_assert_ready;
78 * XXX In chronological code add order. (WHY in that order?)
82 case GLUT_INIT_WINDOW_X:
83 fgState.Position.X = (GLint)value;
86 case GLUT_INIT_WINDOW_Y:
87 fgState.Position.Y = (GLint)value;
90 case GLUT_INIT_WINDOW_WIDTH:
91 fgState.Size.X = (GLint)value;
94 case GLUT_INIT_WINDOW_HEIGHT:
95 fgState.Size.Y = (GLint)value;
98 case GLUT_INIT_DISPLAY_MODE:
99 fgState.DisplayMode = (unsigned int)value;
102 case GLUT_ACTION_ON_WINDOW_CLOSE:
103 fgState.ActionOnWindowClose = value;
106 case GLUT_RENDERING_CONTEXT:
107 fgState.UseCurrentContext =
108 ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
111 case GLUT_DIRECT_RENDERING:
112 fgState.DirectContext = value;
115 case GLUT_WINDOW_CURSOR:
116 if( fgStructure.Window != NULL )
117 fgStructure.Window->State.Cursor = value;
121 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
127 * General settings query method
129 int FGAPIENTRY glutGet( GLenum eWhat )
131 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
133 GLboolean boolValue ;
138 case GLUT_INIT_STATE:
139 return fgState.Initialised;
141 case GLUT_ELAPSED_TIME:
142 return fgElapsedTime();
145 freeglut_assert_ready;
148 * XXX In chronological code add order. (WHY in that order?)
153 * Following values are stored in fgState and fgDisplay global structures
155 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
156 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
157 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
158 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
159 case GLUT_INIT_WINDOW_X: return fgState.Position.X ;
160 case GLUT_INIT_WINDOW_Y: return fgState.Position.Y ;
161 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.X ;
162 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Y ;
163 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
166 * The window/context specific queries are handled mostly by
169 case GLUT_WINDOW_NUM_SAMPLES:
171 * XXX Multisampling. Return what I know about multisampling.
175 #if TARGET_HOST_UNIX_X11
177 * The rest of GLX queries under X are general enough to use a macro to
180 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
182 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
183 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
184 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
185 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
186 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
187 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
188 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
189 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
190 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
191 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
192 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
193 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
194 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
195 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
200 * Colormap size is handled in a bit different way than all the rest
202 case GLUT_WINDOW_COLORMAP_SIZE:
203 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
206 * We've got a RGBA visual, so there is no colormap at all.
207 * The other possibility is that we have no current window set.
211 return fgStructure.Window->Window.VisualInfo->visual->map_entries;
214 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
219 case GLUT_WINDOW_BORDER_WIDTH:
220 case GLUT_WINDOW_HEADER_HEIGHT:
225 if( fgStructure.Window == NULL )
228 XTranslateCoordinates(
230 fgStructure.Window->Window.Handle,
231 fgDisplay.RootWindow,
236 case GLUT_WINDOW_X: return x;
237 case GLUT_WINDOW_Y: return y;
242 XTranslateCoordinates(
244 fgStructure.Window->Window.Handle,
245 w, 0, 0, &x, &y, &w);
249 case GLUT_WINDOW_BORDER_WIDTH: return x;
250 case GLUT_WINDOW_HEADER_HEIGHT: return y;
254 case GLUT_WINDOW_WIDTH:
255 case GLUT_WINDOW_HEIGHT:
257 XWindowAttributes winAttributes;
259 if( fgStructure.Window == NULL )
261 XGetWindowAttributes(
263 fgStructure.Window->Window.Handle,
268 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
269 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
274 * I do not know yet if there will be a fgChooseVisual() function for Win32
276 case GLUT_DISPLAY_MODE_POSSIBLE:
277 return( fgChooseVisual() == NULL ? 0 : 1 );
280 * This is system-dependant
282 case GLUT_WINDOW_FORMAT_ID:
283 if( fgStructure.Window == NULL )
286 return fgStructure.Window->Window.VisualInfo->visualid;
288 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
291 * Handle the OpenGL inquiries
293 case GLUT_WINDOW_RGBA:
294 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
295 returnValue = boolValue ? 1 : 0;
297 case GLUT_WINDOW_DOUBLEBUFFER:
298 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
299 returnValue = boolValue ? 1 : 0;
301 case GLUT_WINDOW_STEREO:
302 glGetBooleanv ( GL_STEREO, &boolValue );
303 returnValue = boolValue ? 1 : 0;
306 case GLUT_WINDOW_RED_SIZE:
307 glGetIntegerv ( GL_RED_BITS, &returnValue );
309 case GLUT_WINDOW_GREEN_SIZE:
310 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
312 case GLUT_WINDOW_BLUE_SIZE:
313 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
315 case GLUT_WINDOW_ALPHA_SIZE:
316 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
318 case GLUT_WINDOW_ACCUM_RED_SIZE:
319 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
321 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
322 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
324 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
325 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
327 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
328 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
330 case GLUT_WINDOW_DEPTH_SIZE:
331 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
334 case GLUT_WINDOW_BUFFER_SIZE:
335 returnValue = 1 ; /* ????? */
337 case GLUT_WINDOW_STENCIL_SIZE:
338 returnValue = 0 ; /* ????? */
343 case GLUT_WINDOW_WIDTH:
344 case GLUT_WINDOW_HEIGHT:
347 * There is considerable confusion about the "right thing to
348 * do" concerning window size and position. GLUT itself is
349 * not consistent between Windows and UNIX/X11; since
350 * platform independence is a virtue for "freeglut", we
351 * decided to break with GLUT's behaviour.
353 * Under UNIX/X11, it is apparently not possible to get the
354 * window border sizes in order to subtract them off the
355 * window's initial position until some time after the window
356 * has been created. Therefore we decided on the following
357 * behaviour, both under Windows and under UNIX/X11:
358 * - When you create a window with position (x,y) and size
359 * (w,h), the upper left hand corner of the outside of the
360 * window is at (x,y) and the size of the drawable area is
362 * - When you query the size and position of the window--as
363 * is happening here for Windows--"freeglut" will return
364 * the size of the drawable area--the (w,h) that you
365 * specified when you created the window--and the coordinates
366 * of the upper left hand corner of the drawable
367 * area--which is NOT the (x,y) you specified.
372 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
375 * We need to call GetWindowRect() first...
376 * (this returns the pixel coordinates of the outside of the window)
378 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
381 * ...then we've got to correct the results we've just received...
384 #if !TARGET_HOST_WINCE
385 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
386 ( ! fgStructure.Window->IsMenu ) )
388 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
389 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
390 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
391 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
393 #endif /* !TARGET_HOST_WINCE */
397 case GLUT_WINDOW_X: return winRect.left ;
398 case GLUT_WINDOW_Y: return winRect.top ;
399 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
400 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
405 case GLUT_WINDOW_BORDER_WIDTH :
406 #if TARGET_HOST_WINCE
409 return GetSystemMetrics( SM_CXSIZEFRAME );
410 #endif /* !TARGET_HOST_WINCE */
412 case GLUT_WINDOW_HEADER_HEIGHT :
413 #if TARGET_HOST_WINCE
416 return GetSystemMetrics( SM_CYCAPTION );
417 #endif /* TARGET_HOST_WINCE */
419 case GLUT_DISPLAY_MODE_POSSIBLE:
420 #if TARGET_HOST_WINCE
423 return fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
425 #endif /* TARGET_HOST_WINCE */
428 case GLUT_WINDOW_FORMAT_ID:
429 #if !TARGET_HOST_WINCE
430 if( fgStructure.Window != NULL )
431 return GetPixelFormat( fgStructure.Window->Window.Device );
432 #endif /* TARGET_HOST_WINCE */
438 * The window structure queries
440 case GLUT_WINDOW_PARENT:
441 if( fgStructure.Window == NULL ) return 0;
442 if( fgStructure.Window->Parent == NULL ) return 0;
443 return fgStructure.Window->Parent->ID;
445 case GLUT_WINDOW_NUM_CHILDREN:
446 if( fgStructure.Window == NULL )
448 return fgListLength( &fgStructure.Window->Children );
450 case GLUT_WINDOW_CURSOR:
451 if( fgStructure.Window == NULL )
453 return fgStructure.Window->State.Cursor;
455 case GLUT_MENU_NUM_ITEMS:
456 if( fgStructure.Menu == NULL )
458 return fgListLength( &fgStructure.Menu->Entries );
460 case GLUT_ACTION_ON_WINDOW_CLOSE:
461 return fgState.ActionOnWindowClose;
464 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
466 case GLUT_RENDERING_CONTEXT:
467 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
468 : GLUT_CREATE_NEW_CONTEXT;
470 case GLUT_DIRECT_RENDERING:
471 return fgState.DirectContext;
475 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
482 * Returns various device information.
484 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
486 freeglut_assert_ready;
489 * XXX WARNING: we are mostly lying in this function.
493 case GLUT_HAS_KEYBOARD:
495 * We always have a keyboard present on PC machines...
497 * XXX I think that some of my PCs will boot without a keyboard.
498 * XXX Also, who says that we are running on a PC? UNIX/X11
499 * XXX is much more generic, and X11 can go over a network.
500 * XXX Though in actuality, we can probably assume BOTH a
501 * XXX mouse and keyboard for most/all of our users.
505 #if TARGET_HOST_UNIX_X11
510 case GLUT_NUM_MOUSE_BUTTONS:
512 * Return the number of mouse buttons available. This is a big guess.
514 * XXX We can probe /var/run/dmesg.boot which is world-readable.
515 * XXX This would be somewhat system-dependant, but is doable.
516 * XXX E.g., on NetBSD, my USB mouse registers:
517 * XXX ums0 at uhidev0: 3 buttons and Z dir.
518 * XXX We can also probe /var/log/XFree86\..*\.log to get
520 * XXX (**) Option "Buttons" "5"
521 * XXX (**) Option "ZAxisMapping" "4 5"
522 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
523 * XXX (**) Mouse0: Buttons: 5
524 * XXX ...which tells us even more, and is a bit less
525 * XXX system-dependant. (Other than MS-WINDOWS, all
526 * XXX target hosts with actual users are probably running
527 * XXX XFree86...) It is at least worth taking a look at
532 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
536 * The Windows can be booted without a mouse.
537 * It would be nice to have this reported.
539 return GetSystemMetrics( SM_MOUSEPRESENT );
541 case GLUT_NUM_MOUSE_BUTTONS:
543 * We are much more fortunate under Win32 about this...
545 #if TARGET_HOST_WINCE
548 return GetSystemMetrics( SM_CMOUSEBUTTONS );
549 #endif /* TARGET_HOST_WINCE */
553 case GLUT_JOYSTICK_POLL_RATE:
554 case GLUT_HAS_JOYSTICK:
555 case GLUT_OWNS_JOYSTICK:
556 case GLUT_JOYSTICK_BUTTONS:
557 case GLUT_JOYSTICK_AXES:
559 * XXX WARNING: THIS IS A BIG LIE!
563 case GLUT_HAS_SPACEBALL:
564 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
565 case GLUT_HAS_TABLET:
568 case GLUT_NUM_SPACEBALL_BUTTONS:
569 case GLUT_NUM_BUTTON_BOX_BUTTONS:
571 case GLUT_NUM_TABLET_BUTTONS:
574 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
575 return fgStructure.Window ? fgStructure.Window->State.IgnoreKeyRepeat : 0;
577 case GLUT_DEVICE_KEY_REPEAT:
579 * XXX WARNING: THIS IS A BIG LIE!
581 return GLUT_KEY_REPEAT_DEFAULT;
584 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
589 * And now -- the failure.
595 * This should return the current state of ALT, SHIFT and CTRL keys.
597 int FGAPIENTRY glutGetModifiers( void )
599 if( fgState.Modifiers == 0xffffffff )
601 fgWarning( "glutGetModifiers() called outside an input callback" );
605 return fgState.Modifiers;
609 * Return the state of the GLUT API overlay subsystem. A misery ;-)
611 int FGAPIENTRY glutLayerGet( GLenum eWhat )
613 freeglut_assert_ready;
616 * This is easy as layers are not implemented ;-)
618 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
619 * XXX is overlay support planned?
624 #if TARGET_HOST_UNIX_X11
626 case GLUT_OVERLAY_POSSIBLE:
629 case GLUT_LAYER_IN_USE:
632 case GLUT_HAS_OVERLAY:
635 case GLUT_TRANSPARENT_INDEX:
637 * Return just anything, which is always defined as zero
643 case GLUT_NORMAL_DAMAGED:
645 * XXX Actually I do not know. Maybe.
649 case GLUT_OVERLAY_DAMAGED:
652 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
654 case GLUT_OVERLAY_POSSIBLE:
655 /* return fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
656 PFD_OVERLAY_PLANE ); */
659 case GLUT_LAYER_IN_USE:
662 case GLUT_HAS_OVERLAY:
665 case GLUT_TRANSPARENT_INDEX:
667 * Return just anything, which is always defined as zero
673 case GLUT_NORMAL_DAMAGED:
675 * XXX Actually I do not know. Maybe.
679 case GLUT_OVERLAY_DAMAGED:
684 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
689 * And fail. That's good. Programs do love failing.
694 /*** END OF FILE ***/