Make the return value of _xcb_conn_wait boolean, instead of syscall-like.
[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
36 #include "xcb.h"
37 #include "xcbint.h"
38
39 static int write_setup(XCBConnection *c, XCBAuthInfo *auth_info)
40 {
41     XCBConnSetupReq out;
42     struct iovec parts[3];
43     int count = 0;
44     int endian = 0x01020304;
45     int ret;
46
47     memset(&out, 0, sizeof(out));
48
49     /* B = 0x42 = MSB first, l = 0x6c = LSB first */
50     if(htonl(endian) == endian)
51         out.byte_order = 0x42;
52     else
53         out.byte_order = 0x6c;
54     out.protocol_major_version = X_PROTOCOL;
55     out.protocol_minor_version = X_PROTOCOL_REVISION;
56     out.authorization_protocol_name_len = 0;
57     out.authorization_protocol_data_len = 0;
58     parts[count].iov_len = sizeof(XCBConnSetupReq);
59     parts[count++].iov_base = &out;
60
61     if(auth_info)
62     {
63         parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
64         parts[count++].iov_base = auth_info->name;
65         parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
66         parts[count++].iov_base = auth_info->data;
67     }
68
69     pthread_mutex_lock(&c->iolock);
70     _xcb_out_write_block(c, parts, count);
71     ret = _xcb_out_flush(c);
72     pthread_mutex_unlock(&c->iolock);
73     if(ret <= 0)
74         return 0;
75     return 1;
76 }
77
78 static int read_setup(XCBConnection *c)
79 {
80     /* Read the server response */
81     c->setup = malloc(sizeof(XCBConnSetupGenericRep));
82     if(!c->setup)
83         return 0;
84
85     if(_xcb_read_block(c->fd, c->setup, sizeof(XCBConnSetupGenericRep)) != sizeof(XCBConnSetupGenericRep))
86         return 0;
87
88     {
89         void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(XCBConnSetupGenericRep));
90         if(!tmp)
91             return 0;
92         c->setup = tmp;
93     }
94
95     if(_xcb_read_block(c->fd, (char *) c->setup + sizeof(XCBConnSetupGenericRep), c->setup->length * 4) <= 0)
96         return 0;
97
98     /* 0 = failed, 2 = authenticate, 1 = success */
99     switch(c->setup->status)
100     {
101     case 0: /* failed */
102         {
103             XCBConnSetupFailedRep *setup = (XCBConnSetupFailedRep *) c->setup;
104             write(STDERR_FILENO, XCBConnSetupFailedRepReason(setup), XCBConnSetupFailedRepReasonLength(setup));
105             return 0;
106         }
107
108     case 2: /* authenticate */
109         {
110             XCBConnSetupAuthenticateRep *setup = (XCBConnSetupAuthenticateRep *) c->setup;
111             write(STDERR_FILENO, XCBConnSetupAuthenticateRepReason(setup), XCBConnSetupAuthenticateRepReasonLength(setup));
112             return 0;
113         }
114     }
115
116     return 1;
117 }
118
119 /* Public interface */
120
121 XCBConnSetupSuccessRep *XCBGetSetup(XCBConnection *c)
122 {
123     /* doesn't need locking because it's never written to. */
124     return c->setup;
125 }
126
127 int XCBGetFileDescriptor(XCBConnection *c)
128 {
129     /* doesn't need locking because it's never written to. */
130     return c->fd;
131 }
132
133 XCBConnection *XCBConnectToFD(int fd, XCBAuthInfo *auth_info)
134 {
135     XCBConnection* c;
136
137     c = calloc(1, sizeof(XCBConnection));
138     if(!c)
139         return 0;
140
141     c->fd = fd;
142
143     if(!(
144         _xcb_set_fd_flags(fd) &&
145         pthread_mutex_init(&c->iolock, 0) == 0 &&
146         _xcb_in_init(&c->in) &&
147         _xcb_out_init(&c->out) &&
148         write_setup(c, auth_info) &&
149         read_setup(c) &&
150         _xcb_ext_init(c) &&
151         _xcb_xid_init(c)
152         ))
153     {
154         XCBDisconnect(c);
155         return 0;
156     }
157
158     return c;
159 }
160
161 void XCBDisconnect(XCBConnection *c)
162 {
163     if(!c)
164         return;
165
166     free(c->setup);
167     close(c->fd);
168
169     pthread_mutex_destroy(&c->iolock);
170     _xcb_in_destroy(&c->in);
171     _xcb_out_destroy(&c->out);
172
173     _xcb_ext_destroy(c);
174     _xcb_xid_destroy(c);
175
176     free(c);
177 }
178
179 /* Private interface */
180
181 int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond)
182 {
183     int ret = 1;
184     fd_set rfds, wfds;
185
186     _xcb_assert_valid_sequence(c);
187
188     /* If the thing I should be doing is already being done, wait for it. */
189     if(should_write ? c->out.writing : c->in.reading)
190     {
191         pthread_cond_wait(cond, &c->iolock);
192         return 1;
193     }
194
195     FD_ZERO(&rfds);
196     FD_SET(c->fd, &rfds);
197     ++c->in.reading;
198
199     FD_ZERO(&wfds);
200     if(should_write)
201     {
202         FD_SET(c->fd, &wfds);
203         ++c->out.writing;
204     }
205
206     pthread_mutex_unlock(&c->iolock);
207     ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
208     pthread_mutex_lock(&c->iolock);
209
210     if(ret <= 0) /* error: select failed */
211         goto done;
212
213     if(FD_ISSET(c->fd, &rfds))
214         if((ret = _xcb_in_read(c)) <= 0)
215             goto done;
216
217     if(FD_ISSET(c->fd, &wfds))
218         if((ret = _xcb_out_write(c)) <= 0)
219             goto done;
220
221 done:
222     if(should_write)
223         --c->out.writing;
224     --c->in.reading;
225
226     return ret > 0;
227 }