resetting manifest requested domain to floor
[platform/upstream/freeglut.git] / src / freeglut_state.c
1 /*
2  * freeglut_state.c
3  *
4  * Freeglut state query methods.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /*
32  * TODO BEFORE THE STABLE RELEASE:
33  *
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?
40  *
41  * The fail-on-call policy will help adding the most needed things imho.
42  */
43
44 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
45
46 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
47
48 #if TARGET_HOST_POSIX_X11
49 /*
50  * Queries the GL context about some attributes
51  */
52 static int fghGetConfig( int attribute )
53 {
54   int returnValue = 0;
55   int result;  /*  Not checked  */
56
57   if( fgStructure.CurrentWindow )
58       result = glXGetFBConfigAttrib( fgDisplay.Display,
59                                      *(fgStructure.CurrentWindow->Window.FBConfig),
60                                      attribute,
61                                      &returnValue );
62
63   return returnValue;
64 }
65 #endif
66
67 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
68
69 /*
70  * General settings assignment method
71  */
72 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
73 {
74     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
75
76     /*
77      * XXX In chronological code add order.  (WHY in that order?)
78      */
79     switch( eWhat )
80     {
81     case GLUT_INIT_WINDOW_X:
82         fgState.Position.X = (GLint)value;
83         break;
84
85     case GLUT_INIT_WINDOW_Y:
86         fgState.Position.Y = (GLint)value;
87         break;
88
89     case GLUT_INIT_WINDOW_WIDTH:
90         fgState.Size.X = (GLint)value;
91         break;
92
93     case GLUT_INIT_WINDOW_HEIGHT:
94         fgState.Size.Y = (GLint)value;
95         break;
96
97     case GLUT_INIT_DISPLAY_MODE:
98         fgState.DisplayMode = (unsigned int)value;
99         break;
100
101     case GLUT_ACTION_ON_WINDOW_CLOSE:
102         fgState.ActionOnWindowClose = value;
103         break;
104
105     case GLUT_RENDERING_CONTEXT:
106         fgState.UseCurrentContext =
107             ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
108         break;
109
110     case GLUT_DIRECT_RENDERING:
111         fgState.DirectContext = value;
112         break;
113
114     case GLUT_WINDOW_CURSOR:
115         if( fgStructure.CurrentWindow != NULL )
116             fgStructure.CurrentWindow->State.Cursor = value;
117         break;
118
119     case GLUT_AUX:
120       fgState.AuxiliaryBufferNumber = value;
121       break;
122
123     case GLUT_MULTISAMPLE:
124       fgState.SampleNumber = value;
125       break;
126
127     default:
128         fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
129         break;
130     }
131 }
132
133 #if TARGET_HOST_MS_WINDOWS
134 /* The following include file is available from SGI but is not standard:
135  *   #include <GL/wglext.h>
136  * So we copy the necessary parts out of it to support the multisampling query
137  */
138 #define WGL_SAMPLES_ARB                0x2042
139 #endif
140
141
142 /*
143  * General settings query method
144  */
145 int FGAPIENTRY glutGet( GLenum eWhat )
146 {
147 #if TARGET_HOST_MS_WINDOWS
148     int returnValue ;
149     GLboolean boolValue ;
150 #endif
151
152     int nsamples = 0;
153
154     switch (eWhat)
155     {
156     case GLUT_INIT_STATE:
157         return fgState.Initialised;
158
159     case GLUT_ELAPSED_TIME:
160         return fgElapsedTime();
161     }
162
163     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
164
165     /* XXX In chronological code add order.  (WHY in that order?) */
166     switch( eWhat )
167     {
168     /* Following values are stored in fgState and fgDisplay global structures */
169     case GLUT_SCREEN_WIDTH:         return fgDisplay.ScreenWidth   ;
170     case GLUT_SCREEN_HEIGHT:        return fgDisplay.ScreenHeight  ;
171     case GLUT_SCREEN_WIDTH_MM:      return fgDisplay.ScreenWidthMM ;
172     case GLUT_SCREEN_HEIGHT_MM:     return fgDisplay.ScreenHeightMM;
173     case GLUT_INIT_WINDOW_X:        return fgState.Position.Use ?
174                                            fgState.Position.X : -1 ;
175     case GLUT_INIT_WINDOW_Y:        return fgState.Position.Use ?
176                                            fgState.Position.Y : -1 ;
177     case GLUT_INIT_WINDOW_WIDTH:    return fgState.Size.Use ?
178                                            fgState.Size.X : -1     ;
179     case GLUT_INIT_WINDOW_HEIGHT:   return fgState.Size.Use ?
180                                            fgState.Size.Y : -1     ;
181     case GLUT_INIT_DISPLAY_MODE:    return fgState.DisplayMode     ;
182     case GLUT_INIT_MAJOR_VERSION:   return fgState.MajorVersion    ;
183     case GLUT_INIT_MINOR_VERSION:   return fgState.MinorVersion    ;
184     case GLUT_INIT_FLAGS:           return fgState.ContextFlags    ;
185     case GLUT_INIT_PROFILE:         return fgState.ContextProfile  ;
186
187 #if TARGET_HOST_POSIX_X11
188     /*
189      * The window/context specific queries are handled mostly by
190      * fghGetConfig().
191      */
192     case GLUT_WINDOW_NUM_SAMPLES:
193 #ifdef GLX_VERSION_1_3
194         glGetIntegerv(GL_SAMPLES, &nsamples);
195 #endif
196         return nsamples;
197
198     /*
199      * The rest of GLX queries under X are general enough to use a macro to
200      * check them
201      */
202 #   define GLX_QUERY(a,b) case a: return fghGetConfig( b );
203
204     GLX_QUERY( GLUT_WINDOW_RGBA,                GLX_RGBA                );
205     GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER,        GLX_DOUBLEBUFFER        );
206     GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE,         GLX_BUFFER_SIZE         );
207     GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE,        GLX_STENCIL_SIZE        );
208     GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE,          GLX_DEPTH_SIZE          );
209     GLX_QUERY( GLUT_WINDOW_RED_SIZE,            GLX_RED_SIZE            );
210     GLX_QUERY( GLUT_WINDOW_GREEN_SIZE,          GLX_GREEN_SIZE          );
211     GLX_QUERY( GLUT_WINDOW_BLUE_SIZE,           GLX_BLUE_SIZE           );
212     GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE,          GLX_ALPHA_SIZE          );
213     GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE,      GLX_ACCUM_RED_SIZE      );
214     GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE,    GLX_ACCUM_GREEN_SIZE    );
215     GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE,     GLX_ACCUM_BLUE_SIZE     );
216     GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE,    GLX_ACCUM_ALPHA_SIZE    );
217     GLX_QUERY( GLUT_WINDOW_STEREO,              GLX_STEREO              );
218
219 #   undef GLX_QUERY
220
221     /* Colormap size is handled in a bit different way than all the rest */
222     case GLUT_WINDOW_COLORMAP_SIZE:
223         if( (fghGetConfig( GLX_RGBA )) || (fgStructure.CurrentWindow == NULL) )
224         {
225             /*
226              * We've got a RGBA visual, so there is no colormap at all.
227              * The other possibility is that we have no current window set.
228              */
229             return 0;
230         }
231         else
232         {
233           const GLXFBConfig * fbconfig =
234                 fgStructure.CurrentWindow->Window.FBConfig;
235
236           XVisualInfo * visualInfo =
237                 glXGetVisualFromFBConfig( fgDisplay.Display, *fbconfig );
238
239           const int result = visualInfo->visual->map_entries;
240
241           XFree(visualInfo);
242
243           return result;
244         }
245
246     /*
247      * Those calls are somewhat similiar, as they use XGetWindowAttributes()
248      * function
249      */
250     case GLUT_WINDOW_X:
251     case GLUT_WINDOW_Y:
252     case GLUT_WINDOW_BORDER_WIDTH:
253     case GLUT_WINDOW_HEADER_HEIGHT:
254     {
255         int x, y;
256         Window w;
257
258         if( fgStructure.CurrentWindow == NULL )
259             return 0;
260
261         XTranslateCoordinates(
262             fgDisplay.Display,
263             fgStructure.CurrentWindow->Window.Handle,
264             fgDisplay.RootWindow,
265             0, 0, &x, &y, &w);
266
267         switch ( eWhat )
268         {
269         case GLUT_WINDOW_X: return x;
270         case GLUT_WINDOW_Y: return y;
271         }
272
273         if ( w == 0 )
274             return 0;
275         XTranslateCoordinates(
276             fgDisplay.Display,
277             fgStructure.CurrentWindow->Window.Handle,
278             w, 0, 0, &x, &y, &w);
279
280         switch ( eWhat )
281         {
282         case GLUT_WINDOW_BORDER_WIDTH:  return x;
283         case GLUT_WINDOW_HEADER_HEIGHT: return y;
284         }
285     }
286
287     case GLUT_WINDOW_WIDTH:
288     case GLUT_WINDOW_HEIGHT:
289     {
290         XWindowAttributes winAttributes;
291
292         if( fgStructure.CurrentWindow == NULL )
293             return 0;
294         XGetWindowAttributes(
295             fgDisplay.Display,
296             fgStructure.CurrentWindow->Window.Handle,
297             &winAttributes
298         );
299         switch ( eWhat )
300         {
301         case GLUT_WINDOW_WIDTH:            return winAttributes.width ;
302         case GLUT_WINDOW_HEIGHT:           return winAttributes.height ;
303         }
304     }
305
306     /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
307     case GLUT_DISPLAY_MODE_POSSIBLE:
308     {
309         /*  We should not have to call fgChooseFBConfig again here.  */
310         GLXFBConfig * fbconfig;
311         int isPossible;
312
313         fbconfig = fgChooseFBConfig(NULL);
314
315         if (fbconfig == NULL)
316         {
317             isPossible = 0;
318         }
319         else
320         {
321             isPossible = 1;
322             XFree(fbconfig);
323         }
324
325         return isPossible;
326     }
327
328     /* This is system-dependant */
329     case GLUT_WINDOW_FORMAT_ID:
330         if( fgStructure.CurrentWindow == NULL )
331             return 0;
332
333         return fghGetConfig( GLX_VISUAL_ID );
334
335 #elif TARGET_HOST_MS_WINDOWS
336
337     case GLUT_WINDOW_NUM_SAMPLES:
338       glGetIntegerv(WGL_SAMPLES_ARB, &nsamples);
339       return nsamples;
340
341     /* Handle the OpenGL inquiries */
342     case GLUT_WINDOW_RGBA:
343 #if defined(_WIN32_WCE)
344       boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
345 #else
346       glGetBooleanv ( GL_RGBA_MODE, &boolValue );
347       returnValue = boolValue ? 1 : 0;
348 #endif
349       return returnValue;
350     case GLUT_WINDOW_DOUBLEBUFFER:
351 #if defined(_WIN32_WCE)
352       boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
353 #else
354       glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
355       returnValue = boolValue ? 1 : 0;
356 #endif
357       return returnValue;
358     case GLUT_WINDOW_STEREO:
359 #if defined(_WIN32_WCE)
360       boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
361 #else
362       glGetBooleanv ( GL_STEREO, &boolValue );
363       returnValue = boolValue ? 1 : 0;
364 #endif
365       return returnValue;
366
367     case GLUT_WINDOW_RED_SIZE:
368       glGetIntegerv ( GL_RED_BITS, &returnValue );
369       return returnValue;
370     case GLUT_WINDOW_GREEN_SIZE:
371       glGetIntegerv ( GL_GREEN_BITS, &returnValue );
372       return returnValue;
373     case GLUT_WINDOW_BLUE_SIZE:
374       glGetIntegerv ( GL_BLUE_BITS, &returnValue );
375       return returnValue;
376     case GLUT_WINDOW_ALPHA_SIZE:
377       glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
378       return returnValue;
379     case GLUT_WINDOW_ACCUM_RED_SIZE:
380 #if defined(_WIN32_WCE)
381       returnValue = 0;  /* WinCE doesn't support this feature */
382 #else
383       glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
384 #endif
385       return returnValue;
386     case GLUT_WINDOW_ACCUM_GREEN_SIZE:
387 #if defined(_WIN32_WCE)
388       returnValue = 0;  /* WinCE doesn't support this feature */
389 #else
390       glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
391 #endif
392       return returnValue;
393     case GLUT_WINDOW_ACCUM_BLUE_SIZE:
394 #if defined(_WIN32_WCE)
395       returnValue = 0;  /* WinCE doesn't support this feature */
396 #else
397       glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
398 #endif
399       return returnValue;
400     case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
401 #if defined(_WIN32_WCE)
402       returnValue = 0;  /* WinCE doesn't support this feature */
403 #else
404       glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
405 #endif
406       return returnValue;
407     case GLUT_WINDOW_DEPTH_SIZE:
408       glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
409       return returnValue;
410
411     case GLUT_WINDOW_BUFFER_SIZE:
412       returnValue = 1 ;                                      /* ????? */
413       return returnValue;
414     case GLUT_WINDOW_STENCIL_SIZE:
415       returnValue = 0 ;                                      /* ????? */
416       return returnValue;
417
418     case GLUT_WINDOW_X:
419     case GLUT_WINDOW_Y:
420     case GLUT_WINDOW_WIDTH:
421     case GLUT_WINDOW_HEIGHT:
422     {
423         /*
424          *  There is considerable confusion about the "right thing to
425          *  do" concerning window  size and position.  GLUT itself is
426          *  not consistent between Windows and UNIX/X11; since
427          *  platform independence is a virtue for "freeglut", we
428          *  decided to break with GLUT's behaviour.
429          *
430          *  Under UNIX/X11, it is apparently not possible to get the
431          *  window border sizes in order to subtract them off the
432          *  window's initial position until some time after the window
433          *  has been created.  Therefore we decided on the following
434          *  behaviour, both under Windows and under UNIX/X11:
435          *  - When you create a window with position (x,y) and size
436          *    (w,h), the upper left hand corner of the outside of the
437          *    window is at (x,y) and the size of the drawable area  is
438          *    (w,h).
439          *  - When you query the size and position of the window--as
440          *    is happening here for Windows--"freeglut" will return
441          *    the size of the drawable area--the (w,h) that you
442          *    specified when you created the window--and the coordinates
443          *    of the upper left hand corner of the drawable
444          *    area--which is NOT the (x,y) you specified.
445          */
446
447         RECT winRect;
448
449         freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
450
451 #if defined(_WIN32_WCE)
452         GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
453 #else
454         winRect = fghGetClientArea(fgStructure.CurrentWindow, FALSE);
455 #endif /* defined(_WIN32_WCE) */
456
457         switch( eWhat )
458         {
459         case GLUT_WINDOW_X:      return winRect.left                ;
460         case GLUT_WINDOW_Y:      return winRect.top                 ;
461         case GLUT_WINDOW_WIDTH:  return winRect.right - winRect.left;
462         case GLUT_WINDOW_HEIGHT: return winRect.bottom - winRect.top;
463         }
464     }
465     break;
466
467     case GLUT_WINDOW_BORDER_WIDTH :
468     case GLUT_WINDOW_HEADER_HEIGHT :
469 #if defined(_WIN32_WCE)
470         return 0;
471 #else
472         {
473             DWORD windowStyle;
474
475             if (fgStructure.CurrentWindow && fgStructure.CurrentWindow->Window.Handle)
476                 windowStyle = GetWindowLong(fgStructure.CurrentWindow->Window.Handle, GWL_STYLE);
477             else
478                 /* If no window, return sizes for a default window with title bar and border */
479                 windowStyle = WS_OVERLAPPEDWINDOW;
480             
481             switch( eWhat )
482             {
483             case GLUT_WINDOW_BORDER_WIDTH:
484                 {
485                     int xBorderWidth, yBorderWidth;
486                     fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
487                     return xBorderWidth;
488                 }
489             case GLUT_WINDOW_HEADER_HEIGHT:
490                 /* Need to query for WS_MAXIMIZEBOX to see if we have a title bar, the WS_CAPTION query is also true for a WS_DLGFRAME only... */
491                 return (windowStyle & WS_MAXIMIZEBOX)? GetSystemMetrics( SM_CYCAPTION ) : 0;
492             }
493         }
494 #endif /* defined(_WIN32_WCE) */
495
496     case GLUT_DISPLAY_MODE_POSSIBLE:
497 #if defined(_WIN32_WCE)
498         return 0;
499 #else
500         return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
501                                     PFD_MAIN_PLANE );
502 #endif /* defined(_WIN32_WCE) */
503
504
505     case GLUT_WINDOW_FORMAT_ID:
506 #if !defined(_WIN32_WCE)
507         if( fgStructure.CurrentWindow != NULL )
508             return GetPixelFormat( fgStructure.CurrentWindow->Window.Device );
509 #endif /* defined(_WIN32_WCE) */
510         return 0;
511
512 #endif
513
514     /* The window structure queries */
515     case GLUT_WINDOW_PARENT:
516         if( fgStructure.CurrentWindow         == NULL ) return 0;
517         if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
518         return fgStructure.CurrentWindow->Parent->ID;
519
520     case GLUT_WINDOW_NUM_CHILDREN:
521         if( fgStructure.CurrentWindow == NULL )
522             return 0;
523         return fgListLength( &fgStructure.CurrentWindow->Children );
524
525     case GLUT_WINDOW_CURSOR:
526         if( fgStructure.CurrentWindow == NULL )
527             return 0;
528         return fgStructure.CurrentWindow->State.Cursor;
529
530     case GLUT_MENU_NUM_ITEMS:
531         if( fgStructure.CurrentMenu == NULL )
532             return 0;
533         return fgListLength( &fgStructure.CurrentMenu->Entries );
534
535     case GLUT_ACTION_ON_WINDOW_CLOSE:
536         return fgState.ActionOnWindowClose;
537
538     case GLUT_VERSION :
539         return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
540
541     case GLUT_RENDERING_CONTEXT:
542         return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
543                                          : GLUT_CREATE_NEW_CONTEXT;
544
545     case GLUT_DIRECT_RENDERING:
546         return fgState.DirectContext;
547
548     case GLUT_FULL_SCREEN:
549         return fgStructure.CurrentWindow->State.IsFullscreen;
550
551     case GLUT_AUX:
552       return fgState.AuxiliaryBufferNumber;
553
554     case GLUT_MULTISAMPLE:
555       return fgState.SampleNumber;
556
557     default:
558         fgWarning( "glutGet(): missing enum handle %d", eWhat );
559         break;
560     }
561     return -1;
562 }
563
564 /*
565  * Returns various device information.
566  */
567 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
568 {
569     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
570
571     /* XXX WARNING: we are mostly lying in this function. */
572     switch( eWhat )
573     {
574     case GLUT_HAS_KEYBOARD:
575         /*
576          * Win32 is assumed a keyboard, and this cannot be queried,
577          * except for WindowsCE.
578          *
579          * X11 has a core keyboard by definition, although it can
580          * be present as a virtual/dummy keyboard. For now, there
581          * is no reliable way to tell if a real keyboard is present.
582          */
583 #if defined(_WIN32_CE)
584         return ( GetKeyboardStatus() & KBDI_KEYBOARD_PRESENT ) ? 1 : 0;
585 #   if FREEGLUT_LIB_PRAGMAS
586 #       pragma comment (lib,"Kbdui.lib")
587 #   endif
588
589 #else
590         return 1;
591 #endif
592
593 #if TARGET_HOST_POSIX_X11
594
595     /* X11 has a mouse by definition */
596     case GLUT_HAS_MOUSE:
597         return 1 ;
598
599     case GLUT_NUM_MOUSE_BUTTONS:
600         /* We should be able to pass NULL when the last argument is zero,
601          * but at least one X server has a bug where this causes a segfault.
602          *
603          * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
604          * rather than an Axis; "freeglut_main.c" expects this when
605          * checking for a wheel event.
606          */
607         {
608             unsigned char map;
609             int nbuttons = XGetPointerMapping(fgDisplay.Display, &map,0);
610             return nbuttons;
611         }
612
613 #elif TARGET_HOST_MS_WINDOWS
614
615     case GLUT_HAS_MOUSE:
616         /*
617          * MS Windows can be booted without a mouse.
618          */
619         return GetSystemMetrics( SM_MOUSEPRESENT );
620
621     case GLUT_NUM_MOUSE_BUTTONS:
622 #  if defined(_WIN32_WCE)
623         return 1;
624 #  else
625         return GetSystemMetrics( SM_CMOUSEBUTTONS );
626 #  endif
627 #endif
628
629     case GLUT_HAS_JOYSTICK:
630         return fgJoystickDetect ();
631
632     case GLUT_OWNS_JOYSTICK:
633         return fgState.JoysticksInitialised;
634
635     case GLUT_JOYSTICK_POLL_RATE:
636         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
637
638     /* XXX The following two are only for Joystick 0 but this is an improvement */
639     case GLUT_JOYSTICK_BUTTONS:
640         return glutJoystickGetNumButtons ( 0 );
641
642     case GLUT_JOYSTICK_AXES:
643         return glutJoystickGetNumAxes ( 0 );
644
645     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
646         return fgInputDeviceDetect ();
647
648     case GLUT_NUM_DIALS:
649         if ( fgState.InputDevsInitialised ) return 8;
650         return 0;
651  
652     case GLUT_NUM_BUTTON_BOX_BUTTONS:
653         return 0;
654
655     case GLUT_HAS_SPACEBALL:
656         return fgHasSpaceball();
657
658     case GLUT_HAS_TABLET:
659         return 0;
660
661     case GLUT_NUM_SPACEBALL_BUTTONS:
662         return fgSpaceballNumButtons();
663
664     case GLUT_NUM_TABLET_BUTTONS:
665         return 0;
666
667     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
668         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
669
670     case GLUT_DEVICE_KEY_REPEAT:
671         return fgState.KeyRepeat;
672
673     default:
674         fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
675         break;
676     }
677
678     /* And now -- the failure. */
679     return -1;
680 }
681
682 /*
683  * This should return the current state of ALT, SHIFT and CTRL keys.
684  */
685 int FGAPIENTRY glutGetModifiers( void )
686 {
687     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
688     if( fgState.Modifiers == INVALID_MODIFIERS )
689     {
690         fgWarning( "glutGetModifiers() called outside an input callback" );
691         return 0;
692     }
693
694     return fgState.Modifiers;
695 }
696
697 /*
698  * Return the state of the GLUT API overlay subsystem. A misery ;-)
699  */
700 int FGAPIENTRY glutLayerGet( GLenum eWhat )
701 {
702     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
703
704     /*
705      * This is easy as layers are not implemented ;-)
706      *
707      * XXX Can we merge the UNIX/X11 and WIN32 sections?  Or
708      * XXX is overlay support planned?
709      */
710     switch( eWhat )
711     {
712
713 #if TARGET_HOST_POSIX_X11
714
715     case GLUT_OVERLAY_POSSIBLE:
716         return 0;
717
718     case GLUT_LAYER_IN_USE:
719         return GLUT_NORMAL;
720
721     case GLUT_HAS_OVERLAY:
722         return 0;
723
724     case GLUT_TRANSPARENT_INDEX:
725         /*
726          * Return just anything, which is always defined as zero
727          *
728          * XXX HUH?
729          */
730         return 0;
731
732     case GLUT_NORMAL_DAMAGED:
733         /* XXX Actually I do not know. Maybe. */
734         return 0;
735
736     case GLUT_OVERLAY_DAMAGED:
737         return -1;
738
739 #elif TARGET_HOST_MS_WINDOWS
740
741     case GLUT_OVERLAY_POSSIBLE:
742 /*      return fgSetupPixelFormat( fgStructure.CurrentWindow, GL_TRUE,
743                                    PFD_OVERLAY_PLANE ); */
744       return 0 ;
745
746     case GLUT_LAYER_IN_USE:
747         return GLUT_NORMAL;
748
749     case GLUT_HAS_OVERLAY:
750         return 0;
751
752     case GLUT_TRANSPARENT_INDEX:
753         /*
754          * Return just anything, which is always defined as zero
755          *
756          * XXX HUH?
757          */
758         return 0;
759
760     case GLUT_NORMAL_DAMAGED:
761         /* XXX Actually I do not know. Maybe. */
762         return 0;
763
764     case GLUT_OVERLAY_DAMAGED:
765         return -1;
766 #endif
767
768     default:
769         fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
770         break;
771     }
772
773     /* And fail. That's good. Programs do love failing. */
774     return -1;
775 }
776
777 int * FGAPIENTRY glutGetModeValues(GLenum eWhat, int * size)
778 {
779   int * array;
780
781 #if TARGET_HOST_POSIX_X11
782   int attributes[9];
783   GLXFBConfig * fbconfigArray;  /*  Array of FBConfigs  */
784   int fbconfigArraySize;        /*  Number of FBConfigs in the array  */
785   int attribute_name = 0;
786 #endif
787
788   FREEGLUT_EXIT_IF_NOT_INITIALISED("glutGetModeValues");
789
790   array = NULL;
791   *size = 0;
792
793   switch (eWhat)
794     {
795 #if TARGET_HOST_POSIX_X11
796     case GLUT_AUX:
797     case GLUT_MULTISAMPLE:
798
799       attributes[0] = GLX_BUFFER_SIZE;
800       attributes[1] = GLX_DONT_CARE;
801
802       switch (eWhat)
803         {
804         case GLUT_AUX:
805           /*
806             FBConfigs are now sorted by increasing number of auxiliary
807             buffers.  We want at least one buffer.
808           */
809           attributes[2] = GLX_AUX_BUFFERS;
810           attributes[3] = 1;
811           attributes[4] = None;
812
813           attribute_name = GLX_AUX_BUFFERS;
814
815           break;
816
817
818         case GLUT_MULTISAMPLE:
819           attributes[2] = GLX_AUX_BUFFERS;
820           attributes[3] = GLX_DONT_CARE;
821           attributes[4] = GLX_SAMPLE_BUFFERS;
822           attributes[5] = 1;
823           /*
824             FBConfigs are now sorted by increasing number of samples per
825             pixel.  We want at least one sample.
826           */
827           attributes[6] = GLX_SAMPLES;
828           attributes[7] = 1;
829           attributes[8] = None;
830
831           attribute_name = GLX_SAMPLES;
832
833           break;
834         }
835
836       fbconfigArray = glXChooseFBConfig(fgDisplay.Display,
837                                         fgDisplay.Screen,
838                                         attributes,
839                                         &fbconfigArraySize);
840
841       if (fbconfigArray != NULL)
842         {
843           int * temp_array;
844           int result;   /*  Returned by glXGetFBConfigAttrib. Not checked.  */
845           int previous_value;
846           int i;
847
848           temp_array = malloc(sizeof(int) * fbconfigArraySize);
849           previous_value = 0;
850
851           for (i = 0; i < fbconfigArraySize; i++)
852             {
853               int value;
854
855               result = glXGetFBConfigAttrib(fgDisplay.Display,
856                                             fbconfigArray[i],
857                                             attribute_name,
858                                             &value);
859               if (value > previous_value)
860                 {
861                   temp_array[*size] = value;
862                   previous_value = value;
863                   (*size)++;
864                 }
865             }
866
867           array = malloc(sizeof(int) * (*size));
868           for (i = 0; i < *size; i++)
869             {
870               array[i] = temp_array[i];
871             }
872
873           free(temp_array);
874           XFree(fbconfigArray);
875         }
876
877       break;
878 #endif      
879
880     default:
881       break;
882     }
883
884   return array;
885 }
886
887 /*** END OF FILE ***/