glx: fix request lengths
[mesa.git] / src / glx / glx_pbuffer.c
1 /*
2 * (C) Copyright IBM Corporation 2004
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 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glx_pbuffer.c
27 * Implementation of pbuffer related functions.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31
32 #include <inttypes.h>
33 #include "glxclient.h"
34 #include <X11/extensions/extutil.h>
35 #include <X11/extensions/Xext.h>
36 #include <assert.h>
37 #include <string.h>
38 #include "glxextensions.h"
39
40 #ifdef GLX_USE_APPLEGL
41 #include <pthread.h>
42 #include "apple_glx_drawable.h"
43 #include "glx_error.h"
44 #endif
45
46 #define WARN_ONCE_GLX_1_3(a, b) { \
47 static int warned=1; \
48 if(warned) { \
49 warn_GLX_1_3((a), b ); \
50 warned=0; \
51 } \
52 }
53
54 /**
55 * Emit a warning when clients use GLX 1.3 functions on pre-1.3 systems.
56 */
57 static void
58 warn_GLX_1_3(Display * dpy, const char *function_name)
59 {
60 struct glx_display *priv = __glXInitialize(dpy);
61
62 if (priv->minorVersion < 3) {
63 fprintf(stderr,
64 "WARNING: Application calling GLX 1.3 function \"%s\" "
65 "when GLX 1.3 is not supported! This is an application bug!\n",
66 function_name);
67 }
68 }
69
70 #ifndef GLX_USE_APPLEGL
71 /**
72 * Change a drawable's attribute.
73 *
74 * This function is used to implement \c glXSelectEvent and
75 * \c glXSelectEventSGIX.
76 *
77 * \note
78 * This function dynamically determines whether to use the SGIX_pbuffer
79 * version of the protocol or the GLX 1.3 version of the protocol.
80 *
81 * \todo
82 * This function needs to be modified to work with direct-rendering drivers.
83 */
84 static void
85 ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
86 const CARD32 * attribs, size_t num_attribs)
87 {
88 struct glx_display *priv = __glXInitialize(dpy);
89 #ifdef GLX_DIRECT_RENDERING
90 __GLXDRIdrawable *pdraw;
91 #endif
92 CARD32 *output;
93 CARD8 opcode;
94 int i;
95
96 if ((dpy == NULL) || (drawable == 0)) {
97 return;
98 }
99
100 opcode = __glXSetupForCommand(dpy);
101 if (!opcode)
102 return;
103
104 LockDisplay(dpy);
105
106 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
107 xGLXChangeDrawableAttributesReq *req;
108
109 GetReqExtra(GLXChangeDrawableAttributes, 8 * num_attribs, req);
110 output = (CARD32 *) (req + 1);
111
112 req->reqType = opcode;
113 req->glxCode = X_GLXChangeDrawableAttributes;
114 req->drawable = drawable;
115 req->numAttribs = (CARD32) num_attribs;
116 }
117 else {
118 xGLXVendorPrivateWithReplyReq *vpreq;
119
120 GetReqExtra(GLXVendorPrivateWithReply, 4 + (8 * num_attribs), vpreq);
121 output = (CARD32 *) (vpreq + 1);
122
123 vpreq->reqType = opcode;
124 vpreq->glxCode = X_GLXVendorPrivateWithReply;
125 vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
126
127 output[0] = (CARD32) drawable;
128 output++;
129 }
130
131 (void) memcpy(output, attribs, sizeof(CARD32) * 2 * num_attribs);
132
133 UnlockDisplay(dpy);
134 SyncHandle();
135
136 #ifdef GLX_DIRECT_RENDERING
137 pdraw = GetGLXDRIDrawable(dpy, drawable);
138
139 for (i = 0; i < num_attribs; i++) {
140 switch(attribs[i * 2]) {
141 case GLX_EVENT_MASK:
142 /* Keep a local copy for masking out DRI2 proto events as needed */
143 pdraw->eventMask = attribs[i * 2 + 1];
144 break;
145 }
146 }
147 #endif
148
149 return;
150 }
151
152
153 #ifdef GLX_DIRECT_RENDERING
154 static GLenum
155 determineTextureTarget(const int *attribs, int numAttribs)
156 {
157 GLenum target = 0;
158 int i;
159
160 for (i = 0; i < numAttribs; i++) {
161 if (attribs[2 * i] == GLX_TEXTURE_TARGET_EXT) {
162 switch (attribs[2 * i + 1]) {
163 case GLX_TEXTURE_2D_EXT:
164 target = GL_TEXTURE_2D;
165 break;
166 case GLX_TEXTURE_RECTANGLE_EXT:
167 target = GL_TEXTURE_RECTANGLE_ARB;
168 break;
169 }
170 }
171 }
172
173 return target;
174 }
175
176 static GLenum
177 determineTextureFormat(const int *attribs, int numAttribs)
178 {
179 int i;
180
181 for (i = 0; i < numAttribs; i++) {
182 if (attribs[2 * i] == GLX_TEXTURE_FORMAT_EXT)
183 return attribs[2 * i + 1];
184 }
185
186 return 0;
187 }
188
189 static void
190 CreateDRIDrawable(Display *dpy, struct glx_config *config,
191 XID drawable, XID glxdrawable,
192 const int *attrib_list, size_t num_attribs)
193 {
194 struct glx_display *const priv = __glXInitialize(dpy);
195 __GLXDRIdrawable *pdraw;
196 struct glx_screen *psc;
197
198 psc = priv->screens[config->screen];
199 if (psc->driScreen == NULL)
200 return;
201
202 pdraw = psc->driScreen->createDrawable(psc, drawable,
203 glxdrawable, config);
204 if (pdraw == NULL) {
205 fprintf(stderr, "failed to create drawable\n");
206 return;
207 }
208
209 if (__glxHashInsert(priv->drawHash, glxdrawable, pdraw)) {
210 (*pdraw->destroyDrawable) (pdraw);
211 return; /* FIXME: Check what we're supposed to do here... */
212 }
213
214 pdraw->textureTarget = determineTextureTarget(attrib_list, num_attribs);
215 pdraw->textureFormat = determineTextureFormat(attrib_list, num_attribs);
216 }
217
218 static void
219 DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
220 {
221 struct glx_display *const priv = __glXInitialize(dpy);
222 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
223 XID xid;
224
225 if (pdraw != NULL) {
226 xid = pdraw->xDrawable;
227 (*pdraw->destroyDrawable) (pdraw);
228 __glxHashDelete(priv->drawHash, drawable);
229 if (destroy_xdrawable)
230 XFreePixmap(priv->dpy, xid);
231 }
232 }
233
234 #else
235
236 static void
237 CreateDRIDrawable(Display *dpy, const struct glx_config * fbconfig,
238 XID drawable, XID glxdrawable,
239 const int *attrib_list, size_t num_attribs)
240 {
241 }
242
243 static void
244 DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
245 {
246 }
247
248 #endif
249
250 /**
251 * Get a drawable's attribute.
252 *
253 * This function is used to implement \c glXGetSelectedEvent and
254 * \c glXGetSelectedEventSGIX.
255 *
256 * \note
257 * This function dynamically determines whether to use the SGIX_pbuffer
258 * version of the protocol or the GLX 1.3 version of the protocol.
259 *
260 * \todo
261 * The number of attributes returned is likely to be small, probably less than
262 * 10. Given that, this routine should try to use an array on the stack to
263 * capture the reply rather than always calling Xmalloc.
264 *
265 * \todo
266 * This function needs to be modified to work with direct-rendering drivers.
267 */
268 static int
269 GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
270 int attribute, unsigned int *value)
271 {
272 struct glx_display *priv;
273 xGLXGetDrawableAttributesReply reply;
274 CARD32 *data;
275 CARD8 opcode;
276 unsigned int length;
277 unsigned int i;
278 unsigned int num_attributes;
279 GLboolean use_glx_1_3;
280
281 if ((dpy == NULL) || (drawable == 0)) {
282 return 0;
283 }
284
285 priv = __glXInitialize(dpy);
286 use_glx_1_3 = ((priv->majorVersion > 1) || (priv->minorVersion >= 3));
287
288 *value = 0;
289
290
291 opcode = __glXSetupForCommand(dpy);
292 if (!opcode)
293 return 0;
294
295 LockDisplay(dpy);
296
297 if (use_glx_1_3) {
298 xGLXGetDrawableAttributesReq *req;
299
300 GetReq(GLXGetDrawableAttributes, req);
301 req->reqType = opcode;
302 req->glxCode = X_GLXGetDrawableAttributes;
303 req->drawable = drawable;
304 }
305 else {
306 xGLXVendorPrivateWithReplyReq *vpreq;
307
308 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
309 data = (CARD32 *) (vpreq + 1);
310 data[0] = (CARD32) drawable;
311
312 vpreq->reqType = opcode;
313 vpreq->glxCode = X_GLXVendorPrivateWithReply;
314 vpreq->vendorCode = X_GLXvop_GetDrawableAttributesSGIX;
315 }
316
317 _XReply(dpy, (xReply *) & reply, 0, False);
318
319 if (reply.type == X_Error) {
320 UnlockDisplay(dpy);
321 SyncHandle();
322 return 0;
323 }
324
325 length = reply.length;
326 if (length) {
327 num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
328 data = (CARD32 *) Xmalloc(length * sizeof(CARD32));
329 if (data == NULL) {
330 /* Throw data on the floor */
331 _XEatData(dpy, length);
332 }
333 else {
334 _XRead(dpy, (char *) data, length * sizeof(CARD32));
335
336 /* Search the set of returned attributes for the attribute requested by
337 * the caller.
338 */
339 for (i = 0; i < num_attributes; i++) {
340 if (data[i * 2] == attribute) {
341 *value = data[(i * 2) + 1];
342 break;
343 }
344 }
345
346 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
347 {
348 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
349
350 if (pdraw != NULL && !pdraw->textureTarget)
351 pdraw->textureTarget =
352 determineTextureTarget((const int *) data, num_attributes);
353 if (pdraw != NULL && !pdraw->textureFormat)
354 pdraw->textureFormat =
355 determineTextureFormat((const int *) data, num_attributes);
356 }
357 #endif
358
359 Xfree(data);
360 }
361 }
362
363 UnlockDisplay(dpy);
364 SyncHandle();
365
366 return 0;
367 }
368
369 /**
370 * Create a non-pbuffer GLX drawable.
371 *
372 * \todo
373 * This function needs to be modified to work with direct-rendering drivers.
374 */
375 static GLXDrawable
376 CreateDrawable(Display *dpy, struct glx_config *config,
377 Drawable drawable, const int *attrib_list, CARD8 glxCode)
378 {
379 xGLXCreateWindowReq *req;
380 CARD32 *data;
381 unsigned int i;
382 CARD8 opcode;
383
384 i = 0;
385 if (attrib_list) {
386 while (attrib_list[i * 2] != None)
387 i++;
388 }
389
390 opcode = __glXSetupForCommand(dpy);
391 if (!opcode)
392 return None;
393
394 LockDisplay(dpy);
395 GetReqExtra(GLXCreateWindow, 8 * i, req);
396 data = (CARD32 *) (req + 1);
397
398 req->reqType = opcode;
399 req->glxCode = glxCode;
400 req->screen = config->screen;
401 req->fbconfig = config->fbconfigID;
402 req->window = drawable;
403 req->glxwindow = XAllocID(dpy);
404 req->numAttribs = i;
405
406 if (attrib_list)
407 memcpy(data, attrib_list, 8 * i);
408
409 UnlockDisplay(dpy);
410 SyncHandle();
411
412 CreateDRIDrawable(dpy, config, drawable, req->glxwindow, attrib_list, i);
413
414 return req->glxwindow;
415 }
416
417
418 /**
419 * Destroy a non-pbuffer GLX drawable.
420 */
421 static void
422 DestroyDrawable(Display * dpy, GLXDrawable drawable, CARD32 glxCode)
423 {
424 xGLXDestroyPbufferReq *req;
425 CARD8 opcode;
426
427 if ((dpy == NULL) || (drawable == 0)) {
428 return;
429 }
430
431
432 opcode = __glXSetupForCommand(dpy);
433 if (!opcode)
434 return;
435
436 LockDisplay(dpy);
437
438 GetReq(GLXDestroyPbuffer, req);
439 req->reqType = opcode;
440 req->glxCode = glxCode;
441 req->pbuffer = (GLXPbuffer) drawable;
442
443 UnlockDisplay(dpy);
444 SyncHandle();
445
446 DestroyDRIDrawable(dpy, drawable, GL_FALSE);
447
448 return;
449 }
450
451
452 /**
453 * Create a pbuffer.
454 *
455 * This function is used to implement \c glXCreatePbuffer and
456 * \c glXCreateGLXPbufferSGIX.
457 *
458 * \note
459 * This function dynamically determines whether to use the SGIX_pbuffer
460 * version of the protocol or the GLX 1.3 version of the protocol.
461 *
462 * \todo
463 * This function needs to be modified to work with direct-rendering drivers.
464 */
465 static GLXDrawable
466 CreatePbuffer(Display * dpy, struct glx_config *config,
467 unsigned int width, unsigned int height,
468 const int *attrib_list, GLboolean size_in_attribs)
469 {
470 struct glx_display *priv = __glXInitialize(dpy);
471 GLXDrawable id = 0;
472 CARD32 *data;
473 CARD8 opcode;
474 unsigned int i;
475 Pixmap pixmap;
476
477 i = 0;
478 if (attrib_list) {
479 while (attrib_list[i * 2])
480 i++;
481 }
482
483 opcode = __glXSetupForCommand(dpy);
484 if (!opcode)
485 return None;
486
487 LockDisplay(dpy);
488 id = XAllocID(dpy);
489
490 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
491 xGLXCreatePbufferReq *req;
492 unsigned int extra = (size_in_attribs) ? 0 : 2;
493
494 GetReqExtra(GLXCreatePbuffer, (8 * (i + extra)), req);
495 data = (CARD32 *) (req + 1);
496
497 req->reqType = opcode;
498 req->glxCode = X_GLXCreatePbuffer;
499 req->screen = config->screen;
500 req->fbconfig = config->fbconfigID;
501 req->pbuffer = id;
502 req->numAttribs = i + extra;
503
504 if (!size_in_attribs) {
505 data[(2 * i) + 0] = GLX_PBUFFER_WIDTH;
506 data[(2 * i) + 1] = width;
507 data[(2 * i) + 2] = GLX_PBUFFER_HEIGHT;
508 data[(2 * i) + 3] = height;
509 data += 4;
510 }
511 }
512 else {
513 xGLXVendorPrivateReq *vpreq;
514
515 GetReqExtra(GLXVendorPrivate, 20 + (8 * i), vpreq);
516 data = (CARD32 *) (vpreq + 1);
517
518 vpreq->reqType = opcode;
519 vpreq->glxCode = X_GLXVendorPrivate;
520 vpreq->vendorCode = X_GLXvop_CreateGLXPbufferSGIX;
521
522 data[0] = config->screen;
523 data[1] = config->fbconfigID;
524 data[2] = id;
525 data[3] = width;
526 data[4] = height;
527 data += 5;
528 }
529
530 (void) memcpy(data, attrib_list, sizeof(CARD32) * 2 * i);
531
532 UnlockDisplay(dpy);
533 SyncHandle();
534
535 pixmap = XCreatePixmap(dpy, RootWindow(dpy, config->screen),
536 width, height, config->rgbBits);
537
538 CreateDRIDrawable(dpy, config, pixmap, id, attrib_list, i);
539
540 return id;
541 }
542
543 /**
544 * Destroy a pbuffer.
545 *
546 * This function is used to implement \c glXDestroyPbuffer and
547 * \c glXDestroyGLXPbufferSGIX.
548 *
549 * \note
550 * This function dynamically determines whether to use the SGIX_pbuffer
551 * version of the protocol or the GLX 1.3 version of the protocol.
552 */
553 static void
554 DestroyPbuffer(Display * dpy, GLXDrawable drawable)
555 {
556 struct glx_display *priv = __glXInitialize(dpy);
557 CARD8 opcode;
558
559 if ((dpy == NULL) || (drawable == 0)) {
560 return;
561 }
562
563 opcode = __glXSetupForCommand(dpy);
564 if (!opcode)
565 return;
566
567 LockDisplay(dpy);
568
569 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
570 xGLXDestroyPbufferReq *req;
571
572 GetReq(GLXDestroyPbuffer, req);
573 req->reqType = opcode;
574 req->glxCode = X_GLXDestroyPbuffer;
575 req->pbuffer = (GLXPbuffer) drawable;
576 }
577 else {
578 xGLXVendorPrivateWithReplyReq *vpreq;
579 CARD32 *data;
580
581 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
582 data = (CARD32 *) (vpreq + 1);
583
584 data[0] = (CARD32) drawable;
585
586 vpreq->reqType = opcode;
587 vpreq->glxCode = X_GLXVendorPrivateWithReply;
588 vpreq->vendorCode = X_GLXvop_DestroyGLXPbufferSGIX;
589 }
590
591 UnlockDisplay(dpy);
592 SyncHandle();
593
594 DestroyDRIDrawable(dpy, drawable, GL_TRUE);
595
596 return;
597 }
598
599 /**
600 * Create a new pbuffer.
601 */
602 _X_EXPORT GLXPbufferSGIX
603 glXCreateGLXPbufferSGIX(Display * dpy, GLXFBConfigSGIX config,
604 unsigned int width, unsigned int height,
605 int *attrib_list)
606 {
607 return (GLXPbufferSGIX) CreatePbuffer(dpy, (struct glx_config *) config,
608 width, height,
609 attrib_list, GL_FALSE);
610 }
611
612 #endif /* GLX_USE_APPLEGL */
613
614 /**
615 * Create a new pbuffer.
616 */
617 _X_EXPORT GLXPbuffer
618 glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
619 {
620 int i, width, height;
621 #ifdef GLX_USE_APPLEGL
622 GLXPbuffer result;
623 int errorcode;
624 #endif
625
626 width = 0;
627 height = 0;
628
629 WARN_ONCE_GLX_1_3(dpy, __func__);
630
631 #ifdef GLX_USE_APPLEGL
632 for (i = 0; attrib_list[i]; ++i) {
633 switch (attrib_list[i]) {
634 case GLX_PBUFFER_WIDTH:
635 width = attrib_list[i + 1];
636 ++i;
637 break;
638
639 case GLX_PBUFFER_HEIGHT:
640 height = attrib_list[i + 1];
641 ++i;
642 break;
643
644 case GLX_LARGEST_PBUFFER:
645 /* This is a hint we should probably handle, but how? */
646 ++i;
647 break;
648
649 case GLX_PRESERVED_CONTENTS:
650 /* The contents are always preserved with AppleSGLX with CGL. */
651 ++i;
652 break;
653
654 default:
655 return None;
656 }
657 }
658
659 if (apple_glx_pbuffer_create(dpy, config, width, height, &errorcode,
660 &result)) {
661 /*
662 * apple_glx_pbuffer_create only sets the errorcode to core X11
663 * errors.
664 */
665 __glXSendError(dpy, errorcode, 0, X_GLXCreatePbuffer, true);
666
667 return None;
668 }
669
670 return result;
671 #else
672 for (i = 0; attrib_list[i * 2]; i++) {
673 switch (attrib_list[i * 2]) {
674 case GLX_PBUFFER_WIDTH:
675 width = attrib_list[i * 2 + 1];
676 break;
677 case GLX_PBUFFER_HEIGHT:
678 height = attrib_list[i * 2 + 1];
679 break;
680 }
681 }
682
683 return (GLXPbuffer) CreatePbuffer(dpy, (struct glx_config *) config,
684 width, height, attrib_list, GL_TRUE);
685 #endif
686 }
687
688
689 /**
690 * Destroy an existing pbuffer.
691 */
692 _X_EXPORT void
693 glXDestroyPbuffer(Display * dpy, GLXPbuffer pbuf)
694 {
695 #ifdef GLX_USE_APPLEGL
696 if (apple_glx_pbuffer_destroy(dpy, pbuf)) {
697 __glXSendError(dpy, GLXBadPbuffer, pbuf, X_GLXDestroyPbuffer, false);
698 }
699 #else
700 DestroyPbuffer(dpy, pbuf);
701 #endif
702 }
703
704
705 /**
706 * Query an attribute of a drawable.
707 */
708 _X_EXPORT void
709 glXQueryDrawable(Display * dpy, GLXDrawable drawable,
710 int attribute, unsigned int *value)
711 {
712 WARN_ONCE_GLX_1_3(dpy, __func__);
713 #ifdef GLX_USE_APPLEGL
714 Window root;
715 int x, y;
716 unsigned int width, height, bd, depth;
717
718 if (apple_glx_pixmap_query(drawable, attribute, value))
719 return; /*done */
720
721 if (apple_glx_pbuffer_query(drawable, attribute, value))
722 return; /*done */
723
724 /*
725 * The OpenGL spec states that we should report GLXBadDrawable if
726 * the drawable is invalid, however doing so would require that we
727 * use XSetErrorHandler(), which is known to not be thread safe.
728 * If we use a round-trip call to validate the drawable, there could
729 * be a race, so instead we just opt in favor of letting the
730 * XGetGeometry request fail with a GetGeometry request X error
731 * rather than GLXBadDrawable, in what is hoped to be a rare
732 * case of an invalid drawable. In practice most and possibly all
733 * X11 apps using GLX shouldn't notice a difference.
734 */
735 if (XGetGeometry
736 (dpy, drawable, &root, &x, &y, &width, &height, &bd, &depth)) {
737 switch (attribute) {
738 case GLX_WIDTH:
739 *value = width;
740 break;
741
742 case GLX_HEIGHT:
743 *value = height;
744 break;
745 }
746 }
747 #else
748 GetDrawableAttribute(dpy, drawable, attribute, value);
749 #endif
750 }
751
752
753 #ifndef GLX_USE_APPLEGL
754 /**
755 * Query an attribute of a pbuffer.
756 */
757 _X_EXPORT int
758 glXQueryGLXPbufferSGIX(Display * dpy, GLXPbufferSGIX drawable,
759 int attribute, unsigned int *value)
760 {
761 return GetDrawableAttribute(dpy, drawable, attribute, value);
762 }
763 #endif
764
765 /**
766 * Select the event mask for a drawable.
767 */
768 _X_EXPORT void
769 glXSelectEvent(Display * dpy, GLXDrawable drawable, unsigned long mask)
770 {
771 #ifdef GLX_USE_APPLEGL
772 XWindowAttributes xwattr;
773
774 if (apple_glx_pbuffer_set_event_mask(drawable, mask))
775 return; /*done */
776
777 /*
778 * The spec allows a window, but currently there are no valid
779 * events for a window, so do nothing.
780 */
781 if (XGetWindowAttributes(dpy, drawable, &xwattr))
782 return; /*done */
783 /* The drawable seems to be invalid. Report an error. */
784
785 __glXSendError(dpy, GLXBadDrawable, drawable,
786 X_GLXChangeDrawableAttributes, false);
787 #else
788 CARD32 attribs[2];
789
790 attribs[0] = (CARD32) GLX_EVENT_MASK;
791 attribs[1] = (CARD32) mask;
792
793 ChangeDrawableAttribute(dpy, drawable, attribs, 1);
794 #endif
795 }
796
797
798 /**
799 * Get the selected event mask for a drawable.
800 */
801 _X_EXPORT void
802 glXGetSelectedEvent(Display * dpy, GLXDrawable drawable, unsigned long *mask)
803 {
804 #ifdef GLX_USE_APPLEGL
805 XWindowAttributes xwattr;
806
807 if (apple_glx_pbuffer_get_event_mask(drawable, mask))
808 return; /*done */
809
810 /*
811 * The spec allows a window, but currently there are no valid
812 * events for a window, so do nothing, but set the mask to 0.
813 */
814 if (XGetWindowAttributes(dpy, drawable, &xwattr)) {
815 /* The window is valid, so set the mask to 0. */
816 *mask = 0;
817 return; /*done */
818 }
819 /* The drawable seems to be invalid. Report an error. */
820
821 __glXSendError(dpy, GLXBadDrawable, drawable, X_GLXGetDrawableAttributes,
822 true);
823 #else
824 unsigned int value;
825
826
827 /* The non-sense with value is required because on LP64 platforms
828 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
829 * we could just type-cast the pointer, but why?
830 */
831
832 GetDrawableAttribute(dpy, drawable, GLX_EVENT_MASK_SGIX, &value);
833 *mask = value;
834 #endif
835 }
836
837
838 _X_EXPORT GLXPixmap
839 glXCreatePixmap(Display * dpy, GLXFBConfig config, Pixmap pixmap,
840 const int *attrib_list)
841 {
842 WARN_ONCE_GLX_1_3(dpy, __func__);
843
844 #ifdef GLX_USE_APPLEGL
845 const struct glx_config *modes = (const __GLcontextModes *) config;
846
847 if (apple_glx_pixmap_create(dpy, modes->screen, pixmap, modes))
848 return None;
849
850 return pixmap;
851 #else
852 return CreateDrawable(dpy, (struct glx_config *) config,
853 (Drawable) pixmap, attrib_list, X_GLXCreatePixmap);
854 #endif
855 }
856
857
858 _X_EXPORT GLXWindow
859 glXCreateWindow(Display * dpy, GLXFBConfig config, Window win,
860 const int *attrib_list)
861 {
862 WARN_ONCE_GLX_1_3(dpy, __func__);
863 #ifdef GLX_USE_APPLEGL
864 XWindowAttributes xwattr;
865 XVisualInfo *visinfo;
866
867 (void) attrib_list; /*unused according to GLX 1.4 */
868
869 XGetWindowAttributes(dpy, win, &xwattr);
870
871 visinfo = glXGetVisualFromFBConfig(dpy, config);
872
873 if (NULL == visinfo) {
874 __glXSendError(dpy, GLXBadFBConfig, 0, X_GLXCreateWindow, false);
875 return None;
876 }
877
878 if (visinfo->visualid != XVisualIDFromVisual(xwattr.visual)) {
879 __glXSendError(dpy, BadMatch, 0, X_GLXCreateWindow, true);
880 return None;
881 }
882
883 XFree(visinfo);
884
885 return win;
886 #else
887 return CreateDrawable(dpy, (struct glx_config *) config,
888 (Drawable) win, attrib_list, X_GLXCreateWindow);
889 #endif
890 }
891
892
893 _X_EXPORT void
894 glXDestroyPixmap(Display * dpy, GLXPixmap pixmap)
895 {
896 WARN_ONCE_GLX_1_3(dpy, __func__);
897 #ifdef GLX_USE_APPLEGL
898 if (apple_glx_pixmap_destroy(dpy, pixmap))
899 __glXSendError(dpy, GLXBadPixmap, pixmap, X_GLXDestroyPixmap, false);
900 #else
901 DestroyDrawable(dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap);
902 #endif
903 }
904
905
906 _X_EXPORT void
907 glXDestroyWindow(Display * dpy, GLXWindow win)
908 {
909 WARN_ONCE_GLX_1_3(dpy, __func__);
910 #ifndef GLX_USE_APPLEGL
911 DestroyDrawable(dpy, (GLXDrawable) win, X_GLXDestroyWindow);
912 #endif
913 }
914
915 #ifndef GLX_USE_APPLEGL
916 _X_EXPORT
917 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
918 (Display * dpy, GLXPbufferSGIX pbuf),
919 (dpy, pbuf), glXDestroyPbuffer)
920
921 _X_EXPORT
922 GLX_ALIAS_VOID(glXSelectEventSGIX,
923 (Display * dpy, GLXDrawable drawable,
924 unsigned long mask), (dpy, drawable, mask), glXSelectEvent)
925
926 _X_EXPORT
927 GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
928 (Display * dpy, GLXDrawable drawable,
929 unsigned long *mask), (dpy, drawable, mask),
930 glXGetSelectedEvent)
931 #endif