Add listener interfaces for output and input_device objects.

dev
Kristian Høgsberg 16 years ago
parent e787bc60e5
commit 94448c0ad7
  1. 37
      flower.c
  2. 22
      gears.c
  3. 24
      terminal.c
  4. 368
      wayland-client.c
  5. 83
      wayland-client.h
  6. 297
      window.c

@ -111,25 +111,31 @@ struct flower {
}; };
static void static void
move_flower(struct flower *flower) handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{ {
wl_surface_map(flower->surface,
flower->x + cos(flower->i / 31.0) * 400 - flower->width / 2,
flower->y + sin(flower->i / 27.0) * 300 - flower->height / 2,
flower->width, flower->height);
flower->i++;
wl_compositor_commit(flower->compositor, 0);
} }
static void static void
event_handler(struct wl_display *display, handle_frame(void *data,
uint32_t object, uint32_t opcode, struct wl_compositor *compositor,
uint32_t size, uint32_t *p, void *data) uint32_t frame, uint32_t timestamp)
{ {
if (object == 2 && opcode == 1) struct flower *flower = data;
move_flower(data);
wl_surface_map(flower->surface,
flower->x + cos(timestamp / 400.0) * 400 - flower->width / 2,
flower->y + sin(timestamp / 320.0) * 300 - flower->height / 2,
flower->width, flower->height);
wl_compositor_commit(flower->compositor, 0);
} }
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct wl_display *display; struct wl_display *display;
@ -177,8 +183,11 @@ int main(int argc, char *argv[])
wl_surface_attach(flower.surface, wl_surface_attach(flower.surface,
buffer->name, flower.width, flower.height, buffer->name, flower.width, flower.height,
buffer->stride, visual); buffer->stride, visual);
wl_display_set_event_handler(display, event_handler, &flower);
move_flower(&flower); wl_compositor_add_listener(flower.compositor,
&compositor_listener, &flower);
wl_compositor_commit(flower.compositor, 0);
g_main_loop_run(loop); g_main_loop_run(loop);

@ -299,7 +299,9 @@ resize_handler(struct window *window, void *data)
} }
static void static void
acknowledge_handler(struct window *window, uint32_t key, void *data) handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{ {
struct gears *gears = data; struct gears *gears = data;
@ -313,7 +315,9 @@ acknowledge_handler(struct window *window, uint32_t key, void *data)
} }
static void static void
frame_handler(struct window *window, uint32_t frame, uint32_t timestamp, void *data) handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{ {
struct gears *gears = data; struct gears *gears = data;
@ -323,9 +327,14 @@ frame_handler(struct window *window, uint32_t frame, uint32_t timestamp, void *d
wl_compositor_commit(gears->compositor, 0); wl_compositor_commit(gears->compositor, 0);
gears->angle += 1; gears->angle = timestamp / 20.0;
} }
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
static struct gears * static struct gears *
gears_create(struct wl_display *display, int fd) gears_create(struct wl_display *display, int fd)
{ {
@ -384,11 +393,12 @@ gears_create(struct wl_display *display, int fd)
draw_gears(gears); draw_gears(gears);
frame_handler(gears->window, 0, 0, gears); handle_frame(gears, gears->compositor, 0, 0);
window_set_resize_handler(gears->window, resize_handler, gears); window_set_resize_handler(gears->window, resize_handler, gears);
window_set_frame_handler(gears->window, frame_handler, gears);
window_set_acknowledge_handler(gears->window, acknowledge_handler, gears); wl_compositor_add_listener(gears->compositor,
&compositor_listener, gears);
return gears; return gears;
} }

@ -397,12 +397,15 @@ resize_handler(struct window *window, void *data)
} }
static void static void
acknowledge_handler(struct window *window, uint32_t key, void *data) handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{ {
struct terminal *terminal = data; struct terminal *terminal = data;
terminal->redraw_scheduled = 0; terminal->redraw_scheduled = 0;
buffer_destroy(terminal->buffer, terminal->fd); if (key == 0)
buffer_destroy(terminal->buffer, terminal->fd);
if (terminal->redraw_pending) { if (terminal->redraw_pending) {
terminal->redraw_pending = 0; terminal->redraw_pending = 0;
@ -410,6 +413,18 @@ acknowledge_handler(struct window *window, uint32_t key, void *data)
} }
} }
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
struct key { struct key {
int code[4]; int code[4];
} evdev_keymap[] = { } evdev_keymap[] = {
@ -549,7 +564,10 @@ terminal_create(struct wl_display *display, int fd, int fullscreen)
terminal->compositor = wl_display_get_compositor(display); terminal->compositor = wl_display_get_compositor(display);
window_set_fullscreen(terminal->window, terminal->fullscreen); window_set_fullscreen(terminal->window, terminal->fullscreen);
window_set_resize_handler(terminal->window, resize_handler, terminal); window_set_resize_handler(terminal->window, resize_handler, terminal);
window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
wl_compositor_add_listener(terminal->compositor,
&compositor_listener, terminal);
window_set_key_handler(terminal->window, key_handler, terminal); window_set_key_handler(terminal->window, key_handler, terminal);
terminal_draw(terminal); terminal_draw(terminal);

@ -44,31 +44,26 @@ struct wl_global {
uint32_t id; uint32_t id;
char *interface; char *interface;
uint32_t version; uint32_t version;
struct wl_proxy *proxy;
struct wl_list link; struct wl_list link;
}; };
struct wl_proxy { struct wl_global_listener {
struct wl_object base; wl_display_global_func_t handler;
struct wl_display *display; void *data;
struct wl_list link;
}; };
struct wl_display { struct wl_listener {
struct wl_proxy proxy; void (**implementation)(void);
struct wl_connection *connection; void *data;
int fd; struct wl_list link;
uint32_t id, id_count, next_range; };
uint32_t mask;
struct wl_hash *objects;
struct wl_list global_list;
struct wl_list visual_list;
wl_display_update_func_t update;
void *update_data;
wl_display_event_func_t event_handler;
void *event_handler_data;
struct wl_output *output; struct wl_proxy {
struct wl_object base;
struct wl_display *display;
struct wl_list listener_list;
}; };
struct wl_compositor { struct wl_compositor {
@ -81,14 +76,42 @@ struct wl_surface {
struct wl_visual { struct wl_visual {
struct wl_proxy proxy; struct wl_proxy proxy;
struct wl_list link;
}; };
struct wl_output { struct wl_output {
struct wl_proxy proxy; struct wl_proxy proxy;
struct wl_listener listener;
int32_t width, height; int32_t width, height;
}; };
struct wl_input_device {
struct wl_proxy proxy;
};
struct wl_display {
struct wl_proxy proxy;
struct wl_connection *connection;
int fd;
uint32_t id, id_count, next_range;
uint32_t mask;
struct wl_hash *objects;
struct wl_list global_list;
struct wl_listener listener;
struct wl_list global_listener_list;
struct wl_visual *argb_visual;
struct wl_visual *premultiplied_argb_visual;
struct wl_visual *rgb_visual;
wl_display_update_func_t update;
void *update_data;
wl_display_global_func_t global_handler;
void *global_handler_data;
struct wl_compositor *compositor;
};
static int static int
connection_update(struct wl_connection *connection, connection_update(struct wl_connection *connection,
uint32_t mask, void *data) uint32_t mask, void *data)
@ -103,46 +126,117 @@ connection_update(struct wl_connection *connection,
return 0; return 0;
} }
static void WL_EXPORT int
output_handle_geometry(struct wl_display *display, wl_object_implements(struct wl_object *object,
struct wl_output *output, int32_t width, int32_t height) const char *interface, int version)
{ {
output->width = width; return strcmp(object->interface->name, interface) == 0 &&
output->height = height; object->interface->version >= version;
} }
struct wl_output_listener { WL_EXPORT struct wl_global_listener *
void (*geometry)(struct wl_display *display, wl_display_add_global_listener(struct wl_display *display,
struct wl_output *output, wl_display_global_func_t handler, void *data)
int32_t width, int32_t height); {
}; struct wl_global_listener *listener;
struct wl_global *global;
static const struct wl_output_listener output_listener = { listener = malloc(sizeof *listener);
output_handle_geometry if (listener == NULL)
}; return NULL;
static void listener->handler = handler;
add_output(struct wl_display *display, struct wl_global *global) listener->data = data;
{ wl_list_insert(display->global_listener_list.prev, &listener->link);
struct wl_output *output;
output = malloc(sizeof *output); /* FIXME: Need a destructor for void *data? */
if (output == NULL)
return;
output->proxy.base.interface = &wl_output_interface; global = container_of(display->global_list.next,
output->proxy.base.implementation = (void(**)(void)) &output_listener; struct wl_global, link);
output->proxy.base.id = global->id; while (&global->link != &display->global_list) {
output->proxy.display = display; if (global->proxy != NULL)
display->output = output; (*handler)(display, &global->proxy->base, data);
wl_hash_insert(display->objects, &output->proxy.base);
global = container_of(global->link.next,
struct wl_global, link);
}
return listener;
} }
WL_EXPORT void WL_EXPORT void
wl_display_get_geometry(struct wl_display *display, int32_t *width, int32_t *height) wl_display_remove_global_listener(struct wl_display *display,
struct wl_global_listener *listener)
{
wl_list_remove(&listener->link);
free(listener);
}
static struct wl_proxy *
wl_proxy_create_for_global(struct wl_display *display,
struct wl_global *global,
const struct wl_interface *interface)
{
struct wl_proxy *proxy;
struct wl_global_listener *listener;
proxy = malloc(sizeof *proxy);
if (proxy == NULL)
return NULL;
proxy->base.interface = interface;
proxy->base.id = global->id;
proxy->display = display;
global->proxy = proxy;
wl_list_init(&proxy->listener_list);
wl_hash_insert(display->objects, &proxy->base);
listener = container_of(display->global_listener_list.next,
struct wl_global_listener, link);
while (&listener->link != &display->global_listener_list) {
(*listener->handler)(display, &proxy->base, listener->data);
listener = container_of(listener->link.next,
struct wl_global_listener, link);
}
return proxy;
}
static int
wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
{ {
*width = display->output->width; struct wl_listener *listener;
*height = display->output->height;
listener = malloc(sizeof *listener);
if (listener == NULL)
return -1;
listener->implementation = (void (**)(void)) implementation;
listener->data = data;
wl_list_insert(proxy->listener_list.prev, &listener->link);
return 0;
}
static void
wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
{
va_list ap;
va_start(ap, opcode);
wl_connection_vmarshal(proxy->display->connection,
&proxy->base, opcode, ap,
&proxy->base.interface->methods[opcode]);
va_end(ap);
}
WL_EXPORT int
wl_output_add_listener(struct wl_output *output,
const struct wl_output_listener *listener,
void *data)
{
return wl_proxy_add_listener(&output->proxy,
(void (**)(void)) listener, data);
} }
static void static void
@ -150,66 +244,69 @@ add_visual(struct wl_display *display, struct wl_global *global)
{ {
struct wl_visual *visual; struct wl_visual *visual;
visual = malloc(sizeof *visual); visual = (struct wl_visual *)
if (visual == NULL) wl_proxy_create_for_global(display, global,
return; &wl_visual_interface);
if (display->argb_visual == NULL)
visual->proxy.base.interface = &wl_visual_interface; display->argb_visual = visual;
visual->proxy.base.id = global->id; else if (display->premultiplied_argb_visual == NULL)
visual->proxy.base.implementation = NULL; display->premultiplied_argb_visual = visual;
visual->proxy.display = display; else
wl_list_insert(display->visual_list.prev, &visual->link); display->rgb_visual = visual;
wl_hash_insert(display->objects, &visual->proxy.base);
} }
WL_EXPORT struct wl_visual * WL_EXPORT struct wl_visual *
wl_display_get_argb_visual(struct wl_display *display) wl_display_get_argb_visual(struct wl_display *display)
{ {
return container_of(display->visual_list.next, return display->argb_visual;
struct wl_visual, link);
} }
WL_EXPORT struct wl_visual * WL_EXPORT struct wl_visual *
wl_display_get_premultiplied_argb_visual(struct wl_display *display) wl_display_get_premultiplied_argb_visual(struct wl_display *display)
{ {
return container_of(display->visual_list.next->next, return display->premultiplied_argb_visual;
struct wl_visual, link);
} }
WL_EXPORT struct wl_visual * WL_EXPORT struct wl_visual *
wl_display_get_rgb_visual(struct wl_display *display) wl_display_get_rgb_visual(struct wl_display *display)
{ {
/* FIXME: Where's cddar when you need it... */ return display->rgb_visual;
}
return container_of(display->visual_list.next->next->next, WL_EXPORT int
struct wl_visual, link); wl_input_device_add_listener(struct wl_input_device *input_device,
const struct wl_input_device_listener *listener,
void *data)
{
return wl_proxy_add_listener(&input_device->proxy,
(void (**)(void)) listener, data);
} }
static void static void
display_handle_invalid_object(struct wl_display *display, display_handle_invalid_object(void *data,
struct wl_object *object, uint32_t id) struct wl_display *display, uint32_t id)
{ {
fprintf(stderr, "sent request to invalid object\n"); fprintf(stderr, "sent request to invalid object\n");
} }
static void static void
display_handle_invalid_method(struct wl_display *display, display_handle_invalid_method(void *data,
struct wl_object *object, struct wl_display *display,
uint32_t id, uint32_t opcode) uint32_t id, uint32_t opcode)
{ {
fprintf(stderr, "sent invalid request opcode\n"); fprintf(stderr, "sent invalid request opcode\n");
} }
static void static void
display_handle_no_memory(struct wl_display *display, display_handle_no_memory(void *data,
struct wl_object *object) struct wl_display *display)
{ {
fprintf(stderr, "server out of memory\n"); fprintf(stderr, "server out of memory\n");
} }
static void static void
display_handle_global(struct wl_display *display, display_handle_global(void *data,
struct wl_object *object, struct wl_display *display,
uint32_t id, const char *interface, uint32_t version) uint32_t id, const char *interface, uint32_t version)
{ {
struct wl_global *global; struct wl_global *global;
@ -222,32 +319,41 @@ display_handle_global(struct wl_display *display,
global->interface = strdup(interface); global->interface = strdup(interface);
global->version = version; global->version = version;
wl_list_insert(display->global_list.prev, &global->link); wl_list_insert(display->global_list.prev, &global->link);
if (strcmp(global->interface, "display") == 0) if (strcmp(global->interface, "display") == 0)
wl_hash_insert(display->objects, &display->proxy.base); wl_hash_insert(display->objects, &display->proxy.base);
if (strcmp(global->interface, "visual") == 0) else if (strcmp(global->interface, "compositor") == 0)
display->compositor = (struct wl_compositor *)
wl_proxy_create_for_global(display, global,
&wl_compositor_interface);
else if (strcmp(global->interface, "visual") == 0)
add_visual(display, global); add_visual(display, global);
else if (strcmp(global->interface, "output") == 0) else if (strcmp(global->interface, "output") == 0)
add_output(display, global); wl_proxy_create_for_global(display, global,
&wl_output_interface);
else if (strcmp(global->interface, "input_device") == 0)
wl_proxy_create_for_global(display, global,
&wl_input_device_interface);
} }
static void static void
display_handle_range(struct wl_display *display, display_handle_range(void *data,
struct wl_object *object, uint32_t range) struct wl_display *display, uint32_t range)
{ {
display->next_range = range; display->next_range = range;
} }
struct wl_display_listener { struct wl_display_listener {
void (*invalid_object)(struct wl_display *display, void (*invalid_object)(void *data,
struct wl_object *object, uint32_t id); struct wl_display *display, uint32_t id);
void (*invalid_method)(struct wl_display *display, void (*invalid_method)(void *data, struct wl_display *display,
struct wl_object *object,
uint32_t id, uint32_t opcode); uint32_t id, uint32_t opcode);
void (*no_memory)(struct wl_display *display, struct wl_object *object); void (*no_memory)(void *data,
void (*global)(struct wl_display *display, struct wl_object *object, struct wl_display *display);
void (*global)(void *data, struct wl_display *display,
uint32_t id, const char *interface, uint32_t version); uint32_t id, const char *interface, uint32_t version);
void (*range)(struct wl_display *display, void (*range)(void *data,
struct wl_object *object, uint32_t range); struct wl_display *display, uint32_t range);
}; };
static const struct wl_display_listener display_listener = { static const struct wl_display_listener display_listener = {
@ -289,12 +395,15 @@ wl_display_create(const char *name, size_t name_size)
display->objects = wl_hash_create(); display->objects = wl_hash_create();
wl_list_init(&display->global_list); wl_list_init(&display->global_list);
wl_list_init(&display->visual_list); wl_list_init(&display->global_listener_list);
display->proxy.base.interface = &wl_display_interface; display->proxy.base.interface = &wl_display_interface;
display->proxy.base.implementation = (void(**)(void)) &display_listener;
display->proxy.base.id = 1; display->proxy.base.id = 1;
display->proxy.display = display; display->proxy.display = display;
wl_list_init(&display->proxy.listener_list);
display->listener.implementation = (void(**)(void)) &display_listener;
wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
display->connection = wl_connection_create(display->fd, display->connection = wl_connection_create(display->fd,
connection_update, connection_update,
@ -351,26 +460,37 @@ handle_event(struct wl_display *display,
uint32_t id, uint32_t opcode, uint32_t size) uint32_t id, uint32_t opcode, uint32_t size)
{ {
uint32_t p[32]; uint32_t p[32];
struct wl_object *object; struct wl_listener *listener;
struct wl_proxy *proxy;
wl_connection_copy(display->connection, p, size); wl_connection_copy(display->connection, p, size);
if (id == 1) if (id == 1)
object = &display->proxy.base; proxy = &display->proxy;
else else
object = wl_hash_lookup(display->objects, id); proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
if (object != NULL) if (proxy != NULL) {
wl_connection_demarshal(display->connection, if (wl_list_empty(&proxy->listener_list)) {
size, printf("proxy found for object %d, opcode %d, but no listeners\n",
display->objects, id, opcode);
object->implementation[opcode], }
display,
object, listener = container_of(proxy->listener_list.next,
&object->interface->events[opcode]); struct wl_listener, link);
else if (display->event_handler != NULL) while (&listener->link != &proxy->listener_list) {
display->event_handler(display, id,
opcode, size, p + 2, wl_connection_demarshal(display->connection,
display->event_handler_data); size,
display->objects,
listener->implementation[opcode],
listener->data,
&proxy->base,
&proxy->base.interface->events[opcode]);
listener = container_of(listener->link.next,
struct wl_listener, link);
}
}
wl_connection_consume(display->connection, size); wl_connection_consume(display->connection, size);
} }
@ -403,16 +523,6 @@ wl_display_iterate(struct wl_display *display, uint32_t mask)
} }
} }
WL_EXPORT void
wl_display_set_event_handler(struct wl_display *display,
wl_display_event_func_t handler,
void *data)
{
/* FIXME: This needs something more generic... */
display->event_handler = handler;
display->event_handler_data = data;
}
WL_EXPORT uint32_t WL_EXPORT uint32_t
wl_display_allocate_id(struct wl_display *display) wl_display_allocate_id(struct wl_display *display)
{ {
@ -435,32 +545,16 @@ wl_display_write(struct wl_display *display, const void *data, size_t count)
WL_EXPORT struct wl_compositor * WL_EXPORT struct wl_compositor *
wl_display_get_compositor(struct wl_display *display) wl_display_get_compositor(struct wl_display *display)
{ {
struct wl_compositor *compositor; return display->compositor;
uint32_t id;
id = wl_display_get_object_id(display, "compositor", 1);
if (id == 0)
return NULL;
compositor = malloc(sizeof *compositor);
compositor->proxy.base.interface = &wl_compositor_interface;
compositor->proxy.base.id = id;
compositor->proxy.display = display;
return compositor;
} }
static void WL_EXPORT int
wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...) wl_compositor_add_listener(struct wl_compositor *compositor,
const struct wl_compositor_listener *listener,
void *data)
{ {
va_list ap; return wl_proxy_add_listener(&compositor->proxy,
(void (**)(void)) listener, data);
va_start(ap, opcode);
wl_connection_vmarshal(proxy->display->connection,
&proxy->base, opcode, ap,
&proxy->base.interface->methods[opcode]);
va_end(ap);
} }
WL_EXPORT struct wl_surface * WL_EXPORT struct wl_surface *

@ -23,13 +23,7 @@
#ifndef _WAYLAND_CLIENT_H #ifndef _WAYLAND_CLIENT_H
#define _WAYLAND_CLIENT_H #define _WAYLAND_CLIENT_H
/* GCC visibility */ struct wl_object;
#if defined(__GNUC__) && __GNUC__ >= 4
#define WL_EXPORT __attribute__ ((visibility("default")))
#else
#define WL_EXPORT
#endif
struct wl_display; struct wl_display;
struct wl_surface; struct wl_surface;
struct wl_visual; struct wl_visual;
@ -37,6 +31,10 @@ struct wl_visual;
#define WL_DISPLAY_READABLE 0x01 #define WL_DISPLAY_READABLE 0x01
#define WL_DISPLAY_WRITABLE 0x02 #define WL_DISPLAY_WRITABLE 0x02
int
wl_object_implements(struct wl_object *object,
const char *interface, int version);
typedef int (*wl_display_update_func_t)(uint32_t mask, void *data); typedef int (*wl_display_update_func_t)(uint32_t mask, void *data);
struct wl_display *wl_display_create(const char *name, size_t name_size); struct wl_display *wl_display_create(const char *name, size_t name_size);
@ -46,20 +44,17 @@ int wl_display_get_fd(struct wl_display *display,
void wl_display_iterate(struct wl_display *display, uint32_t mask); void wl_display_iterate(struct wl_display *display, uint32_t mask);
typedef void (*wl_display_event_func_t)(struct wl_display *display, struct wl_global_listener;
uint32_t object, typedef void (*wl_display_global_func_t)(struct wl_display *display,
uint32_t opcode, struct wl_object *object,
uint32_t size, void *data);
uint32_t *p,
void *data);
void wl_display_set_event_handler(struct wl_display *display,
wl_display_event_func_t handler,
void *data);
void void
wl_display_get_geometry(struct wl_display *display, wl_display_remove_global_listener(struct wl_display *display,
int32_t *width, int32_t *height); struct wl_global_listener *listener);
struct wl_global_listener *
wl_display_add_global_listener(struct wl_display *display,
wl_display_global_func_t handler, void *data);
struct wl_compositor * struct wl_compositor *
wl_display_get_compositor(struct wl_display *display); wl_display_get_compositor(struct wl_display *display);
struct wl_visual * struct wl_visual *
@ -69,10 +64,24 @@ wl_display_get_premultiplied_argb_visual(struct wl_display *display);
struct wl_visual * struct wl_visual *
wl_display_get_rgb_visual(struct wl_display *display); wl_display_get_rgb_visual(struct wl_display *display);
struct wl_compositor_listener {
void (*acknowledge)(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame);
void (*frame)(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp);
};
struct wl_surface * struct wl_surface *
wl_compositor_create_surface(struct wl_compositor *compositor); wl_compositor_create_surface(struct wl_compositor *compositor);
void void
wl_compositor_commit(struct wl_compositor *compositor, uint32_t key); wl_compositor_commit(struct wl_compositor *compositor, uint32_t key);
int
wl_compositor_add_listener(struct wl_compositor *compostior,
const struct wl_compositor_listener *listener,
void *data);
void wl_surface_destroy(struct wl_surface *surface); void wl_surface_destroy(struct wl_surface *surface);
void wl_surface_attach(struct wl_surface *surface, uint32_t name, void wl_surface_attach(struct wl_surface *surface, uint32_t name,
@ -86,6 +95,37 @@ void wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
void wl_surface_damage(struct wl_surface *surface, void wl_surface_damage(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);
struct wl_output;
struct wl_output_listener {
void (*geometry)(void *data,
struct wl_output *output,
int32_t width, int32_t height);
};
int
wl_output_add_listener(struct wl_output *output,
const struct wl_output_listener *listener,
void *data);
struct wl_input_device;
struct wl_input_device_listener {
void (*motion)(void *data,
struct wl_input_device *input_device,
int32_t x, int32_t y, int32_t sx, int32_t sy);
void (*button)(void *data,
struct wl_input_device *input_device,
uint32_t button, uint32_t state,
int32_t x, int32_t y, int32_t sx, int32_t sy);
void (*key)(void *data,
struct wl_input_device *input_device,
uint32_t button, uint32_t state);
};
int
wl_input_device_add_listener(struct wl_input_device *input_device,
const struct wl_input_device_listener *listener,
void *data);
/* These entry points are for client side implementation of custom /* These entry points are for client side implementation of custom
* objects. */ * objects. */
@ -96,5 +136,6 @@ uint32_t wl_display_allocate_id(struct wl_display *display);
void wl_display_write(struct wl_display *display, void wl_display_write(struct wl_display *display,
const void *data, const void *data,
size_t count); size_t count);
void wl_display_advertise_global(struct wl_display *display,
struct wl_object *object);
#endif #endif

@ -44,21 +44,19 @@ struct window {
struct wl_compositor *compositor; struct wl_compositor *compositor;
struct wl_surface *surface; struct wl_surface *surface;
const char *title; const char *title;
struct rectangle allocation, saved_allocation; struct rectangle allocation, saved_allocation, screen_allocation;
int minimum_width, minimum_height; int minimum_width, minimum_height;
int margin; int margin;
int drag_x, drag_y; int drag_x, drag_y;
int state; int state;
int fullscreen; int fullscreen;
uint32_t grab_device; struct wl_input_device *grab_device;
uint32_t name; uint32_t name;
int fd; int fd;
struct buffer *buffer; struct buffer *buffer;
window_resize_handler_t resize_handler; window_resize_handler_t resize_handler;
window_frame_handler_t frame_handler;
window_acknowledge_handler_t acknowledge_handler;
window_key_handler_t key_handler; window_key_handler_t key_handler;
void *user_data; void *user_data;
}; };
@ -212,6 +210,36 @@ window_draw(struct window *window)
window_draw_decorations(window); window_draw_decorations(window);
} }
static void
window_handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct window *window = data;
/* The acknowledge event means that the server
* processed our last commit request and we can now
* safely free the old window buffer if we resized and
* render the next frame into our back buffer.. */
if (key == 0 && window->buffer != NULL) {
buffer_destroy(window->buffer, window->fd);
window->buffer = NULL;
}
}
static void
window_handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
}
static const struct wl_compositor_listener compositor_listener = {
window_handle_acknowledge,
window_handle_frame,
};
enum window_state { enum window_state {
WINDOW_STABLE, WINDOW_STABLE,
WINDOW_MOVING, WINDOW_MOVING,
@ -231,128 +259,105 @@ enum location {
}; };
static void static void
event_handler(struct wl_display *display, window_handle_motion(void *data, struct wl_input_device *input_device,
uint32_t object, uint32_t opcode, int32_t x, int32_t y, int32_t sx, int32_t sy)
uint32_t size, uint32_t *p, void *data)
{ {
struct window *window = data; struct window *window = data;
int location;
int grip_size = 16;
/* FIXME: Object ID 2 is the compositor, for anything else we
* assume it's an input device. */
if (object == 2 && opcode == 0) {
uint32_t key = p[0];
/* Ignore acknowledge events for window move requests. */
if (key != 0)
return;
/* The acknowledge event means that the server
* processed our last commit request and we can now
* safely free the old window buffer if we resized and
* render the next frame into our back buffer.. */
if (window->buffer != NULL) {
buffer_destroy(window->buffer, window->fd);
window->buffer = NULL;
}
if (window->acknowledge_handler)
(*window->acknowledge_handler)(window, key,
window->user_data);
} else if (object == 2 && opcode == 1) {
/* The frame event means that the previous frame was
* composited, and we can now send the request to copy
* the frame we've rendered in the mean time into the
* servers surface buffer. */
if (window->frame_handler)
(*window->frame_handler)(window, p[0], p[1],
window->user_data);
} else if (object == 1) {
fprintf(stderr, "unexpected event from display: %d\n",
opcode);
exit(-1);
} else if (opcode == 0) {
int x = p[0], y = p[1];
switch (window->state) {
case WINDOW_MOVING:
if (window->fullscreen)
break;
if (window->grab_device != object)
break;
window->allocation.x = window->drag_x + x;
window->allocation.y = window->drag_y + y;
wl_surface_map(window->surface,
window->allocation.x - window->margin,
window->allocation.y - window->margin,
window->allocation.width,
window->allocation.height);
wl_compositor_commit(window->compositor, 1);
break;
case WINDOW_RESIZING_LOWER_RIGHT:
if (window->fullscreen)
break;
if (window->grab_device != object)
break;
window->allocation.width = window->drag_x + x;
window->allocation.height = window->drag_y + y;
if (window->resize_handler)
(*window->resize_handler)(window,
window->user_data);
switch (window->state) {
case WINDOW_MOVING:
if (window->fullscreen)
break; break;
} if (window->grab_device != input_device)
} else if (opcode == 1) { break;
int button = p[0], state = p[1]; window->allocation.x = window->drag_x + x;
int32_t x = p[2], y = p[3]; window->allocation.y = window->drag_y + y;
int32_t left = window->allocation.x; wl_surface_map(window->surface,
int32_t right = window->allocation.x + window->allocation.x - window->margin,
window->allocation.width - window->margin * 2; window->allocation.y - window->margin,
int32_t top = window->allocation.y; window->allocation.width,
int32_t bottom = window->allocation.y + window->allocation.height);
window->allocation.height - window->margin * 2; wl_compositor_commit(window->compositor, 1);
break;
if (right - grip_size <= x && x < right && case WINDOW_RESIZING_LOWER_RIGHT:
bottom - grip_size <= y && y < bottom) { if (window->fullscreen)
location = LOCATION_LOWER_RIGHT; break;
} else if (left <= x && x < right && top <= y && y < bottom) { if (window->grab_device != input_device)
location = LOCATION_INTERIOR; break;
} else { window->allocation.width = window->drag_x + x;
location = LOCATION_OUTSIDE; window->allocation.height = window->drag_y + y;
}
if (window->resize_handler)
(*window->resize_handler)(window,
window->user_data);
break;
}
}
static void window_handle_button(void *data, struct wl_input_device *input_device,
uint32_t button, uint32_t state,
int32_t x, int32_t y, int32_t sx, int32_t sy)
{
struct window *window = data;
int32_t left = window->allocation.x;
int32_t right = window->allocation.x +
window->allocation.width - window->margin * 2;
int32_t top = window->allocation.y;
int32_t bottom = window->allocation.y +
window->allocation.height - window->margin * 2;
int grip_size = 16, location;
if (right - grip_size <= x && x < right &&
bottom - grip_size <= y && y < bottom) {
location = LOCATION_LOWER_RIGHT;
} else if (left <= x && x < right && top <= y && y < bottom) {
location = LOCATION_INTERIOR;
} else {
location = LOCATION_OUTSIDE;
}
if (button == BTN_LEFT && state == 1) { if (button == BTN_LEFT && state == 1) {
switch (location) { switch (location) {
case LOCATION_INTERIOR: case LOCATION_INTERIOR:
window->drag_x = window->allocation.x - x; window->drag_x = window->allocation.x - x;
window->drag_y = window->allocation.y - y; window->drag_y = window->allocation.y - y;
window->state = WINDOW_MOVING; window->state = WINDOW_MOVING;
window->grab_device = object; window->grab_device = input_device;
break; break;
case LOCATION_LOWER_RIGHT: case LOCATION_LOWER_RIGHT:
window->drag_x = window->allocation.width - x; window->drag_x = window->allocation.width - x;
window->drag_y = window->allocation.height - y; window->drag_y = window->allocation.height - y;
window->state = WINDOW_RESIZING_LOWER_RIGHT; window->state = WINDOW_RESIZING_LOWER_RIGHT;
window->grab_device = object; window->grab_device = input_device;
break; break;
default: default:
window->state = WINDOW_STABLE;
break;
}
} else if (button == BTN_LEFT &&
state == 0 && object == window->grab_device) {
window->state = WINDOW_STABLE; window->state = WINDOW_STABLE;
break;
} }
} else if (opcode == 2) { } else if (button == BTN_LEFT &&
if (window->key_handler) state == 0 && window->grab_device == input_device) {
(*window->key_handler)(window, p[0], p[1], window->state = WINDOW_STABLE;
window->user_data);
} }
} }
static void
window_handle_key(void *data, struct wl_input_device *input_device,
uint32_t button, uint32_t state)
{
struct window *window = data;
if (window->key_handler)
(*window->key_handler)(window, button, state,
window->user_data);
}
static const struct wl_input_device_listener input_device_listener = {
window_handle_motion,
window_handle_button,
window_handle_key,
};
void void
window_get_child_rectangle(struct window *window, window_get_child_rectangle(struct window *window,
struct rectangle *rectangle) struct rectangle *rectangle)
@ -397,11 +402,7 @@ window_set_fullscreen(struct window *window, int fullscreen)
window->fullscreen = fullscreen; window->fullscreen = fullscreen;
if (window->fullscreen) { if (window->fullscreen) {
window->saved_allocation = window->allocation; window->saved_allocation = window->allocation;
window->allocation.x = 0; window->allocation = window->screen_allocation;
window->allocation.y = 0;
wl_display_get_geometry(window->display,
&window->allocation.width,
&window->allocation.height);
} else { } else {
window->allocation = window->saved_allocation; window->allocation = window->saved_allocation;
} }
@ -416,27 +417,52 @@ window_set_resize_handler(struct window *window,
} }
void void
window_set_frame_handler(struct window *window, window_set_key_handler(struct window *window,
window_frame_handler_t handler, void *data) window_key_handler_t handler, void *data)
{ {
window->frame_handler = handler; window->key_handler = handler;
window->user_data = data; window->user_data = data;
} }
void static void
window_set_acknowledge_handler(struct window *window, window_handle_geometry(void *data,
window_acknowledge_handler_t handler, void *data) struct wl_output *output,
int32_t width, int32_t height)
{ {
window->acknowledge_handler = handler; struct window *window = data;
window->user_data = data;
window->screen_allocation.x = 0;
window->screen_allocation.y = 0;
window->screen_allocation.width = width;
window->screen_allocation.height = height;
} }
void static const struct wl_output_listener output_listener = {
window_set_key_handler(struct window *window, window_handle_geometry,
window_key_handler_t handler, void *data) };
static void
window_handle_global(struct wl_display *display,
struct wl_object *object, void *data)
{ {
window->key_handler = handler; struct window *window = data;
window->user_data = data;
if (wl_object_implements(object, "compositor", 1)) {
window->compositor = (struct wl_compositor *) object;
wl_compositor_add_listener(window->compositor,
&compositor_listener, window);
} else if (wl_object_implements(object, "output", 1)) {
struct wl_output *output = (struct wl_output *) object;
wl_output_add_listener(output,
&output_listener, window);
} else if (wl_object_implements(object, "input_device", 1)) {
struct wl_input_device *input_device =
(struct wl_input_device *) object;
wl_input_device_add_listener(input_device,
&input_device_listener, window);
}
} }
struct window * struct window *
@ -464,7 +490,8 @@ window_create(struct wl_display *display, int fd,
window->state = WINDOW_STABLE; window->state = WINDOW_STABLE;
window->fd = fd; window->fd = fd;
wl_display_set_event_handler(display, event_handler, window); wl_display_add_global_listener(display,
window_handle_global, window);
return window; return window;
} }

Loading…
Cancel
Save