1/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett 2 * This file is licensed under the MIT license. See the file COPYING. */ 3 4#ifdef HAVE_CONFIG_H 5#include <config.h> 6#endif 7 8#include "Xlibint.h" 9#include "Xxcbint.h" 10#include <xcb/xcbext.h> 11#include <X11/Xatom.h> 12#include <X11/Xresource.h> 13#include <stdio.h> 14 15static xcb_auth_info_t xauth; 16 17static void *alloc_copy(const void *src, int *dstn, size_t n) 18{ 19 void *dst; 20 if(n <= 0) 21 { 22 *dstn = 0; 23 return NULL; 24 } 25 dst = Xmalloc(n); 26 if(!dst) 27 return NULL; 28 memcpy(dst, src, n); 29 *dstn = n; 30 return dst; 31} 32 33void XSetAuthorization(char *name, int namelen, char *data, int datalen) 34{ 35 _XLockMutex(_Xglobal_lock); 36 Xfree(xauth.name); 37 Xfree(xauth.data); 38 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); 43 44#if 0 /* but, for the paranoid among us: */ 45 if((namelen > 0 && !xauth.name) || (datalen > 0 && !xauth.data)) 46 { 47 Xfree(xauth.name); 48 Xfree(xauth.data); 49 xauth.name = xauth.data = 0; 50 xauth.namelen = xauth.datalen = 0; 51 } 52#endif 53 54 _XUnlockMutex(_Xglobal_lock); 55} 56 57int _XConnectXCB(Display *dpy, _Xconst char *display, int *screenp) 58{ 59 char *host; 60 int n = 0; 61 xcb_connection_t *c; 62 63 dpy->fd = -1; 64 65 dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate)); 66 if(!dpy->xcb) 67 return 0; 68 69 if(!xcb_parse_display(display, &host, &n, screenp)) 70 return 0; 71 /* host and n are unused, but xcb_parse_display requires them */ 72 free(host); 73 74 _XLockMutex(_Xglobal_lock); 75 if(xauth.name && xauth.data) 76 c = xcb_connect_to_display_with_auth_info(display, &xauth, NULL); 77 else 78 c = xcb_connect(display, NULL); 79 _XUnlockMutex(_Xglobal_lock); 80 81 dpy->fd = xcb_get_file_descriptor(c); 82 83 dpy->xcb->connection = c; 84 dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection); 85 86 dpy->xcb->event_notify = xcondition_malloc(); 87 dpy->xcb->reply_notify = xcondition_malloc(); 88 if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify) 89 return 0; 90 xcondition_init(dpy->xcb->event_notify); 91 xcondition_init(dpy->xcb->reply_notify); 92 return !xcb_connection_has_error(c); 93} 94 95void _XFreeX11XCBStructure(Display *dpy) 96{ 97 /* reply_data was allocated by system malloc, not Xmalloc */ 98 free(dpy->xcb->reply_data); 99 while(dpy->xcb->pending_requests) 100 { 101 PendingRequest *tmp = dpy->xcb->pending_requests; 102 dpy->xcb->pending_requests = tmp->next; 103 free(tmp); 104 } 105 if (dpy->xcb->event_notify) 106 xcondition_clear(dpy->xcb->event_notify); 107 if (dpy->xcb->reply_notify) 108 xcondition_clear(dpy->xcb->reply_notify); 109 xcondition_free(dpy->xcb->event_notify); 110 xcondition_free(dpy->xcb->reply_notify); 111 Xfree(dpy->xcb); 112 dpy->xcb = NULL; 113} 114