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