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 * Check what is the caller querying for. In chronological code add 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;
120 * Just have it reported, so that we can see what needs to be implemented
122 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
128 * General settings query method
130 int FGAPIENTRY glutGet( GLenum eWhat )
133 GLboolean boolValue ;
135 if ( eWhat == GLUT_INIT_STATE )
136 return ( fgState.Time.Set ) ;
138 freeglut_assert_ready;
141 * Check what is the caller querying for. In chronological code add order.
145 case GLUT_ELAPSED_TIME:
147 * This is easy and nicely portable, as we are using GLib...
149 return( fgElapsedTime() );
152 * Following values are stored in fgState and fgDisplay global structures
154 case GLUT_SCREEN_WIDTH: return( fgDisplay.ScreenWidth );
155 case GLUT_SCREEN_HEIGHT: return( fgDisplay.ScreenHeight );
156 case GLUT_SCREEN_WIDTH_MM: return( fgDisplay.ScreenWidthMM );
157 case GLUT_SCREEN_HEIGHT_MM: return( fgDisplay.ScreenHeightMM );
158 case GLUT_INIT_WINDOW_X: return( fgState.Position.X );
159 case GLUT_INIT_WINDOW_Y: return( fgState.Position.Y );
160 case GLUT_INIT_WINDOW_WIDTH: return( fgState.Size.X );
161 case GLUT_INIT_WINDOW_HEIGHT: return( fgState.Size.Y );
162 case GLUT_INIT_DISPLAY_MODE: return( fgState.DisplayMode );
165 * The window/context specific queries are handled mostly by fghGetConfig().
167 case GLUT_WINDOW_NUM_SAMPLES:
169 * Multisampling. Return what I know about multisampling.
173 #if TARGET_HOST_UNIX_X11
175 * The rest of GLX queries under X are general enough to use a macro to check them
177 # define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
179 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
180 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
181 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
182 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
183 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
184 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
185 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
186 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
187 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
188 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
189 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
190 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
191 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
192 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
197 * Colormap size is handled in a bit different way than all the rest
199 case GLUT_WINDOW_COLORMAP_SIZE:
201 * Check for the visual type
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.
213 * Otherwise return the number of entries in the colormap
215 return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
218 * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
222 case GLUT_WINDOW_BORDER_WIDTH:
223 case GLUT_WINDOW_HEADER_HEIGHT:
228 if( fgStructure.Window == NULL )
231 XTranslateCoordinates(
233 fgStructure.Window->Window.Handle,
234 fgDisplay.RootWindow,
239 case GLUT_WINDOW_X: return x;
240 case GLUT_WINDOW_Y: return y;
244 return( 0 ); /* Just in case */
246 XTranslateCoordinates(
248 fgStructure.Window->Window.Handle,
249 w, 0, 0, &x, &y, &w);
253 case GLUT_WINDOW_BORDER_WIDTH: return x;
254 case GLUT_WINDOW_HEADER_HEIGHT: return y;
258 case GLUT_WINDOW_WIDTH:
259 case GLUT_WINDOW_HEIGHT:
261 XWindowAttributes winAttributes;
264 * Return zero if there is no current window set
266 if( fgStructure.Window == NULL )
270 * Grab the current window's attributes now
272 XGetWindowAttributes(
274 fgStructure.Window->Window.Handle,
279 * See which window attribute to return
283 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
284 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
289 * I do not know yet if there will be a fgChooseVisual() function for Win32
291 case GLUT_DISPLAY_MODE_POSSIBLE:
293 * Check if the current display mode is possible
295 return( fgChooseVisual() == NULL ? 0 : 1 );
298 * This is system-dependant
300 case GLUT_WINDOW_FORMAT_ID:
302 * Return the visual ID, if there is a current window naturally:
304 if( fgStructure.Window == NULL )
307 return( fgStructure.Window->Window.VisualInfo->visualid );
309 #elif TARGET_HOST_WIN32
312 * Handle the OpenGL inquiries
314 case GLUT_WINDOW_RGBA:
315 glGetBooleanv ( GL_RGBA_MODE, &boolValue ) ; /* True if color buffers store RGBA */
316 returnValue = boolValue ? 1 : 0 ;
317 return ( returnValue ) ;
318 case GLUT_WINDOW_DOUBLEBUFFER:
319 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue ) ; /* True if front and back buffers exist */
320 returnValue = boolValue ? 1 : 0 ;
321 return ( returnValue ) ;
322 case GLUT_WINDOW_STEREO:
323 glGetBooleanv ( GL_STEREO, &boolValue ) ; /* True if left and right buffers exist */
324 returnValue = boolValue ? 1 : 0 ;
325 return ( returnValue ) ;
327 case GLUT_WINDOW_RED_SIZE:
328 glGetIntegerv ( GL_RED_BITS, &returnValue ) ; /* Number of bits per red component in color buffers */
329 return ( returnValue ) ;
330 case GLUT_WINDOW_GREEN_SIZE:
331 glGetIntegerv ( GL_GREEN_BITS, &returnValue ) ; /* Number of bits per green component in color buffers */
332 return ( returnValue ) ;
333 case GLUT_WINDOW_BLUE_SIZE:
334 glGetIntegerv ( GL_BLUE_BITS, &returnValue ) ; /* Number of bits per blue component in color buffers */
335 return ( returnValue ) ;
336 case GLUT_WINDOW_ALPHA_SIZE:
337 glGetIntegerv ( GL_ALPHA_BITS, &returnValue ) ; /* Number of bits per alpha component in color buffers */
338 return ( returnValue ) ;
339 case GLUT_WINDOW_ACCUM_RED_SIZE:
340 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue ) ; /* Number of bits per red component in the accumulation buffer */
341 return ( returnValue ) ;
342 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
343 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue ) ; /* Number of bits per green component in the accumulation buffer */
344 return ( returnValue ) ;
345 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
346 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue ) ; /* Number of bits per blue component in the accumulation buffer */
347 return ( returnValue ) ;
348 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
349 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue ) ; /* Number of bits per alpha component in the accumulation buffer */
350 return ( returnValue ) ;
351 case GLUT_WINDOW_DEPTH_SIZE:
352 glGetIntegerv ( GL_DEPTH_BITS, &returnValue ) ; /* Number of depth-buffer bitplanes */
353 return ( returnValue ) ;
355 case GLUT_WINDOW_BUFFER_SIZE:
356 returnValue = 1 ; /* ????? */
357 return ( returnValue ) ;
358 case GLUT_WINDOW_STENCIL_SIZE:
359 returnValue = 0 ; /* ????? */
360 return ( returnValue ) ;
363 * Window position and size
367 case GLUT_WINDOW_WIDTH:
368 case GLUT_WINDOW_HEIGHT:
371 * There is considerable confusion about the "right thing to do" concerning window
372 * size and position. GLUT itself is not consistent between Windows and Linux; since
373 * platform independence is a virtue for "freeglut", we decided to break with GLUT's
375 * Under Linux, it is apparently not possible to get the window border sizes in order
376 * to subtract them off the window's initial position until some time after the window
377 * has been created. Therefore we decided on the following behaviour, both under
378 * Windows and under Linux:
379 * - When you create a window with position (x,y) and size (w,h), the upper left hand
380 * corner of the outside of the window is at (x,y) and the size of the drawable area
382 * - When you query the size and position of the window--as is happening here for
383 * Windows--"freeglut" will return the size of the drawable area--the (w,h) that you
384 * specified when you created the window--and the coordinates of the upper left hand
385 * corner of the drawable area--which is NOT the (x,y) you specified.
391 * Check if there is a window to be queried for dimensions:
393 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
396 * We need to call GetWindowRect() first...
397 * (this returns the pixel coordinates of the outside of the window)
399 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
402 * ...then we've got to correct the results we've just received...
404 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
405 ( ! fgStructure.Window->IsMenu ) )
407 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
408 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
409 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
410 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
414 * ...and finally return the caller the desired value:
418 case GLUT_WINDOW_X: return( winRect.left );
419 case GLUT_WINDOW_Y: return( winRect.top );
420 case GLUT_WINDOW_WIDTH: return( winRect.right - winRect.left );
421 case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
426 case GLUT_WINDOW_BORDER_WIDTH :
427 return ( GetSystemMetrics( SM_CXSIZEFRAME ) ) ;
429 case GLUT_WINDOW_HEADER_HEIGHT :
430 return ( GetSystemMetrics( SM_CYCAPTION ) ) ;
432 case GLUT_DISPLAY_MODE_POSSIBLE:
434 * Check if the current display mode is possible
436 return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_MAIN_PLANE ) );
438 case GLUT_WINDOW_FORMAT_ID:
440 * Return the pixel format of the current window
442 if( fgStructure.Window != NULL )
443 return( GetPixelFormat( fgStructure.Window->Window.Device ) );
446 * If the current window does not exist, fail:
453 * The window structure queries
455 case GLUT_WINDOW_PARENT:
457 * Return the ID number of current window's parent, if any
459 if( fgStructure.Window == NULL ) return( 0 );
460 if( fgStructure.Window->Parent == NULL ) return( 0 );
462 return( fgStructure.Window->Parent->ID );
464 case GLUT_WINDOW_NUM_CHILDREN:
466 * Return the number of children attached to the current window
468 if( fgStructure.Window == NULL )
471 return( fgListLength( &fgStructure.Window->Children ) );
473 case GLUT_WINDOW_CURSOR:
475 * Return the currently selected window cursor
477 if( fgStructure.Window == NULL )
480 return( fgStructure.Window->State.Cursor );
482 case GLUT_MENU_NUM_ITEMS:
484 * Return the number of menu entries in the current menu
486 if( fgStructure.Menu == NULL )
489 return( fgListLength( &fgStructure.Menu->Entries ) );
491 case GLUT_ACTION_ON_WINDOW_CLOSE:
492 return fgState.ActionOnWindowClose ;
495 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH ;
497 case GLUT_RENDERING_CONTEXT:
498 return ( fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT : GLUT_CREATE_NEW_CONTEXT ) ;
502 * Just have it reported, so that we can see what needs to be implemented
504 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
509 * If nothing happens, then we are in deep trouble...
515 * Returns various device information.
517 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
519 freeglut_assert_ready;
522 * See why are we bothered...
524 * WARNING: we are mostly lying in this function.
528 case GLUT_HAS_KEYBOARD:
530 * We always have a keyboard present on PC machines...
534 #if TARGET_HOST_UNIX_X11
538 * Hey, my Atari 65XE hasn't had a mouse!
542 case GLUT_NUM_MOUSE_BUTTONS:
544 * Return the number of mouse buttons available. This is a big guess.
548 #elif TARGET_HOST_WIN32
552 * The Windows can be booted without a mouse.
553 * It would be nice to have this reported.
555 return( GetSystemMetrics( SM_MOUSEPRESENT ) );
557 case GLUT_NUM_MOUSE_BUTTONS:
559 * We are much more fortunate under Win32 about this...
561 return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
565 case GLUT_JOYSTICK_POLL_RATE:
566 case GLUT_HAS_JOYSTICK:
567 case GLUT_JOYSTICK_BUTTONS:
568 case GLUT_JOYSTICK_AXES:
570 * WARNING: THIS IS A BIG LIE!
574 case GLUT_HAS_SPACEBALL:
575 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
576 case GLUT_HAS_TABLET:
578 * Sounds cool. And unuseful.
582 case GLUT_NUM_SPACEBALL_BUTTONS:
583 case GLUT_NUM_BUTTON_BOX_BUTTONS:
585 case GLUT_NUM_TABLET_BUTTONS:
587 * Zero is not the answer. Zero is the question. Continuum is the answer.
591 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
593 * Return what we think about the key auto repeat settings
595 return( fgState.IgnoreKeyRepeat );
597 case GLUT_DEVICE_KEY_REPEAT:
599 * WARNING: THIS IS A BIG LIE!
601 return( GLUT_KEY_REPEAT_DEFAULT );
607 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
612 * And now -- the failure.
618 * This should return the current state of ALT, SHIFT and CTRL keys.
620 int FGAPIENTRY glutGetModifiers( void )
623 * Fail if there is no current window or called outside an input callback
625 if( fgStructure.Window == NULL )
628 if( fgStructure.Window->State.Modifiers == 0xffffffff )
630 fgWarning( "glutGetModifiers() called outside an input callback" );
635 * Return the current modifiers state otherwise
637 return( fgStructure.Window->State.Modifiers );
641 * Return the state of the GLUT API overlay subsystem. A misery ;-)
643 int FGAPIENTRY glutLayerGet( GLenum eWhat )
645 freeglut_assert_ready;
648 * This is easy as layers are not implemented ;-)
653 #if TARGET_HOST_UNIX_X11
655 case GLUT_OVERLAY_POSSIBLE:
657 * Nope, overlays are not possible.
661 case GLUT_LAYER_IN_USE:
663 * The normal plane is always in use
665 return( GLUT_NORMAL );
667 case GLUT_HAS_OVERLAY:
669 * No window is allowed to have an overlay
673 case GLUT_TRANSPARENT_INDEX:
675 * Return just anything, which is always defined as zero
679 case GLUT_NORMAL_DAMAGED:
681 * Actually I do not know. Maybe.
685 case GLUT_OVERLAY_DAMAGED:
687 * Return minus one to mark that no layer is in use
691 #elif TARGET_HOST_WIN32
693 case GLUT_OVERLAY_POSSIBLE:
695 * Check if an overlay display mode is possible
697 /* return( fgSetupPixelFormat( fgStructure.Window, TRUE, PFD_OVERLAY_PLANE ) ); */
700 case GLUT_LAYER_IN_USE:
702 * The normal plane is always in use
704 return( GLUT_NORMAL );
706 case GLUT_HAS_OVERLAY:
708 * No window is allowed to have an overlay
712 case GLUT_TRANSPARENT_INDEX:
714 * Return just anything, which is always defined as zero
718 case GLUT_NORMAL_DAMAGED:
720 * Actually I do not know. Maybe.
724 case GLUT_OVERLAY_DAMAGED:
726 * Return minus one to mark that no layer is in use
733 * Complain to the user about the obvious bug
735 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
740 * And fail. That's good. Programs do love failing.
745 /*** END OF FILE ***/