1602e473dSmrg/* Copyright (C) 2001-2008 Bart Massey and Jamey Sharp. 2602e473dSmrg * 3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a 4602e473dSmrg * copy of this software and associated documentation files (the "Software"), 5602e473dSmrg * to deal in the Software without restriction, including without limitation 6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the 8602e473dSmrg * Software is furnished to do so, subject to the following conditions: 9602e473dSmrg * 10602e473dSmrg * The above copyright notice and this permission notice shall be included in 11602e473dSmrg * all copies or substantial portions of the Software. 12602e473dSmrg * 13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19602e473dSmrg * 20602e473dSmrg * Except as contained in this notice, the names of the authors or their 21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the 22602e473dSmrg * sale, use or other dealings in this Software without prior written 23602e473dSmrg * authorization from the authors. 24602e473dSmrg */ 25602e473dSmrg 26602e473dSmrg/* XID allocators. */ 27602e473dSmrg 2821298544Smrg#ifdef HAVE_CONFIG_H 2921298544Smrg#include "config.h" 3021298544Smrg#endif 3121298544Smrg 32602e473dSmrg#include <assert.h> 33602e473dSmrg#include <stdlib.h> 34602e473dSmrg#include "xcb.h" 35602e473dSmrg#include "xcbext.h" 36602e473dSmrg#include "xcbint.h" 37602e473dSmrg#include "xc_misc.h" 38602e473dSmrg 39602e473dSmrg/* Public interface */ 40602e473dSmrg 41602e473dSmrguint32_t xcb_generate_id(xcb_connection_t *c) 42602e473dSmrg{ 43602e473dSmrg uint32_t ret; 44602e473dSmrg if(c->has_error) 45602e473dSmrg return -1; 46602e473dSmrg pthread_mutex_lock(&c->xid.lock); 47602e473dSmrg if(c->xid.last >= c->xid.max - c->xid.inc + 1) 48602e473dSmrg { 49602e473dSmrg xcb_xc_misc_get_xid_range_reply_t *range; 50602e473dSmrg assert(c->xid.last == c->xid.max); 51602e473dSmrg if (c->xid.last == 0) { 52602e473dSmrg /* finish setting up initial range */ 53602e473dSmrg c->xid.max = c->setup->resource_id_mask; 54602e473dSmrg } else { 55602e473dSmrg /* check for extension */ 56602e473dSmrg const xcb_query_extension_reply_t *xc_misc_reply = 57602e473dSmrg xcb_get_extension_data(c, &xcb_xc_misc_id); 588ffb90f1Smrg if (!xc_misc_reply || !xc_misc_reply->present) { 59602e473dSmrg pthread_mutex_unlock(&c->xid.lock); 60602e473dSmrg return -1; 61602e473dSmrg } 62602e473dSmrg /* get new range */ 63602e473dSmrg range = xcb_xc_misc_get_xid_range_reply(c, 64602e473dSmrg xcb_xc_misc_get_xid_range(c), 0); 65602e473dSmrg /* XXX The latter disjunct is what the server returns 66602e473dSmrg when it is out of XIDs. Sweet. */ 67602e473dSmrg if(!range || (range->start_id == 0 && range->count == 1)) 68602e473dSmrg { 69602e473dSmrg pthread_mutex_unlock(&c->xid.lock); 70602e473dSmrg return -1; 71602e473dSmrg } 72602e473dSmrg assert(range->count > 0 && range->start_id > 0); 73602e473dSmrg c->xid.last = range->start_id; 74602e473dSmrg c->xid.max = range->start_id + (range->count - 1) * c->xid.inc; 75602e473dSmrg free(range); 76602e473dSmrg } 77602e473dSmrg } else { 78602e473dSmrg c->xid.last += c->xid.inc; 79602e473dSmrg } 80602e473dSmrg ret = c->xid.last | c->xid.base; 81602e473dSmrg pthread_mutex_unlock(&c->xid.lock); 82602e473dSmrg return ret; 83602e473dSmrg} 84602e473dSmrg 85602e473dSmrg/* Private interface */ 86602e473dSmrg 87602e473dSmrgint _xcb_xid_init(xcb_connection_t *c) 88602e473dSmrg{ 89602e473dSmrg if(pthread_mutex_init(&c->xid.lock, 0)) 90602e473dSmrg return 0; 91602e473dSmrg c->xid.last = 0; 92602e473dSmrg c->xid.max = 0; 93602e473dSmrg c->xid.base = c->setup->resource_id_base; 94602e473dSmrg c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask); 95602e473dSmrg return 1; 96602e473dSmrg} 97602e473dSmrg 98602e473dSmrgvoid _xcb_xid_destroy(xcb_connection_t *c) 99602e473dSmrg{ 100602e473dSmrg pthread_mutex_destroy(&c->xid.lock); 101602e473dSmrg} 102