remove stray 'foo' line
[mesa.git] / src / glx / x11 / XF86dri.c
1 /* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.13 2002/10/30 12:51:25 alanh Exp $ */
2 /**************************************************************************
3
4 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
5 Copyright 2000 VA Linux Systems, Inc.
6 All Rights Reserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sub license, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial portions
18 of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /*
31 * Authors:
32 * Kevin E. Martin <martin@valinux.com>
33 * Jens Owen <jens@tungstengraphics.com>
34 * Rickard E. (Rik) Faith <faith@valinux.com>
35 *
36 */
37
38 /* THIS IS NOT AN X CONSORTIUM STANDARD */
39
40 #define NEED_REPLIES
41 #include <X11/Xlibint.h>
42 #include <X11/extensions/Xext.h>
43 #include <X11/extensions/extutil.h>
44 #include "glheader.h"
45 #include "xf86dristr.h"
46
47 static XExtensionInfo _xf86dri_info_data;
48 static XExtensionInfo *xf86dri_info = &_xf86dri_info_data;
49 static char xf86dri_extension_name[] = XF86DRINAME;
50
51 #define XF86DRICheckExtension(dpy,i,val) \
52 XextCheckExtension (dpy, i, xf86dri_extension_name, val)
53
54 /*****************************************************************************
55 * *
56 * private utility routines *
57 * *
58 *****************************************************************************/
59
60 static int close_display(Display *dpy, XExtCodes *extCodes);
61 static /* const */ XExtensionHooks xf86dri_extension_hooks = {
62 NULL, /* create_gc */
63 NULL, /* copy_gc */
64 NULL, /* flush_gc */
65 NULL, /* free_gc */
66 NULL, /* create_font */
67 NULL, /* free_font */
68 close_display, /* close_display */
69 NULL, /* wire_to_event */
70 NULL, /* event_to_wire */
71 NULL, /* error */
72 NULL, /* error_string */
73 };
74
75 static XEXT_GENERATE_FIND_DISPLAY (find_display, xf86dri_info,
76 xf86dri_extension_name,
77 &xf86dri_extension_hooks,
78 0, NULL)
79
80 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xf86dri_info)
81
82
83 /*****************************************************************************
84 * *
85 * public XFree86-DRI Extension routines *
86 * *
87 *****************************************************************************/
88
89 #if 0
90 #include <stdio.h>
91 #define TRACE(msg) fprintf(stderr,"XF86DRI%s\n", msg);
92 #else
93 #define TRACE(msg)
94 #endif
95
96
97 PUBLIC Bool XF86DRIQueryExtension (dpy, event_basep, error_basep)
98 Display *dpy;
99 int *event_basep, *error_basep;
100 {
101 XExtDisplayInfo *info = find_display (dpy);
102
103 TRACE("QueryExtension...");
104 if (XextHasExtension(info)) {
105 *event_basep = info->codes->first_event;
106 *error_basep = info->codes->first_error;
107 TRACE("QueryExtension... return True");
108 return True;
109 } else {
110 TRACE("QueryExtension... return False");
111 return False;
112 }
113 }
114
115 PUBLIC Bool XF86DRIQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
116 Display* dpy;
117 int* majorVersion;
118 int* minorVersion;
119 int* patchVersion;
120 {
121 XExtDisplayInfo *info = find_display (dpy);
122 xXF86DRIQueryVersionReply rep;
123 xXF86DRIQueryVersionReq *req;
124
125 TRACE("QueryVersion...");
126 XF86DRICheckExtension (dpy, info, False);
127
128 LockDisplay(dpy);
129 GetReq(XF86DRIQueryVersion, req);
130 req->reqType = info->codes->major_opcode;
131 req->driReqType = X_XF86DRIQueryVersion;
132 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
133 UnlockDisplay(dpy);
134 SyncHandle();
135 TRACE("QueryVersion... return False");
136 return False;
137 }
138 *majorVersion = rep.majorVersion;
139 *minorVersion = rep.minorVersion;
140 *patchVersion = rep.patchVersion;
141 UnlockDisplay(dpy);
142 SyncHandle();
143 TRACE("QueryVersion... return True");
144 return True;
145 }
146
147 PUBLIC Bool XF86DRIQueryDirectRenderingCapable(dpy, screen, isCapable)
148 Display* dpy;
149 int screen;
150 Bool* isCapable;
151 {
152 XExtDisplayInfo *info = find_display (dpy);
153 xXF86DRIQueryDirectRenderingCapableReply rep;
154 xXF86DRIQueryDirectRenderingCapableReq *req;
155
156 TRACE("QueryDirectRenderingCapable...");
157 XF86DRICheckExtension (dpy, info, False);
158
159 LockDisplay(dpy);
160 GetReq(XF86DRIQueryDirectRenderingCapable, req);
161 req->reqType = info->codes->major_opcode;
162 req->driReqType = X_XF86DRIQueryDirectRenderingCapable;
163 req->screen = screen;
164 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
165 UnlockDisplay(dpy);
166 SyncHandle();
167 TRACE("QueryDirectRenderingCapable... return False");
168 return False;
169 }
170 *isCapable = rep.isCapable;
171 UnlockDisplay(dpy);
172 SyncHandle();
173 TRACE("QueryDirectRenderingCapable... return True");
174 return True;
175 }
176
177 PUBLIC Bool XF86DRIOpenConnection(dpy, screen, hSAREA, busIdString)
178 Display* dpy;
179 int screen;
180 drm_handle_t * hSAREA;
181 char **busIdString;
182 {
183 XExtDisplayInfo *info = find_display (dpy);
184 xXF86DRIOpenConnectionReply rep;
185 xXF86DRIOpenConnectionReq *req;
186
187 TRACE("OpenConnection...");
188 XF86DRICheckExtension (dpy, info, False);
189
190 LockDisplay(dpy);
191 GetReq(XF86DRIOpenConnection, req);
192 req->reqType = info->codes->major_opcode;
193 req->driReqType = X_XF86DRIOpenConnection;
194 req->screen = screen;
195 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
196 UnlockDisplay(dpy);
197 SyncHandle();
198 TRACE("OpenConnection... return False");
199 return False;
200 }
201
202 *hSAREA = rep.hSAREALow;
203 if (sizeof(drm_handle_t) == 8) {
204 const int shift = 32; /* var to prevent warning on next line */
205 *hSAREA |= ((drm_handle_t) rep.hSAREAHigh) << shift;
206 }
207
208 if (rep.length) {
209 if (!(*busIdString = (char *)Xcalloc(rep.busIdStringLength + 1, 1))) {
210 _XEatData(dpy, ((rep.busIdStringLength+3) & ~3));
211 UnlockDisplay(dpy);
212 SyncHandle();
213 TRACE("OpenConnection... return False");
214 return False;
215 }
216 _XReadPad(dpy, *busIdString, rep.busIdStringLength);
217 } else {
218 *busIdString = NULL;
219 }
220 UnlockDisplay(dpy);
221 SyncHandle();
222 TRACE("OpenConnection... return True");
223 return True;
224 }
225
226 PUBLIC Bool XF86DRIAuthConnection(dpy, screen, magic)
227 Display* dpy;
228 int screen;
229 drm_magic_t magic;
230 {
231 XExtDisplayInfo *info = find_display (dpy);
232 xXF86DRIAuthConnectionReq *req;
233 xXF86DRIAuthConnectionReply rep;
234
235 TRACE("AuthConnection...");
236 XF86DRICheckExtension (dpy, info, False);
237
238 LockDisplay(dpy);
239 GetReq(XF86DRIAuthConnection, req);
240 req->reqType = info->codes->major_opcode;
241 req->driReqType = X_XF86DRIAuthConnection;
242 req->screen = screen;
243 req->magic = magic;
244 rep.authenticated = 0;
245 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.authenticated) {
246 UnlockDisplay(dpy);
247 SyncHandle();
248 TRACE("AuthConnection... return False");
249 return False;
250 }
251 UnlockDisplay(dpy);
252 SyncHandle();
253 TRACE("AuthConnection... return True");
254 return True;
255 }
256
257 PUBLIC Bool XF86DRICloseConnection(dpy, screen)
258 Display* dpy;
259 int screen;
260 {
261 XExtDisplayInfo *info = find_display (dpy);
262 xXF86DRICloseConnectionReq *req;
263
264 TRACE("CloseConnection...");
265
266 XF86DRICheckExtension (dpy, info, False);
267
268 LockDisplay(dpy);
269 GetReq(XF86DRICloseConnection, req);
270 req->reqType = info->codes->major_opcode;
271 req->driReqType = X_XF86DRICloseConnection;
272 req->screen = screen;
273 UnlockDisplay(dpy);
274 SyncHandle();
275 TRACE("CloseConnection... return True");
276 return True;
277 }
278
279 PUBLIC Bool XF86DRIGetClientDriverName(dpy, screen, ddxDriverMajorVersion,
280 ddxDriverMinorVersion, ddxDriverPatchVersion, clientDriverName)
281 Display* dpy;
282 int screen;
283 int* ddxDriverMajorVersion;
284 int* ddxDriverMinorVersion;
285 int* ddxDriverPatchVersion;
286 char** clientDriverName;
287 {
288 XExtDisplayInfo *info = find_display (dpy);
289 xXF86DRIGetClientDriverNameReply rep;
290 xXF86DRIGetClientDriverNameReq *req;
291
292 TRACE("GetClientDriverName...");
293 XF86DRICheckExtension (dpy, info, False);
294
295 LockDisplay(dpy);
296 GetReq(XF86DRIGetClientDriverName, req);
297 req->reqType = info->codes->major_opcode;
298 req->driReqType = X_XF86DRIGetClientDriverName;
299 req->screen = screen;
300 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
301 UnlockDisplay(dpy);
302 SyncHandle();
303 TRACE("GetClientDriverName... return False");
304 return False;
305 }
306
307 *ddxDriverMajorVersion = rep.ddxDriverMajorVersion;
308 *ddxDriverMinorVersion = rep.ddxDriverMinorVersion;
309 *ddxDriverPatchVersion = rep.ddxDriverPatchVersion;
310
311 if (rep.length) {
312 if (!(*clientDriverName = (char *)Xcalloc(rep.clientDriverNameLength + 1, 1))) {
313 _XEatData(dpy, ((rep.clientDriverNameLength+3) & ~3));
314 UnlockDisplay(dpy);
315 SyncHandle();
316 TRACE("GetClientDriverName... return False");
317 return False;
318 }
319 _XReadPad(dpy, *clientDriverName, rep.clientDriverNameLength);
320 } else {
321 *clientDriverName = NULL;
322 }
323 UnlockDisplay(dpy);
324 SyncHandle();
325 TRACE("GetClientDriverName... return True");
326 return True;
327 }
328
329 PUBLIC Bool XF86DRICreateContextWithConfig(dpy, screen, configID, context,
330 hHWContext)
331 Display* dpy;
332 int screen;
333 int configID;
334 XID* context;
335 drm_context_t * hHWContext;
336 {
337 XExtDisplayInfo *info = find_display (dpy);
338 xXF86DRICreateContextReply rep;
339 xXF86DRICreateContextReq *req;
340
341 TRACE("CreateContext...");
342 XF86DRICheckExtension (dpy, info, False);
343
344 LockDisplay(dpy);
345 GetReq(XF86DRICreateContext, req);
346 req->reqType = info->codes->major_opcode;
347 req->driReqType = X_XF86DRICreateContext;
348 req->visual = configID;
349 req->screen = screen;
350 *context = XAllocID(dpy);
351 req->context = *context;
352 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
353 UnlockDisplay(dpy);
354 SyncHandle();
355 TRACE("CreateContext... return False");
356 return False;
357 }
358 *hHWContext = rep.hHWContext;
359 UnlockDisplay(dpy);
360 SyncHandle();
361 TRACE("CreateContext... return True");
362 return True;
363 }
364
365 PUBLIC Bool XF86DRICreateContext(dpy, screen, visual, context, hHWContext)
366 Display* dpy;
367 int screen;
368 Visual* visual;
369 XID* context;
370 drm_context_t * hHWContext;
371 {
372 return XF86DRICreateContextWithConfig( dpy, screen, visual->visualid,
373 context, hHWContext );
374 }
375
376 PUBLIC GLboolean XF86DRIDestroyContext( __DRInativeDisplay * ndpy, int screen,
377 __DRIid context )
378 {
379 Display * const dpy = (Display *) ndpy;
380 XExtDisplayInfo *info = find_display (dpy);
381 xXF86DRIDestroyContextReq *req;
382
383 TRACE("DestroyContext...");
384 XF86DRICheckExtension (dpy, info, False);
385
386 LockDisplay(dpy);
387 GetReq(XF86DRIDestroyContext, req);
388 req->reqType = info->codes->major_opcode;
389 req->driReqType = X_XF86DRIDestroyContext;
390 req->screen = screen;
391 req->context = context;
392 UnlockDisplay(dpy);
393 SyncHandle();
394 TRACE("DestroyContext... return True");
395 return True;
396 }
397
398 PUBLIC GLboolean XF86DRICreateDrawable( __DRInativeDisplay * ndpy, int screen,
399 __DRIid drawable, drm_drawable_t * hHWDrawable )
400 {
401 Display * const dpy = (Display *) ndpy;
402 XExtDisplayInfo *info = find_display (dpy);
403 xXF86DRICreateDrawableReply rep;
404 xXF86DRICreateDrawableReq *req;
405
406 TRACE("CreateDrawable...");
407 XF86DRICheckExtension (dpy, info, False);
408
409 LockDisplay(dpy);
410 GetReq(XF86DRICreateDrawable, req);
411 req->reqType = info->codes->major_opcode;
412 req->driReqType = X_XF86DRICreateDrawable;
413 req->screen = screen;
414 req->drawable = drawable;
415 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
416 UnlockDisplay(dpy);
417 SyncHandle();
418 TRACE("CreateDrawable... return False");
419 return False;
420 }
421 *hHWDrawable = rep.hHWDrawable;
422 UnlockDisplay(dpy);
423 SyncHandle();
424 TRACE("CreateDrawable... return True");
425 return True;
426 }
427
428 PUBLIC GLboolean XF86DRIDestroyDrawable( __DRInativeDisplay * ndpy, int screen,
429 __DRIid drawable )
430 {
431 Display * const dpy = (Display *) ndpy;
432 XExtDisplayInfo *info = find_display (dpy);
433 xXF86DRIDestroyDrawableReq *req;
434
435 TRACE("DestroyDrawable...");
436 XF86DRICheckExtension (dpy, info, False);
437
438 LockDisplay(dpy);
439 GetReq(XF86DRIDestroyDrawable, req);
440 req->reqType = info->codes->major_opcode;
441 req->driReqType = X_XF86DRIDestroyDrawable;
442 req->screen = screen;
443 req->drawable = drawable;
444 UnlockDisplay(dpy);
445 SyncHandle();
446 TRACE("DestroyDrawable... return True");
447 return True;
448 }
449
450 PUBLIC Bool XF86DRIGetDrawableInfo(Display* dpy, int screen, Drawable drawable,
451 unsigned int* index, unsigned int* stamp,
452 int* X, int* Y, int* W, int* H,
453 int* numClipRects, drm_clip_rect_t ** pClipRects,
454 int* backX, int* backY,
455 int* numBackClipRects, drm_clip_rect_t ** pBackClipRects )
456 {
457 XExtDisplayInfo *info = find_display (dpy);
458 xXF86DRIGetDrawableInfoReply rep;
459 xXF86DRIGetDrawableInfoReq *req;
460 int total_rects;
461
462 TRACE("GetDrawableInfo...");
463 XF86DRICheckExtension (dpy, info, False);
464
465 LockDisplay(dpy);
466 GetReq(XF86DRIGetDrawableInfo, req);
467 req->reqType = info->codes->major_opcode;
468 req->driReqType = X_XF86DRIGetDrawableInfo;
469 req->screen = screen;
470 req->drawable = drawable;
471
472 if (!_XReply(dpy, (xReply *)&rep, 1, xFalse))
473 {
474 UnlockDisplay(dpy);
475 SyncHandle();
476 TRACE("GetDrawableInfo... return False");
477 return False;
478 }
479 *index = rep.drawableTableIndex;
480 *stamp = rep.drawableTableStamp;
481 *X = (int)rep.drawableX;
482 *Y = (int)rep.drawableY;
483 *W = (int)rep.drawableWidth;
484 *H = (int)rep.drawableHeight;
485 *numClipRects = rep.numClipRects;
486 total_rects = *numClipRects;
487
488 *backX = rep.backX;
489 *backY = rep.backY;
490 *numBackClipRects = rep.numBackClipRects;
491 total_rects += *numBackClipRects;
492
493 #if 0
494 /* Because of the fix in Xserver/GL/dri/xf86dri.c, this check breaks
495 * backwards compatibility (Because of the >> 2 shift) but the fix
496 * enables multi-threaded apps to work.
497 */
498 if (rep.length != ((((SIZEOF(xXF86DRIGetDrawableInfoReply) -
499 SIZEOF(xGenericReply) +
500 total_rects * sizeof(drm_clip_rect_t)) + 3) & ~3) >> 2)) {
501 _XEatData(dpy, rep.length);
502 UnlockDisplay(dpy);
503 SyncHandle();
504 TRACE("GetDrawableInfo... return False");
505 return False;
506 }
507 #endif
508
509 if (*numClipRects) {
510 int len = sizeof(drm_clip_rect_t) * (*numClipRects);
511
512 *pClipRects = (drm_clip_rect_t *)Xcalloc(len, 1);
513 if (*pClipRects)
514 _XRead(dpy, (char*)*pClipRects, len);
515 } else {
516 *pClipRects = NULL;
517 }
518
519 if (*numBackClipRects) {
520 int len = sizeof(drm_clip_rect_t) * (*numBackClipRects);
521
522 *pBackClipRects = (drm_clip_rect_t *)Xcalloc(len, 1);
523 if (*pBackClipRects)
524 _XRead(dpy, (char*)*pBackClipRects, len);
525 } else {
526 *pBackClipRects = NULL;
527 }
528
529 UnlockDisplay(dpy);
530 SyncHandle();
531 TRACE("GetDrawableInfo... return True");
532 return True;
533 }
534
535 PUBLIC Bool XF86DRIGetDeviceInfo(dpy, screen, hFrameBuffer,
536 fbOrigin, fbSize, fbStride, devPrivateSize, pDevPrivate)
537 Display* dpy;
538 int screen;
539 drm_handle_t * hFrameBuffer;
540 int* fbOrigin;
541 int* fbSize;
542 int* fbStride;
543 int* devPrivateSize;
544 void** pDevPrivate;
545 {
546 XExtDisplayInfo *info = find_display (dpy);
547 xXF86DRIGetDeviceInfoReply rep;
548 xXF86DRIGetDeviceInfoReq *req;
549
550 TRACE("GetDeviceInfo...");
551 XF86DRICheckExtension (dpy, info, False);
552
553 LockDisplay(dpy);
554 GetReq(XF86DRIGetDeviceInfo, req);
555 req->reqType = info->codes->major_opcode;
556 req->driReqType = X_XF86DRIGetDeviceInfo;
557 req->screen = screen;
558 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
559 UnlockDisplay(dpy);
560 SyncHandle();
561 TRACE("GetDeviceInfo... return False");
562 return False;
563 }
564
565 *hFrameBuffer = rep.hFrameBufferLow;
566 if (sizeof(drm_handle_t) == 8) {
567 const int shift = 32; /* var to prevent warning on next line */
568 *hFrameBuffer |= ((drm_handle_t) rep.hFrameBufferHigh) << shift;
569 }
570
571 *fbOrigin = rep.framebufferOrigin;
572 *fbSize = rep.framebufferSize;
573 *fbStride = rep.framebufferStride;
574 *devPrivateSize = rep.devPrivateSize;
575
576 if (rep.length) {
577 if (!(*pDevPrivate = (void *)Xcalloc(rep.devPrivateSize, 1))) {
578 _XEatData(dpy, ((rep.devPrivateSize+3) & ~3));
579 UnlockDisplay(dpy);
580 SyncHandle();
581 TRACE("GetDeviceInfo... return False");
582 return False;
583 }
584 _XRead(dpy, (char*)*pDevPrivate, rep.devPrivateSize);
585 } else {
586 *pDevPrivate = NULL;
587 }
588
589 UnlockDisplay(dpy);
590 SyncHandle();
591 TRACE("GetDeviceInfo... return True");
592 return True;
593 }
594
595 PUBLIC Bool XF86DRIOpenFullScreen(dpy, screen, drawable)
596 Display* dpy;
597 int screen;
598 Drawable drawable;
599 {
600 /* This function and the underlying X protocol are deprecated.
601 */
602 (void) dpy;
603 (void) screen;
604 (void) drawable;
605 return False;
606 }
607
608 PUBLIC Bool XF86DRICloseFullScreen(dpy, screen, drawable)
609 Display* dpy;
610 int screen;
611 Drawable drawable;
612 {
613 /* This function and the underlying X protocol are deprecated.
614 */
615 (void) dpy;
616 (void) screen;
617 (void) drawable;
618 return True;
619 }