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 "../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_WINDOW_CURSOR:
112 if( fgStructure.Window != NULL )
113 fgStructure.Window->State.Cursor = value;
117 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
123 * General settings query method
125 int FGAPIENTRY glutGet( GLenum eWhat )
128 GLboolean boolValue ;
132 case GLUT_INIT_STATE:
133 return ( fgState.Initalized ) ;
135 case GLUT_ELAPSED_TIME:
136 return( fgElapsedTime() );
139 freeglut_assert_ready;
142 * XXX In chronological code add order. (WHY in that order?)
147 * Following values are stored in fgState and fgDisplay global structures
149 case GLUT_SCREEN_WIDTH: return( fgDisplay.ScreenWidth );
150 case GLUT_SCREEN_HEIGHT: return( fgDisplay.ScreenHeight );
151 case GLUT_SCREEN_WIDTH_MM: return( fgDisplay.ScreenWidthMM );
152 case GLUT_SCREEN_HEIGHT_MM: return( fgDisplay.ScreenHeightMM );
153 case GLUT_INIT_WINDOW_X: return( fgState.Position.X );
154 case GLUT_INIT_WINDOW_Y: return( fgState.Position.Y );
155 case GLUT_INIT_WINDOW_WIDTH: return( fgState.Size.X );
156 case GLUT_INIT_WINDOW_HEIGHT: return( fgState.Size.Y );
157 case GLUT_INIT_DISPLAY_MODE: return( fgState.DisplayMode );
160 * The window/context specific queries are handled mostly by
163 case GLUT_WINDOW_NUM_SAMPLES:
165 * XXX Multisampling. Return what I know about multisampling.
169 #if TARGET_HOST_UNIX_X11
171 * The rest of GLX queries under X are general enough to use a macro to
174 # define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
176 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
177 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
178 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
179 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
180 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
181 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
182 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
183 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
184 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
185 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
186 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
187 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
188 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
189 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
194 * Colormap size is handled in a bit different way than all the rest
196 case GLUT_WINDOW_COLORMAP_SIZE:
197 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
200 * We've got a RGBA visual, so there is no colormap at all.
201 * The other possibility is that we have no current window set.
205 return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
208 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
213 case GLUT_WINDOW_BORDER_WIDTH:
214 case GLUT_WINDOW_HEADER_HEIGHT:
219 if( fgStructure.Window == NULL )
222 XTranslateCoordinates(
224 fgStructure.Window->Window.Handle,
225 fgDisplay.RootWindow,
230 case GLUT_WINDOW_X: return x;
231 case GLUT_WINDOW_Y: return y;
236 XTranslateCoordinates(
238 fgStructure.Window->Window.Handle,
239 w, 0, 0, &x, &y, &w);
243 case GLUT_WINDOW_BORDER_WIDTH: return x;
244 case GLUT_WINDOW_HEADER_HEIGHT: return y;
248 case GLUT_WINDOW_WIDTH:
249 case GLUT_WINDOW_HEIGHT:
251 XWindowAttributes winAttributes;
253 if( fgStructure.Window == NULL )
255 XGetWindowAttributes(
257 fgStructure.Window->Window.Handle,
262 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
263 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
268 * I do not know yet if there will be a fgChooseVisual() function for Win32
270 case GLUT_DISPLAY_MODE_POSSIBLE:
271 return( fgChooseVisual() == NULL ? 0 : 1 );
274 * This is system-dependant
276 case GLUT_WINDOW_FORMAT_ID:
277 if( fgStructure.Window == NULL )
280 return( fgStructure.Window->Window.VisualInfo->visualid );
282 #elif TARGET_HOST_WIN32
285 * Handle the OpenGL inquiries
287 case GLUT_WINDOW_RGBA:
288 glGetBooleanv ( GL_RGBA_MODE, &boolValue ) ;
289 returnValue = boolValue ? 1 : 0 ;
290 return ( returnValue ) ;
291 case GLUT_WINDOW_DOUBLEBUFFER:
292 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue ) ;
293 returnValue = boolValue ? 1 : 0 ;
294 return ( returnValue ) ;
295 case GLUT_WINDOW_STEREO:
296 glGetBooleanv ( GL_STEREO, &boolValue ) ;
297 returnValue = boolValue ? 1 : 0 ;
298 return ( returnValue ) ;
300 case GLUT_WINDOW_RED_SIZE:
301 glGetIntegerv ( GL_RED_BITS, &returnValue ) ;
302 return ( returnValue ) ;
303 case GLUT_WINDOW_GREEN_SIZE:
304 glGetIntegerv ( GL_GREEN_BITS, &returnValue ) ;
305 return ( returnValue ) ;
306 case GLUT_WINDOW_BLUE_SIZE:
307 glGetIntegerv ( GL_BLUE_BITS, &returnValue ) ;
308 return ( returnValue ) ;
309 case GLUT_WINDOW_ALPHA_SIZE:
310 glGetIntegerv ( GL_ALPHA_BITS, &returnValue ) ;
311 return ( returnValue ) ;
312 case GLUT_WINDOW_ACCUM_RED_SIZE:
313 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue ) ;
314 return ( returnValue ) ;
315 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
316 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue ) ;
317 return ( returnValue ) ;
318 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
319 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue ) ;
320 return ( returnValue ) ;
321 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
322 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue ) ;
323 return ( returnValue ) ;
324 case GLUT_WINDOW_DEPTH_SIZE:
325 glGetIntegerv ( GL_DEPTH_BITS, &returnValue ) ;
326 return ( returnValue ) ;
328 case GLUT_WINDOW_BUFFER_SIZE:
329 returnValue = 1 ; /* ????? */
330 return ( returnValue ) ;
331 case GLUT_WINDOW_STENCIL_SIZE:
332 returnValue = 0 ; /* ????? */
333 return ( returnValue ) ;
337 case GLUT_WINDOW_WIDTH:
338 case GLUT_WINDOW_HEIGHT:
341 * There is considerable confusion about the "right thing to
342 * do" concerning window size and position. GLUT itself is
343 * not consistent between Windows and UNIX/X11; since
344 * platform independence is a virtue for "freeglut", we
345 * decided to break with GLUT's behaviour.
347 * Under UNIX/X11, it is apparently not possible to get the
348 * window border sizes in order to subtract them off the
349 * window's initial position until some time after the window
350 * has been created. Therefore we decided on the following
351 * behaviour, both under Windows and under UNIX/X11:
352 * - When you create a window with position (x,y) and size
353 * (w,h), the upper left hand corner of the outside of the
354 * window is at (x,y) and the size of the drawable area is
356 * - When you query the size and position of the window--as
357 * is happening here for Windows--"freeglut" will return
358 * the size of the drawable area--the (w,h) that you
359 * specified when you created the window--and the coordinates
360 * of the upper left hand corner of the drawable
361 * area--which is NOT the (x,y) you specified.
366 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
369 * We need to call GetWindowRect() first...
370 * (this returns the pixel coordinates of the outside of the window)
372 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
375 * ...then we've got to correct the results we've just received...
377 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
378 ( ! fgStructure.Window->IsMenu ) )
380 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
381 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
382 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
383 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
388 case GLUT_WINDOW_X: return( winRect.left );
389 case GLUT_WINDOW_Y: return( winRect.top );
390 case GLUT_WINDOW_WIDTH: return( winRect.right - winRect.left );
391 case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
396 case GLUT_WINDOW_BORDER_WIDTH :
397 return ( GetSystemMetrics( SM_CXSIZEFRAME ) ) ;
399 case GLUT_WINDOW_HEADER_HEIGHT :
400 return ( GetSystemMetrics( SM_CYCAPTION ) ) ;
402 case GLUT_DISPLAY_MODE_POSSIBLE:
403 return( fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
406 case GLUT_WINDOW_FORMAT_ID:
407 if( fgStructure.Window != NULL )
408 return( GetPixelFormat( fgStructure.Window->Window.Device ) );
414 * The window structure queries
416 case GLUT_WINDOW_PARENT:
417 if( fgStructure.Window == NULL ) return( 0 );
418 if( fgStructure.Window->Parent == NULL ) return( 0 );
419 return( fgStructure.Window->Parent->ID );
421 case GLUT_WINDOW_NUM_CHILDREN:
422 if( fgStructure.Window == NULL )
424 return( fgListLength( &fgStructure.Window->Children ) );
426 case GLUT_WINDOW_CURSOR:
427 if( fgStructure.Window == NULL )
429 return( fgStructure.Window->State.Cursor );
431 case GLUT_MENU_NUM_ITEMS:
432 if( fgStructure.Menu == NULL )
434 return( fgListLength( &fgStructure.Menu->Entries ) );
436 case GLUT_ACTION_ON_WINDOW_CLOSE:
437 return fgState.ActionOnWindowClose ;
440 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH ;
442 case GLUT_RENDERING_CONTEXT:
443 return ( fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT : GLUT_CREATE_NEW_CONTEXT ) ;
446 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
453 * Returns various device information.
455 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
457 freeglut_assert_ready;
460 * XXX WARNING: we are mostly lying in this function.
464 case GLUT_HAS_KEYBOARD:
466 * We always have a keyboard present on PC machines...
468 * XXX I think that some of my PCs will boot without a keyboard.
469 * XXX Also, who says that we are running on a PC? UNIX/X11
470 * XXX is much more generic, and X11 can go over a network.
471 * XXX Though in actuality, we can probably assume BOTH a
472 * XXX mouse and keyboard for most/all of our users.
476 #if TARGET_HOST_UNIX_X11
481 case GLUT_NUM_MOUSE_BUTTONS:
483 * Return the number of mouse buttons available. This is a big guess.
485 * XXX We can probe /var/run/dmesg.boot which is world-readable.
486 * XXX This would be somewhat system-dependant, but is doable.
487 * XXX E.g., on NetBSD, my USB mouse registers:
488 * XXX ums0 at uhidev0: 3 buttons and Z dir.
489 * XXX We can also probe /var/log/XFree86\..*\.log to get
491 * XXX (**) Option "Buttons" "5"
492 * XXX (**) Option "ZAxisMapping" "4 5"
493 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
494 * XXX (**) Mouse0: Buttons: 5
495 * XXX ...which tells us even more, and is a bit less
496 * XXX system-dependant. (Other than MS-WINDOWS, all
497 * XXX target hosts with actual users are probably running
498 * XXX XFree86...) It is at least worth taking a look at
503 #elif TARGET_HOST_WIN32
507 * The Windows can be booted without a mouse.
508 * It would be nice to have this reported.
510 return( GetSystemMetrics( SM_MOUSEPRESENT ) );
512 case GLUT_NUM_MOUSE_BUTTONS:
514 * We are much more fortunate under Win32 about this...
516 return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
520 case GLUT_JOYSTICK_POLL_RATE:
521 case GLUT_HAS_JOYSTICK:
522 case GLUT_JOYSTICK_BUTTONS:
523 case GLUT_JOYSTICK_AXES:
525 * XXX WARNING: THIS IS A BIG LIE!
529 case GLUT_HAS_SPACEBALL:
530 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
531 case GLUT_HAS_TABLET:
534 case GLUT_NUM_SPACEBALL_BUTTONS:
535 case GLUT_NUM_BUTTON_BOX_BUTTONS:
537 case GLUT_NUM_TABLET_BUTTONS:
540 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
541 return( fgState.IgnoreKeyRepeat );
543 case GLUT_DEVICE_KEY_REPEAT:
545 * XXX WARNING: THIS IS A BIG LIE!
547 return( GLUT_KEY_REPEAT_DEFAULT );
550 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
555 * And now -- the failure.
561 * This should return the current state of ALT, SHIFT and CTRL keys.
563 int FGAPIENTRY glutGetModifiers( void )
565 if( fgStructure.Window == NULL )
568 if( fgStructure.Window->State.Modifiers == 0xffffffff )
570 fgWarning( "glutGetModifiers() called outside an input callback" );
574 return( fgStructure.Window->State.Modifiers );
578 * Return the state of the GLUT API overlay subsystem. A misery ;-)
580 int FGAPIENTRY glutLayerGet( GLenum eWhat )
582 freeglut_assert_ready;
585 * This is easy as layers are not implemented ;-)
587 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
588 * XXX is overlay support planned?
593 #if TARGET_HOST_UNIX_X11
595 case GLUT_OVERLAY_POSSIBLE:
598 case GLUT_LAYER_IN_USE:
599 return( GLUT_NORMAL );
601 case GLUT_HAS_OVERLAY:
604 case GLUT_TRANSPARENT_INDEX:
606 * Return just anything, which is always defined as zero
612 case GLUT_NORMAL_DAMAGED:
614 * XXX Actually I do not know. Maybe.
618 case GLUT_OVERLAY_DAMAGED:
621 #elif TARGET_HOST_WIN32
623 case GLUT_OVERLAY_POSSIBLE:
624 /* return( fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
625 PFD_OVERLAY_PLANE ) ); */
628 case GLUT_LAYER_IN_USE:
629 return( GLUT_NORMAL );
631 case GLUT_HAS_OVERLAY:
634 case GLUT_TRANSPARENT_INDEX:
636 * Return just anything, which is always defined as zero
642 case GLUT_NORMAL_DAMAGED:
644 * XXX Actually I do not know. Maybe.
648 case GLUT_OVERLAY_DAMAGED:
653 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
658 * And fail. That's good. Programs do love failing.
663 /*** END OF FILE ***/