1 /* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett
2 * This file is licensed under the MIT license. See the file COPYING. */
10 #include <xcb/xcbext.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xresource.h>
15 static xcb_auth_info_t xauth;
17 static void *alloc_copy(const void *src, int *dstn, size_t n)
33 void XSetAuthorization(char *name, int namelen, char *data, int datalen)
35 _XLockMutex(_Xglobal_lock);
39 /* if either of these allocs fail, _XConnectXCB won't use this auth
40 * data, so we don't need to check it here. */
41 xauth.name = alloc_copy(name, &xauth.namelen, namelen);
42 xauth.data = alloc_copy(data, &xauth.datalen, datalen);
44 #if 0 /* but, for the paranoid among us: */
45 if((namelen > 0 && !xauth.name) || (datalen > 0 && !xauth.data))
49 xauth.name = xauth.data = 0;
50 xauth.namelen = xauth.datalen = 0;
54 _XUnlockMutex(_Xglobal_lock);
57 int _XConnectXCB(Display *dpy, _Xconst char *display, int *screenp)
65 dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate));
69 if(!xcb_parse_display(display, &host, &n, screenp))
71 /* host and n are unused, but xcb_parse_display requires them */
74 _XLockMutex(_Xglobal_lock);
75 if(xauth.name && xauth.data)
76 c = xcb_connect_to_display_with_auth_info(display, &xauth, NULL);
78 c = xcb_connect(display, NULL);
79 _XUnlockMutex(_Xglobal_lock);
81 dpy->fd = xcb_get_file_descriptor(c);
83 dpy->xcb->connection = c;
84 dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);
86 dpy->xcb->event_notify = xcondition_malloc();
87 dpy->xcb->reply_notify = xcondition_malloc();
88 if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify)
90 xcondition_init(dpy->xcb->event_notify);
91 xcondition_init(dpy->xcb->reply_notify);
92 return !xcb_connection_has_error(c);
95 void _XFreeX11XCBStructure(Display *dpy)
97 /* reply_data was allocated by system malloc, not Xmalloc */
98 free(dpy->xcb->reply_data);
99 while(dpy->xcb->pending_requests)
101 PendingRequest *tmp = dpy->xcb->pending_requests;
102 dpy->xcb->pending_requests = tmp->next;
105 xcondition_free(dpy->xcb->event_notify);
106 xcondition_free(dpy->xcb->reply_notify);