1/*
2
3Copyright 1990, 1991, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25 * Copyright 1990, 1991 Network Computing Devices;
26 * Portions Copyright 1987 by Digital Equipment Corporation
27 *
28 * Permission to use, copy, modify, distribute, and sell this software and
29 * its documentation for any purpose is hereby granted without fee, provided
30 * that the above copyright notice appear in all copies and that both that
31 * copyright notice and this permission notice appear in supporting
32 * documentation, and that the names of Network Computing Devices, or Digital
33 * not be used in advertising or publicity pertaining to distribution
34 * of the software without specific, written prior permission.
35 *
36 * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
37 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
39 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
40 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
41 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
42 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
43 * THIS SOFTWARE.
44 */
45
46#ifndef _MISC_H_
47#define _MISC_H_
48
49#include	<X11/Xosdefs.h>
50#include	<X11/Xfuncs.h>
51#include	<X11/Xdefs.h>
52
53#include	"assert.h"	/* so its everywhere */
54
55#ifndef NULL
56#include	<stddef.h>
57#endif
58
59#define	MAXCLIENTS	128
60
61#define	MILLI_PER_SECOND	(1000)
62#define	MILLI_PER_MINUTE	(1000 * 60)
63
64#ifndef TRUE
65#define	TRUE 1
66#define	FALSE 0
67#endif
68
69#define	min(a, b)	(((a) < (b)) ? (a) : (b))
70#define	max(a, b)	(((a) > (b)) ? (a) : (b))
71
72#include	"os.h"
73
74#define	lowbit(x)	((x) & (~(x) + 1))
75
76/* byte swapping helpers */
77
78/* byte swap a long literal */
79#define lswapl(x) ((((x) & 0xff) << 24) |\
80		   (((x) & 0xff00) << 8) |\
81		   (((x) & 0xff0000) >> 8) |\
82		   (((x) >> 24) & 0xff))
83
84/* byte swap a short literal */
85#define lswaps(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
86
87
88/* byte swap a long */
89#define swapl(x, n) n = ((char *) (x))[0];\
90		 ((char *) (x))[0] = ((char *) (x))[3];\
91		 ((char *) (x))[3] = n;\
92		 n = ((char *) (x))[1];\
93		 ((char *) (x))[1] = ((char *) (x))[2];\
94		 ((char *) (x))[2] = n;
95
96/* byte swap a short */
97#define swaps(x, n) n = ((char *) (x))[0];\
98		 ((char *) (x))[0] = ((char *) (x))[1];\
99		 ((char *) (x))[1] = n
100
101/* copy long from src to dst byteswapping on the way */
102#define cpswapl(src, dst) \
103                 ((char *)&(dst))[0] = ((char *) &(src))[3];\
104                 ((char *)&(dst))[1] = ((char *) &(src))[2];\
105                 ((char *)&(dst))[2] = ((char *) &(src))[1];\
106                 ((char *)&(dst))[3] = ((char *) &(src))[0];
107
108/* copy short from src to dst byteswapping on the way */
109#define cpswaps(src, dst)\
110		 ((char *) &(dst))[0] = ((char *) &(src))[1];\
111		 ((char *) &(dst))[1] = ((char *) &(src))[0];
112
113
114extern int ValidAtom(Atom atom);
115extern const char *NameForAtom(Atom atom);
116extern Atom MakeAtom(const char *string, unsigned int len, Bool makeit);
117
118#define fsCat(x,y) x##_##y
119
120/* copy a xCharInfo into a XCharInfo */
121
122#define fsPack_XCharInfo(structure, packet) \
123    fsCat(packet,left) = (structure)->leftSideBearing; \
124    fsCat(packet,right) = (structure)->rightSideBearing; \
125    fsCat(packet,width) = (structure)->characterWidth; \
126    fsCat(packet,ascent) = (structure)->ascent; \
127    fsCat(packet,descent) = (structure)->descent; \
128    fsCat(packet,attributes) = (structure)->attributes
129
130
131/* copy a FontInfoRec into a XFontInfoHeader */
132
133#define fsPack_XFontInfoHeader(structure, packet, clientversion) \
134    (packet)->font_header_flags = ((structure)->allExist) ? FontInfoAllCharsExist : 0; \
135    (packet)->font_header_draw_direction = ((structure)->drawDirection == LeftToRight) \
136               ? LeftToRightDrawDirection : RightToLeftDrawDirection; \
137 \
138    if ((structure)->inkInside) \
139	(packet)->font_header_flags |= FontInfoInkInside; \
140 \
141    if (clientversion > 1) { \
142	(packet)->font_hdr_char_range_min_char_high = (structure)->firstRow; \
143        (packet)->font_hdr_char_range_min_char_low = (structure)->firstCol; \
144        (packet)->font_hdr_char_range_max_char_high = (structure)->lastRow; \
145        (packet)->font_hdr_char_range_max_char_low = (structure)->lastCol; \
146        (packet)->font_header_default_char_high = (structure)->defaultCh >> 8; \
147        (packet)->font_header_default_char_low = (structure)->defaultCh & 0xff; \
148    } else { \
149	(packet)->font_hdr_char_range_min_char_high = (structure)->firstCol; \
150	(packet)->font_hdr_char_range_min_char_low = (structure)->firstRow; \
151	(packet)->font_hdr_char_range_max_char_high = (structure)->lastCol; \
152	(packet)->font_hdr_char_range_max_char_low = (structure)->lastRow; \
153	(packet)->font_header_default_char_high = (structure)->defaultCh & 0xff; \
154	(packet)->font_header_default_char_low = (structure)->defaultCh >> 8; \
155    } \
156 \
157    fsPack_XCharInfo(&(structure)->ink_minbounds, (packet)->font_header_min_bounds); \
158    fsPack_XCharInfo(&(structure)->ink_maxbounds, (packet)->font_header_max_bounds); \
159 \
160    (packet)->font_header_font_ascent = (structure)->fontAscent; \
161    (packet)->font_header_font_descent = (structure)->fontDescent
162
163
164typedef struct {		/* when cloning, need old transport info */
165    int trans_id;
166    int fd;
167    int portnum;
168} OldListenRec;
169
170/* os/connection.c */
171extern	void	CreateSockets(int old_listen_count, OldListenRec *old_listen);
172
173/* os/xfstrans.c */
174extern OldListenRec *_FontTransGetInetdListenInfo (int fd);
175
176extern void BitOrderInvert(unsigned char *buf, int nbytes);
177extern void TwoByteSwap(unsigned char *buf, int nbytes);
178extern void FourByteSwap(unsigned char *buf, int nbytes);
179
180#endif				/* _MISC_H_ */
181