4 * Freeglut state query methods.
\r
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
\r
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
\r
8 * Creation date: Thu Dec 16 1999
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #include <GL/freeglut.h>
\r
29 #include "freeglut_internal.h"
\r
32 * TODO BEFORE THE STABLE RELEASE:
\r
34 * glutGet() -- X11 tests passed, but check if all enums
\r
35 * handled (what about Win32?)
\r
36 * glutDeviceGet() -- X11 tests passed, but check if all enums
\r
37 * handled (what about Win32?)
\r
38 * glutGetModifiers() -- OK, but could also remove the limitation
\r
39 * glutLayerGet() -- what about GLUT_NORMAL_DAMAGED?
\r
41 * The fail-on-call policy will help adding the most needed things imho.
\r
44 extern int fgPlatformGlutGet ( GLenum eWhat );
\r
45 extern int fgPlatformGlutDeviceGet ( GLenum eWhat );
\r
46 extern int fgPlatformGlutLayerGet ( GLenum eWhat );
\r
48 /* A helper function to check if a display mode is possible to use */
\r
49 #if TARGET_HOST_POSIX_X11
\r
50 GLXFBConfig* fgChooseFBConfig( int* numcfgs );
\r
54 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
\r
56 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
\r
58 #if TARGET_HOST_POSIX_X11
\r
60 * Queries the GL context about some attributes
\r
62 static int fghGetConfig( int attribute )
\r
64 int returnValue = 0;
\r
65 int result; /* Not checked */
\r
67 if( fgStructure.CurrentWindow )
\r
68 result = glXGetFBConfigAttrib( fgDisplay.Display,
\r
69 *(fgStructure.CurrentWindow->Window.FBConfig),
\r
76 int fgPlatformGlutGet ( GLenum eWhat )
\r
83 * The window/context specific queries are handled mostly by
\r
86 case GLUT_WINDOW_NUM_SAMPLES:
\r
87 #ifdef GLX_VERSION_1_3
\r
88 glGetIntegerv(GL_SAMPLES, &nsamples);
\r
93 * The rest of GLX queries under X are general enough to use a macro to
\r
96 # define GLX_QUERY(a,b) case a: return fghGetConfig( b );
\r
98 GLX_QUERY( GLUT_WINDOW_RGBA, GLX_RGBA );
\r
99 GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER, GLX_DOUBLEBUFFER );
\r
100 GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE, GLX_BUFFER_SIZE );
\r
101 GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE, GLX_STENCIL_SIZE );
\r
102 GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE, GLX_DEPTH_SIZE );
\r
103 GLX_QUERY( GLUT_WINDOW_RED_SIZE, GLX_RED_SIZE );
\r
104 GLX_QUERY( GLUT_WINDOW_GREEN_SIZE, GLX_GREEN_SIZE );
\r
105 GLX_QUERY( GLUT_WINDOW_BLUE_SIZE, GLX_BLUE_SIZE );
\r
106 GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE, GLX_ALPHA_SIZE );
\r
107 GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE, GLX_ACCUM_RED_SIZE );
\r
108 GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE, GLX_ACCUM_GREEN_SIZE );
\r
109 GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE, GLX_ACCUM_BLUE_SIZE );
\r
110 GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE, GLX_ACCUM_ALPHA_SIZE );
\r
111 GLX_QUERY( GLUT_WINDOW_STEREO, GLX_STEREO );
\r
115 /* Colormap size is handled in a bit different way than all the rest */
\r
116 case GLUT_WINDOW_COLORMAP_SIZE:
\r
117 if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
\r
120 * We've got a RGBA visual, so there is no colormap at all.
\r
121 * The other possibility is that we have no current window set.
\r
127 const GLXFBConfig * fbconfig =
\r
128 fgStructure.CurrentWindow->Window.FBConfig;
\r
130 XVisualInfo * visualInfo =
\r
131 glXGetVisualFromFBConfig( fgDisplay.Display, *fbconfig );
\r
133 const int result = visualInfo->visual->map_entries;
\r
141 * Those calls are somewhat similiar, as they use XGetWindowAttributes()
\r
144 case GLUT_WINDOW_X:
\r
145 case GLUT_WINDOW_Y:
\r
146 case GLUT_WINDOW_BORDER_WIDTH:
\r
147 case GLUT_WINDOW_HEADER_HEIGHT:
\r
152 if( fgStructure.CurrentWindow == NULL )
\r
155 XTranslateCoordinates(
\r
157 fgStructure.CurrentWindow->Window.Handle,
\r
158 fgDisplay.RootWindow,
\r
163 case GLUT_WINDOW_X: return x;
\r
164 case GLUT_WINDOW_Y: return y;
\r
169 XTranslateCoordinates(
\r
171 fgStructure.CurrentWindow->Window.Handle,
\r
172 w, 0, 0, &x, &y, &w);
\r
176 case GLUT_WINDOW_BORDER_WIDTH: return x;
\r
177 case GLUT_WINDOW_HEADER_HEIGHT: return y;
\r
181 case GLUT_WINDOW_WIDTH:
\r
182 case GLUT_WINDOW_HEIGHT:
\r
184 XWindowAttributes winAttributes;
\r
186 if( fgStructure.CurrentWindow == NULL )
\r
188 XGetWindowAttributes(
\r
190 fgStructure.CurrentWindow->Window.Handle,
\r
195 case GLUT_WINDOW_WIDTH: return winAttributes.width ;
\r
196 case GLUT_WINDOW_HEIGHT: return winAttributes.height ;
\r
200 /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
\r
201 case GLUT_DISPLAY_MODE_POSSIBLE:
\r
203 /* We should not have to call fgChooseFBConfig again here. */
\r
204 GLXFBConfig * fbconfig;
\r
207 fbconfig = fgChooseFBConfig(NULL);
\r
209 if (fbconfig == NULL)
\r
222 /* This is system-dependant */
\r
223 case GLUT_WINDOW_FORMAT_ID:
\r
224 if( fgStructure.CurrentWindow == NULL )
\r
227 return fghGetConfig( GLX_VISUAL_ID );
\r
230 fgWarning( "glutGet(): missing enum handle %d", eWhat );
\r
236 int fgPlatformGlutDeviceGet ( GLenum eWhat )
\r
240 case GLUT_HAS_KEYBOARD:
\r
242 * X11 has a core keyboard by definition, although it can
\r
243 * be present as a virtual/dummy keyboard. For now, there
\r
244 * is no reliable way to tell if a real keyboard is present.
\r
248 /* X11 has a mouse by definition */
\r
249 case GLUT_HAS_MOUSE:
\r
252 case GLUT_NUM_MOUSE_BUTTONS:
\r
253 /* We should be able to pass NULL when the last argument is zero,
\r
254 * but at least one X server has a bug where this causes a segfault.
\r
256 * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
\r
257 * rather than an Axis; "freeglut_main.c" expects this when
\r
258 * checking for a wheel event.
\r
262 int nbuttons = XGetPointerMapping(fgDisplay.Display, &map,0);
\r
267 fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
\r
271 /* And now -- the failure. */
\r
275 int fgPlatformGlutLayerGet( GLenum eWhat )
\r
278 * This is easy as layers are not implemented ;-)
\r
280 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
\r
281 * XXX is overlay support planned?
\r
285 case GLUT_OVERLAY_POSSIBLE:
\r
288 case GLUT_LAYER_IN_USE:
\r
289 return GLUT_NORMAL;
\r
291 case GLUT_HAS_OVERLAY:
\r
294 case GLUT_TRANSPARENT_INDEX:
\r
296 * Return just anything, which is always defined as zero
\r
302 case GLUT_NORMAL_DAMAGED:
\r
303 /* XXX Actually I do not know. Maybe. */
\r
306 case GLUT_OVERLAY_DAMAGED:
\r
310 fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
\r
314 /* And fail. That's good. Programs do love failing. */
\r
322 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
\r
325 * General settings assignment method
\r
327 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
\r
329 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
\r
332 * XXX In chronological code add order. (WHY in that order?)
\r
336 case GLUT_INIT_WINDOW_X:
\r
337 fgState.Position.X = (GLint)value;
\r
340 case GLUT_INIT_WINDOW_Y:
\r
341 fgState.Position.Y = (GLint)value;
\r
344 case GLUT_INIT_WINDOW_WIDTH:
\r
345 fgState.Size.X = (GLint)value;
\r
348 case GLUT_INIT_WINDOW_HEIGHT:
\r
349 fgState.Size.Y = (GLint)value;
\r
352 case GLUT_INIT_DISPLAY_MODE:
\r
353 fgState.DisplayMode = (unsigned int)value;
\r
356 case GLUT_ACTION_ON_WINDOW_CLOSE:
\r
357 fgState.ActionOnWindowClose = value;
\r
360 case GLUT_RENDERING_CONTEXT:
\r
361 fgState.UseCurrentContext =
\r
362 ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
\r
365 case GLUT_DIRECT_RENDERING:
\r
366 fgState.DirectContext = value;
\r
369 case GLUT_WINDOW_CURSOR:
\r
370 if( fgStructure.CurrentWindow != NULL )
\r
371 fgStructure.CurrentWindow->State.Cursor = value;
\r
375 fgState.AuxiliaryBufferNumber = value;
\r
378 case GLUT_MULTISAMPLE:
\r
379 fgState.SampleNumber = value;
\r
383 fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
\r
389 * General settings query method
\r
391 int FGAPIENTRY glutGet( GLenum eWhat )
\r
395 case GLUT_INIT_STATE:
\r
396 return fgState.Initialised;
\r
398 case GLUT_ELAPSED_TIME:
\r
399 return fgElapsedTime();
\r
402 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
\r
404 /* XXX In chronological code add order. (WHY in that order?) */
\r
407 /* Following values are stored in fgState and fgDisplay global structures */
\r
408 case GLUT_SCREEN_WIDTH: return fgDisplay.ScreenWidth ;
\r
409 case GLUT_SCREEN_HEIGHT: return fgDisplay.ScreenHeight ;
\r
410 case GLUT_SCREEN_WIDTH_MM: return fgDisplay.ScreenWidthMM ;
\r
411 case GLUT_SCREEN_HEIGHT_MM: return fgDisplay.ScreenHeightMM;
\r
412 case GLUT_INIT_WINDOW_X: return fgState.Position.Use ?
\r
413 fgState.Position.X : -1 ;
\r
414 case GLUT_INIT_WINDOW_Y: return fgState.Position.Use ?
\r
415 fgState.Position.Y : -1 ;
\r
416 case GLUT_INIT_WINDOW_WIDTH: return fgState.Size.Use ?
\r
417 fgState.Size.X : -1 ;
\r
418 case GLUT_INIT_WINDOW_HEIGHT: return fgState.Size.Use ?
\r
419 fgState.Size.Y : -1 ;
\r
420 case GLUT_INIT_DISPLAY_MODE: return fgState.DisplayMode ;
\r
421 case GLUT_INIT_MAJOR_VERSION: return fgState.MajorVersion ;
\r
422 case GLUT_INIT_MINOR_VERSION: return fgState.MinorVersion ;
\r
423 case GLUT_INIT_FLAGS: return fgState.ContextFlags ;
\r
424 case GLUT_INIT_PROFILE: return fgState.ContextProfile ;
\r
426 /* The window structure queries */
\r
427 case GLUT_WINDOW_PARENT:
\r
428 if( fgStructure.CurrentWindow == NULL ) return 0;
\r
429 if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
\r
430 return fgStructure.CurrentWindow->Parent->ID;
\r
432 case GLUT_WINDOW_NUM_CHILDREN:
\r
433 if( fgStructure.CurrentWindow == NULL )
\r
435 return fgListLength( &fgStructure.CurrentWindow->Children );
\r
437 case GLUT_WINDOW_CURSOR:
\r
438 if( fgStructure.CurrentWindow == NULL )
\r
440 return fgStructure.CurrentWindow->State.Cursor;
\r
442 case GLUT_MENU_NUM_ITEMS:
\r
443 if( fgStructure.CurrentMenu == NULL )
\r
445 return fgListLength( &fgStructure.CurrentMenu->Entries );
\r
447 case GLUT_ACTION_ON_WINDOW_CLOSE:
\r
448 return fgState.ActionOnWindowClose;
\r
450 case GLUT_VERSION :
\r
451 return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
\r
453 case GLUT_RENDERING_CONTEXT:
\r
454 return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
\r
455 : GLUT_CREATE_NEW_CONTEXT;
\r
457 case GLUT_DIRECT_RENDERING:
\r
458 return fgState.DirectContext;
\r
460 case GLUT_FULL_SCREEN:
\r
461 return fgStructure.CurrentWindow->State.IsFullscreen;
\r
464 return fgState.AuxiliaryBufferNumber;
\r
466 case GLUT_MULTISAMPLE:
\r
467 return fgState.SampleNumber;
\r
470 return fgPlatformGlutGet ( eWhat );
\r
477 * Returns various device information.
\r
479 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
\r
481 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
\r
483 /* XXX WARNING: we are mostly lying in this function. */
\r
486 case GLUT_HAS_JOYSTICK:
\r
487 return fgJoystickDetect ();
\r
489 case GLUT_OWNS_JOYSTICK:
\r
490 return fgState.JoysticksInitialised;
\r
492 case GLUT_JOYSTICK_POLL_RATE:
\r
493 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
\r
495 /* XXX The following two are only for Joystick 0 but this is an improvement */
\r
496 case GLUT_JOYSTICK_BUTTONS:
\r
497 return glutJoystickGetNumButtons ( 0 );
\r
499 case GLUT_JOYSTICK_AXES:
\r
500 return glutJoystickGetNumAxes ( 0 );
\r
502 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
\r
503 return fgInputDeviceDetect ();
\r
505 case GLUT_NUM_DIALS:
\r
506 if ( fgState.InputDevsInitialised ) return 8;
\r
509 case GLUT_NUM_BUTTON_BOX_BUTTONS:
\r
512 case GLUT_HAS_SPACEBALL:
\r
513 return fgHasSpaceball();
\r
515 case GLUT_HAS_TABLET:
\r
518 case GLUT_NUM_SPACEBALL_BUTTONS:
\r
519 return fgSpaceballNumButtons();
\r
521 case GLUT_NUM_TABLET_BUTTONS:
\r
524 case GLUT_DEVICE_IGNORE_KEY_REPEAT:
\r
525 return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
\r
527 case GLUT_DEVICE_KEY_REPEAT:
\r
528 return fgState.KeyRepeat;
\r
531 return fgPlatformGlutDeviceGet ( eWhat );
\r
534 /* And now -- the failure. */
\r
539 * This should return the current state of ALT, SHIFT and CTRL keys.
\r
541 int FGAPIENTRY glutGetModifiers( void )
\r
543 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
\r
544 if( fgState.Modifiers == INVALID_MODIFIERS )
\r
546 fgWarning( "glutGetModifiers() called outside an input callback" );
\r
550 return fgState.Modifiers;
\r
554 * Return the state of the GLUT API overlay subsystem. A misery ;-)
\r
556 int FGAPIENTRY glutLayerGet( GLenum eWhat )
\r
558 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
\r
561 * This is easy as layers are not implemented ;-)
\r
563 * XXX Can we merge the UNIX/X11 and WIN32 sections? Or
\r
564 * XXX is overlay support planned?
\r
570 return fgPlatformGlutLayerGet( eWhat );
\r
573 /* And fail. That's good. Programs do love failing. */
\r
577 int * FGAPIENTRY glutGetModeValues(GLenum eWhat, int * size)
\r
581 #if TARGET_HOST_POSIX_X11
\r
583 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
\r
584 int fbconfigArraySize; /* Number of FBConfigs in the array */
\r
585 int attribute_name = 0;
\r
588 FREEGLUT_EXIT_IF_NOT_INITIALISED("glutGetModeValues");
\r
595 #if TARGET_HOST_POSIX_X11
\r
597 case GLUT_MULTISAMPLE:
\r
599 attributes[0] = GLX_BUFFER_SIZE;
\r
600 attributes[1] = GLX_DONT_CARE;
\r
606 FBConfigs are now sorted by increasing number of auxiliary
\r
607 buffers. We want at least one buffer.
\r
609 attributes[2] = GLX_AUX_BUFFERS;
\r
611 attributes[4] = None;
\r
613 attribute_name = GLX_AUX_BUFFERS;
\r
618 case GLUT_MULTISAMPLE:
\r
619 attributes[2] = GLX_AUX_BUFFERS;
\r
620 attributes[3] = GLX_DONT_CARE;
\r
621 attributes[4] = GLX_SAMPLE_BUFFERS;
\r
624 FBConfigs are now sorted by increasing number of samples per
\r
625 pixel. We want at least one sample.
\r
627 attributes[6] = GLX_SAMPLES;
\r
629 attributes[8] = None;
\r
631 attribute_name = GLX_SAMPLES;
\r
636 fbconfigArray = glXChooseFBConfig(fgDisplay.Display,
\r
639 &fbconfigArraySize);
\r
641 if (fbconfigArray != NULL)
\r
644 int result; /* Returned by glXGetFBConfigAttrib. Not checked. */
\r
645 int previous_value;
\r
648 temp_array = malloc(sizeof(int) * fbconfigArraySize);
\r
649 previous_value = 0;
\r
651 for (i = 0; i < fbconfigArraySize; i++)
\r
655 result = glXGetFBConfigAttrib(fgDisplay.Display,
\r
659 if (value > previous_value)
\r
661 temp_array[*size] = value;
\r
662 previous_value = value;
\r
667 array = malloc(sizeof(int) * (*size));
\r
668 for (i = 0; i < *size; i++)
\r
670 array[i] = temp_array[i];
\r
674 XFree(fbconfigArray);
\r
687 /*** END OF FILE ***/
\r