Initial commit
[xorg_rtime.git] / xorg-server-1.4 / GL / glx / single2.c
blob3387af2a265797d1b615d95e844801a12346e19f
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
36 #define NEED_REPLIES
37 #ifdef HAVE_DIX_CONFIG_H
38 #include <dix-config.h>
39 #endif
41 #include <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
45 #include "glxserver.h"
46 #include "glxutil.h"
47 #include "glxext.h"
48 #include "indirect_dispatch.h"
49 #include "unpack.h"
50 #include "glapitable.h"
51 #include "glapi.h"
52 #include "glthread.h"
53 #include "dispatch.h"
55 int __glXDisp_FeedbackBuffer(__GLXclientState *cl, GLbyte *pc)
57 GLsizei size;
58 GLenum type;
59 __GLXcontext *cx;
60 int error;
62 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
63 if (!cx) {
64 return error;
67 pc += __GLX_SINGLE_HDR_SIZE;
68 size = *(GLsizei *)(pc+0);
69 type = *(GLenum *)(pc+4);
70 if (cx->feedbackBufSize < size) {
71 cx->feedbackBuf = (GLfloat *) xrealloc(cx->feedbackBuf,
72 (size_t)size
73 * __GLX_SIZE_FLOAT32);
74 if (!cx->feedbackBuf) {
75 cl->client->errorValue = size;
76 return BadAlloc;
78 cx->feedbackBufSize = size;
80 CALL_FeedbackBuffer( GET_DISPATCH(), (size, type, cx->feedbackBuf) );
81 __GLX_NOTE_UNFLUSHED_CMDS(cx);
82 return Success;
85 int __glXDisp_SelectBuffer(__GLXclientState *cl, GLbyte *pc)
87 __GLXcontext *cx;
88 GLsizei size;
89 int error;
91 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
92 if (!cx) {
93 return error;
96 pc += __GLX_SINGLE_HDR_SIZE;
97 size = *(GLsizei *)(pc+0);
98 if (cx->selectBufSize < size) {
99 cx->selectBuf = (GLuint *) xrealloc(cx->selectBuf,
100 (size_t) size
101 * __GLX_SIZE_CARD32);
102 if (!cx->selectBuf) {
103 cl->client->errorValue = size;
104 return BadAlloc;
106 cx->selectBufSize = size;
108 CALL_SelectBuffer( GET_DISPATCH(), (size, cx->selectBuf) );
109 __GLX_NOTE_UNFLUSHED_CMDS(cx);
110 return Success;
113 int __glXDisp_RenderMode(__GLXclientState *cl, GLbyte *pc)
115 ClientPtr client;
116 xGLXRenderModeReply reply;
117 __GLXcontext *cx;
118 GLint nitems=0, retBytes=0, retval, newModeCheck;
119 GLubyte *retBuffer = NULL;
120 GLenum newMode;
121 int error;
123 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
124 if (!cx) {
125 return error;
128 pc += __GLX_SINGLE_HDR_SIZE;
129 newMode = *(GLenum*) pc;
130 retval = CALL_RenderMode( GET_DISPATCH(), (newMode) );
132 /* Check that render mode worked */
133 CALL_GetIntegerv( GET_DISPATCH(), (GL_RENDER_MODE, &newModeCheck) );
134 if (newModeCheck != newMode) {
135 /* Render mode change failed. Bail */
136 newMode = newModeCheck;
137 goto noChangeAllowed;
141 ** Render mode might have still failed if we get here. But in this
142 ** case we can't really tell, nor does it matter. If it did fail, it
143 ** will return 0, and thus we won't send any data across the wire.
146 switch (cx->renderMode) {
147 case GL_RENDER:
148 cx->renderMode = newMode;
149 break;
150 case GL_FEEDBACK:
151 if (retval < 0) {
152 /* Overflow happened. Copy the entire buffer */
153 nitems = cx->feedbackBufSize;
154 } else {
155 nitems = retval;
157 retBytes = nitems * __GLX_SIZE_FLOAT32;
158 retBuffer = (GLubyte*) cx->feedbackBuf;
159 cx->renderMode = newMode;
160 break;
161 case GL_SELECT:
162 if (retval < 0) {
163 /* Overflow happened. Copy the entire buffer */
164 nitems = cx->selectBufSize;
165 } else {
166 GLuint *bp = cx->selectBuf;
167 GLint i;
170 ** Figure out how many bytes of data need to be sent. Parse
171 ** the selection buffer to determine this fact as the
172 ** return value is the number of hits, not the number of
173 ** items in the buffer.
175 nitems = 0;
176 i = retval;
177 while (--i >= 0) {
178 GLuint n;
180 /* Parse select data for this hit */
181 n = *bp;
182 bp += 3 + n;
184 nitems = bp - cx->selectBuf;
186 retBytes = nitems * __GLX_SIZE_CARD32;
187 retBuffer = (GLubyte*) cx->selectBuf;
188 cx->renderMode = newMode;
189 break;
193 ** First reply is the number of elements returned in the feedback or
194 ** selection array, as per the API for glRenderMode itself.
196 noChangeAllowed:;
197 client = cl->client;
198 reply.length = nitems;
199 reply.type = X_Reply;
200 reply.sequenceNumber = client->sequence;
201 reply.retval = retval;
202 reply.size = nitems;
203 reply.newMode = newMode;
204 WriteToClient(client, sz_xGLXRenderModeReply, (char *)&reply);
205 if (retBytes) {
206 WriteToClient(client, retBytes, (char *)retBuffer);
208 return Success;
211 int __glXDisp_Flush(__GLXclientState *cl, GLbyte *pc)
213 __GLXcontext *cx;
214 int error;
216 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
217 if (!cx) {
218 return error;
221 CALL_Flush( GET_DISPATCH(), () );
222 __GLX_NOTE_FLUSHED_CMDS(cx);
223 return Success;
226 int __glXDisp_Finish(__GLXclientState *cl, GLbyte *pc)
228 __GLXcontext *cx;
229 ClientPtr client;
230 int error;
232 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
233 if (!cx) {
234 return error;
237 /* Do a local glFinish */
238 CALL_Finish( GET_DISPATCH(), () );
239 __GLX_NOTE_FLUSHED_CMDS(cx);
241 /* Send empty reply packet to indicate finish is finished */
242 client = cl->client;
243 __GLX_BEGIN_REPLY(0);
244 __GLX_SEND_HEADER();
245 return Success;
248 #define SEPARATOR " "
250 char *__glXcombine_strings(const char *cext_string, const char *sext_string)
252 size_t clen, slen;
253 char *combo_string, *token, *s1;
254 const char *s2, *end;
256 /* safeguard to prevent potentially fatal errors in the string functions */
257 if (!cext_string)
258 cext_string = "";
259 if (!sext_string)
260 sext_string = "";
263 ** String can't be longer than min(cstring, sstring)
264 ** pull tokens out of shortest string
265 ** include space in combo_string for final separator and null terminator
267 clen = strlen(cext_string);
268 slen = strlen(sext_string);
269 if (clen > slen) {
270 combo_string = (char *) xalloc(slen + 2);
271 s1 = (char *) xalloc(slen + 2);
272 if (s1) strcpy(s1, sext_string);
273 s2 = cext_string;
274 } else {
275 combo_string = (char *) xalloc(clen + 2);
276 s1 = (char *) xalloc(clen + 2);
277 if (s1) strcpy(s1, cext_string);
278 s2 = sext_string;
280 if (!combo_string || !s1) {
281 if (combo_string)
282 xfree(combo_string);
283 if (s1)
284 xfree(s1);
285 return NULL;
287 combo_string[0] = '\0';
289 /* Get first extension token */
290 token = strtok( s1, SEPARATOR);
291 while ( token != NULL ) {
294 ** if token in second string then save it
295 ** beware of extension names which are prefixes of other extension names
297 const char *p = s2;
298 end = p + strlen(p);
299 while (p < end) {
300 size_t n = strcspn(p, SEPARATOR);
301 if ((strlen(token) == n) && (strncmp(token, p, n) == 0)) {
302 combo_string = strcat(combo_string, token);
303 combo_string = strcat(combo_string, SEPARATOR);
305 p += (n + 1);
308 /* Get next extension token */
309 token = strtok( NULL, SEPARATOR);
311 xfree(s1);
312 return combo_string;
315 int DoGetString(__GLXclientState *cl, GLbyte *pc, GLboolean need_swap)
317 ClientPtr client;
318 __GLXcontext *cx;
319 GLenum name;
320 const char *string;
321 __GLX_DECLARE_SWAP_VARIABLES;
322 int error;
323 char *buf = NULL, *buf1 = NULL;
324 GLint length = 0;
326 /* If the client has the opposite byte order, swap the contextTag and
327 * the name.
329 if ( need_swap ) {
330 __GLX_SWAP_INT(pc + 4);
331 __GLX_SWAP_INT(pc + __GLX_SINGLE_HDR_SIZE);
334 cx = __glXForceCurrent(cl, __GLX_GET_SINGLE_CONTEXT_TAG(pc), &error);
335 if (!cx) {
336 return error;
339 pc += __GLX_SINGLE_HDR_SIZE;
340 name = *(GLenum *)(pc + 0);
341 string = (const char *) CALL_GetString( GET_DISPATCH(), (name) );
342 client = cl->client;
345 ** Restrict extensions to those that are supported by both the
346 ** implementation and the connection. That is, return the
347 ** intersection of client, server, and core extension strings.
349 if (name == GL_EXTENSIONS) {
350 buf1 = __glXcombine_strings(string,
351 cl->GLClientextensions);
352 buf = __glXcombine_strings(buf1,
353 cx->pGlxScreen->GLextensions);
354 if (buf1 != NULL) {
355 xfree(buf1);
357 string = buf;
359 else if ( name == GL_VERSION ) {
360 if ( atof( string ) > atof( GLServerVersion ) ) {
361 buf = xalloc( strlen( string ) + strlen( GLServerVersion ) + 4 );
362 if ( buf == NULL ) {
363 string = GLServerVersion;
365 else {
366 sprintf( buf, "%s (%s)", GLServerVersion, string );
367 string = buf;
371 if (string) {
372 length = strlen((const char *) string) + 1;
375 __GLX_BEGIN_REPLY(length);
376 __GLX_PUT_SIZE(length);
378 if ( need_swap ) {
379 __GLX_SWAP_REPLY_SIZE();
380 __GLX_SWAP_REPLY_HEADER();
383 __GLX_SEND_HEADER();
384 WriteToClient(client, length, (char *) string);
385 if (buf != NULL)
386 xfree(buf);
388 return Success;
391 int __glXDisp_GetString(__GLXclientState *cl, GLbyte *pc)
393 return DoGetString(cl, pc, GL_FALSE);