/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

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 names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL 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.

******************************************************************/
/* $XConsortium: dix.h,v 1.56 89/08/20 12:14:05 rws Exp $ */

#ifndef DIX_H
#define DIX_H

#include "gc.h"
#include "window.h"
#include "funcs.h"

#define EARLIER -1
#define SAMETIME 0
#define LATER 1

#define NullClient ((ClientPtr) 0)
#define REQUEST(type) \
	type *stuff = (type *)client->requestBuffer


#define REQUEST_SIZE_MATCH(req)\
    if ((sizeof(req) >> 2) != stuff->length)\
         return(BadLength)

#define REQUEST_AT_LEAST_SIZE(req) \
    if ((sizeof(req) >> 2) > stuff->length )\
         return(BadLength)

#define REQUEST_FIXED_SIZE(req, n)\
    if (((sizeof(req) >> 2) > stuff->length) || \
        (((sizeof(req) + (n) + 3) >> 2) != stuff->length)) \
         return(BadLength)

#define LEGAL_NEW_RESOURCE(id,client)\
    if (!LegalNewID(id,client)) \
    {\
	client->errorValue = id;\
        return(BadIDChoice);\
    }

#define LOOKUP_DRAWABLE(did, client)\
    ((client->lastDrawableID == did) ? \
     client->lastDrawable : LookupDrawable(did, client))

#define VERIFY_GC(pGC, rid, client)\
    if (client->lastGCID == rid)\
        pGC = client->lastGC;\
    else\
	pGC = (GC *)LookupIDByType(rid, RT_GC);\
    if (!pGC)\
    {\
	client->errorValue = rid;\
	return (BadGC);\
    }

#define VALIDATE_DRAWABLE_AND_GC(drawID, pDraw, pGC, client)\
    if ((client->lastDrawableID != drawID) || (client->lastGCID != stuff->gc))\
    {\
        if (client->lastDrawableID != drawID)\
    	    pDraw = (DrawablePtr)LookupIDByClass(drawID, RC_DRAWABLE);\
        else\
	    pDraw = client->lastDrawable;\
        if (client->lastGCID != stuff->gc)\
	    pGC = (GC *)LookupIDByType(stuff->gc, RT_GC);\
        else\
            pGC = client->lastGC;\
	if (pDraw && pGC)\
	{\
	    if ((pDraw->type == UNDRAWABLE_WINDOW) ||\
		(pGC->depth != pDraw->depth) ||\
		(pGC->pScreen != pDraw->pScreen))\
		return (BadMatch);\
	    client->lastDrawable = pDraw;\
	    client->lastDrawableID = drawID;\
            client->lastGC = pGC;\
            client->lastGCID = stuff->gc;\
	}\
    }\
    else\
    {\
        pGC = client->lastGC;\
        pDraw = client->lastDrawable;\
    }\
    if (!pDraw)\
    {\
        client->errorValue = drawID; \
	return (BadDrawable);\
    }\
    if (!pGC)\
    {\
        client->errorValue = stuff->gc;\
        return (BadGC);\
    }\
    if (pGC->serialNumber != pDraw->serialNumber)\
	ValidateGC(pDraw, pGC);

#define WriteReplyToClient(pClient, size, pReply) \
   if ((pClient)->swapped) \
      (*ReplySwapVector[((xReq *)(pClient)->requestBuffer)->reqType]) \
           (pClient, (int)(size), pReply); \
      else (void) WriteToClient(pClient, (int)(size), (char *)(pReply));

#define WriteSwappedDataToClient(pClient, size, pbuf) \
   if ((pClient)->swapped) \
      (*(pClient)->pSwapReplyFunc)(pClient, (int)(size), pbuf); \
   else (void) WriteToClient (pClient, (int)(size), (char *)(pbuf));

typedef struct _TimeStamp *TimeStampPtr;
typedef struct _Client _near *ClientPtr;
extern ClientPtr _near requestingClient;
extern ClientPtr _near clients[];
extern struct _Client _near _serverClient;
#define serverClient	(&_serverClient)
extern int _near currentMaxClients;

extern int _fastcall _near ProcAllowEvents(ClientPtr);
extern int _fastcall _near ProcBell(ClientPtr);
extern int _fastcall _near ProcChangeActivePointerGrab(ClientPtr);
extern int _fastcall _near ProcChangeKeyboardControl(ClientPtr);
extern int _fastcall _near ProcChangePointerControl(ClientPtr);
extern int _fastcall _near ProcGetKeyboardMapping(ClientPtr);
extern int _fastcall _near ProcGetPointerMapping(ClientPtr);
extern int _fastcall _near ProcGetInputFocus(ClientPtr);
extern int _fastcall _near ProcGetKeyboardControl(ClientPtr);
extern int _fastcall _near ProcGetMotionEvents(ClientPtr);
extern int _fastcall _near ProcGetPointerControl(ClientPtr);
extern int _fastcall _near ProcGrabButton(ClientPtr);
extern int _fastcall _near ProcGrabKey(ClientPtr);
extern int _fastcall _near ProcGrabKeyboard(ClientPtr);
extern int _fastcall _near ProcGrabPointer(ClientPtr);
extern int _fastcall _near ProcQueryKeymap(ClientPtr);
extern int _fastcall _near ProcQueryPointer(ClientPtr);
extern int _fastcall _near ProcSetInputFocus(ClientPtr);
extern int _fastcall _near ProcSetKeyboardMapping(ClientPtr);
extern int _fastcall _near ProcSetPointerMapping(ClientPtr);
extern int _fastcall _near ProcSendEvent(ClientPtr);
extern int _fastcall _near ProcUngrabButton(ClientPtr);
extern int _fastcall _near ProcUngrabKey(ClientPtr);
extern int _fastcall _near ProcUngrabKeyboard(ClientPtr);
extern int _fastcall _near ProcUngrabPointer(ClientPtr);
extern int _fastcall _near ProcWarpPointer(ClientPtr);
extern int _fastcall _near ProcRecolorCursor(ClientPtr);

extern WindowPtr LookupWindow(XID, ClientPtr);
extern struct _Drawable _near * _near LookupDrawable(XID, ClientPtr);

extern void NoopDDA();

#endif /* DIX_H */
