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 handled (what about Win32?)
41 * glutDeviceGet() -- X11 tests passed, but check if all enums 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 )
61 * Return nothing if there is no current window set
63 if( fgStructure.Window == NULL )
67 * glXGetConfig should work fine
69 glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo, attribute, &returnValue );
73 * Have the query results returned
75 return ( returnValue ) ;
79 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
82 * General settings assignment method
84 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
86 freeglut_assert_ready;
89 * Check what is the caller querying for. In chronological code add order.
93 case GLUT_INIT_WINDOW_X: fgState.Position.X = (GLint)value ;
95 case GLUT_INIT_WINDOW_Y: fgState.Position.Y = (GLint)value ;
97 case GLUT_INIT_WINDOW_WIDTH: fgState.Size.X = (GLint)value ;
99 case GLUT_INIT_WINDOW_HEIGHT: fgState.Size.Y = (GLint)value ;
101 case GLUT_INIT_DISPLAY_MODE: fgState.DisplayMode = (unsigned int)value ;
104 case GLUT_ACTION_ON_WINDOW_CLOSE: fgState.ActionOnWindowClose = value ;
107 case GLUT_RENDERING_CONTEXT: fgState.UseCurrentContext = ( value == GLUT_USE_CURRENT_CONTEXT ) ? TRUE : FALSE ;
110 case GLUT_WINDOW_CURSOR:
111 if( fgStructure.Window != NULL ) fgStructure.Window->State.Cursor = value ;
116 * Just have it reported, so that we can see what needs to be implemented
118 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
124 * General settings query method
126 int FGAPIENTRY glutGet( GLenum eWhat )
129 GLboolean boolValue ;
131 if ( eWhat == GLUT_INIT_STATE )
132 return ( fgState.Time.Set ) ;
134 freeglut_assert_ready;
137 * Check what is the caller querying for. In chronological code add order.
141 case GLUT_ELAPSED_TIME:
143 * This is easy and nicely portable, as we are using GLib...
145 return( fgElapsedTime() );
148 * Following values are stored in fgState and fgDisplay global structures
150 case GLUT_SCREEN_WIDTH: return( fgDisplay.ScreenWidth );
151 case GLUT_SCREEN_HEIGHT: return( fgDisplay.ScreenHeight );
152 case GLUT_SCREEN_WIDTH_MM: return( fgDisplay.ScreenWidthMM );
153 case GLUT_SCREEN_HEIGHT_MM: return( fgDisplay.ScreenHeightMM );
154 case GLUT_INIT_WINDOW_X: return( fgState.Position.X );
155 case GLUT_INIT_WINDOW_Y: return( fgState.Position.Y );
156 case GLUT_INIT_WINDOW_WIDTH: return( fgState.Size.X );
157 case GLUT_INIT_WINDOW_HEIGHT: return( fgState.Size.Y );
158 case GLUT_INIT_DISPLAY_MODE: return( fgState.DisplayMode );
161 * The window/context specific queries are handled mostly by fghGetConfig().
163 case GLUT_WINDOW_NUM_SAMPLES:
165 * 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 check them
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:
197 * Check for the visual type
199 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
202 * We've got a RGBA visual, so there is no colormap at all.
203 * The other possibility is that we have no current window set.
209 * Otherwise return the number of entries in the colormap
211 return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
214 * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
218 case GLUT_WINDOW_WIDTH:
219 case GLUT_WINDOW_HEIGHT:
220 case GLUT_WINDOW_BORDER_WIDTH :
221 case GLUT_WINDOW_HEADER_HEIGHT :
223 XWindowAttributes winAttributes;
226 * Return zero if there is no current window set
228 if( fgStructure.Window == NULL )
232 * Grab the current window's attributes now
234 XGetWindowAttributes(
236 fgStructure.Window->Window.Handle,
241 * See which window attribute to return
245 case GLUT_WINDOW_X: return winAttributes.x ;
246 case GLUT_WINDOW_Y: return winAttributes.y ;
247 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
248 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
249 case GLUT_WINDOW_BORDER_WIDTH : return winAttributes.border_width ;
250 case GLUT_WINDOW_HEADER_HEIGHT : return winAttributes.border_width * 3 ; /* a kludge for now */
255 * I do not know yet if there will be a fgChooseVisual() function for Win32
257 case GLUT_DISPLAY_MODE_POSSIBLE:
259 * Check if the current display mode is possible
261 return( fgChooseVisual() == NULL ? 0 : 1 );
264 * This is system-dependant
266 case GLUT_WINDOW_FORMAT_ID:
268 * Return the visual ID, if there is a current window naturally:
270 if( fgStructure.Window == NULL )
273 return( fgStructure.Window->Window.VisualInfo->visualid );
275 #elif TARGET_HOST_WIN32
278 * Handle the OpenGL inquiries
280 case GLUT_WINDOW_RGBA:
281 glGetBooleanv ( GL_RGBA_MODE, &boolValue ) ; /* True if color buffers store RGBA */
282 returnValue = boolValue ? 1 : 0 ;
283 return ( returnValue ) ;
284 case GLUT_WINDOW_DOUBLEBUFFER:
285 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue ) ; /* True if front and back buffers exist */
286 returnValue = boolValue ? 1 : 0 ;
287 return ( returnValue ) ;
288 case GLUT_WINDOW_STEREO:
289 glGetBooleanv ( GL_STEREO, &boolValue ) ; /* True if left and right buffers exist */
290 returnValue = boolValue ? 1 : 0 ;
291 return ( returnValue ) ;
293 case GLUT_WINDOW_RED_SIZE:
294 glGetIntegerv ( GL_RED_BITS, &returnValue ) ; /* Number of bits per red component in color buffers */
295 return ( returnValue ) ;
296 case GLUT_WINDOW_GREEN_SIZE:
297 glGetIntegerv ( GL_GREEN_BITS, &returnValue ) ; /* Number of bits per green component in color buffers */
298 return ( returnValue ) ;
299 case GLUT_WINDOW_BLUE_SIZE:
300 glGetIntegerv ( GL_BLUE_BITS, &returnValue ) ; /* Number of bits per blue component in color buffers */
301 return ( returnValue ) ;
302 case GLUT_WINDOW_ALPHA_SIZE:
303 glGetIntegerv ( GL_ALPHA_BITS, &returnValue ) ; /* Number of bits per alpha component in color buffers */
304 return ( returnValue ) ;
305 case GLUT_WINDOW_ACCUM_RED_SIZE:
306 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue ) ; /* Number of bits per red component in the accumulation buffer */
307 return ( returnValue ) ;
308 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
309 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue ) ; /* Number of bits per green component in the accumulation buffer */
310 return ( returnValue ) ;
311 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
312 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue ) ; /* Number of bits per blue component in the accumulation buffer */
313 return ( returnValue ) ;
314 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
315 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue ) ; /* Number of bits per alpha component in the accumulation buffer */
316 return ( returnValue ) ;
317 case GLUT_WINDOW_DEPTH_SIZE:
318 glGetIntegerv ( GL_DEPTH_BITS, &returnValue ) ; /* Number of depth-buffer bitplanes */
319 return ( returnValue ) ;
321 case GLUT_WINDOW_BUFFER_SIZE:
322 returnValue = 1 ; /* ????? */
323 return ( returnValue ) ;
324 case GLUT_WINDOW_STENCIL_SIZE:
325 returnValue = 0 ; /* ????? */
326 return ( returnValue ) ;
329 * Window position and size
333 case GLUT_WINDOW_WIDTH:
334 case GLUT_WINDOW_HEIGHT:
337 * There is considerable confusion about the "right thing to do" concerning window
338 * size and position. GLUT itself is not consistent between Windows and Linux; since
339 * platform independence is a virtue for "freeglut", we decided to break with GLUT's
341 * Under Linux, it is apparently not possible to get the window border sizes in order
342 * to subtract them off the window's initial position until some time after the window
343 * has been created. Therefore we decided on the following behaviour, both under
344 * Windows and under Linux:
345 * - When you create a window with position (x,y) and size (w,h), the upper left hand
346 * corner of the outside of the window is at (x,y) and the size of the drawable area
348 * - When you query the size and position of the window--as is happening here for
349 * Windows--"freeglut" will return the size of the drawable area--the (w,h) that you
350 * specified when you created the window--and the coordinates of the upper left hand
351 * corner of the drawable area--which is NOT the (x,y) you specified.
357 * Check if there is a window to be queried for dimensions:
359 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
362 * We need to call GetWindowRect() first...
363 * (this returns the pixel coordinates of the outside of the window)
365 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
368 * ...then we've got to correct the results we've just received...
370 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
371 ( ! fgStructure.Window->IsMenu ) )
373 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
374 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
375 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
376 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
380 * ...and finally return the caller the desired value:
384 case GLUT_WINDOW_X: return( winRect.left );
385 case GLUT_WINDOW_Y: return( winRect.top );
386 case GLUT_WINDOW_WIDTH: return( winRect.right - winRect.left );
387 case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
392 case GLUT_WINDOW_BORDER_WIDTH :
393 return ( GetSystemMetrics( SM_CXSIZEFRAME ) ) ;
395 case GLUT_WINDOW_HEADER_HEIGHT :
396 return ( GetSystemMetrics( SM_CYCAPTION ) ) ;
398 case GLUT_DISPLAY_MODE_POSSIBLE:
400 * Check if the current display mode is possible
402 return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_MAIN_PLANE ) );
404 case GLUT_WINDOW_FORMAT_ID:
406 * Return the pixel format of the current window
408 if( fgStructure.Window != NULL )
409 return( GetPixelFormat( fgStructure.Window->Window.Device ) );
412 * If the current window does not exist, fail:
419 * The window structure queries
421 case GLUT_WINDOW_PARENT:
423 * Return the ID number of current window's parent, if any
425 if( fgStructure.Window == NULL ) return( 0 );
426 if( fgStructure.Window->Parent == NULL ) return( 0 );
428 return( fgStructure.Window->Parent->ID );
430 case GLUT_WINDOW_NUM_CHILDREN:
432 * Return the number of children attached to the current window
434 if( fgStructure.Window == NULL )
437 return( fgListLength( &fgStructure.Window->Children ) );
439 case GLUT_WINDOW_CURSOR:
441 * Return the currently selected window cursor
443 if( fgStructure.Window == NULL )
446 return( fgStructure.Window->State.Cursor );
448 case GLUT_MENU_NUM_ITEMS:
450 * Return the number of menu entries in the current menu
452 if( fgStructure.Menu == NULL )
455 return( fgListLength( &fgStructure.Menu->Entries ) );
457 case GLUT_ACTION_ON_WINDOW_CLOSE:
458 return fgState.ActionOnWindowClose ;
461 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH ;
463 case GLUT_RENDERING_CONTEXT:
464 return ( fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT : GLUT_CREATE_NEW_CONTEXT ) ;
468 * Just have it reported, so that we can see what needs to be implemented
470 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
475 * If nothing happens, then we are in deep trouble...
481 * Returns various device information.
483 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
485 freeglut_assert_ready;
488 * See why are we bothered...
490 * WARNING: we are mostly lying in this function.
494 case GLUT_HAS_KEYBOARD:
496 * We always have a keyboard present on PC machines...
500 #if TARGET_HOST_UNIX_X11
504 * Hey, my Atari 65XE hasn't had a mouse!
508 case GLUT_NUM_MOUSE_BUTTONS:
510 * Return the number of mouse buttons available. This is a big guess.
514 #elif TARGET_HOST_WIN32
518 * The Windows can be booted without a mouse.
519 * It would be nice to have this reported.
521 return( GetSystemMetrics( SM_MOUSEPRESENT ) );
523 case GLUT_NUM_MOUSE_BUTTONS:
525 * We are much more fortunate under Win32 about this...
527 return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
531 case GLUT_JOYSTICK_POLL_RATE:
532 case GLUT_HAS_JOYSTICK:
533 case GLUT_JOYSTICK_BUTTONS:
534 case GLUT_JOYSTICK_AXES:
536 * WARNING: THIS IS A BIG LIE!
540 case GLUT_HAS_SPACEBALL:
541 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
542 case GLUT_HAS_TABLET:
544 * Sounds cool. And unuseful.
548 case GLUT_NUM_SPACEBALL_BUTTONS:
549 case GLUT_NUM_BUTTON_BOX_BUTTONS:
551 case GLUT_NUM_TABLET_BUTTONS:
553 * Zero is not the answer. Zero is the question. Continuum is the answer.
557 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
559 * Return what we think about the key auto repeat settings
561 return( fgState.IgnoreKeyRepeat );
563 case GLUT_DEVICE_KEY_REPEAT:
565 * WARNING: THIS IS A BIG LIE!
567 return( GLUT_KEY_REPEAT_DEFAULT );
573 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
578 * And now -- the failure.
584 * This should return the current state of ALT, SHIFT and CTRL keys.
586 int FGAPIENTRY glutGetModifiers( void )
589 * Fail if there is no current window or called outside an input callback
591 if( fgStructure.Window == NULL )
594 if( fgStructure.Window->State.Modifiers == 0xffffffff )
596 fgWarning( "glutGetModifiers() called outside an input callback" );
601 * Return the current modifiers state otherwise
603 return( fgStructure.Window->State.Modifiers );
607 * Return the state of the GLUT API overlay subsystem. A misery ;-)
609 int FGAPIENTRY glutLayerGet( GLenum eWhat )
611 freeglut_assert_ready;
614 * This is easy as layers are not implemented ;-)
619 #if TARGET_HOST_UNIX_X11
621 case GLUT_OVERLAY_POSSIBLE:
623 * Nope, overlays are not possible.
627 case GLUT_LAYER_IN_USE:
629 * The normal plane is always in use
631 return( GLUT_NORMAL );
633 case GLUT_HAS_OVERLAY:
635 * No window is allowed to have an overlay
639 case GLUT_TRANSPARENT_INDEX:
641 * Return just anything, which is always defined as zero
645 case GLUT_NORMAL_DAMAGED:
647 * Actually I do not know. Maybe.
651 case GLUT_OVERLAY_DAMAGED:
653 * Return minus one to mark that no layer is in use
657 #elif TARGET_HOST_WIN32
659 case GLUT_OVERLAY_POSSIBLE:
661 * Check if an overlay display mode is possible
663 /* return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_OVERLAY_PLANE ) ); */
666 case GLUT_LAYER_IN_USE:
668 * The normal plane is always in use
670 return( GLUT_NORMAL );
672 case GLUT_HAS_OVERLAY:
674 * No window is allowed to have an overlay
678 case GLUT_TRANSPARENT_INDEX:
680 * Return just anything, which is always defined as zero
684 case GLUT_NORMAL_DAMAGED:
686 * Actually I do not know. Maybe.
690 case GLUT_OVERLAY_DAMAGED:
692 * Return minus one to mark that no layer is in use
699 * Complain to the user about the obvious bug
701 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
706 * And fail. That's good. Programs do love failing.
711 /*** END OF FILE ***/