4a01f6ffebe9e1b528d5ef1d59cb26b43838ab7d
[free-sw/xcb/libxcb] / src / xcbint.h
1 /*
2  * Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  * 
22  * Except as contained in this notice, the names of the authors or their
23  * institutions shall not be used in advertising or otherwise to promote the
24  * sale, use or other dealings in this Software without prior written
25  * authorization from the authors.
26  */
27
28 #ifndef __XCBINT_H
29 #define __XCBINT_H
30
31 #include "bigreq.h"
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #if HAVE_SENDMSG
38 #include <sys/socket.h>
39 #endif
40
41 #ifdef GCC_HAS_VISIBILITY
42 #pragma GCC visibility push(hidden)
43 #endif
44
45 enum workarounds {
46     WORKAROUND_NONE,
47     WORKAROUND_GLX_GET_FB_CONFIGS_BUG,
48     WORKAROUND_EXTERNAL_SOCKET_OWNER
49 };
50
51 enum lazy_reply_tag
52 {
53     LAZY_NONE = 0,
54     LAZY_COOKIE,
55     LAZY_FORCED
56 };
57
58 #define XCB_PAD(i) (-(i) & 3)
59
60 #define XCB_SEQUENCE_COMPARE(a,op,b)    ((int64_t) ((a) - (b)) op 0)
61
62 #ifndef offsetof
63 #define offsetof(type,member) ((size_t) &((type *)0)->member)
64 #endif
65
66 #ifndef MIN
67 #define MIN(x,y) ((x) < (y) ? (x) : (y))
68 #endif
69
70 #define container_of(pointer,type,member) ((type *)(((char *)(pointer)) - offsetof(type, member)))
71
72 /* xcb_list.c */
73
74 typedef void (*xcb_list_free_func_t)(void *);
75
76 typedef struct _xcb_map _xcb_map;
77
78 _xcb_map *_xcb_map_new(void);
79 void _xcb_map_delete(_xcb_map *q, xcb_list_free_func_t do_free);
80 int _xcb_map_put(_xcb_map *q, unsigned int key, void *data);
81 void *_xcb_map_remove(_xcb_map *q, unsigned int key);
82
83
84 /* xcb_out.c */
85
86 typedef void (*xcb_return_socket_func_t)(void *closure);
87
88 #if HAVE_SENDMSG
89 #define XCB_MAX_PASS_FD 16
90
91 typedef struct _xcb_fd {
92     struct cmsghdr cmsghdr;
93     int fd[XCB_MAX_PASS_FD];
94     int nfd;
95     int ifd;
96 } _xcb_fd;
97 #endif
98
99 typedef struct _xcb_out {
100     pthread_cond_t cond;
101     int writing;
102
103     xcb_return_socket_func_t return_socket;
104     void *socket_closure;
105     unsigned int socket_seq;
106
107     char queue[XCB_QUEUE_BUFFER_SIZE];
108     int queue_len;
109
110     uint64_t request;
111     uint64_t request_written;
112
113     pthread_mutex_t reqlenlock;
114     enum lazy_reply_tag maximum_request_length_tag;
115     union {
116         xcb_big_requests_enable_cookie_t cookie;
117         uint32_t value;
118     } maximum_request_length;
119 #if HAVE_SENDMSG
120     _xcb_fd out_fd;
121 #endif
122 } _xcb_out;
123
124 int _xcb_out_init(_xcb_out *out);
125 void _xcb_out_destroy(_xcb_out *out);
126
127 int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count);
128 void _xcb_out_send_sync(xcb_connection_t *c);
129 int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request);
130
131
132 /* xcb_in.c */
133
134 typedef struct _xcb_in {
135     pthread_cond_t event_cond;
136     int reading;
137
138     char queue[4096];
139     int queue_len;
140
141     uint64_t request_expected;
142     uint64_t request_read;
143     uint64_t request_completed;
144     struct reply_list *current_reply;
145     struct reply_list **current_reply_tail;
146
147     _xcb_map *replies;
148     struct event_list *events;
149     struct event_list **events_tail;
150     struct reader_list *readers;
151
152     struct pending_reply *pending_replies;
153     struct pending_reply **pending_replies_tail;
154 #if HAVE_SENDMSG
155     _xcb_fd in_fd;
156 #endif
157 } _xcb_in;
158
159 int _xcb_in_init(_xcb_in *in);
160 void _xcb_in_destroy(_xcb_in *in);
161
162 void _xcb_in_wake_up_next_reader(xcb_connection_t *c);
163
164 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags);
165 void _xcb_in_replies_done(xcb_connection_t *c);
166
167 int _xcb_in_read(xcb_connection_t *c);
168 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int nread);
169
170
171 /* xcb_xid.c */
172
173 typedef struct _xcb_xid {
174     pthread_mutex_t lock;
175     uint32_t last;
176     uint32_t base;
177     uint32_t max;
178     uint32_t inc;
179 } _xcb_xid;
180
181 int _xcb_xid_init(xcb_connection_t *c);
182 void _xcb_xid_destroy(xcb_connection_t *c);
183
184
185 /* xcb_ext.c */
186
187 typedef struct _xcb_ext {
188     pthread_mutex_t lock;
189     struct lazyreply *extensions;
190     int extensions_size;
191 } _xcb_ext;
192
193 int _xcb_ext_init(xcb_connection_t *c);
194 void _xcb_ext_destroy(xcb_connection_t *c);
195
196
197 /* xcb_conn.c */
198
199 struct xcb_connection_t {
200     int has_error;
201
202     /* constant data */
203     xcb_setup_t *setup;
204     int fd;
205
206     /* I/O data */
207     pthread_mutex_t iolock;
208     _xcb_in in;
209     _xcb_out out;
210
211     /* misc data */
212     _xcb_ext ext;
213     _xcb_xid xid;
214 };
215
216 void _xcb_conn_shutdown(xcb_connection_t *c, int err);
217
218 xcb_connection_t *_xcb_conn_ret_error(int err);
219
220 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);
221
222
223 /* xcb_auth.c */
224
225 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display);
226
227 #ifdef GCC_HAS_VISIBILITY
228 #pragma GCC visibility pop
229 #endif
230
231 #endif