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.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
32 * TODO BEFORE THE STABLE RELEASE:
34 * glutGet() -- X11 tests passed, but check if all enums
35 * handled (what about Win32?)
36 * glutDeviceGet() -- X11 tests passed, but check if all enums
37 * handled (what about Win32?)
38 * glutGetModifiers() -- OK, but could also remove the limitation
39 * glutLayerGet() -- what about GLUT_NORMAL_DAMAGED?
41 * The fail-on-call policy will help adding the most needed things imho.
44 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
46 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
48 #if TARGET_HOST_POSIX_X11
50 * Queries the GL context about some attributes
52 static int fghGetConfig( int attribute )
56 if( fgStructure.CurrentWindow )
57 glXGetConfig( fgDisplay.Display, fgStructure.CurrentWindow->Window.VisualInfo,
58 attribute, &returnValue );
64 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
67 * General settings assignment method
69 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
71 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
74 * XXX In chronological code add order. (WHY in that order?)
78 case GLUT_INIT_WINDOW_X:
79 fgState.Position.X = (GLint)value;
82 case GLUT_INIT_WINDOW_Y:
83 fgState.Position.Y = (GLint)value;
86 case GLUT_INIT_WINDOW_WIDTH:
87 fgState.Size.X = (GLint)value;
90 case GLUT_INIT_WINDOW_HEIGHT:
91 fgState.Size.Y = (GLint)value;
94 case GLUT_INIT_DISPLAY_MODE:
95 fgState.DisplayMode = (unsigned int)value;
98 case GLUT_ACTION_ON_WINDOW_CLOSE:
99 fgState.ActionOnWindowClose = value;
102 case GLUT_RENDERING_CONTEXT:
103 fgState.UseCurrentContext =
104 ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
107 case GLUT_DIRECT_RENDERING:
108 fgState.DirectContext = value;
111 case GLUT_WINDOW_CURSOR:
112 if( fgStructure.CurrentWindow != NULL )
113 fgStructure.CurrentWindow->State.Cursor = value;
117 fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
122 #if TARGET_HOST_MS_WINDOWS
124 /* The following include file is available from SGI but is not standard:
125 * #include <GL/wglext.h>
126 * So we copy the necessary parts out of it to support the multisampling query
128 #define WGL_SAMPLES_ARB 0x2042
133 * General settings query method
135 int FGAPIENTRY glutGet( GLenum eWhat )
137 #if TARGET_HOST_MS_WINDOWS
139 GLboolean boolValue ;
146 case GLUT_INIT_STATE:
147 return fgState.Initialised;
149 case GLUT_ELAPSED_TIME:
150 return fgElapsedTime();
153 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
155 /* XXX In chronological code add order. (WHY in that order?) */
158 /* Following values are stored in fgState and fgDisplay global structures */
159 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
160 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
161 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
162 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
163 case GLUT_INIT_WINDOW_X: return fgState.Position.X ;
164 case GLUT_INIT_WINDOW_Y: return fgState.Position.Y ;
165 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.X ;
166 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Y ;
167 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
169 #if TARGET_HOST_POSIX_X11
171 * The window/context specific queries are handled mostly by
174 case GLUT_WINDOW_NUM_SAMPLES:
175 #ifdef GLX_VERSION_1_3
176 glGetIntegerv(GL_SAMPLES, &nsamples);
181 * The rest of GLX queries under X are general enough to use a macro to
184 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
186 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
187 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
188 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
189 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
190 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
191 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
192 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
193 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
194 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
195 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
196 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
197 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
198 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
199 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
203 /* Colormap size is handled in a bit different way than all the rest */
204 case GLUT_WINDOW_COLORMAP_SIZE:
205 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
208 * We've got a RGBA visual, so there is no colormap at all.
209 * The other possibility is that we have no current window set.
213 return fgStructure.CurrentWindow->Window.VisualInfo->visual->map_entries;
216 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
221 case GLUT_WINDOW_BORDER_WIDTH:
222 case GLUT_WINDOW_HEADER_HEIGHT:
227 if( fgStructure.CurrentWindow == NULL )
230 XTranslateCoordinates(
232 fgStructure.CurrentWindow->Window.Handle,
233 fgDisplay.RootWindow,
238 case GLUT_WINDOW_X: return x;
239 case GLUT_WINDOW_Y: return y;
244 XTranslateCoordinates(
246 fgStructure.CurrentWindow->Window.Handle,
247 w, 0, 0, &x, &y, &w);
251 case GLUT_WINDOW_BORDER_WIDTH: return x;
252 case GLUT_WINDOW_HEADER_HEIGHT: return y;
256 case GLUT_WINDOW_WIDTH:
257 case GLUT_WINDOW_HEIGHT:
259 XWindowAttributes winAttributes;
261 if( fgStructure.CurrentWindow == NULL )
263 XGetWindowAttributes(
265 fgStructure.CurrentWindow->Window.Handle,
270 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
271 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
275 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
276 case GLUT_DISPLAY_MODE_POSSIBLE:
278 XVisualInfo* visualInfo = fgChooseVisual();
279 if ( visualInfo == NULL ) {
287 /* This is system-dependant */
288 case GLUT_WINDOW_FORMAT_ID:
289 if( fgStructure.CurrentWindow == NULL )
292 return fgStructure.CurrentWindow->Window.VisualInfo->visualid;
294 #elif TARGET_HOST_MS_WINDOWS
296 case GLUT_WINDOW_NUM_SAMPLES:
297 glGetIntegerv(WGL_SAMPLES_ARB, &nsamples);
300 /* Handle the OpenGL inquiries */
301 case GLUT_WINDOW_RGBA:
302 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
303 returnValue = boolValue ? 1 : 0;
305 case GLUT_WINDOW_DOUBLEBUFFER:
306 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
307 returnValue = boolValue ? 1 : 0;
309 case GLUT_WINDOW_STEREO:
310 glGetBooleanv ( GL_STEREO, &boolValue );
311 returnValue = boolValue ? 1 : 0;
314 case GLUT_WINDOW_RED_SIZE:
315 glGetIntegerv ( GL_RED_BITS, &returnValue );
317 case GLUT_WINDOW_GREEN_SIZE:
318 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
320 case GLUT_WINDOW_BLUE_SIZE:
321 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
323 case GLUT_WINDOW_ALPHA_SIZE:
324 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
326 case GLUT_WINDOW_ACCUM_RED_SIZE:
327 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
329 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
330 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
332 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
333 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
335 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
336 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
338 case GLUT_WINDOW_DEPTH_SIZE:
339 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
342 case GLUT_WINDOW_BUFFER_SIZE:
343 returnValue = 1 ; /* ????? */
345 case GLUT_WINDOW_STENCIL_SIZE:
346 returnValue = 0 ; /* ????? */
351 case GLUT_WINDOW_WIDTH:
352 case GLUT_WINDOW_HEIGHT:
355 * There is considerable confusion about the "right thing to
356 * do" concerning window size and position. GLUT itself is
357 * not consistent between Windows and UNIX/X11; since
358 * platform independence is a virtue for "freeglut", we
359 * decided to break with GLUT's behaviour.
361 * Under UNIX/X11, it is apparently not possible to get the
362 * window border sizes in order to subtract them off the
363 * window's initial position until some time after the window
364 * has been created. Therefore we decided on the following
365 * behaviour, both under Windows and under UNIX/X11:
366 * - When you create a window with position (x,y) and size
367 * (w,h), the upper left hand corner of the outside of the
368 * window is at (x,y) and the size of the drawable area is
370 * - When you query the size and position of the window--as
371 * is happening here for Windows--"freeglut" will return
372 * the size of the drawable area--the (w,h) that you
373 * specified when you created the window--and the coordinates
374 * of the upper left hand corner of the drawable
375 * area--which is NOT the (x,y) you specified.
380 freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
383 * We need to call GetWindowRect() first...
384 * (this returns the pixel coordinates of the outside of the window)
386 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
388 /* ...then we've got to correct the results we've just received... */
390 #if !defined(_WIN32_WCE)
391 if ( ( fgStructure.GameModeWindow != fgStructure.CurrentWindow ) && ( fgStructure.CurrentWindow->Parent == NULL ) &&
392 ( ! fgStructure.CurrentWindow->IsMenu ) )
394 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
395 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
396 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
397 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
399 #endif /* !defined(_WIN32_WCE) */
403 case GLUT_WINDOW_X: return winRect.left ;
404 case GLUT_WINDOW_Y: return winRect.top ;
405 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
406 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
411 case GLUT_WINDOW_BORDER_WIDTH :
412 #if defined(_WIN32_WCE)
415 return GetSystemMetrics( SM_CXSIZEFRAME );
416 #endif /* !defined(_WIN32_WCE) */
418 case GLUT_WINDOW_HEADER_HEIGHT :
419 #if defined(_WIN32_WCE)
422 return GetSystemMetrics( SM_CYCAPTION );
423 #endif /* defined(_WIN32_WCE) */
425 case GLUT_DISPLAY_MODE_POSSIBLE:
426 #if defined(_WIN32_WCE)
429 return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
431 #endif /* defined(_WIN32_WCE) */
434 case GLUT_WINDOW_FORMAT_ID:
435 #if !defined(_WIN32_WCE)
436 if( fgStructure.CurrentWindow != NULL )
437 return GetPixelFormat( fgStructure.CurrentWindow->Window.Device );
438 #endif /* defined(_WIN32_WCE) */
443 /* The window structure queries */
444 case GLUT_WINDOW_PARENT:
445 if( fgStructure.CurrentWindow == NULL ) return 0;
446 if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
447 return fgStructure.CurrentWindow->Parent->ID;
449 case GLUT_WINDOW_NUM_CHILDREN:
450 if( fgStructure.CurrentWindow == NULL )
452 return fgListLength( &fgStructure.CurrentWindow->Children );
454 case GLUT_WINDOW_CURSOR:
455 if( fgStructure.CurrentWindow == NULL )
457 return fgStructure.CurrentWindow->State.Cursor;
459 case GLUT_MENU_NUM_ITEMS:
460 if( fgStructure.CurrentMenu == NULL )
462 return fgListLength( &fgStructure.CurrentMenu->Entries );
464 case GLUT_ACTION_ON_WINDOW_CLOSE:
465 return fgState.ActionOnWindowClose;
468 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
470 case GLUT_RENDERING_CONTEXT:
471 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
472 : GLUT_CREATE_NEW_CONTEXT;
474 case GLUT_DIRECT_RENDERING:
475 return fgState.DirectContext;
479 fgWarning( "glutGet(): missing enum handle %d", eWhat );
486 * Returns various device information.
488 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
490 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
492 /* XXX WARNING: we are mostly lying in this function. */
495 case GLUT_HAS_KEYBOARD:
497 * We always have a keyboard present on PC machines...
499 * XXX I think that some of my PCs will boot without a keyboard.
500 * XXX Also, who says that we are running on a PC? UNIX/X11
501 * XXX is much more generic, and X11 can go over a network.
502 * XXX Though in actuality, we can probably assume BOTH a
503 * XXX mouse and keyboard for most/all of our users.
507 #if TARGET_HOST_POSIX_X11
512 case GLUT_NUM_MOUSE_BUTTONS:
514 * Return the number of mouse buttons available. This is a big guess.
516 * XXX We can probe /var/run/dmesg.boot which is world-readable.
517 * XXX This would be somewhat system-dependant, but is doable.
518 * XXX E.g., on NetBSD, my USB mouse registers:
519 * XXX ums0 at uhidev0: 3 buttons and Z dir.
520 * XXX We can also probe /var/log/XFree86\..*\.log to get
522 * XXX (**) Option "Buttons" "5"
523 * XXX (**) Option "ZAxisMapping" "4 5"
524 * XXX (**) Mouse0: ZAxisMapping: buttons 4 and 5
525 * XXX (**) Mouse0: Buttons: 5
526 * XXX ...which tells us even more, and is a bit less
527 * XXX system-dependant. (Other than MS-WINDOWS, all
528 * XXX target hosts with actual users are probably running
529 * XXX XFree86...) It is at least worth taking a look at
534 #elif TARGET_HOST_MS_WINDOWS
538 * The Windows can be booted without a mouse.
539 * It would be nice to have this reported.
541 return GetSystemMetrics( SM_MOUSEPRESENT );
543 case GLUT_NUM_MOUSE_BUTTONS:
544 /* We are much more fortunate under Win32 about this... */
545 #if defined(_WIN32_WCE)
548 return GetSystemMetrics( SM_CMOUSEBUTTONS );
549 #endif /* defined(_WIN32_WCE) */
553 case GLUT_HAS_JOYSTICK:
554 return fgJoystickDetect ();
556 case GLUT_OWNS_JOYSTICK:
557 return fgState.JoysticksInitialised;
559 case GLUT_JOYSTICK_POLL_RATE:
560 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
562 /* XXX The following two are only for Joystick 0 but this is an improvement */
563 case GLUT_JOYSTICK_BUTTONS:
564 return glutJoystickGetNumButtons ( 0 );
566 case GLUT_JOYSTICK_AXES:
567 return glutJoystickGetNumAxes ( 0 );
569 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
570 return fgInputDeviceDetect ();
573 if ( fgState.InputDevsInitialised ) return 8;
576 case GLUT_NUM_BUTTON_BOX_BUTTONS:
579 case GLUT_HAS_SPACEBALL:
580 case GLUT_HAS_TABLET:
583 case GLUT_NUM_SPACEBALL_BUTTONS:
584 case GLUT_NUM_TABLET_BUTTONS:
587 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
588 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
590 case GLUT_DEVICE_KEY_REPEAT:
591 return fgState.KeyRepeat;
594 fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
598 /* And now -- the failure. */
603 * This should return the current state of ALT, SHIFT and CTRL keys.
605 int FGAPIENTRY glutGetModifiers( void )
607 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
608 if( fgState.Modifiers == INVALID_MODIFIERS )
610 fgWarning( "glutGetModifiers() called outside an input callback" );
614 return fgState.Modifiers;
618 * Return the state of the GLUT API overlay subsystem. A misery ;-)
620 int FGAPIENTRY glutLayerGet( GLenum eWhat )
622 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
625 * This is easy as layers are not implemented ;-)
627 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
628 * XXX is overlay support planned?
633 #if TARGET_HOST_POSIX_X11
635 case GLUT_OVERLAY_POSSIBLE:
638 case GLUT_LAYER_IN_USE:
641 case GLUT_HAS_OVERLAY:
644 case GLUT_TRANSPARENT_INDEX:
646 * Return just anything, which is always defined as zero
652 case GLUT_NORMAL_DAMAGED:
653 /* XXX Actually I do not know. Maybe. */
656 case GLUT_OVERLAY_DAMAGED:
659 #elif TARGET_HOST_MS_WINDOWS
661 case GLUT_OVERLAY_POSSIBLE:
662 /* return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
663 PFD_OVERLAY_PLANE ); */
666 case GLUT_LAYER_IN_USE:
669 case GLUT_HAS_OVERLAY:
672 case GLUT_TRANSPARENT_INDEX:
674 * Return just anything, which is always defined as zero
680 case GLUT_NORMAL_DAMAGED:
681 /* XXX Actually I do not know. Maybe. */
684 case GLUT_OVERLAY_DAMAGED:
689 fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
693 /* And fail. That's good. Programs do love failing. */
697 /*** END OF FILE ***/