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 #define G_LOG_DOMAIN "freeglut-state"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * glutGet() -- X11 tests passed, but check if all enums
41 * handled (what about Win32?)
42 * glutDeviceGet() -- X11 tests passed, but check if all enums
43 * handled (what about Win32?)
44 * glutGetModifiers() -- OK, but could also remove the limitation
45 * glutLayerGet() -- what about GLUT_NORMAL_DAMAGED?
47 * The fail-on-call policy will help adding the most needed things imho.
50 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
52 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
54 #if TARGET_HOST_UNIX_X11
56 * Queries the GL context about some attributes
58 static int fghGetConfig( int attribute )
62 if( fgStructure.Window )
63 glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo,
64 attribute, &returnValue );
70 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
73 * General settings assignment method
75 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
77 freeglut_assert_ready;
80 * XXX In chronological code add order. (WHY in that order?)
84 case GLUT_INIT_WINDOW_X:
85 fgState.Position.X = (GLint)value;
88 case GLUT_INIT_WINDOW_Y:
89 fgState.Position.Y = (GLint)value;
92 case GLUT_INIT_WINDOW_WIDTH:
93 fgState.Size.X = (GLint)value;
96 case GLUT_INIT_WINDOW_HEIGHT:
97 fgState.Size.Y = (GLint)value;
100 case GLUT_INIT_DISPLAY_MODE:
101 fgState.DisplayMode = (unsigned int)value;
104 case GLUT_ACTION_ON_WINDOW_CLOSE:
105 fgState.ActionOnWindowClose = value;
108 case GLUT_RENDERING_CONTEXT:
109 fgState.UseCurrentContext =
110 ( value == GLUT_USE_CURRENT_CONTEXT ) ? TRUE : FALSE;
113 case GLUT_WINDOW_CURSOR:
114 if( fgStructure.Window != NULL )
115 fgStructure.Window->State.Cursor = value;
119 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
125 * General settings query method
127 int FGAPIENTRY glutGet( GLenum eWhat )
130 GLboolean boolValue ;
132 if ( eWhat == GLUT_INIT_STATE )
133 return ( fgState.Time.Set ) ;
135 freeglut_assert_ready;
138 * XXX In chronological code add order. (WHY in that order?)
142 case GLUT_ELAPSED_TIME:
143 return( fgElapsedTime() );
146 * Following values are stored in fgState and fgDisplay global structures
148 case GLUT_SCREEN_WIDTH: return( fgDisplay.ScreenWidth );
149 case GLUT_SCREEN_HEIGHT: return( fgDisplay.ScreenHeight );
150 case GLUT_SCREEN_WIDTH_MM: return( fgDisplay.ScreenWidthMM );
151 case GLUT_SCREEN_HEIGHT_MM: return( fgDisplay.ScreenHeightMM );
152 case GLUT_INIT_WINDOW_X: return( fgState.Position.X );
153 case GLUT_INIT_WINDOW_Y: return( fgState.Position.Y );
154 case GLUT_INIT_WINDOW_WIDTH: return( fgState.Size.X );
155 case GLUT_INIT_WINDOW_HEIGHT: return( fgState.Size.Y );
156 case GLUT_INIT_DISPLAY_MODE: return( fgState.DisplayMode );
159 * The window/context specific queries are handled mostly by
162 case GLUT_WINDOW_NUM_SAMPLES:
164 * XXX Multisampling. Return what I know about multisampling.
168 #if TARGET_HOST_UNIX_X11
170 * The rest of GLX queries under X are general enough to use a macro to
173 # define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
175 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
176 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
177 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
178 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
179 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
180 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
181 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
182 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
183 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
184 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
185 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
186 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
187 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
188 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
193 * Colormap size is handled in a bit different way than all the rest
195 case GLUT_WINDOW_COLORMAP_SIZE:
196 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
199 * We've got a RGBA visual, so there is no colormap at all.
200 * The other possibility is that we have no current window set.
204 return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
207 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
212 case GLUT_WINDOW_BORDER_WIDTH:
213 case GLUT_WINDOW_HEADER_HEIGHT:
218 if( fgStructure.Window == NULL )
221 XTranslateCoordinates(
223 fgStructure.Window->Window.Handle,
224 fgDisplay.RootWindow,
229 case GLUT_WINDOW_X: return x;
230 case GLUT_WINDOW_Y: return y;
235 XTranslateCoordinates(
237 fgStructure.Window->Window.Handle,
238 w, 0, 0, &x, &y, &w);
242 case GLUT_WINDOW_BORDER_WIDTH: return x;
243 case GLUT_WINDOW_HEADER_HEIGHT: return y;
247 case GLUT_WINDOW_WIDTH:
248 case GLUT_WINDOW_HEIGHT:
250 XWindowAttributes winAttributes;
252 if( fgStructure.Window == NULL )
254 XGetWindowAttributes(
256 fgStructure.Window->Window.Handle,
261 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
262 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
267 * I do not know yet if there will be a fgChooseVisual() function for Win32
269 case GLUT_DISPLAY_MODE_POSSIBLE:
270 return( fgChooseVisual() == NULL ? 0 : 1 );
273 * This is system-dependant
275 case GLUT_WINDOW_FORMAT_ID:
276 if( fgStructure.Window == NULL )
279 return( fgStructure.Window->Window.VisualInfo->visualid );
281 #elif TARGET_HOST_WIN32
284 * Handle the OpenGL inquiries
286 case GLUT_WINDOW_RGBA:
287 glGetBooleanv ( GL_RGBA_MODE, &boolValue ) ;
288 returnValue = boolValue ? 1 : 0 ;
289 return ( returnValue ) ;
290 case GLUT_WINDOW_DOUBLEBUFFER:
291 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue ) ;
292 returnValue = boolValue ? 1 : 0 ;
293 return ( returnValue ) ;
294 case GLUT_WINDOW_STEREO:
295 glGetBooleanv ( GL_STEREO, &boolValue ) ;
296 returnValue = boolValue ? 1 : 0 ;
297 return ( returnValue ) ;
299 case GLUT_WINDOW_RED_SIZE:
300 glGetIntegerv ( GL_RED_BITS, &returnValue ) ;
301 return ( returnValue ) ;
302 case GLUT_WINDOW_GREEN_SIZE:
303 glGetIntegerv ( GL_GREEN_BITS, &returnValue ) ;
304 return ( returnValue ) ;
305 case GLUT_WINDOW_BLUE_SIZE:
306 glGetIntegerv ( GL_BLUE_BITS, &returnValue ) ;
307 return ( returnValue ) ;
308 case GLUT_WINDOW_ALPHA_SIZE:
309 glGetIntegerv ( GL_ALPHA_BITS, &returnValue ) ;
310 return ( returnValue ) ;
311 case GLUT_WINDOW_ACCUM_RED_SIZE:
312 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue ) ;
313 return ( returnValue ) ;
314 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
315 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue ) ;
316 return ( returnValue ) ;
317 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
318 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue ) ;
319 return ( returnValue ) ;
320 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
321 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue ) ;
322 return ( returnValue ) ;
323 case GLUT_WINDOW_DEPTH_SIZE:
324 glGetIntegerv ( GL_DEPTH_BITS, &returnValue ) ;
325 return ( returnValue ) ;
327 case GLUT_WINDOW_BUFFER_SIZE:
328 returnValue = 1 ; /* ????? */
329 return ( returnValue ) ;
330 case GLUT_WINDOW_STENCIL_SIZE:
331 returnValue = 0 ; /* ????? */
332 return ( returnValue ) ;
336 case GLUT_WINDOW_WIDTH:
337 case GLUT_WINDOW_HEIGHT:
340 * There is considerable confusion about the "right thing to
341 * do" concerning window size and position. GLUT itself is
342 * not consistent between Windows and UNIX/X11; since
343 * platform independence is a virtue for "freeglut", we
344 * decided to break with GLUT's behaviour.
346 * Under UNIX/X11, it is apparently not possible to get the
347 * window border sizes in order to subtract them off the
348 * window's initial position until some time after the window
349 * has been created. Therefore we decided on the following
350 * behaviour, both under Windows and under UNIX/X11:
351 * - When you create a window with position (x,y) and size
352 * (w,h), the upper left hand corner of the outside of the
353 * window is at (x,y) and the size of the drawable area is
355 * - When you query the size and position of the window--as
356 * is happening here for Windows--"freeglut" will return
357 * the size of the drawable area--the (w,h) that you
358 * specified when you created the window--and the coordinates
359 * of the upper left hand corner of the drawable
360 * area--which is NOT the (x,y) you specified.
365 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
368 * We need to call GetWindowRect() first...
369 * (this returns the pixel coordinates of the outside of the window)
371 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
374 * ...then we've got to correct the results we've just received...
376 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
377 ( ! fgStructure.Window->IsMenu ) )
379 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
380 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
381 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
382 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
387 case GLUT_WINDOW_X: return( winRect.left );
388 case GLUT_WINDOW_Y: return( winRect.top );
389 case GLUT_WINDOW_WIDTH: return( winRect.right - winRect.left );
390 case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
395 case GLUT_WINDOW_BORDER_WIDTH :
396 return ( GetSystemMetrics( SM_CXSIZEFRAME ) ) ;
398 case GLUT_WINDOW_HEADER_HEIGHT :
399 return ( GetSystemMetrics( SM_CYCAPTION ) ) ;
401 case GLUT_DISPLAY_MODE_POSSIBLE:
402 return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_MAIN_PLANE ) );
404 case GLUT_WINDOW_FORMAT_ID:
405 if( fgStructure.Window != NULL )
406 return( GetPixelFormat( fgStructure.Window->Window.Device ) );
412 * The window structure queries
414 case GLUT_WINDOW_PARENT:
415 if( fgStructure.Window == NULL ) return( 0 );
416 if( fgStructure.Window->Parent == NULL ) return( 0 );
417 return( fgStructure.Window->Parent->ID );
419 case GLUT_WINDOW_NUM_CHILDREN:
420 if( fgStructure.Window == NULL )
422 return( fgListLength( &fgStructure.Window->Children ) );
424 case GLUT_WINDOW_CURSOR:
425 if( fgStructure.Window == NULL )
427 return( fgStructure.Window->State.Cursor );
429 case GLUT_MENU_NUM_ITEMS:
430 if( fgStructure.Menu == NULL )
432 return( fgListLength( &fgStructure.Menu->Entries ) );
434 case GLUT_ACTION_ON_WINDOW_CLOSE:
435 return fgState.ActionOnWindowClose ;
438 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH ;
440 case GLUT_RENDERING_CONTEXT:
441 return ( fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT : GLUT_CREATE_NEW_CONTEXT ) ;
444 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
451 * Returns various device information.
453 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
455 freeglut_assert_ready;
458 * XXX WARNING: we are mostly lying in this function.
462 case GLUT_HAS_KEYBOARD:
464 * We always have a keyboard present on PC machines...
466 * XXX I think that some of my PCs will boot without a keyboard.
467 * XXX Also, who says that we are running on a PC? UNIX/X11
468 * XXX is much more generic, and X11 can go over a network.
469 * XXX Though in actuality, we can probably assume BOTH a
470 * XXX mouse and keyboard for most/all of our users.
474 #if TARGET_HOST_UNIX_X11
479 case GLUT_NUM_MOUSE_BUTTONS:
481 * Return the number of mouse buttons available. This is a big guess.
483 * XXX We can probe /var/run/dmesg.boot which is world-readable.
484 * XXX This would be somewhat system-dependant, but is doable.
485 * XXX E.g., on NetBSD, my USB mouse registers:
486 * XXX ums0 at uhidev0: 3 buttons and Z dir.
487 * XXX We can also probe /var/log/XFree86\..*\.log to get
489 * XXX (**) Option "Buttons" "5"
490 * XXX (**) Option "ZAxisMapping" "4 5"
491 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
492 * XXX (**) Mouse0: Buttons: 5
493 * XXX ...which tells us even more, and is a bit less
494 * XXX system-dependant. (Other than MS-WINDOWS, all
495 * XXX target hosts with actual users are probably running
496 * XXX XFree86...) It is at least worth taking a look at
501 #elif TARGET_HOST_WIN32
505 * The Windows can be booted without a mouse.
506 * It would be nice to have this reported.
508 return( GetSystemMetrics( SM_MOUSEPRESENT ) );
510 case GLUT_NUM_MOUSE_BUTTONS:
512 * We are much more fortunate under Win32 about this...
514 return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
518 case GLUT_JOYSTICK_POLL_RATE:
519 case GLUT_HAS_JOYSTICK:
520 case GLUT_JOYSTICK_BUTTONS:
521 case GLUT_JOYSTICK_AXES:
523 * XXX WARNING: THIS IS A BIG LIE!
527 case GLUT_HAS_SPACEBALL:
528 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
529 case GLUT_HAS_TABLET:
532 case GLUT_NUM_SPACEBALL_BUTTONS:
533 case GLUT_NUM_BUTTON_BOX_BUTTONS:
535 case GLUT_NUM_TABLET_BUTTONS:
538 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
539 return( fgState.IgnoreKeyRepeat );
541 case GLUT_DEVICE_KEY_REPEAT:
543 * XXX WARNING: THIS IS A BIG LIE!
545 return( GLUT_KEY_REPEAT_DEFAULT );
548 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
553 * And now -- the failure.
559 * This should return the current state of ALT, SHIFT and CTRL keys.
561 int FGAPIENTRY glutGetModifiers( void )
563 if( fgStructure.Window == NULL )
566 if( fgStructure.Window->State.Modifiers == 0xffffffff )
568 fgWarning( "glutGetModifiers() called outside an input callback" );
572 return( fgStructure.Window->State.Modifiers );
576 * Return the state of the GLUT API overlay subsystem. A misery ;-)
578 int FGAPIENTRY glutLayerGet( GLenum eWhat )
580 freeglut_assert_ready;
583 * This is easy as layers are not implemented ;-)
585 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
586 * XXX is overlay support planned?
591 #if TARGET_HOST_UNIX_X11
593 case GLUT_OVERLAY_POSSIBLE:
596 case GLUT_LAYER_IN_USE:
597 return( GLUT_NORMAL );
599 case GLUT_HAS_OVERLAY:
602 case GLUT_TRANSPARENT_INDEX:
604 * Return just anything, which is always defined as zero
610 case GLUT_NORMAL_DAMAGED:
612 * XXX Actually I do not know. Maybe.
616 case GLUT_OVERLAY_DAMAGED:
619 #elif TARGET_HOST_WIN32
621 case GLUT_OVERLAY_POSSIBLE:
622 /* return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_OVERLAY_PLANE ) ); */
625 case GLUT_LAYER_IN_USE:
626 return( GLUT_NORMAL );
628 case GLUT_HAS_OVERLAY:
631 case GLUT_TRANSPARENT_INDEX:
633 * Return just anything, which is always defined as zero
639 case GLUT_NORMAL_DAMAGED:
641 * XXX Actually I do not know. Maybe.
645 case GLUT_OVERLAY_DAMAGED:
650 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
655 * And fail. That's good. Programs do love failing.
660 /*** END OF FILE ***/