xcb_disp.c revision 88de56cc
11ab64890Smrg/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett
21ab64890Smrg * This file is licensed under the MIT license. See the file COPYING. */
31ab64890Smrg
488de56ccSmrg#ifdef HAVE_CONFIG_H
588de56ccSmrg#include <config.h>
688de56ccSmrg#endif
788de56ccSmrg
81ab64890Smrg#include "Xlibint.h"
91ab64890Smrg#include "Xxcbint.h"
101ab64890Smrg#include <xcb/xcbext.h>
111ab64890Smrg#include <X11/Xatom.h>
121ab64890Smrg#include <X11/Xresource.h>
131ab64890Smrg#include <stdio.h>
141ab64890Smrg
151ab64890Smrgstatic xcb_auth_info_t xauth;
161ab64890Smrg
171ab64890Smrgstatic void *alloc_copy(const void *src, int *dstn, size_t n)
181ab64890Smrg{
191ab64890Smrg	void *dst;
201ab64890Smrg	if(n <= 0)
211ab64890Smrg	{
221ab64890Smrg		*dstn = 0;
2361b2299dSmrg		return NULL;
241ab64890Smrg	}
251ab64890Smrg	dst = Xmalloc(n);
261ab64890Smrg	if(!dst)
2761b2299dSmrg		return NULL;
281ab64890Smrg	memcpy(dst, src, n);
291ab64890Smrg	*dstn = n;
301ab64890Smrg	return dst;
311ab64890Smrg}
321ab64890Smrg
331ab64890Smrgvoid XSetAuthorization(char *name, int namelen, char *data, int datalen)
341ab64890Smrg{
351ab64890Smrg	_XLockMutex(_Xglobal_lock);
361ab64890Smrg	Xfree(xauth.name);
371ab64890Smrg	Xfree(xauth.data);
381ab64890Smrg
391ab64890Smrg	/* if either of these allocs fail, _XConnectXCB won't use this auth
401ab64890Smrg	 * data, so we don't need to check it here. */
411ab64890Smrg	xauth.name = alloc_copy(name, &xauth.namelen, namelen);
421ab64890Smrg	xauth.data = alloc_copy(data, &xauth.datalen, datalen);
431ab64890Smrg
441ab64890Smrg#if 0 /* but, for the paranoid among us: */
451ab64890Smrg	if((namelen > 0 && !xauth.name) || (datalen > 0 && !xauth.data))
461ab64890Smrg	{
471ab64890Smrg		Xfree(xauth.name);
481ab64890Smrg		Xfree(xauth.data);
491ab64890Smrg		xauth.name = xauth.data = 0;
501ab64890Smrg		xauth.namelen = xauth.datalen = 0;
511ab64890Smrg	}
521ab64890Smrg#endif
531ab64890Smrg
541ab64890Smrg	_XUnlockMutex(_Xglobal_lock);
551ab64890Smrg}
561ab64890Smrg
571ab64890Smrgint _XConnectXCB(Display *dpy, _Xconst char *display, char **fullnamep, int *screenp)
581ab64890Smrg{
591ab64890Smrg	char *host;
601ab64890Smrg	int n = 0;
611ab64890Smrg	int len;
621ab64890Smrg	xcb_connection_t *c;
631ab64890Smrg
641ab64890Smrg	dpy->fd = -1;
651ab64890Smrg
661ab64890Smrg	dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate));
671ab64890Smrg	if(!dpy->xcb)
681ab64890Smrg		return 0;
691ab64890Smrg
7088de56ccSmrg	if(!xcb_parse_display(display, &host, &n, screenp))
7188de56ccSmrg		return 0;
7261b2299dSmrg
7388de56ccSmrg	len = strlen(host) + (1 + 20 + 1 + 20 + 1);
7488de56ccSmrg	*fullnamep = Xmalloc(len);
7588de56ccSmrg	if (!*fullnamep) {
761ab64890Smrg		free(host);
7788de56ccSmrg		return 0;
781ab64890Smrg	}
791ab64890Smrg
8088de56ccSmrg#ifdef HAVE_LAUNCHD
8188de56ccSmrg	if(strncmp(host, "/tmp/launch", 11) == 0)
8288de56ccSmrg		snprintf(*fullnamep, len, "%s:%d", host, n);
8388de56ccSmrg	else
8488de56ccSmrg#endif
8588de56ccSmrg		snprintf(*fullnamep, len, "%s:%d.%d", host, n, *screenp);
8688de56ccSmrg	free(host);
8788de56ccSmrg
881ab64890Smrg	_XLockMutex(_Xglobal_lock);
891ab64890Smrg	if(xauth.name && xauth.data)
9061b2299dSmrg		c = xcb_connect_to_display_with_auth_info(display, &xauth, NULL);
911ab64890Smrg	else
9261b2299dSmrg		c = xcb_connect(display, NULL);
931ab64890Smrg	_XUnlockMutex(_Xglobal_lock);
941ab64890Smrg
951ab64890Smrg	dpy->fd = xcb_get_file_descriptor(c);
961ab64890Smrg
971ab64890Smrg	dpy->xcb->connection = c;
981ab64890Smrg	dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);
991ab64890Smrg
10061b2299dSmrg	dpy->xcb->event_notify = xcondition_malloc();
10188de56ccSmrg	dpy->xcb->reply_notify = xcondition_malloc();
10288de56ccSmrg	if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify)
10361b2299dSmrg		return 0;
10461b2299dSmrg	xcondition_init(dpy->xcb->event_notify);
10588de56ccSmrg	xcondition_init(dpy->xcb->reply_notify);
1061ab64890Smrg	return !xcb_connection_has_error(c);
1071ab64890Smrg}
1081ab64890Smrg
1091ab64890Smrgvoid _XFreeX11XCBStructure(Display *dpy)
1101ab64890Smrg{
1111ab64890Smrg	/* reply_data was allocated by system malloc, not Xmalloc */
1121ab64890Smrg	free(dpy->xcb->reply_data);
1131ab64890Smrg	while(dpy->xcb->pending_requests)
1141ab64890Smrg	{
1151ab64890Smrg		PendingRequest *tmp = dpy->xcb->pending_requests;
1161ab64890Smrg		dpy->xcb->pending_requests = tmp->next;
1171ab64890Smrg		free(tmp);
1181ab64890Smrg	}
11961b2299dSmrg	xcondition_free(dpy->xcb->event_notify);
12088de56ccSmrg	xcondition_free(dpy->xcb->reply_notify);
1211ab64890Smrg	Xfree(dpy->xcb);
1221ab64890Smrg}
123