⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xlibint.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * $XConsortium: XlibInt.c,v 11.156.1.1 92/11/11 10:10:50 rws Exp $ *//* Copyright    Massachusetts Institute of Technology    1985, 1986, 1987 *//*Permission to use, copy, modify, distribute, and sell this software and itsdocumentation for any purpose is hereby granted without fee, provided thatthe above copyright notice appear in all copies and that both thatcopyright notice and this permission notice appear in supportingdocumentation, and that the name of M.I.T. not be used in advertising orpublicity pertaining to distribution of the software without specific,written prior permission.  M.I.T. makes no representations about thesuitability of this software for any purpose.  It is provided "as is"without express or implied warranty.*//* *	XlibInternal.c - Internal support routines for the C subroutine *	interface library (Xlib) to the X Window System Protocol V11.0. */#define NEED_EVENTS#define NEED_REPLIES#include <X11/Xlibint.h>#include <X11/Xos.h>#include "Xlibnet.h"#include <stdio.h>static void _EatData32();/* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX * systems are broken and return EWOULDBLOCK when they should return EAGAIN */#if defined(EAGAIN) && defined(EWOULDBLOCK)#define ETEST(err) (err == EAGAIN || err == EWOULDBLOCK)#else#ifdef EAGAIN#define ETEST(err) (err == EAGAIN)#else#define ETEST(err) (err == EWOULDBLOCK)#endif#endif#ifdef LACHMAN#ifdef EMSGSIZE#undef EMSGSIZE#endif#define EMSGSIZE ERANGE#endif/* * The following routines are internal routines used by Xlib for protocol * packet transmission and reception. * * XIOError(Display *) will be called if any sort of system call error occurs. * This is assumed to be a fatal condition, i.e., XIOError should not return. * * XError(Display *, XErrorEvent *) will be called whenever an X_Error event is * received.  This is not assumed to be a fatal condition, i.e., it is * acceptable for this procedure to return.  However, XError should NOT * perform any operations (directly or indirectly) on the DISPLAY. * * Routines declared with a return type of 'Status' return 0 on failure, * and non 0 on success.  Routines with no declared return type don't  * return anything.  Whenever possible routines that create objects return * the object they have created. */extern _XQEvent *_qfree;static int padlength[4] = {0, 3, 2, 1};    /* lookup table for adding padding bytes to data that is read from    	or written to the X socket.  */static xReq _dummy_request = {	0, 0, 0};/* * _XFlush - Flush the X request buffer.  If the buffer is empty, no * action is taken.  This routine correctly handles incremental writes. * This routine may have to be reworked if int < long. */_XFlush (dpy)	register Display *dpy;{	register long size, todo;	register int write_stat;	register char *bufindex;	if (dpy->flags & XlibDisplayIOError) return;	size = todo = dpy->bufptr - dpy->buffer;	bufindex = dpy->bufptr = dpy->buffer;	/*	 * While write has not written the entire buffer, keep looping	 * until the entire buffer is written.  bufindex will be incremented	 * and size decremented as buffer is written out.	 */	while (size) {	    errno = 0;	    write_stat = WriteToServer(dpy->fd, bufindex, (int) todo);	    if (write_stat >= 0) {		size -= write_stat;		todo = size;		bufindex += write_stat;	    } else if (ETEST(errno)) {		_XWaitForWritable(dpy);#ifdef SUNSYSV	    } else if (errno == 0) {		_XWaitForWritable(dpy);#endif#ifdef EMSGSIZE	    } else if (errno == EMSGSIZE) {		if (todo > 1) 		  todo >>= 1;		else		  _XWaitForWritable(dpy);#endif	    } else if (errno != EINTR) {		/* Write failed! */		/* errno set by write system call. */		_XIOError(dpy);	    }	}	dpy->last_req = (char *)&_dummy_request;}int_XEventsQueued (dpy, mode)    register Display *dpy;    int mode;{	register int len;	int pend;	char buf[BUFSIZE];	register xReply *rep;		if (mode == QueuedAfterFlush)	{	    _XFlush(dpy);	    if (dpy->qlen)		return(dpy->qlen);	}	if (dpy->flags & XlibDisplayIOError) return(dpy->qlen);	if (BytesReadable(dpy->fd, (char *) &pend) < 0)	    _XIOError(dpy);#ifdef XCONN_CHECK_FREQ	/* This is a crock, required because FIONREAD or equivalent is	 * not guaranteed to detect a broken connection.	 */	if (!pend && !dpy->qlen && ++dpy->conn_checker >= XCONN_CHECK_FREQ)	{	    unsigned long r_mask[MSKCNT];	    static struct timeval zero_time;	    dpy->conn_checker = 0;	    CLEARBITS(r_mask);	    BITSET(r_mask, dpy->fd);	    if (pend = select(dpy->fd + 1, (int *)r_mask, NULL, NULL,			      &zero_time))	    {		if (pend > 0)		{		    if (BytesReadable(dpy->fd, (char *) &pend) < 0)			_XIOError(dpy);		    /* we should not get zero, if we do, force a read */		    if (!pend)			pend = SIZEOF(xReply);		}		else if (pend < 0 && errno != EINTR)		    _XIOError(dpy);	    }	}#endif /* XCONN_CHECK_FREQ */	if (!(len = pend))	    return(dpy->qlen);	/* _XFlush can enqueue events */      /* Force a read if there is not enough data.  Otherwise,       * a select() loop at a higher-level will spin undesirably,       * and we've seen at least one OS that appears to not update       * the result from FIONREAD once it has returned nonzero.       */	if (len < SIZEOF(xReply))	    len = SIZEOF(xReply);	else if (len > BUFSIZE)	    len = BUFSIZE;	len /= SIZEOF(xReply);	pend = len * SIZEOF(xReply);#ifdef XCONN_CHECK_FREQ	dpy->conn_checker = 0;#endif	_XRead (dpy, buf, (long) pend);	/* no space between comma and type or else macro will die */	STARTITERATE (rep,xReply, buf, (len > 0), len--) {	    if (rep->generic.type == X_Error)		_XError(dpy, (xError *)rep);	    else   /* must be an event packet */		_XEnq(dpy, (xEvent *) rep);	}	ENDITERATE	return(dpy->qlen);}/* _XReadEvents - Flush the output queue, * then read as many events as possible (but at least 1) and enqueue them */_XReadEvents(dpy)	register Display *dpy;{	char buf[BUFSIZE];	long pend_not_register; /* because can't "&" a register variable */	register long pend;	register xEvent *ev;	Bool not_yet_flushed = True;	do {	    /* find out how much data can be read */	    if (BytesReadable(dpy->fd, (char *) &pend_not_register) < 0)	    	_XIOError(dpy);	    pend = pend_not_register;	    /* must read at least one xEvent; if none is pending, then	       we'll just flush and block waiting for it */	    if (pend < SIZEOF(xEvent)) {	    	pend = SIZEOF(xEvent);		/* don't flush until we block the first time */		if (not_yet_flushed) {		    int qlen = dpy->qlen;		    _XFlush (dpy);		    if (qlen != dpy->qlen) return;		    not_yet_flushed = False;		}	    }			    /* but we won't read more than the max buffer size */	    if (pend > BUFSIZE)	    	pend = BUFSIZE;	    /* round down to an integral number of XReps */	    pend = (pend / SIZEOF(xEvent)) * SIZEOF(xEvent);	    _XRead (dpy, buf, pend);	    /* no space between comma and type or else macro will die */	    STARTITERATE (ev,xEvent, buf, (pend > 0),			  pend -= SIZEOF(xEvent)) {		if (ev->u.u.type == X_Error)		    _XError (dpy, (xError *) ev);		else  /* it's an event packet; enqueue it */		    _XEnq (dpy, ev);	    }	    ENDITERATE	} while (dpy->head == NULL);}/*  * _XRead - Read bytes from the socket taking into account incomplete * reads.  This routine may have to be reworked if int < long. */_XRead (dpy, data, size)	register Display *dpy;	register char *data;	register long size;{	register long bytes_read;	if ((dpy->flags & XlibDisplayIOError) || size == 0) return;	errno = 0;	while ((bytes_read = ReadFromServer(dpy->fd, data, (int)size))		!= size) {	    	if (bytes_read > 0) {		    size -= bytes_read;		    data += bytes_read;		    }		else if (ETEST(errno)) {		    _XWaitForReadable(dpy);		    errno = 0;		}#ifdef SUNSYSV		else if (errno == 0) {		    _XWaitForReadable(dpy);		}#endif		else if (bytes_read == 0) {		    /* Read failed because of end of file! */		    errno = EPIPE;		    _XIOError(dpy);		    }		else  /* bytes_read is less than 0; presumably -1 */ {		    /* If it's a system call interrupt, it's not an error. */		    if (errno != EINTR)		    	_XIOError(dpy);		    }	    	 }}#ifdef WORD64/* * XXX This is a *really* stupid way of doing this.... * PACKBUFFERSIZE must be a multiple of 4. */#define PACKBUFFERSIZE 4096/* * _XRead32 - Read bytes from the socket unpacking each 32 bits *            into a long (64 bits on a CRAY computer). *  */static _doXRead32 (dpy, data, size, packbuffer)        register Display *dpy;        register long *data;        register long size;	register char *packbuffer;{ long *lpack,*lp; long mask32 = 0x00000000ffffffff; long maskw, nwords, i, bits;        _XReadPad (dpy, packbuffer, size);        lp = data;        lpack = (long *) packbuffer;        nwords = size >> 2;        bits = 32;        for(i=0;i<nwords;i++){            maskw = mask32 << bits;           *lp++ = ( *lpack & maskw ) >> bits;            bits = bits ^32;            if(bits){               lpack++;            }        }}_XRead32 (dpy, data, len)    Display *dpy;    long *data;    long len;{    char packbuffer[PACKBUFFERSIZE];    unsigned nunits = PACKBUFFERSIZE >> 2;    for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {	_doXRead32 (dpy, data, PACKBUFFERSIZE, packbuffer);    }    if (len) _doXRead32 (dpy, data, len, packbuffer);}/* * _XRead16 - Read bytes from the socket unpacking each 16 bits *            into a long (64 bits on a CRAY computer). * */static _doXRead16 (dpy, data, size, packbuffer)        register Display *dpy;        register short *data;        register long size;	char *packbuffer;{	long *lpack,*lp;	long mask16 = 0x000000000000ffff;	long maskw, nwords, i, bits;        _XRead(dpy,packbuffer,size);	/* don't do a padded read... */        lp = (long *) data;        lpack = (long *) packbuffer;        nwords = size >> 1;  /* number of 16 bit words to be unpacked */        bits = 48;        for(i=0;i<nwords;i++){            maskw = mask16 << bits;           *lp++ = ( *lpack & maskw ) >> bits;            bits -= 16;            if(bits < 0){               lpack++;               bits = 48;            }        }}_XRead16 (dpy, data, len)    Display *dpy;    short *data;    long len;{    char packbuffer[PACKBUFFERSIZE];    unsigned nunits = PACKBUFFERSIZE >> 1;    for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {	_doXRead16 (dpy, data, PACKBUFFERSIZE, packbuffer);    }    if (len) _doXRead16 (dpy, data, len, packbuffer);}_XRead16Pad (dpy, data, size)    Display *dpy;    short *data;    long size;{    int slop = (size & 3);    short slopbuf[3];    _XRead16 (dpy, data, size);    if (slop > 0) {	_XRead16 (dpy, slopbuf, 4 - slop);    }}#endif /* WORD64 *//* * _XReadPad - Read bytes from the socket taking into account incomplete * reads.  If the number of bytes is not 0 mod 32, read additional pad * bytes. This routine may have to be reworked if int < long. */_XReadPad (dpy, data, size)    	register Display *dpy;		register char *data;	register long size;{    	register long bytes_read;	struct iovec iov[2];	char pad[3];	if ((dpy->flags & XlibDisplayIOError) || size == 0) return;	iov[0].iov_len = (int)size;	iov[0].iov_base = data;	/* 	 * The following hack is used to provide 32 bit long-word	 * aligned padding.  The [1] vector is of length 0, 1, 2, or 3,	 * whatever is needed.	 */	iov[1].iov_len = padlength[size & 3];	iov[1].iov_base = pad;	size += iov[1].iov_len;	errno = 0;	while ((bytes_read = ReadvFromServer (dpy->fd, iov, 2)) != size) {	    if (bytes_read > 0) {		size -= bytes_read;	    	if ((iov[0].iov_len -= bytes_read) < 0) {		    iov[1].iov_len += iov[0].iov_len;		    iov[1].iov_base -= iov[0].iov_len;		    iov[0].iov_len = 0;		    }	    	else	    	    iov[0].iov_base += bytes_read;	    	}	    else if (ETEST(errno)) {		_XWaitForReadable(dpy);		errno = 0;	    }#ifdef SUNSYSV	    else if (errno == 0) {		_XWaitForReadable(dpy);	    }#endif	    else if (bytes_read == 0) {		/* Read failed because of end of file! */		errno = EPIPE;		_XIOError(dpy);		}	    	    else  /* bytes_read is less than 0; presumably -1 */ {		/* If it's a system call interrupt, it's not an error. */		if (errno != EINTR)		    _XIOError(dpy);		}	    }}/* * _XSend - Flush the buffer and send the client data. 32 bit word aligned * transmission is used, if size is not 0 mod 4, extra bytes are transmitted. * This routine may have to be reworked if int < long; */_XSend (dpy, data, size)	register Display *dpy;	char *data;	register long size;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -