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-09-30 17:46:10 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2008-11-29 03:12:45 +03:00
|
|
|
#include <stdarg.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2008-11-06 18:38:17 +03:00
|
|
|
#include <dlfcn.h>
|
2008-11-29 03:12:45 +03:00
|
|
|
#include <assert.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <ffi.h>
|
2008-10-08 20:48:46 +04:00
|
|
|
|
2008-12-22 05:50:23 +03:00
|
|
|
#include "wayland-protocol.h"
|
2008-09-30 17:46:10 +04:00
|
|
|
#include "wayland.h"
|
2008-10-08 20:48:46 +04:00
|
|
|
#include "connection.h"
|
2008-09-30 17:46:10 +04:00
|
|
|
|
|
|
|
struct wl_client {
|
2008-10-08 20:48:46 +04:00
|
|
|
struct wl_connection *connection;
|
2008-09-30 17:46:10 +04:00
|
|
|
struct wl_event_source *source;
|
|
|
|
struct wl_display *display;
|
2008-10-12 05:37:55 +04:00
|
|
|
struct wl_list object_list;
|
2008-11-02 18:12:29 +03:00
|
|
|
struct wl_list link;
|
2008-09-30 17:46:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wl_display {
|
|
|
|
struct wl_object base;
|
|
|
|
struct wl_event_loop *loop;
|
2008-12-12 19:05:17 +03:00
|
|
|
struct wl_hash *objects;
|
2008-10-07 18:10:36 +04:00
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
struct wl_list pending_frame_list;
|
2008-10-12 02:40:23 +04:00
|
|
|
uint32_t client_id_range;
|
2008-11-24 07:41:08 +03:00
|
|
|
uint32_t id;
|
2008-11-02 18:12:29 +03:00
|
|
|
|
2008-11-24 03:10:23 +03:00
|
|
|
struct wl_list global_list;
|
2008-09-30 17:46:10 +04:00
|
|
|
};
|
|
|
|
|
2008-10-12 05:37:55 +04:00
|
|
|
struct wl_object_ref {
|
|
|
|
struct wl_object *object;
|
|
|
|
struct wl_list link;
|
|
|
|
};
|
2008-12-07 23:22:22 +03:00
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
void
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_client_destroy(struct wl_client *client);
|
2008-10-08 18:47:59 +04:00
|
|
|
|
2008-11-29 03:12:45 +03:00
|
|
|
static void
|
2008-12-10 21:16:50 +03:00
|
|
|
wl_client_vmarshal(struct wl_client *client, struct wl_object *sender,
|
|
|
|
uint32_t opcode, va_list ap)
|
2008-11-29 03:12:45 +03:00
|
|
|
{
|
2008-12-22 05:54:51 +03:00
|
|
|
const struct wl_message *event;
|
2008-11-29 03:12:45 +03:00
|
|
|
struct wl_object *object;
|
2008-12-22 04:25:16 +03:00
|
|
|
uint32_t args[32], length, *p, size;
|
|
|
|
const char *s;
|
2008-12-05 18:38:42 +03:00
|
|
|
int i, count;
|
2008-11-29 03:12:45 +03:00
|
|
|
|
|
|
|
event = &sender->interface->events[opcode];
|
2008-12-22 04:25:16 +03:00
|
|
|
count = strlen(event->signature);
|
2008-12-05 18:38:42 +03:00
|
|
|
assert(count <= ARRAY_LENGTH(args));
|
|
|
|
|
2008-12-22 04:25:16 +03:00
|
|
|
p = &args[2];
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
switch (event->signature[i]) {
|
2008-12-05 18:38:42 +03:00
|
|
|
case 'u':
|
|
|
|
case 'i':
|
2008-12-22 04:25:16 +03:00
|
|
|
*p++ = va_arg(ap, uint32_t);
|
2008-11-29 03:12:45 +03:00
|
|
|
break;
|
2008-12-05 18:38:42 +03:00
|
|
|
case 's':
|
2008-12-22 04:25:16 +03:00
|
|
|
s = va_arg(ap, const char *);
|
|
|
|
length = strlen(s);
|
|
|
|
*p++ = length;
|
|
|
|
memcpy(p, s, length);
|
|
|
|
p += DIV_ROUNDUP(length, sizeof(*p));
|
2008-11-29 03:12:45 +03:00
|
|
|
break;
|
2008-12-05 18:38:42 +03:00
|
|
|
case 'o':
|
2008-11-29 03:12:45 +03:00
|
|
|
object = va_arg(ap, struct wl_object *);
|
2008-12-22 04:25:16 +03:00
|
|
|
*p++ = object->id;
|
2008-11-29 03:12:45 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-22 04:25:16 +03:00
|
|
|
size = (p - args) * sizeof *p;
|
2008-11-29 03:12:45 +03:00
|
|
|
args[0] = sender->id;
|
|
|
|
args[1] = opcode | (size << 16);
|
|
|
|
wl_connection_write(client->connection, args, size);
|
|
|
|
}
|
|
|
|
|
2008-12-10 21:16:50 +03:00
|
|
|
static void
|
|
|
|
wl_client_marshal(struct wl_client *client, struct wl_object *sender,
|
|
|
|
uint32_t opcode, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, opcode);
|
|
|
|
wl_client_vmarshal(client, sender, opcode, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2008-10-08 18:47:59 +04:00
|
|
|
static void
|
|
|
|
wl_client_demarshal(struct wl_client *client, struct wl_object *target,
|
2008-12-16 04:35:24 +03:00
|
|
|
uint32_t opcode, size_t size)
|
2008-09-30 17:46:10 +04:00
|
|
|
{
|
2008-12-22 05:54:51 +03:00
|
|
|
const struct wl_message *method;
|
2008-11-07 16:39:37 +03:00
|
|
|
ffi_type *types[20];
|
2008-09-30 17:46:10 +04:00
|
|
|
ffi_cif cif;
|
|
|
|
uint32_t *p, result;
|
2008-12-19 01:55:33 +03:00
|
|
|
int i, count;
|
2008-09-30 17:46:10 +04:00
|
|
|
union {
|
|
|
|
uint32_t uint32;
|
|
|
|
const char *string;
|
|
|
|
void *object;
|
|
|
|
uint32_t new_id;
|
2008-11-07 16:39:37 +03:00
|
|
|
} values[20];
|
|
|
|
void *args[20];
|
2008-09-30 17:46:10 +04:00
|
|
|
struct wl_object *object;
|
2008-10-08 18:47:59 +04:00
|
|
|
uint32_t data[64];
|
2008-12-16 04:35:24 +03:00
|
|
|
void (*func)(void);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
method = &target->interface->methods[opcode];
|
2008-12-05 18:38:42 +03:00
|
|
|
count = strlen(method->signature) + 2;
|
|
|
|
if (count > ARRAY_LENGTH(types)) {
|
|
|
|
printf("too many args (%d)\n", count);
|
2008-09-30 17:46:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-08 18:47:59 +04:00
|
|
|
if (sizeof data < size) {
|
|
|
|
printf("request too big, should malloc tmp buffer here\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
types[0] = &ffi_type_pointer;
|
|
|
|
values[0].object = client;
|
|
|
|
args[0] = &values[0];
|
|
|
|
|
|
|
|
types[1] = &ffi_type_pointer;
|
|
|
|
values[1].object = target;
|
|
|
|
args[1] = &values[1];
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_copy(client->connection, data, size);
|
2008-10-08 18:47:59 +04:00
|
|
|
p = &data[2];
|
2008-12-05 18:38:42 +03:00
|
|
|
for (i = 2; i < count; i++) {
|
|
|
|
switch (method->signature[i - 2]) {
|
|
|
|
case 'u':
|
|
|
|
case 'i':
|
|
|
|
types[i] = &ffi_type_uint32;
|
|
|
|
values[i].uint32 = *p;
|
2008-09-30 17:46:10 +04:00
|
|
|
p++;
|
|
|
|
break;
|
2008-12-05 18:38:42 +03:00
|
|
|
case 's':
|
|
|
|
types[i] = &ffi_type_pointer;
|
2008-09-30 17:46:10 +04:00
|
|
|
/* FIXME */
|
2008-12-05 18:38:42 +03:00
|
|
|
values[i].uint32 = *p++;
|
2008-09-30 17:46:10 +04:00
|
|
|
break;
|
2008-12-05 18:38:42 +03:00
|
|
|
case 'o':
|
|
|
|
types[i] = &ffi_type_pointer;
|
2008-12-12 19:05:17 +03:00
|
|
|
object = wl_hash_lookup(client->display->objects, *p);
|
2008-09-30 17:46:10 +04:00
|
|
|
if (object == NULL)
|
|
|
|
printf("unknown object (%d)\n", *p);
|
2008-12-05 18:38:42 +03:00
|
|
|
values[i].object = object;
|
2008-09-30 17:46:10 +04:00
|
|
|
p++;
|
|
|
|
break;
|
2008-12-05 18:38:42 +03:00
|
|
|
case 'n':
|
|
|
|
types[i] = &ffi_type_uint32;
|
|
|
|
values[i].new_id = *p;
|
2008-12-12 19:05:17 +03:00
|
|
|
object = wl_hash_lookup(client->display->objects, *p);
|
2008-09-30 17:46:10 +04:00
|
|
|
if (object != NULL)
|
|
|
|
printf("object already exists (%d)\n", *p);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("unknown type\n");
|
|
|
|
break;
|
|
|
|
}
|
2008-12-05 18:38:42 +03:00
|
|
|
args[i] = &values[i];
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
func = target->implementation[opcode];
|
2008-12-05 18:38:42 +03:00
|
|
|
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, count, &ffi_type_uint32, types);
|
2008-12-16 04:35:24 +03:00
|
|
|
ffi_call(&cif, func, &result, args);
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
static void
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_client_connection_data(int fd, uint32_t mask, void *data)
|
2008-10-07 18:10:36 +04:00
|
|
|
{
|
2008-10-08 20:48:46 +04:00
|
|
|
struct wl_client *client = data;
|
|
|
|
struct wl_connection *connection = client->connection;
|
2008-10-07 18:10:36 +04:00
|
|
|
struct wl_object *object;
|
2008-10-08 20:48:46 +04:00
|
|
|
uint32_t p[2], opcode, size;
|
|
|
|
uint32_t cmask = 0;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_READABLE)
|
|
|
|
cmask |= WL_CONNECTION_READABLE;
|
|
|
|
if (mask & WL_EVENT_WRITEABLE)
|
|
|
|
cmask |= WL_CONNECTION_WRITABLE;
|
|
|
|
|
|
|
|
len = wl_connection_data(connection, cmask);
|
|
|
|
if (len < 0) {
|
|
|
|
wl_client_destroy(client);
|
|
|
|
return;
|
|
|
|
}
|
2008-10-08 18:47:59 +04:00
|
|
|
|
2008-11-24 07:41:08 +03:00
|
|
|
while (len >= sizeof p) {
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_copy(connection, p, sizeof p);
|
2008-10-07 18:10:36 +04:00
|
|
|
opcode = p[1] & 0xffff;
|
|
|
|
size = p[1] >> 16;
|
2008-10-08 20:48:46 +04:00
|
|
|
if (len < size)
|
2008-10-07 18:10:36 +04:00
|
|
|
break;
|
|
|
|
|
2008-12-12 19:05:17 +03:00
|
|
|
object = wl_hash_lookup(client->display->objects, p[0]);
|
2008-10-07 18:10:36 +04:00
|
|
|
if (object == NULL) {
|
2008-11-29 03:12:45 +03:00
|
|
|
wl_client_marshal(client, &client->display->base,
|
|
|
|
WL_DISPLAY_INVALID_OBJECT, p[0]);
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_consume(connection, size);
|
|
|
|
len -= size;
|
2008-10-07 18:10:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode >= object->interface->method_count) {
|
2008-11-29 03:12:45 +03:00
|
|
|
wl_client_marshal(client, &client->display->base,
|
|
|
|
WL_DISPLAY_INVALID_METHOD, p[0], opcode);
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_consume(connection, size);
|
|
|
|
len -= size;
|
2008-10-07 18:10:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_client_demarshal(client, object, opcode, size);
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_consume(connection, size);
|
|
|
|
len -= size;
|
2008-10-07 18:10:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
static int
|
|
|
|
wl_client_connection_update(struct wl_connection *connection,
|
|
|
|
uint32_t mask, void *data)
|
2008-09-30 17:46:10 +04:00
|
|
|
{
|
|
|
|
struct wl_client *client = data;
|
2008-10-08 20:48:46 +04:00
|
|
|
uint32_t emask = 0;
|
2008-10-08 18:47:59 +04:00
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
if (mask & WL_CONNECTION_READABLE)
|
|
|
|
emask |= WL_EVENT_READABLE;
|
|
|
|
if (mask & WL_CONNECTION_WRITABLE)
|
|
|
|
emask |= WL_EVENT_WRITEABLE;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-29 02:35:25 +03:00
|
|
|
return wl_event_source_fd_update(client->source, mask);
|
2008-10-08 20:48:46 +04:00
|
|
|
}
|
2008-10-08 18:47:59 +04:00
|
|
|
|
2008-11-08 23:39:41 +03:00
|
|
|
static struct wl_client *
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_client_create(struct wl_display *display, int fd)
|
|
|
|
{
|
|
|
|
struct wl_client *client;
|
2008-11-24 03:10:23 +03:00
|
|
|
struct wl_object_ref *ref;
|
2008-10-08 20:48:46 +04:00
|
|
|
|
|
|
|
client = malloc(sizeof *client);
|
|
|
|
if (client == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(client, 0, sizeof *client);
|
|
|
|
client->display = display;
|
|
|
|
client->source = wl_event_loop_add_fd(display->loop, fd,
|
|
|
|
WL_EVENT_READABLE,
|
|
|
|
wl_client_connection_data, client);
|
|
|
|
client->connection = wl_connection_create(fd,
|
|
|
|
wl_client_connection_update,
|
|
|
|
client);
|
2008-10-12 05:37:55 +04:00
|
|
|
wl_list_init(&client->object_list);
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_list_init(&client->link);
|
2008-10-08 20:48:46 +04:00
|
|
|
|
2008-10-12 02:40:23 +04:00
|
|
|
wl_connection_write(client->connection,
|
|
|
|
&display->client_id_range,
|
|
|
|
sizeof display->client_id_range);
|
|
|
|
display->client_id_range += 256;
|
|
|
|
|
2008-11-24 03:10:23 +03:00
|
|
|
ref = container_of(display->global_list.next,
|
|
|
|
struct wl_object_ref, link);
|
|
|
|
while (&ref->link != &display->global_list) {
|
2008-12-22 04:25:16 +03:00
|
|
|
wl_client_marshal(client, &client->display->base,
|
|
|
|
WL_DISPLAY_GLOBAL,
|
|
|
|
ref->object,
|
|
|
|
ref->object->interface->name,
|
|
|
|
ref->object->interface->version);
|
2008-11-24 03:10:23 +03:00
|
|
|
|
|
|
|
ref = container_of(ref->link.next,
|
|
|
|
struct wl_object_ref, link);
|
|
|
|
}
|
2008-10-08 20:48:46 +04:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
static void
|
|
|
|
wl_object_destroy(struct wl_object *object)
|
|
|
|
{
|
|
|
|
const struct wl_surface_interface *interface =
|
|
|
|
(const struct wl_surface_interface *) object->implementation;
|
|
|
|
|
|
|
|
/* FIXME: Need generic object destructor. */
|
|
|
|
interface->destroy(NULL, (struct wl_surface *) object);
|
|
|
|
}
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
void
|
|
|
|
wl_client_destroy(struct wl_client *client)
|
|
|
|
{
|
2008-10-12 05:37:55 +04:00
|
|
|
struct wl_object_ref *ref;
|
|
|
|
|
2008-10-08 20:48:46 +04:00
|
|
|
printf("disconnect from client %p\n", client);
|
2008-10-12 05:37:55 +04:00
|
|
|
|
2008-11-02 18:12:29 +03:00
|
|
|
wl_list_remove(&client->link);
|
|
|
|
|
2008-10-12 05:37:55 +04:00
|
|
|
while (client->object_list.next != &client->object_list) {
|
|
|
|
ref = container_of(client->object_list.next,
|
|
|
|
struct wl_object_ref, link);
|
|
|
|
wl_list_remove(&ref->link);
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_object_destroy(ref->object);
|
2008-10-12 05:37:55 +04:00
|
|
|
free(ref);
|
|
|
|
}
|
|
|
|
|
2008-11-29 02:35:25 +03:00
|
|
|
wl_event_source_remove(client->source);
|
2008-10-08 20:48:46 +04:00
|
|
|
wl_connection_destroy(client->connection);
|
|
|
|
free(client);
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
WL_EXPORT int
|
|
|
|
wl_client_add_surface(struct wl_client *client,
|
|
|
|
struct wl_surface *surface,
|
|
|
|
const struct wl_surface_interface *implementation,
|
|
|
|
uint32_t id)
|
2008-09-30 17:46:10 +04:00
|
|
|
{
|
2008-12-16 04:35:24 +03:00
|
|
|
struct wl_display *display = client->display;
|
2008-10-12 05:37:55 +04:00
|
|
|
struct wl_object_ref *ref;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
surface->base.id = id;
|
2008-12-22 05:50:23 +03:00
|
|
|
surface->base.interface = &wl_surface_interface;
|
2008-12-16 04:35:24 +03:00
|
|
|
surface->base.implementation = (void (**)(void)) implementation;
|
|
|
|
surface->client = client;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-12 05:37:55 +04:00
|
|
|
ref = malloc(sizeof *ref);
|
|
|
|
if (ref == NULL) {
|
2008-11-29 03:12:45 +03:00
|
|
|
wl_client_marshal(client, &display->base,
|
|
|
|
WL_DISPLAY_NO_MEMORY);
|
2008-10-12 05:37:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->object = &surface->base;
|
2008-12-12 19:05:17 +03:00
|
|
|
wl_hash_insert(display->objects, &surface->base);
|
2008-10-12 05:37:55 +04:00
|
|
|
wl_list_insert(client->object_list.prev, &ref->link);
|
2008-10-07 18:10:36 +04:00
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
WL_EXPORT void
|
|
|
|
wl_client_send_acknowledge(struct wl_client *client,
|
|
|
|
struct wl_compositor *compositor,
|
|
|
|
uint32_t key, uint32_t frame)
|
2008-11-26 06:40:39 +03:00
|
|
|
{
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_list_remove(&client->link);
|
|
|
|
wl_list_insert(client->display->pending_frame_list.prev,
|
|
|
|
&client->link);
|
|
|
|
wl_client_marshal(client, &compositor->base,
|
|
|
|
WL_COMPOSITOR_ACKNOWLEDGE, key, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_EXPORT int
|
|
|
|
wl_display_set_compositor(struct wl_display *display,
|
|
|
|
struct wl_compositor *compositor,
|
|
|
|
const struct wl_compositor_interface *implementation)
|
|
|
|
{
|
2008-12-22 05:50:23 +03:00
|
|
|
compositor->base.interface = &wl_compositor_interface;
|
2008-12-16 04:35:24 +03:00
|
|
|
compositor->base.implementation = (void (**)(void)) implementation;
|
2008-11-29 01:06:06 +03:00
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_display_add_object(display, &compositor->base);
|
|
|
|
if (wl_display_add_global(display, &compositor->base))
|
|
|
|
return -1;
|
2008-11-26 06:40:39 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:13:50 +03:00
|
|
|
WL_EXPORT struct wl_display *
|
2008-09-30 17:46:10 +04:00
|
|
|
wl_display_create(void)
|
|
|
|
{
|
|
|
|
struct wl_display *display;
|
|
|
|
|
|
|
|
display = malloc(sizeof *display);
|
|
|
|
if (display == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
display->loop = wl_event_loop_create();
|
|
|
|
if (display->loop == NULL) {
|
|
|
|
free(display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-12 19:05:17 +03:00
|
|
|
display->objects = wl_hash_create();
|
|
|
|
if (display->objects == NULL) {
|
|
|
|
free(display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_list_init(&display->pending_frame_list);
|
2008-11-24 03:10:23 +03:00
|
|
|
wl_list_init(&display->global_list);
|
2008-11-02 18:12:29 +03:00
|
|
|
|
2008-10-12 02:40:23 +04:00
|
|
|
display->client_id_range = 256; /* Gah, arbitrary... */
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-24 07:41:08 +03:00
|
|
|
display->id = 1;
|
2008-12-22 05:50:23 +03:00
|
|
|
display->base.interface = &wl_display_interface;
|
2008-12-16 04:35:24 +03:00
|
|
|
display->base.implementation = NULL;
|
2008-11-24 07:41:08 +03:00
|
|
|
wl_display_add_object(display, &display->base);
|
2008-11-24 03:10:23 +03:00
|
|
|
if (wl_display_add_global(display, &display->base)) {
|
|
|
|
wl_event_loop_destroy(display->loop);
|
|
|
|
free(display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
return display;
|
|
|
|
}
|
|
|
|
|
2008-11-24 07:41:08 +03:00
|
|
|
WL_EXPORT void
|
|
|
|
wl_display_add_object(struct wl_display *display, struct wl_object *object)
|
|
|
|
{
|
|
|
|
object->id = display->id++;
|
2008-12-12 19:05:17 +03:00
|
|
|
wl_hash_insert(display->objects, object);
|
2008-11-24 07:41:08 +03:00
|
|
|
}
|
|
|
|
|
2008-11-24 03:10:23 +03:00
|
|
|
WL_EXPORT int
|
|
|
|
wl_display_add_global(struct wl_display *display, struct wl_object *object)
|
|
|
|
{
|
|
|
|
struct wl_object_ref *ref;
|
|
|
|
|
|
|
|
ref = malloc(sizeof *ref);
|
|
|
|
if (ref == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ref->object = object;
|
|
|
|
wl_list_insert(display->global_list.prev, &ref->link);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-10 18:42:04 +03:00
|
|
|
WL_EXPORT void
|
2008-12-10 21:16:50 +03:00
|
|
|
wl_surface_post_event(struct wl_surface *surface,
|
|
|
|
struct wl_object *sender,
|
|
|
|
uint32_t event, ...)
|
2008-12-10 18:42:04 +03:00
|
|
|
{
|
2008-12-10 21:16:50 +03:00
|
|
|
va_list ap;
|
2008-12-10 18:42:04 +03:00
|
|
|
|
2008-12-10 21:16:50 +03:00
|
|
|
va_start(ap, event);
|
|
|
|
wl_client_vmarshal(surface->client, sender, event, ap);
|
|
|
|
va_end(ap);
|
2008-12-10 18:42:04 +03:00
|
|
|
}
|
|
|
|
|
2008-11-26 20:57:31 +03:00
|
|
|
WL_EXPORT void
|
2008-11-29 01:06:06 +03:00
|
|
|
wl_display_post_frame(struct wl_display *display,
|
2008-12-16 04:35:24 +03:00
|
|
|
struct wl_compositor *compositor,
|
2008-11-29 01:06:06 +03:00
|
|
|
uint32_t frame, uint32_t msecs)
|
2008-11-26 20:57:31 +03:00
|
|
|
{
|
|
|
|
struct wl_client *client;
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
client = container_of(display->pending_frame_list.next,
|
2008-11-26 20:57:31 +03:00
|
|
|
struct wl_client, link);
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
while (&client->link != &display->pending_frame_list) {
|
|
|
|
wl_client_marshal(client, &compositor->base,
|
|
|
|
WL_COMPOSITOR_FRAME, frame, msecs);
|
2008-11-26 20:57:31 +03:00
|
|
|
client = container_of(client->link.next,
|
|
|
|
struct wl_client, link);
|
|
|
|
}
|
|
|
|
|
2008-12-16 04:35:24 +03:00
|
|
|
wl_list_remove(&display->pending_frame_list);
|
|
|
|
wl_list_init(&display->pending_frame_list);
|
2008-10-07 18:10:36 +04:00
|
|
|
}
|
|
|
|
|
2008-11-08 23:39:41 +03:00
|
|
|
WL_EXPORT struct wl_event_loop *
|
2008-10-12 03:21:35 +04:00
|
|
|
wl_display_get_event_loop(struct wl_display *display)
|
|
|
|
{
|
|
|
|
return display->loop;
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:13:50 +03:00
|
|
|
WL_EXPORT void
|
2008-09-30 17:46:10 +04:00
|
|
|
wl_display_run(struct wl_display *display)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
wl_event_loop_wait(display->loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
socket_data(int fd, uint32_t mask, void *data)
|
|
|
|
{
|
|
|
|
struct wl_display *display = data;
|
|
|
|
struct sockaddr_un name;
|
|
|
|
socklen_t length;
|
|
|
|
int client_fd;
|
|
|
|
|
|
|
|
length = sizeof name;
|
|
|
|
client_fd = accept (fd, (struct sockaddr *) &name, &length);
|
|
|
|
if (client_fd < 0)
|
|
|
|
fprintf(stderr, "failed to accept\n");
|
|
|
|
|
|
|
|
wl_client_create(display, client_fd);
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:13:50 +03:00
|
|
|
WL_EXPORT int
|
2008-12-07 23:22:22 +03:00
|
|
|
wl_display_add_socket(struct wl_display *display,
|
|
|
|
const char *name, size_t name_size)
|
2008-09-30 17:46:10 +04:00
|
|
|
{
|
2008-12-07 23:22:22 +03:00
|
|
|
struct sockaddr_un addr;
|
2008-09-30 17:46:10 +04:00
|
|
|
int sock;
|
|
|
|
socklen_t size;
|
|
|
|
|
|
|
|
sock = socket(PF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-07 23:22:22 +03:00
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
memcpy(addr.sun_path, name, name_size);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-12-07 23:22:22 +03:00
|
|
|
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
|
|
|
if (bind(sock, (struct sockaddr *) &addr, size) < 0)
|
2008-09-30 17:46:10 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (listen(sock, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
wl_event_loop_add_fd(display->loop, sock,
|
|
|
|
WL_EVENT_READABLE,
|
2008-09-30 17:46:10 +04:00
|
|
|
socket_data, display);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|