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