Xlibint.h revision 2e9c7c8c
1/* $Xorg: Xlibint.h,v 1.5 2001/02/09 02:03:38 xorgcvs Exp $ */
2
3/*
4
5Copyright 1984, 1985, 1987, 1989, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included
14in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of The Open Group shall
25not be used in advertising or otherwise to promote the sale, use or
26other dealings in this Software without prior written authorization
27from The Open Group.
28
29*/
30/* $XFree86: xc/lib/X11/Xlibint.h,v 3.27 2003/05/27 22:26:26 tsi Exp $ */
31
32#ifndef _XLIBINT_H_
33#define _XLIBINT_H_ 1
34
35/*
36 *	Xlibint.h - Header definition and support file for the internal
37 *	support routines used by the C subroutine interface
38 *	library (Xlib) to the X Window System.
39 *
40 *	Warning, there be dragons here....
41 */
42
43#include <X11/Xlib.h>
44#include <X11/Xproto.h>		/* to declare xEvent */
45#include <X11/XlibConf.h>	/* for configured options like XTHREADS */
46
47#ifdef WIN32
48#define _XFlush _XFlushIt
49#endif
50
51/*
52 * If your BytesReadable correctly detects broken connections, then
53 * you should NOT define XCONN_CHECK_FREQ.
54 */
55#ifndef XCONN_CHECK_FREQ
56#define XCONN_CHECK_FREQ 256
57#endif
58
59struct _XGC
60{
61    XExtData *ext_data;	/* hook for extension to hang data */
62    GContext gid;	/* protocol ID for graphics context */
63    Bool rects;		/* boolean: TRUE if clipmask is list of rectangles */
64    Bool dashes;	/* boolean: TRUE if dash-list is really a list */
65    unsigned long dirty;/* cache dirty bits */
66    XGCValues values;	/* shadow structure of values */
67};
68
69struct _XDisplay
70{
71	XExtData *ext_data;	/* hook for extension to hang data */
72	struct _XFreeFuncs *free_funcs; /* internal free functions */
73	int fd;			/* Network socket. */
74	int conn_checker;         /* ugly thing used by _XEventsQueued */
75	int proto_major_version;/* maj. version of server's X protocol */
76	int proto_minor_version;/* minor version of server's X protocol */
77	char *vendor;		/* vendor of the server hardware */
78        XID resource_base;	/* resource ID base */
79	XID resource_mask;	/* resource ID mask bits */
80	XID resource_id;	/* allocator current ID */
81	int resource_shift;	/* allocator shift to correct bits */
82	XID (*resource_alloc)(	/* allocator function */
83		struct _XDisplay*
84		);
85	int byte_order;		/* screen byte order, LSBFirst, MSBFirst */
86	int bitmap_unit;	/* padding and data requirements */
87	int bitmap_pad;		/* padding requirements on bitmaps */
88	int bitmap_bit_order;	/* LeastSignificant or MostSignificant */
89	int nformats;		/* number of pixmap formats in list */
90	ScreenFormat *pixmap_format;	/* pixmap format list */
91	int vnumber;		/* Xlib's X protocol version number. */
92	int release;		/* release of the server */
93	struct _XSQEvent *head, *tail;	/* Input event queue. */
94	int qlen;		/* Length of input event queue */
95	unsigned long last_request_read; /* seq number of last event read */
96	unsigned long request;	/* sequence number of last request. */
97	char *last_req;		/* beginning of last request, or dummy */
98	char *buffer;		/* Output buffer starting address. */
99	char *bufptr;		/* Output buffer index pointer. */
100	char *bufmax;		/* Output buffer maximum+1 address. */
101	unsigned max_request_size; /* maximum number 32 bit words in request*/
102	struct _XrmHashBucketRec *db;
103	int (*synchandler)(	/* Synchronization handler */
104		struct _XDisplay*
105		);
106	char *display_name;	/* "host:display" string used on this connect*/
107	int default_screen;	/* default screen for operations */
108	int nscreens;		/* number of screens on this server*/
109	Screen *screens;	/* pointer to list of screens */
110	unsigned long motion_buffer;	/* size of motion buffer */
111	volatile unsigned long flags;	   /* internal connection flags */
112	int min_keycode;	/* minimum defined keycode */
113	int max_keycode;	/* maximum defined keycode */
114	KeySym *keysyms;	/* This server's keysyms */
115	XModifierKeymap *modifiermap;	/* This server's modifier keymap */
116	int keysyms_per_keycode;/* number of rows */
117	char *xdefaults;	/* contents of defaults from server */
118	char *scratch_buffer;	/* place to hang scratch buffer */
119	unsigned long scratch_length;	/* length of scratch buffer */
120	int ext_number;		/* extension number on this display */
121	struct _XExten *ext_procs; /* extensions initialized on this display */
122	/*
123	 * the following can be fixed size, as the protocol defines how
124	 * much address space is available.
125	 * While this could be done using the extension vector, there
126	 * may be MANY events processed, so a search through the extension
127	 * list to find the right procedure for each event might be
128	 * expensive if many extensions are being used.
129	 */
130	Bool (*event_vec[128])(	/* vector for wire to event */
131		Display *	/* dpy */,
132		XEvent *	/* re */,
133		xEvent *	/* event */
134		);
135	Status (*wire_vec[128])( /* vector for event to wire */
136		Display *	/* dpy */,
137		XEvent *	/* re */,
138		xEvent *	/* event */
139		);
140	KeySym lock_meaning;	   /* for XLookupString */
141	struct _XLockInfo *lock;   /* multi-thread state, display lock */
142	struct _XInternalAsync *async_handlers; /* for internal async */
143	unsigned long bigreq_size; /* max size of big requests */
144	struct _XLockPtrs *lock_fns; /* pointers to threads functions */
145	void (*idlist_alloc)(	   /* XID list allocator function */
146		Display *	/* dpy */,
147		XID *		/* ids */,
148		int		/* count */
149		);
150	/* things above this line should not move, for binary compatibility */
151	struct _XKeytrans *key_bindings; /* for XLookupString */
152	Font cursor_font;	   /* for XCreateFontCursor */
153	struct _XDisplayAtoms *atoms; /* for XInternAtom */
154	unsigned int mode_switch;  /* keyboard group modifiers */
155	unsigned int num_lock;  /* keyboard numlock modifiers */
156	struct _XContextDB *context_db; /* context database */
157	Bool (**error_vec)(	/* vector for wire to error */
158		Display     *	/* display */,
159		XErrorEvent *	/* he */,
160		xError      *	/* we */
161		);
162	/*
163	 * Xcms information
164	 */
165	struct {
166	   XPointer defaultCCCs;  /* pointer to an array of default XcmsCCC */
167	   XPointer clientCmaps;  /* pointer to linked list of XcmsCmapRec */
168	   XPointer perVisualIntensityMaps;
169				  /* linked list of XcmsIntensityMap */
170	} cms;
171	struct _XIMFilter *im_filters;
172	struct _XSQEvent *qfree; /* unallocated event queue elements */
173	unsigned long next_event_serial_num; /* inserted into next queue elt */
174	struct _XExten *flushes; /* Flush hooks */
175	struct _XConnectionInfo *im_fd_info; /* _XRegisterInternalConnection */
176	int im_fd_length;	/* number of im_fd_info */
177	struct _XConnWatchInfo *conn_watchers; /* XAddConnectionWatch */
178	int watcher_count;	/* number of conn_watchers */
179	XPointer filedes;	/* struct pollfd cache for _XWaitForReadable */
180	int (*savedsynchandler)( /* user synchandler when Xlib usurps */
181		Display *	/* dpy */
182		);
183	XID resource_max;	/* allocator max ID */
184	int xcmisc_opcode;	/* major opcode for XC-MISC */
185	struct _XkbInfoRec *xkb_info; /* XKB info */
186	struct _XtransConnInfo *trans_conn; /* transport connection object */
187	struct _X11XCBPrivate *xcb; /* XCB glue private data */
188};
189
190#define XAllocIDs(dpy,ids,n) (*(dpy)->idlist_alloc)(dpy,ids,n)
191
192/*
193 * define the following if you want the Data macro to be a procedure instead
194 */
195#ifdef CRAY
196#define DataRoutineIsProcedure
197#endif /* CRAY */
198
199#ifndef _XEVENT_
200/*
201 * _QEvent datatype for use in input queueing.
202 */
203typedef struct _XSQEvent
204{
205    struct _XSQEvent *next;
206    XEvent event;
207    unsigned long qserial_num;	/* so multi-threaded code can find new ones */
208} _XQEvent;
209#endif
210
211#ifdef XTHREADS			/* for xReply */
212#define NEED_REPLIES
213#endif
214
215#define NEED_EVENTS
216#define NEED_REPLIES
217#include <X11/Xproto.h>
218#ifdef __sgi
219#define _SGI_MP_SOURCE  /* turn this on to get MP safe errno */
220#endif
221#include <errno.h>
222#define _XBCOPYFUNC _Xbcopy
223#include <X11/Xfuncs.h>
224#include <X11/Xosdefs.h>
225
226/* Utek leaves kernel macros around in include files (bleah) */
227#ifdef dirty
228#undef dirty
229#endif
230
231#include <stdlib.h>
232#include <string.h>
233
234#include <X11/Xfuncproto.h>
235
236_XFUNCPROTOBEGIN
237
238/*
239 * The following definitions can be used for locking requests in multi-threaded
240 * address spaces.
241 */
242#ifdef XTHREADS
243/* Author: Stephen Gildea, MIT X Consortium
244 *
245 * declarations for C Threads locking
246 */
247
248typedef struct _LockInfoRec *LockInfoPtr;
249
250/* interfaces for locking.c */
251struct _XLockPtrs {
252    /* used by all, including extensions; do not move */
253    void (*lock_display)(
254		Display *dpy
255#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
256		, char *file
257		, int line
258#endif
259	);
260    void (*unlock_display)(
261		Display *dpy
262#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
263		, char *file
264		, int line
265#endif
266	);
267};
268
269#if defined(WIN32) && !defined(_XLIBINT_)
270#define _XCreateMutex_fn (*_XCreateMutex_fn_p)
271#define _XFreeMutex_fn (*_XFreeMutex_fn_p)
272#define _XLockMutex_fn (*_XLockMutex_fn_p)
273#define _XUnlockMutex_fn (*_XUnlockMutex_fn_p)
274#define _Xglobal_lock (*_Xglobal_lock_p)
275#endif
276
277/* in XlibInt.c */
278extern void (*_XCreateMutex_fn)(
279    LockInfoPtr /* lock */
280);
281extern void (*_XFreeMutex_fn)(
282    LockInfoPtr /* lock */
283);
284extern void (*_XLockMutex_fn)(
285    LockInfoPtr	/* lock */
286#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
287    , char * /* file */
288    , int /* line */
289#endif
290);
291extern void (*_XUnlockMutex_fn)(
292    LockInfoPtr	/* lock */
293#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
294    , char * /* file */
295    , int /* line */
296#endif
297);
298
299extern LockInfoPtr _Xglobal_lock;
300
301#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
302#define LockDisplay(d)	     if ((d)->lock_fns) (*(d)->lock_fns->lock_display)((d),__FILE__,__LINE__)
303#define UnlockDisplay(d)     if ((d)->lock_fns) (*(d)->lock_fns->unlock_display)((d),__FILE__,__LINE__)
304#define _XLockMutex(lock)		if (_XLockMutex_fn) (*_XLockMutex_fn)(lock,__FILE__,__LINE__)
305#define _XUnlockMutex(lock)	if (_XUnlockMutex_fn) (*_XUnlockMutex_fn)(lock,__FILE__,__LINE__)
306#else
307/* used everywhere, so must be fast if not using threads */
308#define LockDisplay(d)	     if ((d)->lock_fns) (*(d)->lock_fns->lock_display)(d)
309#define UnlockDisplay(d)     if ((d)->lock_fns) (*(d)->lock_fns->unlock_display)(d)
310#define _XLockMutex(lock)		if (_XLockMutex_fn) (*_XLockMutex_fn)(lock)
311#define _XUnlockMutex(lock)	if (_XUnlockMutex_fn) (*_XUnlockMutex_fn)(lock)
312#endif
313#define _XCreateMutex(lock)	if (_XCreateMutex_fn) (*_XCreateMutex_fn)(lock);
314#define _XFreeMutex(lock)	if (_XFreeMutex_fn) (*_XFreeMutex_fn)(lock);
315
316#else /* XTHREADS */
317#define LockDisplay(dis)
318#define _XLockMutex(lock)
319#define _XUnlockMutex(lock)
320#define UnlockDisplay(dis)
321#define _XCreateMutex(lock)
322#define _XFreeMutex(lock)
323#endif
324
325#define Xfree(ptr) free((ptr))
326
327/*
328 * Note that some machines do not return a valid pointer for malloc(0), in
329 * which case we provide an alternate under the control of the
330 * define MALLOC_0_RETURNS_NULL.  This is necessary because some
331 * Xlib code expects malloc(0) to return a valid pointer to storage.
332 */
333#ifdef MALLOC_0_RETURNS_NULL
334
335# define Xmalloc(size) malloc(((size) == 0 ? 1 : (size)))
336# define Xrealloc(ptr, size) realloc((ptr), ((size) == 0 ? 1 : (size)))
337# define Xcalloc(nelem, elsize) calloc(((nelem) == 0 ? 1 : (nelem)), (elsize))
338
339#else
340
341# define Xmalloc(size) malloc((size))
342# define Xrealloc(ptr, size) realloc((ptr), (size))
343# define Xcalloc(nelem, elsize) calloc((nelem), (elsize))
344
345#endif
346
347#include <stddef.h>
348
349#define LOCKED 1
350#define UNLOCKED 0
351
352#ifndef BUFSIZE
353#define BUFSIZE 2048			/* X output buffer size. */
354#endif
355#ifndef PTSPERBATCH
356#define PTSPERBATCH 1024		/* point batching */
357#endif
358#ifndef WLNSPERBATCH
359#define WLNSPERBATCH 50			/* wide line batching */
360#endif
361#ifndef ZLNSPERBATCH
362#define ZLNSPERBATCH 1024		/* thin line batching */
363#endif
364#ifndef WRCTSPERBATCH
365#define WRCTSPERBATCH 10		/* wide line rectangle batching */
366#endif
367#ifndef ZRCTSPERBATCH
368#define ZRCTSPERBATCH 256		/* thin line rectangle batching */
369#endif
370#ifndef FRCTSPERBATCH
371#define FRCTSPERBATCH 256		/* filled rectangle batching */
372#endif
373#ifndef FARCSPERBATCH
374#define FARCSPERBATCH 256		/* filled arc batching */
375#endif
376#ifndef CURSORFONT
377#define CURSORFONT "cursor"		/* standard cursor fonts */
378#endif
379
380/*
381 * Display flags
382 */
383#define XlibDisplayIOError	(1L << 0)
384#define XlibDisplayClosing	(1L << 1)
385#define XlibDisplayNoXkb	(1L << 2)
386#define XlibDisplayPrivSync	(1L << 3)
387#define XlibDisplayProcConni	(1L << 4) /* in _XProcessInternalConnection */
388#define XlibDisplayReadEvents	(1L << 5) /* in _XReadEvents */
389#define XlibDisplayReply	(1L << 5) /* in _XReply */
390#define XlibDisplayWriting	(1L << 6) /* in _XFlushInt, _XSend */
391#define XlibDisplayDfltRMDB     (1L << 7) /* mark if RM db from XGetDefault */
392
393/*
394 * X Protocol packetizing macros.
395 */
396
397/*   Need to start requests on 64 bit word boundaries
398 *   on a CRAY computer so add a NoOp (127) if needed.
399 *   A character pointer on a CRAY computer will be non-zero
400 *   after shifting right 61 bits of it is not pointing to
401 *   a word boundary.
402 */
403#ifdef WORD64
404#define WORD64ALIGN if ((long)dpy->bufptr >> 61) {\
405           dpy->last_req = dpy->bufptr;\
406           *(dpy->bufptr)   = X_NoOperation;\
407           *(dpy->bufptr+1) =  0;\
408           *(dpy->bufptr+2) =  0;\
409           *(dpy->bufptr+3) =  1;\
410             dpy->request++;\
411             dpy->bufptr += 4;\
412         }
413#else /* else does not require alignment on 64-bit boundaries */
414#define WORD64ALIGN
415#endif /* WORD64 */
416
417
418/*
419 * GetReq - Get the next available X request packet in the buffer and
420 * return it.
421 *
422 * "name" is the name of the request, e.g. CreatePixmap, OpenFont, etc.
423 * "req" is the name of the request pointer.
424 *
425 */
426
427#if !defined(UNIXCPP) || defined(ANSICPP)
428#define GetReq(name, req) \
429        WORD64ALIGN\
430	if ((dpy->bufptr + SIZEOF(x##name##Req)) > dpy->bufmax)\
431		_XFlush(dpy);\
432	req = (x##name##Req *)(dpy->last_req = dpy->bufptr);\
433	req->reqType = X_##name;\
434	req->length = (SIZEOF(x##name##Req))>>2;\
435	dpy->bufptr += SIZEOF(x##name##Req);\
436	dpy->request++
437
438#else  /* non-ANSI C uses empty comment instead of "##" for token concatenation */
439#define GetReq(name, req) \
440        WORD64ALIGN\
441	if ((dpy->bufptr + SIZEOF(x/**/name/**/Req)) > dpy->bufmax)\
442		_XFlush(dpy);\
443	req = (x/**/name/**/Req *)(dpy->last_req = dpy->bufptr);\
444	req->reqType = X_/**/name;\
445	req->length = (SIZEOF(x/**/name/**/Req))>>2;\
446	dpy->bufptr += SIZEOF(x/**/name/**/Req);\
447	dpy->request++
448#endif
449
450/* GetReqExtra is the same as GetReq, but allocates "n" additional
451   bytes after the request. "n" must be a multiple of 4!  */
452
453#if !defined(UNIXCPP) || defined(ANSICPP)
454#define GetReqExtra(name, n, req) \
455        WORD64ALIGN\
456	if ((dpy->bufptr + SIZEOF(x##name##Req) + n) > dpy->bufmax)\
457		_XFlush(dpy);\
458	req = (x##name##Req *)(dpy->last_req = dpy->bufptr);\
459	req->reqType = X_##name;\
460	req->length = (SIZEOF(x##name##Req) + n)>>2;\
461	dpy->bufptr += SIZEOF(x##name##Req) + n;\
462	dpy->request++
463#else
464#define GetReqExtra(name, n, req) \
465        WORD64ALIGN\
466	if ((dpy->bufptr + SIZEOF(x/**/name/**/Req) + n) > dpy->bufmax)\
467		_XFlush(dpy);\
468	req = (x/**/name/**/Req *)(dpy->last_req = dpy->bufptr);\
469	req->reqType = X_/**/name;\
470	req->length = (SIZEOF(x/**/name/**/Req) + n)>>2;\
471	dpy->bufptr += SIZEOF(x/**/name/**/Req) + n;\
472	dpy->request++
473#endif
474
475
476/*
477 * GetResReq is for those requests that have a resource ID
478 * (Window, Pixmap, GContext, etc.) as their single argument.
479 * "rid" is the name of the resource.
480 */
481
482#if !defined(UNIXCPP) || defined(ANSICPP)
483#define GetResReq(name, rid, req) \
484        WORD64ALIGN\
485	if ((dpy->bufptr + SIZEOF(xResourceReq)) > dpy->bufmax)\
486	    _XFlush(dpy);\
487	req = (xResourceReq *) (dpy->last_req = dpy->bufptr);\
488	req->reqType = X_##name;\
489	req->length = 2;\
490	req->id = (rid);\
491	dpy->bufptr += SIZEOF(xResourceReq);\
492	dpy->request++
493#else
494#define GetResReq(name, rid, req) \
495        WORD64ALIGN\
496	if ((dpy->bufptr + SIZEOF(xResourceReq)) > dpy->bufmax)\
497	    _XFlush(dpy);\
498	req = (xResourceReq *) (dpy->last_req = dpy->bufptr);\
499	req->reqType = X_/**/name;\
500	req->length = 2;\
501	req->id = (rid);\
502	dpy->bufptr += SIZEOF(xResourceReq);\
503	dpy->request++
504#endif
505
506/*
507 * GetEmptyReq is for those requests that have no arguments
508 * at all.
509 */
510#if !defined(UNIXCPP) || defined(ANSICPP)
511#define GetEmptyReq(name, req) \
512        WORD64ALIGN\
513	if ((dpy->bufptr + SIZEOF(xReq)) > dpy->bufmax)\
514	    _XFlush(dpy);\
515	req = (xReq *) (dpy->last_req = dpy->bufptr);\
516	req->reqType = X_##name;\
517	req->length = 1;\
518	dpy->bufptr += SIZEOF(xReq);\
519	dpy->request++
520#else
521#define GetEmptyReq(name, req) \
522        WORD64ALIGN\
523	if ((dpy->bufptr + SIZEOF(xReq)) > dpy->bufmax)\
524	    _XFlush(dpy);\
525	req = (xReq *) (dpy->last_req = dpy->bufptr);\
526	req->reqType = X_/**/name;\
527	req->length = 1;\
528	dpy->bufptr += SIZEOF(xReq);\
529	dpy->request++
530#endif
531
532#ifdef WORD64
533#define MakeBigReq(req,n) \
534    { \
535    char _BRdat[4]; \
536    unsigned long _BRlen = req->length - 1; \
537    req->length = 0; \
538    memcpy(_BRdat, ((char *)req) + (_BRlen << 2), 4); \
539    memmove(((char *)req) + 8, ((char *)req) + 4, _BRlen << 2); \
540    memcpy(((char *)req) + 4, _BRdat, 4); \
541    Data32(dpy, (long *)&_BRdat, 4); \
542    }
543#else
544#ifdef LONG64
545#define MakeBigReq(req,n) \
546    { \
547    CARD64 _BRdat; \
548    CARD32 _BRlen = req->length - 1; \
549    req->length = 0; \
550    _BRdat = ((CARD32 *)req)[_BRlen]; \
551    memmove(((char *)req) + 8, ((char *)req) + 4, _BRlen << 2); \
552    ((CARD32 *)req)[1] = _BRlen + n + 2; \
553    Data32(dpy, &_BRdat, 4); \
554    }
555#else
556#define MakeBigReq(req,n) \
557    { \
558    CARD32 _BRdat; \
559    CARD32 _BRlen = req->length - 1; \
560    req->length = 0; \
561    _BRdat = ((CARD32 *)req)[_BRlen]; \
562    memmove(((char *)req) + 8, ((char *)req) + 4, _BRlen << 2); \
563    ((CARD32 *)req)[1] = _BRlen + n + 2; \
564    Data32(dpy, &_BRdat, 4); \
565    }
566#endif
567#endif
568
569#define SetReqLen(req,n,badlen) \
570    if ((req->length + n) > (unsigned)65535) { \
571	if (dpy->bigreq_size) { \
572	    MakeBigReq(req,n) \
573	} else { \
574	    n = badlen; \
575	    req->length += n; \
576	} \
577    } else \
578	req->length += n
579
580#define SyncHandle() \
581	if (dpy->synchandler) (*dpy->synchandler)(dpy)
582
583extern void _XFlushGCCache(Display *dpy, GC gc);
584#define FlushGC(dpy, gc) \
585	if ((gc)->dirty) _XFlushGCCache((dpy), (gc))
586/*
587 * Data - Place data in the buffer and pad the end to provide
588 * 32 bit word alignment.  Transmit if the buffer fills.
589 *
590 * "dpy" is a pointer to a Display.
591 * "data" is a pinter to a data buffer.
592 * "len" is the length of the data buffer.
593 */
594#ifndef DataRoutineIsProcedure
595#define Data(dpy, data, len) {\
596	if (dpy->bufptr + (len) <= dpy->bufmax) {\
597		memcpy(dpy->bufptr, data, (int)len);\
598		dpy->bufptr += ((len) + 3) & ~3;\
599	} else\
600		_XSend(dpy, data, len);\
601	}
602#endif /* DataRoutineIsProcedure */
603
604
605/* Allocate bytes from the buffer.  No padding is done, so if
606 * the length is not a multiple of 4, the caller must be
607 * careful to leave the buffer aligned after sending the
608 * current request.
609 *
610 * "type" is the type of the pointer being assigned to.
611 * "ptr" is the pointer being assigned to.
612 * "n" is the number of bytes to allocate.
613 *
614 * Example:
615 *    xTextElt *elt;
616 *    BufAlloc (xTextElt *, elt, nbytes)
617 */
618
619#define BufAlloc(type, ptr, n) \
620    if (dpy->bufptr + (n) > dpy->bufmax) \
621        _XFlush (dpy); \
622    ptr = (type) dpy->bufptr; \
623    (void)ptr; \
624    dpy->bufptr += (n);
625
626#ifdef WORD64
627#define Data16(dpy, data, len) _XData16(dpy, (short *)data, len)
628#define Data32(dpy, data, len) _XData32(dpy, (long *)data, len)
629#else
630#define Data16(dpy, data, len) Data((dpy), (char *)(data), (len))
631#define _XRead16Pad(dpy, data, len) _XReadPad((dpy), (char *)(data), (len))
632#define _XRead16(dpy, data, len) _XRead((dpy), (char *)(data), (len))
633#ifdef LONG64
634#define Data32(dpy, data, len) _XData32(dpy, (long *)data, len)
635extern int _XData32(
636	     Display *dpy,
637	     register long *data,
638	     unsigned len
639);
640extern void _XRead32(
641	     Display *dpy,
642	     register long *data,
643	     long len
644);
645#else
646#define Data32(dpy, data, len) Data((dpy), (char *)(data), (len))
647#define _XRead32(dpy, data, len) _XRead((dpy), (char *)(data), (len))
648#endif
649#endif /* not WORD64 */
650
651#define PackData16(dpy,data,len) Data16 (dpy, data, len)
652#define PackData32(dpy,data,len) Data32 (dpy, data, len)
653
654/* Xlib manual is bogus */
655#define PackData(dpy,data,len) PackData16 (dpy, data, len)
656
657#define min(a,b) (((a) < (b)) ? (a) : (b))
658#define max(a,b) (((a) > (b)) ? (a) : (b))
659
660#define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
661			     (((cs)->rbearing|(cs)->lbearing| \
662			       (cs)->ascent|(cs)->descent) == 0))
663
664/*
665 * CI_GET_CHAR_INFO_1D - return the charinfo struct for the indicated 8bit
666 * character.  If the character is in the column and exists, then return the
667 * appropriate metrics (note that fonts with common per-character metrics will
668 * return min_bounds).  If none of these hold true, try again with the default
669 * char.
670 */
671#define CI_GET_CHAR_INFO_1D(fs,col,def,cs) \
672{ \
673    cs = def; \
674    if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
675	if (fs->per_char == NULL) { \
676	    cs = &fs->min_bounds; \
677	} else { \
678	    cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
679	    if (CI_NONEXISTCHAR(cs)) cs = def; \
680	} \
681    } \
682}
683
684#define CI_GET_DEFAULT_INFO_1D(fs,cs) \
685  CI_GET_CHAR_INFO_1D (fs, fs->default_char, NULL, cs)
686
687
688
689/*
690 * CI_GET_CHAR_INFO_2D - return the charinfo struct for the indicated row and
691 * column.  This is used for fonts that have more than row zero.
692 */
693#define CI_GET_CHAR_INFO_2D(fs,row,col,def,cs) \
694{ \
695    cs = def; \
696    if (row >= fs->min_byte1 && row <= fs->max_byte1 && \
697	col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
698	if (fs->per_char == NULL) { \
699	    cs = &fs->min_bounds; \
700	} else { \
701	    cs = &fs->per_char[((row - fs->min_byte1) * \
702			        (fs->max_char_or_byte2 - \
703				 fs->min_char_or_byte2 + 1)) + \
704			       (col - fs->min_char_or_byte2)]; \
705	    if (CI_NONEXISTCHAR(cs)) cs = def; \
706        } \
707    } \
708}
709
710#define CI_GET_DEFAULT_INFO_2D(fs,cs) \
711{ \
712    unsigned int r = (fs->default_char >> 8); \
713    unsigned int c = (fs->default_char & 0xff); \
714    CI_GET_CHAR_INFO_2D (fs, r, c, NULL, cs); \
715}
716
717
718#ifdef MUSTCOPY
719
720/* for when 32-bit alignment is not good enough */
721#define OneDataCard32(dpy,dstaddr,srcvar) \
722  { dpy->bufptr -= 4; Data32 (dpy, (char *) &(srcvar), 4); }
723
724#else
725
726/* srcvar must be a variable for large architecture version */
727#define OneDataCard32(dpy,dstaddr,srcvar) \
728  { *(CARD32 *)(dstaddr) = (srcvar); }
729
730#endif /* MUSTCOPY */
731
732typedef struct _XInternalAsync {
733    struct _XInternalAsync *next;
734    /*
735     * handler arguments:
736     * rep is the generic reply that caused this handler
737     * to be invoked.  It must also be passed to _XGetAsyncReply.
738     * buf and len are opaque values that must be passed to
739     * _XGetAsyncReply or _XGetAsyncData.
740     * data is the closure stored in this struct.
741     * The handler returns True iff it handled this reply.
742     */
743    Bool (*handler)(
744		    Display*	/* dpy */,
745		    xReply*	/* rep */,
746		    char*	/* buf */,
747		    int		/* len */,
748		    XPointer	/* data */
749		    );
750    XPointer data;
751} _XAsyncHandler;
752
753typedef struct _XAsyncEState {
754    unsigned long min_sequence_number;
755    unsigned long max_sequence_number;
756    unsigned char error_code;
757    unsigned char major_opcode;
758    unsigned short minor_opcode;
759    unsigned char last_error_received;
760    int error_count;
761} _XAsyncErrorState;
762
763extern void _XDeqAsyncHandler(Display *dpy, _XAsyncHandler *handler);
764#define DeqAsyncHandler(dpy,handler) { \
765    if (dpy->async_handlers == (handler)) \
766	dpy->async_handlers = (handler)->next; \
767    else \
768	_XDeqAsyncHandler(dpy, handler); \
769    }
770
771typedef void (*FreeFuncType) (
772    Display*	/* display */
773);
774
775typedef int (*FreeModmapType) (
776    XModifierKeymap*	/* modmap */
777);
778
779/*
780 * This structure is private to the library.
781 */
782typedef struct _XFreeFuncs {
783    FreeFuncType atoms;		/* _XFreeAtomTable */
784    FreeModmapType modifiermap;	/* XFreeModifierMap */
785    FreeFuncType key_bindings;	/* _XFreeKeyBindings */
786    FreeFuncType context_db;	/* _XFreeContextDB */
787    FreeFuncType defaultCCCs;	/* _XcmsFreeDefaultCCCs */
788    FreeFuncType clientCmaps;	/* _XcmsFreeClientCmaps */
789    FreeFuncType intensityMaps;	/* _XcmsFreeIntensityMaps */
790    FreeFuncType im_filters;	/* _XFreeIMFilters */
791    FreeFuncType xkb;		/* _XkbFreeInfo */
792} _XFreeFuncRec;
793
794/* types for InitExt.c */
795typedef int (*CreateGCType) (
796    Display*	/* display */,
797    GC		/* gc */,
798    XExtCodes*	/* codes */
799);
800
801typedef int (*CopyGCType)(
802    Display*	/* display */,
803    GC		/* gc */,
804    XExtCodes*	/* codes */
805);
806
807typedef int (*FlushGCType) (
808    Display*	/* display */,
809    GC		/* gc */,
810    XExtCodes*	/* codes */
811);
812
813typedef int (*FreeGCType) (
814    Display*	/* display */,
815    GC		/* gc */,
816    XExtCodes*	/* codes */
817);
818
819typedef int (*CreateFontType) (
820    Display*	/* display */,
821    XFontStruct* /* fs */,
822    XExtCodes*	/* codes */
823);
824
825typedef int (*FreeFontType) (
826    Display*	/* display */,
827    XFontStruct* /* fs */,
828    XExtCodes*	/* codes */
829);
830
831typedef int (*CloseDisplayType) (
832    Display*	/* display */,
833    XExtCodes*	/* codes */
834);
835
836typedef int (*ErrorType) (
837    Display*	/* display */,
838    xError*	/* err */,
839    XExtCodes*	/* codes */,
840    int*	/* ret_code */
841);
842
843typedef char* (*ErrorStringType) (
844    Display*	/* display */,
845    int		/* code */,
846    XExtCodes*	/* codes */,
847    char*	/* buffer */,
848    int		/* nbytes */
849);
850
851typedef void (*PrintErrorType)(
852    Display*	/* display */,
853    XErrorEvent* /* ev */,
854    void*	/* fp */
855);
856
857typedef void (*BeforeFlushType)(
858    Display*	/* display */,
859    XExtCodes*	/* codes */,
860    _Xconst char* /* data */,
861    long	/* len */
862);
863
864/*
865 * This structure is private to the library.
866 */
867typedef struct _XExten {		/* private to extension mechanism */
868	struct _XExten *next;		/* next in list */
869	XExtCodes codes;		/* public information, all extension told */
870	CreateGCType create_GC;		/* routine to call when GC created */
871	CopyGCType copy_GC;		/* routine to call when GC copied */
872	FlushGCType flush_GC;		/* routine to call when GC flushed */
873	FreeGCType free_GC;		/* routine to call when GC freed */
874	CreateFontType create_Font;	/* routine to call when Font created */
875	FreeFontType free_Font;		/* routine to call when Font freed */
876	CloseDisplayType close_display;	/* routine to call when connection closed */
877	ErrorType error;		/* who to call when an error occurs */
878	ErrorStringType error_string;	/* routine to supply error string */
879	char *name;			/* name of this extension */
880	PrintErrorType error_values;	/* routine to supply error values */
881	BeforeFlushType before_flush;	/* routine to call when sending data */
882	struct _XExten *next_flush;	/* next in list of those with flushes */
883} _XExtension;
884
885/* extension hooks */
886
887#ifdef DataRoutineIsProcedure
888extern void Data(Display *dpy, char *data, long len);
889#endif
890extern int _XError(
891    Display*	/* dpy */,
892    xError*	/* rep */
893);
894extern int _XIOError(
895    Display*	/* dpy */
896);
897extern int (*_XIOErrorFunction)(
898    Display*	/* dpy */
899);
900extern int (*_XErrorFunction)(
901    Display*		/* dpy */,
902    XErrorEvent*	/* error_event */
903);
904extern void _XEatData(
905    Display*		/* dpy */,
906    unsigned long	/* n */
907);
908extern char *_XAllocScratch(
909    Display*		/* dpy */,
910    unsigned long	/* nbytes */
911);
912extern char *_XAllocTemp(
913    Display*		/* dpy */,
914    unsigned long	/* nbytes */
915);
916extern void _XFreeTemp(
917    Display*		/* dpy */,
918    char*		/* buf */,
919    unsigned long	/* nbytes */
920);
921extern Visual *_XVIDtoVisual(
922    Display*	/* dpy */,
923    VisualID	/* id */
924);
925extern unsigned long _XSetLastRequestRead(
926    Display*		/* dpy */,
927    xGenericReply*	/* rep */
928);
929extern int _XGetHostname(
930    char*	/* buf */,
931    int		/* maxlen */
932);
933extern Screen *_XScreenOfWindow(
934    Display*	/* dpy */,
935    Window	/* w */
936);
937extern Bool _XAsyncErrorHandler(
938    Display*	/* dpy */,
939    xReply*	/* rep */,
940    char*	/* buf */,
941    int		/* len */,
942    XPointer	/* data */
943);
944extern char *_XGetAsyncReply(
945    Display*	/* dpy */,
946    char*	/* replbuf */,
947    xReply*	/* rep */,
948    char*	/* buf */,
949    int		/* len */,
950    int		/* extra */,
951    Bool	/* discard */
952);
953extern void _XGetAsyncData(
954    Display*	/* dpy */,
955    char *	/* data */,
956    char *	/* buf */,
957    int		/* len */,
958    int		/* skip */,
959    int		/* datalen */,
960    int		/* discardtotal */
961);
962extern void _XFlush(
963    Display*	/* dpy */
964);
965extern int _XEventsQueued(
966    Display*	/* dpy */,
967    int 	/* mode */
968);
969extern void _XReadEvents(
970    Display*	/* dpy */
971);
972extern int _XRead(
973    Display*	/* dpy */,
974    char*	/* data */,
975    long	/* size */
976);
977extern void _XReadPad(
978    Display*	/* dpy */,
979    char*	/* data */,
980    long	/* size */
981);
982extern void _XSend(
983    Display*		/* dpy */,
984    _Xconst char*	/* data */,
985    long		/* size */
986);
987extern Status _XReply(
988    Display*	/* dpy */,
989    xReply*	/* rep */,
990    int		/* extra */,
991    Bool	/* discard */
992);
993extern void _XEnq(
994    Display*	/* dpy */,
995    xEvent*	/* event */
996);
997extern void _XDeq(
998    Display*	/* dpy */,
999    _XQEvent*	/* prev */,
1000    _XQEvent*	/* qelt */
1001);
1002
1003extern Bool _XUnknownWireEvent(
1004    Display*	/* dpy */,
1005    XEvent*	/* re */,
1006    xEvent*	/* event */
1007);
1008extern Status _XUnknownNativeEvent(
1009    Display*	/* dpy */,
1010    XEvent*	/* re */,
1011    xEvent*	/* event */
1012);
1013
1014extern Bool _XWireToEvent(Display *dpy, XEvent *re, xEvent *event);
1015extern Bool _XDefaultWireError(Display *display, XErrorEvent *he, xError *we);
1016extern Bool _XPollfdCacheInit(Display *dpy);
1017extern void _XPollfdCacheAdd(Display *dpy, int fd);
1018extern void _XPollfdCacheDel(Display *dpy, int fd);
1019extern XID _XAllocID(Display *dpy);
1020extern void _XAllocIDs(Display *dpy, XID *ids, int count);
1021
1022extern int _XFreeExtData(
1023    XExtData*	/* extension */
1024);
1025
1026extern int (*XESetCreateGC(
1027    Display*		/* display */,
1028    int			/* extension */,
1029    int (*) (
1030	      Display*			/* display */,
1031	      GC			/* gc */,
1032	      XExtCodes*		/* codes */
1033	    )		/* proc */
1034))(
1035    Display*, GC, XExtCodes*
1036);
1037
1038extern int (*XESetCopyGC(
1039    Display*		/* display */,
1040    int			/* extension */,
1041    int (*) (
1042	      Display*			/* display */,
1043              GC			/* gc */,
1044              XExtCodes*		/* codes */
1045            )		/* proc */
1046))(
1047    Display*, GC, XExtCodes*
1048);
1049
1050extern int (*XESetFlushGC(
1051    Display*		/* display */,
1052    int			/* extension */,
1053    int (*) (
1054	      Display*			/* display */,
1055              GC			/* gc */,
1056              XExtCodes*		/* codes */
1057            )		/* proc */
1058))(
1059    Display*, GC, XExtCodes*
1060);
1061
1062extern int (*XESetFreeGC(
1063    Display*		/* display */,
1064    int			/* extension */,
1065    int (*) (
1066	      Display*			/* display */,
1067              GC			/* gc */,
1068              XExtCodes*		/* codes */
1069            )		/* proc */
1070))(
1071    Display*, GC, XExtCodes*
1072);
1073
1074extern int (*XESetCreateFont(
1075    Display*		/* display */,
1076    int			/* extension */,
1077    int (*) (
1078	      Display*			/* display */,
1079              XFontStruct*		/* fs */,
1080              XExtCodes*		/* codes */
1081            )		/* proc */
1082))(
1083    Display*, XFontStruct*, XExtCodes*
1084);
1085
1086extern int (*XESetFreeFont(
1087    Display*		/* display */,
1088    int			/* extension */,
1089    int (*) (
1090	      Display*			/* display */,
1091              XFontStruct*		/* fs */,
1092              XExtCodes*		/* codes */
1093            )		/* proc */
1094))(
1095    Display*, XFontStruct*, XExtCodes*
1096);
1097
1098extern int (*XESetCloseDisplay(
1099    Display*		/* display */,
1100    int			/* extension */,
1101    int (*) (
1102	      Display*			/* display */,
1103              XExtCodes*		/* codes */
1104            )		/* proc */
1105))(
1106    Display*, XExtCodes*
1107);
1108
1109extern int (*XESetError(
1110    Display*		/* display */,
1111    int			/* extension */,
1112    int (*) (
1113	      Display*			/* display */,
1114              xError*			/* err */,
1115              XExtCodes*		/* codes */,
1116              int*			/* ret_code */
1117            )		/* proc */
1118))(
1119    Display*, xError*, XExtCodes*, int*
1120);
1121
1122extern char* (*XESetErrorString(
1123    Display*		/* display */,
1124    int			/* extension */,
1125    char* (*) (
1126	        Display*		/* display */,
1127                int			/* code */,
1128                XExtCodes*		/* codes */,
1129                char*			/* buffer */,
1130                int			/* nbytes */
1131              )		/* proc */
1132))(
1133    Display*, int, XExtCodes*, char*, int
1134);
1135
1136extern void (*XESetPrintErrorValues (
1137    Display*		/* display */,
1138    int			/* extension */,
1139    void (*)(
1140	      Display*			/* display */,
1141	      XErrorEvent*		/* ev */,
1142	      void*			/* fp */
1143	     )		/* proc */
1144))(
1145    Display*, XErrorEvent*, void*
1146);
1147
1148extern Bool (*XESetWireToEvent(
1149    Display*		/* display */,
1150    int			/* event_number */,
1151    Bool (*) (
1152	       Display*			/* display */,
1153               XEvent*			/* re */,
1154               xEvent*			/* event */
1155             )		/* proc */
1156))(
1157    Display*, XEvent*, xEvent*
1158);
1159
1160extern Status (*XESetEventToWire(
1161    Display*		/* display */,
1162    int			/* event_number */,
1163    Status (*) (
1164	      Display*			/* display */,
1165              XEvent*			/* re */,
1166              xEvent*			/* event */
1167            )		/* proc */
1168))(
1169    Display*, XEvent*, xEvent*
1170);
1171
1172extern Bool (*XESetWireToError(
1173    Display*		/* display */,
1174    int			/* error_number */,
1175    Bool (*) (
1176	       Display*			/* display */,
1177	       XErrorEvent*		/* he */,
1178	       xError*			/* we */
1179            )		/* proc */
1180))(
1181    Display*, XErrorEvent*, xError*
1182);
1183
1184extern void (*XESetBeforeFlush(
1185    Display*		/* display */,
1186    int			/* error_number */,
1187    void (*) (
1188	       Display*			/* display */,
1189	       XExtCodes*		/* codes */,
1190	       _Xconst char*		/* data */,
1191	       long			/* len */
1192            )		/* proc */
1193))(
1194    Display*, XExtCodes*, _Xconst char*, long
1195);
1196
1197/* internal connections for IMs */
1198
1199typedef void (*_XInternalConnectionProc)(
1200    Display*			/* dpy */,
1201    int				/* fd */,
1202    XPointer			/* call_data */
1203);
1204
1205
1206extern Status _XRegisterInternalConnection(
1207    Display*			/* dpy */,
1208    int				/* fd */,
1209    _XInternalConnectionProc	/* callback */,
1210    XPointer			/* call_data */
1211);
1212
1213extern void _XUnregisterInternalConnection(
1214    Display*			/* dpy */,
1215    int				/* fd */
1216);
1217
1218extern void _XProcessInternalConnection(
1219    Display*			/* dpy */,
1220    struct _XConnectionInfo*	/* conn_info */
1221);
1222
1223/* Display structure has pointers to these */
1224
1225struct _XConnectionInfo {	/* info from _XRegisterInternalConnection */
1226    int fd;
1227    _XInternalConnectionProc read_callback;
1228    XPointer call_data;
1229    XPointer *watch_data;	/* set/used by XConnectionWatchProc */
1230    struct _XConnectionInfo *next;
1231};
1232
1233struct _XConnWatchInfo {	/* info from XAddConnectionWatch */
1234    XConnectionWatchProc fn;
1235    XPointer client_data;
1236    struct _XConnWatchInfo *next;
1237};
1238
1239#ifdef __UNIXOS2__
1240extern char* __XOS2RedirRoot(
1241    char*
1242);
1243#endif
1244
1245extern int _XTextHeight(
1246    XFontStruct*	/* font_struct */,
1247    _Xconst char*	/* string */,
1248    int			/* count */
1249);
1250
1251extern int _XTextHeight16(
1252    XFontStruct*	/* font_struct */,
1253    _Xconst XChar2b*	/* string */,
1254    int			/* count */
1255);
1256
1257#if defined(WIN32)
1258
1259extern int _XOpenFile(
1260    _Xconst char*	/* path */,
1261    int			/* flags */
1262);
1263
1264extern int _XOpenFileMode(
1265    _Xconst char*	/* path */,
1266    int			/* flags */,
1267    mode_t              /* mode */
1268);
1269
1270extern void* _XFopenFile(
1271    _Xconst char*	/* path */,
1272    _Xconst char*	/* mode */
1273);
1274
1275extern int _XAccessFile(
1276    _Xconst char*	/* path */
1277);
1278#else
1279#define _XOpenFile(path,flags) open(path,flags)
1280#define _XOpenFileMode(path,flags,mode) open(path,flags,mode)
1281#define _XFopenFile(path,mode) fopen(path,mode)
1282#endif
1283
1284/* EvToWire.c */
1285extern Status _XEventToWire(Display *dpy, XEvent *re, xEvent *event);
1286
1287extern int _XF86LoadQueryLocaleFont(
1288    Display*		/* dpy */,
1289    _Xconst char*	/* name*/,
1290    XFontStruct**	/* xfp*/,
1291    Font*		/* fidp */
1292);
1293
1294extern void _XProcessWindowAttributes (
1295    register Display *dpy,
1296    xChangeWindowAttributesReq *req,
1297    register unsigned long valuemask,
1298    register XSetWindowAttributes *attributes);
1299
1300extern int _XDefaultError(
1301        Display *dpy,
1302        XErrorEvent *event);
1303
1304extern int _XDefaultIOError(
1305        Display *dpy);
1306
1307extern void _XSetClipRectangles (
1308    register Display *dpy,
1309    GC gc,
1310    int clip_x_origin, int clip_y_origin,
1311    XRectangle *rectangles,
1312    int n,
1313    int ordering);
1314
1315Status _XGetWindowAttributes(
1316    register Display *dpy,
1317    Window w,
1318    XWindowAttributes *attr);
1319
1320int _XPutBackEvent (
1321    register Display *dpy,
1322    register XEvent *event);
1323
1324_XFUNCPROTOEND
1325
1326#endif /* _XLIBINT_H_ */
1327