2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include "wayland-util.h"
37 #include "connection.h"
39 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
46 #define MASK(i) ((i) & 4095)
50 const struct wl_message *message;
57 struct wl_connection {
58 struct wl_buffer in, out;
59 struct wl_buffer fds_in, fds_out;
63 wl_connection_update_func_t update;
64 struct wl_closure closure;
70 struct wl_object *object;
72 struct wl_array *array;
76 wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
81 if (head + count <= sizeof b->data) {
82 memcpy(b->data + head, data, count);
84 size = sizeof b->data - head;
85 memcpy(b->data + head, data, size);
86 memcpy(b->data, (const char *) data + size, count - size);
93 wl_buffer_put_iov(struct wl_buffer *b, struct iovec *iov, int *count)
100 iov[0].iov_base = b->data + head;
101 iov[0].iov_len = tail - head;
103 } else if (tail == 0) {
104 iov[0].iov_base = b->data + head;
105 iov[0].iov_len = sizeof b->data - head;
108 iov[0].iov_base = b->data + head;
109 iov[0].iov_len = sizeof b->data - head;
110 iov[1].iov_base = b->data;
111 iov[1].iov_len = tail;
117 wl_buffer_get_iov(struct wl_buffer *b, struct iovec *iov, int *count)
121 head = MASK(b->head);
122 tail = MASK(b->tail);
124 iov[0].iov_base = b->data + tail;
125 iov[0].iov_len = head - tail;
127 } else if (head == 0) {
128 iov[0].iov_base = b->data + tail;
129 iov[0].iov_len = sizeof b->data - tail;
132 iov[0].iov_base = b->data + tail;
133 iov[0].iov_len = sizeof b->data - tail;
134 iov[1].iov_base = b->data;
135 iov[1].iov_len = head;
141 wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
145 tail = MASK(b->tail);
146 if (tail + count <= sizeof b->data) {
147 memcpy(data, b->data + tail, count);
149 size = sizeof b->data - tail;
150 memcpy(data, b->data + tail, size);
151 memcpy((char *) data + size, b->data, count - size);
155 struct wl_connection *
156 wl_connection_create(int fd,
157 wl_connection_update_func_t update,
160 struct wl_connection *connection;
162 connection = malloc(sizeof *connection);
163 memset(connection, 0, sizeof *connection);
165 connection->update = update;
166 connection->data = data;
168 connection->update(connection,
169 WL_CONNECTION_READABLE,
176 wl_connection_destroy(struct wl_connection *connection)
178 close(connection->fd);
183 wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
185 wl_buffer_copy(&connection->in, data, size);
189 wl_connection_consume(struct wl_connection *connection, size_t size)
191 connection->in.tail += size;
192 connection->fds_in.tail = connection->fds_in_tail;
196 build_cmsg(struct wl_buffer *buffer, char *data, int *clen)
198 struct cmsghdr *cmsg;
201 size = buffer->head - buffer->tail;
203 cmsg = (struct cmsghdr *) data;
204 cmsg->cmsg_level = SOL_SOCKET;
205 cmsg->cmsg_type = SCM_RIGHTS;
206 cmsg->cmsg_len = CMSG_LEN(size);
207 wl_buffer_copy(buffer, CMSG_DATA(cmsg), size);
208 *clen = cmsg->cmsg_len;
215 close_fds(struct wl_buffer *buffer)
217 int fds[32], i, count;
220 size = buffer->head - buffer->tail;
224 wl_buffer_copy(buffer, fds, size);
225 count = size / sizeof fds[0];
226 for (i = 0; i < count; i++)
228 buffer->tail += size;
232 decode_cmsg(struct wl_buffer *buffer, struct msghdr *msg)
234 struct cmsghdr *cmsg;
237 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
238 cmsg = CMSG_NXTHDR(msg, cmsg)) {
239 if (cmsg->cmsg_level == SOL_SOCKET &&
240 cmsg->cmsg_type == SCM_RIGHTS) {
241 size = cmsg->cmsg_len - CMSG_LEN(0);
242 wl_buffer_put(buffer, CMSG_DATA(cmsg), size);
248 wl_connection_data(struct wl_connection *connection, uint32_t mask)
253 int len, count, clen;
255 if (mask & WL_CONNECTION_READABLE) {
256 wl_buffer_put_iov(&connection->in, iov, &count);
261 msg.msg_iovlen = count;
262 msg.msg_control = cmsg;
263 msg.msg_controllen = sizeof cmsg;
267 len = recvmsg(connection->fd, &msg, 0);
268 } while (len < 0 && errno == EINTR);
272 "read error from connection %p: %m (%d)\n",
275 } else if (len == 0) {
276 /* FIXME: Handle this better? */
280 decode_cmsg(&connection->fds_in, &msg);
282 connection->in.head += len;
285 if (mask & WL_CONNECTION_WRITABLE) {
286 wl_buffer_get_iov(&connection->out, iov, &count);
288 build_cmsg(&connection->fds_out, cmsg, &clen);
293 msg.msg_iovlen = count;
294 msg.msg_control = cmsg;
295 msg.msg_controllen = clen;
299 len = sendmsg(connection->fd, &msg, 0);
300 } while (len < 0 && errno == EINTR);
304 "write error for connection %p, fd %d: %m\n",
305 connection, connection->fd);
309 close_fds(&connection->fds_out);
311 connection->out.tail += len;
312 if (connection->out.tail == connection->out.head)
313 connection->update(connection,
314 WL_CONNECTION_READABLE,
318 return connection->in.head - connection->in.tail;
322 wl_connection_write(struct wl_connection *connection,
323 const void *data, size_t count)
325 wl_buffer_put(&connection->out, data, count);
327 if (connection->out.head - connection->out.tail == count)
328 connection->update(connection,
329 WL_CONNECTION_READABLE |
330 WL_CONNECTION_WRITABLE,
335 wl_connection_vmarshal(struct wl_connection *connection,
336 struct wl_object *sender,
337 uint32_t opcode, va_list ap,
338 const struct wl_message *message)
340 struct wl_closure *closure = &connection->closure;
341 struct wl_object *object;
342 uint32_t length, *p, size;
344 struct wl_array *array;
348 count = strlen(message->signature);
349 p = &closure->buffer[2];
350 for (i = 0; i < count; i++) {
351 switch (message->signature[i]) {
353 *p++ = va_arg(ap, uint32_t);
356 *p++ = va_arg(ap, int32_t);
359 s = va_arg(ap, const char *);
360 length = s ? strlen(s) + 1: 0;
362 memcpy(p, s, length);
363 p += DIV_ROUNDUP(length, sizeof(*p));
367 object = va_arg(ap, struct wl_object *);
368 *p++ = object ? object->id : 0;
371 array = va_arg(ap, struct wl_array *);
372 if (array == NULL || array->size == 0) {
377 memcpy(p, array->data, array->size);
378 p = (void *) p + array->size;
381 fd = va_arg(ap, int);
384 fprintf(stderr, "dup failed: %m");
387 wl_buffer_put(&connection->fds_out,
388 &dup_fd, sizeof dup_fd);
396 size = (p - closure->buffer) * sizeof *p;
397 closure->buffer[0] = sender->id;
398 closure->buffer[1] = opcode | (size << 16);
399 wl_connection_write(connection, closure->buffer, size);
403 wl_message_size_extra(const struct wl_message *message)
407 for (i = 0, extra = 0; message->signature[i]; i++) {
409 switch (message->signature[i - 2]) {
412 extra += sizeof (void *);
415 extra += sizeof (void *) + sizeof (struct wl_array);
418 extra += sizeof (uint32_t);
428 wl_connection_demarshal(struct wl_connection *connection,
430 struct wl_hash_table *objects,
431 const struct wl_message *message)
433 uint32_t *p, *next, *end, length;
436 int i, count, extra_space;
437 struct wl_object **object;
438 struct wl_array **array;
439 struct wl_closure *closure = &connection->closure;
441 count = strlen(message->signature) + 2;
442 if (count > ARRAY_LENGTH(closure->types)) {
443 printf("too many args (%d)\n", count);
447 extra_space = wl_message_size_extra(message);
448 if (sizeof closure->buffer < size + extra_space) {
449 printf("request too big, should malloc tmp buffer here\n");
453 closure->message = message;
454 closure->types[0] = &ffi_type_pointer;
455 closure->types[1] = &ffi_type_pointer;
457 wl_connection_copy(connection, closure->buffer, size);
458 p = &closure->buffer[2];
459 end = (uint32_t *) ((char *) (p + size));
460 extra = (char *) end;
461 for (i = 2; i < count; i++) {
463 printf("message too short, "
464 "object (%d), message %s(%s)\n",
465 *p, message->name, message->signature);
470 switch (message->signature[i - 2]) {
472 closure->types[i] = &ffi_type_uint32;
473 closure->args[i] = p++;
476 closure->types[i] = &ffi_type_sint32;
477 closure->args[i] = p++;
480 closure->types[i] = &ffi_type_pointer;
483 next = p + DIV_ROUNDUP(length, sizeof *p);
485 printf("message too short, "
486 "object (%d), message %s(%s)\n",
487 *p, message->name, message->signature);
494 closure->args[i] = s;
502 if (length > 0 && (*s)[length - 1] != '\0') {
503 printf("string not nul-terminated, "
505 message->name, message->signature);
512 closure->types[i] = &ffi_type_pointer;
513 object = (struct wl_object **) extra;
514 extra += sizeof *object;
515 closure->args[i] = object;
517 *object = wl_hash_table_lookup(objects, *p);
518 if (*object == NULL && *p != 0) {
519 printf("unknown object (%d), message %s(%s)\n",
520 *p, message->name, message->signature);
528 closure->types[i] = &ffi_type_uint32;
529 closure->args[i] = p;
530 object = wl_hash_table_lookup(objects, *p);
531 if (object != NULL) {
532 printf("not a new object (%d), "
534 *p, message->name, message->signature);
541 closure->types[i] = &ffi_type_pointer;
544 next = p + DIV_ROUNDUP(length, sizeof *p);
546 printf("message too short, "
547 "object (%d), message %s(%s)\n",
548 *p, message->name, message->signature);
553 array = (struct wl_array **) extra;
554 extra += sizeof *array;
555 closure->args[i] = array;
557 *array = (struct wl_array *) extra;
558 extra += sizeof **array;
560 (*array)->size = length;
566 closure->types[i] = &ffi_type_sint;
570 closure->args[i] = fd;
572 wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
573 connection->fds_in.tail += sizeof *fd;
576 printf("unknown type\n");
583 ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
584 closure->count, &ffi_type_uint32, closure->types);
586 wl_connection_consume(connection, size);
592 wl_closure_destroy(closure);
598 wl_closure_invoke(struct wl_closure *closure,
599 struct wl_object *target, void (*func)(void), void *data)
603 closure->args[0] = &data;
604 closure->args[1] = ⌖
606 ffi_call(&closure->cif, func, &result, closure->args);
610 wl_closure_print(struct wl_closure *closure, struct wl_object *target)
612 union wl_value *value;
615 fprintf(stderr, "%s(%d).%s(",
616 target->interface->name, target->id,
617 closure->message->name);
619 for (i = 2; i < closure->count; i++) {
621 fprintf(stderr, ", ");
623 value = closure->args[i];
624 switch (closure->message->signature[i - 2]) {
626 fprintf(stderr, "%u", value->uint32);
629 fprintf(stderr, "%d", value->uint32);
632 fprintf(stderr, "\"%s\"", value->string);
635 fprintf(stderr, "object %u",
636 value->object ? value->object->id : 0);
639 fprintf(stderr, "new id %u", value->uint32);
642 fprintf(stderr, "array");
645 fprintf(stderr, "fd %d", value->uint32);
650 fprintf(stderr, ")\n");
654 wl_closure_destroy(struct wl_closure *closure)