XlibAsync.c revision 61b2299d
1/* $Xorg: XlibAsync.c,v 1.4 2001/02/09 02:03:38 xorgcvs Exp $ */ 2/* 3 4Copyright 1992, 1998 The Open Group 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation. 11 12The above copyright notice and this permission notice shall be included 13in all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21OTHER DEALINGS IN THE SOFTWARE. 22 23Except as contained in this notice, the name of The Open Group shall 24not be used in advertising or otherwise to promote the sale, use or 25other dealings in this Software without prior written authorization 26from The Open Group. 27 28*/ 29 30#define NEED_REPLIES 31#ifdef HAVE_CONFIG_H 32#include <config.h> 33#endif 34#include <X11/Xlibint.h> 35#include <X11/Xos.h> 36 37/*ARGSUSED*/ 38Bool 39_XAsyncErrorHandler( 40 register Display *dpy, 41 register xReply *rep, 42 char *buf, 43 int len, 44 XPointer data) 45{ 46 register _XAsyncErrorState *state; 47 48 state = (_XAsyncErrorState *)data; 49 if (rep->generic.type == X_Error && 50 (!state->error_code || 51 rep->error.errorCode == state->error_code) && 52 (!state->major_opcode || 53 rep->error.majorCode == state->major_opcode) && 54 (!state->minor_opcode || 55 rep->error.minorCode == state->minor_opcode) && 56 (!state->min_sequence_number || 57 (state->min_sequence_number <= dpy->last_request_read)) && 58 (!state->max_sequence_number || 59 (state->max_sequence_number >= dpy->last_request_read))) { 60 state->last_error_received = rep->error.errorCode; 61 state->error_count++; 62 return True; 63 } 64 return False; 65} 66 67void _XDeqAsyncHandler( 68 Display *dpy, 69 register _XAsyncHandler *handler) 70{ 71 register _XAsyncHandler **prev; 72 register _XAsyncHandler *async; 73 74 for (prev = &dpy->async_handlers; 75 (async = *prev) && (async != handler); 76 prev = &async->next) 77 ; 78 if (async) 79 *prev = async->next; 80} 81 82char * 83_XGetAsyncReply( 84 register Display *dpy, 85 register char *replbuf, /* data is read into this buffer */ 86 register xReply *rep, /* value passed to calling handler */ 87 char *buf, /* value passed to calling handler */ 88 int len, /* value passed to calling handler */ 89 int extra, /* extra words to read, ala _XReply */ 90 Bool discard) /* discard after extra?, ala _XReply */ 91{ 92 if (extra == 0) { 93 if (discard && (rep->generic.length << 2) > len) 94 _XEatData (dpy, (rep->generic.length << 2) - len); 95 return (char *)rep; 96 } 97 98 if (extra <= rep->generic.length) { 99 int size = SIZEOF(xReply) + (extra << 2); 100 if (size > len) { 101 memcpy(replbuf, buf, len); 102 _XRead(dpy, replbuf + len, size - len); 103 buf = replbuf; 104 len = size; 105#ifdef MUSTCOPY 106 } else { 107 memcpy(replbuf, buf, size); 108 buf = replbuf; 109#endif 110 } 111 112 if (discard && rep->generic.length > extra && 113 (rep->generic.length << 2) > len) 114 _XEatData (dpy, (rep->generic.length << 2) - len); 115 116 return buf; 117 } 118 /* 119 *if we get here, then extra > rep->generic.length--meaning we 120 * read a reply that's shorter than we expected. This is an 121 * error, but we still need to figure out how to handle it... 122 */ 123 if ((rep->generic.length << 2) > len) 124 _XEatData (dpy, (rep->generic.length << 2) - len); 125 _XIOError (dpy); 126 return (char *)rep; 127} 128 129void 130_XGetAsyncData( 131 Display *dpy, 132 char *data, /* data is read into this buffer */ 133 char *buf, /* value passed to calling handler */ 134 int len, /* value passed to calling handler */ 135 int skip, /* number of bytes already read in previous 136 _XGetAsyncReply or _XGetAsyncData calls */ 137 int datalen, /* size of data buffer in bytes */ 138 int discardtotal) /* min. bytes to consume (after skip) */ 139{ 140 buf += skip; 141 len -= skip; 142 if (!data) { 143 if (datalen > len) 144 _XEatData(dpy, datalen - len); 145 } else if (datalen <= len) { 146 memcpy(data, buf, datalen); 147 } else { 148 memcpy(data, buf, len); 149 _XRead(dpy, data + len, datalen - len); 150 } 151 if (discardtotal > len) { 152 if (datalen > len) 153 len = datalen; 154 _XEatData(dpy, discardtotal - len); 155 } 156} 157