OpenDis.c revision 2e9c7c8c
11ab64890Smrg/* $Xorg: OpenDis.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1985, 1986, 1998  The Open Group
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
81ab64890Smrgthe above copyright notice appear in all copies and that both that
91ab64890Smrgcopyright notice and this permission notice appear in supporting
101ab64890Smrgdocumentation.
111ab64890Smrg
121ab64890SmrgThe above copyright notice and this permission notice shall be included in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg*/
271ab64890Smrg/* $XFree86: xc/lib/X11/OpenDis.c,v 3.16 2003/07/04 16:24:23 eich Exp $ */
281ab64890Smrg
291ab64890Smrg#define NEED_REPLIES
301ab64890Smrg#define NEED_EVENTS
311ab64890Smrg#ifdef HAVE_CONFIG_H
321ab64890Smrg#include <config.h>
331ab64890Smrg#endif
341ab64890Smrg#include "Xlibint.h"
351ab64890Smrg#if USE_XCB
361ab64890Smrg#include "Xxcbint.h"
371ab64890Smrg#else /* !USE_XCB */
381ab64890Smrg#include <X11/Xtrans/Xtrans.h>
391ab64890Smrg#include <X11/extensions/bigreqstr.h>
401ab64890Smrg#endif /* USE_XCB */
411ab64890Smrg#include <X11/Xatom.h>
421ab64890Smrg#include <X11/Xresource.h>
431ab64890Smrg#include <stdio.h>
441ab64890Smrg#include "Xintconn.h"
451ab64890Smrg
461ab64890Smrg#ifdef XKB
471ab64890Smrg#include "XKBlib.h"
481ab64890Smrg#endif /* XKB */
491ab64890Smrg
501ab64890Smrg#if !USE_XCB
511ab64890Smrg#ifdef X_NOT_POSIX
521ab64890Smrg#define Size_t unsigned int
531ab64890Smrg#else
541ab64890Smrg#define Size_t size_t
551ab64890Smrg#endif
561ab64890Smrg
571ab64890Smrg#define bignamelen (sizeof(XBigReqExtensionName) - 1)
581ab64890Smrg
591ab64890Smrgtypedef struct {
601ab64890Smrg    unsigned long seq;
611ab64890Smrg    int opcode;
621ab64890Smrg} _XBigReqState;
631ab64890Smrg#endif /* !USE_XCB */
641ab64890Smrg
651ab64890Smrg#ifdef XTHREADS
661ab64890Smrg#include "locking.h"
671ab64890Smrgint  (*_XInitDisplayLock_fn)(Display *dpy) = NULL;
681ab64890Smrgvoid (*_XFreeDisplayLock_fn)(Display *dpy) = NULL;
691ab64890Smrg
701ab64890Smrg#define InitDisplayLock(d)	(_XInitDisplayLock_fn ? (*_XInitDisplayLock_fn)(d) : Success)
711ab64890Smrg#define FreeDisplayLock(d)	if (_XFreeDisplayLock_fn) (*_XFreeDisplayLock_fn)(d)
721ab64890Smrg#else
731ab64890Smrg#define InitDisplayLock(dis) Success
741ab64890Smrg#define FreeDisplayLock(dis)
751ab64890Smrg#endif /* XTHREADS */
761ab64890Smrg
771ab64890Smrgstatic xReq _dummy_request = {
781ab64890Smrg	0, 0, 0
791ab64890Smrg};
801ab64890Smrg
811ab64890Smrgstatic void OutOfMemory(Display *dpy, char *setup);
821ab64890Smrg#if !USE_XCB
831ab64890Smrgstatic Bool _XBigReqHandler(Display *dpy, xReply *rep, char *buf, int len,
841ab64890Smrg				XPointer data);
851ab64890Smrg#endif /* !USE_XCB */
861ab64890Smrg
8761b2299dSmrg/*
881ab64890Smrg * Connects to a server, creates a Display object and returns a pointer to
891ab64890Smrg * the newly created Display back to the caller.
901ab64890Smrg */
911ab64890SmrgDisplay *
921ab64890SmrgXOpenDisplay (
931ab64890Smrg	register _Xconst char *display)
941ab64890Smrg{
951ab64890Smrg	register Display *dpy;		/* New Display object being created. */
961ab64890Smrg	register int i;
971ab64890Smrg	int j, k;			/* random iterator indexes */
981ab64890Smrg#if !USE_XCB
991ab64890Smrg	char *display_name;		/* pointer to display name */
1001ab64890Smrg	int endian;			/* to determine which endian. */
1011ab64890Smrg	xConnClientPrefix client;	/* client information */
1021ab64890Smrg	int idisplay;			/* display number */
1031ab64890Smrg	int prefixread = 0;             /* setup prefix already read? */
1041ab64890Smrg	char *conn_auth_name, *conn_auth_data;
1051ab64890Smrg	int conn_auth_namelen, conn_auth_datalen;
1061ab64890Smrg#endif /* !USE_XCB */
1071ab64890Smrg	char *setup = NULL;		/* memory allocated at startup */
1081ab64890Smrg	char *fullname = NULL;		/* expanded name of display */
1091ab64890Smrg	int iscreen;			/* screen number */
1101ab64890Smrg	xConnSetupPrefix prefix;	/* prefix information */
1111ab64890Smrg	int vendorlen;			/* length of vendor string */
1121ab64890Smrg	union {
1131ab64890Smrg		xConnSetup *setup;
1141ab64890Smrg		char *failure;
1151ab64890Smrg		char *vendor;
1161ab64890Smrg		xPixmapFormat *sf;
1171ab64890Smrg		xWindowRoot *rp;
1181ab64890Smrg		xDepth *dp;
1191ab64890Smrg		xVisualType *vp;
1201ab64890Smrg	} u;				/* proto data returned from server */
1211ab64890Smrg	long setuplength;	/* number of bytes in setup message */
1221ab64890Smrg	long usedbytes = 0;     /* number of bytes we have processed */
1231ab64890Smrg	unsigned long mask;
1241ab64890Smrg       long int conn_buf_size;
1251ab64890Smrg       char *xlib_buffer_size;
1261ab64890Smrg
1271ab64890Smrg#if !USE_XCB
1281ab64890Smrg	bzero((char *) &client, sizeof(client));
1291ab64890Smrg	bzero((char *) &prefix, sizeof(prefix));
1301ab64890Smrg
1311ab64890Smrg	/*
13261b2299dSmrg	 * If the display specifier string supplied as an argument to this
1331ab64890Smrg	 * routine is NULL or a pointer to NULL, read the DISPLAY variable.
1341ab64890Smrg	 */
1351ab64890Smrg	if (display == NULL || *display == '\0') {
1361ab64890Smrg		if ((display_name = getenv("DISPLAY")) == NULL) {
1371ab64890Smrg			/* Oops! No DISPLAY environment variable - error. */
1381ab64890Smrg			return(NULL);
1391ab64890Smrg		}
1401ab64890Smrg	}
1411ab64890Smrg	else {
1421ab64890Smrg		/* Display is non-NULL, copy the pointer */
1431ab64890Smrg		display_name = (char *)display;
1441ab64890Smrg	}
1451ab64890Smrg#endif /* !USE_XCB */
1461ab64890Smrg
1471ab64890Smrg/*
1481ab64890Smrg * Set the default error handlers.  This allows the global variables to
1491ab64890Smrg * default to NULL for use with shared libraries.
1501ab64890Smrg */
1511ab64890Smrg	if (_XErrorFunction == NULL) (void) XSetErrorHandler (NULL);
1521ab64890Smrg	if (_XIOErrorFunction == NULL) (void) XSetIOErrorHandler (NULL);
1531ab64890Smrg
1541ab64890Smrg/*
1551ab64890Smrg * Attempt to allocate a display structure. Return NULL if allocation fails.
1561ab64890Smrg */
1571ab64890Smrg	if ((dpy = (Display *)Xcalloc(1, sizeof(Display))) == NULL) {
1581ab64890Smrg		return(NULL);
1591ab64890Smrg	}
1601ab64890Smrg
1611ab64890Smrg/*
1621ab64890Smrg * Call the Connect routine to get the transport connection object.
1631ab64890Smrg * If NULL is returned, the connection failed. The connect routine
1641ab64890Smrg * will set fullname to point to the expanded name.
1651ab64890Smrg */
1661ab64890Smrg
1671ab64890Smrg#if USE_XCB
1681ab64890Smrg	if(!_XConnectXCB(dpy, display, &fullname, &iscreen)) {
16961b2299dSmrg		dpy->display_name = fullname;
1702e9c7c8cSmrg		OutOfMemory(dpy, NULL);
1712e9c7c8cSmrg		return NULL;
1721ab64890Smrg	}
1731ab64890Smrg#else /* !USE_XCB */
1741ab64890Smrg	if ((dpy->trans_conn = _X11TransConnectDisplay (
1751ab64890Smrg					 display_name, &fullname, &idisplay,
1761ab64890Smrg					 &iscreen, &conn_auth_name,
1771ab64890Smrg					 &conn_auth_namelen, &conn_auth_data,
1781ab64890Smrg					 &conn_auth_datalen)) == NULL) {
1791ab64890Smrg		Xfree ((char *) dpy);
1801ab64890Smrg		return(NULL);
1811ab64890Smrg	}
1821ab64890Smrg
1831ab64890Smrg	dpy->fd = _X11TransGetConnectionNumber (dpy->trans_conn);
1841ab64890Smrg#endif /* USE_XCB */
1851ab64890Smrg
1861ab64890Smrg	/* Initialize as much of the display structure as we can.
1871ab64890Smrg	 * Initialize pointers to NULL so that XFreeDisplayStructure will
1881ab64890Smrg	 * work if we run out of memory before we finish initializing.
1891ab64890Smrg	 */
1901ab64890Smrg	dpy->display_name	= fullname;
1911ab64890Smrg	dpy->keysyms		= (KeySym *) NULL;
1921ab64890Smrg	dpy->modifiermap	= NULL;
1931ab64890Smrg	dpy->lock_meaning	= NoSymbol;
1941ab64890Smrg	dpy->keysyms_per_keycode = 0;
1951ab64890Smrg	dpy->xdefaults		= (char *)NULL;
1961ab64890Smrg	dpy->scratch_length	= 0L;
1971ab64890Smrg	dpy->scratch_buffer	= NULL;
1981ab64890Smrg	dpy->key_bindings	= NULL;
1991ab64890Smrg	dpy->ext_procs		= (_XExtension *)NULL;
2001ab64890Smrg	dpy->ext_data		= (XExtData *)NULL;
2011ab64890Smrg	dpy->ext_number 	= 0;
2021ab64890Smrg	dpy->event_vec[X_Error] = _XUnknownWireEvent;
2031ab64890Smrg	dpy->event_vec[X_Reply] = _XUnknownWireEvent;
2041ab64890Smrg	dpy->wire_vec[X_Error]  = _XUnknownNativeEvent;
2051ab64890Smrg	dpy->wire_vec[X_Reply]  = _XUnknownNativeEvent;
2061ab64890Smrg	for (i = KeyPress; i < LASTEvent; i++) {
2071ab64890Smrg	    dpy->event_vec[i] 	= _XWireToEvent;
2081ab64890Smrg	    dpy->wire_vec[i] 	= NULL;
2091ab64890Smrg	}
2101ab64890Smrg	for (i = LASTEvent; i < 128; i++) {
2111ab64890Smrg	    dpy->event_vec[i] 	= _XUnknownWireEvent;
2121ab64890Smrg	    dpy->wire_vec[i] 	= _XUnknownNativeEvent;
2131ab64890Smrg	}
2141ab64890Smrg	dpy->resource_id	= 0;
2151ab64890Smrg	dpy->db 		= (struct _XrmHashBucketRec *)NULL;
2161ab64890Smrg	dpy->cursor_font	= None;
2171ab64890Smrg	dpy->flags		= 0;
2181ab64890Smrg	dpy->async_handlers	= NULL;
2191ab64890Smrg	dpy->screens		= NULL;
2201ab64890Smrg	dpy->vendor		= NULL;
2211ab64890Smrg	dpy->buffer		= NULL;
2221ab64890Smrg	dpy->atoms		= NULL;
2231ab64890Smrg	dpy->error_vec		= NULL;
2241ab64890Smrg	dpy->context_db		= NULL;
2251ab64890Smrg	dpy->free_funcs		= NULL;
2261ab64890Smrg	dpy->pixmap_format	= NULL;
2271ab64890Smrg	dpy->cms.clientCmaps	= NULL;
2281ab64890Smrg	dpy->cms.defaultCCCs	= NULL;
2291ab64890Smrg	dpy->cms.perVisualIntensityMaps = NULL;
2301ab64890Smrg	dpy->im_filters		= NULL;
2311ab64890Smrg 	dpy->bigreq_size	= 0;
2321ab64890Smrg	dpy->lock		= NULL;
2331ab64890Smrg	dpy->lock_fns		= NULL;
2341ab64890Smrg	dpy->qfree		= NULL;
2351ab64890Smrg	dpy->next_event_serial_num = 1;
2361ab64890Smrg	dpy->im_fd_info		= NULL;
2371ab64890Smrg	dpy->im_fd_length	= 0;
2381ab64890Smrg	dpy->conn_watchers	= NULL;
2391ab64890Smrg	dpy->watcher_count	= 0;
2401ab64890Smrg	dpy->filedes		= NULL;
2411ab64890Smrg	dpy->flushes		= NULL;
2421ab64890Smrg	dpy->xcmisc_opcode	= 0;
2431ab64890Smrg	dpy->xkb_info		= NULL;
2441ab64890Smrg
2451ab64890Smrg/*
2461ab64890Smrg * Setup other information in this display structure.
2471ab64890Smrg */
2481ab64890Smrg	dpy->vnumber = X_PROTOCOL;
2491ab64890Smrg	dpy->resource_alloc = _XAllocID;
2501ab64890Smrg	dpy->idlist_alloc = _XAllocIDs;
2511ab64890Smrg	dpy->synchandler = NULL;
2521ab64890Smrg	dpy->savedsynchandler = NULL;
2531ab64890Smrg	dpy->request = 0;
2541ab64890Smrg	dpy->last_request_read = 0;
2551ab64890Smrg	dpy->default_screen = iscreen;  /* Value returned by ConnectDisplay */
2561ab64890Smrg	dpy->last_req = (char *)&_dummy_request;
2571ab64890Smrg
2581ab64890Smrg	/* Initialize the display lock */
2591ab64890Smrg	if (InitDisplayLock(dpy) != 0) {
2601ab64890Smrg	        OutOfMemory (dpy, setup);
2611ab64890Smrg		return(NULL);
2621ab64890Smrg	}
2631ab64890Smrg
2641ab64890Smrg	if (!_XPollfdCacheInit(dpy)) {
2651ab64890Smrg	        OutOfMemory (dpy, setup);
2661ab64890Smrg		return(NULL);
26761b2299dSmrg	}
2681ab64890Smrg
2691ab64890Smrg	/* Set up the output buffers. */
2701ab64890Smrg#ifndef XLIBDEFAULTBUFSIZE
2711ab64890Smrg#define XLIBDEFAULTBUFSIZE 16384 /* 16k */
2721ab64890Smrg#endif
2731ab64890Smrg#ifndef XLIBMINBUFSIZE
2741ab64890Smrg#define XLIBMINBUFSIZE BUFSIZE /* old default buffer size */
2751ab64890Smrg#endif
2761ab64890Smrg    if ((xlib_buffer_size = getenv("XLIBBUFFERSIZE")) == NULL)
2771ab64890Smrg        conn_buf_size = XLIBDEFAULTBUFSIZE;
2781ab64890Smrg    else
2791ab64890Smrg        conn_buf_size = 1024 * strtol(xlib_buffer_size, NULL, 10);
2801ab64890Smrg    if (conn_buf_size < XLIBMINBUFSIZE)
2811ab64890Smrg        conn_buf_size = XLIBMINBUFSIZE;
2821ab64890Smrg
2831ab64890Smrg    if ((dpy->bufptr = dpy->buffer = Xcalloc(1, conn_buf_size)) == NULL) {
2841ab64890Smrg         OutOfMemory (dpy, setup);
2851ab64890Smrg         return(NULL);
2861ab64890Smrg    }
2871ab64890Smrg    dpy->bufmax = dpy->buffer + conn_buf_size;
28861b2299dSmrg#if USE_XCB
28961b2299dSmrg    dpy->xcb->real_bufmax = dpy->bufmax;
29061b2299dSmrg    dpy->bufmax = dpy->buffer;
29161b2299dSmrg#endif
29261b2299dSmrg
2931ab64890Smrg	/* Set up the input event queue and input event queue parameters. */
2941ab64890Smrg	dpy->head = dpy->tail = NULL;
2951ab64890Smrg	dpy->qlen = 0;
2961ab64890Smrg
2971ab64890Smrg	/* Set up free-function record */
2981ab64890Smrg	if ((dpy->free_funcs = (_XFreeFuncRec *)Xcalloc(1,
2991ab64890Smrg							sizeof(_XFreeFuncRec)))
3001ab64890Smrg	    == NULL) {
3011ab64890Smrg	    OutOfMemory (dpy, setup);
3021ab64890Smrg	    return(NULL);
3031ab64890Smrg	}
3041ab64890Smrg
3051ab64890Smrg#if !USE_XCB
3061ab64890Smrg/*
3071ab64890Smrg * The xConnClientPrefix describes the initial connection setup information
3081ab64890Smrg * and is followed by the authorization information.  Sites that are interested
30961b2299dSmrg * in security are strongly encouraged to use an authentication and
3101ab64890Smrg * authorization system such as Kerberos.
3111ab64890Smrg */
3121ab64890Smrg	endian = 1;
3131ab64890Smrg	if (*(char *) &endian)
3141ab64890Smrg	    client.byteOrder = '\154'; /* 'l' */
3151ab64890Smrg	else
3161ab64890Smrg	    client.byteOrder = '\102'; /* 'B' */
3171ab64890Smrg	client.majorVersion = X_PROTOCOL;
3181ab64890Smrg	client.minorVersion = X_PROTOCOL_REVISION;
3191ab64890Smrg	client.nbytesAuthProto = conn_auth_namelen;
3201ab64890Smrg	client.nbytesAuthString = conn_auth_datalen;
3211ab64890Smrg	prefixread = _XSendClientPrefix(dpy, &client,
3221ab64890Smrg					conn_auth_name, conn_auth_data,
3231ab64890Smrg					&prefix);
3241ab64890Smrg	if (prefixread < 0)
3251ab64890Smrg	{
3261ab64890Smrg	    _XDisconnectDisplay (dpy->trans_conn);
3271ab64890Smrg	    Xfree ((char *)dpy);
3281ab64890Smrg	    return(NULL);
3291ab64890Smrg	}
3301ab64890Smrg	if (conn_auth_name) Xfree(conn_auth_name);
3311ab64890Smrg	if (conn_auth_data) Xfree(conn_auth_data);
3321ab64890Smrg/*
3331ab64890Smrg * Now see if connection was accepted...
3341ab64890Smrg */
3351ab64890Smrg	/* these internal functions expect the display to be locked */
3361ab64890Smrg	LockDisplay(dpy);
3371ab64890Smrg
3381ab64890Smrg	if (prefixread == 0)
3391ab64890Smrg	    _XRead (dpy, (char *)&prefix,(long)SIZEOF(xConnSetupPrefix));
3401ab64890Smrg
3411ab64890Smrg	/* an Authenticate reply we weren't expecting? */
3421ab64890Smrg	if (prefix.success != xTrue && prefix.success != xFalse) {
3431ab64890Smrg	    fprintf (stderr,
3441ab64890Smrg      "Xlib: unexpected connection setup reply from server, type %d.\r\n",
3451ab64890Smrg		     prefix.success);
3461ab64890Smrg	    _XDisconnectDisplay (dpy->trans_conn);
3471ab64890Smrg	    Xfree ((char *)dpy);
3481ab64890Smrg	    return(NULL);
3491ab64890Smrg	}
3501ab64890Smrg
3511ab64890Smrg	if (prefix.majorVersion != X_PROTOCOL) {
3521ab64890Smrg	    /* XXX - printing messages marks a bad programming interface */
3531ab64890Smrg	    fprintf (stderr,
3541ab64890Smrg     "Xlib: client uses different protocol version (%d) than server (%d)!\r\n",
3551ab64890Smrg		     X_PROTOCOL, prefix.majorVersion);
3561ab64890Smrg	    _XDisconnectDisplay (dpy->trans_conn);
3571ab64890Smrg	    Xfree ((char *)dpy);
3581ab64890Smrg	    return(NULL);
3591ab64890Smrg	}
3601ab64890Smrg#endif /* !USE_XCB */
3611ab64890Smrg
3621ab64890Smrg#if USE_XCB
3631ab64890Smrg	{
3641ab64890Smrg		const struct xcb_setup_t *xcbsetup = xcb_get_setup(dpy->xcb->connection);
3651ab64890Smrg		memcpy(&prefix, xcbsetup, sizeof(prefix));
3661ab64890Smrg		setuplength = prefix.length << 2;
3671ab64890Smrg		setup = (char *) xcbsetup;
3681ab64890Smrg		setup += SIZEOF(xConnSetupPrefix);
3691ab64890Smrg		u.setup = (xConnSetup *) setup;
3701ab64890Smrg	}
3711ab64890Smrg#else /* !USE_XCB */
3721ab64890Smrg	setuplength = prefix.length << 2;
3731ab64890Smrg	if ( (u.setup = (xConnSetup *)
3741ab64890Smrg	      (setup =  Xmalloc ((unsigned) setuplength))) == NULL) {
3751ab64890Smrg		_XDisconnectDisplay (dpy->trans_conn);
3761ab64890Smrg		Xfree ((char *)dpy);
3771ab64890Smrg		return(NULL);
3781ab64890Smrg	}
3791ab64890Smrg	_XRead (dpy, (char *)u.setup, setuplength);
3801ab64890Smrg
3811ab64890Smrg/*
3821ab64890Smrg * If the connection was not accepted by the server due to problems,
3831ab64890Smrg * give error message to the user....
3841ab64890Smrg */
3851ab64890Smrg	if (prefix.success != xTrue) {
3861ab64890Smrg		/* XXX - printing messages marks a bad programming interface */
38761b2299dSmrg		fprintf (stderr,
3881ab64890Smrg		      "Xlib: connection to \"%s\" refused by server\r\nXlib: ",
3891ab64890Smrg			 fullname);
3901ab64890Smrg
3911ab64890Smrg		if (prefix.lengthReason > setuplength) {
3921ab64890Smrg		    fprintf (stderr, "Xlib: Broken initial reply: length of reason > length of packet\r\n");
3931ab64890Smrg		}else{
3941ab64890Smrg		    (void) fwrite (u.failure, (Size_t)sizeof(char),
3951ab64890Smrg			       (Size_t)prefix.lengthReason, stderr);
3961ab64890Smrg		    (void) fwrite ("\r\n", sizeof(char), 2, stderr);
3971ab64890Smrg		}
3981ab64890Smrg
3991ab64890Smrg		OutOfMemory(dpy, setup);
4001ab64890Smrg		return (NULL);
4011ab64890Smrg	}
4021ab64890Smrg#endif /* USE_XCB */
4031ab64890Smrg
4041ab64890Smrg/*
4051ab64890Smrg * Check if the reply was long enough to get any information out of it.
4061ab64890Smrg */
4071ab64890Smrg	usedbytes = sz_xConnSetup;
4081ab64890Smrg	if (setuplength < usedbytes ) {
4091ab64890Smrg	    fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
4101ab64890Smrg	    OutOfMemory(dpy, setup);
4111ab64890Smrg	    return (NULL);
4121ab64890Smrg	}
4131ab64890Smrg
4141ab64890Smrg/*
4151ab64890Smrg * We succeeded at authorization, so let us move the data into
4161ab64890Smrg * the display structure.
4171ab64890Smrg */
4181ab64890Smrg	dpy->proto_major_version= prefix.majorVersion;
4191ab64890Smrg	dpy->proto_minor_version= prefix.minorVersion;
4201ab64890Smrg	dpy->release 		= u.setup->release;
4211ab64890Smrg	dpy->resource_base	= u.setup->ridBase;
4221ab64890Smrg	dpy->resource_mask	= u.setup->ridMask;
4231ab64890Smrg	dpy->min_keycode	= u.setup->minKeyCode;
4241ab64890Smrg	dpy->max_keycode	= u.setup->maxKeyCode;
4251ab64890Smrg	dpy->motion_buffer	= u.setup->motionBufferSize;
4261ab64890Smrg	dpy->nformats		= u.setup->numFormats;
4271ab64890Smrg	dpy->nscreens		= u.setup->numRoots;
4281ab64890Smrg	dpy->byte_order		= u.setup->imageByteOrder;
4291ab64890Smrg	dpy->bitmap_unit	= u.setup->bitmapScanlineUnit;
4301ab64890Smrg	dpy->bitmap_pad		= u.setup->bitmapScanlinePad;
4311ab64890Smrg	dpy->bitmap_bit_order   = u.setup->bitmapBitOrder;
4321ab64890Smrg	dpy->max_request_size	= u.setup->maxRequestSize;
4331ab64890Smrg	mask = dpy->resource_mask;
4341ab64890Smrg	dpy->resource_shift	= 0;
4351ab64890Smrg	if (!mask)
4361ab64890Smrg	{
4371ab64890Smrg	    fprintf (stderr, "Xlib: connection to \"%s\" invalid setup\n",
4381ab64890Smrg		     fullname);
4391ab64890Smrg	    OutOfMemory(dpy, setup);
4401ab64890Smrg	    return (NULL);
4411ab64890Smrg	}
44261b2299dSmrg
4431ab64890Smrg	while (!(mask & 1)) {
4441ab64890Smrg	    dpy->resource_shift++;
4451ab64890Smrg	    mask = mask >> 1;
4461ab64890Smrg	}
4471ab64890Smrg	dpy->resource_max = (dpy->resource_mask >> dpy->resource_shift) - 5;
4481ab64890Smrg/*
4491ab64890Smrg * now extract the vendor string...  String must be null terminated,
4501ab64890Smrg * padded to multiple of 4 bytes.
4511ab64890Smrg */
4521ab64890Smrg	/* Check for a sane vendor string length */
4531ab64890Smrg	if (u.setup->nbytesVendor > 256) {
4541ab64890Smrg	    OutOfMemory(dpy, setup);
4551ab64890Smrg	    return (NULL);
4561ab64890Smrg	}
4571ab64890Smrg
4581ab64890Smrg	dpy->vendor = (char *) Xmalloc((unsigned) (u.setup->nbytesVendor + 1));
4591ab64890Smrg	if (dpy->vendor == NULL) {
4601ab64890Smrg	    OutOfMemory(dpy, setup);
4611ab64890Smrg	    return (NULL);
4621ab64890Smrg	}
4631ab64890Smrg	vendorlen = u.setup->nbytesVendor;
4641ab64890Smrg
4651ab64890Smrg/*
4661ab64890Smrg * validate setup length
4671ab64890Smrg */
4681ab64890Smrg	usedbytes += (vendorlen + 3) & ~3;
4691ab64890Smrg	if (setuplength < usedbytes) {
4701ab64890Smrg	    fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
4711ab64890Smrg	    OutOfMemory(dpy, setup);
4721ab64890Smrg	    return (NULL);
4731ab64890Smrg	}
4741ab64890Smrg
4751ab64890Smrg 	u.setup = (xConnSetup *) (((char *) u.setup) + sz_xConnSetup);
4761ab64890Smrg  	(void) strncpy(dpy->vendor, u.vendor, vendorlen);
4771ab64890Smrg	dpy->vendor[vendorlen] = '\0';
4781ab64890Smrg 	vendorlen = (vendorlen + 3) & ~3;	/* round up */
4791ab64890Smrg	u.vendor += vendorlen;
4801ab64890Smrg
4811ab64890Smrg/*
4821ab64890Smrg * Now iterate down setup information.....
4831ab64890Smrg */
48461b2299dSmrg	dpy->pixmap_format =
4851ab64890Smrg	    (ScreenFormat *)Xmalloc(
4861ab64890Smrg		(unsigned) (dpy->nformats *sizeof(ScreenFormat)));
4871ab64890Smrg	if (dpy->pixmap_format == NULL) {
4881ab64890Smrg	        OutOfMemory (dpy, setup);
4891ab64890Smrg		return(NULL);
4901ab64890Smrg	}
4911ab64890Smrg/*
4921ab64890Smrg * First decode the Z axis Screen format information.
4931ab64890Smrg */
4941ab64890Smrg	usedbytes += dpy->nformats * sz_xPixmapFormat;
4951ab64890Smrg
4961ab64890Smrg	if (setuplength < usedbytes) {
4971ab64890Smrg	    fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
4981ab64890Smrg	    OutOfMemory (dpy, setup);
4991ab64890Smrg	    return(NULL);
5001ab64890Smrg	}
5011ab64890Smrg
5021ab64890Smrg	for (i = 0; i < dpy->nformats; i++) {
5031ab64890Smrg	    register ScreenFormat *fmt = &dpy->pixmap_format[i];
5041ab64890Smrg	    fmt->depth = u.sf->depth;
5051ab64890Smrg	    fmt->bits_per_pixel = u.sf->bitsPerPixel;
5061ab64890Smrg	    fmt->scanline_pad = u.sf->scanLinePad;
5071ab64890Smrg	    fmt->ext_data = NULL;
5081ab64890Smrg	    u.sf = (xPixmapFormat *) (((char *) u.sf) + sz_xPixmapFormat);
5091ab64890Smrg	}
5101ab64890Smrg
5111ab64890Smrg/*
5121ab64890Smrg * next the Screen structures.
5131ab64890Smrg */
51461b2299dSmrg	dpy->screens =
5151ab64890Smrg	    (Screen *)Xmalloc((unsigned) dpy->nscreens*sizeof(Screen));
5161ab64890Smrg	if (dpy->screens == NULL) {
5171ab64890Smrg	        OutOfMemory (dpy, setup);
5181ab64890Smrg		return(NULL);
5191ab64890Smrg	}
5201ab64890Smrg
5211ab64890Smrg/*
5221ab64890Smrg * Now go deal with each screen structure.
5231ab64890Smrg */
5241ab64890Smrg	for (i = 0; i < dpy->nscreens; i++) {
5251ab64890Smrg	    register Screen *sp = &dpy->screens[i];
5261ab64890Smrg	    VisualID root_visualID;
5271ab64890Smrg
5281ab64890Smrg	    usedbytes += sz_xWindowRoot;
5291ab64890Smrg	    if (setuplength < usedbytes) {
5301ab64890Smrg		fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
5311ab64890Smrg		OutOfMemory (dpy, setup);
5321ab64890Smrg		return(NULL);
5331ab64890Smrg	    }
5341ab64890Smrg
53561b2299dSmrg	    root_visualID = u.rp->rootVisualID;
5361ab64890Smrg	    sp->display	    = dpy;
5371ab64890Smrg	    sp->root 	    = u.rp->windowId;
5381ab64890Smrg	    sp->cmap 	    = u.rp->defaultColormap;
5391ab64890Smrg	    sp->white_pixel = u.rp->whitePixel;
5401ab64890Smrg	    sp->black_pixel = u.rp->blackPixel;
5411ab64890Smrg	    sp->root_input_mask = u.rp->currentInputMask;
5421ab64890Smrg	    sp->width	    = u.rp->pixWidth;
5431ab64890Smrg	    sp->height	    = u.rp->pixHeight;
5441ab64890Smrg	    sp->mwidth	    = u.rp->mmWidth;
5451ab64890Smrg	    sp->mheight	    = u.rp->mmHeight;
5461ab64890Smrg	    sp->min_maps    = u.rp->minInstalledMaps;
5471ab64890Smrg	    sp->max_maps    = u.rp->maxInstalledMaps;
5481ab64890Smrg	    sp->backing_store= u.rp->backingStore;
5491ab64890Smrg	    sp->save_unders = u.rp->saveUnders;
5501ab64890Smrg	    sp->root_depth  = u.rp->rootDepth;
5511ab64890Smrg	    sp->ndepths	    = u.rp->nDepths;
5521ab64890Smrg	    sp->ext_data   = NULL;
5531ab64890Smrg	    u.rp = (xWindowRoot *) (((char *) u.rp) + sz_xWindowRoot);
5541ab64890Smrg/*
5551ab64890Smrg * lets set up the depth structures.
5561ab64890Smrg */
5571ab64890Smrg	    sp->depths = (Depth *)Xmalloc(
5581ab64890Smrg			(unsigned)sp->ndepths*sizeof(Depth));
5591ab64890Smrg	    if (sp->depths == NULL) {
5601ab64890Smrg		OutOfMemory (dpy, setup);
5611ab64890Smrg		return(NULL);
5621ab64890Smrg	    }
5631ab64890Smrg	    /*
5641ab64890Smrg	     * for all depths on this screen.
5651ab64890Smrg	     */
5661ab64890Smrg	    for (j = 0; j < sp->ndepths; j++) {
5671ab64890Smrg		Depth *dp = &sp->depths[j];
5681ab64890Smrg
5691ab64890Smrg		usedbytes += sz_xDepth;
5701ab64890Smrg		if (setuplength < usedbytes) {
5711ab64890Smrg		    fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
5721ab64890Smrg		    OutOfMemory (dpy, setup);
5731ab64890Smrg		    return(NULL);
5741ab64890Smrg		}
57561b2299dSmrg
5761ab64890Smrg		dp->depth = u.dp->depth;
5771ab64890Smrg		dp->nvisuals = u.dp->nVisuals;
5781ab64890Smrg		u.dp = (xDepth *) (((char *) u.dp) + sz_xDepth);
5791ab64890Smrg		if (dp->nvisuals > 0) {
58061b2299dSmrg		    dp->visuals =
5811ab64890Smrg		      (Visual *)Xmalloc((unsigned)dp->nvisuals*sizeof(Visual));
5821ab64890Smrg		    if (dp->visuals == NULL) {
5831ab64890Smrg			OutOfMemory (dpy, setup);
5841ab64890Smrg			return(NULL);
5851ab64890Smrg		    }
5861ab64890Smrg		    for (k = 0; k < dp->nvisuals; k++) {
5871ab64890Smrg			register Visual *vp = &dp->visuals[k];
5881ab64890Smrg
5891ab64890Smrg			usedbytes += sz_xVisualType;
5901ab64890Smrg			if (setuplength < usedbytes) {
5911ab64890Smrg			    fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
5921ab64890Smrg			    OutOfMemory (dpy, setup);
5931ab64890Smrg			    return(NULL);
5941ab64890Smrg			}
59561b2299dSmrg
5961ab64890Smrg			vp->visualid	= u.vp->visualID;
5971ab64890Smrg			vp->class	= u.vp->class;
5981ab64890Smrg			vp->bits_per_rgb= u.vp->bitsPerRGB;
5991ab64890Smrg			vp->map_entries	= u.vp->colormapEntries;
6001ab64890Smrg			vp->red_mask	= u.vp->redMask;
6011ab64890Smrg			vp->green_mask	= u.vp->greenMask;
6021ab64890Smrg			vp->blue_mask	= u.vp->blueMask;
6031ab64890Smrg			vp->ext_data	= NULL;
6041ab64890Smrg			u.vp = (xVisualType *) (((char *) u.vp) +
6051ab64890Smrg						sz_xVisualType);
6061ab64890Smrg		    }
6071ab64890Smrg		    if (dp->depth == 32 && getenv ("XLIB_SKIP_ARGB_VISUALS"))
6081ab64890Smrg		    {
6091ab64890Smrg			Xfree (dp->visuals);
6101ab64890Smrg			dp->visuals = NULL;
6111ab64890Smrg			dp->nvisuals = 0;
6121ab64890Smrg		    }
6131ab64890Smrg		} else {
6141ab64890Smrg		    dp->visuals = (Visual *) NULL;
6151ab64890Smrg		}
6161ab64890Smrg	    }
6171ab64890Smrg	    sp->root_visual = _XVIDtoVisual(dpy, root_visualID);
6181ab64890Smrg	}
6191ab64890Smrg
6201ab64890Smrg	if(usedbytes != setuplength){
6211ab64890Smrg	    /* Sanity check, shouldn't happen. */
6221ab64890Smrg	    fprintf(stderr, "Xlib: Did not parse entire setup message: "
6231ab64890Smrg	                    "parsed: %ld, message: %ld\n",
6241ab64890Smrg		    usedbytes, setuplength);
6251ab64890Smrg	    OutOfMemory(dpy, setup);
6261ab64890Smrg	    return(NULL);
6271ab64890Smrg	}
6281ab64890Smrg
6291ab64890Smrg/*
6301ab64890Smrg * Now start talking to the server to setup all other information...
6311ab64890Smrg */
6321ab64890Smrg
6331ab64890Smrg#if !USE_XCB
6341ab64890Smrg	Xfree (setup);	/* all finished with setup information */
6351ab64890Smrg#endif /* !USE_XCB */
6361ab64890Smrg
6371ab64890Smrg/*
6381ab64890Smrg * Make sure default screen is legal.
6391ab64890Smrg */
6401ab64890Smrg	if (iscreen >= dpy->nscreens) {
6411ab64890Smrg	    OutOfMemory(dpy, (char *) NULL);
6421ab64890Smrg	    return(NULL);
6431ab64890Smrg	}
6441ab64890Smrg
6451ab64890Smrg#if !USE_XCB
6461ab64890Smrg/*
6471ab64890Smrg * finished calling internal routines, now unlock for external routines
6481ab64890Smrg */
6491ab64890Smrg	UnlockDisplay(dpy);
6501ab64890Smrg#endif /* !USE_XCB */
6511ab64890Smrg
65261b2299dSmrg#if USE_XCB
65361b2299dSmrg	dpy->bigreq_size = xcb_get_maximum_request_length(dpy->xcb->connection);
65461b2299dSmrg	if(dpy->bigreq_size <= dpy->max_request_size)
65561b2299dSmrg		dpy->bigreq_size = 0;
65661b2299dSmrg#endif /* USE_XCB */
65761b2299dSmrg
6581ab64890Smrg/*
6591ab64890Smrg * Set up other stuff clients are always going to use.
6601ab64890Smrg */
6611ab64890Smrg	for (i = 0; i < dpy->nscreens; i++) {
6621ab64890Smrg	    register Screen *sp = &dpy->screens[i];
6631ab64890Smrg	    XGCValues values;
6641ab64890Smrg	    values.foreground = sp->black_pixel;
6651ab64890Smrg	    values.background = sp->white_pixel;
6661ab64890Smrg	    if ((sp->default_gc = XCreateGC (dpy, sp->root,
6671ab64890Smrg					     GCForeground|GCBackground,
6681ab64890Smrg					     &values)) == NULL) {
6691ab64890Smrg		OutOfMemory(dpy, (char *) NULL);
6701ab64890Smrg		return (NULL);
6711ab64890Smrg	    }
6721ab64890Smrg	}
6731ab64890Smrg/*
6741ab64890Smrg * call into synchronization routine so that all programs can be
6751ab64890Smrg * forced synchronous
6761ab64890Smrg */
6771ab64890Smrg	(void) XSynchronize(dpy, _Xdebug);
6781ab64890Smrg
6791ab64890Smrg/*
6801ab64890Smrg * get availability of large requests, and
6811ab64890Smrg * get the resource manager database off the root window.
6821ab64890Smrg */
6831ab64890Smrg	LockDisplay(dpy);
6841ab64890Smrg	{
6851ab64890Smrg	    xGetPropertyReply reply;
6861ab64890Smrg	    xGetPropertyReq *req;
6871ab64890Smrg#if !USE_XCB
6881ab64890Smrg	    _XAsyncHandler async;
6891ab64890Smrg	    _XBigReqState async_state;
6901ab64890Smrg	    xQueryExtensionReq *qreq;
6911ab64890Smrg	    xBigReqEnableReq *breq;
6921ab64890Smrg	    xBigReqEnableReply brep;
6931ab64890Smrg
6941ab64890Smrg	    GetReq(QueryExtension, qreq);
6951ab64890Smrg	    async_state.seq = dpy->request;
6961ab64890Smrg	    async_state.opcode = 0;
6971ab64890Smrg	    async.next = dpy->async_handlers;
6981ab64890Smrg	    async.handler = _XBigReqHandler;
6991ab64890Smrg	    async.data = (XPointer)&async_state;
7001ab64890Smrg	    dpy->async_handlers = &async;
7011ab64890Smrg	    qreq->nbytes = bignamelen;
7021ab64890Smrg	    qreq->length += (bignamelen+3)>>2;
7031ab64890Smrg	    Data(dpy, XBigReqExtensionName, bignamelen);
7041ab64890Smrg#endif /* !USE_XCB */
7051ab64890Smrg
7061ab64890Smrg	    GetReq (GetProperty, req);
7071ab64890Smrg	    req->window = RootWindow(dpy, 0);
7081ab64890Smrg	    req->property = XA_RESOURCE_MANAGER;
7091ab64890Smrg	    req->type = XA_STRING;
7101ab64890Smrg	    req->delete = False;
7111ab64890Smrg	    req->longOffset = 0;
7121ab64890Smrg	    req->longLength = 100000000L;
7131ab64890Smrg
7141ab64890Smrg	    if (_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
7151ab64890Smrg		if (reply.format == 8 && reply.propertyType == XA_STRING &&
7161ab64890Smrg		    (reply.nItems + 1 > 0) &&
7171ab64890Smrg		    (reply.nItems <= req->longLength * 4) &&
7181ab64890Smrg		    (dpy->xdefaults = Xmalloc (reply.nItems + 1))) {
7191ab64890Smrg		    _XReadPad (dpy, dpy->xdefaults, reply.nItems);
7201ab64890Smrg		    dpy->xdefaults[reply.nItems] = '\0';
7211ab64890Smrg		}
7221ab64890Smrg		else if (reply.propertyType != None)
7231ab64890Smrg		    _XEatData(dpy, reply.nItems * (reply.format >> 3));
7241ab64890Smrg	    }
7251ab64890Smrg#if !USE_XCB
7261ab64890Smrg	    DeqAsyncHandler(dpy, &async);
7271ab64890Smrg	    if (async_state.opcode) {
7281ab64890Smrg		GetReq(BigReqEnable, breq);
7291ab64890Smrg		breq->reqType = async_state.opcode;
7301ab64890Smrg		breq->brReqType = X_BigReqEnable;
7311ab64890Smrg		if (_XReply(dpy, (xReply *)&brep, 0, xFalse))
7321ab64890Smrg		    dpy->bigreq_size = brep.max_request_size;
7331ab64890Smrg	    }
7341ab64890Smrg#endif /* !USE_XCB */
7351ab64890Smrg	}
7361ab64890Smrg	UnlockDisplay(dpy);
7371ab64890Smrg
7381ab64890Smrg#ifdef MOTIFBC
7391ab64890Smrg	{
7401ab64890Smrg	    extern Display *_XHeadOfDisplayList;
7411ab64890Smrg	    _XHeadOfDisplayList = dpy;
7421ab64890Smrg	}
7431ab64890Smrg#endif
7441ab64890Smrg#ifdef XKB
7451ab64890Smrg	XkbUseExtension(dpy,NULL,NULL);
7461ab64890Smrg#endif
7471ab64890Smrg/*
7481ab64890Smrg * and return successfully
7491ab64890Smrg */
7501ab64890Smrg 	return(dpy);
7511ab64890Smrg}
7521ab64890Smrg
7531ab64890Smrg#if !USE_XCB
7541ab64890Smrgstatic Bool
7551ab64890Smrg_XBigReqHandler(
7561ab64890Smrg    register Display *dpy,
7571ab64890Smrg    register xReply *rep,
7581ab64890Smrg    char *buf,
7591ab64890Smrg    int len,
7601ab64890Smrg    XPointer data)
7611ab64890Smrg{
7621ab64890Smrg    _XBigReqState *state;
7631ab64890Smrg    xQueryExtensionReply replbuf;
7641ab64890Smrg    xQueryExtensionReply *repl;
7651ab64890Smrg
7661ab64890Smrg    state = (_XBigReqState *)data;
7671ab64890Smrg    if (dpy->last_request_read != state->seq)
7681ab64890Smrg	return False;
7691ab64890Smrg    if (rep->generic.type == X_Error)
7701ab64890Smrg	return True;
7711ab64890Smrg    repl = (xQueryExtensionReply *)
7721ab64890Smrg	_XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
7731ab64890Smrg			(SIZEOF(xQueryExtensionReply) - SIZEOF(xReply)) >> 2,
7741ab64890Smrg			True);
7751ab64890Smrg    if (repl->present)
7761ab64890Smrg	state->opcode = repl->major_opcode;
7771ab64890Smrg    return True;
7781ab64890Smrg}
7791ab64890Smrg#endif /* !USE_XCB */
7801ab64890Smrg
7811ab64890Smrg
78261b2299dSmrg/* XFreeDisplayStructure frees all the storage associated with a
7831ab64890Smrg * Display.  It is used by XOpenDisplay if it runs out of memory,
7841ab64890Smrg * and also by XCloseDisplay.   It needs to check whether all pointers
7851ab64890Smrg * are non-NULL before dereferencing them, since it may be called
7861ab64890Smrg * by XOpenDisplay before the Display structure is fully formed.
7871ab64890Smrg * XOpenDisplay must be sure to initialize all the pointers to NULL
7881ab64890Smrg * before the first possible call on this.
7891ab64890Smrg */
7901ab64890Smrg
79161b2299dSmrgvoid _XFreeDisplayStructure(Display *dpy)
7921ab64890Smrg{
7931ab64890Smrg	while (dpy->ext_procs) {
7941ab64890Smrg	    _XExtension *ext = dpy->ext_procs;
7951ab64890Smrg	    dpy->ext_procs = ext->next;
7961ab64890Smrg	    if (ext->name)
7971ab64890Smrg		Xfree (ext->name);
7981ab64890Smrg	    Xfree ((char *)ext);
7991ab64890Smrg	}
8001ab64890Smrg	if (dpy->im_filters)
8011ab64890Smrg	   (*dpy->free_funcs->im_filters)(dpy);
8021ab64890Smrg	if (dpy->cms.clientCmaps)
8031ab64890Smrg	   (*dpy->free_funcs->clientCmaps)(dpy);
8041ab64890Smrg	if (dpy->cms.defaultCCCs)
8051ab64890Smrg	   (*dpy->free_funcs->defaultCCCs)(dpy);
8061ab64890Smrg	if (dpy->cms.perVisualIntensityMaps)
8071ab64890Smrg	   (*dpy->free_funcs->intensityMaps)(dpy);
8081ab64890Smrg	if (dpy->atoms)
8091ab64890Smrg	    (*dpy->free_funcs->atoms)(dpy);
8101ab64890Smrg	if (dpy->modifiermap)
8111ab64890Smrg	   (*dpy->free_funcs->modifiermap)(dpy->modifiermap);
8121ab64890Smrg	if (dpy->key_bindings)
8131ab64890Smrg	   (*dpy->free_funcs->key_bindings)(dpy);
8141ab64890Smrg	if (dpy->context_db)
8151ab64890Smrg	   (*dpy->free_funcs->context_db)(dpy);
8161ab64890Smrg	if (dpy->xkb_info)
8171ab64890Smrg	   (*dpy->free_funcs->xkb)(dpy);
8181ab64890Smrg
8191ab64890Smrg	/* if RM database was allocated by XGetDefault() free it */
8201ab64890Smrg	if (dpy->db && (dpy->flags & XlibDisplayDfltRMDB))
8211ab64890Smrg	    XrmDestroyDatabase(dpy->db);
8221ab64890Smrg
8231ab64890Smrg	if (dpy->screens) {
8241ab64890Smrg	    register int i;
8251ab64890Smrg
8261ab64890Smrg            for (i = 0; i < dpy->nscreens; i++) {
8271ab64890Smrg		Screen *sp = &dpy->screens[i];
8281ab64890Smrg
8291ab64890Smrg		if (sp->depths) {
8301ab64890Smrg		   register int j;
8311ab64890Smrg
8321ab64890Smrg		   for (j = 0; j < sp->ndepths; j++) {
8331ab64890Smrg			Depth *dp = &sp->depths[j];
8341ab64890Smrg
8351ab64890Smrg			if (dp->visuals) {
8361ab64890Smrg			   register int k;
8371ab64890Smrg
8381ab64890Smrg			   for (k = 0; k < dp->nvisuals; k++)
8391ab64890Smrg			     _XFreeExtData (dp->visuals[k].ext_data);
8401ab64890Smrg			   Xfree ((char *) dp->visuals);
8411ab64890Smrg			   }
8421ab64890Smrg			}
8431ab64890Smrg
8441ab64890Smrg		   Xfree ((char *) sp->depths);
8451ab64890Smrg		   }
8461ab64890Smrg
8471ab64890Smrg		_XFreeExtData (sp->ext_data);
8481ab64890Smrg		}
8491ab64890Smrg
8501ab64890Smrg	    Xfree ((char *)dpy->screens);
8511ab64890Smrg	    }
85261b2299dSmrg
8531ab64890Smrg	if (dpy->pixmap_format) {
8541ab64890Smrg	    register int i;
8551ab64890Smrg
8561ab64890Smrg	    for (i = 0; i < dpy->nformats; i++)
8571ab64890Smrg	      _XFreeExtData (dpy->pixmap_format[i].ext_data);
8581ab64890Smrg            Xfree ((char *)dpy->pixmap_format);
8591ab64890Smrg	    }
8601ab64890Smrg
8611ab64890Smrg	if (dpy->display_name)
8621ab64890Smrg	   Xfree (dpy->display_name);
8631ab64890Smrg	if (dpy->vendor)
8641ab64890Smrg	   Xfree (dpy->vendor);
8651ab64890Smrg
8661ab64890Smrg        if (dpy->buffer)
8671ab64890Smrg	   Xfree (dpy->buffer);
8681ab64890Smrg	if (dpy->keysyms)
8691ab64890Smrg	   Xfree ((char *) dpy->keysyms);
8701ab64890Smrg	if (dpy->xdefaults)
8711ab64890Smrg	   Xfree (dpy->xdefaults);
8721ab64890Smrg	if (dpy->error_vec)
8731ab64890Smrg	    Xfree ((char *)dpy->error_vec);
8741ab64890Smrg
8751ab64890Smrg	_XFreeExtData (dpy->ext_data);
8761ab64890Smrg	if (dpy->free_funcs)
8771ab64890Smrg	    Xfree ((char *)dpy->free_funcs);
8781ab64890Smrg 	if (dpy->scratch_buffer)
8791ab64890Smrg 	    Xfree (dpy->scratch_buffer);
8801ab64890Smrg	FreeDisplayLock(dpy);
8811ab64890Smrg
8821ab64890Smrg	if (dpy->qfree) {
8831ab64890Smrg	    register _XQEvent *qelt = dpy->qfree;
8841ab64890Smrg
8851ab64890Smrg	    while (qelt) {
8861ab64890Smrg		register _XQEvent *qnxt = qelt->next;
8871ab64890Smrg		Xfree ((char *) qelt);
8881ab64890Smrg		qelt = qnxt;
8891ab64890Smrg	    }
8901ab64890Smrg	}
8911ab64890Smrg	while (dpy->im_fd_info) {
8921ab64890Smrg	    struct _XConnectionInfo *conni = dpy->im_fd_info;
8931ab64890Smrg	    dpy->im_fd_info = conni->next;
8941ab64890Smrg	    if (conni->watch_data)
8951ab64890Smrg		Xfree (conni->watch_data);
8961ab64890Smrg	    Xfree (conni);
8971ab64890Smrg	}
8981ab64890Smrg	if (dpy->conn_watchers) {
8991ab64890Smrg	    struct _XConnWatchInfo *watcher = dpy->conn_watchers;
9001ab64890Smrg	    dpy->conn_watchers = watcher->next;
9011ab64890Smrg	    Xfree (watcher);
9021ab64890Smrg	}
9031ab64890Smrg	if (dpy->filedes)
9041ab64890Smrg	    Xfree (dpy->filedes);
9051ab64890Smrg
9061ab64890Smrg#if USE_XCB
9071ab64890Smrg	_XFreeX11XCBStructure(dpy);
9081ab64890Smrg#endif /* USE_XCB */
9091ab64890Smrg
9101ab64890Smrg	Xfree ((char *)dpy);
9111ab64890Smrg}
9121ab64890Smrg
9131ab64890Smrg/* OutOfMemory is called if malloc fails.  XOpenDisplay returns NULL
9141ab64890Smrg   after this returns. */
9151ab64890Smrg
91661b2299dSmrgstatic void OutOfMemory(Display *dpy, char *setup)
9171ab64890Smrg{
9181ab64890Smrg#if USE_XCB
9191ab64890Smrg    if(dpy->xcb->connection)
9201ab64890Smrg	xcb_disconnect(dpy->xcb->connection);
9211ab64890Smrg#else /* !USE_XCB */
9221ab64890Smrg    _XDisconnectDisplay (dpy->trans_conn);
9231ab64890Smrg#endif /* USE_XCB */
9241ab64890Smrg    _XFreeDisplayStructure (dpy);
9251ab64890Smrg#if !USE_XCB
9261ab64890Smrg    if (setup) Xfree (setup);
9271ab64890Smrg#endif /* !USE_XCB */
9281ab64890Smrg}
929