Initial revision
[freeglut] / freeglut-1.3 / freeglut_misc.c
1 /*
2  * freeglut_misc.c
3  *
4  * Functions that didn't fit anywhere else...
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 9 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-misc"
33
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  glutSetColor()     --
41  *  glutGetColor()     --
42  *  glutCopyColormap() --
43  *  glutSetKeyRepeat() -- this is evil and should be removed from API
44  */
45
46 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
47
48 /*
49  * This functions checks if an OpenGL extension is supported or not
50  */
51 int FGAPIENTRY glutExtensionSupported( const char* extension )
52 {
53     /*
54      * Grab the current context's OpenGL extensions
55      * and create a new GLib lexical analyzer...
56      */
57     gchar *glExtensions = (gchar *) glGetString( GL_EXTENSIONS );
58     GScanner* scanner = g_scanner_new( NULL );
59     gint i;
60
61     /*
62      * Make sure there is a current window, and thus -- a current context available
63      */
64     freeglut_assert_ready; freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
65
66     /*
67      * Fail if there is no extension, extensions or scanner available
68      */
69     freeglut_return_val_if_fail( (scanner != NULL) && (strlen( extension ) > 0)
70                                           && (strlen( glExtensions ) > 0), 0 );
71
72     /*
73      * Check if the extension itself looks valid
74      */
75     for( i=0; i<(gint) strlen( extension ); i++ )
76         if( extension[ i ] == ' ' )
77             return( 0 );
78
79     /*
80      * Set the scanner's input name (for debugging)
81      */
82     scanner->input_name = "glutExtensionSupported()";
83
84     /*
85      * Start the lexical analysis of the extensions string
86      */
87     g_scanner_input_text( scanner, glExtensions, strlen( glExtensions ) );
88
89     /*
90      * While there are any more tokens to be checked...
91      */
92     while( !g_scanner_eof( scanner ) )
93     {
94         /*
95          * Actually we're expecting only string tokens
96          */
97         GTokenType tokenType = g_scanner_get_next_token( scanner );
98
99         /*
100          * We are looking for identifiers
101          */
102         if( tokenType == G_TOKEN_IDENTIFIER )
103         {
104             /*
105              * Compare the token and the extension string
106              */
107             if( strcmp( scanner->value.v_identifier, extension ) == 0 )
108             {
109                 /*
110                  * OKi, we have found the extension string we've been looking for
111                  */
112                 g_scanner_destroy( scanner );
113                 return( 1 );
114             }
115         }
116     }
117
118     /*
119      * Well, looks like we have failed to find the extension string
120      */
121     g_scanner_destroy( scanner );
122     return( 0 );
123 }
124
125 /*
126  * This function reports all the errors that happened till now
127  */
128 void FGAPIENTRY glutReportErrors( void )
129 {
130     GLenum error = glGetError();
131
132     /*
133      * Keep reporting errors as long as there are any...
134      */
135     while( error != GL_NO_ERROR )
136     {
137         /*
138          * Print the current error
139          */
140 #       undef  G_LOG_DOMAIN
141 #       define G_LOG_DOMAIN ((gchar *) 0)
142
143         g_warning( "GL error: %s", gluErrorString( error ) );
144
145 #       undef   G_LOG_DOMAIN
146 #       define  G_LOG_DOMAIN  "freeglut_misc.c"
147
148         /*
149          * Grab the next error value
150          */
151         error = glGetError();
152     };
153 }
154
155 /*
156  * Turns the ignore key auto repeat feature on and off
157  */
158 void FGAPIENTRY glutIgnoreKeyRepeat( int ignore )
159 {
160     /*
161      * This is simple and not demanging...
162      */
163     fgState.IgnoreKeyRepeat = ignore ? TRUE : FALSE;
164 }
165
166 /*
167  * Hints the window system whether to generate key auto repeat, or not. This is evil.
168  */
169 void FGAPIENTRY glutSetKeyRepeat( int repeatMode )
170 {
171 #if TARGET_HOST_UNIX_X11
172
173     freeglut_assert_ready;
174
175     /*
176      * This is really evil, but let's have this done.
177      */
178     switch( repeatMode )
179     {
180     case GLUT_KEY_REPEAT_OFF:   XAutoRepeatOff( fgDisplay.Display ); break;
181     case GLUT_KEY_REPEAT_ON:    XAutoRepeatOn( fgDisplay.Display );  break;
182     case GLUT_KEY_REPEAT_DEFAULT:
183         {
184             XKeyboardState keyboardState;
185
186             /*
187              * Query the current keyboard state
188              */
189             XGetKeyboardControl( fgDisplay.Display, &keyboardState );
190
191             /*
192              * Set the auto key repeat basing on the global settings
193              */
194             glutSetKeyRepeat(
195                 keyboardState.global_auto_repeat == AutoRepeatModeOn ?
196                 GLUT_KEY_REPEAT_ON : GLUT_KEY_REPEAT_OFF
197             );
198         }
199         break;
200
201     default:
202         /*
203          * Whoops, this was not expected at all
204          */
205         g_assert_not_reached();
206     }
207
208 #endif
209 }
210
211 /*
212  * Forces the joystick callback to be executed
213  */
214 void FGAPIENTRY glutForceJoystickFunc( void )
215 {
216     freeglut_assert_ready;
217
218     /*
219      * Is there a current window selected?
220      */
221     freeglut_return_if_fail( fgStructure.Window != NULL );
222
223     /*
224      * Check if there is a joystick callback hooked to the current window
225      */
226     freeglut_return_if_fail( fgStructure.Window->Callbacks.Joystick != NULL );
227
228     /*
229      * Poll the joystick now, using the current window's joystick callback
230      */
231     fgJoystickPollWindow( fgStructure.Window );
232 }
233
234 /*
235  *
236  */
237 void FGAPIENTRY glutSetColor( int nColor, GLfloat red, GLfloat green, GLfloat blue )
238 {
239     /*
240      *
241      */
242 }
243
244 /*
245  *
246  */
247 GLfloat FGAPIENTRY glutGetColor( int color, int component )
248 {
249     /*
250      *
251      */
252     return( 0.0f );
253 }
254
255 /*
256  *
257  */
258 void FGAPIENTRY glutCopyColormap( int window )
259 {
260     /*
261      *
262      */
263 }
264
265 /*** END OF FILE ***/