Move _xcb_set_fd_flags to xcb_conn.c and make it static. xcb_util.c now has only...
[free-sw/xcb/libxcb] / src / xcb_conn.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* Connection management: the core of XCB. */
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <netinet/in.h>
34 #include <sys/select.h>
35 #include <sys/fcntl.h>
36
37 #include "xcb.h"
38 #include "xcbint.h"
39
40 static int set_fd_flags(const int fd)
41 {
42     long flags = fcntl(fd, F_GETFL, 0);
43     if(flags == -1)
44         return 0;
45     flags |= O_NONBLOCK;
46     if(fcntl(fd, F_SETFL, flags) == -1)
47         return 0;
48     if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
49         return 0;
50     return 1;
51 }
52
53 static int write_setup(XCBConnection *c, XCBAuthInfo *auth_info)
54 {
55     static const char pad[3];
56     XCBConnSetupReq out;
57     struct iovec parts[6];
58     int count = 0;
59     int endian = 0x01020304;
60     int ret;
61
62     memset(&out, 0, sizeof(out));
63
64     /* B = 0x42 = MSB first, l = 0x6c = LSB first */
65     if(htonl(endian) == endian)
66         out.byte_order = 0x42;
67     else
68         out.byte_order = 0x6c;
69     out.protocol_major_version = X_PROTOCOL;
70     out.protocol_minor_version = X_PROTOCOL_REVISION;
71     out.authorization_protocol_name_len = 0;
72     out.authorization_protocol_data_len = 0;
73     parts[count].iov_len = sizeof(XCBConnSetupReq);
74     parts[count++].iov_base = &out;
75     parts[count].iov_len = XCB_PAD(sizeof(XCBConnSetupReq));
76     parts[count++].iov_base = (caddr_t) pad;
77
78     if(auth_info)
79     {
80         parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
81         parts[count++].iov_base = auth_info->name;
82         parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
83         parts[count++].iov_base = (caddr_t) pad;
84         parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
85         parts[count++].iov_base = auth_info->data;
86         parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
87         parts[count++].iov_base = (caddr_t) pad;
88     }
89     assert(count <= sizeof(parts) / sizeof(*parts));
90
91     pthread_mutex_lock(&c->iolock);
92     _xcb_out_write_block(c, parts, count);
93     ret = _xcb_out_flush(c);
94     pthread_mutex_unlock(&c->iolock);
95     return ret;
96 }
97
98 static int read_setup(XCBConnection *c)
99 {
100     /* Read the server response */
101     c->setup = malloc(sizeof(XCBConnSetupGenericRep));
102     if(!c->setup)
103         return 0;
104
105     if(_xcb_in_read_block(c, c->setup, sizeof(XCBConnSetupGenericRep)) != sizeof(XCBConnSetupGenericRep))
106         return 0;
107
108     {
109         void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(XCBConnSetupGenericRep));
110         if(!tmp)
111             return 0;
112         c->setup = tmp;
113     }
114
115     if(_xcb_in_read_block(c, (char *) c->setup + sizeof(XCBConnSetupGenericRep), c->setup->length * 4) <= 0)
116         return 0;
117
118     /* 0 = failed, 2 = authenticate, 1 = success */
119     switch(c->setup->status)
120     {
121     case 0: /* failed */
122         {
123             XCBConnSetupFailedRep *setup = (XCBConnSetupFailedRep *) c->setup;
124             write(STDERR_FILENO, XCBConnSetupFailedRepReason(setup), XCBConnSetupFailedRepReasonLength(setup));
125             return 0;
126         }
127
128     case 2: /* authenticate */
129         {
130             XCBConnSetupAuthenticateRep *setup = (XCBConnSetupAuthenticateRep *) c->setup;
131             write(STDERR_FILENO, XCBConnSetupAuthenticateRepReason(setup), XCBConnSetupAuthenticateRepReasonLength(setup));
132             return 0;
133         }
134     }
135
136     return 1;
137 }
138
139 /* Public interface */
140
141 XCBConnSetupSuccessRep *XCBGetSetup(XCBConnection *c)
142 {
143     /* doesn't need locking because it's never written to. */
144     return c->setup;
145 }
146
147 int XCBGetFileDescriptor(XCBConnection *c)
148 {
149     /* doesn't need locking because it's never written to. */
150     return c->fd;
151 }
152
153 XCBConnection *XCBConnectToFD(int fd, XCBAuthInfo *auth_info)
154 {
155     XCBConnection* c;
156
157     c = calloc(1, sizeof(XCBConnection));
158     if(!c)
159         return 0;
160
161     c->fd = fd;
162
163     if(!(
164         set_fd_flags(fd) &&
165         pthread_mutex_init(&c->iolock, 0) == 0 &&
166         _xcb_in_init(&c->in) &&
167         _xcb_out_init(&c->out) &&
168         write_setup(c, auth_info) &&
169         read_setup(c) &&
170         _xcb_ext_init(c) &&
171         _xcb_xid_init(c)
172         ))
173     {
174         XCBDisconnect(c);
175         return 0;
176     }
177
178     return c;
179 }
180
181 void XCBDisconnect(XCBConnection *c)
182 {
183     if(!c)
184         return;
185
186     free(c->setup);
187     close(c->fd);
188
189     pthread_mutex_destroy(&c->iolock);
190     _xcb_in_destroy(&c->in);
191     _xcb_out_destroy(&c->out);
192
193     _xcb_ext_destroy(c);
194     _xcb_xid_destroy(c);
195
196     free(c);
197 }
198
199 /* Private interface */
200
201 int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond)
202 {
203     int ret;
204     fd_set rfds, wfds;
205
206     /* If the thing I should be doing is already being done, wait for it. */
207     if(should_write ? c->out.writing : c->in.reading)
208     {
209         pthread_cond_wait(cond, &c->iolock);
210         return 1;
211     }
212
213     FD_ZERO(&rfds);
214     FD_SET(c->fd, &rfds);
215     ++c->in.reading;
216
217     FD_ZERO(&wfds);
218     if(should_write)
219     {
220         FD_SET(c->fd, &wfds);
221         ++c->out.writing;
222     }
223
224     pthread_mutex_unlock(&c->iolock);
225     ret = select(c->fd + 1, &rfds, &wfds, 0, 0) > 0;
226     pthread_mutex_lock(&c->iolock);
227
228     if(ret)
229     {
230         if(FD_ISSET(c->fd, &rfds))
231             ret = ret && _xcb_in_read(c);
232
233         if(FD_ISSET(c->fd, &wfds))
234             ret = ret && _xcb_out_write(c);
235     }
236
237     if(should_write)
238         --c->out.writing;
239     --c->in.reading;
240
241     return ret;
242 }