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 "../include/GL/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 ---------------------------------------------------- */
51 * Those are definitions introduced to make the glutGet() more easy,
52 * however they might introduce errors if someone ports GLX to Win32 :)
54 * Btw. this is not that a bad idea (wrapping WGL around GLX)...
57 # define GLX_RGBA 0x01
58 # define GLX_DOUBLEBUFFER 0x02
59 # define GLX_BUFFER_SIZE 0x03
60 # define GLX_STENCIL_SIZE 0x04
61 # define GLX_DEPTH_SIZE 0x05
62 # define GLX_RED_SIZE 0x06
63 # define GLX_GREEN_SIZE 0x07
64 # define GLX_BLUE_SIZE 0x08
65 # define GLX_ALPHA_SIZE 0x09
66 # define GLX_ACCUM_RED_SIZE 0x0A
67 # define GLX_ACCUM_GREEN_SIZE 0x0B
68 # define GLX_ACCUM_BLUE_SIZE 0x0C
69 # define GLX_ACCUM_ALPHA_SIZE 0x0D
70 # define GLX_STEREO 0x0E
74 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
77 * Queries the GL context about some attributes
79 static gint fghGetConfig( gint attribute )
84 * Return nothing if there is no current window set
86 if( fgStructure.Window == NULL )
89 #if TARGET_HOST_UNIX_X11
91 * glXGetConfig should work fine
93 glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo, attribute, &returnValue );
95 #elif TARGET_HOST_WIN32
97 * This is going to be a bit harder than the X11 version...
99 # pragma message( "freeglut_state.c::fghGetConfig() says hello world my name is not implemented!" )
103 case GLX_DOUBLEBUFFER:
104 case GLX_BUFFER_SIZE:
111 case GLX_STENCIL_SIZE:
113 case GLX_ACCUM_RED_SIZE:
114 case GLX_ACCUM_GREEN_SIZE:
115 case GLX_ACCUM_BLUE_SIZE:
116 case GLX_ACCUM_ALPHA_SIZE:
120 * Well, this looks like not implemented to me :)
128 * Have the query results returned
130 return( returnValue );
133 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
136 * General settings query method
138 int FGAPIENTRY glutGet( GLenum eWhat )
142 freeglut_assert_ready;
145 * Check what is the caller querying for. In chronological code add order.
149 case GLUT_ELAPSED_TIME:
151 * This is easy and nicely portable, as we are using GLib...
153 return( (int) (g_timer_elapsed( fgState.Timer, NULL ) * 1000.0) );
156 * Following values are stored in fgState and fgDisplay global structures
158 case GLUT_SCREEN_WIDTH: return( fgDisplay.ScreenWidth );
159 case GLUT_SCREEN_HEIGHT: return( fgDisplay.ScreenHeight );
160 case GLUT_SCREEN_WIDTH_MM: return( fgDisplay.ScreenWidthMM );
161 case GLUT_SCREEN_HEIGHT_MM: return( fgDisplay.ScreenHeightMM );
162 case GLUT_INIT_WINDOW_X: return( fgState.Position.X );
163 case GLUT_INIT_WINDOW_Y: return( fgState.Position.Y );
164 case GLUT_INIT_WINDOW_WIDTH: return( fgState.Size.X );
165 case GLUT_INIT_WINDOW_HEIGHT: return( fgState.Size.Y );
166 case GLUT_INIT_DISPLAY_MODE: return( fgState.DisplayMode );
169 * The window/context specific queries are handled mostly by fghGetConfig().
171 case GLUT_WINDOW_NUM_SAMPLES:
173 * Multisampling. Return what I know about multisampling.
178 * The rest of GLX queries is general enough to use a macro to check them
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 );
199 #if TARGET_HOST_UNIX_X11
201 * Colormap size is handled in a bit different way than all the rest
203 case GLUT_WINDOW_COLORMAP_SIZE:
205 * Check for the visual type
207 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
210 * We've got a RGBA visual, so there is no colormap at all.
211 * The other possibility is that we have no current window set.
217 * Otherwise return the number of entries in the colormap
219 return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
222 * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
227 XWindowAttributes winAttributes;
228 Window another, window;
232 * Return zero if there is no current window set
234 if( fgStructure.Window == NULL )
238 * So, grab the current window's position
240 window = fgStructure.Window->Window.Handle;
243 * Grab the current window's attributes now
245 XGetWindowAttributes(
252 * Correct the results for the parental relation and border size
254 XTranslateCoordinates(
258 -winAttributes.border_width,
259 -winAttributes.border_width,
265 * See if we have to return the X or Y coordinate
267 return( eWhat == GLUT_WINDOW_X ? x : y );
270 case GLUT_WINDOW_WIDTH:
271 case GLUT_WINDOW_HEIGHT:
273 XWindowAttributes winAttributes;
276 * Return zero if there is no current window set
278 if( fgStructure.Window == NULL )
282 * Checking for window's size is much easier:
284 XGetWindowAttributes(
286 fgStructure.Window->Window.Handle,
291 * See if to return the window's width or height
293 return( eWhat == GLUT_WINDOW_WIDTH ? winAttributes.width : winAttributes.height );
297 * I do not know yet if there will be a fgChooseVisual() function for Win32
299 case GLUT_DISPLAY_MODE_POSSIBLE:
301 * Check if the current display mode is possible
303 return( fgChooseVisual() == NULL ? 0 : 1 );
306 * This is system-dependant
308 case GLUT_WINDOW_FORMAT_ID:
310 * Return the visual ID, if there is a current window naturally:
312 if( fgStructure.Window == NULL )
315 return( fgStructure.Window->Window.VisualInfo->visualid );
317 #elif TARGET_HOST_WIN32
321 case GLUT_WINDOW_WIDTH:
322 case GLUT_WINDOW_HEIGHT:
327 * Check if there is a window to be queried for dimensions:
329 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
332 * We need to call GetWindowRect() first...
334 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
337 * ...then we've got to correct the results we've just received...
339 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
340 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
341 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) - 1 + GetSystemMetrics( SM_CYCAPTION );
342 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME ) + 1;
345 * ...and finally return the caller the desired value:
349 case GLUT_WINDOW_X: return( winRect.left );
350 case GLUT_WINDOW_Y: return( winRect.top );
351 case GLUT_WINDOW_WIDTH: return( winRect.right - winRect.left );
352 case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
357 case GLUT_DISPLAY_MODE_POSSIBLE:
359 * Check if the current display mode is possible
361 return( fgSetupPixelFormat( fgStructure.Window, TRUE ) );
363 case GLUT_WINDOW_FORMAT_ID:
365 * Return the pixel format of the current window
367 if( fgStructure.Window != NULL )
368 return( GetPixelFormat( fgStructure.Window->Window.Device ) );
371 * If the current window does not exist, fail:
378 * The window structure queries
380 case GLUT_WINDOW_PARENT:
382 * Return the ID number of current window's parent, if any
384 if( fgStructure.Window == NULL ) return( 0 );
385 if( fgStructure.Window->Parent == NULL ) return( 0 );
387 return( fgStructure.Window->Parent->ID );
389 case GLUT_WINDOW_NUM_CHILDREN:
391 * Return the number of children attached to the current window
393 if( fgStructure.Window == NULL )
396 return( g_list_length( fgStructure.Window->Children ) );
398 case GLUT_WINDOW_CURSOR:
400 * Return the currently selected window cursor
402 if( fgStructure.Window == NULL )
405 return( fgStructure.Window->State.Cursor );
407 case GLUT_MENU_NUM_ITEMS:
409 * Return the number of menu entries in the current menu
411 if( fgStructure.Menu == NULL )
414 return( g_list_length( fgStructure.Menu->Entries ) );
418 * Just have it reported, so that we can see what needs to be implemented
420 g_warning( "glutGet(): missing enum handle %i\n", eWhat );
425 * If nothing happens, then we are in deep trouble...
427 g_assert_not_reached();
431 * Returns various device information.
433 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
435 freeglut_assert_ready;
438 * See why are we bothered...
440 * WARNING: we are mostly lying in this function.
444 case GLUT_HAS_KEYBOARD:
446 * We always have a keyboard present on PC machines...
450 #if TARGET_HOST_UNIX_X11
454 * Hey, my Atari 65XE hasn't had a mouse!
458 case GLUT_NUM_MOUSE_BUTTONS:
460 * Return the number of mouse buttons available. This is a big guess.
464 #elif TARGET_HOST_WIN32
468 * The Windows can be booted without a mouse.
469 * It would be nice to have this reported.
471 return( GetSystemMetrics( SM_MOUSEPRESENT ) );
473 case GLUT_NUM_MOUSE_BUTTONS:
475 * We are much more fortunate under Win32 about this...
477 return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
481 case GLUT_JOYSTICK_POLL_RATE:
482 case GLUT_HAS_JOYSTICK:
483 case GLUT_JOYSTICK_BUTTONS:
484 case GLUT_JOYSTICK_AXES:
486 * WARNING: THIS IS A BIG LIE!
490 case GLUT_HAS_SPACEBALL:
491 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
492 case GLUT_HAS_TABLET:
494 * Sounds cool. And unuseful.
498 case GLUT_NUM_SPACEBALL_BUTTONS:
499 case GLUT_NUM_BUTTON_BOX_BUTTONS:
501 case GLUT_NUM_TABLET_BUTTONS:
503 * Zero is not the answer. Zero is the question. Continuum is the answer.
507 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
509 * Return what we think about the key auto repeat settings
511 return( fgState.IgnoreKeyRepeat );
513 case GLUT_DEVICE_KEY_REPEAT:
515 * WARNING: THIS IS A BIG LIE!
517 return( GLUT_KEY_REPEAT_DEFAULT );
523 g_warning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
528 * And now -- the failure.
530 g_assert_not_reached();
534 * This should return the current state of ALT, SHIFT and CTRL keys.
536 int FGAPIENTRY glutGetModifiers( void )
539 * Fail if there is no current window or called outside an input callback
541 if( fgStructure.Window == NULL )
544 if( fgStructure.Window->State.Modifiers == 0xffffffff )
546 g_warning( "glutGetModifiers() called outside an input callback" );
551 * Return the current modifiers state otherwise
553 return( fgStructure.Window->State.Modifiers );
557 * Return the state of the GLUT API overlay subsystem. A misery ;-)
559 int FGAPIENTRY glutLayerGet( GLenum eWhat )
561 freeglut_assert_ready;
564 * This is easy as layers are not implemented ;-)
568 case GLUT_OVERLAY_POSSIBLE:
570 * Nope, overlays are not possible.
574 case GLUT_LAYER_IN_USE:
576 * The normal plane is always in use
578 return( GLUT_NORMAL );
580 case GLUT_HAS_OVERLAY:
582 * No window is allowed to have an overlay
586 case GLUT_TRANSPARENT_INDEX:
588 * Return just anything, which is always defined as zero
592 case GLUT_NORMAL_DAMAGED:
594 * Actually I do not know. Maybe.
598 case GLUT_OVERLAY_DAMAGED:
600 * Return minus one to mark that no layer is in use
606 * Complain to the user about the obvious bug
608 g_warning( "glutLayerGet(): missing enum handle %i\n", eWhat );
613 * And fail. That's good. Programs do love failing.
615 g_assert_not_reached();
618 /*** END OF FILE ***/