misc.h revision 6747b715
105b261ecSmrg/***********************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1987, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrg
2605b261ecSmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
2705b261ecSmrg
2805b261ecSmrg                        All Rights Reserved
2905b261ecSmrg
3005b261ecSmrgPermission to use, copy, modify, and distribute this software and its
3105b261ecSmrgdocumentation for any purpose and without fee is hereby granted,
3205b261ecSmrgprovided that the above copyright notice appear in all copies and that
3305b261ecSmrgboth that copyright notice and this permission notice appear in
3405b261ecSmrgsupporting documentation, and that the name of Digital not be
3505b261ecSmrgused in advertising or publicity pertaining to distribution of the
3605b261ecSmrgsoftware without specific, written prior permission.
3705b261ecSmrg
3805b261ecSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3905b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
4005b261ecSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4105b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4205b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4305b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4405b261ecSmrgSOFTWARE.
4505b261ecSmrg
4605b261ecSmrgCopyright 1992, 1993 Data General Corporation;
4705b261ecSmrgCopyright 1992, 1993 OMRON Corporation
4805b261ecSmrg
4905b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
5005b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that the
5105b261ecSmrgabove copyright notice appear in all copies and that both that copyright
5205b261ecSmrgnotice and this permission notice appear in supporting documentation, and that
5305b261ecSmrgneither the name OMRON or DATA GENERAL be used in advertising or publicity
5405b261ecSmrgpertaining to distribution of the software without specific, written prior
5505b261ecSmrgpermission of the party whose name is to be used.  Neither OMRON or
5605b261ecSmrgDATA GENERAL make any representation about the suitability of this software
5705b261ecSmrgfor any purpose.  It is provided "as is" without express or implied warranty.
5805b261ecSmrg
5905b261ecSmrgOMRON AND DATA GENERAL EACH DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
6005b261ecSmrgSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
6105b261ecSmrgIN NO EVENT SHALL OMRON OR DATA GENERAL BE LIABLE FOR ANY SPECIAL, INDIRECT
6205b261ecSmrgOR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
6305b261ecSmrgDATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
6405b261ecSmrgTORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
6505b261ecSmrgOF THIS SOFTWARE.
6605b261ecSmrg
6705b261ecSmrg******************************************************************/
6805b261ecSmrg#ifndef MISC_H
6905b261ecSmrg#define MISC_H 1
7005b261ecSmrg/*
7105b261ecSmrg *  X internal definitions
7205b261ecSmrg *
7305b261ecSmrg */
7405b261ecSmrg
7505b261ecSmrg#include <X11/Xosdefs.h>
7605b261ecSmrg#include <X11/Xfuncproto.h>
7705b261ecSmrg#include <X11/Xmd.h>
7805b261ecSmrg#include <X11/X.h>
7905b261ecSmrg#include <X11/Xdefs.h>
8005b261ecSmrg
8105b261ecSmrg#include <stddef.h>
8205b261ecSmrg
8305b261ecSmrg#ifndef MAXSCREENS
8405b261ecSmrg#define MAXSCREENS	16
8505b261ecSmrg#endif
8605b261ecSmrg#define MAXCLIENTS	256
874642e01fSmrg#define MAXEXTENSIONS   128
8805b261ecSmrg#define MAXFORMATS	8
896747b715Smrg#define MAXDEVICES	40 /* input devices */
904642e01fSmrg
914642e01fSmrg#define EXTENSION_EVENT_BASE 64
924642e01fSmrg#define EXTENSION_BASE 128
9305b261ecSmrg
9405b261ecSmrgtypedef unsigned long PIXEL;
9505b261ecSmrgtypedef unsigned long ATOM;
9605b261ecSmrg
9705b261ecSmrg
9805b261ecSmrg#ifndef TRUE
9905b261ecSmrg#define TRUE 1
10005b261ecSmrg#define FALSE 0
10105b261ecSmrg#endif
10205b261ecSmrg
10305b261ecSmrg#ifndef _XTYPEDEF_CALLBACKLISTPTR
10405b261ecSmrgtypedef struct _CallbackList *CallbackListPtr; /* also in dix.h */
10505b261ecSmrg#define _XTYPEDEF_CALLBACKLISTPTR
10605b261ecSmrg#endif
10705b261ecSmrg
10805b261ecSmrgtypedef struct _xReq *xReqPtr;
10905b261ecSmrg
11005b261ecSmrg#include "os.h" 	/* for ALLOCATE_LOCAL and DEALLOCATE_LOCAL */
11105b261ecSmrg#include <X11/Xfuncs.h> /* for bcopy, bzero, and bcmp */
11205b261ecSmrg
11305b261ecSmrg#define NullBox ((BoxPtr)0)
11405b261ecSmrg#define MILLI_PER_MIN (1000 * 60)
11505b261ecSmrg#define MILLI_PER_SECOND (1000)
11605b261ecSmrg
11705b261ecSmrg    /* this next is used with None and ParentRelative to tell
11805b261ecSmrg       PaintWin() what to use to paint the background. Also used
11905b261ecSmrg       in the macro IS_VALID_PIXMAP */
12005b261ecSmrg
12105b261ecSmrg#define USE_BACKGROUND_PIXEL 3
12205b261ecSmrg#define USE_BORDER_PIXEL 3
12305b261ecSmrg
12405b261ecSmrg
12505b261ecSmrg/* byte swap a 32-bit literal */
12605b261ecSmrg#define lswapl(x) ((((x) & 0xff) << 24) |\
12705b261ecSmrg		   (((x) & 0xff00) << 8) |\
12805b261ecSmrg		   (((x) & 0xff0000) >> 8) |\
12905b261ecSmrg		   (((x) >> 24) & 0xff))
13005b261ecSmrg
13105b261ecSmrg/* byte swap a short literal */
13205b261ecSmrg#define lswaps(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
13305b261ecSmrg
13405b261ecSmrg#undef min
13505b261ecSmrg#undef max
13605b261ecSmrg
13705b261ecSmrg#define min(a, b) (((a) < (b)) ? (a) : (b))
13805b261ecSmrg#define max(a, b) (((a) > (b)) ? (a) : (b))
13905b261ecSmrg/* abs() is a function, not a macro; include the file declaring
14005b261ecSmrg * it in case we haven't done that yet.
14105b261ecSmrg */
14205b261ecSmrg#include <stdlib.h>
14305b261ecSmrg#ifndef Fabs
14405b261ecSmrg#define Fabs(a) ((a) > 0.0 ? (a) : -(a))	/* floating absolute value */
14505b261ecSmrg#endif
14605b261ecSmrg#define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
14705b261ecSmrg/* this assumes b > 0 */
14805b261ecSmrg#define modulus(a, b, d)    if (((d) = (a) % (b)) < 0) (d) += (b)
14905b261ecSmrg/*
15005b261ecSmrg * return the least significant bit in x which is set
15105b261ecSmrg *
15205b261ecSmrg * This works on 1's complement and 2's complement machines.
15305b261ecSmrg * If you care about the extra instruction on 2's complement
15405b261ecSmrg * machines, change to ((x) & (-(x)))
15505b261ecSmrg */
15605b261ecSmrg#define lowbit(x) ((x) & (~(x) + 1))
15705b261ecSmrg
15805b261ecSmrg/* XXX Not for modules */
15905b261ecSmrg#include <limits.h>
16005b261ecSmrg#if !defined(MAXSHORT) || !defined(MINSHORT) || \
16105b261ecSmrg    !defined(MAXINT) || !defined(MININT)
16205b261ecSmrg/*
16305b261ecSmrg * Some implementations #define these through <math.h>, so preclude
16405b261ecSmrg * #include'ing it later.
16505b261ecSmrg */
16605b261ecSmrg
16705b261ecSmrg#include <math.h>
16805b261ecSmrg#undef MAXSHORT
16905b261ecSmrg#define MAXSHORT SHRT_MAX
17005b261ecSmrg#undef MINSHORT
17105b261ecSmrg#define MINSHORT SHRT_MIN
17205b261ecSmrg#undef MAXINT
17305b261ecSmrg#define MAXINT INT_MAX
17405b261ecSmrg#undef MININT
17505b261ecSmrg#define MININT INT_MIN
17605b261ecSmrg
17705b261ecSmrg#include <assert.h>
17805b261ecSmrg#include <ctype.h>
17905b261ecSmrg#include <stdio.h>     /* for fopen, etc... */
18005b261ecSmrg
18105b261ecSmrg#endif
18205b261ecSmrg
1836747b715Smrg/**
1846747b715Smrg * Calculate the number of bytes needed to hold bits.
1856747b715Smrg * @param bits The minimum number of bits needed.
1866747b715Smrg * @return The number of bytes needed to hold bits.
1876747b715Smrg */
1886747b715Smrgstatic inline int
1896747b715Smrgbits_to_bytes(const int bits) {
1906747b715Smrg    return ((bits + 7) >> 3);
1916747b715Smrg}
1926747b715Smrg/**
1936747b715Smrg * Calculate the number of 4-byte units needed to hold the given number of
1946747b715Smrg * bytes.
1956747b715Smrg * @param bytes The minimum number of bytes needed.
1966747b715Smrg * @return The number of 4-byte units needed to hold bytes.
1976747b715Smrg */
1986747b715Smrgstatic inline int
1996747b715Smrgbytes_to_int32(const int bytes) {
2006747b715Smrg    return (((bytes) + 3) >> 2);
2016747b715Smrg}
2026747b715Smrg
2036747b715Smrg/**
2046747b715Smrg * Calculate the number of bytes (in multiples of 4) needed to hold bytes.
2056747b715Smrg * @param bytes The minimum number of bytes needed.
2066747b715Smrg * @return The closest multiple of 4 that is equal or higher than bytes.
2076747b715Smrg */
2086747b715Smrgstatic inline int
2096747b715Smrgpad_to_int32(const int bytes) {
2106747b715Smrg    return (((bytes) + 3) & ~3);
2116747b715Smrg}
2126747b715Smrg
2136747b715Smrgextern char**
2146747b715Smrgxstrtokenize(const char *str, const char* separators);
2156747b715Smrg
21605b261ecSmrg/* some macros to help swap requests, replies, and events */
21705b261ecSmrg
21805b261ecSmrg#define LengthRestB(stuff) \
21905b261ecSmrg    ((client->req_len << 2) - sizeof(*stuff))
22005b261ecSmrg
22105b261ecSmrg#define LengthRestS(stuff) \
22205b261ecSmrg    ((client->req_len << 1) - (sizeof(*stuff) >> 1))
22305b261ecSmrg
22405b261ecSmrg#define LengthRestL(stuff) \
22505b261ecSmrg    (client->req_len - (sizeof(*stuff) >> 2))
22605b261ecSmrg
22705b261ecSmrg#define SwapRestS(stuff) \
22805b261ecSmrg    SwapShorts((short *)(stuff + 1), LengthRestS(stuff))
22905b261ecSmrg
23005b261ecSmrg#define SwapRestL(stuff) \
23105b261ecSmrg    SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
23205b261ecSmrg
23305b261ecSmrg/* byte swap a 32-bit value */
23405b261ecSmrg#define swapl(x, n) { \
23505b261ecSmrg		 n = ((char *) (x))[0];\
23605b261ecSmrg		 ((char *) (x))[0] = ((char *) (x))[3];\
23705b261ecSmrg		 ((char *) (x))[3] = n;\
23805b261ecSmrg		 n = ((char *) (x))[1];\
23905b261ecSmrg		 ((char *) (x))[1] = ((char *) (x))[2];\
24005b261ecSmrg		 ((char *) (x))[2] = n; }
24105b261ecSmrg
24205b261ecSmrg/* byte swap a short */
24305b261ecSmrg#define swaps(x, n) { \
24405b261ecSmrg		 n = ((char *) (x))[0];\
24505b261ecSmrg		 ((char *) (x))[0] = ((char *) (x))[1];\
24605b261ecSmrg		 ((char *) (x))[1] = n; }
24705b261ecSmrg
24805b261ecSmrg/* copy 32-bit value from src to dst byteswapping on the way */
24905b261ecSmrg#define cpswapl(src, dst) { \
25005b261ecSmrg                 ((char *)&(dst))[0] = ((char *) &(src))[3];\
25105b261ecSmrg                 ((char *)&(dst))[1] = ((char *) &(src))[2];\
25205b261ecSmrg                 ((char *)&(dst))[2] = ((char *) &(src))[1];\
25305b261ecSmrg                 ((char *)&(dst))[3] = ((char *) &(src))[0]; }
25405b261ecSmrg
25505b261ecSmrg/* copy short from src to dst byteswapping on the way */
25605b261ecSmrg#define cpswaps(src, dst) { \
25705b261ecSmrg		 ((char *) &(dst))[0] = ((char *) &(src))[1];\
25805b261ecSmrg		 ((char *) &(dst))[1] = ((char *) &(src))[0]; }
25905b261ecSmrg
2606747b715Smrgextern _X_EXPORT void SwapLongs(
26105b261ecSmrg    CARD32 *list,
26205b261ecSmrg    unsigned long count);
26305b261ecSmrg
2646747b715Smrgextern _X_EXPORT void SwapShorts(
26505b261ecSmrg    short *list,
26605b261ecSmrg    unsigned long count);
26705b261ecSmrg
2686747b715Smrgextern _X_EXPORT void MakePredeclaredAtoms(void);
26905b261ecSmrg
2706747b715Smrgextern _X_EXPORT int Ones(
27105b261ecSmrg    unsigned long /*mask*/);
27205b261ecSmrg
27305b261ecSmrgtypedef struct _xPoint *DDXPointPtr;
27405b261ecSmrgtypedef struct pixman_box16 *BoxPtr;
27505b261ecSmrgtypedef struct _xEvent *xEventPtr;
27605b261ecSmrgtypedef struct _xRectangle *xRectanglePtr;
27705b261ecSmrgtypedef struct _GrabRec *GrabPtr;
27805b261ecSmrg
27905b261ecSmrg/*  typedefs from other places - duplicated here to minimize the amount
28005b261ecSmrg *  of unnecessary junk that one would normally have to include to get
28105b261ecSmrg *  these symbols defined
28205b261ecSmrg */
28305b261ecSmrg
28405b261ecSmrg#ifndef _XTYPEDEF_CHARINFOPTR
28505b261ecSmrgtypedef struct _CharInfo *CharInfoPtr; /* also in fonts/include/font.h */
28605b261ecSmrg#define _XTYPEDEF_CHARINFOPTR
28705b261ecSmrg#endif
28805b261ecSmrg
2896747b715Smrgextern _X_EXPORT unsigned long globalSerialNumber;
2906747b715Smrgextern _X_EXPORT unsigned long serverGeneration;
2916747b715Smrg
29205b261ecSmrg#endif /* MISC_H */
293