FSlibint.h revision ba6a1819
1/* $Xorg: FSlibint.h,v 1.4 2001/02/09 02:03:25 xorgcvs Exp $ */
2
3/*
4 * Copyright 1990 Network Computing Devices;
5 * Portions Copyright 1987 by Digital Equipment Corporation
6 *
7 * Permission to use, copy, modify, distribute, and sell this software
8 * and its documentation for any purpose is hereby granted without fee,
9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the names of Network Computing
12 * Devices or Digital not be used in advertising or publicity pertaining
13 * to distribution of the software without specific, written prior
14 * permission. Network Computing Devices or Digital make no representations
15 * about the suitability of this software for any purpose.  It is provided
16 * "as is" without express or implied warranty.
17 *
18 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
19 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
21 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
22 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25 * SOFTWARE.
26 */
27
28/*
29
30Copyright 1987, 1994, 1998  The Open Group
31
32Permission to use, copy, modify, distribute, and sell this software and its
33documentation for any purpose is hereby granted without fee, provided that
34the above copyright notice appear in all copies and that both that
35copyright notice and this permission notice appear in supporting
36documentation.
37
38The above copyright notice and this permission notice shall be included in
39all copies or substantial portions of the Software.
40
41THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
44OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
45AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
46CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47
48Except as contained in this notice, the name of The Open Group shall not be
49used in advertising or otherwise to promote the sale, use or other dealings
50in this Software without prior written authorization from The Open Group.
51
52*/
53/* $XFree86: xc/lib/FS/FSlibint.h,v 3.8 2003/08/29 18:01:12 herrb Exp $ */
54
55/*
56 * FSlib internal decls
57 */
58#include <X11/Xos.h>
59#include <stdio.h>
60
61/* This is all probably superfluous given Xos.h above */
62#ifdef USG
63#ifndef __TYPES__
64#include <sys/types.h>			/* forgot to protect it... */
65#define __TYPES__
66#endif /* __TYPES__ */
67#else
68#if defined(_POSIX_SOURCE) && defined(MOTOROLA)
69#undef _POSIX_SOURCE
70#include <sys/types.h>
71#define _POSIX_SOURCE
72#else
73#include <sys/types.h>
74#endif
75#endif /* USG */
76
77#include	"FSlib.h"
78#include	"FSlibos.h"
79#include	<errno.h>
80#include	<stddef.h>
81
82typedef int (* FSIOErrorHandler)(FSServer *);
83typedef int (* FSErrorHandler)(FSServer *, FSErrorEvent *);
84
85extern FSIOErrorHandler _FSIOErrorFunction;
86extern FSErrorHandler _FSErrorFunction;
87
88extern void _FSEatData ( FSServer *svr, unsigned long n );
89extern void _FSWaitForWritable ( FSServer *svr );
90extern void _FSWaitForReadable ( FSServer *svr );
91extern void _FSFlush ( FSServer *svr );
92extern void _FSRead ( FSServer *svr, char *data, long size );
93extern void _FSReadEvents ( FSServer *svr );
94extern void _FSReadPad ( FSServer *svr, char *data, long size );
95extern void _FSSend ( FSServer *svr, char *data, long size );
96extern void _FSEnq ( FSServer *svr, fsEvent *event );
97extern void _FSFreeServerStructure ( FSServer *svr );
98extern int _FSError ( FSServer *svr, fsError *rep );
99extern int _FSReply ( FSServer *svr, fsReply *rep, int extra, int discard );
100extern XtransConnInfo _FSConnectServer ( char *server_name );
101extern void _FSDisconnectServer ( XtransConnInfo trans_conn );
102extern void _FSSendClientPrefix ( FSServer *svr, fsConnClientPrefix *client );
103extern int _FSEventsQueued ( FSServer *svr, int mode );
104extern unsigned long _FSSetLastRequestRead ( FSServer *svr,
105					     fsGenericReply *rep );
106extern int _FSUnknownWireEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
107extern int _FSUnknownNativeEvent ( FSServer *svr, FSEvent *re,
108				   fsEvent *event );
109extern int _FSWireToEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
110extern int _FSDefaultIOError ( FSServer *svr );
111extern int _FSPrintDefaultError ( FSServer *svr, FSErrorEvent *event,
112				  FILE *fp );
113extern int _FSDefaultError ( FSServer *svr, FSErrorEvent *event );
114extern char * _FSAllocScratch ( FSServer *svr, unsigned long nbytes );
115extern void _FSFreeQ ( void );
116extern int _FSGetHostname ( char *buf, int maxlen );
117
118extern FSErrorHandler  FSSetErrorHandler ( FSErrorHandler handler );
119extern FSIOErrorHandler FSSetIOErrorHandler ( FSIOErrorHandler handler );
120
121#ifndef BUFSIZE
122#define BUFSIZE 2048		/* FS output buffer size. */
123#endif
124
125/*
126 * server flags
127 */
128#define	FSlibServerIOError	(1L << 0)
129#define	FSlibServerClosing	(1L << 1)
130
131/*   Need to start requests on 64 bit word boundries
132 *   on a CRAY computer so add a NoOp (127) if needed.
133 *   A character pointer on a CRAY computer will be non-zero
134 *   after shifting right 61 bits of it is not pointing to
135 *   a word boundary.
136 */
137
138#ifdef WORD64
139#define WORD64ALIGN if ((long)svr->bufptr >> 61) {\
140           svr->last_req = svr->bufptr;\
141           *(svr->bufptr)   = FS_Noop;\
142           *(svr->bufptr+1) =  0;\
143           *(svr->bufptr+2) =  0;\
144           *(svr->bufptr+3) =  1;\
145             svr->request += 1;\
146             svr->bufptr += 4;\
147         }
148#else				/* else does not require alignment on 64-bit
149				 * boundaries */
150#define WORD64ALIGN
151#endif				/* WORD64 */
152
153/*
154 * GetReq - Get the next avilable FS request packet in the buffer and
155 * return it.
156 *
157 * "name" is the name of the request, e.g. InternAtom, OpenFont, etc.
158 * "req" is the name of the request pointer.
159 *
160 */
161
162#if !defined(UNIXCPP) || defined(ANSICPP)
163#define GetReq(name, req) \
164        WORD64ALIGN\
165	if ((svr->bufptr + SIZEOF(fs##name##Req)) > svr->bufmax)\
166		_FSFlush(svr);\
167	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
168	req->reqType = FS_##name;\
169	req->length = (SIZEOF(fs##name##Req))>>2;\
170	svr->bufptr += SIZEOF(fs##name##Req);\
171	svr->request++
172
173#else				/* non-ANSI C uses empty comment instead of
174				 * "##" for token concatenation */
175#define GetReq(name, req) \
176        WORD64ALIGN\
177	if ((svr->bufptr + SIZEOF(fs/**/name/**/Req)) > svr->bufmax)\
178		_FSFlush(svr);\
179	req = (fs/**/name/**/Req *)(svr->last_req = svr->bufptr);\
180	req->reqType = FS_/**/name;\
181	req->length = (SIZEOF(fs/**/name/**/Req))>>2;\
182	svr->bufptr += SIZEOF(fs/**/name/**/Req);\
183	svr->request++
184#endif
185
186/* GetReqExtra is the same as GetReq, but allocates "n" additional
187   bytes after the request. "n" must be a multiple of 4!  */
188
189#if !defined(UNIXCPP) || defined(ANSICPP)
190#define GetReqExtra(name, n, req) \
191        WORD64ALIGN\
192	if ((svr->bufptr + SIZEOF(fs##name##Req) + n) > svr->bufmax)\
193		_FSFlush(svr);\
194	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
195	req->reqType = FS_##name;\
196	req->length = (SIZEOF(fs##name##Req) + n)>>2;\
197	svr->bufptr += SIZEOF(fs##name##Req) + n;\
198	svr->request++
199#else
200#define GetReqExtra(name, n, req) \
201        WORD64ALIGN\
202	if ((svr->bufptr + SIZEOF(fs/**/name/**/Req) + n) > svr->bufmax)\
203		_FSFlush(svr);\
204	req = (fs/**/name/**/Req *)(svr->last_req = svr->bufptr);\
205	req->reqType = FS_/**/name;\
206	req->length = (SIZEOF(fs/**/name/**/Req) + n)>>2;\
207	svr->bufptr += SIZEOF(fs/**/name/**/Req) + n;\
208	svr->request++
209#endif
210
211
212/*
213 * GetResReq is for those requests that have a resource ID
214 * (Window, Pixmap, GContext, etc.) as their single argument.
215 * "rid" is the name of the resource.
216 */
217
218#if !defined(UNIXCPP) || defined(ANSICPP)
219#define GetResReq(name, rid, req) \
220        WORD64ALIGN\
221	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
222	    _FSFlush(svr);\
223	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
224	req->reqType = FS_##name;\
225	req->length = 2;\
226	req->id = (rid);\
227	svr->bufptr += SIZEOF(fsResourceReq);\
228	svr->request++
229#else
230#define GetResReq(name, rid, req) \
231        WORD64ALIGN\
232	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
233	    _FSFlush(svr);\
234	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
235	req->reqType = FS_/**/name;\
236	req->length = 2;\
237	req->id = (rid);\
238	svr->bufptr += SIZEOF(fsResourceReq);\
239	svr->request++
240#endif
241
242/*
243 * GetEmptyReq is for those requests that have no arguments
244 * at all.
245 */
246
247#if !defined(UNIXCPP) || defined(ANSICPP)
248#define GetEmptyReq(name, req) \
249        WORD64ALIGN\
250	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
251	    _FSFlush(svr);\
252	req = (fsReq *) (svr->last_req = svr->bufptr);\
253	req->reqType = FS_##name;\
254	req->length = 1;\
255	svr->bufptr += SIZEOF(fsReq);\
256	svr->request++
257#else
258#define GetEmptyReq(name, req) \
259        WORD64ALIGN\
260	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
261	    _FSFlush(svr);\
262	req = (fsReq *) (svr->last_req = svr->bufptr);\
263	req->reqType = FS_/**/name;\
264	req->length = 1;\
265	svr->bufptr += SIZEOF(fsReq);\
266	svr->request++
267#endif
268
269#define	SyncHandle()	\
270	if (svr->synchandler) (*svr->synchandler)(svr)
271
272/*
273 * Data - Place data in the buffer and pad the end to provide
274 * 32 bit word alignment.  Transmit if the buffer fills.
275 *
276 * "svr" is a pointer to a Display.
277 * "data" is a pinter to a data buffer.
278 * "len" is the length of the data buffer.
279 * we can presume buffer less than 2^16 bytes, so bcopy can be used safely.
280 */
281
282#ifdef DataRoutineIsProcedure
283extern void Data();
284
285#else
286#define Data(svr, data, len) \
287	if (svr->bufptr + (len) <= svr->bufmax) {\
288		bcopy(data, svr->bufptr, (int)len);\
289		svr->bufptr += ((len) + 3) & ~3;\
290	} else\
291		_FSSend(svr, data, len)
292#endif				/* DataRoutineIsProcedure */
293
294
295/* Allocate bytes from the buffer.  No padding is done, so if
296 * the length is not a multiple of 4, the caller must be
297 * careful to leave the buffer aligned after sending the
298 * current request.
299 *
300 * "type" is the type of the pointer being assigned to.
301 * "ptr" is the pointer being assigned to.
302 * "n" is the number of bytes to allocate.
303 *
304 * Example:
305 *    xTextElt *elt;
306 *    BufAlloc (xTextElt *, elt, nbytes)
307 */
308
309#define BufAlloc(type, ptr, n) \
310    if (svr->bufptr + (n) > svr->bufmax) \
311        _FSFlush (svr); \
312    ptr = (type) svr->bufptr; \
313    svr->bufptr += (n);
314
315/*
316 * provide emulation routines for smaller architectures
317 */
318#ifndef WORD64
319#define Data16(dpy, data, len) Data((dpy), (char *)(data), (len))
320#define Data32(dpy, data, len) Data((dpy), (char *)(data), (len))
321#define _FSRead16Pad(dpy, data, len) _FSReadPad((dpy), (char *)(data), (len))
322#define _FSRead16(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
323#define _FSRead32(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
324#endif /* not WORD64 */
325
326#define PackData16(dpy,data,len) Data16 (dpy, data, len)
327#define PackData32(dpy,data,len) Data32 (dpy, data, len)
328
329#define min(a,b) (((a) < (b)) ? (a) : (b))
330#define max(a,b) (((a) > (b)) ? (a) : (b))
331
332#ifdef MUSTCOPY
333/* a little bit of magic */
334#define OneDataCard32(svr,dstaddr,srcvar) \
335  { svr->bufptr -= 4; Data32 (svr, (char *) &(srcvar), 4); }
336
337#define STARTITERATE(tpvar,type,start,endcond,decr) \
338  { register char *cpvar; \
339  for (cpvar = (char *) start; endcond; cpvar = NEXTPTR(cpvar,type), decr) { \
340    type dummy; bcopy (cpvar, (char *) &dummy, SIZEOF(type)); \
341    tpvar = (type *) cpvar;
342#define ENDITERATE }}
343
344#else
345
346/* srcvar must be a variable for large architecture version */
347#define OneDataCard32(svr,dstaddr,srcvar) \
348  { *(unsigned long *)(dstaddr) = (srcvar); }
349
350#define STARTITERATE(tpvar,type,start,endcond,decr) \
351  for (tpvar = (type *) start; endcond; tpvar++, decr) {
352#define ENDITERATE }
353#endif				/* MUSTCOPY - used machines whose C structs
354				 * don't line up with proto */
355
356
357#if !defined(UNIXCPP) || defined(ANSICPP)
358#define FSCat(x,y) x##_##y
359#else
360#define FSCat(x,y) x/**/_/**/y
361#endif
362
363/* copy XCharInfo parts of a protocol reply into a FSXCharInfo */
364
365#define FSUnpack_XCharInfo(packet, structure) \
366    (structure)->left = FSCat(packet,left); \
367    (structure)->right = FSCat(packet,right); \
368    (structure)->width = FSCat(packet,width); \
369    (structure)->ascent = FSCat(packet,ascent); \
370    (structure)->descent = FSCat(packet,descent); \
371    (structure)->attributes = FSCat(packet,attributes)
372
373
374/* copy XFontInfoHeader parts of a protocol reply into a FSXFontInfoHeader */
375
376#define FSUnpack_XFontInfoHeader(packet, structure, serverversion) \
377    (structure)->flags = (packet)->font_header_flags; \
378    (structure)->draw_direction = (packet)->font_header_draw_direction; \
379 \
380    if (serverversion > 1) { \
381	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_high; \
382	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_low; \
383	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_high; \
384	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_low; \
385	(structure)->default_char.high = (packet)->font_header_default_char_high; \
386	(structure)->default_char.low = (packet)->font_header_default_char_low; \
387    } else { \
388	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_low; \
389	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_high; \
390	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_low; \
391	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_high; \
392	(structure)->default_char.high = (packet)->font_header_default_char_low; \
393	(structure)->default_char.low = (packet)->font_header_default_char_high; \
394	} \
395 \
396    (structure)->font_ascent = (packet)->font_header_font_ascent; \
397    (structure)->font_descent = (packet)->font_header_font_descent; \
398 \
399    FSUnpack_XCharInfo((packet)->font_header_min_bounds, &(structure)->min_bounds); \
400    FSUnpack_XCharInfo((packet)->font_header_max_bounds, &(structure)->max_bounds)
401