ICEmsg.h revision 266e564d
1266e564dSmrg/* $Xorg: ICEmsg.h,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */
2266e564dSmrg/******************************************************************************
3266e564dSmrg
4266e564dSmrg
5266e564dSmrgCopyright 1993, 1998  The Open Group
6266e564dSmrg
7266e564dSmrgPermission to use, copy, modify, distribute, and sell this software and its
8266e564dSmrgdocumentation for any purpose is hereby granted without fee, provided that
9266e564dSmrgthe above copyright notice appear in all copies and that both that
10266e564dSmrgcopyright notice and this permission notice appear in supporting
11266e564dSmrgdocumentation.
12266e564dSmrg
13266e564dSmrgThe above copyright notice and this permission notice shall be included in
14266e564dSmrgall copies or substantial portions of the Software.
15266e564dSmrg
16266e564dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17266e564dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18266e564dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19266e564dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20266e564dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21266e564dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22266e564dSmrg
23266e564dSmrgExcept as contained in this notice, the name of The Open Group shall not be
24266e564dSmrgused in advertising or otherwise to promote the sale, use or other dealings
25266e564dSmrgin this Software without prior written authorization from The Open Group.
26266e564dSmrg
27266e564dSmrgAuthor: Ralph Mor, X Consortium
28266e564dSmrg******************************************************************************/
29266e564dSmrg/* $XFree86: xc/lib/ICE/ICEmsg.h,v 1.4 2001/12/20 19:40:59 tsi Exp $ */
30266e564dSmrg
31266e564dSmrg#ifndef _ICEMSG_H_
32266e564dSmrg#define _ICEMSG_H_
33266e564dSmrg
34266e564dSmrg#include <X11/Xfuncproto.h>
35266e564dSmrg
36266e564dSmrg#include <X11/ICE/ICEconn.h>
37266e564dSmrg
38266e564dSmrg_XFUNCPROTOBEGIN
39266e564dSmrg
40266e564dSmrg/*
41266e564dSmrg * Function prototypes for internal ICElib functions
42266e564dSmrg */
43266e564dSmrg
44266e564dSmrgextern Status _IceRead (
45266e564dSmrg    IceConn		/* iceConn */,
46266e564dSmrg    unsigned long	/* nbytes */,
47266e564dSmrg    char *		/* ptr */
48266e564dSmrg);
49266e564dSmrg
50266e564dSmrgextern void _IceReadSkip (
51266e564dSmrg    IceConn		/* iceConn */,
52266e564dSmrg    unsigned long	/* nbytes */
53266e564dSmrg);
54266e564dSmrg
55266e564dSmrgextern void _IceWrite (
56266e564dSmrg    IceConn		/* iceConn */,
57266e564dSmrg    unsigned long	/* nbytes */,
58266e564dSmrg    char *		/* ptr */
59266e564dSmrg);
60266e564dSmrg
61266e564dSmrg
62266e564dSmrgextern void _IceErrorBadMinor (
63266e564dSmrg    IceConn		/* iceConn */,
64266e564dSmrg    int			/* majorOpcode */,
65266e564dSmrg    int			/* offendingMinor */,
66266e564dSmrg    int			/* severity */
67266e564dSmrg);
68266e564dSmrg
69266e564dSmrgextern void _IceErrorBadState (
70266e564dSmrg    IceConn		/* iceConn */,
71266e564dSmrg    int			/* majorOpcode */,
72266e564dSmrg    int			/* offendingMinor */,
73266e564dSmrg    int			/* severity */
74266e564dSmrg);
75266e564dSmrg
76266e564dSmrgextern void _IceErrorBadLength (
77266e564dSmrg    IceConn		/* iceConn */,
78266e564dSmrg    int			/* majorOpcode */,
79266e564dSmrg    int			/* offendingMinor */,
80266e564dSmrg    int			/* severity */
81266e564dSmrg);
82266e564dSmrg
83266e564dSmrgextern void _IceErrorBadValue (
84266e564dSmrg    IceConn		/* iceConn */,
85266e564dSmrg    int			/* majorOpcode */,
86266e564dSmrg    int			/* offendingMinor */,
87266e564dSmrg    int			/* offset */,
88266e564dSmrg    int			/* length */,
89266e564dSmrg    IcePointer		/* value */
90266e564dSmrg);
91266e564dSmrg
92266e564dSmrg
93266e564dSmrg/*
94266e564dSmrg * Macro to check if IO operations are valid on an ICE connection.
95266e564dSmrg */
96266e564dSmrg
97266e564dSmrg#define IceValidIO(_iceConn) _iceConn->io_ok
98266e564dSmrg
99266e564dSmrg
100266e564dSmrg/*
101266e564dSmrg * Macros for writing messages.
102266e564dSmrg */
103266e564dSmrg
104266e564dSmrg#define IceGetHeader(_iceConn, _major, _minor, _headerSize, _msgType, _pMsg) \
105266e564dSmrg    if ((_iceConn->outbufptr + _headerSize) > _iceConn->outbufmax) \
106266e564dSmrg        IceFlush (_iceConn); \
107266e564dSmrg    _pMsg = (_msgType *) _iceConn->outbufptr; \
108266e564dSmrg    _pMsg->majorOpcode = _major; \
109266e564dSmrg    _pMsg->minorOpcode = _minor; \
110266e564dSmrg    _pMsg->length = (_headerSize - SIZEOF (iceMsg)) >> 3; \
111266e564dSmrg    _iceConn->outbufptr += _headerSize; \
112266e564dSmrg    _iceConn->send_sequence++
113266e564dSmrg
114266e564dSmrg#define IceGetHeaderExtra(_iceConn, _major, _minor, _headerSize, _extra, _msgType, _pMsg, _pData) \
115266e564dSmrg    if ((_iceConn->outbufptr + \
116266e564dSmrg	_headerSize + ((_extra) << 3)) > _iceConn->outbufmax) \
117266e564dSmrg        IceFlush (_iceConn); \
118266e564dSmrg    _pMsg = (_msgType *) _iceConn->outbufptr; \
119266e564dSmrg    if ((_iceConn->outbufptr + \
120266e564dSmrg	_headerSize + ((_extra) << 3)) <= _iceConn->outbufmax) \
121266e564dSmrg        _pData = (char *) _pMsg + _headerSize; \
122266e564dSmrg    else \
123266e564dSmrg        _pData = NULL; \
124266e564dSmrg    _pMsg->majorOpcode = _major; \
125266e564dSmrg    _pMsg->minorOpcode = _minor; \
126266e564dSmrg    _pMsg->length = ((_headerSize - SIZEOF (iceMsg)) >> 3) + (_extra); \
127266e564dSmrg    _iceConn->outbufptr += (_headerSize + ((_extra) << 3)); \
128266e564dSmrg    _iceConn->send_sequence++
129266e564dSmrg
130266e564dSmrg#define IceSimpleMessage(_iceConn, _major, _minor) \
131266e564dSmrg{ \
132266e564dSmrg    iceMsg *_pMsg; \
133266e564dSmrg    IceGetHeader (_iceConn, _major, _minor, SIZEOF (iceMsg), iceMsg, _pMsg); \
134266e564dSmrg}
135266e564dSmrg
136266e564dSmrg#define IceErrorHeader(_iceConn, _offendingMajorOpcode, _offendingMinorOpcode, _offendingSequenceNum, _severity, _errorClass, _dataLength) \
137266e564dSmrg{ \
138266e564dSmrg    iceErrorMsg	*_pMsg; \
139266e564dSmrg\
140266e564dSmrg    IceGetHeader (_iceConn, _offendingMajorOpcode, ICE_Error, \
141266e564dSmrg	SIZEOF (iceErrorMsg), iceErrorMsg, _pMsg); \
142266e564dSmrg    _pMsg->length += (_dataLength); \
143266e564dSmrg    _pMsg->offendingMinorOpcode = _offendingMinorOpcode; \
144266e564dSmrg    _pMsg->severity = _severity; \
145266e564dSmrg    _pMsg->offendingSequenceNum = _offendingSequenceNum; \
146266e564dSmrg    _pMsg->errorClass = _errorClass; \
147266e564dSmrg}
148266e564dSmrg
149266e564dSmrg
150266e564dSmrg/*
151266e564dSmrg * Write data into the ICE output buffer.
152266e564dSmrg */
153266e564dSmrg
154266e564dSmrg#define IceWriteData(_iceConn, _bytes, _data) \
155266e564dSmrg{ \
156266e564dSmrg    if ((_iceConn->outbufptr + (_bytes)) > _iceConn->outbufmax) \
157266e564dSmrg    { \
158266e564dSmrg	IceFlush (_iceConn); \
159266e564dSmrg        _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
160266e564dSmrg    } \
161266e564dSmrg    else \
162266e564dSmrg    { \
163266e564dSmrg        memcpy (_iceConn->outbufptr, _data, _bytes); \
164266e564dSmrg        _iceConn->outbufptr += (_bytes); \
165266e564dSmrg    } \
166266e564dSmrg}
167266e564dSmrg
168266e564dSmrg#ifndef WORD64
169266e564dSmrg
170266e564dSmrg#define IceWriteData16(_iceConn, _bytes, _data) \
171266e564dSmrg    IceWriteData (_iceConn, _bytes, (char *) _data)
172266e564dSmrg
173266e564dSmrg#define IceWriteData32(_iceConn, _bytes, _data) \
174266e564dSmrg    IceWriteData (_iceConn, _bytes, (char *) _data)
175266e564dSmrg
176266e564dSmrg#else /* WORD64 */
177266e564dSmrg
178266e564dSmrg/* IceWriteData16 and IceWriteData32 defined in misc.c for WORD64 */
179266e564dSmrg
180266e564dSmrg#endif /* WORD64 */
181266e564dSmrg
182266e564dSmrg
183266e564dSmrg/*
184266e564dSmrg * The IceSendData macro bypasses copying the data to the
185266e564dSmrg * ICE connection buffer and sends the data directly.  If necessary,
186266e564dSmrg * the ICE connection buffer is first flushed.
187266e564dSmrg */
188266e564dSmrg
189266e564dSmrg#define IceSendData(_iceConn, _bytes, _data) \
190266e564dSmrg{ \
191266e564dSmrg    if (_iceConn->outbufptr > _iceConn->outbuf) \
192266e564dSmrg	IceFlush (_iceConn); \
193266e564dSmrg    _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
194266e564dSmrg}
195266e564dSmrg
196266e564dSmrg
197266e564dSmrg/*
198266e564dSmrg * Write pad bytes.  Used to force 32 or 64 bit alignment.
199266e564dSmrg * A maxium of 7 pad bytes can be specified.
200266e564dSmrg */
201266e564dSmrg
202266e564dSmrg#define IceWritePad(_iceConn, _bytes) \
203266e564dSmrg{ \
204266e564dSmrg    if ((_iceConn->outbufptr + (_bytes)) > _iceConn->outbufmax) \
205266e564dSmrg    { \
206266e564dSmrg        char _dummy[7]; \
207266e564dSmrg	IceFlush (_iceConn); \
208266e564dSmrg        _IceWrite (_iceConn, (unsigned long) (_bytes), _dummy); \
209266e564dSmrg    } \
210266e564dSmrg    else \
211266e564dSmrg    { \
212266e564dSmrg        _iceConn->outbufptr += (_bytes); \
213266e564dSmrg    } \
214266e564dSmrg}
215266e564dSmrg
216266e564dSmrg
217266e564dSmrg/*
218266e564dSmrg * Macros for reading messages.
219266e564dSmrg */
220266e564dSmrg
221266e564dSmrg#define IceReadCompleteMessage(_iceConn, _headerSize, _msgType, _pMsg, _pData)\
222266e564dSmrg{ \
223266e564dSmrg    unsigned long _bytes; \
224266e564dSmrg    IceReadMessageHeader (_iceConn, _headerSize, _msgType, _pMsg); \
225266e564dSmrg    _bytes = (_pMsg->length << 3) - (_headerSize - SIZEOF (iceMsg)); \
226266e564dSmrg    if ((_iceConn->inbufmax - _iceConn->inbufptr) >= _bytes) \
227266e564dSmrg    { \
228266e564dSmrg	_IceRead (_iceConn, _bytes, _iceConn->inbufptr); \
229266e564dSmrg	_pData = _iceConn->inbufptr; \
230266e564dSmrg	_iceConn->inbufptr += _bytes; \
231266e564dSmrg    } \
232266e564dSmrg    else \
233266e564dSmrg    { \
234266e564dSmrg	_pData = (char *) malloc ((unsigned) _bytes); \
235266e564dSmrg        if (_pData) \
236266e564dSmrg	    _IceRead (_iceConn, _bytes, _pData); \
237266e564dSmrg        else \
238266e564dSmrg	    _IceReadSkip (_iceConn, _bytes); \
239266e564dSmrg    } \
240266e564dSmrg}
241266e564dSmrg
242266e564dSmrg#define IceDisposeCompleteMessage(_iceConn, _pData) \
243266e564dSmrg    if ((char *) _pData < _iceConn->inbuf || \
244266e564dSmrg	(char *) _pData >= _iceConn->inbufmax) \
245266e564dSmrg        free ((char *) _pData);
246266e564dSmrg
247266e564dSmrg
248266e564dSmrg#define IceReadSimpleMessage(_iceConn, _msgType, _pMsg) \
249266e564dSmrg    _pMsg = (_msgType *) (_iceConn->inbuf);
250266e564dSmrg
251266e564dSmrg#define IceReadMessageHeader(_iceConn, _headerSize, _msgType, _pMsg) \
252266e564dSmrg{ \
253266e564dSmrg    _IceRead (_iceConn, \
254266e564dSmrg	(unsigned long) (_headerSize - SIZEOF (iceMsg)), \
255266e564dSmrg	_iceConn->inbufptr); \
256266e564dSmrg    _pMsg = (_msgType *) (_iceConn->inbuf); \
257266e564dSmrg    _iceConn->inbufptr += (_headerSize - SIZEOF (iceMsg)); \
258266e564dSmrg}
259266e564dSmrg
260266e564dSmrg#define IceReadData(_iceConn, _bytes, _pData) \
261266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
262266e564dSmrg
263266e564dSmrg#ifndef WORD64
264266e564dSmrg
265266e564dSmrg#define IceReadData16(_iceConn, _swap, _bytes, _pData) \
266266e564dSmrg{ \
267266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
268266e564dSmrg}
269266e564dSmrg
270266e564dSmrg#define IceReadData32(_iceConn, _swap, _bytes, _pData) \
271266e564dSmrg{ \
272266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
273266e564dSmrg}
274266e564dSmrg
275266e564dSmrg#else /* WORD64 */
276266e564dSmrg
277266e564dSmrg/* IceReadData16 and IceReadData32 defined in misc.c for WORD64 */
278266e564dSmrg
279266e564dSmrg#endif /* WORD64 */
280266e564dSmrg
281266e564dSmrg
282266e564dSmrg/*
283266e564dSmrg * Read pad bytes (for 32 or 64 bit alignment).
284266e564dSmrg * A maxium of 7 pad bytes can be specified.
285266e564dSmrg */
286266e564dSmrg
287266e564dSmrg#define IceReadPad(_iceConn, _bytes) \
288266e564dSmrg{ \
289266e564dSmrg    char _dummy[7]; \
290266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), _dummy); \
291266e564dSmrg}
292266e564dSmrg
293266e564dSmrg_XFUNCPROTOEND
294266e564dSmrg
295266e564dSmrg#endif /* _ICEMSG_H_ */
296