xcb_disp.c revision 61b2299d
11ab64890Smrg/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett
21ab64890Smrg * This file is licensed under the MIT license. See the file COPYING. */
31ab64890Smrg
41ab64890Smrg#include "Xlibint.h"
51ab64890Smrg#include "Xxcbint.h"
61ab64890Smrg#include <xcb/xcbext.h>
71ab64890Smrg#include <X11/Xatom.h>
81ab64890Smrg#include <X11/Xresource.h>
91ab64890Smrg#include <stdio.h>
101ab64890Smrg
111ab64890Smrgstatic xcb_auth_info_t xauth;
121ab64890Smrg
131ab64890Smrgstatic void *alloc_copy(const void *src, int *dstn, size_t n)
141ab64890Smrg{
151ab64890Smrg	void *dst;
161ab64890Smrg	if(n <= 0)
171ab64890Smrg	{
181ab64890Smrg		*dstn = 0;
1961b2299dSmrg		return NULL;
201ab64890Smrg	}
211ab64890Smrg	dst = Xmalloc(n);
221ab64890Smrg	if(!dst)
2361b2299dSmrg		return NULL;
241ab64890Smrg	memcpy(dst, src, n);
251ab64890Smrg	*dstn = n;
261ab64890Smrg	return dst;
271ab64890Smrg}
281ab64890Smrg
291ab64890Smrgvoid XSetAuthorization(char *name, int namelen, char *data, int datalen)
301ab64890Smrg{
311ab64890Smrg	_XLockMutex(_Xglobal_lock);
321ab64890Smrg	Xfree(xauth.name);
331ab64890Smrg	Xfree(xauth.data);
341ab64890Smrg
351ab64890Smrg	/* if either of these allocs fail, _XConnectXCB won't use this auth
361ab64890Smrg	 * data, so we don't need to check it here. */
371ab64890Smrg	xauth.name = alloc_copy(name, &xauth.namelen, namelen);
381ab64890Smrg	xauth.data = alloc_copy(data, &xauth.datalen, datalen);
391ab64890Smrg
401ab64890Smrg#if 0 /* but, for the paranoid among us: */
411ab64890Smrg	if((namelen > 0 && !xauth.name) || (datalen > 0 && !xauth.data))
421ab64890Smrg	{
431ab64890Smrg		Xfree(xauth.name);
441ab64890Smrg		Xfree(xauth.data);
451ab64890Smrg		xauth.name = xauth.data = 0;
461ab64890Smrg		xauth.namelen = xauth.datalen = 0;
471ab64890Smrg	}
481ab64890Smrg#endif
491ab64890Smrg
501ab64890Smrg	_XUnlockMutex(_Xglobal_lock);
511ab64890Smrg}
521ab64890Smrg
531ab64890Smrgint _XConnectXCB(Display *dpy, _Xconst char *display, char **fullnamep, int *screenp)
541ab64890Smrg{
551ab64890Smrg	char *host;
561ab64890Smrg	int n = 0;
571ab64890Smrg	int len;
581ab64890Smrg	xcb_connection_t *c;
591ab64890Smrg
601ab64890Smrg	dpy->fd = -1;
611ab64890Smrg
621ab64890Smrg	dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate));
631ab64890Smrg	if(!dpy->xcb)
641ab64890Smrg		return 0;
651ab64890Smrg
661ab64890Smrg#ifdef HAVE_LAUNCHD
671ab64890Smrg	if(!display || !*display) display = getenv("DISPLAY");
6861b2299dSmrg
691ab64890Smrg	if(display && strlen(display)>11 && !strncmp(display, "/tmp/launch", 11)) {
701ab64890Smrg		/* do nothing -- the magic happens inside of xcb_connect */
711ab64890Smrg	} else
721ab64890Smrg#endif
731ab64890Smrg	{
741ab64890Smrg		if(!xcb_parse_display(display, &host, &n, screenp))
751ab64890Smrg			return 0;
761ab64890Smrg
771ab64890Smrg		len = strlen(host) + (1 + 20 + 1 + 20 + 1);
781ab64890Smrg		*fullnamep = Xmalloc(len);
7961b2299dSmrg		if (!*fullnamep) {
8061b2299dSmrg			free(host);
8161b2299dSmrg			return 0;
8261b2299dSmrg		}
8361b2299dSmrg
841ab64890Smrg		snprintf(*fullnamep, len, "%s:%d.%d", host, n, *screenp);
851ab64890Smrg		free(host);
861ab64890Smrg	}
871ab64890Smrg
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->pending_requests_tail = &dpy->xcb->pending_requests;
991ab64890Smrg	dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);
1001ab64890Smrg
10161b2299dSmrg	dpy->xcb->event_notify = xcondition_malloc();
10261b2299dSmrg	if (!dpy->xcb->event_notify)
10361b2299dSmrg		return 0;
10461b2299dSmrg	xcondition_init(dpy->xcb->event_notify);
1051ab64890Smrg	return !xcb_connection_has_error(c);
1061ab64890Smrg}
1071ab64890Smrg
1081ab64890Smrgvoid _XFreeX11XCBStructure(Display *dpy)
1091ab64890Smrg{
1101ab64890Smrg	/* reply_data was allocated by system malloc, not Xmalloc */
1111ab64890Smrg	free(dpy->xcb->reply_data);
1121ab64890Smrg	while(dpy->xcb->pending_requests)
1131ab64890Smrg	{
1141ab64890Smrg		PendingRequest *tmp = dpy->xcb->pending_requests;
1151ab64890Smrg		dpy->xcb->pending_requests = tmp->next;
1161ab64890Smrg		free(tmp);
1171ab64890Smrg	}
11861b2299dSmrg	xcondition_free(dpy->xcb->event_notify);
1191ab64890Smrg	Xfree(dpy->xcb);
1201ab64890Smrg}
121