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 <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_DIRECT_RENDERING:
112 fgState.DirectContext = value;
115 case GLUT_WINDOW_CURSOR:
116 if( fgStructure.Window != NULL )
117 fgStructure.Window->State.Cursor = value;
121 fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
127 * General settings query method
129 int FGAPIENTRY glutGet( GLenum eWhat )
131 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
133 GLboolean boolValue ;
138 case GLUT_INIT_STATE:
139 return fgState.Initialised;
141 case GLUT_ELAPSED_TIME:
142 return fgElapsedTime();
145 freeglut_assert_ready;
147 /* XXX In chronological code add order. (WHY in that order?) */
150 /* Following values are stored in fgState and fgDisplay global structures */
151 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
152 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
153 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
154 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
155 case GLUT_INIT_WINDOW_X: return fgState.Position.X ;
156 case GLUT_INIT_WINDOW_Y: return fgState.Position.Y ;
157 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.X ;
158 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Y ;
159 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
162 * The window/context specific queries are handled mostly by
165 case GLUT_WINDOW_NUM_SAMPLES:
166 /* 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 );
193 /* Colormap size is handled in a bit different way than all the rest */
194 case GLUT_WINDOW_COLORMAP_SIZE:
195 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
198 * We've got a RGBA visual, so there is no colormap at all.
199 * The other possibility is that we have no current window set.
203 return fgStructure.Window->Window.VisualInfo->visual->map_entries;
206 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
211 case GLUT_WINDOW_BORDER_WIDTH:
212 case GLUT_WINDOW_HEADER_HEIGHT:
217 if( fgStructure.Window == NULL )
220 XTranslateCoordinates(
222 fgStructure.Window->Window.Handle,
223 fgDisplay.RootWindow,
228 case GLUT_WINDOW_X: return x;
229 case GLUT_WINDOW_Y: return y;
234 XTranslateCoordinates(
236 fgStructure.Window->Window.Handle,
237 w, 0, 0, &x, &y, &w);
241 case GLUT_WINDOW_BORDER_WIDTH: return x;
242 case GLUT_WINDOW_HEADER_HEIGHT: return y;
246 case GLUT_WINDOW_WIDTH:
247 case GLUT_WINDOW_HEIGHT:
249 XWindowAttributes winAttributes;
251 if( fgStructure.Window == NULL )
253 XGetWindowAttributes(
255 fgStructure.Window->Window.Handle,
260 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
261 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
265 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
266 case GLUT_DISPLAY_MODE_POSSIBLE:
267 return( fgChooseVisual() == NULL ? 0 : 1 );
269 /* This is system-dependant */
270 case GLUT_WINDOW_FORMAT_ID:
271 if( fgStructure.Window == NULL )
274 return fgStructure.Window->Window.VisualInfo->visualid;
276 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
278 /* Handle the OpenGL inquiries */
279 case GLUT_WINDOW_RGBA:
280 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
281 returnValue = boolValue ? 1 : 0;
283 case GLUT_WINDOW_DOUBLEBUFFER:
284 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
285 returnValue = boolValue ? 1 : 0;
287 case GLUT_WINDOW_STEREO:
288 glGetBooleanv ( GL_STEREO, &boolValue );
289 returnValue = boolValue ? 1 : 0;
292 case GLUT_WINDOW_RED_SIZE:
293 glGetIntegerv ( GL_RED_BITS, &returnValue );
295 case GLUT_WINDOW_GREEN_SIZE:
296 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
298 case GLUT_WINDOW_BLUE_SIZE:
299 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
301 case GLUT_WINDOW_ALPHA_SIZE:
302 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
304 case GLUT_WINDOW_ACCUM_RED_SIZE:
305 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
307 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
308 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
310 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
311 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
313 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
314 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
316 case GLUT_WINDOW_DEPTH_SIZE:
317 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
320 case GLUT_WINDOW_BUFFER_SIZE:
321 returnValue = 1 ; /* ????? */
323 case GLUT_WINDOW_STENCIL_SIZE:
324 returnValue = 0 ; /* ????? */
329 case GLUT_WINDOW_WIDTH:
330 case GLUT_WINDOW_HEIGHT:
333 * There is considerable confusion about the "right thing to
334 * do" concerning window size and position. GLUT itself is
335 * not consistent between Windows and UNIX/X11; since
336 * platform independence is a virtue for "freeglut", we
337 * decided to break with GLUT's behaviour.
339 * Under UNIX/X11, it is apparently not possible to get the
340 * window border sizes in order to subtract them off the
341 * window's initial position until some time after the window
342 * has been created. Therefore we decided on the following
343 * behaviour, both under Windows and under UNIX/X11:
344 * - When you create a window with position (x,y) and size
345 * (w,h), the upper left hand corner of the outside of the
346 * window is at (x,y) and the size of the drawable area is
348 * - When you query the size and position of the window--as
349 * is happening here for Windows--"freeglut" will return
350 * the size of the drawable area--the (w,h) that you
351 * specified when you created the window--and the coordinates
352 * of the upper left hand corner of the drawable
353 * area--which is NOT the (x,y) you specified.
358 freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
361 * We need to call GetWindowRect() first...
362 * (this returns the pixel coordinates of the outside of the window)
364 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
366 /* ...then we've got to correct the results we've just received... */
368 #if !TARGET_HOST_WINCE
369 if ( ( fgStructure.GameMode != fgStructure.Window ) && ( fgStructure.Window->Parent == NULL ) &&
370 ( ! fgStructure.Window->IsMenu ) )
372 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
373 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
374 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
375 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
377 #endif /* !TARGET_HOST_WINCE */
381 case GLUT_WINDOW_X: return winRect.left ;
382 case GLUT_WINDOW_Y: return winRect.top ;
383 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
384 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
389 case GLUT_WINDOW_BORDER_WIDTH :
390 #if TARGET_HOST_WINCE
393 return GetSystemMetrics( SM_CXSIZEFRAME );
394 #endif /* !TARGET_HOST_WINCE */
396 case GLUT_WINDOW_HEADER_HEIGHT :
397 #if TARGET_HOST_WINCE
400 return GetSystemMetrics( SM_CYCAPTION );
401 #endif /* TARGET_HOST_WINCE */
403 case GLUT_DISPLAY_MODE_POSSIBLE:
404 #if TARGET_HOST_WINCE
407 return fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
409 #endif /* TARGET_HOST_WINCE */
412 case GLUT_WINDOW_FORMAT_ID:
413 #if !TARGET_HOST_WINCE
414 if( fgStructure.Window != NULL )
415 return GetPixelFormat( fgStructure.Window->Window.Device );
416 #endif /* TARGET_HOST_WINCE */
421 /* The window structure queries */
422 case GLUT_WINDOW_PARENT:
423 if( fgStructure.Window == NULL ) return 0;
424 if( fgStructure.Window->Parent == NULL ) return 0;
425 return fgStructure.Window->Parent->ID;
427 case GLUT_WINDOW_NUM_CHILDREN:
428 if( fgStructure.Window == NULL )
430 return fgListLength( &fgStructure.Window->Children );
432 case GLUT_WINDOW_CURSOR:
433 if( fgStructure.Window == NULL )
435 return fgStructure.Window->State.Cursor;
437 case GLUT_MENU_NUM_ITEMS:
438 if( fgStructure.Menu == NULL )
440 return fgListLength( &fgStructure.Menu->Entries );
442 case GLUT_ACTION_ON_WINDOW_CLOSE:
443 return fgState.ActionOnWindowClose;
446 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
448 case GLUT_RENDERING_CONTEXT:
449 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
450 : GLUT_CREATE_NEW_CONTEXT;
452 case GLUT_DIRECT_RENDERING:
453 return fgState.DirectContext;
457 fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
464 * Returns various device information.
466 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
468 freeglut_assert_ready;
470 /* XXX WARNING: we are mostly lying in this function. */
473 case GLUT_HAS_KEYBOARD:
475 * We always have a keyboard present on PC machines...
477 * XXX I think that some of my PCs will boot without a keyboard.
478 * XXX Also, who says that we are running on a PC? UNIX/X11
479 * XXX is much more generic, and X11 can go over a network.
480 * XXX Though in actuality, we can probably assume BOTH a
481 * XXX mouse and keyboard for most/all of our users.
485 #if TARGET_HOST_UNIX_X11
490 case GLUT_NUM_MOUSE_BUTTONS:
492 * Return the number of mouse buttons available. This is a big guess.
494 * XXX We can probe /var/run/dmesg.boot which is world-readable.
495 * XXX This would be somewhat system-dependant, but is doable.
496 * XXX E.g., on NetBSD, my USB mouse registers:
497 * XXX ums0 at uhidev0: 3 buttons and Z dir.
498 * XXX We can also probe /var/log/XFree86\..*\.log to get
500 * XXX (**) Option "Buttons" "5"
501 * XXX (**) Option "ZAxisMapping" "4 5"
502 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
503 * XXX (**) Mouse0: Buttons: 5
504 * XXX ...which tells us even more, and is a bit less
505 * XXX system-dependant. (Other than MS-WINDOWS, all
506 * XXX target hosts with actual users are probably running
507 * XXX XFree86...) It is at least worth taking a look at
512 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
516 * The Windows can be booted without a mouse.
517 * It would be nice to have this reported.
519 return GetSystemMetrics( SM_MOUSEPRESENT );
521 case GLUT_NUM_MOUSE_BUTTONS:
522 /* We are much more fortunate under Win32 about this... */
523 #if TARGET_HOST_WINCE
526 return GetSystemMetrics( SM_CMOUSEBUTTONS );
527 #endif /* TARGET_HOST_WINCE */
531 case GLUT_JOYSTICK_POLL_RATE:
532 case GLUT_HAS_JOYSTICK:
533 case GLUT_OWNS_JOYSTICK:
534 case GLUT_JOYSTICK_BUTTONS:
535 case GLUT_JOYSTICK_AXES:
536 /* XXX WARNING: THIS IS A BIG LIE! */
539 case GLUT_HAS_SPACEBALL:
540 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
541 case GLUT_HAS_TABLET:
544 case GLUT_NUM_SPACEBALL_BUTTONS:
545 case GLUT_NUM_BUTTON_BOX_BUTTONS:
547 case GLUT_NUM_TABLET_BUTTONS:
550 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
551 return fgStructure.Window ? fgStructure.Window->State.IgnoreKeyRepeat : 0;
553 case GLUT_DEVICE_KEY_REPEAT:
554 /* XXX WARNING: THIS IS A BIG LIE! */
555 return GLUT_KEY_REPEAT_DEFAULT;
558 fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
562 /* And now -- the failure. */
567 * This should return the current state of ALT, SHIFT and CTRL keys.
569 int FGAPIENTRY glutGetModifiers( void )
571 if( fgState.Modifiers == 0xffffffff )
573 fgWarning( "glutGetModifiers() called outside an input callback" );
577 return fgState.Modifiers;
581 * Return the state of the GLUT API overlay subsystem. A misery ;-)
583 int FGAPIENTRY glutLayerGet( GLenum eWhat )
585 freeglut_assert_ready;
588 * This is easy as layers are not implemented ;-)
590 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
591 * XXX is overlay support planned?
596 #if TARGET_HOST_UNIX_X11
598 case GLUT_OVERLAY_POSSIBLE:
601 case GLUT_LAYER_IN_USE:
604 case GLUT_HAS_OVERLAY:
607 case GLUT_TRANSPARENT_INDEX:
609 * Return just anything, which is always defined as zero
615 case GLUT_NORMAL_DAMAGED:
616 /* XXX Actually I do not know. Maybe. */
619 case GLUT_OVERLAY_DAMAGED:
622 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
624 case GLUT_OVERLAY_POSSIBLE:
625 /* return fgSetupPixelFormat( fgStructure.Window, GL_TRUE,
626 PFD_OVERLAY_PLANE ); */
629 case GLUT_LAYER_IN_USE:
632 case GLUT_HAS_OVERLAY:
635 case GLUT_TRANSPARENT_INDEX:
637 * Return just anything, which is always defined as zero
643 case GLUT_NORMAL_DAMAGED:
644 /* XXX Actually I do not know. Maybe. */
647 case GLUT_OVERLAY_DAMAGED:
652 fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
656 /* And fail. That's good. Programs do love failing. */
660 /*** END OF FILE ***/