FSlibint.h revision 298453a4
1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1994, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52/*
53 * FSlib internal decls
54 */
55#include <X11/Xos.h>
56#include <stdio.h>
57#include <sys/types.h>
58
59#include	"FSlib.h"
60#include	"FSlibos.h"
61#include	<errno.h>
62#include	<stddef.h>
63
64typedef int (* FSIOErrorHandler)(FSServer *) _X_NORETURN;
65typedef int (* FSErrorHandler)(FSServer *, FSErrorEvent *);
66
67extern FSIOErrorHandler _FSIOErrorFunction;
68extern FSErrorHandler _FSErrorFunction;
69
70extern void _FSEatData ( FSServer *svr, unsigned long n );
71extern void _FSWaitForWritable ( FSServer *svr );
72extern void _FSWaitForReadable ( FSServer *svr );
73extern void _FSFlush ( FSServer *svr );
74extern void _FSRead ( FSServer *svr, char *data, long size );
75extern void _FSReadEvents ( FSServer *svr );
76extern void _FSReadPad ( FSServer *svr, char *data, long size );
77extern void _FSSend ( FSServer *svr, const char *data, long size );
78extern void _FSEnq ( FSServer *svr, fsEvent *event );
79extern void _FSFreeServerStructure ( FSServer *svr );
80extern int _FSError ( FSServer *svr, fsError *rep );
81extern int _FSReply ( FSServer *svr, fsReply *rep, int extra, int discard );
82extern XtransConnInfo _FSConnectServer ( char *server_name );
83extern void _FSDisconnectServer ( XtransConnInfo trans_conn );
84extern void _FSSendClientPrefix ( FSServer *svr, fsConnClientPrefix *client );
85extern unsigned long _FSSetLastRequestRead ( FSServer *svr,
86					     fsGenericReply *rep );
87extern int _FSUnknownWireEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
88extern int _FSUnknownNativeEvent ( FSServer *svr, FSEvent *re,
89				   fsEvent *event );
90extern int _FSDefaultIOError ( FSServer *svr ) _X_NORETURN;
91extern int _FSPrintDefaultError ( FSServer *svr, FSErrorEvent *event,
92				  FILE *fp );
93extern int _FSDefaultError ( FSServer *svr, FSErrorEvent *event );
94extern void _FSFreeQ ( void );
95
96extern FSErrorHandler  FSSetErrorHandler ( FSErrorHandler handler );
97extern FSIOErrorHandler FSSetIOErrorHandler ( FSIOErrorHandler handler );
98
99extern _FSQEvent *_FSqfree;
100extern FSServer *_FSHeadOfServerList;
101
102#ifndef BUFSIZE
103#define BUFSIZE 2048		/* FS output buffer size. */
104#endif
105
106/*
107 * server flags
108 */
109#define	FSlibServerIOError	(1L << 0)
110#define	FSlibServerClosing	(1L << 1)
111
112
113/*
114 * GetReq - Get the next available FS request packet in the buffer and
115 * return it.
116 *
117 * "name" is the name of the request, e.g. InternAtom, OpenFont, etc.
118 * "req" is the name of the request pointer.
119 *
120 */
121
122#define GetReq(name, req) \
123	if ((svr->bufptr + SIZEOF(fs##name##Req)) > svr->bufmax)\
124		_FSFlush(svr);\
125	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
126	req->reqType = FS_##name;\
127	req->length = (SIZEOF(fs##name##Req))>>2;\
128	svr->bufptr += SIZEOF(fs##name##Req);\
129	svr->request++
130
131/* GetReqExtra is the same as GetReq, but allocates "n" additional
132   bytes after the request. "n" must be a multiple of 4!  */
133
134#define GetReqExtra(name, n, req) \
135	if ((svr->bufptr + SIZEOF(fs##name##Req) + n) > svr->bufmax)\
136		_FSFlush(svr);\
137	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
138	req->reqType = FS_##name;\
139	req->length = (SIZEOF(fs##name##Req) + n)>>2;\
140	svr->bufptr += SIZEOF(fs##name##Req) + n;\
141	svr->request++
142
143/*
144 * GetResReq is for those requests that have a resource ID
145 * (Window, Pixmap, GContext, etc.) as their single argument.
146 * "rid" is the name of the resource.
147 */
148
149#define GetResReq(name, rid, req) \
150	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
151	    _FSFlush(svr);\
152	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
153	req->reqType = FS_##name;\
154	req->length = 2;\
155	req->id = (CARD32) (rid);\
156	svr->bufptr += SIZEOF(fsResourceReq);\
157	svr->request++
158
159/*
160 * GetEmptyReq is for those requests that have no arguments
161 * at all.
162 */
163
164#define GetEmptyReq(name, req) \
165	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
166	    _FSFlush(svr);\
167	req = (fsReq *) (svr->last_req = svr->bufptr);\
168	req->reqType = FS_##name;\
169	req->length = 1;\
170	svr->bufptr += SIZEOF(fsReq);\
171	svr->request++
172
173#define	SyncHandle()	\
174	if (svr->synchandler) (*svr->synchandler)(svr)
175
176/*
177 * Data - Place data in the buffer and pad the end to provide
178 * 32 bit word alignment.  Transmit if the buffer fills.
179 *
180 * "svr" is a pointer to a Display.
181 * "data" is a pinter to a data buffer.
182 * "len" is the length of the data buffer.
183 * we can presume buffer less than 2^16 bytes, so bcopy can be used safely.
184 */
185
186#ifdef DataRoutineIsProcedure
187extern void Data();
188
189#else
190#define Data(svr, data, len) \
191	if (svr->bufptr + (len) <= svr->bufmax) {\
192		memmove(svr->bufptr, data, len);\
193		svr->bufptr += ((len) + 3) & ~3;\
194	} else\
195		_FSSend(svr, data, len)
196#endif				/* DataRoutineIsProcedure */
197
198
199/* Allocate bytes from the buffer.  No padding is done, so if
200 * the length is not a multiple of 4, the caller must be
201 * careful to leave the buffer aligned after sending the
202 * current request.
203 *
204 * "type" is the type of the pointer being assigned to.
205 * "ptr" is the pointer being assigned to.
206 * "n" is the number of bytes to allocate.
207 *
208 * Example:
209 *    xTextElt *elt;
210 *    BufAlloc (xTextElt *, elt, nbytes)
211 */
212
213#define BufAlloc(type, ptr, n) \
214    if (svr->bufptr + (n) > svr->bufmax) \
215        _FSFlush (svr); \
216    ptr = (type) svr->bufptr; \
217    svr->bufptr += (n);
218
219/*
220 * provide emulation routines for smaller architectures
221 */
222#define Data16(dpy, data, len) Data((dpy), (char *)(data), (len))
223#define Data32(dpy, data, len) Data((dpy), (char *)(data), (len))
224#define _FSRead16Pad(dpy, data, len) _FSReadPad((dpy), (char *)(data), (len))
225#define _FSRead16(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
226#define _FSRead32(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
227
228#define PackData16(dpy,data,len) Data16 (dpy, data, len)
229#define PackData32(dpy,data,len) Data32 (dpy, data, len)
230
231#define min(a,b) (((a) < (b)) ? (a) : (b))
232#define max(a,b) (((a) > (b)) ? (a) : (b))
233
234/* srcvar must be a variable for large architecture version */
235#define OneDataCard32(svr,dstaddr,srcvar) \
236  { *(unsigned long *)(dstaddr) = (srcvar); }
237
238#define STARTITERATE(tpvar,type,start,endcond,decr) \
239  for (tpvar = (type *) start; endcond; tpvar++, decr) {
240#define ENDITERATE }
241
242
243#define FSCat(x,y) x##_##y
244
245/* copy XCharInfo parts of a protocol reply into a FSXCharInfo */
246
247#define FSUnpack_XCharInfo(packet, structure) \
248    (structure)->left = FSCat(packet,left); \
249    (structure)->right = FSCat(packet,right); \
250    (structure)->width = FSCat(packet,width); \
251    (structure)->ascent = FSCat(packet,ascent); \
252    (structure)->descent = FSCat(packet,descent); \
253    (structure)->attributes = FSCat(packet,attributes)
254
255
256/* copy XFontInfoHeader parts of a protocol reply into a FSXFontInfoHeader */
257
258#define FSUnpack_XFontInfoHeader(packet, structure, serverversion) \
259    (structure)->flags = (packet)->font_header_flags; \
260    (structure)->draw_direction = (packet)->font_header_draw_direction; \
261 \
262    if (serverversion > 1) { \
263	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_high; \
264	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_low; \
265	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_high; \
266	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_low; \
267	(structure)->default_char.high = (packet)->font_header_default_char_high; \
268	(structure)->default_char.low = (packet)->font_header_default_char_low; \
269    } else { \
270	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_low; \
271	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_high; \
272	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_low; \
273	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_high; \
274	(structure)->default_char.high = (packet)->font_header_default_char_low; \
275	(structure)->default_char.low = (packet)->font_header_default_char_high; \
276	} \
277 \
278    (structure)->font_ascent = (packet)->font_header_font_ascent; \
279    (structure)->font_descent = (packet)->font_header_font_descent; \
280 \
281    FSUnpack_XCharInfo((packet)->font_header_min_bounds, &(structure)->min_bounds); \
282    FSUnpack_XCharInfo((packet)->font_header_max_bounds, &(structure)->max_bounds)
283