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