2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/uio.h>
|
2008-12-25 03:30:25 +03:00
|
|
|
#include <ffi.h>
|
|
|
|
#include <assert.h>
|
2010-08-06 01:44:31 +04:00
|
|
|
#include <fcntl.h>
|
2010-08-09 22:34:11 +04:00
|
|
|
#include <unistd.h>
|
2010-08-26 01:11:29 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2008-10-08 20:48:46 +04:00
|
|
|
|
2008-12-22 05:50:23 +03:00
|
|
|
#include "wayland-util.h"
|
2008-10-08 20:48:46 +04:00
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
|
|
|
|
|
|
|
|
struct wl_buffer {
|
|
|
|
char data[4096];
|
|
|
|
int head, tail;
|
|
|
|
};
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
#define MASK(i) ((i) & 4095)
|
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
struct wl_closure {
|
|
|
|
int count;
|
|
|
|
const struct wl_message *message;
|
|
|
|
ffi_type *types[20];
|
|
|
|
ffi_cif cif;
|
|
|
|
void *args[20];
|
2010-09-07 23:26:39 +04:00
|
|
|
uint32_t buffer[64];
|
2010-09-08 05:34:45 +04:00
|
|
|
uint32_t *start;
|
2010-08-30 17:47:36 +04:00
|
|
|
};
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
struct wl_connection {
|
|
|
|
struct wl_buffer in, out;
|
2010-08-27 05:49:44 +04:00
|
|
|
struct wl_buffer fds_in, fds_out;
|
|
|
|
int fds_in_tail;
|
2008-10-08 20:48:46 +04:00
|
|
|
int fd;
|
|
|
|
void *data;
|
|
|
|
wl_connection_update_func_t update;
|
2010-08-30 17:47:36 +04:00
|
|
|
struct wl_closure closure;
|
2008-10-08 20:48:46 +04:00
|
|
|
};
|
|
|
|
|
2010-09-08 01:00:34 +04:00
|
|
|
union wl_value {
|
|
|
|
uint32_t uint32;
|
|
|
|
char *string;
|
|
|
|
struct wl_object *object;
|
|
|
|
uint32_t new_id;
|
|
|
|
struct wl_array *array;
|
|
|
|
};
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
static void
|
|
|
|
wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
|
|
|
|
{
|
|
|
|
int head, size;
|
|
|
|
|
|
|
|
head = MASK(b->head);
|
|
|
|
if (head + count <= sizeof b->data) {
|
|
|
|
memcpy(b->data + head, data, count);
|
|
|
|
} else {
|
|
|
|
size = sizeof b->data - head;
|
|
|
|
memcpy(b->data + head, data, size);
|
|
|
|
memcpy(b->data, (const char *) data + size, count - size);
|
|
|
|
}
|
|
|
|
|
|
|
|
b->head += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wl_buffer_put_iov(struct wl_buffer *b, struct iovec *iov, int *count)
|
|
|
|
{
|
|
|
|
int head, tail;
|
|
|
|
|
|
|
|
head = MASK(b->head);
|
|
|
|
tail = MASK(b->tail);
|
|
|
|
if (head < tail) {
|
|
|
|
iov[0].iov_base = b->data + head;
|
|
|
|
iov[0].iov_len = tail - head;
|
|
|
|
*count = 1;
|
|
|
|
} else if (tail == 0) {
|
|
|
|
iov[0].iov_base = b->data + head;
|
|
|
|
iov[0].iov_len = sizeof b->data - head;
|
|
|
|
*count = 1;
|
|
|
|
} else {
|
|
|
|
iov[0].iov_base = b->data + head;
|
|
|
|
iov[0].iov_len = sizeof b->data - head;
|
|
|
|
iov[1].iov_base = b->data;
|
|
|
|
iov[1].iov_len = tail;
|
|
|
|
*count = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wl_buffer_get_iov(struct wl_buffer *b, struct iovec *iov, int *count)
|
|
|
|
{
|
|
|
|
int head, tail;
|
|
|
|
|
|
|
|
head = MASK(b->head);
|
|
|
|
tail = MASK(b->tail);
|
|
|
|
if (tail < head) {
|
|
|
|
iov[0].iov_base = b->data + tail;
|
|
|
|
iov[0].iov_len = head - tail;
|
|
|
|
*count = 1;
|
|
|
|
} else if (head == 0) {
|
|
|
|
iov[0].iov_base = b->data + tail;
|
|
|
|
iov[0].iov_len = sizeof b->data - tail;
|
|
|
|
*count = 1;
|
|
|
|
} else {
|
|
|
|
iov[0].iov_base = b->data + tail;
|
|
|
|
iov[0].iov_len = sizeof b->data - tail;
|
|
|
|
iov[1].iov_base = b->data;
|
|
|
|
iov[1].iov_len = head;
|
|
|
|
*count = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
|
|
|
|
{
|
|
|
|
int tail, size;
|
|
|
|
|
|
|
|
tail = MASK(b->tail);
|
|
|
|
if (tail + count <= sizeof b->data) {
|
|
|
|
memcpy(data, b->data + tail, count);
|
|
|
|
} else {
|
|
|
|
size = sizeof b->data - tail;
|
|
|
|
memcpy(data, b->data + tail, size);
|
|
|
|
memcpy((char *) data + size, b->data, count - size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
struct wl_connection *
|
|
|
|
wl_connection_create(int fd,
|
|
|
|
wl_connection_update_func_t update,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct wl_connection *connection;
|
|
|
|
|
|
|
|
connection = malloc(sizeof *connection);
|
|
|
|
memset(connection, 0, sizeof *connection);
|
|
|
|
connection->fd = fd;
|
|
|
|
connection->update = update;
|
|
|
|
connection->data = data;
|
|
|
|
|
|
|
|
connection->update(connection,
|
|
|
|
WL_CONNECTION_READABLE,
|
|
|
|
connection->data);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wl_connection_destroy(struct wl_connection *connection)
|
|
|
|
{
|
2010-08-06 01:44:31 +04:00
|
|
|
close(connection->fd);
|
2008-10-08 20:48:46 +04:00
|
|
|
free(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
|
|
|
|
{
|
2010-08-27 01:44:31 +04:00
|
|
|
wl_buffer_copy(&connection->in, data, size);
|
2008-10-08 20:48:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wl_connection_consume(struct wl_connection *connection, size_t size)
|
|
|
|
{
|
2010-08-27 01:44:31 +04:00
|
|
|
connection->in.tail += size;
|
2010-08-27 05:49:44 +04:00
|
|
|
connection->fds_in.tail = connection->fds_in_tail;
|
2008-10-08 20:48:46 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 05:49:44 +04:00
|
|
|
static void
|
|
|
|
build_cmsg(struct wl_buffer *buffer, char *data, int *clen)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = buffer->head - buffer->tail;
|
|
|
|
if (size > 0) {
|
|
|
|
cmsg = (struct cmsghdr *) data;
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(size);
|
|
|
|
wl_buffer_copy(buffer, CMSG_DATA(cmsg), size);
|
|
|
|
*clen = cmsg->cmsg_len;
|
|
|
|
} else {
|
|
|
|
*clen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_fds(struct wl_buffer *buffer)
|
|
|
|
{
|
|
|
|
int fds[32], i, count;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = buffer->head - buffer->tail;
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wl_buffer_copy(buffer, fds, size);
|
2010-09-08 01:03:17 +04:00
|
|
|
count = size / sizeof fds[0];
|
2010-08-27 05:49:44 +04:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
close(fds[i]);
|
|
|
|
buffer->tail += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_cmsg(struct wl_buffer *buffer, struct msghdr *msg)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
|
|
|
|
cmsg = CMSG_NXTHDR(msg, cmsg)) {
|
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET &&
|
|
|
|
cmsg->cmsg_type == SCM_RIGHTS) {
|
|
|
|
size = cmsg->cmsg_len - CMSG_LEN(0);
|
|
|
|
wl_buffer_put(buffer, CMSG_DATA(cmsg), size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wl_connection_data(struct wl_connection *connection, uint32_t mask)
|
2008-10-08 20:48:46 +04:00
|
|
|
{
|
|
|
|
struct iovec iov[2];
|
2010-08-26 01:11:29 +04:00
|
|
|
struct msghdr msg;
|
2010-08-27 05:49:44 +04:00
|
|
|
char cmsg[128];
|
|
|
|
int len, count, clen;
|
2008-10-08 20:48:46 +04:00
|
|
|
|
|
|
|
if (mask & WL_CONNECTION_READABLE) {
|
2010-08-27 01:44:31 +04:00
|
|
|
wl_buffer_put_iov(&connection->in, iov, &count);
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = count;
|
2010-08-27 05:49:44 +04:00
|
|
|
msg.msg_control = cmsg;
|
|
|
|
msg.msg_controllen = sizeof cmsg;
|
|
|
|
msg.msg_flags = 0;
|
2010-08-27 01:44:31 +04:00
|
|
|
|
2008-12-09 18:47:36 +03:00
|
|
|
do {
|
2010-08-26 01:11:29 +04:00
|
|
|
len = recvmsg(connection->fd, &msg, 0);
|
2008-12-09 18:47:36 +03:00
|
|
|
} while (len < 0 && errno == EINTR);
|
2010-08-27 01:44:31 +04:00
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"read error from connection %p: %m (%d)\n",
|
|
|
|
connection, errno);
|
|
|
|
return -1;
|
|
|
|
} else if (len == 0) {
|
|
|
|
/* FIXME: Handle this better? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-27 05:49:44 +04:00
|
|
|
decode_cmsg(&connection->fds_in, &msg);
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
connection->in.head += len;
|
2008-10-08 20:48:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & WL_CONNECTION_WRITABLE) {
|
2010-08-27 01:44:31 +04:00
|
|
|
wl_buffer_get_iov(&connection->out, iov, &count);
|
|
|
|
|
2010-08-27 05:49:44 +04:00
|
|
|
build_cmsg(&connection->fds_out, cmsg, &clen);
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = count;
|
2010-08-27 05:49:44 +04:00
|
|
|
msg.msg_control = cmsg;
|
|
|
|
msg.msg_controllen = clen;
|
|
|
|
msg.msg_flags = 0;
|
2010-08-27 01:44:31 +04:00
|
|
|
|
2008-12-09 18:47:36 +03:00
|
|
|
do {
|
2010-08-26 01:11:29 +04:00
|
|
|
len = sendmsg(connection->fd, &msg, 0);
|
2008-12-09 18:47:36 +03:00
|
|
|
} while (len < 0 && errno == EINTR);
|
2010-08-27 01:44:31 +04:00
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
if (len < 0) {
|
2010-08-27 05:49:44 +04:00
|
|
|
fprintf(stderr,
|
|
|
|
"write error for connection %p, fd %d: %m\n",
|
|
|
|
connection, connection->fd);
|
2008-10-08 20:48:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-27 05:49:44 +04:00
|
|
|
close_fds(&connection->fds_out);
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
connection->out.tail += len;
|
|
|
|
if (connection->out.tail == connection->out.head)
|
2008-10-08 20:48:46 +04:00
|
|
|
connection->update(connection,
|
|
|
|
WL_CONNECTION_READABLE,
|
|
|
|
connection->data);
|
|
|
|
}
|
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
return connection->in.head - connection->in.tail;
|
2008-10-08 20:48:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-27 01:44:31 +04:00
|
|
|
wl_connection_write(struct wl_connection *connection,
|
|
|
|
const void *data, size_t count)
|
2008-10-08 20:48:46 +04:00
|
|
|
{
|
2010-08-27 01:44:31 +04:00
|
|
|
wl_buffer_put(&connection->out, data, count);
|
2008-10-08 20:48:46 +04:00
|
|
|
|
2010-08-27 01:44:31 +04:00
|
|
|
if (connection->out.head - connection->out.tail == count)
|
2008-10-08 20:48:46 +04:00
|
|
|
connection->update(connection,
|
|
|
|
WL_CONNECTION_READABLE |
|
|
|
|
WL_CONNECTION_WRITABLE,
|
|
|
|
connection->data);
|
|
|
|
}
|
2008-12-25 03:30:25 +03:00
|
|
|
|
2010-09-08 04:57:40 +04:00
|
|
|
static int
|
|
|
|
wl_message_size_extra(const struct wl_message *message)
|
|
|
|
{
|
|
|
|
int i, extra;
|
|
|
|
|
|
|
|
for (i = 0, extra = 0; message->signature[i]; i++) {
|
|
|
|
|
|
|
|
switch (message->signature[i]) {
|
|
|
|
case 's':
|
|
|
|
case 'o':
|
|
|
|
extra += sizeof (void *);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
extra += sizeof (void *) + sizeof (struct wl_array);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
extra += sizeof (uint32_t);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return extra;
|
|
|
|
}
|
|
|
|
|
2010-09-08 05:34:45 +04:00
|
|
|
struct wl_closure *
|
2008-12-25 03:30:25 +03:00
|
|
|
wl_connection_vmarshal(struct wl_connection *connection,
|
|
|
|
struct wl_object *sender,
|
|
|
|
uint32_t opcode, va_list ap,
|
|
|
|
const struct wl_message *message)
|
|
|
|
{
|
2010-09-07 23:26:39 +04:00
|
|
|
struct wl_closure *closure = &connection->closure;
|
2010-09-08 04:57:40 +04:00
|
|
|
struct wl_object **objectp, *object;
|
|
|
|
uint32_t length, *p, *start, size;
|
2010-09-08 01:03:17 +04:00
|
|
|
int dup_fd;
|
2010-09-08 04:57:40 +04:00
|
|
|
struct wl_array **arrayp, *array;
|
|
|
|
const char **sp, *s;
|
|
|
|
char *extra;
|
|
|
|
int i, count, fd, extra_size;
|
2008-12-25 03:30:25 +03:00
|
|
|
|
2010-09-08 04:57:40 +04:00
|
|
|
extra_size = wl_message_size_extra(message);
|
|
|
|
closure->message = message;
|
|
|
|
count = strlen(message->signature) + 2;
|
|
|
|
extra = (char *) closure->buffer;
|
|
|
|
start = &closure->buffer[DIV_ROUNDUP(extra_size, sizeof *p)];
|
|
|
|
p = &start[2];
|
|
|
|
for (i = 2; i < count; i++) {
|
|
|
|
switch (message->signature[i - 2]) {
|
2008-12-25 03:30:25 +03:00
|
|
|
case 'u':
|
2010-09-08 04:57:40 +04:00
|
|
|
closure->types[i] = &ffi_type_uint32;
|
|
|
|
closure->args[i] = p;
|
2008-12-25 03:30:25 +03:00
|
|
|
*p++ = va_arg(ap, uint32_t);
|
|
|
|
break;
|
2010-09-08 01:03:17 +04:00
|
|
|
case 'i':
|
2010-09-08 04:57:40 +04:00
|
|
|
closure->types[i] = &ffi_type_sint32;
|
|
|
|
closure->args[i] = p;
|
2010-09-08 01:03:17 +04:00
|
|
|
*p++ = va_arg(ap, int32_t);
|
|
|
|
break;
|
2008-12-25 03:30:25 +03:00
|
|
|
case 's':
|
2010-09-08 04:57:40 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
|
|
|
closure->args[i] = extra;
|
|
|
|
sp = (const char **) extra;
|
|
|
|
extra += sizeof *sp;
|
|
|
|
|
2008-12-25 03:30:25 +03:00
|
|
|
s = va_arg(ap, const char *);
|
2010-09-07 23:33:56 +04:00
|
|
|
length = s ? strlen(s) + 1: 0;
|
2008-12-25 03:30:25 +03:00
|
|
|
*p++ = length;
|
2010-09-08 04:57:40 +04:00
|
|
|
|
|
|
|
*sp = (const char *) p;
|
|
|
|
|
2008-12-25 03:30:25 +03:00
|
|
|
memcpy(p, s, length);
|
2010-09-08 04:57:40 +04:00
|
|
|
p += DIV_ROUNDUP(length, sizeof *p);
|
2008-12-25 03:30:25 +03:00
|
|
|
break;
|
|
|
|
case 'o':
|
2010-09-08 04:57:40 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
|
|
|
closure->args[i] = extra;
|
|
|
|
objectp = (struct wl_object **) extra;
|
|
|
|
extra += sizeof *objectp;
|
|
|
|
|
2008-12-25 03:30:25 +03:00
|
|
|
object = va_arg(ap, struct wl_object *);
|
2010-09-08 04:57:40 +04:00
|
|
|
*objectp = object;
|
2009-02-23 04:57:57 +03:00
|
|
|
*p++ = object ? object->id : 0;
|
2008-12-25 03:30:25 +03:00
|
|
|
break;
|
2010-09-08 04:57:40 +04:00
|
|
|
|
|
|
|
case 'n':
|
|
|
|
closure->types[i] = &ffi_type_uint32;
|
|
|
|
closure->args[i] = p;
|
|
|
|
object = va_arg(ap, struct wl_object *);
|
|
|
|
*p++ = object->id;
|
|
|
|
break;
|
|
|
|
|
2009-02-24 06:30:29 +03:00
|
|
|
case 'a':
|
2010-09-08 04:57:40 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
|
|
|
closure->args[i] = extra;
|
|
|
|
arrayp = (struct wl_array **) extra;
|
|
|
|
extra += sizeof *arrayp;
|
|
|
|
|
|
|
|
*arrayp = (struct wl_array *) extra;
|
|
|
|
extra += sizeof **arrayp;
|
|
|
|
|
2009-02-24 06:30:29 +03:00
|
|
|
array = va_arg(ap, struct wl_array *);
|
|
|
|
if (array == NULL || array->size == 0) {
|
|
|
|
*p++ = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p++ = array->size;
|
|
|
|
memcpy(p, array->data, array->size);
|
2010-09-08 04:57:40 +04:00
|
|
|
|
|
|
|
(*arrayp)->size = array->size;
|
|
|
|
(*arrayp)->alloc = array->alloc;
|
|
|
|
(*arrayp)->data = p;
|
|
|
|
|
|
|
|
p += DIV_ROUNDUP(array->size, sizeof *p);
|
2009-02-24 06:30:29 +03:00
|
|
|
break;
|
2010-09-08 04:57:40 +04:00
|
|
|
|
2010-08-27 05:49:44 +04:00
|
|
|
case 'h':
|
|
|
|
fd = va_arg(ap, int);
|
|
|
|
dup_fd = dup(fd);
|
|
|
|
if (dup_fd < 0) {
|
|
|
|
fprintf(stderr, "dup failed: %m");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
wl_buffer_put(&connection->fds_out,
|
|
|
|
&dup_fd, sizeof dup_fd);
|
|
|
|
break;
|
2008-12-25 03:30:25 +03:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-08 04:57:40 +04:00
|
|
|
size = (p - start) * sizeof *p;
|
|
|
|
start[0] = sender->id;
|
|
|
|
start[1] = opcode | (size << 16);
|
2010-09-08 05:34:45 +04:00
|
|
|
closure->start = start;
|
|
|
|
|
|
|
|
return closure;
|
2010-09-08 01:00:34 +04:00
|
|
|
}
|
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
struct wl_closure *
|
2008-12-25 03:30:25 +03:00
|
|
|
wl_connection_demarshal(struct wl_connection *connection,
|
|
|
|
uint32_t size,
|
2010-02-26 19:42:59 +03:00
|
|
|
struct wl_hash_table *objects,
|
2008-12-25 03:30:25 +03:00
|
|
|
const struct wl_message *message)
|
|
|
|
{
|
2010-09-08 01:03:17 +04:00
|
|
|
uint32_t *p, *next, *end, length;
|
|
|
|
int *fd;
|
2010-09-08 01:00:34 +04:00
|
|
|
char *extra, **s;
|
|
|
|
int i, count, extra_space;
|
|
|
|
struct wl_object **object;
|
|
|
|
struct wl_array **array;
|
2010-08-30 17:47:36 +04:00
|
|
|
struct wl_closure *closure = &connection->closure;
|
2008-12-25 03:30:25 +03:00
|
|
|
|
|
|
|
count = strlen(message->signature) + 2;
|
2010-08-30 17:47:36 +04:00
|
|
|
if (count > ARRAY_LENGTH(closure->types)) {
|
2008-12-25 03:30:25 +03:00
|
|
|
printf("too many args (%d)\n", count);
|
2010-08-09 22:34:11 +04:00
|
|
|
assert(0);
|
2008-12-25 03:30:25 +03:00
|
|
|
}
|
|
|
|
|
2010-09-08 01:00:34 +04:00
|
|
|
extra_space = wl_message_size_extra(message);
|
|
|
|
if (sizeof closure->buffer < size + extra_space) {
|
2008-12-25 03:30:25 +03:00
|
|
|
printf("request too big, should malloc tmp buffer here\n");
|
2010-08-09 22:34:11 +04:00
|
|
|
assert(0);
|
2008-12-25 03:30:25 +03:00
|
|
|
}
|
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->message = message;
|
|
|
|
closure->types[0] = &ffi_type_pointer;
|
|
|
|
closure->types[1] = &ffi_type_pointer;
|
2008-12-25 03:30:25 +03:00
|
|
|
|
2010-09-07 23:26:39 +04:00
|
|
|
wl_connection_copy(connection, closure->buffer, size);
|
|
|
|
p = &closure->buffer[2];
|
2010-08-09 22:34:11 +04:00
|
|
|
end = (uint32_t *) ((char *) (p + size));
|
2010-09-07 23:51:41 +04:00
|
|
|
extra = (char *) end;
|
2008-12-25 03:30:25 +03:00
|
|
|
for (i = 2; i < count; i++) {
|
2010-08-09 22:34:11 +04:00
|
|
|
if (p + 1 > end) {
|
|
|
|
printf("message too short, "
|
|
|
|
"object (%d), message %s(%s)\n",
|
|
|
|
*p, message->name, message->signature);
|
|
|
|
errno = EINVAL;
|
2010-08-30 17:47:36 +04:00
|
|
|
goto err;
|
2010-08-09 22:34:11 +04:00
|
|
|
}
|
|
|
|
|
2008-12-25 03:30:25 +03:00
|
|
|
switch (message->signature[i - 2]) {
|
|
|
|
case 'u':
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->types[i] = &ffi_type_uint32;
|
2010-09-08 01:00:34 +04:00
|
|
|
closure->args[i] = p++;
|
2008-12-25 03:30:25 +03:00
|
|
|
break;
|
2010-09-08 01:03:17 +04:00
|
|
|
case 'i':
|
|
|
|
closure->types[i] = &ffi_type_sint32;
|
|
|
|
closure->args[i] = p++;
|
|
|
|
break;
|
2008-12-25 03:30:25 +03:00
|
|
|
case 's':
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
2008-12-25 03:30:25 +03:00
|
|
|
length = *p++;
|
2010-08-09 22:34:11 +04:00
|
|
|
|
|
|
|
next = p + DIV_ROUNDUP(length, sizeof *p);
|
|
|
|
if (next > end) {
|
|
|
|
printf("message too short, "
|
|
|
|
"object (%d), message %s(%s)\n",
|
|
|
|
*p, message->name, message->signature);
|
|
|
|
errno = EINVAL;
|
2010-08-30 17:47:36 +04:00
|
|
|
goto err;
|
2010-08-09 22:34:11 +04:00
|
|
|
}
|
|
|
|
|
2010-09-08 01:00:34 +04:00
|
|
|
s = (char **) extra;
|
|
|
|
extra += sizeof *s;
|
|
|
|
closure->args[i] = s;
|
|
|
|
|
2010-08-28 06:16:50 +04:00
|
|
|
if (length == 0) {
|
2010-09-08 01:00:34 +04:00
|
|
|
*s = NULL;
|
2010-08-28 06:16:50 +04:00
|
|
|
} else {
|
2010-09-08 01:00:34 +04:00
|
|
|
*s = (char *) p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0 && (*s)[length - 1] != '\0') {
|
|
|
|
printf("string not nul-terminated, "
|
|
|
|
"message %s(%s)\n",
|
|
|
|
message->name, message->signature);
|
|
|
|
errno = EINVAL;
|
|
|
|
goto err;
|
2008-12-25 03:30:25 +03:00
|
|
|
}
|
2010-08-09 22:34:11 +04:00
|
|
|
p = next;
|
2008-12-25 03:30:25 +03:00
|
|
|
break;
|
|
|
|
case 'o':
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
2010-09-08 01:00:34 +04:00
|
|
|
object = (struct wl_object **) extra;
|
|
|
|
extra += sizeof *object;
|
|
|
|
closure->args[i] = object;
|
|
|
|
|
|
|
|
*object = wl_hash_table_lookup(objects, *p);
|
|
|
|
if (*object == NULL && *p != 0) {
|
2010-07-29 07:47:16 +04:00
|
|
|
printf("unknown object (%d), message %s(%s)\n",
|
|
|
|
*p, message->name, message->signature);
|
2010-08-09 22:34:11 +04:00
|
|
|
errno = EINVAL;
|
2010-08-30 17:47:36 +04:00
|
|
|
goto err;
|
2010-08-09 22:34:11 +04:00
|
|
|
}
|
2010-09-08 01:00:34 +04:00
|
|
|
|
2008-12-25 03:30:25 +03:00
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->types[i] = &ffi_type_uint32;
|
2010-09-08 01:00:34 +04:00
|
|
|
closure->args[i] = p;
|
2010-02-26 19:42:59 +03:00
|
|
|
object = wl_hash_table_lookup(objects, *p);
|
2010-08-09 22:34:11 +04:00
|
|
|
if (object != NULL) {
|
|
|
|
printf("not a new object (%d), "
|
|
|
|
"message %s(%s)\n",
|
|
|
|
*p, message->name, message->signature);
|
|
|
|
errno = EINVAL;
|
2010-08-30 17:47:36 +04:00
|
|
|
goto err;
|
2010-08-09 22:34:11 +04:00
|
|
|
}
|
2008-12-25 03:30:25 +03:00
|
|
|
p++;
|
|
|
|
break;
|
2009-02-24 06:30:29 +03:00
|
|
|
case 'a':
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->types[i] = &ffi_type_pointer;
|
2009-02-24 06:30:29 +03:00
|
|
|
length = *p++;
|
2010-08-09 22:34:11 +04:00
|
|
|
|
|
|
|
next = p + DIV_ROUNDUP(length, sizeof *p);
|
|
|
|
if (next > end) {
|
|
|
|
printf("message too short, "
|
|
|
|
"object (%d), message %s(%s)\n",
|
|
|
|
*p, message->name, message->signature);
|
|
|
|
errno = EINVAL;
|
2010-08-30 17:47:36 +04:00
|
|
|
goto err;
|
2010-08-09 22:34:11 +04:00
|
|
|
}
|
|
|
|
|
2010-09-08 01:00:34 +04:00
|
|
|
array = (struct wl_array **) extra;
|
|
|
|
extra += sizeof *array;
|
|
|
|
closure->args[i] = array;
|
|
|
|
|
|
|
|
*array = (struct wl_array *) extra;
|
|
|
|
extra += sizeof **array;
|
|
|
|
|
|
|
|
(*array)->size = length;
|
|
|
|
(*array)->alloc = 0;
|
|
|
|
(*array)->data = p;
|
2010-08-09 22:34:11 +04:00
|
|
|
p = next;
|
2009-02-24 06:30:29 +03:00
|
|
|
break;
|
2010-08-27 05:49:44 +04:00
|
|
|
case 'h':
|
2010-09-08 01:03:17 +04:00
|
|
|
closure->types[i] = &ffi_type_sint;
|
2010-09-08 01:00:34 +04:00
|
|
|
|
2010-09-08 01:03:17 +04:00
|
|
|
fd = (int *) extra;
|
|
|
|
extra += sizeof *fd;
|
|
|
|
closure->args[i] = fd;
|
2010-09-08 01:00:34 +04:00
|
|
|
|
2010-09-08 01:03:17 +04:00
|
|
|
wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
|
|
|
|
connection->fds_in.tail += sizeof *fd;
|
2010-08-27 05:49:44 +04:00
|
|
|
break;
|
2008-12-25 03:30:25 +03:00
|
|
|
default:
|
|
|
|
printf("unknown type\n");
|
2010-08-09 22:34:11 +04:00
|
|
|
assert(0);
|
2008-12-25 03:30:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
closure->count = i;
|
|
|
|
ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
|
|
|
|
closure->count, &ffi_type_uint32, closure->types);
|
2008-12-25 03:30:25 +03:00
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
wl_connection_consume(connection, size);
|
2010-08-27 05:49:44 +04:00
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
return closure;
|
|
|
|
|
|
|
|
err:
|
|
|
|
closure->count = i;
|
|
|
|
wl_closure_destroy(closure);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wl_closure_invoke(struct wl_closure *closure,
|
|
|
|
struct wl_object *target, void (*func)(void), void *data)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2010-09-08 01:00:34 +04:00
|
|
|
closure->args[0] = &data;
|
|
|
|
closure->args[1] = ⌖
|
2010-08-30 17:47:36 +04:00
|
|
|
|
|
|
|
ffi_call(&closure->cif, func, &result, closure->args);
|
|
|
|
}
|
|
|
|
|
2010-09-08 05:34:45 +04:00
|
|
|
void
|
|
|
|
wl_closure_send(struct wl_closure *closure, struct wl_connection *connection)
|
|
|
|
{
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
size = closure->start[1] >> 16;
|
|
|
|
wl_connection_write(connection, closure->start, size);
|
|
|
|
}
|
|
|
|
|
2010-09-07 18:58:19 +04:00
|
|
|
void
|
|
|
|
wl_closure_print(struct wl_closure *closure, struct wl_object *target)
|
|
|
|
{
|
2010-09-08 01:00:34 +04:00
|
|
|
union wl_value *value;
|
2010-09-07 18:58:19 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s(%d).%s(",
|
|
|
|
target->interface->name, target->id,
|
|
|
|
closure->message->name);
|
|
|
|
|
|
|
|
for (i = 2; i < closure->count; i++) {
|
|
|
|
if (i > 2)
|
|
|
|
fprintf(stderr, ", ");
|
2010-09-08 01:00:34 +04:00
|
|
|
|
|
|
|
value = closure->args[i];
|
2010-09-07 18:58:19 +04:00
|
|
|
switch (closure->message->signature[i - 2]) {
|
|
|
|
case 'u':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "%u", value->uint32);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
case 'i':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "%d", value->uint32);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
case 's':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "\"%s\"", value->string);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
case 'o':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "object %u",
|
|
|
|
value->object ? value->object->id : 0);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
case 'n':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "new id %u", value->uint32);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
fprintf(stderr, "array");
|
|
|
|
break;
|
|
|
|
case 'h':
|
2010-09-08 01:00:34 +04:00
|
|
|
fprintf(stderr, "fd %d", value->uint32);
|
2010-09-07 18:58:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, ")\n");
|
|
|
|
}
|
|
|
|
|
2010-08-30 17:47:36 +04:00
|
|
|
void
|
|
|
|
wl_closure_destroy(struct wl_closure *closure)
|
|
|
|
{
|
2008-12-25 03:30:25 +03:00
|
|
|
}
|