Xdbe.c revision b9b4fd27
1/* $Xorg: Xdbe.c,v 1.4 2000/08/17 19:45:53 cpqbld Exp $ */ 2/****************************************************************************** 3 * 4 * Copyright (c) 1994, 1995 Hewlett-Packard Company 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL HEWLETT-PACKARD COMPANY BE LIABLE FOR ANY CLAIM, 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Except as contained in this notice, the name of the Hewlett-Packard 26 * Company shall not be used in advertising or otherwise to promote the 27 * sale, use or other dealings in this Software without prior written 28 * authorization from the Hewlett-Packard Company. 29 * 30 * Xlib DBE code 31 * 32 *****************************************************************************/ 33/* $XFree86: xc/lib/Xext/Xdbe.c,v 3.7 2002/10/16 02:19:22 dawes Exp $ */ 34 35#define NEED_EVENTS 36#define NEED_REPLIES 37#ifdef HAVE_CONFIG_H 38#include <config.h> 39#endif 40#include <stdio.h> 41#include <X11/Xlibint.h> 42#include <X11/extensions/Xext.h> 43#include <X11/extensions/extutil.h> 44#include <X11/extensions/Xdbe.h> 45#include <X11/extensions/dbeproto.h> 46 47static XExtensionInfo _dbe_info_data; 48static XExtensionInfo *dbe_info = &_dbe_info_data; 49static char *dbe_extension_name = DBE_PROTOCOL_NAME; 50 51#define DbeCheckExtension(dpy,i,val) \ 52 XextCheckExtension (dpy, i, dbe_extension_name, val) 53#define DbeSimpleCheckExtension(dpy,i) \ 54 XextSimpleCheckExtension (dpy, i, dbe_extension_name) 55 56#if !defined(UNIXCPP) 57#define DbeGetReq(name,req,info) GetReq (name, req); \ 58 req->reqType = info->codes->major_opcode; \ 59 req->dbeReqType = X_##name; 60#else 61#define DbeGetReq(name,req,info) GetReq (name, req); \ 62 req->reqType = info->codes->major_opcode; \ 63 req->dbeReqType = X_/**/name; 64#endif 65 66 67/***************************************************************************** 68 * * 69 * private utility routines * 70 * * 71 *****************************************************************************/ 72 73/* 74 * find_display - locate the display info block 75 */ 76static int close_display(Display *dpy, XExtCodes *codes); 77static char *error_string(Display *dpy, int code, XExtCodes *codes, 78 char *buf, int n); 79static XExtensionHooks dbe_extension_hooks = { 80 NULL, /* create_gc */ 81 NULL, /* copy_gc */ 82 NULL, /* flush_gc */ 83 NULL, /* free_gc */ 84 NULL, /* create_font */ 85 NULL, /* free_font */ 86 close_display, /* close_display */ 87 NULL, /* wire_to_event */ 88 NULL, /* event_to_wire */ 89 NULL, /* error */ 90 error_string, /* error_string */ 91}; 92 93static char *dbe_error_list[] = { 94 "BadBuffer", /* DbeBadBuffer */ 95}; 96 97static XEXT_GENERATE_FIND_DISPLAY (find_display, dbe_info, 98 dbe_extension_name, 99 &dbe_extension_hooks, 100 DbeNumberEvents, NULL) 101 102static XEXT_GENERATE_CLOSE_DISPLAY (close_display, dbe_info) 103 104static XEXT_GENERATE_ERROR_STRING (error_string, dbe_extension_name, 105 DbeNumberErrors, 106 dbe_error_list) 107 108 109/***************************************************************************** 110 * * 111 * Double-Buffering public interfaces * 112 * * 113 *****************************************************************************/ 114 115/* 116 * XdbeQueryExtension - 117 * Sets major_version_return and minor_verion_return to the major and 118 * minor DBE protocol version supported by the server. If the DBE 119 * library is compatible with the version returned by the server, this 120 * function returns non-zero. If dpy does not support the DBE 121 * extension, or if there was an error during communication with the 122 * server, or if the server and library protocol versions are 123 * incompatible, this functions returns zero. No other Xdbe functions 124 * may be called before this function. If a client violates this rule, 125 * the effects of all subsequent Xdbe calls are undefined. 126 */ 127Status XdbeQueryExtension ( 128 Display *dpy, 129 int *major_version_return, 130 int *minor_version_return) 131{ 132 XExtDisplayInfo *info = find_display (dpy); 133 xDbeGetVersionReply rep; 134 register xDbeGetVersionReq *req; 135 136 if (!XextHasExtension (info)) 137 return (Status)0; /* failure */ 138 139 LockDisplay (dpy); 140 DbeGetReq (DbeGetVersion, req, info); 141 req->majorVersion = DBE_MAJOR_VERSION; 142 req->minorVersion = DBE_MINOR_VERSION; 143 144 if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) { 145 UnlockDisplay (dpy); 146 SyncHandle (); 147 return (Status)0; /* failure */ 148 } 149 *major_version_return = rep.majorVersion; 150 *minor_version_return = rep.minorVersion; 151 UnlockDisplay (dpy); 152 153 SyncHandle (); 154 155 if (*major_version_return != DBE_MAJOR_VERSION) 156 return (Status)0; /* failure */ 157 else 158 return (Status)1; /* success */ 159} 160 161 162/* 163 * XdbeAllocateBackBuffer - 164 * This function returns a drawable ID used to refer to the back buffer 165 * of the specified window. The swap_action is a hint to indicate the 166 * swap action that will likely be used in subsequent calls to 167 * XdbeSwapBuffers. The actual swap action used in calls to 168 * XdbeSwapBuffers does not have to be the same as the swap_action 169 * passed to this function, though clients are encouraged to provide 170 * accurate information whenever possible. 171 */ 172 173XdbeBackBuffer XdbeAllocateBackBufferName( 174 Display *dpy, 175 Window window, 176 XdbeSwapAction swap_action) 177{ 178 XExtDisplayInfo *info = find_display (dpy); 179 register xDbeAllocateBackBufferNameReq *req; 180 XdbeBackBuffer buffer; 181 182 /* make sure extension is available; if not, return the 183 * third parameter (0). 184 */ 185 DbeCheckExtension (dpy, info, (XdbeBackBuffer)0); 186 187 LockDisplay(dpy); 188 DbeGetReq(DbeAllocateBackBufferName, req, info); 189 req->window = window; 190 req->swapAction = (unsigned char)swap_action; 191 req->buffer = buffer = XAllocID (dpy); 192 193 UnlockDisplay (dpy); 194 SyncHandle (); 195 return buffer; 196 197} /* XdbeAllocateBackBufferName() */ 198 199/* 200 * XdbeDeallocateBackBufferName - 201 * This function frees a drawable ID, buffer, that was obtained via 202 * XdbeAllocateBackBufferName. The buffer must refer to the back buffer 203 * of the specified window, or a protocol error results. 204 */ 205Status XdbeDeallocateBackBufferName ( 206 Display *dpy, 207 XdbeBackBuffer buffer) 208{ 209 XExtDisplayInfo *info = find_display (dpy); 210 register xDbeDeallocateBackBufferNameReq *req; 211 212 DbeCheckExtension (dpy, info, (Status)0 /* failure */); 213 214 LockDisplay (dpy); 215 DbeGetReq (DbeDeallocateBackBufferName, req, info); 216 req->buffer = buffer; 217 UnlockDisplay (dpy); 218 SyncHandle (); 219 220 return (Status)1; /* success */ 221} 222 223 224/* 225 * XdbeSwapBuffers - 226 * This function swaps the front and back buffers for a list of windows. 227 * The argument num_windows specifies how many windows are to have their 228 * buffers swapped; it is the number of elements in the swap_info array. 229 * The argument swap_info specifies the information needed per window 230 * to do the swap. 231 */ 232Status XdbeSwapBuffers ( 233 Display *dpy, 234 XdbeSwapInfo *swap_info, 235 int num_windows) 236{ 237 XExtDisplayInfo *info = find_display (dpy); 238 register xDbeSwapBuffersReq *req; 239 int i; 240 241 DbeCheckExtension (dpy, info, (Status)0 /* failure */); 242 243 LockDisplay (dpy); 244 DbeGetReq (DbeSwapBuffers, req, info); 245 req->length += 2*num_windows; 246 req->n = num_windows; 247 248 /* We need to handle 64-bit machines, where we can not use PackData32 249 * directly because info would be lost in translating from 32- to 64-bit. 250 * Instead we send data via a loop that accounts for the translation. 251 */ 252 for (i = 0; i < num_windows; i++) 253 { 254 char tmp[4]; 255 Data32 (dpy, (long *)&swap_info[i].swap_window, 4); 256 tmp[0] = swap_info[i].swap_action; 257 Data (dpy, (char *)tmp, 4); 258 } 259 260 UnlockDisplay (dpy); 261 SyncHandle (); 262 263 264 return (Status)1; /* success */ 265 266} /* XdbeSwapBuffers() */ 267 268 269/* 270 * XdbeBeginIdiom - 271 * This function marks the beginning of an idiom sequence. 272 */ 273Status XdbeBeginIdiom (Display *dpy) 274{ 275 XExtDisplayInfo *info = find_display(dpy); 276 register xDbeBeginIdiomReq *req; 277 278 DbeCheckExtension (dpy, info, (Status)0 /* failure */); 279 280 LockDisplay (dpy); 281 DbeGetReq (DbeBeginIdiom, req, info); 282 UnlockDisplay (dpy); 283 SyncHandle (); 284 285 return (Status)1; /* success */ 286} 287 288 289/* 290 * XdbeEndIdiom - 291 * This function marks the end of an idiom sequence. 292 */ 293Status XdbeEndIdiom (Display *dpy) 294{ 295 XExtDisplayInfo *info = find_display(dpy); 296 register xDbeEndIdiomReq *req; 297 298 DbeCheckExtension (dpy, info, (Status)0 /* failure */); 299 300 LockDisplay (dpy); 301 DbeGetReq (DbeEndIdiom, req, info); 302 UnlockDisplay (dpy); 303 SyncHandle (); 304 305 return (Status)1; /* success */ 306} 307 308 309/* 310 * XdbeGetVisualInfo - 311 * This function returns information about which visuals support 312 * double buffering. The argument num_screens specifies how many 313 * elements there are in the screen_specifiers list. Each drawable 314 * in screen_specifiers designates a screen for which the supported 315 * visuals are being requested. If num_screens is zero, information 316 * for all screens is requested. In this case, upon return from this 317 * function, num_screens will be set to the number of screens that were 318 * found. If an error occurs, this function returns NULL, else it returns 319 * a pointer to a list of XdbeScreenVisualInfo structures of length 320 * num_screens. The nth element in the returned list corresponds to the 321 * nth drawable in the screen_specifiers list, unless num_screens was 322 * passed in with the value zero, in which case the nth element in the 323 * returned list corresponds to the nth screen of the server, starting 324 * with screen zero. 325 */ 326XdbeScreenVisualInfo *XdbeGetVisualInfo ( 327 Display *dpy, 328 Drawable *screen_specifiers, 329 int *num_screens) /* SEND and RETURN */ 330{ 331 XExtDisplayInfo *info = find_display(dpy); 332 register xDbeGetVisualInfoReq *req; 333 xDbeGetVisualInfoReply rep; 334 XdbeScreenVisualInfo *scrVisInfo; 335 int i; 336 337 DbeCheckExtension (dpy, info, (XdbeScreenVisualInfo *)NULL); 338 339 LockDisplay (dpy); 340 341 DbeGetReq(DbeGetVisualInfo, req, info); 342 req->length = 2 + *num_screens; 343 req->n = *num_screens; 344 Data32 (dpy, screen_specifiers, (*num_screens * sizeof (CARD32))); 345 346 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) { 347 UnlockDisplay (dpy); 348 SyncHandle (); 349 return NULL; 350 } 351 352 /* return the number of screens actually found if we 353 * requested information about all screens (*num_screens == 0) 354 */ 355 if (*num_screens == 0) 356 *num_screens = rep.m; 357 358 /* allocate list of visual information to be returned */ 359 if (!(scrVisInfo = 360 (XdbeScreenVisualInfo *)Xmalloc( 361 (unsigned)(*num_screens * sizeof(XdbeScreenVisualInfo))))) { 362 UnlockDisplay (dpy); 363 SyncHandle (); 364 return NULL; 365 } 366 367 for (i = 0; i < *num_screens; i++) 368 { 369 int nbytes; 370 int j; 371 long c; 372 373 _XRead32 (dpy, &c, sizeof(CARD32)); 374 scrVisInfo[i].count = c; 375 376 nbytes = scrVisInfo[i].count * sizeof(XdbeVisualInfo); 377 378 /* if we can not allocate the list of visual/depth info 379 * then free the lists that we already allocate as well 380 * as the visual info list itself 381 */ 382 if (!(scrVisInfo[i].visinfo = (XdbeVisualInfo *)Xmalloc( 383 (unsigned)nbytes))) { 384 for (j = 0; j < i; j++) { 385 Xfree ((char *)scrVisInfo[j].visinfo); 386 } 387 Xfree ((char *)scrVisInfo); 388 UnlockDisplay (dpy); 389 SyncHandle (); 390 return NULL; 391 } 392 393 /* Read the visual info item into the wire structure. Then copy each 394 * element into the library structure. The element sizes and/or 395 * padding may be different in the two structures. 396 */ 397 for (j = 0; j < scrVisInfo[i].count; j++) { 398 xDbeVisInfo xvi; 399 400 _XRead (dpy, (char *)&xvi, sizeof(xDbeVisInfo)); 401 scrVisInfo[i].visinfo[j].visual = xvi.visualID; 402 scrVisInfo[i].visinfo[j].depth = xvi.depth; 403 scrVisInfo[i].visinfo[j].perflevel = xvi.perfLevel; 404 } 405 406 } 407 408 UnlockDisplay (dpy); 409 SyncHandle (); 410 return scrVisInfo; 411 412} /* XdbeGetVisualInfo() */ 413 414 415/* 416 * XdbeFreeVisualInfo - 417 * This function frees the list of XdbeScreenVisualInfo returned by the 418 * function XdbeGetVisualInfo. 419 */ 420void XdbeFreeVisualInfo(XdbeScreenVisualInfo *visual_info) 421{ 422 if (visual_info == NULL) { 423 return; 424 } 425 426 if (visual_info->visinfo) { 427 XFree(visual_info->visinfo); 428 } 429 430 XFree(visual_info); 431} 432 433 434/* 435 * XdbeGetBackBufferAttributes - 436 * This function returns the attributes associated with the specified 437 * buffer. 438 */ 439XdbeBackBufferAttributes *XdbeGetBackBufferAttributes( 440 Display *dpy, 441 XdbeBackBuffer buffer) 442{ 443 XExtDisplayInfo *info = find_display(dpy); 444 register xDbeGetBackBufferAttributesReq *req; 445 xDbeGetBackBufferAttributesReply rep; 446 XdbeBackBufferAttributes *attr; 447 448 DbeCheckExtension(dpy, info, (XdbeBackBufferAttributes *)NULL); 449 450 if (!(attr = 451 (XdbeBackBufferAttributes *)Xmalloc(sizeof(XdbeBackBufferAttributes)))) { 452 return NULL; 453 } 454 455 LockDisplay(dpy); 456 DbeGetReq(DbeGetBackBufferAttributes, req, info); 457 req->buffer = buffer; 458 459 if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) { 460 UnlockDisplay (dpy); 461 SyncHandle (); 462 Xfree(attr); 463 return NULL; 464 } 465 attr->window = rep.attributes; 466 467 UnlockDisplay (dpy); 468 SyncHandle (); 469 470 return attr; 471} 472 473