1848b8605Smrg/* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.12 2001/08/27 17:40:57 dawes Exp $ */
2848b8605Smrg/**************************************************************************
3848b8605Smrg
4848b8605SmrgCopyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
5848b8605SmrgCopyright 2000 VA Linux Systems, Inc.
6848b8605SmrgCopyright (c) 2002, 2008 Apple Computer, Inc.
7848b8605SmrgAll Rights Reserved.
8848b8605Smrg
9848b8605SmrgPermission is hereby granted, free of charge, to any person obtaining a
10848b8605Smrgcopy of this software and associated documentation files (the
11848b8605Smrg"Software"), to deal in the Software without restriction, including
12848b8605Smrgwithout limitation the rights to use, copy, modify, merge, publish,
13848b8605Smrgdistribute, sub license, and/or sell copies of the Software, and to
14848b8605Smrgpermit persons to whom the Software is furnished to do so, subject to
15848b8605Smrgthe following conditions:
16848b8605Smrg
17848b8605SmrgThe above copyright notice and this permission notice (including the
18848b8605Smrgnext paragraph) shall be included in all copies or substantial portions
19848b8605Smrgof the Software.
20848b8605Smrg
21848b8605SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22848b8605SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23848b8605SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24848b8605SmrgIN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25848b8605SmrgANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26848b8605SmrgTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27848b8605SmrgSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28848b8605Smrg
29848b8605Smrg**************************************************************************/
30848b8605Smrg
31848b8605Smrg/*
32848b8605Smrg * Authors:
33848b8605Smrg *   Kevin E. Martin <martin@valinux.com>
34848b8605Smrg *   Jens Owen <jens@valinux.com>
35848b8605Smrg *   Rickard E. (Rik) Faith <faith@valinux.com>
36848b8605Smrg *
37848b8605Smrg */
38848b8605Smrg
39848b8605Smrg/* THIS IS NOT AN X CONSORTIUM STANDARD */
40848b8605Smrg
41848b8605Smrg#include <X11/Xlibint.h>
42848b8605Smrg#include "appledristr.h"
43848b8605Smrg#include <X11/extensions/Xext.h>
44848b8605Smrg#include <X11/extensions/extutil.h>
45848b8605Smrg#include <stdio.h>
46848b8605Smrg
47848b8605Smrgstatic XExtensionInfo _appledri_info_data;
48848b8605Smrgstatic XExtensionInfo *appledri_info = &_appledri_info_data;
49848b8605Smrgstatic char *appledri_extension_name = APPLEDRINAME;
50848b8605Smrg
51848b8605Smrg#define AppleDRICheckExtension(dpy,i,val) \
52848b8605Smrg  XextCheckExtension (dpy, i, appledri_extension_name, val)
53848b8605Smrg
54848b8605Smrg/*****************************************************************************
55848b8605Smrg *                                                                           *
56848b8605Smrg *			   private utility routines                          *
57848b8605Smrg *                                                                           *
58848b8605Smrg *****************************************************************************/
59848b8605Smrg
60848b8605Smrgstatic int close_display(Display * dpy, XExtCodes * extCodes);
61848b8605Smrgstatic Bool wire_to_event(Display * dpy, XEvent * re, xEvent * event);
62848b8605Smrg
63848b8605Smrgstatic /* const */ XExtensionHooks appledri_extension_hooks = {
64848b8605Smrg   NULL,                        /* create_gc */
65848b8605Smrg   NULL,                        /* copy_gc */
66848b8605Smrg   NULL,                        /* flush_gc */
67848b8605Smrg   NULL,                        /* free_gc */
68848b8605Smrg   NULL,                        /* create_font */
69848b8605Smrg   NULL,                        /* free_font */
70848b8605Smrg   close_display,               /* close_display */
71848b8605Smrg   wire_to_event,               /* wire_to_event */
72848b8605Smrg   NULL,                        /* event_to_wire */
73848b8605Smrg   NULL,                        /* error */
74848b8605Smrg   NULL,                        /* error_string */
75848b8605Smrg};
76848b8605Smrg
77848b8605Smrgstatic
78848b8605SmrgXEXT_GENERATE_FIND_DISPLAY(find_display, appledri_info,
79848b8605Smrg                           appledri_extension_name,
80848b8605Smrg                           &appledri_extension_hooks,
81848b8605Smrg                           AppleDRINumberEvents, NULL)
82848b8605Smrg
83848b8605Smrg     static XEXT_GENERATE_CLOSE_DISPLAY(close_display, appledri_info)
84848b8605Smrg
85848b8605Smrg     static void (*surface_notify_handler) ();
86848b8605Smrg
87848b8605Smrg     void *XAppleDRISetSurfaceNotifyHandler(void (*fun) ())
88848b8605Smrg{
89848b8605Smrg   void *old = surface_notify_handler;
90848b8605Smrg   surface_notify_handler = fun;
91848b8605Smrg   return old;
92848b8605Smrg}
93848b8605Smrg
94848b8605Smrgstatic Bool
95848b8605Smrgwire_to_event(Display *dpy, XEvent *re, xEvent *event)
96848b8605Smrg{
97848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
98848b8605Smrg   xAppleDRINotifyEvent *sevent;
99848b8605Smrg
100848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
101848b8605Smrg
102848b8605Smrg   switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
103848b8605Smrg   case AppleDRISurfaceNotify:
104848b8605Smrg      sevent = (xAppleDRINotifyEvent *) event;
105848b8605Smrg      if (surface_notify_handler != NULL) {
106848b8605Smrg         (*surface_notify_handler) (dpy, (unsigned int) sevent->arg,
107848b8605Smrg                                    (int) sevent->kind);
108848b8605Smrg      }
109848b8605Smrg      return False;
110848b8605Smrg   }
111848b8605Smrg   return False;
112848b8605Smrg}
113848b8605Smrg
114848b8605Smrg/*****************************************************************************
115848b8605Smrg *                                                                           *
116848b8605Smrg *		    public Apple-DRI Extension routines                      *
117848b8605Smrg *                                                                           *
118848b8605Smrg *****************************************************************************/
119848b8605Smrg
120848b8605Smrg#if 0
121848b8605Smrg#include <stdio.h>
122848b8605Smrg#define TRACE(msg)  fprintf(stderr, "AppleDRI%s\n", msg);
123848b8605Smrg#else
124848b8605Smrg#define TRACE(msg)
125848b8605Smrg#endif
126848b8605Smrg
127848b8605Smrg
128848b8605SmrgBool
129848b8605SmrgXAppleDRIQueryExtension(dpy, event_basep, error_basep)
130848b8605Smrg     Display *dpy;
131848b8605Smrg     int *event_basep, *error_basep;
132848b8605Smrg{
133848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
134848b8605Smrg
135848b8605Smrg   TRACE("QueryExtension...");
136848b8605Smrg   if (XextHasExtension(info)) {
137848b8605Smrg      *event_basep = info->codes->first_event;
138848b8605Smrg      *error_basep = info->codes->first_error;
139848b8605Smrg      TRACE("QueryExtension... return True");
140848b8605Smrg      return True;
141848b8605Smrg   }
142848b8605Smrg   else {
143848b8605Smrg      TRACE("QueryExtension... return False");
144848b8605Smrg      return False;
145848b8605Smrg   }
146848b8605Smrg}
147848b8605Smrg
148848b8605SmrgBool
149848b8605SmrgXAppleDRIQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
150848b8605Smrg     Display *dpy;
151848b8605Smrg     int *majorVersion;
152848b8605Smrg     int *minorVersion;
153848b8605Smrg     int *patchVersion;
154848b8605Smrg{
155848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
156848b8605Smrg   xAppleDRIQueryVersionReply rep;
157848b8605Smrg   xAppleDRIQueryVersionReq *req;
158848b8605Smrg
159848b8605Smrg   TRACE("QueryVersion...");
160848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
161848b8605Smrg
162848b8605Smrg   LockDisplay(dpy);
163848b8605Smrg   GetReq(AppleDRIQueryVersion, req);
164848b8605Smrg   req->reqType = info->codes->major_opcode;
165848b8605Smrg   req->driReqType = X_AppleDRIQueryVersion;
166848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
167848b8605Smrg      UnlockDisplay(dpy);
168848b8605Smrg      SyncHandle();
169848b8605Smrg      TRACE("QueryVersion... return False");
170848b8605Smrg      return False;
171848b8605Smrg   }
172848b8605Smrg   *majorVersion = rep.majorVersion;
173848b8605Smrg   *minorVersion = rep.minorVersion;
174848b8605Smrg   *patchVersion = rep.patchVersion;
175848b8605Smrg   UnlockDisplay(dpy);
176848b8605Smrg   SyncHandle();
177848b8605Smrg   TRACE("QueryVersion... return True");
178848b8605Smrg   return True;
179848b8605Smrg}
180848b8605Smrg
181848b8605SmrgBool
182848b8605SmrgXAppleDRIQueryDirectRenderingCapable(dpy, screen, isCapable)
183848b8605Smrg     Display *dpy;
184848b8605Smrg     int screen;
185848b8605Smrg     Bool *isCapable;
186848b8605Smrg{
187848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
188848b8605Smrg   xAppleDRIQueryDirectRenderingCapableReply rep;
189848b8605Smrg   xAppleDRIQueryDirectRenderingCapableReq *req;
190848b8605Smrg
191848b8605Smrg   TRACE("QueryDirectRenderingCapable...");
192848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
193848b8605Smrg
194848b8605Smrg   LockDisplay(dpy);
195848b8605Smrg   GetReq(AppleDRIQueryDirectRenderingCapable, req);
196848b8605Smrg   req->reqType = info->codes->major_opcode;
197848b8605Smrg   req->driReqType = X_AppleDRIQueryDirectRenderingCapable;
198848b8605Smrg   req->screen = screen;
199848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
200848b8605Smrg      UnlockDisplay(dpy);
201848b8605Smrg      SyncHandle();
202848b8605Smrg      TRACE("QueryDirectRenderingCapable... return False");
203848b8605Smrg      return False;
204848b8605Smrg   }
205848b8605Smrg   *isCapable = rep.isCapable;
206848b8605Smrg   UnlockDisplay(dpy);
207848b8605Smrg   SyncHandle();
208848b8605Smrg   TRACE("QueryDirectRenderingCapable... return True");
209848b8605Smrg   return True;
210848b8605Smrg}
211848b8605Smrg
212848b8605SmrgBool
213848b8605SmrgXAppleDRIAuthConnection(dpy, screen, magic)
214848b8605Smrg     Display *dpy;
215848b8605Smrg     int screen;
216848b8605Smrg     unsigned int magic;
217848b8605Smrg{
218848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
219848b8605Smrg   xAppleDRIAuthConnectionReq *req;
220848b8605Smrg   xAppleDRIAuthConnectionReply rep;
221848b8605Smrg
222848b8605Smrg   TRACE("AuthConnection...");
223848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
224848b8605Smrg
225848b8605Smrg   LockDisplay(dpy);
226848b8605Smrg   GetReq(AppleDRIAuthConnection, req);
227848b8605Smrg   req->reqType = info->codes->major_opcode;
228848b8605Smrg   req->driReqType = X_AppleDRIAuthConnection;
229848b8605Smrg   req->screen = screen;
230848b8605Smrg   req->magic = magic;
231848b8605Smrg   rep.authenticated = 0;
232848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse) || !rep.authenticated) {
233848b8605Smrg      UnlockDisplay(dpy);
234848b8605Smrg      SyncHandle();
235848b8605Smrg      TRACE("AuthConnection... return False");
236848b8605Smrg      return False;
237848b8605Smrg   }
238848b8605Smrg   UnlockDisplay(dpy);
239848b8605Smrg   SyncHandle();
240848b8605Smrg   TRACE("AuthConnection... return True");
241848b8605Smrg   return True;
242848b8605Smrg}
243848b8605Smrg
244848b8605SmrgBool
245848b8605SmrgXAppleDRICreateSurface(dpy, screen, drawable, client_id, key, uid)
246848b8605Smrg     Display *dpy;
247848b8605Smrg     int screen;
248848b8605Smrg     Drawable drawable;
249848b8605Smrg     unsigned int client_id;
250848b8605Smrg     unsigned int *key;
251848b8605Smrg     unsigned int *uid;
252848b8605Smrg{
253848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
254848b8605Smrg   xAppleDRICreateSurfaceReply rep;
255848b8605Smrg   xAppleDRICreateSurfaceReq *req;
256848b8605Smrg
257848b8605Smrg   TRACE("CreateSurface...");
258848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
259848b8605Smrg
260848b8605Smrg   LockDisplay(dpy);
261848b8605Smrg   GetReq(AppleDRICreateSurface, req);
262848b8605Smrg   req->reqType = info->codes->major_opcode;
263848b8605Smrg   req->driReqType = X_AppleDRICreateSurface;
264848b8605Smrg   req->screen = screen;
265848b8605Smrg   req->drawable = drawable;
266848b8605Smrg   req->client_id = client_id;
267848b8605Smrg   rep.key_0 = rep.key_1 = rep.uid = 0;
268848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse) || !rep.key_0) {
269848b8605Smrg      UnlockDisplay(dpy);
270848b8605Smrg      SyncHandle();
271848b8605Smrg      TRACE("CreateSurface... return False");
272848b8605Smrg      return False;
273848b8605Smrg   }
274848b8605Smrg   key[0] = rep.key_0;
275848b8605Smrg   key[1] = rep.key_1;
276848b8605Smrg   *uid = rep.uid;
277848b8605Smrg   UnlockDisplay(dpy);
278848b8605Smrg   SyncHandle();
279848b8605Smrg   TRACE("CreateSurface... return True");
280848b8605Smrg   return True;
281848b8605Smrg}
282848b8605Smrg
283848b8605SmrgBool
284848b8605SmrgXAppleDRIDestroySurface(dpy, screen, drawable)
285848b8605Smrg     Display *dpy;
286848b8605Smrg     int screen;
287848b8605Smrg     Drawable drawable;
288848b8605Smrg{
289848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
290848b8605Smrg   xAppleDRIDestroySurfaceReq *req;
291848b8605Smrg
292848b8605Smrg   TRACE("DestroySurface...");
293848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
294848b8605Smrg
295848b8605Smrg   LockDisplay(dpy);
296848b8605Smrg   GetReq(AppleDRIDestroySurface, req);
297848b8605Smrg   req->reqType = info->codes->major_opcode;
298848b8605Smrg   req->driReqType = X_AppleDRIDestroySurface;
299848b8605Smrg   req->screen = screen;
300848b8605Smrg   req->drawable = drawable;
301848b8605Smrg   UnlockDisplay(dpy);
302848b8605Smrg   SyncHandle();
303848b8605Smrg   TRACE("DestroySurface... return True");
304848b8605Smrg   return True;
305848b8605Smrg}
306848b8605Smrg
307848b8605SmrgBool
308848b8605SmrgXAppleDRICreateSharedBuffer(Display * dpy, int screen, Drawable drawable,
309848b8605Smrg                            Bool doubleSwap, char *path, size_t pathlen,
310848b8605Smrg                            int *width, int *height)
311848b8605Smrg{
312848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
313848b8605Smrg   xAppleDRICreateSharedBufferReq *req;
314848b8605Smrg   xAppleDRICreateSharedBufferReply rep;
315848b8605Smrg
316848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
317848b8605Smrg
318848b8605Smrg   LockDisplay(dpy);
319848b8605Smrg   GetReq(AppleDRICreateSharedBuffer, req);
320848b8605Smrg   req->reqType = info->codes->major_opcode;
321848b8605Smrg   req->driReqType = X_AppleDRICreateSharedBuffer;
322848b8605Smrg   req->screen = screen;
323848b8605Smrg   req->drawable = drawable;
324848b8605Smrg   req->doubleSwap = doubleSwap;
325848b8605Smrg
326848b8605Smrg
327848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
328848b8605Smrg      puts("REPLY ERROR");
329848b8605Smrg
330848b8605Smrg      UnlockDisplay(dpy);
331848b8605Smrg      SyncHandle();
332848b8605Smrg      return False;
333848b8605Smrg   }
334848b8605Smrg
335848b8605Smrg   /* printf("rep.stringLength %d\n", (int) rep.stringLength); */
336848b8605Smrg
337848b8605Smrg   if (rep.stringLength > 0 && rep.stringLength <= pathlen) {
338848b8605Smrg      _XReadPad(dpy, path, rep.stringLength);
339848b8605Smrg
340848b8605Smrg      /* printf("path: %s\n", path); */
341848b8605Smrg
342848b8605Smrg      *width = rep.width;
343848b8605Smrg      *height = rep.height;
344848b8605Smrg
345848b8605Smrg      UnlockDisplay(dpy);
346848b8605Smrg      SyncHandle();
347848b8605Smrg      return True;
348848b8605Smrg   }
349848b8605Smrg
350848b8605Smrg   UnlockDisplay(dpy);
351848b8605Smrg   SyncHandle();
352848b8605Smrg
353848b8605Smrg   return False;
354848b8605Smrg}
355848b8605Smrg
356848b8605SmrgBool
357848b8605SmrgXAppleDRISwapBuffers(Display * dpy, int screen, Drawable drawable)
358848b8605Smrg{
359848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
360848b8605Smrg   xAppleDRISwapBuffersReq *req;
361848b8605Smrg
362848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
363848b8605Smrg
364848b8605Smrg   LockDisplay(dpy);
365848b8605Smrg   GetReq(AppleDRISwapBuffers, req);
366848b8605Smrg   req->reqType = info->codes->major_opcode;
367848b8605Smrg   req->driReqType = X_AppleDRISwapBuffers;
368848b8605Smrg   req->screen = screen;
369848b8605Smrg   req->drawable = drawable;
370848b8605Smrg   UnlockDisplay(dpy);
371848b8605Smrg   SyncHandle();
372848b8605Smrg
373848b8605Smrg   return True;
374848b8605Smrg}
375848b8605Smrg
376848b8605SmrgBool
377848b8605SmrgXAppleDRICreatePixmap(Display * dpy, int screen, Drawable drawable,
378848b8605Smrg                      int *width, int *height, int *pitch, int *bpp,
379848b8605Smrg                      size_t * size, char *bufname, size_t bufnamesize)
380848b8605Smrg{
381848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
382848b8605Smrg   xAppleDRICreatePixmapReq *req;
383848b8605Smrg   xAppleDRICreatePixmapReply rep;
384848b8605Smrg
385848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
386848b8605Smrg
387848b8605Smrg   LockDisplay(dpy);
388848b8605Smrg   GetReq(AppleDRICreatePixmap, req);
389848b8605Smrg   req->reqType = info->codes->major_opcode;
390848b8605Smrg   req->driReqType = X_AppleDRICreatePixmap;
391848b8605Smrg   req->screen = screen;
392848b8605Smrg   req->drawable = drawable;
393848b8605Smrg
394848b8605Smrg   if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
395848b8605Smrg      UnlockDisplay(dpy);
396848b8605Smrg      SyncHandle();
397848b8605Smrg      return False;
398848b8605Smrg   }
399848b8605Smrg
400848b8605Smrg   /*
401848b8605Smrg      printf("rep.stringLength %d\n", (int) rep.stringLength);
402848b8605Smrg    */
403848b8605Smrg
404848b8605Smrg   if (rep.stringLength > 0 && rep.stringLength <= bufnamesize) {
405848b8605Smrg      _XReadPad(dpy, bufname, rep.stringLength);
406848b8605Smrg
407848b8605Smrg      /* printf("path: %s\n", bufname); */
408848b8605Smrg
409848b8605Smrg      *width = rep.width;
410848b8605Smrg      *height = rep.height;
411848b8605Smrg      *pitch = rep.pitch;
412848b8605Smrg      *bpp = rep.bpp;
413848b8605Smrg      *size = rep.size;
414848b8605Smrg
415848b8605Smrg      UnlockDisplay(dpy);
416848b8605Smrg      SyncHandle();
417848b8605Smrg      return True;
418848b8605Smrg   }
419848b8605Smrg   else if (rep.stringLength > 0) {
420848b8605Smrg      _XEatData(dpy, rep.stringLength);
421848b8605Smrg   }
422848b8605Smrg
423848b8605Smrg   UnlockDisplay(dpy);
424848b8605Smrg   SyncHandle();
425848b8605Smrg
426848b8605Smrg   return True;
427848b8605Smrg}
428848b8605Smrg
429848b8605Smrg/*
430848b8605Smrg * Call it a drawable, because we really don't know what it is
431848b8605Smrg * until it reaches the server, and we should keep that in mind.
432848b8605Smrg */
433848b8605SmrgBool
434848b8605SmrgXAppleDRIDestroyPixmap(Display * dpy, Pixmap drawable)
435848b8605Smrg{
436848b8605Smrg   XExtDisplayInfo *info = find_display(dpy);
437848b8605Smrg   xAppleDRIDestroyPixmapReq *req;
438848b8605Smrg
439848b8605Smrg   AppleDRICheckExtension(dpy, info, False);
440848b8605Smrg
441848b8605Smrg   LockDisplay(dpy);
442848b8605Smrg   GetReq(AppleDRIDestroyPixmap, req);
443848b8605Smrg   req->reqType = info->codes->major_opcode;
444848b8605Smrg   req->driReqType = X_AppleDRIDestroyPixmap;
445848b8605Smrg   req->drawable = drawable;
446848b8605Smrg   UnlockDisplay(dpy);
447848b8605Smrg   SyncHandle();
448848b8605Smrg
449848b8605Smrg   return True;
450848b8605Smrg}
451