1450N/A/*
1450N/A * cslibint.c -- low level I/O
1450N/A *
1450N/A * (c) Copyright 1993-1994 Adobe Systems Incorporated.
1450N/A * All rights reserved.
1450N/A *
1450N/A * Permission to use, copy, modify, distribute, and sublicense this software
1450N/A * and its documentation for any purpose and without fee is hereby granted,
1450N/A * provided that the above copyright notices appear in all copies and that
1450N/A * both those copyright notices and this permission notice appear in
1450N/A * supporting documentation and that the name of Adobe Systems Incorporated
1450N/A * not be used in advertising or publicity pertaining to distribution of the
1450N/A * software without specific, written prior permission. No trademark license
1450N/A * to use the Adobe trademarks is hereby granted. If the Adobe trademark
1450N/A * "Display PostScript"(tm) is used to describe this software, its
1450N/A * functionality or for any other purpose, such use shall be limited to a
1450N/A * statement that this software works in conjunction with the Display
1450N/A * PostScript system. Proper trademark attribution to reflect Adobe's
1450N/A * ownership of the trademark shall be given whenever any such reference to
1450N/A * the Display PostScript system is made.
1450N/A *
1450N/A * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
1450N/A * ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
1450N/A * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
1450N/A * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1450N/A * NON- INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL ADOBE BE LIABLE
1450N/A * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
1450N/A * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
1450N/A * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
1450N/A * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ADOBE WILL NOT
1450N/A * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
1450N/A *
1450N/A * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
1450N/A * Incorporated which may be registered in certain jurisdictions
1450N/A *
1450N/A * Portions Copyright 1985, 1986, 1987 Massachusetts Institute of Technology
1450N/A *
1450N/A * Permission to use, copy, modify, distribute, and sell this software and its
1450N/A * documentation for any purpose is hereby granted without fee, provided that
1450N/A * the above copyright notice appear in all copies and that both that
1450N/A * copyright notice and this permission notice appear in supporting
1450N/A * documentation, and that the name of M.I.T. not be used in advertising or
1450N/A * publicity pertaining to distribution of the software without specific,
1450N/A * written prior permission. M.I.T. makes no representations about the
1450N/A * suitability of this software for any purpose. It is provided "as is"
1450N/A * without express or implied warranty.
1450N/A *
1450N/A * Author: Adobe Systems Incorporated and MIT X Consortium
1450N/A */
1450N/A/* $XFree86: xc/lib/dps/cslibint.c,v 1.4tsi Exp $ */
1450N/A
1450N/A/*
1450N/A * XlibInternal.c - Internal support routines for the C subroutine
1450N/A * interface library (Xlib) to the X Window System Protocol V11.0.
1450N/A */
1450N/A#define NEED_EVENTS
1450N/A#define NEED_REPLIES
1450N/A
1450N/A#include <X11/Xlibint.h>
1450N/A#include <X11/Xos.h>
1450N/A#include "Xlibnet.h"
1450N/A#include <stdio.h>
1450N/A
1450N/A#include "dpsassert.h"
1450N/A#include "cslibint.h"
1450N/A
1450N/Astatic void _EatData32 (Display *dpy, unsigned long n);
1450N/A
1450N/A/* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
1450N/A * systems are broken and return EWOULDBLOCK when they should return EAGAIN
1450N/A */
1450N/A#if defined(EAGAIN) && defined(EWOULDBLOCK)
1450N/A#define ETEST(err) (err == EAGAIN || err == EWOULDBLOCK)
1450N/A#else
1450N/A#ifdef EAGAIN
1450N/A#define ETEST(err) (err == EAGAIN)
1450N/A#else
1450N/A#define ETEST(err) (err == EWOULDBLOCK)
1450N/A#endif
1450N/A#endif
1450N/A
1450N/A#ifdef LACHMAN
1450N/A#ifdef EMSGSIZE
1450N/A#undef EMSGSIZE
1450N/A#endif
1450N/A#define EMSGSIZE ERANGE
1450N/A#endif
1450N/A
1450N/A#if defined(SVR4) && defined(sun)
1450N/A#define SUNSYSV 1
1450N/A#endif
1450N/A
1450N/A/*
1450N/A * The following routines are internal routines used by Xlib for protocol
1450N/A * packet transmission and reception.
1450N/A *
1450N/A * XIOError(Display *) will be called if any sort of system call error occurs.
1450N/A * This is assumed to be a fatal condition, i.e., XIOError should not return.
1450N/A *
1450N/A * XError(Display *, XErrorEvent *) will be called whenever an X_Error event is
1450N/A * received. This is not assumed to be a fatal condition, i.e., it is
1450N/A * acceptable for this procedure to return. However, XError should NOT
1450N/A * perform any operations (directly or indirectly) on the DISPLAY.
1450N/A *
1450N/A * Routines declared with a return type of 'Status' return 0 on failure,
1450N/A * and non 0 on success. Routines with no declared return type don't
1450N/A * return anything. Whenever possible routines that create objects return
1450N/A * the object they have created.
1450N/A */
1450N/A
1450N/Aextern _XQEvent *_qfree;
1450N/A
1450N/Astatic int padlength[4] = {0, 3, 2, 1};
1450N/A /* lookup table for adding padding bytes to data that is read from
1450N/A or written to the X socket. */
1450N/A
1450N/Astatic xReq _dummy_request = {
1450N/A 0, 0, 0
1450N/A};
1450N/A/*
1450N/A * N_XFlush - Flush the X request buffer. If the buffer is empty, no
1450N/A * action is taken. This routine correctly handles incremental writes.
1450N/A * This routine may have to be reworked if int < long.
1450N/A */
1450N/Avoid N_XFlush (Display *dpy)
1450N/A{
1450N/A register long size, todo;
1450N/A register int write_stat;
1450N/A register char *bufindex;
1450N/A
1450N/A if (!dpy) return;
1450N/A
1450N/A if (dpy->flags & XlibDisplayIOError) return;
1450N/A
1450N/A size = todo = dpy->bufptr - dpy->buffer;
1450N/A bufindex = dpy->bufptr = dpy->buffer;
1450N/A /*
1450N/A * While write has not written the entire buffer, keep looping
1450N/A * until the entire buffer is written. bufindex will be incremented
1450N/A * and size decremented as buffer is written out.
1450N/A */
1450N/A while (size) {
1450N/A errno = 0;
1450N/A write_stat = WriteToServer(dpy->fd, bufindex, (int) todo);
1450N/A if (write_stat >= 0) {
1450N/A size -= write_stat;
1450N/A todo = size;
1450N/A bufindex += write_stat;
1450N/A } else if (ETEST(errno)) {
1450N/A N_XWaitForWritable(dpy);
1450N/A#ifdef SUNSYSV
1450N/A } else if (errno == 0) {
1450N/A N_XWaitForWritable(dpy);
1450N/A#endif
1450N/A#ifdef EMSGSIZE
1450N/A } else if (errno == EMSGSIZE) {
1450N/A if (todo > 1)
1450N/A todo >>= 1;
1450N/A else
1450N/A N_XWaitForWritable(dpy);
1450N/A#endif
1450N/A } else if (errno != EINTR) {
1450N/A /* Write failed! */
1450N/A /* errno set by write system call. */
1450N/A _XIOError(dpy);
1450N/A }
1450N/A }
1450N/A dpy->last_req = (char *)&_dummy_request;
1450N/A}
1450N/A
1450N/A#ifdef NEEDFORNX
1450N/A
1450N/Aint
1450N/A_XEventsQueued (Display *dpy, int mode)
1450N/A{
1450N/A register int len;
1450N/A int pend;
1450N/A char buf[BUFSIZE];
1450N/A register xReply *rep;
1450N/A
1450N/A if (mode == QueuedAfterFlush)
1450N/A {
1450N/A _XFlush(dpy);
1450N/A if (dpy->qlen)
1450N/A return(dpy->qlen);
1450N/A }
1450N/A if (dpy->flags & XlibDisplayIOError) return(dpy->qlen);
1450N/A if (BytesReadable(dpy->fd, (char *) &pend) < 0)
1450N/A _XIOError(dpy);
1450N/A#ifdef XCONN_CHECK_FREQ
1450N/A /* This is a crock, required because FIONREAD or equivalent is
1450N/A * not guaranteed to detect a broken connection.
1450N/A */
1450N/A if (!pend && !dpy->qlen && ++dpy->conn_checker >= XCONN_CHECK_FREQ)
1450N/A {
1450N/A unsigned long r_mask[MSKCNT];
1450N/A static struct timeval zero_time;
1450N/A
1450N/A dpy->conn_checker = 0;
1450N/A CLEARBITS(r_mask);
1450N/A BITSET(r_mask, dpy->fd);
1450N/A if (pend = select(dpy->fd + 1, (int *)r_mask, NULL, NULL,
1450N/A &zero_time))
1450N/A {
1450N/A if (pend > 0)
1450N/A {
1450N/A if (BytesReadable(dpy->fd, (char *) &pend) < 0)
1450N/A _XIOError(dpy);
1450N/A /* we should not get zero, if we do, force a read */
1450N/A if (!pend)
1450N/A pend = SIZEOF(xReply);
1450N/A }
1450N/A else if (pend < 0 && errno != EINTR)
1450N/A _XIOError(dpy);
1450N/A }
1450N/A }
1450N/A#endif /* XCONN_CHECK_FREQ */
1450N/A if (!(len = pend))
1450N/A return(dpy->qlen); /* _XFlush can enqueue events */
1450N/A /* Force a read if there is not enough data. Otherwise,
1450N/A * a select() loop at a higher-level will spin undesirably,
1450N/A * and we've seen at least one OS that appears to not update
1450N/A * the result from FIONREAD once it has returned nonzero.
1450N/A */
1450N/A if (len < SIZEOF(xReply))
1450N/A len = SIZEOF(xReply);
1450N/A else if (len > BUFSIZE)
1450N/A len = BUFSIZE;
1450N/A len /= SIZEOF(xReply);
1450N/A pend = len * SIZEOF(xReply);
1450N/A#ifdef XCONN_CHECK_FREQ
1450N/A dpy->conn_checker = 0;
1450N/A#endif
1450N/A _XRead (dpy, buf, (long) pend);
1450N/A
1450N/A /* no space between comma and type or else macro will die */
1450N/A STARTITERATE (rep,xReply, buf, (len > 0), len--) {
1450N/A if (rep->generic.type == X_Error)
1450N/A _XError(dpy, (xError *)rep);
1450N/A else /* must be an event packet */
1450N/A _XEnq(dpy, (xEvent *) rep);
1450N/A }
1450N/A ENDITERATE
1450N/A return(dpy->qlen);
1450N/A}
1450N/A
1450N/A/* _XReadEvents - Flush the output queue,
1450N/A * then read as many events as possible (but at least 1) and enqueue them
1450N/A */
1450N/Avoid _XReadEvents(Display *dpy)
1450N/A{
1450N/A char buf[BUFSIZE];
1450N/A long pend_not_register; /* because can't "&" a register variable */
1450N/A register long pend;
1450N/A register xEvent *ev;
1450N/A Bool not_yet_flushed = True;
1450N/A
1450N/A do {
1450N/A /* find out how much data can be read */
1450N/A if (BytesReadable(dpy->fd, (char *) &pend_not_register) < 0)
1450N/A _XIOError(dpy);
1450N/A pend = pend_not_register;
1450N/A
1450N/A /* must read at least one xEvent; if none is pending, then
1450N/A we'll just flush and block waiting for it */
1450N/A if (pend < SIZEOF(xEvent)) {
1450N/A pend = SIZEOF(xEvent);
1450N/A /* don't flush until we block the first time */
1450N/A if (not_yet_flushed) {
1450N/A int qlen = dpy->qlen;
1450N/A _XFlush (dpy);
1450N/A if (qlen != dpy->qlen) return;
1450N/A not_yet_flushed = False;
1450N/A }
1450N/A }
1450N/A
1450N/A /* but we won't read more than the max buffer size */
1450N/A if (pend > BUFSIZE)
1450N/A pend = BUFSIZE;
1450N/A
1450N/A /* round down to an integral number of XReps */
1450N/A pend = (pend / SIZEOF(xEvent)) * SIZEOF(xEvent);
1450N/A
1450N/A _XRead (dpy, buf, pend);
1450N/A
1450N/A /* no space between comma and type or else macro will die */
1450N/A STARTITERATE (ev,xEvent, buf, (pend > 0),
1450N/A pend -= SIZEOF(xEvent)) {
1450N/A if (ev->u.u.type == X_Error)
1450N/A _XError (dpy, (xError *) ev);
1450N/A else /* it's an event packet; enqueue it */
1450N/A _XEnq (dpy, ev);
1450N/A }
1450N/A ENDITERATE
1450N/A } while (dpy->head == NULL);
1450N/A}
1450N/A
1450N/A#endif /* NEEDFORNX */
1450N/A
1450N/A/*
1450N/A * N_XRead - Read bytes from the socket taking into account incomplete
1450N/A * reads. This routine may have to be reworked if int < long.
1450N/A */
1450N/Aint N_XRead (Display *dpy, char *data, long size)
1450N/A{
1450N/A register long bytes_read;
1450N/A
1450N/A if (!dpy) return 0;
1450N/A if ((dpy->flags & XlibDisplayIOError) || size == 0) return 0;
1450N/A errno = 0;
1450N/A while ((bytes_read = ReadFromServer(dpy->fd, data, (int)size))
1450N/A != size) {
1450N/A
1450N/A if (bytes_read > 0) {
1450N/A size -= bytes_read;
1450N/A data += bytes_read;
1450N/A }
1450N/A else if (ETEST(errno)) {
1450N/A N_XWaitForReadable(dpy);
1450N/A errno = 0;
1450N/A }
1450N/A#ifdef SUNSYSV
1450N/A else if (errno == 0) {
1450N/A N_XWaitForReadable(dpy);
1450N/A }
1450N/A#endif
1450N/A else if (bytes_read == 0) {
1450N/A /* Read failed because of end of file! */
1450N/A errno = EPIPE;
1450N/A _XIOError(dpy);
1450N/A }
1450N/A
1450N/A else /* bytes_read is less than 0; presumably -1 */ {
1450N/A /* If it's a system call interrupt, it's not an error. */
1450N/A if (errno != EINTR)
1450N/A _XIOError(dpy);
1450N/A }
1450N/A }
1450N/A return 0;
1450N/A}
1450N/A
1450N/A#ifdef WORD64
1450N/A
1450N/A/*
1450N/A * XXX This is a *really* stupid way of doing this....
1450N/A * PACKBUFFERSIZE must be a multiple of 4.
1450N/A */
1450N/A
1450N/A#define PACKBUFFERSIZE 4096
1450N/A
1450N/A
1450N/A/*
1450N/A * _XRead32 - Read bytes from the socket unpacking each 32 bits
1450N/A * into a long (64 bits on a CRAY computer).
1450N/A *
1450N/A */
1450N/Astatic void _doXRead32 (Display *dpy, long *data, long size, char *packbuffer)
1450N/A{
1450N/A long *lpack,*lp;
1450N/A long mask32 = 0x00000000ffffffff;
1450N/A long maskw, nwords, i, bits;
1450N/A
1450N/A _XReadPad (dpy, packbuffer, size);
1450N/A
1450N/A lp = data;
1450N/A lpack = (long *) packbuffer;
1450N/A nwords = size >> 2;
1450N/A bits = 32;
1450N/A
1450N/A for(i=0;i<nwords;i++){
1450N/A maskw = mask32 << bits;
1450N/A *lp++ = ( *lpack & maskw ) >> bits;
1450N/A bits = bits ^32;
1450N/A if(bits){
1450N/A lpack++;
1450N/A }
1450N/A }
1450N/A}
1450N/A
1450N/Avoid _XRead32 (Display *dpy, long *data, long len)
1450N/A{
1450N/A char packbuffer[PACKBUFFERSIZE];
1450N/A unsigned nunits = PACKBUFFERSIZE >> 2;
1450N/A
1450N/A for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {
1450N/A _doXRead32 (dpy, data, PACKBUFFERSIZE, packbuffer);
1450N/A }
1450N/A if (len) _doXRead32 (dpy, data, len, packbuffer);
1450N/A}
1450N/A
1450N/A
1450N/A
1450N/A/*
1450N/A * _XRead16 - Read bytes from the socket unpacking each 16 bits
1450N/A * into a long (64 bits on a CRAY computer).
1450N/A *
1450N/A */
1450N/Astatic void _doXRead16 (Display *dpy, short *data, long size, char *packbuffer)
1450N/A{
1450N/A long *lpack,*lp;
1450N/A long mask16 = 0x000000000000ffff;
1450N/A long maskw, nwords, i, bits;
1450N/A
1450N/A _XRead(dpy,packbuffer,size); /* don't do a padded read... */
1450N/A
1450N/A lp = (long *) data;
1450N/A lpack = (long *) packbuffer;
1450N/A nwords = size >> 1; /* number of 16 bit words to be unpacked */
1450N/A bits = 48;
1450N/A for(i=0;i<nwords;i++){
1450N/A maskw = mask16 << bits;
1450N/A *lp++ = ( *lpack & maskw ) >> bits;
1450N/A bits -= 16;
1450N/A if(bits < 0){
1450N/A lpack++;
1450N/A bits = 48;
1450N/A }
1450N/A }
1450N/A}
1450N/A
1450N/Avoid _XRead16 (Display *dpy, short *data, long len)
1450N/A{
1450N/A char packbuffer[PACKBUFFERSIZE];
1450N/A unsigned nunits = PACKBUFFERSIZE >> 1;
1450N/A
1450N/A for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {
1450N/A _doXRead16 (dpy, data, PACKBUFFERSIZE, packbuffer);
1450N/A }
1450N/A if (len) _doXRead16 (dpy, data, len, packbuffer);
1450N/A}
1450N/A
1450N/Avoid _XRead16Pad (Display *dpy, short *data, long size)
1450N/A{
1450N/A int slop = (size & 3);
1450N/A short slopbuf[3];
1450N/A
1450N/A _XRead16 (dpy, data, size);
1450N/A if (slop > 0) {
1450N/A _XRead16 (dpy, slopbuf, 4 - slop);
1450N/A }
1450N/A}
1450N/A#endif /* WORD64 */
1450N/A
1450N/A
1450N/A/*
1450N/A * N_XReadPad - Read bytes from the socket taking into account incomplete
1450N/A * reads. If the number of bytes is not 0 mod 32, read additional pad
1450N/A * bytes. This routine may have to be reworked if int < long.
1450N/A */
1450N/Avoid N_XReadPad (Display *dpy, char *data, long size)
1450N/A{
1450N/A register long bytes_read;
1450N/A struct iovec iov[2];
1450N/A char pad[3];
1450N/A
1450N/A if (!dpy) return;
1450N/A if ((dpy->flags & XlibDisplayIOError) || size == 0) return;
1450N/A iov[0].iov_len = (int)size;
1450N/A iov[0].iov_base = data;
1450N/A /*
1450N/A * The following hack is used to provide 32 bit long-word
1450N/A * aligned padding. The [1] vector is of length 0, 1, 2, or 3,
1450N/A * whatever is needed.
1450N/A */
1450N/A
1450N/A iov[1].iov_len = padlength[size & 3];
1450N/A iov[1].iov_base = pad;
1450N/A size += iov[1].iov_len;
1450N/A errno = 0;
1450N/A while ((bytes_read = ReadvFromServer (dpy->fd, iov, 2)) != size) {
1450N/A
1450N/A if (bytes_read > 0) {
1450N/A size -= bytes_read;
1450N/A if (iov[0].iov_len < bytes_read) {
1450N/A iov[1].iov_len += iov[0].iov_len - bytes_read;
1450N/A iov[1].iov_base =
1450N/A (char *)iov[1].iov_base + bytes_read - iov[0].iov_len;
1450N/A iov[0].iov_len = 0;
1450N/A }
1450N/A else {
1450N/A iov[0].iov_len -= bytes_read;
1450N/A iov[0].iov_base = (char *)iov[0].iov_base + bytes_read;
1450N/A }
1450N/A }
1450N/A else if (ETEST(errno)) {
1450N/A N_XWaitForReadable(dpy);
1450N/A errno = 0;
1450N/A }
1450N/A#ifdef SUNSYSV
1450N/A else if (errno == 0) {
1450N/A N_XWaitForReadable(dpy);
1450N/A }
1450N/A#endif
1450N/A else if (bytes_read == 0) {
1450N/A /* Read failed because of end of file! */
1450N/A errno = EPIPE;
1450N/A _XIOError(dpy);
1450N/A }
1450N/A
1450N/A else /* bytes_read is less than 0; presumably -1 */ {
1450N/A /* If it's a system call interrupt, it's not an error. */
1450N/A if (errno != EINTR)
1450N/A _XIOError(dpy);
1450N/A }
1450N/A }
1450N/A}
1450N/A
1450N/A/*
1450N/A * N_XSend - Flush the buffer and send the client data. 32 bit word aligned
1450N/A * transmission is used, if size is not 0 mod 4, extra bytes are transmitted.
1450N/A * This routine may have to be reworked if int < long;
1450N/A */
1450N/Avoid N_XSend (Display *dpy, _Xconst char *data, long size)
1450N/A{
1450N/A struct iovec iov[3];
1450N/A static char pad[3] = {0, 0, 0};
1450N/A /* XText8 and XText16 require that the padding bytes be zero! */
1450N/A
1450N/A long skip = 0;
1450N/A long dpybufsize = (dpy->bufptr - dpy->buffer);
1450N/A long padsize = padlength[size & 3];
1450N/A long total = dpybufsize + size + padsize;
1450N/A long todo = total;
1450N/A
1450N/A if (dpy->flags & XlibDisplayIOError) return;
1450N/A
1450N/A /*
1450N/A * There are 3 pieces that may need to be written out:
1450N/A *
1450N/A * o whatever is in the display buffer
1450N/A * o the data passed in by the user
1450N/A * o any padding needed to 32bit align the whole mess
1450N/A *
1450N/A * This loop looks at all 3 pieces each time through. It uses skip
1450N/A * to figure out whether or not a given piece is needed.
1450N/A */
1450N/A while (total) {
1450N/A long before = skip; /* amount of whole thing written */
1450N/A long remain = todo; /* amount to try this time, <= total */
1450N/A int i = 0;
1450N/A long len;
1450N/A
1450N/A /* You could be very general here and have "in" and "out" iovecs
1450N/A * and write a loop without using a macro, but what the heck. This
1450N/A * translates to:
1450N/A *
1450N/A * how much of this piece is new?
1450N/A * if more new then we are trying this time, clamp
1450N/A * if nothing new
1450N/A * then bump down amount already written, for next piece
1450N/A * else put new stuff in iovec, will need all of next piece
1450N/A *
1450N/A * Note that todo had better be at least 1 or else we'll end up
1450N/A * writing 0 iovecs.
1450N/A */
1450N/A#define InsertIOV(pointer, length) \
1450N/A len = (length) - before; \
1450N/A if (len > remain) \
1450N/A len = remain; \
1450N/A if (len <= 0) { \
1450N/A before = (-len); \
1450N/A } else { \
1450N/A iov[i].iov_len = len; \
1450N/A iov[i].iov_base = (pointer) + before; \
1450N/A i++; \
1450N/A remain -= len; \
1450N/A before = 0; \
1450N/A }
1450N/A
1450N/A InsertIOV (dpy->buffer, dpybufsize)
1450N/A InsertIOV ((char *)data, size)
1450N/A InsertIOV (pad, padsize)
1450N/A
1450N/A errno = 0;
1450N/A if ((len = WritevToServer(dpy->fd, iov, i)) >= 0) {
1450N/A skip += len;
1450N/A total -= len;
1450N/A todo = total;
1450N/A } else if (ETEST(errno)) {
1450N/A N_XWaitForWritable(dpy);
1450N/A#ifdef SUNSYSV
1450N/A } else if (errno == 0) {
1450N/A N_XWaitForWritable(dpy);
1450N/A#endif
1450N/A#ifdef EMSGSIZE
1450N/A } else if (errno == EMSGSIZE) {
1450N/A if (todo > 1)
1450N/A todo >>= 1;
1450N/A else
1450N/A N_XWaitForWritable(dpy);
1450N/A#endif
1450N/A } else if (errno != EINTR) {
1450N/A _XIOError(dpy);
1450N/A }
1450N/A }
1450N/A
1450N/A dpy->bufptr = dpy->buffer;
1450N/A dpy->last_req = (char *) & _dummy_request;
1450N/A return;
1450N/A}
1450N/A
1450N/A#ifdef NEEDFORNX
1450N/A/*
1450N/A * _XAllocID - normal resource ID allocation routine. A client
1450N/A * can roll his own and instatantiate it if he wants, but must
1450N/A * follow the rules.
1450N/A */
1450N/AXID _XAllocID(Display *dpy)
1450N/A{
1450N/A XID id;
1450N/A
1450N/A id = dpy->resource_id << dpy->resource_shift;
1450N/A if (id <= dpy->resource_mask) {
1450N/A dpy->resource_id++;
1450N/A return (dpy->resource_base + id);
1450N/A }
1450N/A if (id != 0x10000000) {
1450N/A (void) fprintf(stderr,
1450N/A "Xlib: resource ID allocation space exhausted!\n");
1450N/A id = 0x10000000;
1450N/A dpy->resource_id = id >> dpy->resource_shift;
1450N/A }
1450N/A return id;
1450N/A}
1450N/A
1450N/A/*
1450N/A * The hard part about this is that we only get 16 bits from a reply. Well,
1450N/A * then, we have three values that will march along, with the following
1450N/A * invariant:
1450N/A * dpy->last_request_read <= rep->sequenceNumber <= dpy->request
1450N/A * The right choice for rep->sequenceNumber is the largest that
1450N/A * still meets these constraints.
1450N/A */
1450N/A
1450N/Aunsigned long
1450N/A_XSetLastRequestRead(Display *dpy, xGenericReply *rep)
1450N/A{
1450N/A register unsigned long newseq, lastseq;
1450N/A
1450N/A /*
1450N/A * KeymapNotify has no sequence number, but is always guaranteed
1450N/A * to immediately follow another event, except when generated via
1450N/A * SendEvent (hmmm).
1450N/A */
1450N/A if ((rep->type & 0x7f) == KeymapNotify)
1450N/A return(dpy->last_request_read);
1450N/A
1450N/A newseq = (dpy->last_request_read & ~((unsigned long)0xffff)) |
1450N/A rep->sequenceNumber;
1450N/A lastseq = dpy->last_request_read;
1450N/A while (newseq < lastseq) {
1450N/A newseq += 0x10000;
1450N/A if (newseq > dpy->request) {
1450N/A (void) fprintf (stderr,
1450N/A "Xlib: sequence lost (0x%lx > 0x%lx) in reply type 0x%x!\n",
1450N/A newseq, dpy->request,
1450N/A (unsigned int) rep->type);
1450N/A newseq -= 0x10000;
1450N/A break;
1450N/A }
1450N/A }
1450N/A
1450N/A dpy->last_request_read = newseq;
1450N/A return(newseq);
1450N/A}
1450N/A
1450N/A#endif /* NEEDFORNX */
1450N/A
1450N/A/*
1450N/A * N_XReply - Wait for a reply packet and copy its contents into the
1450N/A * specified rep. Mean while we must handle error and event packets that
1450N/A * we may encounter.
1450N/A */
1450N/AStatus N_XReply (
1450N/A Display *dpy,
1450N/A xReply *rep,
1450N/A int extra, /* number of 32-bit words expected after the reply */
1450N/A Bool discard) /* should I discard data following "extra" words? */
1450N/A{
1450N/A /* Pull out the serial number now, so that (currently illegal) requests
1450N/A * generated by an error handler don't confuse us.
1450N/A */
1450N/A unsigned long cur_request = dpy->request;
1450N/A
1450N/A if (dpy->flags & XlibDisplayIOError) return (0);
1450N/A
1450N/A N_XFlush(dpy);
1450N/A while (1) {
1450N/A N_XRead(dpy, (char *)rep, (long)SIZEOF(xReply));
1450N/A switch ((int)rep->generic.type) {
1450N/A
1450N/A case X_Reply:
1450N/A /* Reply received. Fast update for synchronous replies,
1450N/A * but deal with multiple outstanding replies.
1450N/A */
1450N/A if (rep->generic.sequenceNumber == (cur_request & 0xffff))
1450N/A dpy->last_request_read = cur_request;
1450N/A else
1450N/A (void) _XSetLastRequestRead(dpy, &rep->generic);
1450N/A if (extra == 0) {
1450N/A if (discard && (rep->generic.length > 0))
1450N/A /* unexpectedly long reply! */
1450N/A _EatData32 (dpy, rep->generic.length);
1450N/A return (1);
1450N/A }
1450N/A if ((unsigned) extra == rep->generic.length) {
1450N/A /*
1450N/A * Read the extra data into storage immediately following
1450N/A * the GenericReply structure.
1450N/A */
1450N/A N_XRead (dpy, (char *) (NEXTPTR(rep,xReply)),
1450N/A ((long)extra) << 2);
1450N/A return (1);
1450N/A }
1450N/A if ((unsigned) extra < rep->generic.length) {
1450N/A /* Actual reply is longer than "extra" */
1450N/A N_XRead (dpy, (char *) (NEXTPTR(rep,xReply)),
1450N/A ((long)extra) << 2);
1450N/A if (discard)
1450N/A _EatData32 (dpy, rep->generic.length - extra);
1450N/A return (1);
1450N/A }
1450N/A /*
1450N/A *if we get here, then extra > rep->generic.length--meaning we
1450N/A * read a reply that's shorter than we expected. This is an
1450N/A * error, but we still need to figure out how to handle it...
1450N/A */
1450N/A N_XRead (dpy, (char *) (NEXTPTR(rep,xReply)),
1450N/A ((long) rep->generic.length) << 2);
1450N/A _XIOError (dpy);
1450N/A return (0);
1450N/A
1450N/A case X_Error:
1450N/A {
1450N/A register _XExtension *ext;
1450N/A register Bool ret = False;
1450N/A int ret_code;
1450N/A xError *err = (xError *) rep;
1450N/A unsigned long serial;
1450N/A
1450N/A serial = _XSetLastRequestRead(dpy, (xGenericReply *)rep);
1450N/A /*
1450N/A * we better see if there is an extension who may
1450N/A * want to suppress the error.
1450N/A */
1450N/A for (ext = dpy->ext_procs; !ret && ext; ext = ext->next) {
1450N/A if (ext->error)
1450N/A ret = (*ext->error)(dpy, err, &ext->codes, &ret_code);
1450N/A }
1450N/A if (!ret) {
1450N/A _XError(dpy, err);
1450N/A ret_code = 0;
1450N/A }
1450N/A if (serial == cur_request)
1450N/A return(ret_code);
1450N/A }
1450N/A break;
1450N/A default:
1450N/A /* There should never be any events on this connection! */
1450N/A DPSFatalProc(NULL, "N_XReply read bogus X event");
1450N/A break;
1450N/A }
1450N/A }
1450N/A}
1450N/A
1450N/A
1450N/A/* Read and discard "n" 8-bit bytes of data */
1450N/A
1450N/Astatic void
1450N/AN_XEatData (Display *dpy, unsigned long n)
1450N/A{
1450N/A#define SCRATCHSIZE 2048
1450N/A char buf[SCRATCHSIZE];
1450N/A
1450N/A while (n > 0) {
1450N/A register long bytes_read = (n > SCRATCHSIZE) ? SCRATCHSIZE : n;
1450N/A N_XRead (dpy, buf, bytes_read);
1450N/A n -= bytes_read;
1450N/A }
1450N/A#undef SCRATCHSIZE
1450N/A}
1450N/A
1450N/A
1450N/A/* Read and discard "n" 32-bit words. */
1450N/A
1450N/Astatic void _EatData32 (Display *dpy, unsigned long n)
1450N/A{
1450N/A N_XEatData (dpy, n << 2);
1450N/A}
1450N/A
1450N/A
1450N/A#ifdef NEEDFORNX
1450N/A/*
1450N/A * _XEnq - Place event packets on the display's queue.
1450N/A * note that no squishing of move events in V11, since there
1450N/A * is pointer motion hints....
1450N/A */
1450N/Avoid _XEnq (Display *dpy, xEvent *event)
1450N/A{
1450N/A register _XQEvent *qelt;
1450N/A
1450N/A/*NOSTRICT*/
1450N/A if (qelt = _qfree) {
1450N/A /* If _qfree is non-NULL do this, else malloc a new one. */
1450N/A _qfree = qelt->next;
1450N/A }
1450N/A else if ((qelt =
1450N/A (_XQEvent *) Xmalloc((unsigned)sizeof(_XQEvent))) == NULL) {
1450N/A /* Malloc call failed! */
1450N/A errno = ENOMEM;
1450N/A _XIOError(dpy);
1450N/A }
1450N/A qelt->next = NULL;
1450N/A /* go call through display to find proper event reformatter */
1450N/A if ((*dpy->event_vec[event->u.u.type & 0177])(dpy, &qelt->event, event)) {
1450N/A if (dpy->tail) dpy->tail->next = qelt;
1450N/A else dpy->head = qelt;
1450N/A
1450N/A dpy->tail = qelt;
1450N/A dpy->qlen++;
1450N/A } else {
1450N/A /* ignored, or stashed away for many-to-one compression */
1450N/A qelt->next = _qfree;
1450N/A _qfree = qelt;
1450N/A }
1450N/A}
1450N/A/*
1450N/A * EventToWire in separate file in that often not needed.
1450N/A */
1450N/A#endif /* NEEDFORNX */
1450N/A
1450N/A/*ARGSUSED*/
1450N/ABool
1450N/AN_XUnknownWireEvent(
1450N/A Display *dpy, /* pointer to display structure */
1450N/A XEvent *re, /* pointer to where event should be reformatted */
1450N/A xEvent *event) /* wire protocol event */
1450N/A{
1450N/A char mbuf[256];
1450N/A
1450N/A sprintf(mbuf, "NX: unhandled wire event %d, agent = %lx", re->type, (long)dpy);
1450N/A DPSWarnProc(NULL, mbuf);
1450N/A return(False);
1450N/A}
1450N/A
1450N/A/*ARGSUSED*/
1450N/AStatus
1450N/AN_XUnknownNativeEvent(
1450N/A Display *dpy, /* pointer to display structure */
1450N/A XEvent *re, /* pointer to where event should be reformatted */
1450N/A xEvent *event) /* wire protocol event */
1450N/A{
1450N/A char mbuf[256];
1450N/A
1450N/A sprintf(mbuf, "NX: unhandled native event %d, agent = %lx", re->type, (long)dpy);
1450N/A DPSWarnProc(NULL, mbuf);
1450N/A return(0);
1450N/A}
1450N/A
1450N/A#ifdef NEEDFORNX
1450N/A/*
1450N/A * reformat a wire event into an XEvent structure of the right type.
1450N/A */
1450N/ABool
1450N/A_XWireToEvent(
1450N/A Display *dpy, /* pointer to display structure */
1450N/A XEvent *re, /* pointer to where event should be reformatted */
1450N/A xEvent *event) /* wire protocol event */
1450N/A{
1450N/A
1450N/A re->type = event->u.u.type & 0x7f;
1450N/A ((XAnyEvent *)re)->serial = _XSetLastRequestRead(dpy,
1450N/A (xGenericReply *)event);
1450N/A ((XAnyEvent *)re)->send_event = ((event->u.u.type & 0x80) != 0);
1450N/A ((XAnyEvent *)re)->display = dpy;
1450N/A
1450N/A /* Ignore the leading bit of the event type since it is set when a
1450N/A client sends an event rather than the server. */
1450N/A
1450N/A switch (event-> u.u.type & 0177) {
1450N/A case KeyPress:
1450N/A case KeyRelease:
1450N/A {
1450N/A register XKeyEvent *ev = (XKeyEvent*) re;
1450N/A ev->root = event->u.keyButtonPointer.root;
1450N/A ev->window = event->u.keyButtonPointer.event;
1450N/A ev->subwindow = event->u.keyButtonPointer.child;
1450N/A ev->time = event->u.keyButtonPointer.time;
1450N/A ev->x = cvtINT16toInt(event->u.keyButtonPointer.eventX);
1450N/A ev->y = cvtINT16toInt(event->u.keyButtonPointer.eventY);
1450N/A ev->x_root = cvtINT16toInt(event->u.keyButtonPointer.rootX);
1450N/A ev->y_root = cvtINT16toInt(event->u.keyButtonPointer.rootY);
1450N/A ev->state = event->u.keyButtonPointer.state;
1450N/A ev->same_screen = event->u.keyButtonPointer.sameScreen;
1450N/A ev->keycode = event->u.u.detail;
1450N/A }
1450N/A break;
1450N/A case ButtonPress:
1450N/A case ButtonRelease:
1450N/A {
1450N/A register XButtonEvent *ev = (XButtonEvent *) re;
1450N/A ev->root = event->u.keyButtonPointer.root;
1450N/A ev->window = event->u.keyButtonPointer.event;
1450N/A ev->subwindow = event->u.keyButtonPointer.child;
1450N/A ev->time = event->u.keyButtonPointer.time;
1450N/A ev->x = cvtINT16toInt(event->u.keyButtonPointer.eventX);
1450N/A ev->y = cvtINT16toInt(event->u.keyButtonPointer.eventY);
1450N/A ev->x_root = cvtINT16toInt(event->u.keyButtonPointer.rootX);
1450N/A ev->y_root = cvtINT16toInt(event->u.keyButtonPointer.rootY);
1450N/A ev->state = event->u.keyButtonPointer.state;
1450N/A ev->same_screen = event->u.keyButtonPointer.sameScreen;
1450N/A ev->button = event->u.u.detail;
1450N/A }
1450N/A break;
1450N/A case MotionNotify:
1450N/A {
1450N/A register XMotionEvent *ev = (XMotionEvent *)re;
1450N/A ev->root = event->u.keyButtonPointer.root;
1450N/A ev->window = event->u.keyButtonPointer.event;
1450N/A ev->subwindow = event->u.keyButtonPointer.child;
1450N/A ev->time = event->u.keyButtonPointer.time;
1450N/A ev->x = cvtINT16toInt(event->u.keyButtonPointer.eventX);
1450N/A ev->y = cvtINT16toInt(event->u.keyButtonPointer.eventY);
1450N/A ev->x_root = cvtINT16toInt(event->u.keyButtonPointer.rootX);
1450N/A ev->y_root = cvtINT16toInt(event->u.keyButtonPointer.rootY);
1450N/A ev->state = event->u.keyButtonPointer.state;
1450N/A ev->same_screen = event->u.keyButtonPointer.sameScreen;
1450N/A ev->is_hint = event->u.u.detail;
1450N/A }
1450N/A break;
1450N/A case EnterNotify:
1450N/A case LeaveNotify:
1450N/A {
1450N/A register XCrossingEvent *ev = (XCrossingEvent *) re;
ev->root = event->u.enterLeave.root;
ev->window = event->u.enterLeave.event;
ev->subwindow = event->u.enterLeave.child;
ev->time = event->u.enterLeave.time;
ev->x = cvtINT16toInt(event->u.enterLeave.eventX);
ev->y = cvtINT16toInt(event->u.enterLeave.eventY);
ev->x_root = cvtINT16toInt(event->u.enterLeave.rootX);
ev->y_root = cvtINT16toInt(event->u.enterLeave.rootY);
ev->state = event->u.enterLeave.state;
ev->mode = event->u.enterLeave.mode;
ev->same_screen = (event->u.enterLeave.flags &
ELFlagSameScreen) && True;
ev->focus = (event->u.enterLeave.flags &
ELFlagFocus) && True;
ev->detail = event->u.u.detail;
}
break;
case FocusIn:
case FocusOut:
{
register XFocusChangeEvent *ev = (XFocusChangeEvent *) re;
ev->window = event->u.focus.window;
ev->mode = event->u.focus.mode;
ev->detail = event->u.u.detail;
}
break;
case KeymapNotify:
{
register XKeymapEvent *ev = (XKeymapEvent *) re;
ev->window = dpy->current;
bcopy ((char *)((xKeymapEvent *) event)->map,
&ev->key_vector[1],
sizeof (((xKeymapEvent *) event)->map));
}
break;
case Expose:
{
register XExposeEvent *ev = (XExposeEvent *) re;
ev->window = event->u.expose.window;
ev->x = event->u.expose.x;
ev->y = event->u.expose.y;
ev->width = event->u.expose.width;
ev->height = event->u.expose.height;
ev->count = event->u.expose.count;
}
break;
case GraphicsExpose:
{
register XGraphicsExposeEvent *ev =
(XGraphicsExposeEvent *) re;
ev->drawable = event->u.graphicsExposure.drawable;
ev->x = event->u.graphicsExposure.x;
ev->y = event->u.graphicsExposure.y;
ev->width = event->u.graphicsExposure.width;
ev->height = event->u.graphicsExposure.height;
ev->count = event->u.graphicsExposure.count;
ev->major_code = event->u.graphicsExposure.majorEvent;
ev->minor_code = event->u.graphicsExposure.minorEvent;
}
break;
case NoExpose:
{
register XNoExposeEvent *ev = (XNoExposeEvent *) re;
ev->drawable = event->u.noExposure.drawable;
ev->major_code = event->u.noExposure.majorEvent;
ev->minor_code = event->u.noExposure.minorEvent;
}
break;
case VisibilityNotify:
{
register XVisibilityEvent *ev = (XVisibilityEvent *) re;
ev->window = event->u.visibility.window;
ev->state = event->u.visibility.state;
}
break;
case CreateNotify:
{
register XCreateWindowEvent *ev =
(XCreateWindowEvent *) re;
ev->window = event->u.createNotify.window;
ev->parent = event->u.createNotify.parent;
ev->x = cvtINT16toInt(event->u.createNotify.x);
ev->y = cvtINT16toInt(event->u.createNotify.y);
ev->width = event->u.createNotify.width;
ev->height = event->u.createNotify.height;
ev->border_width = event->u.createNotify.borderWidth;
ev->override_redirect = event->u.createNotify.override;
}
break;
case DestroyNotify:
{
register XDestroyWindowEvent *ev =
(XDestroyWindowEvent *) re;
ev->window = event->u.destroyNotify.window;
ev->event = event->u.destroyNotify.event;
}
break;
case UnmapNotify:
{
register XUnmapEvent *ev = (XUnmapEvent *) re;
ev->window = event->u.unmapNotify.window;
ev->event = event->u.unmapNotify.event;
ev->from_configure = event->u.unmapNotify.fromConfigure;
}
break;
case MapNotify:
{
register XMapEvent *ev = (XMapEvent *) re;
ev->window = event->u.mapNotify.window;
ev->event = event->u.mapNotify.event;
ev->override_redirect = event->u.mapNotify.override;
}
break;
case MapRequest:
{
register XMapRequestEvent *ev = (XMapRequestEvent *) re;
ev->window = event->u.mapRequest.window;
ev->parent = event->u.mapRequest.parent;
}
break;
case ReparentNotify:
{
register XReparentEvent *ev = (XReparentEvent *) re;
ev->event = event->u.reparent.event;
ev->window = event->u.reparent.window;
ev->parent = event->u.reparent.parent;
ev->x = cvtINT16toInt(event->u.reparent.x);
ev->y = cvtINT16toInt(event->u.reparent.y);
ev->override_redirect = event->u.reparent.override;
}
break;
case ConfigureNotify:
{
register XConfigureEvent *ev = (XConfigureEvent *) re;
ev->event = event->u.configureNotify.event;
ev->window = event->u.configureNotify.window;
ev->above = event->u.configureNotify.aboveSibling;
ev->x = cvtINT16toInt(event->u.configureNotify.x);
ev->y = cvtINT16toInt(event->u.configureNotify.y);
ev->width = event->u.configureNotify.width;
ev->height = event->u.configureNotify.height;
ev->border_width = event->u.configureNotify.borderWidth;
ev->override_redirect = event->u.configureNotify.override;
}
break;
case ConfigureRequest:
{
register XConfigureRequestEvent *ev =
(XConfigureRequestEvent *) re;
ev->window = event->u.configureRequest.window;
ev->parent = event->u.configureRequest.parent;
ev->above = event->u.configureRequest.sibling;
ev->x = cvtINT16toInt(event->u.configureRequest.x);
ev->y = cvtINT16toInt(event->u.configureRequest.y);
ev->width = event->u.configureRequest.width;
ev->height = event->u.configureRequest.height;
ev->border_width = event->u.configureRequest.borderWidth;
ev->value_mask = event->u.configureRequest.valueMask;
ev->detail = event->u.u.detail;
}
break;
case GravityNotify:
{
register XGravityEvent *ev = (XGravityEvent *) re;
ev->window = event->u.gravity.window;
ev->event = event->u.gravity.event;
ev->x = cvtINT16toInt(event->u.gravity.x);
ev->y = cvtINT16toInt(event->u.gravity.y);
}
break;
case ResizeRequest:
{
register XResizeRequestEvent *ev =
(XResizeRequestEvent *) re;
ev->window = event->u.resizeRequest.window;
ev->width = event->u.resizeRequest.width;
ev->height = event->u.resizeRequest.height;
}
break;
case CirculateNotify:
{
register XCirculateEvent *ev = (XCirculateEvent *) re;
ev->window = event->u.circulate.window;
ev->event = event->u.circulate.event;
ev->place = event->u.circulate.place;
}
break;
case CirculateRequest:
{
register XCirculateRequestEvent *ev =
(XCirculateRequestEvent *) re;
ev->window = event->u.circulate.window;
ev->parent = event->u.circulate.event;
ev->place = event->u.circulate.place;
}
break;
case PropertyNotify:
{
register XPropertyEvent *ev = (XPropertyEvent *) re;
ev->window = event->u.property.window;
ev->atom = event->u.property.atom;
ev->time = event->u.property.time;
ev->state = event->u.property.state;
}
break;
case SelectionClear:
{
register XSelectionClearEvent *ev =
(XSelectionClearEvent *) re;
ev->window = event->u.selectionClear.window;
ev->selection = event->u.selectionClear.atom;
ev->time = event->u.selectionClear.time;
}
break;
case SelectionRequest:
{
register XSelectionRequestEvent *ev =
(XSelectionRequestEvent *) re;
ev->owner = event->u.selectionRequest.owner;
ev->requestor = event->u.selectionRequest.requestor;
ev->selection = event->u.selectionRequest.selection;
ev->target = event->u.selectionRequest.target;
ev->property = event->u.selectionRequest.property;
ev->time = event->u.selectionRequest.time;
}
break;
case SelectionNotify:
{
register XSelectionEvent *ev = (XSelectionEvent *) re;
ev->requestor = event->u.selectionNotify.requestor;
ev->selection = event->u.selectionNotify.selection;
ev->target = event->u.selectionNotify.target;
ev->property = event->u.selectionNotify.property;
ev->time = event->u.selectionNotify.time;
}
break;
case ColormapNotify:
{
register XColormapEvent *ev = (XColormapEvent *) re;
ev->window = event->u.colormap.window;
ev->colormap = event->u.colormap.colormap;
ev->new = event->u.colormap.new;
ev->state = event->u.colormap.state;
}
break;
case ClientMessage:
{
register int i;
register XClientMessageEvent *ev
= (XClientMessageEvent *) re;
ev->window = event->u.clientMessage.window;
ev->format = event->u.u.detail;
switch (ev->format) {
case 8:
ev->message_type = event->u.clientMessage.u.b.type;
for (i = 0; i < 20; i++)
ev->data.b[i] = event->u.clientMessage.u.b.bytes[i];
break;
case 16:
ev->message_type = event->u.clientMessage.u.s.type;
ev->data.s[0] = cvtINT16toShort(event->u.clientMessage.u.s.shorts0);
ev->data.s[1] = cvtINT16toShort(event->u.clientMessage.u.s.shorts1);
ev->data.s[2] = cvtINT16toShort(event->u.clientMessage.u.s.shorts2);
ev->data.s[3] = cvtINT16toShort(event->u.clientMessage.u.s.shorts3);
ev->data.s[4] = cvtINT16toShort(event->u.clientMessage.u.s.shorts4);
ev->data.s[5] = cvtINT16toShort(event->u.clientMessage.u.s.shorts5);
ev->data.s[6] = cvtINT16toShort(event->u.clientMessage.u.s.shorts6);
ev->data.s[7] = cvtINT16toShort(event->u.clientMessage.u.s.shorts7);
ev->data.s[8] = cvtINT16toShort(event->u.clientMessage.u.s.shorts8);
ev->data.s[9] = cvtINT16toShort(event->u.clientMessage.u.s.shorts9);
break;
case 32:
ev->message_type = event->u.clientMessage.u.l.type;
ev->data.l[0] = cvtINT32toLong(event->u.clientMessage.u.l.longs0);
ev->data.l[1] = cvtINT32toLong(event->u.clientMessage.u.l.longs1);
ev->data.l[2] = cvtINT32toLong(event->u.clientMessage.u.l.longs2);
ev->data.l[3] = cvtINT32toLong(event->u.clientMessage.u.l.longs3);
ev->data.l[4] = cvtINT32toLong(event->u.clientMessage.u.l.longs4);
break;
default: /* XXX should never occur */
break;
}
}
break;
case MappingNotify:
{
register XMappingEvent *ev = (XMappingEvent *)re;
ev->window = 0;
ev->first_keycode = event->u.mappingNotify.firstKeyCode;
ev->request = event->u.mappingNotify.request;
ev->count = event->u.mappingNotify.count;
}
break;
default:
return(_XUnknownWireEvent(dpy, re, event));
}
return(True);
}
#ifndef USL_SHARELIB
static char *_SysErrorMsg (int n)
{
extern char *sys_errlist[];
extern int sys_nerr;
char *s = ((n >= 0 && n < sys_nerr) ? sys_errlist[n] : "unknown error");
return (s ? s : "no such error");
}
#endif /* USL sharedlibs in don't define for SVR3.2 */
/*
* _XDefaultIOError - Default fatal system error reporting routine. Called
* when an X internal system error is encountered.
*/
_XDefaultIOError (Display *dpy)
{
(void) fprintf (stderr,
"XIO: fatal IO error %d (%s) on X server \"%s\"\r\n",
errno, _SysErrorMsg (errno), DisplayString (dpy));
(void) fprintf (stderr,
" after %lu requests (%lu known processed) with %d events remaining.\r\n",
NextRequest(dpy) - 1, LastKnownRequestProcessed(dpy),
QLength(dpy));
if (errno == EPIPE) {
(void) fprintf (stderr,
" The connection was probably broken by a server shutdown or KillClient.\r\n");
}
exit(1);
}
static int _XPrintDefaultError (Display *dpy, XErrorEvent *event, FILE *fp)
{
char buffer[BUFSIZ];
char mesg[BUFSIZ];
char number[32];
char *mtype = "XlibMessage";
register _XExtension *ext = (_XExtension *)NULL;
_XExtension *bext = (_XExtension *)NULL;
XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
(void) fprintf(fp, "%s: %s\n ", mesg, buffer);
XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->request_code);
if (event->request_code < 128) {
sprintf(number, "%d", event->request_code);
XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
} else {
for (ext = dpy->ext_procs;
ext && (ext->codes.major_opcode != event->request_code);
ext = ext->next)
;
if (ext)
strcpy(buffer, ext->name);
else
buffer[0] = '\0';
}
(void) fprintf(fp, " (%s)\n", buffer);
if (event->request_code >= 128) {
XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->minor_code);
if (ext) {
sprintf(mesg, "%s.%d", ext->name, event->minor_code);
XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
(void) fprintf(fp, " (%s)", buffer);
}
fputs("\n", fp);
}
if (event->error_code >= 128) {
/* kludge, try to find the extension that caused it */
buffer[0] = '\0';
for (ext = dpy->ext_procs; ext; ext = ext->next) {
if (ext->error_string)
(*ext->error_string)(dpy, event->error_code, &ext->codes,
buffer, BUFSIZ);
if (buffer[0]) {
bext = ext;
break;
}
if (ext->codes.first_error &&
ext->codes.first_error < event->error_code &&
(!bext || ext->codes.first_error > bext->codes.first_error))
bext = ext;
}
if (bext)
sprintf(buffer, "%s.%d", bext->name,
event->error_code - bext->codes.first_error);
else
strcpy(buffer, "Value");
XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
if (mesg[0]) {
fputs(" ", fp);
(void) fprintf(fp, mesg, event->resourceid);
fputs("\n", fp);
}
/* let extensions try to print the values */
for (ext = dpy->ext_procs; ext; ext = ext->next) {
if (ext->error_values)
(*ext->error_values)(dpy, event, fp);
}
} else if ((event->error_code == BadWindow) ||
(event->error_code == BadPixmap) ||
(event->error_code == BadCursor) ||
(event->error_code == BadFont) ||
(event->error_code == BadDrawable) ||
(event->error_code == BadColor) ||
(event->error_code == BadGC) ||
(event->error_code == BadIDChoice) ||
(event->error_code == BadValue) ||
(event->error_code == BadAtom)) {
if (event->error_code == BadValue)
XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
mesg, BUFSIZ);
else if (event->error_code == BadAtom)
XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
mesg, BUFSIZ);
else
XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->resourceid);
fputs("\n", fp);
}
XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->serial);
XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
mesg, BUFSIZ);
fputs("\n ", fp);
(void) fprintf(fp, mesg, dpy->request);
fputs("\n", fp);
if (event->error_code == BadImplementation) return 0;
return 1;
}
int _XDefaultError(Display *dpy, XErrorEvent *event)
{
if (_XPrintDefaultError (dpy, event, stderr) == 0) return 0;
exit(1);
/*NOTREACHED*/
}
/*ARGSUSED*/
Bool _XDefaultWireError(Display *display, XErrorEvent *he, xError *we)
{
return True;
}
/*
* _XError - prepare to upcall user protocol error handler
*/
int _XError (Display *dpy, xError *rep)
{
/*
* X_Error packet encountered! We need to unpack the error before
* giving it to the user.
*/
XEvent event; /* make it a large event */
event.xerror.display = dpy;
event.xerror.type = X_Error;
event.xerror.serial = _XSetLastRequestRead(dpy, (xGenericReply *)rep);
event.xerror.resourceid = rep->resourceID;
event.xerror.error_code = rep->errorCode;
event.xerror.request_code = rep->majorCode;
event.xerror.minor_code = rep->minorCode;
if (dpy->error_vec &&
!(*dpy->error_vec[rep->errorCode])(dpy, &event.xerror, rep))
return 0;
if (_XErrorFunction != NULL) {
return ((*_XErrorFunction)(dpy, &event)); /* upcall */
} else {
return _XDefaultError(dpy, &event);
}
}
/*
* _XIOError - call user connection error handler and exit
*/
int _XIOError (Display *dpy)
{
dpy->flags |= XlibDisplayIOError;
if (_XIOErrorFunction != NULL)
(*_XIOErrorFunction)(dpy);
else
_XDefaultIOError(dpy);
exit (1);
}
/*
* This routine can be used to (cheaply) get some memory within a single
* Xlib routine for scratch space. It is reallocated from the same place
* each time, unless the library needs a large scratch space.
*/
char *_XAllocScratch (Display *dpy, unsigned long nbytes)
{
if (nbytes > dpy->scratch_length) {
if (dpy->scratch_buffer) Xfree (dpy->scratch_buffer);
if (dpy->scratch_buffer = Xmalloc((unsigned) nbytes))
dpy->scratch_length = nbytes;
else dpy->scratch_length = 0;
}
return (dpy->scratch_buffer);
}
/*
* Given a visual id, find the visual structure for this id on this display.
*/
Visual *_XVIDtoVisual (Display *dpy, VisualID id)
{
register int i, j, k;
register Screen *sp;
register Depth *dp;
register Visual *vp;
for (i = 0; i < dpy->nscreens; i++) {
sp = &dpy->screens[i];
for (j = 0; j < sp->ndepths; j++) {
dp = &sp->depths[j];
/* if nvisuals == 0 then visuals will be NULL */
for (k = 0; k < dp->nvisuals; k++) {
vp = &dp->visuals[k];
if (vp->visualid == id) return (vp);
}
}
}
return (NULL);
}
void XFree (void *data)
{
Xfree (data);
}
#ifdef _XNEEDBCOPYFUNC
void _Xbcopy(char *b1, char *b2, length)
{
if (b1 < b2) {
b2 += length;
b1 += length;
while (length--)
*--b2 = *--b1;
} else {
while (length--)
*b2++ = *b1++;
}
}
#endif
#endif /* NEEDFORNX */
void NXProcData (Display *dpy, char *data, long len)
{
if (dpy->bufptr + (len) <= dpy->bufmax) {
bcopy(data, dpy->bufptr, (int)len);
dpy->bufptr += ((len) + 3) & ~3;
} else {
N_XSend(dpy, data, len);
}
}
#ifdef WORD64
/*
* XXX This is a *really* stupid way of doing this. It should just use
* dpy->bufptr directly, taking into account where in the word it is.
*/
/*
* Data16 - Place 16 bit data in the buffer.
*
* "dpy" is a pointer to a Display.
* "data" is a pointer to the data.
* "len" is the length in bytes of the data.
*/
static void
doData16(Display *dpy, short *data, unsigned len, char *packbuffer)
{
long *lp,*lpack;
long i, nwords,bits;
long mask16 = 0x000000000000ffff;
lp = (long *)data;
lpack = (long *)packbuffer;
/* nwords is the number of 16 bit values to be packed,
* the low order 16 bits of each word will be packed
* into 64 bit words
*/
nwords = len >> 1;
bits = 48;
for(i=0;i<nwords;i++){
if (bits == 48) *lpack = 0;
*lpack ^= (*lp & mask16) << bits;
bits -= 16 ;
lp++;
if(bits < 0){
lpack++;
bits = 48;
}
}
Data(dpy, packbuffer, len);
}
void
Data16 (Display *dpy, short *data, unsigned len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nunits = PACKBUFFERSIZE >> 1;
for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {
doData16 (dpy, data, PACKBUFFERSIZE, packbuffer);
}
if (len) doData16 (dpy, data, len, packbuffer);
}
/*
* Data32 - Place 32 bit data in the buffer.
*
* "dpy" is a pointer to a Display.
* "data" is a pointer to the data.
* "len" is the length in bytes of the data.
*/
static doData32 (Display *dpy, long *data, unsigned len, char *packbuffer)
{
long *lp,*lpack;
long i,bits,nwords;
long mask32 = 0x00000000ffffffff;
lpack = (long *) packbuffer;
lp = data;
/* nwords is the number of 32 bit values to be packed
* the low order 32 bits of each word will be packed
* into 64 bit words
*/
nwords = len >> 2;
bits = 32;
for(i=0;i<nwords;i++){
if (bits == 32) *lpack = 0;
*lpack ^= (*lp & mask32) << bits;
bits = bits ^32;
lp++;
if(bits)
lpack++;
}
Data(dpy, packbuffer, len);
}
Data32 (Display *dpy, long *data, unsigned len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nunits = PACKBUFFERSIZE >> 2;
for (; len > PACKBUFFERSIZE; len -= PACKBUFFERSIZE, data += nunits) {
doData32 (dpy, data, PACKBUFFERSIZE, packbuffer);
}
if (len) doData32 (dpy, data, len, packbuffer);
}
#endif /* WORD64 */
#ifdef NEEDFORNX
/*
* _XFreeQ - free the queue of events, called by XCloseDisplay
*/
void _XFreeQ (void)
{
register _XQEvent *qelt = _qfree;
while (qelt) {
register _XQEvent *qnxt = qelt->next;
Xfree ((char *) qelt);
qelt = qnxt;
}
_qfree = NULL;
return;
}
#endif /* NEEDFORNX */
/* Make sure this produces the same string as DefineLocal/DefineSelf in xdm.
* Otherwise, Xau will not be able to find your cookies in the Xauthority file.
*
* Note: POSIX says that the ``nodename'' member of utsname does _not_ have
* to have sufficient information for interfacing to the network,
* and so, you may be better off using gethostname (if it exists).
*/
#if (defined(_POSIX_SOURCE) && !defined(AIXV3)) || defined(hpux) || defined(USG) || defined(SVR4)
#define NEED_UTSNAME
#include <sys/utsname.h>
#endif
/*
* N_XGetHostname - similar to gethostname but allows special processing.
*/
int N_XGetHostname (char *buf, int maxlen)
{
int len;
#ifdef NEED_UTSNAME
struct utsname name;
uname (&name);
len = strlen (name.nodename);
if (len >= maxlen) len = maxlen - 1;
strncpy (buf, name.nodename, len);
buf[len] = '\0';
#else
buf[0] = '\0';
(void) gethostname (buf, maxlen);
buf [maxlen - 1] = '\0';
len = strlen(buf);
#endif /* NEED_UTSNAME */
return len;
}
#ifdef NEEDFORNX
/*
* _XScreenOfWindow - get the Screen of a given window
*/
Screen *_XScreenOfWindow (Display *dpy, Window w)
{
register int i;
Window root;
int x, y; /* dummy variables */
unsigned int width, height, bw, depth; /* dummy variables */
if (XGetGeometry (dpy, w, &root, &x, &y, &width, &height,
&bw, &depth) == False) {
return None;
}
for (i = 0; i < ScreenCount (dpy); i++) { /* find root from list */
if (root == RootWindow (dpy, i)) {
return ScreenOfDisplay (dpy, i);
}
}
return NULL;
}
#endif /* NEEDFORNX */
#if (MSKCNT > 4)
/*
* This is a macro if MSKCNT <= 4
*/
int
N_XANYSET(unsigned long *src)
{
int i;
for (i=0; i<MSKCNT; i++)
if (src[ i ])
return (1);
return (0);
}
#endif
#ifdef NEEDFORNX
#ifdef CRAY
/*
* Cray UniCOS does not have readv and writev so we emulate
*/
#include <sys/socket.h>
int _XReadV (int fd, struct iovec *iov, int iovcnt)
{
struct msghdr hdr;
hdr.msg_iov = iov;
hdr.msg_iovlen = iovcnt;
hdr.msg_accrights = 0;
hdr.msg_accrightslen = 0;
hdr.msg_name = 0;
hdr.msg_namelen = 0;
return (recvmsg (fd, &hdr, 0));
}
int _XWriteV (int fd, struct iovec *iov, int iovcnt)
{
struct msghdr hdr;
hdr.msg_iov = iov;
hdr.msg_iovlen = iovcnt;
hdr.msg_accrights = 0;
hdr.msg_accrightslen = 0;
hdr.msg_name = 0;
hdr.msg_namelen = 0;
return (sendmsg (fd, &hdr, 0));
}
#endif /* CRAY */
#if defined(SYSV) && defined(i386) && !defined(STREAMSCONN)
/*
* SYSV/386 does not have readv so we emulate
*/
#include <sys/uio.h>
int _XReadV (int fd, struct iovec *iov, int iovcnt)
{
int i, len, total;
char *base;
errno = 0;
for (i=0, total=0; i<iovcnt; i++, iov++) {
len = iov->iov_len;
base = iov->iov_base;
while (len > 0) {
register int nbytes;
nbytes = read(fd, base, len);
if (nbytes < 0 && total == 0) return -1;
if (nbytes <= 0) return total;
errno = 0;
len -= nbytes;
total += nbytes;
base += nbytes;
}
}
return total;
}
#endif /* SYSV && i386 && !STREAMSCONN */
#ifdef STREAMSCONN
/*
* Copyright 1988, 1989 AT&T, Inc.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of AT&T not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission. AT&T makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL AT&T
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/*
iovec.c (C source file)
Acc: 575557389 Mon Mar 28 08:03:09 1988
Mod: 575557397 Mon Mar 28 08:03:17 1988
Sta: 575557397 Mon Mar 28 08:03:17 1988
Owner: 2011
Group: 1985
Permissions: 664
*/
/*
START USER STAMP AREA
*/
/*
END USER STAMP AREA
*/
extern char _XsTypeofStream[];
extern Xstream _XsStream[];
#define MAX_WORKAREA 4096
static char workarea[MAX_WORKAREA];
int
_XReadV (int fd, struct iovec v[], int n)
{
int i, rc, len, size = 0;
char * buf = workarea;
char * p;
if (n <= 0 || n > 16)
{
errno = EINVAL;
return (-1);
}
for (i = 0; i < n; ++i)
{
if ((len = v[i].iov_len) < 0 || v[i].iov_base == NULL)
{
errno = EINVAL;
return (-1);
}
size += len;
}
if ((size > MAX_WORKAREA) && ((buf = malloc (size)) == NULL))
{
errno = EINVAL;
return (-1);
}
if((rc = (*_XsStream[_XsTypeOfStream[fd]].ReadFromStream)(fd, buf, size,
BUFFERING))> 0)
{
for (i = 0, p = buf; i < n; ++i)
{
memcpy (v[i].iov_base, p, len = v[i].iov_len);
p += len;
}
}
if (size > MAX_WORKAREA)
free (buf);
return (rc);
}
int
_XWriteV (int fd, struct iovec v[], int n)
{
int i, rc, len, size = 0;
char * buf = workarea;
char * p;
if (n <= 0 || n > 16)
{
errno = EINVAL;
return (-1);
}
for (i = 0; i < n; ++i)
{
if ((len = v[i].iov_len) < 0 || v[i].iov_base == NULL)
{
errno = EINVAL;
return (-1);
}
size += len;
}
if ((size > MAX_WORKAREA) && ((buf = malloc (size)) == NULL))
{
errno = EINVAL;
return (-1);
}
for (i = 0, p = buf; i < n; ++i)
{
memcpy (p, v[i].iov_base, len = v[i].iov_len);
p += len;
}
rc = (*_XsStream[_XsTypeOfStream[fd]].WriteToStream)(fd, buf, size);
if (size > MAX_WORKAREA)
free (buf);
return (rc);
}
#endif /* STREAMSCONN */
#endif /* NEEDFORNX */