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