Add xcb_send_fd API
[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 } _xcb_fd;
96 #endif
97
98 typedef struct _xcb_out {
99     pthread_cond_t cond;
100     int writing;
101
102     xcb_return_socket_func_t return_socket;
103     void *socket_closure;
104     unsigned int socket_seq;
105
106     char queue[XCB_QUEUE_BUFFER_SIZE];
107     int queue_len;
108
109     uint64_t request;
110     uint64_t request_written;
111
112     pthread_mutex_t reqlenlock;
113     enum lazy_reply_tag maximum_request_length_tag;
114     union {
115         xcb_big_requests_enable_cookie_t cookie;
116         uint32_t value;
117     } maximum_request_length;
118 #if HAVE_SENDMSG
119     _xcb_fd out_fd;
120 #endif
121 } _xcb_out;
122
123 int _xcb_out_init(_xcb_out *out);
124 void _xcb_out_destroy(_xcb_out *out);
125
126 int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count);
127 void _xcb_out_send_sync(xcb_connection_t *c);
128 int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request);
129
130
131 /* xcb_in.c */
132
133 typedef struct _xcb_in {
134     pthread_cond_t event_cond;
135     int reading;
136
137     char queue[4096];
138     int queue_len;
139
140     uint64_t request_expected;
141     uint64_t request_read;
142     uint64_t request_completed;
143     struct reply_list *current_reply;
144     struct reply_list **current_reply_tail;
145
146     _xcb_map *replies;
147     struct event_list *events;
148     struct event_list **events_tail;
149     struct reader_list *readers;
150
151     struct pending_reply *pending_replies;
152     struct pending_reply **pending_replies_tail;
153 } _xcb_in;
154
155 int _xcb_in_init(_xcb_in *in);
156 void _xcb_in_destroy(_xcb_in *in);
157
158 void _xcb_in_wake_up_next_reader(xcb_connection_t *c);
159
160 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags);
161 void _xcb_in_replies_done(xcb_connection_t *c);
162
163 int _xcb_in_read(xcb_connection_t *c);
164 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int nread);
165
166
167 /* xcb_xid.c */
168
169 typedef struct _xcb_xid {
170     pthread_mutex_t lock;
171     uint32_t last;
172     uint32_t base;
173     uint32_t max;
174     uint32_t inc;
175 } _xcb_xid;
176
177 int _xcb_xid_init(xcb_connection_t *c);
178 void _xcb_xid_destroy(xcb_connection_t *c);
179
180
181 /* xcb_ext.c */
182
183 typedef struct _xcb_ext {
184     pthread_mutex_t lock;
185     struct lazyreply *extensions;
186     int extensions_size;
187 } _xcb_ext;
188
189 int _xcb_ext_init(xcb_connection_t *c);
190 void _xcb_ext_destroy(xcb_connection_t *c);
191
192
193 /* xcb_conn.c */
194
195 struct xcb_connection_t {
196     int has_error;
197
198     /* constant data */
199     xcb_setup_t *setup;
200     int fd;
201
202     /* I/O data */
203     pthread_mutex_t iolock;
204     _xcb_in in;
205     _xcb_out out;
206
207     /* misc data */
208     _xcb_ext ext;
209     _xcb_xid xid;
210 };
211
212 void _xcb_conn_shutdown(xcb_connection_t *c, int err);
213
214 xcb_connection_t *_xcb_conn_ret_error(int err);
215
216 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);
217
218
219 /* xcb_auth.c */
220
221 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display);
222
223 #ifdef GCC_HAS_VISIBILITY
224 #pragma GCC visibility pop
225 #endif
226
227 #endif