can now also draw the normal vectors (seems very useful for instructive
[freeglut] / src / fg_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 "fg_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 extern int fgPlatformGlutGet ( GLenum eWhat );
45 extern int fgPlatformGlutDeviceGet ( GLenum eWhat );
46 extern int *fgPlatformGlutGetModeValues(GLenum eWhat, int *size);
47
48
49 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
50
51 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
52
53
54 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
55
56 /*
57  * General settings assignment method
58  */
59 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
60 {
61     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
62
63     /*
64      * XXX In chronological code add order.  (WHY in that order?)
65      */
66     switch( eWhat )
67     {
68     case GLUT_INIT_WINDOW_X:
69         fgState.Position.X = (GLint)value;
70         break;
71
72     case GLUT_INIT_WINDOW_Y:
73         fgState.Position.Y = (GLint)value;
74         break;
75
76     case GLUT_INIT_WINDOW_WIDTH:
77         fgState.Size.X = (GLint)value;
78         break;
79
80     case GLUT_INIT_WINDOW_HEIGHT:
81         fgState.Size.Y = (GLint)value;
82         break;
83
84     case GLUT_INIT_DISPLAY_MODE:
85         fgState.DisplayMode = (unsigned int)value;
86         break;
87
88     case GLUT_ACTION_ON_WINDOW_CLOSE:
89         fgState.ActionOnWindowClose = value;
90         break;
91
92     case GLUT_RENDERING_CONTEXT:
93         fgState.UseCurrentContext =
94             ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
95         break;
96
97     case GLUT_DIRECT_RENDERING:
98         fgState.DirectContext = value;
99         break;
100
101     case GLUT_WINDOW_CURSOR:
102         if( fgStructure.CurrentWindow != NULL )
103             fgStructure.CurrentWindow->State.Cursor = value;
104         break;
105
106     case GLUT_AUX:
107       fgState.AuxiliaryBufferNumber = value;
108       break;
109
110     case GLUT_MULTISAMPLE:
111       fgState.SampleNumber = value;
112       break;
113
114     case GLUT_SKIP_STALE_MOTION_EVENTS:
115       fgState.SkipStaleMotion = value;
116       break;
117
118     case GLUT_OBJECTS_VISUALIZE_NORMALS:
119       if( fgStructure.CurrentWindow != NULL )
120         fgStructure.CurrentWindow->State.VisualizeNormals = value;
121       break;
122
123     default:
124         fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
125         break;
126     }
127 }
128
129 /*
130  * General settings query method
131  */
132 int FGAPIENTRY glutGet( GLenum eWhat )
133 {
134     switch (eWhat)
135     {
136     case GLUT_INIT_STATE:
137         return fgState.Initialised;
138
139     case GLUT_ELAPSED_TIME:
140         return (int) fgElapsedTime();
141     }
142
143     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
144
145     /* XXX In chronological code add order.  (WHY in that order?) */
146     switch( eWhat )
147     {
148     /* Following values are stored in fgState and fgDisplay global structures */
149     case GLUT_SCREEN_WIDTH:         return fgDisplay.ScreenWidth   ;
150     case GLUT_SCREEN_HEIGHT:        return fgDisplay.ScreenHeight  ;
151     case GLUT_SCREEN_WIDTH_MM:      return fgDisplay.ScreenWidthMM ;
152     case GLUT_SCREEN_HEIGHT_MM:     return fgDisplay.ScreenHeightMM;
153     case GLUT_INIT_WINDOW_X:        return fgState.Position.Use ?
154                                            fgState.Position.X : -1 ;
155     case GLUT_INIT_WINDOW_Y:        return fgState.Position.Use ?
156                                            fgState.Position.Y : -1 ;
157     case GLUT_INIT_WINDOW_WIDTH:    return fgState.Size.Use ?
158                                            fgState.Size.X : -1     ;
159     case GLUT_INIT_WINDOW_HEIGHT:   return fgState.Size.Use ?
160                                            fgState.Size.Y : -1     ;
161     case GLUT_INIT_DISPLAY_MODE:    return fgState.DisplayMode     ;
162     case GLUT_INIT_MAJOR_VERSION:   return fgState.MajorVersion    ;
163     case GLUT_INIT_MINOR_VERSION:   return fgState.MinorVersion    ;
164     case GLUT_INIT_FLAGS:           return fgState.ContextFlags    ;
165     case GLUT_INIT_PROFILE:         return fgState.ContextProfile  ;
166
167     /* The window structure queries */
168     case GLUT_WINDOW_PARENT:
169         if( fgStructure.CurrentWindow         == NULL ) return 0;
170         if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
171         return fgStructure.CurrentWindow->Parent->ID;
172
173     case GLUT_WINDOW_NUM_CHILDREN:
174         if( fgStructure.CurrentWindow == NULL )
175             return 0;
176         return fgListLength( &fgStructure.CurrentWindow->Children );
177
178     case GLUT_WINDOW_CURSOR:
179         if( fgStructure.CurrentWindow == NULL )
180             return 0;
181         return fgStructure.CurrentWindow->State.Cursor;
182
183     case GLUT_MENU_NUM_ITEMS:
184         if( fgStructure.CurrentMenu == NULL )
185             return 0;
186         return fgListLength( &fgStructure.CurrentMenu->Entries );
187
188     case GLUT_ACTION_ON_WINDOW_CLOSE:
189         return fgState.ActionOnWindowClose;
190
191     case GLUT_VERSION :
192         return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
193
194     case GLUT_RENDERING_CONTEXT:
195         return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
196                                          : GLUT_CREATE_NEW_CONTEXT;
197
198     case GLUT_DIRECT_RENDERING:
199         return fgState.DirectContext;
200
201     case GLUT_FULL_SCREEN:
202         return fgStructure.CurrentWindow->State.IsFullscreen;
203
204     case GLUT_AUX:
205       return fgState.AuxiliaryBufferNumber;
206
207     case GLUT_MULTISAMPLE:
208       return fgState.SampleNumber;
209
210     case GLUT_SKIP_STALE_MOTION_EVENTS:
211       return fgState.SkipStaleMotion;
212
213     case GLUT_OBJECTS_VISUALIZE_NORMALS:
214       if( fgStructure.CurrentWindow == NULL )
215         return GL_FALSE;
216       return fgStructure.CurrentWindow->State.VisualizeNormals;
217
218     default:
219         return fgPlatformGlutGet ( eWhat );
220         break;
221     }
222     return -1;
223 }
224
225 /*
226  * Returns various device information.
227  */
228 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
229 {
230     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
231
232     /* XXX WARNING: we are mostly lying in this function. */
233     switch( eWhat )
234     {
235     case GLUT_HAS_JOYSTICK:
236         return fgJoystickDetect ();
237
238     case GLUT_OWNS_JOYSTICK:
239         return fgState.JoysticksInitialised;
240
241     case GLUT_JOYSTICK_POLL_RATE:
242         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
243
244     /* XXX The following two are only for Joystick 0 but this is an improvement */
245     case GLUT_JOYSTICK_BUTTONS:
246         return glutJoystickGetNumButtons ( 0 );
247
248     case GLUT_JOYSTICK_AXES:
249         return glutJoystickGetNumAxes ( 0 );
250
251     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
252         return fgInputDeviceDetect ();
253
254     case GLUT_NUM_DIALS:
255         if ( fgState.InputDevsInitialised ) return 8;
256         return 0;
257  
258     case GLUT_NUM_BUTTON_BOX_BUTTONS:
259         return 0;
260
261     case GLUT_HAS_SPACEBALL:
262         return fgHasSpaceball();
263
264     case GLUT_HAS_TABLET:
265         return 0;
266
267     case GLUT_NUM_SPACEBALL_BUTTONS:
268         return fgSpaceballNumButtons();
269
270     case GLUT_NUM_TABLET_BUTTONS:
271         return 0;
272
273     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
274         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
275
276     case GLUT_DEVICE_KEY_REPEAT:
277         return fgState.KeyRepeat;
278
279     default:
280                 return fgPlatformGlutDeviceGet ( eWhat );
281     }
282
283     /* And now -- the failure. */
284     return -1;
285 }
286
287 /*
288  * This should return the current state of ALT, SHIFT and CTRL keys.
289  */
290 int FGAPIENTRY glutGetModifiers( void )
291 {
292     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
293     if( fgState.Modifiers == INVALID_MODIFIERS )
294     {
295         fgWarning( "glutGetModifiers() called outside an input callback" );
296         return 0;
297     }
298
299     return fgState.Modifiers;
300 }
301
302 /*
303  * Return the state of the GLUT API overlay subsystem. A misery ;-)
304  */
305 int FGAPIENTRY glutLayerGet( GLenum eWhat )
306 {
307     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
308
309     /*
310      * This is easy as layers are not implemented and
311      * overlay support is not planned. E.g. on Windows,
312      * overlay requests in PFDs are ignored
313      * (see iLayerType at http://msdn.microsoft.com/en-us/library/dd368826(v=vs.85).aspx)
314      */
315     switch( eWhat )
316     {
317
318     case GLUT_OVERLAY_POSSIBLE:
319         return 0 ;
320
321     case GLUT_LAYER_IN_USE:
322         return GLUT_NORMAL;
323
324     case GLUT_HAS_OVERLAY:
325         return 0;
326
327     case GLUT_TRANSPARENT_INDEX:
328         /*
329         * Return just anything, which is always defined as zero
330         *
331         * XXX HUH?
332         */
333         return 0;
334
335     case GLUT_NORMAL_DAMAGED:
336         /* XXX Actually I do not know. Maybe. */
337         return 0;
338
339     case GLUT_OVERLAY_DAMAGED:
340         return -1;
341
342     default:
343         fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
344         break;
345     }
346
347     /* And fail. That's good. Programs do love failing. */
348     return -1;
349 }
350
351 int * FGAPIENTRY glutGetModeValues(GLenum eWhat, int *size)
352 {
353   int *array;
354
355   FREEGLUT_EXIT_IF_NOT_INITIALISED("glutGetModeValues");
356
357   *size = 0;
358   array = fgPlatformGlutGetModeValues ( eWhat, size );
359
360   return array;
361 }
362
363 /*** END OF FILE ***/