aboutsummaryrefslogtreecommitdiff
path: root/libX11/src/xcb_disp.c
blob: 079c87ffa72ce9f7c9e6a72dc49845b37f531422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett
 * This file is licensed under the MIT license. See the file COPYING. */

#include "Xlibint.h"
#include "Xxcbint.h"
#include <xcb/xcbext.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <stdio.h>

static xcb_auth_info_t xauth;

static void *alloc_copy(const void *src, int *dstn, size_t n)
{
	void *dst;
	if(n <= 0)
	{
		*dstn = 0;
		return 0;
	}
	dst = Xmalloc(n);
	if(!dst)
		return 0;
	memcpy(dst, src, n);
	*dstn = n;
	return dst;
}

void XSetAuthorization(char *name, int namelen, char *data, int datalen)
{
	_XLockMutex(_Xglobal_lock);
	Xfree(xauth.name);
	Xfree(xauth.data);

	/* if either of these allocs fail, _XConnectXCB won't use this auth
	 * data, so we don't need to check it here. */
	xauth.name = alloc_copy(name, &xauth.namelen, namelen);
	xauth.data = alloc_copy(data, &xauth.datalen, datalen);

#if 0 /* but, for the paranoid among us: */
	if((namelen > 0 && !xauth.name) || (datalen > 0 && !xauth.data))
	{
		Xfree(xauth.name);
		Xfree(xauth.data);
		xauth.name = xauth.data = 0;
		xauth.namelen = xauth.datalen = 0;
	}
#endif

	_XUnlockMutex(_Xglobal_lock);
}

int _XConnectXCB(Display *dpy, _Xconst char *display, char **fullnamep, int *screenp)
{
	char *host;
	int n = 0;
	int len;
	xcb_connection_t *c;

	dpy->fd = -1;

	dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate));
	if(!dpy->xcb)
		return 0;

#ifdef HAVE_LAUNCHD
	if(!display || !*display) display = getenv("DISPLAY");
	
	if(display && strlen(display)>11 && !strncmp(display, "/tmp/launch", 11)) {
		/* do nothing -- the magic happens inside of xcb_connect */
	} else
#endif
	{
		if(!xcb_parse_display(display, &host, &n, screenp))
			return 0;

		len = strlen(host) + (1 + 20 + 1 + 20 + 1);
		*fullnamep = Xmalloc(len);
		snprintf(*fullnamep, len, "%s:%d.%d", host, n, *screenp);
		free(host);
	}

	_XLockMutex(_Xglobal_lock);
	if(xauth.name && xauth.data)
		c = xcb_connect_to_display_with_auth_info(display, &xauth, 0);
	else
		c = xcb_connect(display, 0);
	_XUnlockMutex(_Xglobal_lock);

	dpy->fd = xcb_get_file_descriptor(c);

	dpy->xcb->connection = c;
	dpy->xcb->pending_requests_tail = &dpy->xcb->pending_requests;
	dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);

	return !xcb_connection_has_error(c);
}

void _XFreeX11XCBStructure(Display *dpy)
{
	/* reply_data was allocated by system malloc, not Xmalloc */
	free(dpy->xcb->reply_data);
	while(dpy->xcb->pending_requests)
	{
		PendingRequest *tmp = dpy->xcb->pending_requests;
		dpy->xcb->pending_requests = tmp->next;
		free(tmp);
	}
	Xfree(dpy->xcb);
}