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 57e9fcaa8aSmrgint _XConnectXCB(Display *dpy, _Xconst char *display, int *screenp) 581ab64890Smrg{ 591ab64890Smrg char *host; 601ab64890Smrg int n = 0; 611ab64890Smrg xcb_connection_t *c; 621ab64890Smrg 631ab64890Smrg dpy->fd = -1; 641ab64890Smrg 651ab64890Smrg dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate)); 661ab64890Smrg if(!dpy->xcb) 671ab64890Smrg return 0; 681ab64890Smrg 6988de56ccSmrg if(!xcb_parse_display(display, &host, &n, screenp)) 7088de56ccSmrg return 0; 71e9fcaa8aSmrg /* host and n are unused, but xcb_parse_display requires them */ 7288de56ccSmrg free(host); 7388de56ccSmrg 741ab64890Smrg _XLockMutex(_Xglobal_lock); 751ab64890Smrg if(xauth.name && xauth.data) 7661b2299dSmrg c = xcb_connect_to_display_with_auth_info(display, &xauth, NULL); 771ab64890Smrg else 7861b2299dSmrg c = xcb_connect(display, NULL); 791ab64890Smrg _XUnlockMutex(_Xglobal_lock); 801ab64890Smrg 811ab64890Smrg dpy->fd = xcb_get_file_descriptor(c); 821ab64890Smrg 831ab64890Smrg dpy->xcb->connection = c; 841ab64890Smrg dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection); 851ab64890Smrg 8661b2299dSmrg dpy->xcb->event_notify = xcondition_malloc(); 8788de56ccSmrg dpy->xcb->reply_notify = xcondition_malloc(); 8888de56ccSmrg if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify) 8961b2299dSmrg return 0; 9061b2299dSmrg xcondition_init(dpy->xcb->event_notify); 9188de56ccSmrg xcondition_init(dpy->xcb->reply_notify); 921ab64890Smrg return !xcb_connection_has_error(c); 931ab64890Smrg} 941ab64890Smrg 951ab64890Smrgvoid _XFreeX11XCBStructure(Display *dpy) 961ab64890Smrg{ 971ab64890Smrg /* reply_data was allocated by system malloc, not Xmalloc */ 981ab64890Smrg free(dpy->xcb->reply_data); 991ab64890Smrg while(dpy->xcb->pending_requests) 1001ab64890Smrg { 1011ab64890Smrg PendingRequest *tmp = dpy->xcb->pending_requests; 1021ab64890Smrg dpy->xcb->pending_requests = tmp->next; 1031ab64890Smrg free(tmp); 1041ab64890Smrg } 105e9628295Smrg if (dpy->xcb->event_notify) 106e9628295Smrg xcondition_clear(dpy->xcb->event_notify); 107e9628295Smrg if (dpy->xcb->reply_notify) 108e9628295Smrg xcondition_clear(dpy->xcb->reply_notify); 10961b2299dSmrg xcondition_free(dpy->xcb->event_notify); 11088de56ccSmrg xcondition_free(dpy->xcb->reply_notify); 1111ab64890Smrg Xfree(dpy->xcb); 112d4a3aaf4Smrg dpy->xcb = NULL; 1131ab64890Smrg} 114