glx: kill old K&R syntax in XF86dri.c
[mesa.git] / src / glx / x11 / XF86dri.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
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 #ifdef GLX_DIRECT_RENDERING
41
42 #define NEED_REPLIES
43 #include <X11/Xlibint.h>
44 #include <X11/extensions/Xext.h>
45 #include <X11/extensions/extutil.h>
46 #include "xf86dristr.h"
47
48
49 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303
50 # define PUBLIC __attribute__((visibility("default")))
51 # define USED __attribute__((used))
52 #else
53 # define PUBLIC
54 # define USED
55 #endif
56
57
58
59 static XExtensionInfo _xf86dri_info_data;
60 static XExtensionInfo *xf86dri_info = &_xf86dri_info_data;
61 static char xf86dri_extension_name[] = XF86DRINAME;
62
63 #define XF86DRICheckExtension(dpy,i,val) \
64 XextCheckExtension (dpy, i, xf86dri_extension_name, val)
65
66 /*****************************************************************************
67 * *
68 * private utility routines *
69 * *
70 *****************************************************************************/
71
72 static int close_display(Display *dpy, XExtCodes *extCodes);
73 static /* const */ XExtensionHooks xf86dri_extension_hooks = {
74 NULL, /* create_gc */
75 NULL, /* copy_gc */
76 NULL, /* flush_gc */
77 NULL, /* free_gc */
78 NULL, /* create_font */
79 NULL, /* free_font */
80 close_display, /* close_display */
81 NULL, /* wire_to_event */
82 NULL, /* event_to_wire */
83 NULL, /* error */
84 NULL, /* error_string */
85 };
86
87 static XEXT_GENERATE_FIND_DISPLAY (find_display, xf86dri_info,
88 xf86dri_extension_name,
89 &xf86dri_extension_hooks,
90 0, NULL)
91
92 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xf86dri_info)
93
94
95 /*****************************************************************************
96 * *
97 * public XFree86-DRI Extension routines *
98 * *
99 *****************************************************************************/
100
101 #if 0
102 #include <stdio.h>
103 #define TRACE(msg) fprintf(stderr,"XF86DRI%s\n", msg);
104 #else
105 #define TRACE(msg)
106 #endif
107
108
109 PUBLIC Bool XF86DRIQueryExtension (Display* dpy, int* event_basep, int* error_basep)
110 {
111 XExtDisplayInfo *info = find_display (dpy);
112
113 TRACE("QueryExtension...");
114 if (XextHasExtension(info)) {
115 *event_basep = info->codes->first_event;
116 *error_basep = info->codes->first_error;
117 TRACE("QueryExtension... return True");
118 return True;
119 } else {
120 TRACE("QueryExtension... return False");
121 return False;
122 }
123 }
124
125 PUBLIC Bool XF86DRIQueryVersion(Display* dpy, int* majorVersion, int* minorVersion, int* patchVersion)
126 {
127 XExtDisplayInfo *info = find_display (dpy);
128 xXF86DRIQueryVersionReply rep;
129 xXF86DRIQueryVersionReq *req;
130
131 TRACE("QueryVersion...");
132 XF86DRICheckExtension (dpy, info, False);
133
134 LockDisplay(dpy);
135 GetReq(XF86DRIQueryVersion, req);
136 req->reqType = info->codes->major_opcode;
137 req->driReqType = X_XF86DRIQueryVersion;
138 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
139 UnlockDisplay(dpy);
140 SyncHandle();
141 TRACE("QueryVersion... return False");
142 return False;
143 }
144 *majorVersion = rep.majorVersion;
145 *minorVersion = rep.minorVersion;
146 *patchVersion = rep.patchVersion;
147 UnlockDisplay(dpy);
148 SyncHandle();
149 TRACE("QueryVersion... return True");
150 return True;
151 }
152
153 PUBLIC Bool XF86DRIQueryDirectRenderingCapable(Display* dpy, int screen, Bool* isCapable)
154 {
155 XExtDisplayInfo *info = find_display (dpy);
156 xXF86DRIQueryDirectRenderingCapableReply rep;
157 xXF86DRIQueryDirectRenderingCapableReq *req;
158
159 TRACE("QueryDirectRenderingCapable...");
160 XF86DRICheckExtension (dpy, info, False);
161
162 LockDisplay(dpy);
163 GetReq(XF86DRIQueryDirectRenderingCapable, req);
164 req->reqType = info->codes->major_opcode;
165 req->driReqType = X_XF86DRIQueryDirectRenderingCapable;
166 req->screen = screen;
167 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
168 UnlockDisplay(dpy);
169 SyncHandle();
170 TRACE("QueryDirectRenderingCapable... return False");
171 return False;
172 }
173 *isCapable = rep.isCapable;
174 UnlockDisplay(dpy);
175 SyncHandle();
176 TRACE("QueryDirectRenderingCapable... return True");
177 return True;
178 }
179
180 PUBLIC Bool XF86DRIOpenConnection(Display* dpy, int screen, drm_handle_t* hSAREA, char** busIdString)
181 {
182 XExtDisplayInfo *info = find_display (dpy);
183 xXF86DRIOpenConnectionReply rep;
184 xXF86DRIOpenConnectionReq *req;
185
186 TRACE("OpenConnection...");
187 XF86DRICheckExtension (dpy, info, False);
188
189 LockDisplay(dpy);
190 GetReq(XF86DRIOpenConnection, req);
191 req->reqType = info->codes->major_opcode;
192 req->driReqType = X_XF86DRIOpenConnection;
193 req->screen = screen;
194 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
195 UnlockDisplay(dpy);
196 SyncHandle();
197 TRACE("OpenConnection... return False");
198 return False;
199 }
200
201 *hSAREA = rep.hSAREALow;
202 if (sizeof(drm_handle_t) == 8) {
203 int shift = 32; /* var to prevent warning on next line */
204 *hSAREA |= ((drm_handle_t) rep.hSAREAHigh) << shift;
205 }
206
207 if (rep.length) {
208 if (!(*busIdString = (char *)Xcalloc(rep.busIdStringLength + 1, 1))) {
209 _XEatData(dpy, ((rep.busIdStringLength+3) & ~3));
210 UnlockDisplay(dpy);
211 SyncHandle();
212 TRACE("OpenConnection... return False");
213 return False;
214 }
215 _XReadPad(dpy, *busIdString, rep.busIdStringLength);
216 } else {
217 *busIdString = NULL;
218 }
219 UnlockDisplay(dpy);
220 SyncHandle();
221 TRACE("OpenConnection... return True");
222 return True;
223 }
224
225 PUBLIC Bool XF86DRIAuthConnection(Display* dpy, int screen, drm_magic_t magic)
226 {
227 XExtDisplayInfo *info = find_display (dpy);
228 xXF86DRIAuthConnectionReq *req;
229 xXF86DRIAuthConnectionReply rep;
230
231 TRACE("AuthConnection...");
232 XF86DRICheckExtension (dpy, info, False);
233
234 LockDisplay(dpy);
235 GetReq(XF86DRIAuthConnection, req);
236 req->reqType = info->codes->major_opcode;
237 req->driReqType = X_XF86DRIAuthConnection;
238 req->screen = screen;
239 req->magic = magic;
240 rep.authenticated = 0;
241 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.authenticated) {
242 UnlockDisplay(dpy);
243 SyncHandle();
244 TRACE("AuthConnection... return False");
245 return False;
246 }
247 UnlockDisplay(dpy);
248 SyncHandle();
249 TRACE("AuthConnection... return True");
250 return True;
251 }
252
253 PUBLIC Bool XF86DRICloseConnection(Display* dpy, int screen)
254 {
255 XExtDisplayInfo *info = find_display (dpy);
256 xXF86DRICloseConnectionReq *req;
257
258 TRACE("CloseConnection...");
259
260 XF86DRICheckExtension (dpy, info, False);
261
262 LockDisplay(dpy);
263 GetReq(XF86DRICloseConnection, req);
264 req->reqType = info->codes->major_opcode;
265 req->driReqType = X_XF86DRICloseConnection;
266 req->screen = screen;
267 UnlockDisplay(dpy);
268 SyncHandle();
269 TRACE("CloseConnection... return True");
270 return True;
271 }
272
273 PUBLIC Bool XF86DRIGetClientDriverName(Display* dpy, int screen, int* ddxDriverMajorVersion,
274 int* ddxDriverMinorVersion, int* ddxDriverPatchVersion, char** clientDriverName)
275 {
276 XExtDisplayInfo *info = find_display (dpy);
277 xXF86DRIGetClientDriverNameReply rep;
278 xXF86DRIGetClientDriverNameReq *req;
279
280 TRACE("GetClientDriverName...");
281 XF86DRICheckExtension (dpy, info, False);
282
283 LockDisplay(dpy);
284 GetReq(XF86DRIGetClientDriverName, req);
285 req->reqType = info->codes->major_opcode;
286 req->driReqType = X_XF86DRIGetClientDriverName;
287 req->screen = screen;
288 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
289 UnlockDisplay(dpy);
290 SyncHandle();
291 TRACE("GetClientDriverName... return False");
292 return False;
293 }
294
295 *ddxDriverMajorVersion = rep.ddxDriverMajorVersion;
296 *ddxDriverMinorVersion = rep.ddxDriverMinorVersion;
297 *ddxDriverPatchVersion = rep.ddxDriverPatchVersion;
298
299 if (rep.length) {
300 if (!(*clientDriverName = (char *)Xcalloc(rep.clientDriverNameLength + 1, 1))) {
301 _XEatData(dpy, ((rep.clientDriverNameLength+3) & ~3));
302 UnlockDisplay(dpy);
303 SyncHandle();
304 TRACE("GetClientDriverName... return False");
305 return False;
306 }
307 _XReadPad(dpy, *clientDriverName, rep.clientDriverNameLength);
308 } else {
309 *clientDriverName = NULL;
310 }
311 UnlockDisplay(dpy);
312 SyncHandle();
313 TRACE("GetClientDriverName... return True");
314 return True;
315 }
316
317 PUBLIC Bool XF86DRICreateContextWithConfig(Display* dpy, int screen, int configID, XID* context,
318 drm_context_t* hHWContext)
319 {
320 XExtDisplayInfo *info = find_display (dpy);
321 xXF86DRICreateContextReply rep;
322 xXF86DRICreateContextReq *req;
323
324 TRACE("CreateContext...");
325 XF86DRICheckExtension (dpy, info, False);
326
327 LockDisplay(dpy);
328 GetReq(XF86DRICreateContext, req);
329 req->reqType = info->codes->major_opcode;
330 req->driReqType = X_XF86DRICreateContext;
331 req->visual = configID;
332 req->screen = screen;
333 *context = XAllocID(dpy);
334 req->context = *context;
335 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
336 UnlockDisplay(dpy);
337 SyncHandle();
338 TRACE("CreateContext... return False");
339 return False;
340 }
341 *hHWContext = rep.hHWContext;
342 UnlockDisplay(dpy);
343 SyncHandle();
344 TRACE("CreateContext... return True");
345 return True;
346 }
347
348 PUBLIC Bool XF86DRICreateContext(Display* dpy, int screen, Visual* visual, XID* context, drm_context_t* hHWContext)
349 {
350 return XF86DRICreateContextWithConfig( dpy, screen, visual->visualid,
351 context, hHWContext );
352 }
353
354 PUBLIC Bool XF86DRIDestroyContext(Display *dpy, int screen,
355 XID context )
356 {
357 XExtDisplayInfo *info = find_display (dpy);
358 xXF86DRIDestroyContextReq *req;
359
360 TRACE("DestroyContext...");
361 XF86DRICheckExtension (dpy, info, False);
362
363 LockDisplay(dpy);
364 GetReq(XF86DRIDestroyContext, req);
365 req->reqType = info->codes->major_opcode;
366 req->driReqType = X_XF86DRIDestroyContext;
367 req->screen = screen;
368 req->context = context;
369 UnlockDisplay(dpy);
370 SyncHandle();
371 TRACE("DestroyContext... return True");
372 return True;
373 }
374
375 PUBLIC Bool XF86DRICreateDrawable(Display *dpy, int screen,
376 XID drawable, drm_drawable_t * hHWDrawable )
377 {
378 XExtDisplayInfo *info = find_display (dpy);
379 xXF86DRICreateDrawableReply rep;
380 xXF86DRICreateDrawableReq *req;
381
382 TRACE("CreateDrawable...");
383 XF86DRICheckExtension (dpy, info, False);
384
385 LockDisplay(dpy);
386 GetReq(XF86DRICreateDrawable, req);
387 req->reqType = info->codes->major_opcode;
388 req->driReqType = X_XF86DRICreateDrawable;
389 req->screen = screen;
390 req->drawable = drawable;
391 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
392 UnlockDisplay(dpy);
393 SyncHandle();
394 TRACE("CreateDrawable... return False");
395 return False;
396 }
397 *hHWDrawable = rep.hHWDrawable;
398 UnlockDisplay(dpy);
399 SyncHandle();
400 TRACE("CreateDrawable... return True");
401 return True;
402 }
403
404 static int noopErrorHandler(Display *dpy, XErrorEvent *xerr)
405 {
406 return 0;
407 }
408
409 PUBLIC Bool XF86DRIDestroyDrawable(Display *dpy, int screen,
410 XID drawable )
411 {
412 XExtDisplayInfo *info = find_display (dpy);
413 xXF86DRIDestroyDrawableReq *req;
414 int (*oldXErrorHandler)(Display *, XErrorEvent *);
415
416 TRACE("DestroyDrawable...");
417 XF86DRICheckExtension (dpy, info, False);
418
419 /* This is called from the DRI driver, which used call it like this
420 *
421 * if (windowExists(drawable))
422 * destroyDrawable(drawable);
423 *
424 * which is a textbook race condition - the window may disappear
425 * from the server between checking for its existance and
426 * destroying it. Instead we change the semantics of
427 * __DRIinterfaceMethodsRec::destroyDrawable() to succeed even if
428 * the windows is gone, by wrapping the destroy call in an error
429 * handler. */
430
431 XSync(dpy, False);
432 oldXErrorHandler = XSetErrorHandler(noopErrorHandler);
433
434 LockDisplay(dpy);
435 GetReq(XF86DRIDestroyDrawable, req);
436 req->reqType = info->codes->major_opcode;
437 req->driReqType = X_XF86DRIDestroyDrawable;
438 req->screen = screen;
439 req->drawable = drawable;
440 UnlockDisplay(dpy);
441 SyncHandle();
442
443 XSetErrorHandler(oldXErrorHandler);
444
445 TRACE("DestroyDrawable... return True");
446 return True;
447 }
448
449 PUBLIC Bool XF86DRIGetDrawableInfo(Display* dpy, int screen, Drawable drawable,
450 unsigned int* index, unsigned int* stamp,
451 int* X, int* Y, int* W, int* H,
452 int* numClipRects, drm_clip_rect_t ** pClipRects,
453 int* backX, int* backY,
454 int* numBackClipRects, drm_clip_rect_t ** pBackClipRects )
455 {
456 XExtDisplayInfo *info = find_display (dpy);
457 xXF86DRIGetDrawableInfoReply rep;
458 xXF86DRIGetDrawableInfoReq *req;
459 int total_rects;
460
461 TRACE("GetDrawableInfo...");
462 XF86DRICheckExtension (dpy, info, False);
463
464 LockDisplay(dpy);
465 GetReq(XF86DRIGetDrawableInfo, req);
466 req->reqType = info->codes->major_opcode;
467 req->driReqType = X_XF86DRIGetDrawableInfo;
468 req->screen = screen;
469 req->drawable = drawable;
470
471 if (!_XReply(dpy, (xReply *)&rep, 1, xFalse))
472 {
473 UnlockDisplay(dpy);
474 SyncHandle();
475 TRACE("GetDrawableInfo... return False");
476 return False;
477 }
478 *index = rep.drawableTableIndex;
479 *stamp = rep.drawableTableStamp;
480 *X = (int)rep.drawableX;
481 *Y = (int)rep.drawableY;
482 *W = (int)rep.drawableWidth;
483 *H = (int)rep.drawableHeight;
484 *numClipRects = rep.numClipRects;
485 total_rects = *numClipRects;
486
487 *backX = rep.backX;
488 *backY = rep.backY;
489 *numBackClipRects = rep.numBackClipRects;
490 total_rects += *numBackClipRects;
491
492 #if 0
493 /* Because of the fix in Xserver/GL/dri/xf86dri.c, this check breaks
494 * backwards compatibility (Because of the >> 2 shift) but the fix
495 * enables multi-threaded apps to work.
496 */
497 if (rep.length != ((((SIZEOF(xXF86DRIGetDrawableInfoReply) -
498 SIZEOF(xGenericReply) +
499 total_rects * sizeof(drm_clip_rect_t)) + 3) & ~3) >> 2)) {
500 _XEatData(dpy, rep.length);
501 UnlockDisplay(dpy);
502 SyncHandle();
503 TRACE("GetDrawableInfo... return False");
504 return False;
505 }
506 #endif
507
508 if (*numClipRects) {
509 int len = sizeof(drm_clip_rect_t) * (*numClipRects);
510
511 *pClipRects = (drm_clip_rect_t *)Xcalloc(len, 1);
512 if (*pClipRects)
513 _XRead(dpy, (char*)*pClipRects, len);
514 } else {
515 *pClipRects = NULL;
516 }
517
518 if (*numBackClipRects) {
519 int len = sizeof(drm_clip_rect_t) * (*numBackClipRects);
520
521 *pBackClipRects = (drm_clip_rect_t *)Xcalloc(len, 1);
522 if (*pBackClipRects)
523 _XRead(dpy, (char*)*pBackClipRects, len);
524 } else {
525 *pBackClipRects = NULL;
526 }
527
528 UnlockDisplay(dpy);
529 SyncHandle();
530 TRACE("GetDrawableInfo... return True");
531 return True;
532 }
533
534 PUBLIC Bool XF86DRIGetDeviceInfo(Display* dpy, int screen, drm_handle_t* hFrameBuffer,
535 int* fbOrigin, int* fbSize, int* fbStride, int* devPrivateSize, void** pDevPrivate)
536 {
537 XExtDisplayInfo *info = find_display (dpy);
538 xXF86DRIGetDeviceInfoReply rep;
539 xXF86DRIGetDeviceInfoReq *req;
540
541 TRACE("GetDeviceInfo...");
542 XF86DRICheckExtension (dpy, info, False);
543
544 LockDisplay(dpy);
545 GetReq(XF86DRIGetDeviceInfo, req);
546 req->reqType = info->codes->major_opcode;
547 req->driReqType = X_XF86DRIGetDeviceInfo;
548 req->screen = screen;
549 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
550 UnlockDisplay(dpy);
551 SyncHandle();
552 TRACE("GetDeviceInfo... return False");
553 return False;
554 }
555
556 *hFrameBuffer = rep.hFrameBufferLow;
557 if (sizeof(drm_handle_t) == 8) {
558 int shift = 32; /* var to prevent warning on next line */
559 *hFrameBuffer |= ((drm_handle_t) rep.hFrameBufferHigh) << shift;
560 }
561
562 *fbOrigin = rep.framebufferOrigin;
563 *fbSize = rep.framebufferSize;
564 *fbStride = rep.framebufferStride;
565 *devPrivateSize = rep.devPrivateSize;
566
567 if (rep.length) {
568 if (!(*pDevPrivate = (void *)Xcalloc(rep.devPrivateSize, 1))) {
569 _XEatData(dpy, ((rep.devPrivateSize+3) & ~3));
570 UnlockDisplay(dpy);
571 SyncHandle();
572 TRACE("GetDeviceInfo... return False");
573 return False;
574 }
575 _XRead(dpy, (char*)*pDevPrivate, rep.devPrivateSize);
576 } else {
577 *pDevPrivate = NULL;
578 }
579
580 UnlockDisplay(dpy);
581 SyncHandle();
582 TRACE("GetDeviceInfo... return True");
583 return True;
584 }
585
586 PUBLIC Bool XF86DRIOpenFullScreen(Display* dpy, int screen, Drawable drawable)
587 {
588 /* This function and the underlying X protocol are deprecated.
589 */
590 (void) dpy;
591 (void) screen;
592 (void) drawable;
593 return False;
594 }
595
596 PUBLIC Bool XF86DRICloseFullScreen(Display* dpy, int screen, Drawable drawable)
597 {
598 /* This function and the underlying X protocol are deprecated.
599 */
600 (void) dpy;
601 (void) screen;
602 (void) drawable;
603 return True;
604 }
605
606 #endif /* GLX_DIRECT_RENDERING */