glx: convert a malloc+memset to calloc
[mesa.git] / src / glx / glxconfig.c
1 /*
2 * (C) Copyright IBM Corporation 2003
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glxconfig.c
27 * Utility routines for working with \c struct glx_config structures. At
28 * some point most or all of these functions will be moved to the Mesa
29 * code base.
30 *
31 * \author Ian Romanick <idr@us.ibm.com>
32 */
33
34 #include <GL/glx.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "glxconfig.h"
39
40 #define NUM_VISUAL_TYPES 6
41
42 /**
43 * Get data from a GLX config
44 *
45 * \param mode GL context mode whose data is to be returned.
46 * \param attribute Attribute of \c mode that is to be returned.
47 * \param value_return Location to store the data member of \c mode.
48 * \return If \c attribute is a valid attribute of \c mode, zero is
49 * returned. Otherwise \c GLX_BAD_ATTRIBUTE is returned.
50 */
51 _X_HIDDEN int
52 glx_config_get(struct glx_config * mode, int attribute, int *value_return)
53 {
54 switch (attribute) {
55 case GLX_USE_GL:
56 *value_return = GL_TRUE;
57 return 0;
58 case GLX_BUFFER_SIZE:
59 *value_return = mode->rgbBits;
60 return 0;
61 case GLX_RGBA:
62 *value_return = mode->rgbMode;
63 return 0;
64 case GLX_RED_SIZE:
65 *value_return = mode->redBits;
66 return 0;
67 case GLX_GREEN_SIZE:
68 *value_return = mode->greenBits;
69 return 0;
70 case GLX_BLUE_SIZE:
71 *value_return = mode->blueBits;
72 return 0;
73 case GLX_ALPHA_SIZE:
74 *value_return = mode->alphaBits;
75 return 0;
76 case GLX_DOUBLEBUFFER:
77 *value_return = mode->doubleBufferMode;
78 return 0;
79 case GLX_STEREO:
80 *value_return = mode->stereoMode;
81 return 0;
82 case GLX_AUX_BUFFERS:
83 *value_return = mode->numAuxBuffers;
84 return 0;
85 case GLX_DEPTH_SIZE:
86 *value_return = mode->depthBits;
87 return 0;
88 case GLX_STENCIL_SIZE:
89 *value_return = mode->stencilBits;
90 return 0;
91 case GLX_ACCUM_RED_SIZE:
92 *value_return = mode->accumRedBits;
93 return 0;
94 case GLX_ACCUM_GREEN_SIZE:
95 *value_return = mode->accumGreenBits;
96 return 0;
97 case GLX_ACCUM_BLUE_SIZE:
98 *value_return = mode->accumBlueBits;
99 return 0;
100 case GLX_ACCUM_ALPHA_SIZE:
101 *value_return = mode->accumAlphaBits;
102 return 0;
103 case GLX_LEVEL:
104 *value_return = mode->level;
105 return 0;
106 #ifndef GLX_USE_APPLEGL /* This isn't supported by CGL. */
107 case GLX_TRANSPARENT_TYPE_EXT:
108 *value_return = mode->transparentPixel;
109 return 0;
110 #endif
111 case GLX_TRANSPARENT_RED_VALUE:
112 *value_return = mode->transparentRed;
113 return 0;
114 case GLX_TRANSPARENT_GREEN_VALUE:
115 *value_return = mode->transparentGreen;
116 return 0;
117 case GLX_TRANSPARENT_BLUE_VALUE:
118 *value_return = mode->transparentBlue;
119 return 0;
120 case GLX_TRANSPARENT_ALPHA_VALUE:
121 *value_return = mode->transparentAlpha;
122 return 0;
123 case GLX_TRANSPARENT_INDEX_VALUE:
124 *value_return = mode->transparentIndex;
125 return 0;
126 case GLX_X_VISUAL_TYPE:
127 *value_return = mode->visualType;
128 return 0;
129 case GLX_CONFIG_CAVEAT:
130 *value_return = mode->visualRating;
131 return 0;
132 case GLX_VISUAL_ID:
133 *value_return = mode->visualID;
134 return 0;
135 case GLX_DRAWABLE_TYPE:
136 *value_return = mode->drawableType;
137 return 0;
138 case GLX_RENDER_TYPE:
139 *value_return = mode->renderType;
140 return 0;
141 case GLX_X_RENDERABLE:
142 *value_return = mode->xRenderable;
143 return 0;
144 case GLX_FBCONFIG_ID:
145 *value_return = mode->fbconfigID;
146 return 0;
147 case GLX_MAX_PBUFFER_WIDTH:
148 *value_return = mode->maxPbufferWidth;
149 return 0;
150 case GLX_MAX_PBUFFER_HEIGHT:
151 *value_return = mode->maxPbufferHeight;
152 return 0;
153 case GLX_MAX_PBUFFER_PIXELS:
154 *value_return = mode->maxPbufferPixels;
155 return 0;
156 #ifndef GLX_USE_APPLEGL /* These aren't supported by CGL. */
157 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
158 *value_return = mode->optimalPbufferWidth;
159 return 0;
160 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
161 *value_return = mode->optimalPbufferHeight;
162 return 0;
163 case GLX_SWAP_METHOD_OML:
164 *value_return = mode->swapMethod;
165 return 0;
166 #endif
167 case GLX_SAMPLE_BUFFERS_SGIS:
168 *value_return = mode->sampleBuffers;
169 return 0;
170 case GLX_SAMPLES_SGIS:
171 *value_return = mode->samples;
172 return 0;
173 case GLX_BIND_TO_TEXTURE_RGB_EXT:
174 *value_return = mode->bindToTextureRgb;
175 return 0;
176 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
177 *value_return = mode->bindToTextureRgba;
178 return 0;
179 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
180 *value_return = mode->bindToMipmapTexture == GL_TRUE ? GL_TRUE :
181 GL_FALSE;
182 return 0;
183 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
184 *value_return = mode->bindToTextureTargets;
185 return 0;
186 case GLX_Y_INVERTED_EXT:
187 *value_return = mode->yInverted;
188 return 0;
189
190 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:
191 *value_return = mode->sRGBCapable;
192 return 0;
193
194 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
195 * It is ONLY for communication between the GLX client and the GLX
196 * server.
197 */
198 case GLX_VISUAL_SELECT_GROUP_SGIX:
199 default:
200 return GLX_BAD_ATTRIBUTE;
201 }
202 }
203
204
205 /**
206 * Allocate a linked list of \c struct glx_config structures. The fields of
207 * each structure will be initialized to "reasonable" default values. In
208 * most cases this is the default value defined by table 3.4 of the GLX
209 * 1.3 specification. This means that most values are either initialized to
210 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
211 * extensions is added, the new values will be initialized to appropriate
212 * values from the extension specification.
213 *
214 * \param count Number of structures to allocate.
215 * \returns A pointer to the first element in a linked list of \c count
216 * stuctures on success, or \c NULL on failure.
217 */
218 _X_HIDDEN struct glx_config *
219 glx_config_create_list(unsigned count)
220 {
221 const size_t size = sizeof(struct glx_config);
222 struct glx_config *base = NULL;
223 struct glx_config **next;
224 unsigned i;
225
226 next = &base;
227 for (i = 0; i < count; i++) {
228 *next = calloc(1, size);
229 if (*next == NULL) {
230 glx_config_destroy_list(base);
231 base = NULL;
232 break;
233 }
234
235 (*next)->visualID = GLX_DONT_CARE;
236 (*next)->visualType = GLX_DONT_CARE;
237 (*next)->visualRating = GLX_NONE;
238 (*next)->transparentPixel = GLX_NONE;
239 (*next)->transparentRed = GLX_DONT_CARE;
240 (*next)->transparentGreen = GLX_DONT_CARE;
241 (*next)->transparentBlue = GLX_DONT_CARE;
242 (*next)->transparentAlpha = GLX_DONT_CARE;
243 (*next)->transparentIndex = GLX_DONT_CARE;
244 (*next)->xRenderable = GLX_DONT_CARE;
245 (*next)->fbconfigID = GLX_DONT_CARE;
246 (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
247 (*next)->bindToTextureRgb = GLX_DONT_CARE;
248 (*next)->bindToTextureRgba = GLX_DONT_CARE;
249 (*next)->bindToMipmapTexture = GLX_DONT_CARE;
250 (*next)->bindToTextureTargets = GLX_DONT_CARE;
251 (*next)->yInverted = GLX_DONT_CARE;
252 (*next)->sRGBCapable = GLX_DONT_CARE;
253
254 next = &((*next)->next);
255 }
256
257 return base;
258 }
259
260 _X_HIDDEN void
261 glx_config_destroy_list(struct glx_config *configs)
262 {
263 while (configs != NULL) {
264 struct glx_config *const next = configs->next;
265
266 free(configs);
267 configs = next;
268 }
269 }
270
271
272 /**
273 * Find a context mode matching a Visual ID.
274 *
275 * \param modes List list of context-mode structures to be searched.
276 * \param vid Visual ID to be found.
277 * \returns A pointer to a context-mode in \c modes if \c vid was found in
278 * the list, or \c NULL if it was not.
279 */
280
281 _X_HIDDEN struct glx_config *
282 glx_config_find_visual(struct glx_config *configs, int vid)
283 {
284 struct glx_config *c;
285
286 for (c = configs; c != NULL; c = c->next)
287 if (c->visualID == vid)
288 return c;
289
290 return NULL;
291 }
292
293 _X_HIDDEN struct glx_config *
294 glx_config_find_fbconfig(struct glx_config *configs, int fbid)
295 {
296 struct glx_config *c;
297
298 for (c = configs; c != NULL; c = c->next)
299 if (c->fbconfigID == fbid)
300 return c;
301
302 return NULL;
303 }