affa1ee7de3e493beb125d96dbec78eca84180ad
[freeglut] / freeglut-1.3 / 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-state"
33
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  glutGet()               -- X11 tests passed, but check if all enums handled (what about Win32?)
41  *  glutDeviceGet()         -- X11 tests passed, but check if all enums handled (what about Win32?)
42  *  glutGetModifiers()      -- OK, but could also remove the limitation
43  *  glutLayerGet()          -- what about GLUT_NORMAL_DAMAGED?
44  *
45  * The fail-on-call policy will help adding the most needed things imho.
46  */
47
48 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
49
50 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
51
52 #if TARGET_HOST_UNIX_X11
53 /*
54  * Queries the GL context about some attributes
55  */
56 static int fghGetConfig( int attribute )
57 {
58   int returnValue ;
59
60   /*
61    * Return nothing if there is no current window set
62    */
63   if( fgStructure.Window == NULL )
64     return( 0 );
65
66   /*
67    * glXGetConfig should work fine
68    */
69   glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo, attribute, &returnValue );
70
71
72   /*
73    * Have the query results returned
74    */
75   return ( returnValue ) ;
76 }
77 #endif
78
79 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
80
81 /*
82  * General settings assignment method
83  */
84 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
85 {
86   freeglut_assert_ready;
87
88   /*
89    * Check what is the caller querying for. In chronological code add order.
90    */
91   switch( eWhat )
92   {
93   case GLUT_INIT_WINDOW_X:          fgState.Position.X          = (GLint)value ;
94                                     break ;
95   case GLUT_INIT_WINDOW_Y:          fgState.Position.Y          = (GLint)value ;
96                                     break ;
97   case GLUT_INIT_WINDOW_WIDTH:      fgState.Size.X              = (GLint)value ;
98                                     break ;
99   case GLUT_INIT_WINDOW_HEIGHT:     fgState.Size.Y              = (GLint)value ;
100                                     break ;
101   case GLUT_INIT_DISPLAY_MODE:      fgState.DisplayMode         = (unsigned int)value ;
102                                     break ;
103
104   case GLUT_ACTION_ON_WINDOW_CLOSE: fgState.ActionOnWindowClose = value ;
105                                     break ;
106
107   case GLUT_WINDOW_CURSOR:
108       if( fgStructure.Window != NULL ) fgStructure.Window->State.Cursor = value ;
109       break ;
110
111   default:
112       /*
113        * Just have it reported, so that we can see what needs to be implemented
114        */
115       fgWarning( "glutSetOption(): missing enum handle %i\n", eWhat );
116       break;
117   }
118 }
119
120 /*
121  * General settings query method
122  */
123 int FGAPIENTRY glutGet( GLenum eWhat )
124 {
125   int returnValue ;
126   GLboolean boolValue ;
127     freeglut_assert_ready;
128
129     /*
130      * Check what is the caller querying for. In chronological code add order.
131      */
132     switch( eWhat )
133     {
134     case GLUT_ELAPSED_TIME:
135         /*
136          * This is easy and nicely portable, as we are using GLib...
137          */
138         return( fgElapsedTime() );
139
140     /*
141      * Following values are stored in fgState and fgDisplay global structures
142      */
143     case GLUT_SCREEN_WIDTH:         return( fgDisplay.ScreenWidth    );
144     case GLUT_SCREEN_HEIGHT:        return( fgDisplay.ScreenHeight   );
145     case GLUT_SCREEN_WIDTH_MM:      return( fgDisplay.ScreenWidthMM  );
146     case GLUT_SCREEN_HEIGHT_MM:     return( fgDisplay.ScreenHeightMM );
147     case GLUT_INIT_WINDOW_X:        return( fgState.Position.X       );
148     case GLUT_INIT_WINDOW_Y:        return( fgState.Position.Y       );
149     case GLUT_INIT_WINDOW_WIDTH:    return( fgState.Size.X           );
150     case GLUT_INIT_WINDOW_HEIGHT:   return( fgState.Size.Y           );
151     case GLUT_INIT_DISPLAY_MODE:    return( fgState.DisplayMode      );
152
153     /*
154      * The window/context specific queries are handled mostly by fghGetConfig().
155      */
156     case GLUT_WINDOW_NUM_SAMPLES:
157         /*
158          * Multisampling. Return what I know about multisampling.
159          */
160         return( 0 );
161
162 #if TARGET_HOST_UNIX_X11
163     /*
164      * The rest of GLX queries under X are general enough to use a macro to check them
165      */
166 #   define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
167
168     GLX_QUERY( GLUT_WINDOW_RGBA,                GLX_RGBA                );
169     GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER,        GLX_DOUBLEBUFFER        );
170     GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE,         GLX_BUFFER_SIZE         );
171     GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE,        GLX_STENCIL_SIZE        );
172     GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE,          GLX_DEPTH_SIZE          );
173     GLX_QUERY( GLUT_WINDOW_RED_SIZE,            GLX_RED_SIZE            );
174     GLX_QUERY( GLUT_WINDOW_GREEN_SIZE,          GLX_GREEN_SIZE          );
175     GLX_QUERY( GLUT_WINDOW_BLUE_SIZE,           GLX_BLUE_SIZE           );
176     GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE,          GLX_ALPHA_SIZE          );
177     GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE,      GLX_ACCUM_RED_SIZE      );
178     GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE,    GLX_ACCUM_GREEN_SIZE    );
179     GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE,     GLX_ACCUM_BLUE_SIZE     );
180     GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE,    GLX_ACCUM_ALPHA_SIZE    );
181     GLX_QUERY( GLUT_WINDOW_STEREO,              GLX_STEREO              );
182
183 #   undef GLX_QUERY
184
185     /*
186      * Colormap size is handled in a bit different way than all the rest
187      */
188     case GLUT_WINDOW_COLORMAP_SIZE:
189         /*
190          * Check for the visual type
191          */
192         if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
193         {
194             /*
195              * We've got a RGBA visual, so there is no colormap at all.
196              * The other possibility is that we have no current window set.
197              */
198             return( 0 );
199         }
200
201         /*
202          * Otherwise return the number of entries in the colormap
203          */
204         return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
205
206     /*
207      * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
208      */
209     case GLUT_WINDOW_X:
210     case GLUT_WINDOW_Y:
211     {
212         XWindowAttributes winAttributes;
213         Window another, window;
214         int x, y;
215
216         /*
217          * Return zero if there is no current window set
218          */
219         if( fgStructure.Window == NULL )
220             return( 0 );
221
222         /*
223          * So, grab the current window's position
224          */
225         window = fgStructure.Window->Window.Handle;
226
227         /*
228          * Grab the current window's attributes now
229          */
230         XGetWindowAttributes(
231             fgDisplay.Display,
232             window,
233             &winAttributes
234         );
235
236         /*
237          * Correct the results for the parental relation and border size
238          */
239         XTranslateCoordinates(
240             fgDisplay.Display,
241             window,
242             winAttributes.root,
243             -winAttributes.border_width,
244             -winAttributes.border_width,
245             &x, &y,
246             &another
247         );
248
249         /*
250          * See if we have to return the X or Y coordinate
251          */
252         return( eWhat == GLUT_WINDOW_X ? x : y );
253     }
254
255     case GLUT_WINDOW_WIDTH:
256     case GLUT_WINDOW_HEIGHT:
257     {
258         XWindowAttributes winAttributes;
259
260         /*
261          * Return zero if there is no current window set
262          */
263         if( fgStructure.Window == NULL )
264             return( 0 );
265
266         /*
267          * Checking for window's size is much easier:
268          */
269         XGetWindowAttributes(
270             fgDisplay.Display,
271             fgStructure.Window->Window.Handle,
272             &winAttributes
273         );
274
275         /*
276          * See if to return the window's width or height
277          */
278         return( eWhat == GLUT_WINDOW_WIDTH ? winAttributes.width : winAttributes.height );
279     }
280
281     /*
282      * I do not know yet if there will be a fgChooseVisual() function for Win32
283      */
284     case GLUT_DISPLAY_MODE_POSSIBLE:
285         /*
286          * Check if the current display mode is possible
287          */
288         return( fgChooseVisual() == NULL ? 0 : 1 );
289
290     /*
291      * This is system-dependant
292      */
293     case GLUT_WINDOW_FORMAT_ID:
294         /*
295          * Return the visual ID, if there is a current window naturally:
296          */
297         if( fgStructure.Window == NULL )
298             return( 0 );
299
300         return( fgStructure.Window->Window.VisualInfo->visualid );
301
302 #elif TARGET_HOST_WIN32
303
304     /*
305      * Handle the OpenGL inquiries
306      */
307     case GLUT_WINDOW_RGBA:
308       glGetBooleanv ( GL_RGBA_MODE, &boolValue ) ;         /* True if color buffers store RGBA */
309       returnValue = boolValue ? 1 : 0 ;
310       return ( returnValue ) ;
311     case GLUT_WINDOW_DOUBLEBUFFER:
312       glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue ) ;      /* True if front and back buffers exist */
313       returnValue = boolValue ? 1 : 0 ;
314       return ( returnValue ) ;
315     case GLUT_WINDOW_STEREO:
316       glGetBooleanv ( GL_STEREO, &boolValue ) ;            /* True if left and right buffers exist */
317       returnValue = boolValue ? 1 : 0 ;
318       return ( returnValue ) ;
319
320     case GLUT_WINDOW_RED_SIZE:
321       glGetIntegerv ( GL_RED_BITS, &returnValue ) ;          /* Number of bits per red component in color buffers */
322       return ( returnValue ) ;
323     case GLUT_WINDOW_GREEN_SIZE:
324       glGetIntegerv ( GL_GREEN_BITS, &returnValue ) ;        /* Number of bits per green component in color buffers */
325       return ( returnValue ) ;
326     case GLUT_WINDOW_BLUE_SIZE:
327       glGetIntegerv ( GL_BLUE_BITS, &returnValue ) ;         /* Number of bits per blue component in color buffers */
328       return ( returnValue ) ;
329     case GLUT_WINDOW_ALPHA_SIZE:
330       glGetIntegerv ( GL_ALPHA_BITS, &returnValue ) ;        /* Number of bits per alpha component in color buffers */
331       return ( returnValue ) ;
332     case GLUT_WINDOW_ACCUM_RED_SIZE:
333       glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue ) ;    /* Number of bits per red component in the accumulation buffer */
334       return ( returnValue ) ;
335     case GLUT_WINDOW_ACCUM_GREEN_SIZE:
336       glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue ) ;  /* Number of bits per green component in the accumulation buffer */
337       return ( returnValue ) ;
338     case GLUT_WINDOW_ACCUM_BLUE_SIZE:
339       glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue ) ;   /* Number of bits per blue component in the accumulation buffer */
340       return ( returnValue ) ;
341     case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
342       glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue ) ;  /* Number of bits per alpha component in the accumulation buffer */
343       return ( returnValue ) ;
344     case GLUT_WINDOW_DEPTH_SIZE:
345       glGetIntegerv ( GL_DEPTH_BITS, &returnValue ) ;        /* Number of depth-buffer bitplanes */
346       return ( returnValue ) ;
347
348     case GLUT_WINDOW_BUFFER_SIZE:
349       returnValue = 1 ;                                      /* ????? */
350       return ( returnValue ) ;
351     case GLUT_WINDOW_STENCIL_SIZE:
352       returnValue = 0 ;                                      /* ????? */
353       return ( returnValue ) ;
354
355     /*
356      * Window position and size
357      */
358     case GLUT_WINDOW_X:
359     case GLUT_WINDOW_Y:
360     case GLUT_WINDOW_WIDTH:
361     case GLUT_WINDOW_HEIGHT:
362     {
363         RECT winRect;
364
365         /*
366          * Check if there is a window to be queried for dimensions:
367          */
368         freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
369
370         /*
371          * We need to call GetWindowRect() first...
372          */
373         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
374
375         /*
376          * ...then we've got to correct the results we've just received...
377          */
378         winRect.left   += GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
379         winRect.right  -= GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
380         winRect.top    += GetSystemMetrics( SM_CYSIZEFRAME ) - 1 + GetSystemMetrics( SM_CYCAPTION );
381         winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME ) + 1;
382
383         /*
384          * ...and finally return the caller the desired value:
385          */
386         switch( eWhat )
387         {
388         case GLUT_WINDOW_X:      return( winRect.left                 );
389         case GLUT_WINDOW_Y:      return( winRect.top                  );
390         case GLUT_WINDOW_WIDTH:  return( winRect.right - winRect.left );
391         case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
392         }
393     }
394     break;
395
396     case GLUT_DISPLAY_MODE_POSSIBLE:
397         /*
398          * Check if the current display mode is possible
399          */
400         return( fgSetupPixelFormat( fgStructure.Window, TRUE ) );
401
402     case GLUT_WINDOW_FORMAT_ID:
403         /*
404          * Return the pixel format of the current window
405          */
406         if( fgStructure.Window != NULL )
407             return( GetPixelFormat( fgStructure.Window->Window.Device ) );
408
409         /*
410          * If the current window does not exist, fail:
411          */
412         return( 0 );
413
414 #endif
415
416     /*
417      * The window structure queries
418      */
419     case GLUT_WINDOW_PARENT:
420         /*
421          * Return the ID number of current window's parent, if any
422          */
423         if( fgStructure.Window         == NULL ) return( 0 );
424         if( fgStructure.Window->Parent == NULL ) return( 0 );
425
426         return( fgStructure.Window->Parent->ID );
427
428     case GLUT_WINDOW_NUM_CHILDREN:
429         /*
430          * Return the number of children attached to the current window
431          */
432         if( fgStructure.Window == NULL )
433             return( 0 );
434
435         return( fgListLength( &fgStructure.Window->Children ) );
436
437     case GLUT_WINDOW_CURSOR:
438         /*
439          * Return the currently selected window cursor
440          */
441         if( fgStructure.Window == NULL )
442             return( 0 );
443
444         return( fgStructure.Window->State.Cursor );
445
446     case GLUT_MENU_NUM_ITEMS:
447         /*
448          * Return the number of menu entries in the current menu
449          */
450         if( fgStructure.Menu == NULL )
451             return( 0 );
452
453         return( fgListLength( &fgStructure.Menu->Entries ) );
454
455     case GLUT_ACTION_ON_WINDOW_CLOSE: return ( fgState.ActionOnWindowClose ) ;
456
457     default:
458         /*
459          * Just have it reported, so that we can see what needs to be implemented
460          */
461         fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
462         break;
463     }
464
465     /*
466      * If nothing happens, then we are in deep trouble...
467      */
468     return( -1 );
469 }
470
471 /*
472  * Returns various device information.
473  */
474 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
475 {
476     freeglut_assert_ready;
477
478     /*
479      * See why are we bothered...
480      *
481      * WARNING: we are mostly lying in this function.
482      */
483     switch( eWhat )
484     {
485     case GLUT_HAS_KEYBOARD:
486         /*
487          * We always have a keyboard present on PC machines...
488          */
489         return( TRUE );
490
491 #if TARGET_HOST_UNIX_X11
492
493     case GLUT_HAS_MOUSE:
494         /*
495          * Hey, my Atari 65XE hasn't had a mouse!
496          */
497         return( TRUE );
498
499     case GLUT_NUM_MOUSE_BUTTONS:
500         /*
501          * Return the number of mouse buttons available. This is a big guess.
502          */
503         return( 3 );
504
505 #elif TARGET_HOST_WIN32
506
507     case GLUT_HAS_MOUSE:
508         /*
509          * The Windows can be booted without a mouse. 
510          * It would be nice to have this reported.
511          */
512         return( GetSystemMetrics( SM_MOUSEPRESENT ) );
513
514     case GLUT_NUM_MOUSE_BUTTONS:
515         /*
516          * We are much more fortunate under Win32 about this...
517          */
518         return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
519
520 #endif
521
522     case GLUT_JOYSTICK_POLL_RATE:
523     case GLUT_HAS_JOYSTICK:
524     case GLUT_JOYSTICK_BUTTONS:
525     case GLUT_JOYSTICK_AXES:
526         /*
527          * WARNING: THIS IS A BIG LIE!
528          */
529         return( 0 );
530
531     case GLUT_HAS_SPACEBALL:
532     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
533     case GLUT_HAS_TABLET:
534         /*
535          * Sounds cool. And unuseful.
536          */
537         return( FALSE );
538
539     case GLUT_NUM_SPACEBALL_BUTTONS:
540     case GLUT_NUM_BUTTON_BOX_BUTTONS:
541     case GLUT_NUM_DIALS:
542     case GLUT_NUM_TABLET_BUTTONS:
543         /*
544          * Zero is not the answer. Zero is the question. Continuum is the answer.
545          */
546         return( 0 );
547
548     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
549         /*
550          * Return what we think about the key auto repeat settings
551          */
552         return( fgState.IgnoreKeyRepeat );
553
554     case GLUT_DEVICE_KEY_REPEAT:
555         /*
556          * WARNING: THIS IS A BIG LIE!
557          */
558         return( GLUT_KEY_REPEAT_DEFAULT );
559
560     default:
561         /*
562          * Complain.
563          */
564         fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
565         break;
566     }
567
568     /*
569      * And now -- the failure.
570      */
571     return( -1 );
572 }
573
574 /*
575  * This should return the current state of ALT, SHIFT and CTRL keys.
576  */
577 int FGAPIENTRY glutGetModifiers( void )
578 {
579     /*
580      * Fail if there is no current window or called outside an input callback
581      */
582     if( fgStructure.Window == NULL )
583         return( 0 );
584
585     if( fgStructure.Window->State.Modifiers == 0xffffffff )
586     {
587         fgWarning( "glutGetModifiers() called outside an input callback" );
588         return( 0 );
589     }
590
591     /*
592      * Return the current modifiers state otherwise
593      */
594     return( fgStructure.Window->State.Modifiers );
595 }
596
597 /*
598  * Return the state of the GLUT API overlay subsystem. A misery ;-)
599  */
600 int FGAPIENTRY glutLayerGet( GLenum eWhat )
601 {
602     freeglut_assert_ready;
603
604     /*
605      * This is easy as layers are not implemented ;-)
606      */
607     switch( eWhat )
608     {
609     case GLUT_OVERLAY_POSSIBLE:
610         /*
611          * Nope, overlays are not possible.
612          */
613         return( FALSE );
614
615     case GLUT_LAYER_IN_USE:
616         /*
617          * The normal plane is always in use
618          */
619         return( GLUT_NORMAL );
620
621     case GLUT_HAS_OVERLAY:
622         /*
623          * No window is allowed to have an overlay
624          */
625         return( FALSE );
626
627     case GLUT_TRANSPARENT_INDEX:
628         /*
629          * Return just anything, which is always defined as zero
630          */
631         return( 0 );
632
633     case GLUT_NORMAL_DAMAGED:
634         /*
635          * Actually I do not know. Maybe.
636          */
637         return( FALSE );
638
639     case GLUT_OVERLAY_DAMAGED:
640         /*
641          * Return minus one to mark that no layer is in use
642          */
643         return( -1 );
644
645     default:
646         /*
647          * Complain to the user about the obvious bug
648          */
649         fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
650         break;
651     }
652
653     /*
654      * And fail. That's good. Programs do love failing.
655      */
656     return( -1 );
657 }
658
659 /*** END OF FILE ***/