mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2026-04-06 07:15:47 -04:00
Use new connection object in client code too.
This commit is contained in:
parent
680f1c7025
commit
427524aedf
6 changed files with 104 additions and 123 deletions
2
Makefile
2
Makefile
|
|
@ -8,7 +8,7 @@ wayland_objs = wayland.o event-loop.o connection.o hash.o compositor.o
|
||||||
wayland : $(wayland_objs)
|
wayland : $(wayland_objs)
|
||||||
gcc -o $@ $(wayland_objs) $(LDLIBS)
|
gcc -o $@ $(wayland_objs) $(LDLIBS)
|
||||||
|
|
||||||
libwayland_objs = wayland-client.o
|
libwayland_objs = wayland-client.o connection.o
|
||||||
|
|
||||||
libwayland.so : $(libwayland_objs)
|
libwayland.so : $(libwayland_objs)
|
||||||
gcc -o $@ $(libwayland_objs) -shared
|
gcc -o $@ $(libwayland_objs) -shared
|
||||||
|
|
|
||||||
4
NOTES
4
NOTES
|
|
@ -60,6 +60,10 @@ synaptics, 3-button emulation, xkb, scim
|
||||||
|
|
||||||
changing screen resolution, adding monitors.
|
changing screen resolution, adding monitors.
|
||||||
|
|
||||||
|
What to do when protocol out buffer fills up? Just block on write
|
||||||
|
would work I guess. Clients are supposed to throttle using the bread
|
||||||
|
crumb events, so we shouldn't get into this situation.
|
||||||
|
|
||||||
|
|
||||||
RMI
|
RMI
|
||||||
|
|
||||||
|
|
|
||||||
53
client.c
53
client.c
|
|
@ -4,6 +4,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <i915_drm.h>
|
#include <i915_drm.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/poll.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
@ -92,15 +93,30 @@ draw_stuff(int width, int height)
|
||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
connection_update(struct wl_connection *connection,
|
||||||
|
uint32_t mask, void *data)
|
||||||
|
{
|
||||||
|
struct pollfd *p = data;
|
||||||
|
|
||||||
|
p->events = 0;
|
||||||
|
if (mask & WL_CONNECTION_READABLE)
|
||||||
|
p->events |= POLLIN;
|
||||||
|
if (mask & WL_CONNECTION_WRITABLE)
|
||||||
|
p->events |= POLLOUT;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct wl_connection *connection;
|
|
||||||
struct wl_display *display;
|
struct wl_display *display;
|
||||||
struct wl_surface *surface;
|
struct wl_surface *surface;
|
||||||
const int x = 400, y = 200, width = 300, height = 300;
|
const int x = 400, y = 200, width = 300, height = 300;
|
||||||
int fd;
|
int fd, i, ret;
|
||||||
uint32_t name;
|
uint32_t name, mask;
|
||||||
cairo_surface_t *s;
|
cairo_surface_t *s;
|
||||||
|
struct pollfd p[1];
|
||||||
|
|
||||||
fd = open(gem_device, O_RDWR);
|
fd = open(gem_device, O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
|
|
@ -108,13 +124,13 @@ int main(int argc, char *argv[])
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
connection = wl_connection_create(socket_name);
|
display = wl_display_create(socket_name,
|
||||||
if (connection == NULL) {
|
connection_update, &p[0]);
|
||||||
fprintf(stderr, "failed to create connection: %m\n");
|
if (display == NULL) {
|
||||||
|
fprintf(stderr, "failed to create display: %m\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
p[0].fd = wl_display_get_fd(display);
|
||||||
display = wl_connection_get_display(connection);
|
|
||||||
|
|
||||||
surface = wl_display_create_surface(display);
|
surface = wl_display_create_surface(display);
|
||||||
|
|
||||||
|
|
@ -124,13 +140,22 @@ int main(int argc, char *argv[])
|
||||||
wl_surface_attach(surface, name, width, height,
|
wl_surface_attach(surface, name, width, height,
|
||||||
cairo_image_surface_get_stride(s));
|
cairo_image_surface_get_stride(s));
|
||||||
|
|
||||||
wl_surface_map(surface, x, y, width, height);
|
i = 0;
|
||||||
if (wl_connection_flush(connection) < 0) {
|
while (ret = poll(p, 1, 40), ret >= 0) {
|
||||||
fprintf(stderr, "flush error: %m\n");
|
if (ret == 0) {
|
||||||
return -1;
|
wl_surface_map(surface, x + i, y + i, width, height);
|
||||||
}
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
wl_connection_iterate(connection);
|
mask = 0;
|
||||||
|
if (p[0].revents & POLLIN)
|
||||||
|
mask |= WL_CONNECTION_READABLE;
|
||||||
|
if (p[0].revents & POLLOUT)
|
||||||
|
mask |= WL_CONNECTION_WRITABLE;
|
||||||
|
if (mask)
|
||||||
|
wl_display_iterate(display, mask);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
0
socket.c
0
socket.c
148
wayland-client.c
148
wayland-client.c
|
|
@ -10,6 +10,9 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
|
|
||||||
|
#include "connection.h"
|
||||||
|
#include "wayland-client.h"
|
||||||
|
|
||||||
static const char socket_name[] = "\0wayland";
|
static const char socket_name[] = "\0wayland";
|
||||||
|
|
||||||
struct wl_buffer {
|
struct wl_buffer {
|
||||||
|
|
@ -17,45 +20,41 @@ struct wl_buffer {
|
||||||
int head, tail;
|
int head, tail;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wl_connection {
|
|
||||||
int fd;
|
|
||||||
struct wl_buffer in, out;
|
|
||||||
struct wl_display *display;
|
|
||||||
uint32_t id;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct wl_proxy {
|
struct wl_proxy {
|
||||||
struct wl_connection *connection;
|
struct wl_display *display;
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wl_display {
|
struct wl_display {
|
||||||
struct wl_proxy proxy;
|
struct wl_proxy proxy;
|
||||||
|
struct wl_connection *connection;
|
||||||
|
int fd;
|
||||||
|
uint32_t id;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wl_surface {
|
struct wl_surface {
|
||||||
struct wl_proxy proxy;
|
struct wl_proxy proxy;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wl_connection *
|
struct wl_display *
|
||||||
wl_connection_create(const char *address)
|
wl_display_create(const char *address,
|
||||||
|
wl_connection_update_func_t update, void *data)
|
||||||
{
|
{
|
||||||
struct wl_connection *connection;
|
|
||||||
struct wl_display *display;
|
struct wl_display *display;
|
||||||
struct sockaddr_un name;
|
struct sockaddr_un name;
|
||||||
socklen_t size;
|
socklen_t size;
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
uint32_t id, length;
|
uint32_t id, length;
|
||||||
|
|
||||||
connection = malloc(sizeof *connection);
|
display = malloc(sizeof *display);
|
||||||
if (connection == NULL)
|
if (display == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memset(connection, 0, sizeof *connection);
|
memset(display, 0, sizeof *display);
|
||||||
connection->id = 256; /* Need to get our id-range. */
|
display->id = 256; /* Need to get our id-range. */
|
||||||
connection->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
|
display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
|
||||||
if (connection->fd < 0) {
|
if (display->fd < 0) {
|
||||||
free(connection);
|
free(display);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,76 +63,71 @@ wl_connection_create(const char *address)
|
||||||
|
|
||||||
size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name;
|
size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name;
|
||||||
|
|
||||||
if (connect (connection->fd, (struct sockaddr *) &name, size) < 0) {
|
if (connect(display->fd, (struct sockaddr *) &name, size) < 0) {
|
||||||
close(connection->fd);
|
close(display->fd);
|
||||||
free(connection);
|
free(display);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME: actually discover advertised objects here. */
|
/* FIXME: actually discover advertised objects here. */
|
||||||
read(connection->fd, &id, sizeof id);
|
read(display->fd, &id, sizeof id);
|
||||||
read(connection->fd, &length, sizeof length);
|
read(display->fd, &length, sizeof length);
|
||||||
read(connection->fd, buffer, (length + 3) & ~3);
|
read(display->fd, buffer, (length + 3) & ~3);
|
||||||
|
|
||||||
display = malloc(sizeof *display);
|
display->proxy.display = display;
|
||||||
display->proxy.connection = connection;
|
|
||||||
display->proxy.id = id;
|
display->proxy.id = id;
|
||||||
connection->display = display;
|
|
||||||
|
|
||||||
return connection;
|
display->connection = wl_connection_create(display->fd,
|
||||||
|
update, data);
|
||||||
|
|
||||||
|
return display;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
wl_connection_destroy(struct wl_connection *connection)
|
wl_display_destroy(struct wl_display *display)
|
||||||
{
|
{
|
||||||
close(connection->fd);
|
wl_connection_destroy(display->connection);
|
||||||
free(connection->display);
|
close(display->fd);
|
||||||
free(connection);
|
free(display);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
wl_connection_get_fd(struct wl_connection *connection)
|
wl_display_get_fd(struct wl_display *display)
|
||||||
{
|
{
|
||||||
return connection->fd;
|
return display->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_event(struct wl_connection *connection)
|
handle_event(struct wl_connection *connection)
|
||||||
{
|
{
|
||||||
struct wl_buffer *b;
|
uint32_t p[2], opcode, size;
|
||||||
uint32_t *p, opcode, size;
|
|
||||||
|
|
||||||
b = &connection->in;
|
wl_connection_copy(connection, p, sizeof p);
|
||||||
p = (uint32_t *) (b->data + b->tail);
|
|
||||||
opcode = p[1] & 0xffff;
|
opcode = p[1] & 0xffff;
|
||||||
size = p[1] >> 16;
|
size = p[1] >> 16;
|
||||||
printf("signal from object %d, opcode %d, size %d\n",
|
printf("signal from object %d, opcode %d, size %d\n",
|
||||||
p[0], opcode, size);
|
p[0], opcode, size);
|
||||||
b->tail += size;
|
wl_connection_consume(connection, sizeof p);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
wl_connection_iterate(struct wl_connection *connection)
|
wl_display_iterate(struct wl_display *display, uint32_t mask)
|
||||||
{
|
{
|
||||||
struct wl_buffer *b;
|
uint32_t p[2], opcode, size;
|
||||||
uint32_t *p, opcode, size;
|
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
b = &connection->in;
|
len = wl_connection_data(display->connection, mask);
|
||||||
len = read(connection->fd, b->data + b->head, sizeof b->data);
|
|
||||||
b->head += len;
|
|
||||||
while (len > 0) {
|
while (len > 0) {
|
||||||
|
if (len < sizeof p)
|
||||||
if (b->head - b->tail < 8)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
p = (uint32_t *) (b->data + b->tail);
|
wl_connection_copy(display->connection, p, sizeof p);
|
||||||
opcode = p[1] & 0xffff;
|
opcode = p[1] & 0xffff;
|
||||||
size = p[1] >> 16;
|
size = p[1] >> 16;
|
||||||
if (b->head - b->tail < size)
|
if (len < size)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
handle_event(connection);
|
handle_event(display->connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
|
|
@ -142,52 +136,25 @@ wl_connection_iterate(struct wl_connection *connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
wl_connection_flush(struct wl_connection *connection)
|
|
||||||
{
|
|
||||||
struct wl_buffer *b;
|
|
||||||
int len;
|
|
||||||
|
|
||||||
b = &connection->out;
|
|
||||||
if (b->head == b->tail)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
len = write(connection->fd, b->data + b->tail, b->head - b->tail);
|
|
||||||
b->tail += len;
|
|
||||||
|
|
||||||
return len;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct wl_display *
|
|
||||||
wl_connection_get_display(struct wl_connection *connection)
|
|
||||||
{
|
|
||||||
return connection->display;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define WL_DISPLAY_CREATE_SURFACE 0
|
#define WL_DISPLAY_CREATE_SURFACE 0
|
||||||
|
|
||||||
struct wl_surface *
|
struct wl_surface *
|
||||||
wl_display_create_surface(struct wl_display *display)
|
wl_display_create_surface(struct wl_display *display)
|
||||||
{
|
{
|
||||||
struct wl_surface *surface;
|
struct wl_surface *surface;
|
||||||
struct wl_connection *connection;
|
|
||||||
uint32_t request[3];
|
uint32_t request[3];
|
||||||
|
|
||||||
surface = malloc(sizeof *surface);
|
surface = malloc(sizeof *surface);
|
||||||
if (surface == NULL)
|
if (surface == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
connection = display->proxy.connection;
|
surface->proxy.id = display->id++;
|
||||||
surface->proxy.id = connection->id++;
|
surface->proxy.display = display;
|
||||||
surface->proxy.connection = connection;
|
|
||||||
|
|
||||||
request[0] = display->proxy.id;
|
request[0] = display->proxy.id;
|
||||||
request[1] = WL_DISPLAY_CREATE_SURFACE | ((sizeof request) << 16);
|
request[1] = WL_DISPLAY_CREATE_SURFACE | ((sizeof request) << 16);
|
||||||
request[2] = surface->proxy.id;
|
request[2] = surface->proxy.id;
|
||||||
|
wl_connection_write(display->connection, request, sizeof request);
|
||||||
memcpy(connection->out.data + connection->out.head,
|
|
||||||
request, sizeof request);
|
|
||||||
connection->out.head += sizeof request;
|
|
||||||
|
|
||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
@ -199,22 +166,18 @@ wl_display_create_surface(struct wl_display *display)
|
||||||
void wl_surface_destroy(struct wl_surface *surface)
|
void wl_surface_destroy(struct wl_surface *surface)
|
||||||
{
|
{
|
||||||
uint32_t request[2];
|
uint32_t request[2];
|
||||||
struct wl_connection *connection;
|
|
||||||
|
|
||||||
request[0] = surface->proxy.id;
|
request[0] = surface->proxy.id;
|
||||||
request[1] = WL_SURFACE_DESTROY | ((sizeof request) << 16);
|
request[1] = WL_SURFACE_DESTROY | ((sizeof request) << 16);
|
||||||
|
|
||||||
connection = surface->proxy.connection;
|
wl_connection_write(surface->proxy.display->connection,
|
||||||
memcpy(connection->out.data + connection->out.head,
|
request, sizeof request);
|
||||||
request, sizeof request);
|
|
||||||
connection->out.head += sizeof request;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wl_surface_attach(struct wl_surface *surface,
|
void wl_surface_attach(struct wl_surface *surface,
|
||||||
uint32_t name, int width, int height, int stride)
|
uint32_t name, int width, int height, int stride)
|
||||||
{
|
{
|
||||||
uint32_t request[6];
|
uint32_t request[6];
|
||||||
struct wl_connection *connection;
|
|
||||||
|
|
||||||
request[0] = surface->proxy.id;
|
request[0] = surface->proxy.id;
|
||||||
request[1] = WL_SURFACE_ATTACH | ((sizeof request) << 16);
|
request[1] = WL_SURFACE_ATTACH | ((sizeof request) << 16);
|
||||||
|
|
@ -223,10 +186,8 @@ void wl_surface_attach(struct wl_surface *surface,
|
||||||
request[4] = height;
|
request[4] = height;
|
||||||
request[5] = stride;
|
request[5] = stride;
|
||||||
|
|
||||||
connection = surface->proxy.connection;
|
wl_connection_write(surface->proxy.display->connection,
|
||||||
memcpy(connection->out.data + connection->out.head,
|
request, sizeof request);
|
||||||
request, sizeof request);
|
|
||||||
connection->out.head += sizeof request;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -234,7 +195,6 @@ void wl_surface_map(struct wl_surface *surface,
|
||||||
int32_t x, int32_t y, int32_t width, int32_t height)
|
int32_t x, int32_t y, int32_t width, int32_t height)
|
||||||
{
|
{
|
||||||
uint32_t request[6];
|
uint32_t request[6];
|
||||||
struct wl_connection *connection;
|
|
||||||
|
|
||||||
request[0] = surface->proxy.id;
|
request[0] = surface->proxy.id;
|
||||||
request[1] = WL_SURFACE_MAP | ((sizeof request) << 16);
|
request[1] = WL_SURFACE_MAP | ((sizeof request) << 16);
|
||||||
|
|
@ -243,8 +203,6 @@ void wl_surface_map(struct wl_surface *surface,
|
||||||
request[4] = width;
|
request[4] = width;
|
||||||
request[5] = height;
|
request[5] = height;
|
||||||
|
|
||||||
connection = surface->proxy.connection;
|
wl_connection_write(surface->proxy.display->connection,
|
||||||
memcpy(connection->out.data + connection->out.head,
|
request, sizeof request);
|
||||||
request, sizeof request);
|
|
||||||
connection->out.head += sizeof request;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,17 @@
|
||||||
#ifndef _WAYLAND_CLIENT_H
|
#ifndef _WAYLAND_CLIENT_H
|
||||||
#define _WAYLAND_CLIENT_H
|
#define _WAYLAND_CLIENT_H
|
||||||
|
|
||||||
struct wl_connection;
|
#include "connection.h"
|
||||||
|
|
||||||
struct wl_display;
|
struct wl_display;
|
||||||
struct wl_surface;
|
struct wl_surface;
|
||||||
|
|
||||||
struct wl_connection *
|
struct wl_display *wl_display_create(const char *address,
|
||||||
wl_connection_create(const char *address);
|
wl_connection_update_func_t update, void *data);
|
||||||
void
|
void wl_display_destroy(struct wl_display *display);
|
||||||
wl_connection_destroy(struct wl_connection *connection);
|
int wl_display_get_fd(struct wl_display *display);
|
||||||
int
|
void wl_display_iterate(struct wl_display *display, uint32_t mask);
|
||||||
wl_connection_get_fd(struct wl_connection *connection);
|
|
||||||
void
|
|
||||||
wl_connection_iterate(struct wl_connection *connection);
|
|
||||||
int
|
|
||||||
wl_connection_flush(struct wl_connection *connection);
|
|
||||||
|
|
||||||
struct wl_display *
|
|
||||||
wl_connection_get_display(struct wl_connection *connection);
|
|
||||||
struct wl_surface *
|
struct wl_surface *
|
||||||
wl_display_create_surface(struct wl_display *display);
|
wl_display_create_surface(struct wl_display *display);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue