Merge branch '7.8'
[mesa.git] / src / glx / apple / appledri.c
1 /* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.12 2001/08/27 17:40:57 dawes Exp $ */
2 /**************************************************************************
3
4 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
5 Copyright 2000 VA Linux Systems, Inc.
6 Copyright (c) 2002, 2008 Apple Computer, Inc.
7 All Rights Reserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Kevin E. Martin <martin@valinux.com>
34 * Jens Owen <jens@valinux.com>
35 * Rickard E. (Rik) Faith <faith@valinux.com>
36 *
37 */
38
39 /* THIS IS NOT AN X CONSORTIUM STANDARD */
40
41 #define NEED_EVENTS
42 #define NEED_REPLIES
43 #include <X11/Xlibint.h>
44 #include "appledristr.h"
45 #include <X11/extensions/Xext.h>
46 #include <X11/extensions/extutil.h>
47 #include <stdio.h>
48
49 static XExtensionInfo _appledri_info_data;
50 static XExtensionInfo *appledri_info = &_appledri_info_data;
51 static char *appledri_extension_name = APPLEDRINAME;
52
53 #define AppleDRICheckExtension(dpy,i,val) \
54 XextCheckExtension (dpy, i, appledri_extension_name, val)
55
56 /*****************************************************************************
57 * *
58 * private utility routines *
59 * *
60 *****************************************************************************/
61
62 static int close_display(Display * dpy, XExtCodes * extCodes);
63 static Bool wire_to_event(Display * dpy, XEvent * re, xEvent * event);
64
65 static /* const */ XExtensionHooks appledri_extension_hooks = {
66 NULL, /* create_gc */
67 NULL, /* copy_gc */
68 NULL, /* flush_gc */
69 NULL, /* free_gc */
70 NULL, /* create_font */
71 NULL, /* free_font */
72 close_display, /* close_display */
73 wire_to_event, /* wire_to_event */
74 NULL, /* event_to_wire */
75 NULL, /* error */
76 NULL, /* error_string */
77 };
78
79 static
80 XEXT_GENERATE_FIND_DISPLAY(find_display, appledri_info,
81 appledri_extension_name,
82 &appledri_extension_hooks,
83 AppleDRINumberEvents, NULL)
84
85 static XEXT_GENERATE_CLOSE_DISPLAY(close_display, appledri_info)
86
87 static void (*surface_notify_handler) ();
88
89 void *XAppleDRISetSurfaceNotifyHandler(void (*fun) ())
90 {
91 void *old = surface_notify_handler;
92 surface_notify_handler = fun;
93 return old;
94 }
95
96 static Bool
97 wire_to_event(Display *dpy, XEvent *re, xEvent *event)
98 {
99 XExtDisplayInfo *info = find_display(dpy);
100 xAppleDRINotifyEvent *sevent;
101
102 AppleDRICheckExtension(dpy, info, False);
103
104 switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
105 case AppleDRISurfaceNotify:
106 sevent = (xAppleDRINotifyEvent *) event;
107 if (surface_notify_handler != NULL) {
108 (*surface_notify_handler) (dpy, (unsigned int) sevent->arg,
109 (int) sevent->kind);
110 }
111 return False;
112 }
113 return False;
114 }
115
116 /*****************************************************************************
117 * *
118 * public Apple-DRI Extension routines *
119 * *
120 *****************************************************************************/
121
122 #if 0
123 #include <stdio.h>
124 #define TRACE(msg) fprintf(stderr, "AppleDRI%s\n", msg);
125 #else
126 #define TRACE(msg)
127 #endif
128
129
130 Bool
131 XAppleDRIQueryExtension(dpy, event_basep, error_basep)
132 Display *dpy;
133 int *event_basep, *error_basep;
134 {
135 XExtDisplayInfo *info = find_display(dpy);
136
137 TRACE("QueryExtension...");
138 if (XextHasExtension(info)) {
139 *event_basep = info->codes->first_event;
140 *error_basep = info->codes->first_error;
141 TRACE("QueryExtension... return True");
142 return True;
143 }
144 else {
145 TRACE("QueryExtension... return False");
146 return False;
147 }
148 }
149
150 Bool
151 XAppleDRIQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
152 Display *dpy;
153 int *majorVersion;
154 int *minorVersion;
155 int *patchVersion;
156 {
157 XExtDisplayInfo *info = find_display(dpy);
158 xAppleDRIQueryVersionReply rep;
159 xAppleDRIQueryVersionReq *req;
160
161 TRACE("QueryVersion...");
162 AppleDRICheckExtension(dpy, info, False);
163
164 LockDisplay(dpy);
165 GetReq(AppleDRIQueryVersion, req);
166 req->reqType = info->codes->major_opcode;
167 req->driReqType = X_AppleDRIQueryVersion;
168 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
169 UnlockDisplay(dpy);
170 SyncHandle();
171 TRACE("QueryVersion... return False");
172 return False;
173 }
174 *majorVersion = rep.majorVersion;
175 *minorVersion = rep.minorVersion;
176 *patchVersion = rep.patchVersion;
177 UnlockDisplay(dpy);
178 SyncHandle();
179 TRACE("QueryVersion... return True");
180 return True;
181 }
182
183 Bool
184 XAppleDRIQueryDirectRenderingCapable(dpy, screen, isCapable)
185 Display *dpy;
186 int screen;
187 Bool *isCapable;
188 {
189 XExtDisplayInfo *info = find_display(dpy);
190 xAppleDRIQueryDirectRenderingCapableReply rep;
191 xAppleDRIQueryDirectRenderingCapableReq *req;
192
193 TRACE("QueryDirectRenderingCapable...");
194 AppleDRICheckExtension(dpy, info, False);
195
196 LockDisplay(dpy);
197 GetReq(AppleDRIQueryDirectRenderingCapable, req);
198 req->reqType = info->codes->major_opcode;
199 req->driReqType = X_AppleDRIQueryDirectRenderingCapable;
200 req->screen = screen;
201 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
202 UnlockDisplay(dpy);
203 SyncHandle();
204 TRACE("QueryDirectRenderingCapable... return False");
205 return False;
206 }
207 *isCapable = rep.isCapable;
208 UnlockDisplay(dpy);
209 SyncHandle();
210 TRACE("QueryDirectRenderingCapable... return True");
211 return True;
212 }
213
214 Bool
215 XAppleDRIAuthConnection(dpy, screen, magic)
216 Display *dpy;
217 int screen;
218 unsigned int magic;
219 {
220 XExtDisplayInfo *info = find_display(dpy);
221 xAppleDRIAuthConnectionReq *req;
222 xAppleDRIAuthConnectionReply rep;
223
224 TRACE("AuthConnection...");
225 AppleDRICheckExtension(dpy, info, False);
226
227 LockDisplay(dpy);
228 GetReq(AppleDRIAuthConnection, req);
229 req->reqType = info->codes->major_opcode;
230 req->driReqType = X_AppleDRIAuthConnection;
231 req->screen = screen;
232 req->magic = magic;
233 rep.authenticated = 0;
234 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse) || !rep.authenticated) {
235 UnlockDisplay(dpy);
236 SyncHandle();
237 TRACE("AuthConnection... return False");
238 return False;
239 }
240 UnlockDisplay(dpy);
241 SyncHandle();
242 TRACE("AuthConnection... return True");
243 return True;
244 }
245
246 Bool
247 XAppleDRICreateSurface(dpy, screen, drawable, client_id, key, uid)
248 Display *dpy;
249 int screen;
250 Drawable drawable;
251 unsigned int client_id;
252 unsigned int *key;
253 unsigned int *uid;
254 {
255 XExtDisplayInfo *info = find_display(dpy);
256 xAppleDRICreateSurfaceReply rep;
257 xAppleDRICreateSurfaceReq *req;
258
259 TRACE("CreateSurface...");
260 AppleDRICheckExtension(dpy, info, False);
261
262 LockDisplay(dpy);
263 GetReq(AppleDRICreateSurface, req);
264 req->reqType = info->codes->major_opcode;
265 req->driReqType = X_AppleDRICreateSurface;
266 req->screen = screen;
267 req->drawable = drawable;
268 req->client_id = client_id;
269 rep.key_0 = rep.key_1 = rep.uid = 0;
270 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse) || !rep.key_0) {
271 UnlockDisplay(dpy);
272 SyncHandle();
273 TRACE("CreateSurface... return False");
274 return False;
275 }
276 key[0] = rep.key_0;
277 key[1] = rep.key_1;
278 *uid = rep.uid;
279 UnlockDisplay(dpy);
280 SyncHandle();
281 TRACE("CreateSurface... return True");
282 return True;
283 }
284
285 Bool
286 XAppleDRIDestroySurface(dpy, screen, drawable)
287 Display *dpy;
288 int screen;
289 Drawable drawable;
290 {
291 XExtDisplayInfo *info = find_display(dpy);
292 xAppleDRIDestroySurfaceReq *req;
293
294 TRACE("DestroySurface...");
295 AppleDRICheckExtension(dpy, info, False);
296
297 LockDisplay(dpy);
298 GetReq(AppleDRIDestroySurface, req);
299 req->reqType = info->codes->major_opcode;
300 req->driReqType = X_AppleDRIDestroySurface;
301 req->screen = screen;
302 req->drawable = drawable;
303 UnlockDisplay(dpy);
304 SyncHandle();
305 TRACE("DestroySurface... return True");
306 return True;
307 }
308
309 Bool
310 XAppleDRICreateSharedBuffer(Display * dpy, int screen, Drawable drawable,
311 Bool doubleSwap, char *path, size_t pathlen,
312 int *width, int *height)
313 {
314 XExtDisplayInfo *info = find_display(dpy);
315 xAppleDRICreateSharedBufferReq *req;
316 xAppleDRICreateSharedBufferReply rep;
317
318 AppleDRICheckExtension(dpy, info, False);
319
320 LockDisplay(dpy);
321 GetReq(AppleDRICreateSharedBuffer, req);
322 req->reqType = info->codes->major_opcode;
323 req->driReqType = X_AppleDRICreateSharedBuffer;
324 req->screen = screen;
325 req->drawable = drawable;
326 req->doubleSwap = doubleSwap;
327
328
329 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
330 puts("REPLY ERROR");
331
332 UnlockDisplay(dpy);
333 SyncHandle();
334 return False;
335 }
336
337 printf("rep.stringLength %d\n", (int) rep.stringLength);
338
339 if (rep.stringLength > 0 && rep.stringLength <= pathlen) {
340 _XReadPad(dpy, path, rep.stringLength);
341
342 printf("path: %s\n", path);
343
344 *width = rep.width;
345 *height = rep.height;
346
347 UnlockDisplay(dpy);
348 SyncHandle();
349 return True;
350 }
351
352 UnlockDisplay(dpy);
353 SyncHandle();
354
355 return False;
356 }
357
358 Bool
359 XAppleDRISwapBuffers(Display * dpy, int screen, Drawable drawable)
360 {
361 XExtDisplayInfo *info = find_display(dpy);
362 xAppleDRISwapBuffersReq *req;
363
364 AppleDRICheckExtension(dpy, info, False);
365
366 LockDisplay(dpy);
367 GetReq(AppleDRISwapBuffers, req);
368 req->reqType = info->codes->major_opcode;
369 req->driReqType = X_AppleDRISwapBuffers;
370 req->screen = screen;
371 req->drawable = drawable;
372 UnlockDisplay(dpy);
373 SyncHandle();
374
375 return True;
376 }
377
378 Bool
379 XAppleDRICreatePixmap(Display * dpy, int screen, Drawable drawable,
380 int *width, int *height, int *pitch, int *bpp,
381 size_t * size, char *bufname, size_t bufnamesize)
382 {
383 XExtDisplayInfo *info = find_display(dpy);
384 xAppleDRICreatePixmapReq *req;
385 xAppleDRICreatePixmapReply rep;
386
387 AppleDRICheckExtension(dpy, info, False);
388
389 LockDisplay(dpy);
390 GetReq(AppleDRICreatePixmap, req);
391 req->reqType = info->codes->major_opcode;
392 req->driReqType = X_AppleDRICreatePixmap;
393 req->screen = screen;
394 req->drawable = drawable;
395
396 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
397 UnlockDisplay(dpy);
398 SyncHandle();
399 return False;
400 }
401
402 /*
403 printf("rep.stringLength %d\n", (int) rep.stringLength);
404 */
405
406 if (rep.stringLength > 0 && rep.stringLength <= bufnamesize) {
407 _XReadPad(dpy, bufname, rep.stringLength);
408
409 printf("path: %s\n", bufname);
410
411 *width = rep.width;
412 *height = rep.height;
413 *pitch = rep.pitch;
414 *bpp = rep.bpp;
415 *size = rep.size;
416
417 UnlockDisplay(dpy);
418 SyncHandle();
419 return True;
420 }
421 else if (rep.stringLength > 0) {
422 _XEatData(dpy, rep.stringLength);
423 }
424
425 UnlockDisplay(dpy);
426 SyncHandle();
427
428 return True;
429 }
430
431 /*
432 * Call it a drawable, because we really don't know what it is
433 * until it reaches the server, and we should keep that in mind.
434 */
435 Bool
436 XAppleDRIDestroyPixmap(Display * dpy, Pixmap drawable)
437 {
438 XExtDisplayInfo *info = find_display(dpy);
439 xAppleDRIDestroyPixmapReq *req;
440
441 AppleDRICheckExtension(dpy, info, False);
442
443 LockDisplay(dpy);
444 GetReq(AppleDRIDestroyPixmap, req);
445 req->reqType = info->codes->major_opcode;
446 req->driReqType = X_AppleDRIDestroyPixmap;
447 req->drawable = drawable;
448 UnlockDisplay(dpy);
449 SyncHandle();
450
451 return True;
452 }