Moving X11-specific code from "freeglut_display.c" into its own file
[freeglut] / src / Common / freeglut_misc.c
1 /*\r
2  * freeglut_misc.c\r
3  *\r
4  * Functions that didn't fit anywhere else...\r
5  *\r
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.\r
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>\r
8  * Creation date: Thu Dec 9 1999\r
9  *\r
10  * Permission is hereby granted, free of charge, to any person obtaining a\r
11  * copy of this software and associated documentation files (the "Software"),\r
12  * to deal in the Software without restriction, including without limitation\r
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
14  * and/or sell copies of the Software, and to permit persons to whom the\r
15  * Software is furnished to do so, subject to the following conditions:\r
16  *\r
17  * The above copyright notice and this permission notice shall be included\r
18  * in all copies or substantial portions of the Software.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
26  */\r
27 \r
28 #include <GL/freeglut.h>\r
29 #include "freeglut_internal.h"\r
30 \r
31 /*\r
32  * TODO BEFORE THE STABLE RELEASE:\r
33  *\r
34  *  glutSetColor()     --\r
35  *  glutGetColor()     --\r
36  *  glutCopyColormap() --\r
37  *  glutSetKeyRepeat() -- this is evil and should be removed from API\r
38  */\r
39 \r
40 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
41 \r
42 /*\r
43  * This functions checks if an OpenGL extension is supported or not\r
44  *\r
45  * XXX Wouldn't this be simpler and clearer if we used strtok()?\r
46  */\r
47 int FGAPIENTRY glutExtensionSupported( const char* extension )\r
48 {\r
49   const char *extensions, *start;\r
50   const size_t len = strlen( extension );\r
51 \r
52   /* Make sure there is a current window, and thus a current context available */\r
53   FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutExtensionSupported" );\r
54   freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );\r
55 \r
56   if (strchr(extension, ' '))\r
57     return 0;\r
58   start = extensions = (const char *) glGetString(GL_EXTENSIONS);\r
59 \r
60   /* XXX consider printing a warning to stderr that there's no current\r
61    * rendering context.\r
62    */\r
63   freeglut_return_val_if_fail( extensions != NULL, 0 );\r
64 \r
65   while (1) {\r
66      const char *p = strstr(extensions, extension);\r
67      if (!p)\r
68         return 0;  /* not found */\r
69      /* check that the match isn't a super string */\r
70      if ((p == start || p[-1] == ' ') && (p[len] == ' ' || p[len] == 0))\r
71         return 1;\r
72      /* skip the false match and continue */\r
73      extensions = p + len;\r
74   }\r
75 \r
76   return 0 ;\r
77 }\r
78 \r
79 #ifndef GL_INVALID_FRAMEBUFFER_OPERATION\r
80 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT\r
81 #define GL_INVALID_FRAMEBUFFER_OPERATION GL_INVALID_FRAMEBUFFER_OPERATION_EXT\r
82 #else\r
83 #define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506\r
84 #endif\r
85 #endif\r
86 \r
87 #ifndef GL_TABLE_TOO_LARGE\r
88 #ifdef GL_TABLE_TOO_LARGE_EXT\r
89 #define GL_TABLE_TOO_LARGE GL_TABLE_TOO_LARGE_EXT\r
90 #else\r
91 #define GL_TABLE_TOO_LARGE 0x8031\r
92 #endif\r
93 #endif\r
94 \r
95 #ifndef GL_TEXTURE_TOO_LARGE\r
96 #ifdef GL_TEXTURE_TOO_LARGE_EXT\r
97 #define GL_TEXTURE_TOO_LARGE GL_TEXTURE_TOO_LARGE_EXT\r
98 #else\r
99 #define GL_TEXTURE_TOO_LARGE 0x8065\r
100 #endif\r
101 #endif\r
102 \r
103 /*\r
104  * A cut-down local version of gluErrorString to avoid depending on GLU.\r
105  */\r
106 static const char* fghErrorString( GLenum error )\r
107 {\r
108   switch ( error ) {\r
109   case GL_INVALID_ENUM: return "invalid enumerant";\r
110   case GL_INVALID_VALUE: return "invalid value";\r
111   case GL_INVALID_OPERATION: return "invalid operation";\r
112   case GL_STACK_OVERFLOW: return "stack overflow";\r
113   case GL_STACK_UNDERFLOW: return "stack underflow";\r
114   case GL_OUT_OF_MEMORY: return "out of memory";\r
115   case GL_TABLE_TOO_LARGE: return "table too large";\r
116   case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffer operation";\r
117   case GL_TEXTURE_TOO_LARGE: return "texture too large";\r
118   default: return "unknown GL error";\r
119   }\r
120 }\r
121 \r
122 /*\r
123  * This function reports all the OpenGL errors that happened till now\r
124  */\r
125 void FGAPIENTRY glutReportErrors( void )\r
126 {\r
127     GLenum error;\r
128     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReportErrors" );\r
129     while( ( error = glGetError() ) != GL_NO_ERROR )\r
130         fgWarning( "GL error: %s", fghErrorString( error ) );\r
131 }\r
132 \r
133 /*\r
134  * Control the auto-repeat of keystrokes to the current window\r
135  */\r
136 void FGAPIENTRY glutIgnoreKeyRepeat( int ignore )\r
137 {\r
138     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIgnoreKeyRepeat" );\r
139     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIgnoreKeyRepeat" );\r
140 \r
141     fgStructure.CurrentWindow->State.IgnoreKeyRepeat = ignore ? GL_TRUE : GL_FALSE;\r
142 }\r
143 \r
144 /*\r
145  * Set global auto-repeat of keystrokes\r
146  *\r
147  * RepeatMode should be either:\r
148  *    GLUT_KEY_REPEAT_OFF\r
149  *    GLUT_KEY_REPEAT_ON\r
150  *    GLUT_KEY_REPEAT_DEFAULT\r
151  */\r
152 void FGAPIENTRY glutSetKeyRepeat( int repeatMode )\r
153 {\r
154     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetKeyRepeat" );\r
155 \r
156     switch( repeatMode )\r
157     {\r
158     case GLUT_KEY_REPEAT_OFF:\r
159     case GLUT_KEY_REPEAT_ON:\r
160      fgState.KeyRepeat = repeatMode;\r
161      break;\r
162 \r
163     case GLUT_KEY_REPEAT_DEFAULT:\r
164      fgState.KeyRepeat = GLUT_KEY_REPEAT_ON;\r
165      break;\r
166 \r
167     default:\r
168         fgError ("Invalid glutSetKeyRepeat mode: %d", repeatMode);\r
169         break;\r
170     }\r
171 }\r
172 \r
173 /*\r
174  * Forces the joystick callback to be executed\r
175  */\r
176 void FGAPIENTRY glutForceJoystickFunc( void )\r
177 {\r
178     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutForceJoystickFunc" );\r
179 #if !defined(_WIN32_WCE)\r
180     freeglut_return_if_fail( fgStructure.CurrentWindow != NULL );\r
181     freeglut_return_if_fail( FETCH_WCB( *( fgStructure.CurrentWindow ), Joystick ) );\r
182     fgJoystickPollWindow( fgStructure.CurrentWindow );\r
183 #endif /* !defined(_WIN32_WCE) */\r
184 }\r
185 \r
186 /*\r
187  *\r
188  */\r
189 void FGAPIENTRY glutSetColor( int nColor, GLfloat red, GLfloat green, GLfloat blue )\r
190 {\r
191     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetColor" );\r
192     /* We really need to do something here. */\r
193 }\r
194 \r
195 /*\r
196  *\r
197  */\r
198 GLfloat FGAPIENTRY glutGetColor( int color, int component )\r
199 {\r
200     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetColor" );\r
201     /* We really need to do something here. */\r
202     return( 0.0f );\r
203 }\r
204 \r
205 /*\r
206  *\r
207  */\r
208 void FGAPIENTRY glutCopyColormap( int window )\r
209 {\r
210     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCopyColormap" );\r
211     /* We really need to do something here. */\r
212 }\r
213 \r
214 /*** END OF FILE ***/\r