Add listener interfaces for output and input_device objects.

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
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
event_handler(struct wl_display *display,
uint32_t object, uint32_t opcode,
uint32_t size, uint32_t *p, void *data)
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
if (object == 2 && opcode == 1)
move_flower(data);
struct flower *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[])
{
struct wl_display *display;
@ -177,8 +183,11 @@ int main(int argc, char *argv[])
wl_surface_attach(flower.surface,
buffer->name, flower.width, flower.height,
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);

@ -299,7 +299,9 @@ resize_handler(struct window *window, void *data)
}
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;
@ -313,7 +315,9 @@ acknowledge_handler(struct window *window, uint32_t key, void *data)
}
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;
@ -323,9 +327,14 @@ frame_handler(struct window *window, uint32_t frame, uint32_t timestamp, void *d
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 *
gears_create(struct wl_display *display, int fd)
{
@ -384,11 +393,12 @@ gears_create(struct wl_display *display, int fd)
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_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;
}

@ -397,12 +397,15 @@ resize_handler(struct window *window, void *data)
}
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;
terminal->redraw_scheduled = 0;
buffer_destroy(terminal->buffer, terminal->fd);
if (key == 0)
buffer_destroy(terminal->buffer, terminal->fd);
if (terminal->redraw_pending) {
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 {
int code[4];
} evdev_keymap[] = {
@ -549,7 +564,10 @@ terminal_create(struct wl_display *display, int fd, int fullscreen)
terminal->compositor = wl_display_get_compositor(display);
window_set_fullscreen(terminal->window, terminal->fullscreen);
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);
terminal_draw(terminal);

@ -44,31 +44,26 @@ struct wl_global {
uint32_t id;
char *interface;
uint32_t version;
struct wl_proxy *proxy;
struct wl_list link;
};
struct wl_proxy {
struct wl_object base;
struct wl_display *display;
struct wl_global_listener {
wl_display_global_func_t handler;
void *data;
struct wl_list link;
};
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_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_listener {
void (**implementation)(void);
void *data;
struct wl_list link;
};
struct wl_output *output;
struct wl_proxy {
struct wl_object base;
struct wl_display *display;
struct wl_list listener_list;
};
struct wl_compositor {
@ -81,14 +76,42 @@ struct wl_surface {
struct wl_visual {
struct wl_proxy proxy;
struct wl_list link;
};
struct wl_output {
struct wl_proxy proxy;
struct wl_listener listener;
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
connection_update(struct wl_connection *connection,
uint32_t mask, void *data)
@ -103,46 +126,117 @@ connection_update(struct wl_connection *connection,
return 0;
}
static void
output_handle_geometry(struct wl_display *display,
struct wl_output *output, int32_t width, int32_t height)
WL_EXPORT int
wl_object_implements(struct wl_object *object,
const char *interface, int version)
{
output->width = width;
output->height = height;
return strcmp(object->interface->name, interface) == 0 &&
object->interface->version >= version;
}
struct wl_output_listener {
void (*geometry)(struct wl_display *display,
struct wl_output *output,
int32_t width, int32_t height);
};
WL_EXPORT struct wl_global_listener *
wl_display_add_global_listener(struct wl_display *display,
wl_display_global_func_t handler, void *data)
{
struct wl_global_listener *listener;
struct wl_global *global;
static const struct wl_output_listener output_listener = {
output_handle_geometry
};
listener = malloc(sizeof *listener);
if (listener == NULL)
return NULL;
static void
add_output(struct wl_display *display, struct wl_global *global)
{
struct wl_output *output;
listener->handler = handler;
listener->data = data;
wl_list_insert(display->global_listener_list.prev, &listener->link);
output = malloc(sizeof *output);
if (output == NULL)
return;
/* FIXME: Need a destructor for void *data? */
output->proxy.base.interface = &wl_output_interface;
output->proxy.base.implementation = (void(**)(void)) &output_listener;
output->proxy.base.id = global->id;
output->proxy.display = display;
display->output = output;
wl_hash_insert(display->objects, &output->proxy.base);
global = container_of(display->global_list.next,
struct wl_global, link);
while (&global->link != &display->global_list) {
if (global->proxy != NULL)
(*handler)(display, &global->proxy->base, data);
global = container_of(global->link.next,
struct wl_global, link);
}
return listener;
}
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;
*height = display->output->height;
struct wl_listener *listener;
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
@ -150,66 +244,69 @@ add_visual(struct wl_display *display, struct wl_global *global)
{
struct wl_visual *visual;
visual = malloc(sizeof *visual);
if (visual == NULL)
return;
visual->proxy.base.interface = &wl_visual_interface;
visual->proxy.base.id = global->id;
visual->proxy.base.implementation = NULL;
visual->proxy.display = display;
wl_list_insert(display->visual_list.prev, &visual->link);
wl_hash_insert(display->objects, &visual->proxy.base);
visual = (struct wl_visual *)
wl_proxy_create_for_global(display, global,
&wl_visual_interface);
if (display->argb_visual == NULL)
display->argb_visual = visual;
else if (display->premultiplied_argb_visual == NULL)
display->premultiplied_argb_visual = visual;
else
display->rgb_visual = visual;
}
WL_EXPORT struct wl_visual *
wl_display_get_argb_visual(struct wl_display *display)
{
return container_of(display->visual_list.next,
struct wl_visual, link);
return display->argb_visual;
}
WL_EXPORT struct wl_visual *
wl_display_get_premultiplied_argb_visual(struct wl_display *display)
{
return container_of(display->visual_list.next->next,
struct wl_visual, link);
return display->premultiplied_argb_visual;
}
WL_EXPORT struct wl_visual *
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,
struct wl_visual, link);
WL_EXPORT int
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
display_handle_invalid_object(struct wl_display *display,
struct wl_object *object, uint32_t id)
display_handle_invalid_object(void *data,
struct wl_display *display, uint32_t id)
{
fprintf(stderr, "sent request to invalid object\n");
}
static void
display_handle_invalid_method(struct wl_display *display,
struct wl_object *object,
display_handle_invalid_method(void *data,
struct wl_display *display,
uint32_t id, uint32_t opcode)
{
fprintf(stderr, "sent invalid request opcode\n");
}
static void
display_handle_no_memory(struct wl_display *display,
struct wl_object *object)
display_handle_no_memory(void *data,
struct wl_display *display)
{
fprintf(stderr, "server out of memory\n");
}
static void
display_handle_global(struct wl_display *display,
struct wl_object *object,
display_handle_global(void *data,
struct wl_display *display,
uint32_t id, const char *interface, uint32_t version)
{
struct wl_global *global;
@ -222,32 +319,41 @@ display_handle_global(struct wl_display *display,
global->interface = strdup(interface);
global->version = version;
wl_list_insert(display->global_list.prev, &global->link);
if (strcmp(global->interface, "display") == 0)
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);
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
display_handle_range(struct wl_display *display,
struct wl_object *object, uint32_t range)
display_handle_range(void *data,
struct wl_display *display, uint32_t range)
{
display->next_range = range;
}
struct wl_display_listener {
void (*invalid_object)(struct wl_display *display,
struct wl_object *object, uint32_t id);
void (*invalid_method)(struct wl_display *display,
struct wl_object *object,
void (*invalid_object)(void *data,
struct wl_display *display, uint32_t id);
void (*invalid_method)(void *data, struct wl_display *display,
uint32_t id, uint32_t opcode);
void (*no_memory)(struct wl_display *display, struct wl_object *object);
void (*global)(struct wl_display *display, struct wl_object *object,
void (*no_memory)(void *data,
struct wl_display *display);
void (*global)(void *data, struct wl_display *display,
uint32_t id, const char *interface, uint32_t version);
void (*range)(struct wl_display *display,
struct wl_object *object, uint32_t range);
void (*range)(void *data,
struct wl_display *display, uint32_t range);
};
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();
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.implementation = (void(**)(void)) &display_listener;
display->proxy.base.id = 1;
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,
connection_update,
@ -351,26 +460,37 @@ handle_event(struct wl_display *display,
uint32_t id, uint32_t opcode, uint32_t size)
{
uint32_t p[32];
struct wl_object *object;
struct wl_listener *listener;
struct wl_proxy *proxy;
wl_connection_copy(display->connection, p, size);
if (id == 1)
object = &display->proxy.base;
proxy = &display->proxy;
else
object = wl_hash_lookup(display->objects, id);
if (object != NULL)
wl_connection_demarshal(display->connection,
size,
display->objects,
object->implementation[opcode],
display,
object,
&object->interface->events[opcode]);
else if (display->event_handler != NULL)
display->event_handler(display, id,
opcode, size, p + 2,
display->event_handler_data);
proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
if (proxy != NULL) {
if (wl_list_empty(&proxy->listener_list)) {
printf("proxy found for object %d, opcode %d, but no listeners\n",
id, opcode);
}
listener = container_of(proxy->listener_list.next,
struct wl_listener, link);
while (&listener->link != &proxy->listener_list) {
wl_connection_demarshal(display->connection,
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);
}
@ -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_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_display_get_compositor(struct wl_display *display)
{
struct wl_compositor *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;
return display->compositor;
}
static void
wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
WL_EXPORT int
wl_compositor_add_listener(struct wl_compositor *compositor,
const struct wl_compositor_listener *listener,
void *data)
{
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);
return wl_proxy_add_listener(&compositor->proxy,
(void (**)(void)) listener, data);
}
WL_EXPORT struct wl_surface *

@ -23,13 +23,7 @@
#ifndef _WAYLAND_CLIENT_H
#define _WAYLAND_CLIENT_H
/* GCC visibility */
#if defined(__GNUC__) && __GNUC__ >= 4
#define WL_EXPORT __attribute__ ((visibility("default")))
#else
#define WL_EXPORT
#endif
struct wl_object;
struct wl_display;
struct wl_surface;
struct wl_visual;
@ -37,6 +31,10 @@ struct wl_visual;
#define WL_DISPLAY_READABLE 0x01
#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);
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);
typedef void (*wl_display_event_func_t)(struct wl_display *display,
uint32_t object,
uint32_t opcode,
uint32_t size,
uint32_t *p,
void *data);
void wl_display_set_event_handler(struct wl_display *display,
wl_display_event_func_t handler,
void *data);
struct wl_global_listener;
typedef void (*wl_display_global_func_t)(struct wl_display *display,
struct wl_object *object,
void *data);
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);
struct wl_global_listener *
wl_display_add_global_listener(struct wl_display *display,
wl_display_global_func_t handler, void *data);
struct wl_compositor *
wl_display_get_compositor(struct wl_display *display);
struct wl_visual *
@ -69,10 +64,24 @@ wl_display_get_premultiplied_argb_visual(struct wl_display *display);
struct wl_visual *
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 *
wl_compositor_create_surface(struct wl_compositor *compositor);
void
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_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,
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
* objects. */
@ -96,5 +136,6 @@ uint32_t wl_display_allocate_id(struct wl_display *display);
void wl_display_write(struct wl_display *display,
const void *data,
size_t count);
void wl_display_advertise_global(struct wl_display *display,
struct wl_object *object);
#endif

@ -44,21 +44,19 @@ struct window {
struct wl_compositor *compositor;
struct wl_surface *surface;
const char *title;
struct rectangle allocation, saved_allocation;
struct rectangle allocation, saved_allocation, screen_allocation;
int minimum_width, minimum_height;
int margin;
int drag_x, drag_y;
int state;
int fullscreen;
uint32_t grab_device;
struct wl_input_device *grab_device;
uint32_t name;
int fd;
struct buffer *buffer;
window_resize_handler_t resize_handler;
window_frame_handler_t frame_handler;
window_acknowledge_handler_t acknowledge_handler;
window_key_handler_t key_handler;
void *user_data;
};
@ -212,6 +210,36 @@ window_draw(struct window *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 {
WINDOW_STABLE,
WINDOW_MOVING,
@ -231,128 +259,105 @@ enum location {
};
static void
event_handler(struct wl_display *display,
uint32_t object, uint32_t opcode,
uint32_t size, uint32_t *p, void *data)
window_handle_motion(void *data, struct wl_input_device *input_device,
int32_t x, int32_t y, int32_t sx, int32_t sy)
{
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;
}
} else if (opcode == 1) {
int button = p[0], state = p[1];
int32_t x = p[2], y = p[3];
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;
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 (window->grab_device != input_device)
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 != input_device)
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);
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) {
switch (location) {
case LOCATION_INTERIOR:
window->drag_x = window->allocation.x - x;
window->drag_y = window->allocation.y - y;
window->state = WINDOW_MOVING;
window->grab_device = object;
break;
case LOCATION_LOWER_RIGHT:
window->drag_x = window->allocation.width - x;
window->drag_y = window->allocation.height - y;
window->state = WINDOW_RESIZING_LOWER_RIGHT;
window->grab_device = object;
break;
default:
window->state = WINDOW_STABLE;
break;
}
} else if (button == BTN_LEFT &&
state == 0 && object == window->grab_device) {
if (button == BTN_LEFT && state == 1) {
switch (location) {
case LOCATION_INTERIOR:
window->drag_x = window->allocation.x - x;
window->drag_y = window->allocation.y - y;
window->state = WINDOW_MOVING;
window->grab_device = input_device;
break;
case LOCATION_LOWER_RIGHT:
window->drag_x = window->allocation.width - x;
window->drag_y = window->allocation.height - y;
window->state = WINDOW_RESIZING_LOWER_RIGHT;
window->grab_device = input_device;
break;
default:
window->state = WINDOW_STABLE;
break;
}
} else if (opcode == 2) {
if (window->key_handler)
(*window->key_handler)(window, p[0], p[1],
window->user_data);
} else if (button == BTN_LEFT &&
state == 0 && window->grab_device == input_device) {
window->state = WINDOW_STABLE;
}
}
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
window_get_child_rectangle(struct window *window,
struct rectangle *rectangle)
@ -397,11 +402,7 @@ window_set_fullscreen(struct window *window, int fullscreen)
window->fullscreen = fullscreen;
if (window->fullscreen) {
window->saved_allocation = window->allocation;
window->allocation.x = 0;
window->allocation.y = 0;
wl_display_get_geometry(window->display,
&window->allocation.width,
&window->allocation.height);
window->allocation = window->screen_allocation;
} else {
window->allocation = window->saved_allocation;
}
@ -416,27 +417,52 @@ window_set_resize_handler(struct window *window,
}
void
window_set_frame_handler(struct window *window,
window_frame_handler_t handler, void *data)
window_set_key_handler(struct window *window,
window_key_handler_t handler, void *data)
{
window->frame_handler = handler;
window->key_handler = handler;
window->user_data = data;
}
void
window_set_acknowledge_handler(struct window *window,
window_acknowledge_handler_t handler, void *data)
static void
window_handle_geometry(void *data,
struct wl_output *output,
int32_t width, int32_t height)
{
window->acknowledge_handler = handler;
window->user_data = data;
struct window *window = data;
window->screen_allocation.x = 0;
window->screen_allocation.y = 0;
window->screen_allocation.width = width;
window->screen_allocation.height = height;
}
void
window_set_key_handler(struct window *window,
window_key_handler_t handler, void *data)
static const struct wl_output_listener output_listener = {
window_handle_geometry,
};
static void
window_handle_global(struct wl_display *display,
struct wl_object *object, void *data)
{
window->key_handler = handler;
window->user_data = data;
struct window *window = 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 *
@ -464,7 +490,8 @@ window_create(struct wl_display *display, int fd,
window->state = WINDOW_STABLE;
window->fd = fd;
wl_display_set_event_handler(display, event_handler, window);
wl_display_add_global_listener(display,
window_handle_global, window);
return window;
}

Loading…
Cancel
Save