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