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