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
123 /* The following include file is available from SGI but is not standard:
124 * #include <GL/wglext.h>
125 * So we copy the necessary parts out of it to support the multisampling query
127 #define WGL_SAMPLES_ARB 0x2042
132 * General settings query method
134 int FGAPIENTRY glutGet( GLenum eWhat )
136 #if TARGET_HOST_MS_WINDOWS
138 GLboolean boolValue ;
145 case GLUT_INIT_STATE:
146 return fgState.Initialised;
148 case GLUT_ELAPSED_TIME:
149 return fgElapsedTime();
152 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
154 /* XXX In chronological code add order. (WHY in that order?) */
157 /* 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.Use ?
163 fgState.Position.X : -1 ;
164 case GLUT_INIT_WINDOW_Y: return fgState.Position.Use ?
165 fgState.Position.Y : -1 ;
166 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.Use ?
167 fgState.Size.X : -1 ;
168 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Use ?
169 fgState.Size.Y : -1 ;
170 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
172 #if TARGET_HOST_POSIX_X11
174 * The window/context specific queries are handled mostly by
177 case GLUT_WINDOW_NUM_SAMPLES:
178 #ifdef GLX_VERSION_1_3
179 glGetIntegerv(GL_SAMPLES, &nsamples);
184 * The rest of GLX queries under X are general enough to use a macro to
187 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
189 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
190 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
191 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
192 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
193 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
194 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
195 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
196 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
197 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
198 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
199 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
200 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
201 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
202 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
206 /* Colormap size is handled in a bit different way than all the rest */
207 case GLUT_WINDOW_COLORMAP_SIZE:
208 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
211 * We've got a RGBA visual, so there is no colormap at all.
212 * The other possibility is that we have no current window set.
216 return fgStructure.CurrentWindow->Window.VisualInfo->visual->map_entries;
219 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
224 case GLUT_WINDOW_BORDER_WIDTH:
225 case GLUT_WINDOW_HEADER_HEIGHT:
230 if( fgStructure.CurrentWindow == NULL )
233 XTranslateCoordinates(
235 fgStructure.CurrentWindow->Window.Handle,
236 fgDisplay.RootWindow,
241 case GLUT_WINDOW_X: return x;
242 case GLUT_WINDOW_Y: return y;
247 XTranslateCoordinates(
249 fgStructure.CurrentWindow->Window.Handle,
250 w, 0, 0, &x, &y, &w);
254 case GLUT_WINDOW_BORDER_WIDTH: return x;
255 case GLUT_WINDOW_HEADER_HEIGHT: return y;
259 case GLUT_WINDOW_WIDTH:
260 case GLUT_WINDOW_HEIGHT:
262 XWindowAttributes winAttributes;
264 if( fgStructure.CurrentWindow == NULL )
266 XGetWindowAttributes(
268 fgStructure.CurrentWindow->Window.Handle,
273 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
274 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
278 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
279 case GLUT_DISPLAY_MODE_POSSIBLE:
281 XVisualInfo* visualInfo = fgChooseVisual();
282 if ( visualInfo == NULL ) {
290 /* This is system-dependant */
291 case GLUT_WINDOW_FORMAT_ID:
292 if( fgStructure.CurrentWindow == NULL )
295 return fgStructure.CurrentWindow->Window.VisualInfo->visualid;
297 #elif TARGET_HOST_MS_WINDOWS
299 case GLUT_WINDOW_NUM_SAMPLES:
300 glGetIntegerv(WGL_SAMPLES_ARB, &nsamples);
303 /* Handle the OpenGL inquiries */
304 case GLUT_WINDOW_RGBA:
305 glGetBooleanv ( GL_RGBA_MODE, &boolValue );
306 returnValue = boolValue ? 1 : 0;
308 case GLUT_WINDOW_DOUBLEBUFFER:
309 glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
310 returnValue = boolValue ? 1 : 0;
312 case GLUT_WINDOW_STEREO:
313 glGetBooleanv ( GL_STEREO, &boolValue );
314 returnValue = boolValue ? 1 : 0;
317 case GLUT_WINDOW_RED_SIZE:
318 glGetIntegerv ( GL_RED_BITS, &returnValue );
320 case GLUT_WINDOW_GREEN_SIZE:
321 glGetIntegerv ( GL_GREEN_BITS, &returnValue );
323 case GLUT_WINDOW_BLUE_SIZE:
324 glGetIntegerv ( GL_BLUE_BITS, &returnValue );
326 case GLUT_WINDOW_ALPHA_SIZE:
327 glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
329 case GLUT_WINDOW_ACCUM_RED_SIZE:
330 glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
332 case GLUT_WINDOW_ACCUM_GREEN_SIZE:
333 glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
335 case GLUT_WINDOW_ACCUM_BLUE_SIZE:
336 glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
338 case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
339 glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
341 case GLUT_WINDOW_DEPTH_SIZE:
342 glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
345 case GLUT_WINDOW_BUFFER_SIZE:
346 returnValue = 1 ; /* ????? */
348 case GLUT_WINDOW_STENCIL_SIZE:
349 returnValue = 0 ; /* ????? */
354 case GLUT_WINDOW_WIDTH:
355 case GLUT_WINDOW_HEIGHT:
358 * There is considerable confusion about the "right thing to
359 * do" concerning window size and position. GLUT itself is
360 * not consistent between Windows and UNIX/X11; since
361 * platform independence is a virtue for "freeglut", we
362 * decided to break with GLUT's behaviour.
364 * Under UNIX/X11, it is apparently not possible to get the
365 * window border sizes in order to subtract them off the
366 * window's initial position until some time after the window
367 * has been created. Therefore we decided on the following
368 * behaviour, both under Windows and under UNIX/X11:
369 * - When you create a window with position (x,y) and size
370 * (w,h), the upper left hand corner of the outside of the
371 * window is at (x,y) and the size of the drawable area is
373 * - When you query the size and position of the window--as
374 * is happening here for Windows--"freeglut" will return
375 * the size of the drawable area--the (w,h) that you
376 * specified when you created the window--and the coordinates
377 * of the upper left hand corner of the drawable
378 * area--which is NOT the (x,y) you specified.
383 freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
386 * We need to call GetWindowRect() first...
387 * (this returns the pixel coordinates of the outside of the window)
389 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
391 /* ...then we've got to correct the results we've just received... */
393 #if !defined(_WIN32_WCE)
394 if ( ( fgStructure.GameModeWindow != fgStructure.CurrentWindow ) && ( fgStructure.CurrentWindow->Parent == NULL ) &&
395 ( ! fgStructure.CurrentWindow->IsMenu ) )
397 winRect.left += GetSystemMetrics( SM_CXSIZEFRAME );
398 winRect.right -= GetSystemMetrics( SM_CXSIZEFRAME );
399 winRect.top += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
400 winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
402 #endif /* !defined(_WIN32_WCE) */
406 case GLUT_WINDOW_X: return winRect.left ;
407 case GLUT_WINDOW_Y: return winRect.top ;
408 case GLUT_WINDOW_WIDTH: return winRect.right - winRect.left;
409 case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
414 case GLUT_WINDOW_BORDER_WIDTH :
415 #if defined(_WIN32_WCE)
418 return GetSystemMetrics( SM_CXSIZEFRAME );
419 #endif /* !defined(_WIN32_WCE) */
421 case GLUT_WINDOW_HEADER_HEIGHT :
422 #if defined(_WIN32_WCE)
425 return GetSystemMetrics( SM_CYCAPTION );
426 #endif /* defined(_WIN32_WCE) */
428 case GLUT_DISPLAY_MODE_POSSIBLE:
429 #if defined(_WIN32_WCE)
432 return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
434 #endif /* defined(_WIN32_WCE) */
437 case GLUT_WINDOW_FORMAT_ID:
438 #if !defined(_WIN32_WCE)
439 if( fgStructure.CurrentWindow != NULL )
440 return GetPixelFormat( fgStructure.CurrentWindow->Window.Device );
441 #endif /* defined(_WIN32_WCE) */
446 /* The window structure queries */
447 case GLUT_WINDOW_PARENT:
448 if( fgStructure.CurrentWindow == NULL ) return 0;
449 if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
450 return fgStructure.CurrentWindow->Parent->ID;
452 case GLUT_WINDOW_NUM_CHILDREN:
453 if( fgStructure.CurrentWindow == NULL )
455 return fgListLength( &fgStructure.CurrentWindow->Children );
457 case GLUT_WINDOW_CURSOR:
458 if( fgStructure.CurrentWindow == NULL )
460 return fgStructure.CurrentWindow->State.Cursor;
462 case GLUT_MENU_NUM_ITEMS:
463 if( fgStructure.CurrentMenu == NULL )
465 return fgListLength( &fgStructure.CurrentMenu->Entries );
467 case GLUT_ACTION_ON_WINDOW_CLOSE:
468 return fgState.ActionOnWindowClose;
471 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
473 case GLUT_RENDERING_CONTEXT:
474 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
475 : GLUT_CREATE_NEW_CONTEXT;
477 case GLUT_DIRECT_RENDERING:
478 return fgState.DirectContext;
482 fgWarning( "glutGet(): missing enum handle %d", eWhat );
489 * Returns various device information.
491 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
493 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
495 /* XXX WARNING: we are mostly lying in this function. */
498 case GLUT_HAS_KEYBOARD:
500 * Win32 is assumed a keyboard, and this cannot be queried,
501 * except for WindowsCE.
503 * X11 has a core keyboard by definition, although it can
504 * be present as a virtual/dummy keyboard. For now, there
505 * is no reliable way to tell if a real keyboard is present.
507 #if defined(_WIN32_CE)
508 return ( GetKeyboardStatus() & KBDI_KEYBOARD_PRESENT ) ? 1 : 0;
509 # if FREEGLUT_LIB_PRAGMAS
510 # pragma comment (lib,"Kbdui.lib")
517 #if TARGET_HOST_POSIX_X11
519 /* X11 has a mouse by definition */
523 case GLUT_NUM_MOUSE_BUTTONS:
524 /* We should be able to pass NULL when the last argument is zero,
525 * but at least one X server has a bug where this causes a segfault.
527 * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
528 * rather than an Axis; "freeglut_main.c" expects this when
529 * checking for a wheel event.
533 int nbuttons = XGetPointerMapping(fgDisplay.Display, &map,0);
537 #elif TARGET_HOST_MS_WINDOWS
541 * MS Windows can be booted without a mouse.
543 return GetSystemMetrics( SM_MOUSEPRESENT );
545 case GLUT_NUM_MOUSE_BUTTONS:
546 # if defined(_WIN32_WCE)
549 return GetSystemMetrics( SM_CMOUSEBUTTONS );
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 ***/