Only allow one listener per proxy

This commit is contained in:
Kristian Høgsberg 2011-02-18 15:28:54 -05:00
parent 9a2015562c
commit 9136e05ebd

View file

@ -44,16 +44,9 @@ struct wl_global_listener {
struct wl_list link; struct wl_list link;
}; };
struct wl_listener {
void (**implementation)(void);
void *data;
struct wl_list link;
};
struct wl_proxy { struct wl_proxy {
struct wl_object object; struct wl_object object;
struct wl_display *display; struct wl_display *display;
struct wl_list listener_list;
void *user_data; void *user_data;
}; };
@ -78,7 +71,6 @@ struct wl_display {
uint32_t id, id_count, next_range; uint32_t id, id_count, next_range;
uint32_t mask; uint32_t mask;
struct wl_hash_table *objects; struct wl_hash_table *objects;
struct wl_listener listener;
struct wl_list global_listener_list; struct wl_list global_listener_list;
struct wl_visual *argb_visual; struct wl_visual *argb_visual;
@ -147,9 +139,9 @@ wl_proxy_create_for_id(struct wl_display *display,
return NULL; return NULL;
proxy->object.interface = interface; proxy->object.interface = interface;
proxy->object.implementation = NULL;
proxy->object.id = id; proxy->object.id = id;
proxy->display = display; proxy->display = display;
wl_list_init(&proxy->listener_list);
wl_hash_table_insert(display->objects, proxy->object.id, proxy); wl_hash_table_insert(display->objects, proxy->object.id, proxy);
return proxy; return proxy;
@ -166,11 +158,6 @@ wl_proxy_create(struct wl_proxy *factory,
WL_EXPORT void WL_EXPORT void
wl_proxy_destroy(struct wl_proxy *proxy) wl_proxy_destroy(struct wl_proxy *proxy)
{ {
struct wl_listener *listener, *next;
wl_list_for_each_safe(listener, next, &proxy->listener_list, link)
free(listener);
wl_hash_table_remove(proxy->display->objects, proxy->object.id); wl_hash_table_remove(proxy->display->objects, proxy->object.id);
free(proxy); free(proxy);
} }
@ -179,15 +166,13 @@ WL_EXPORT int
wl_proxy_add_listener(struct wl_proxy *proxy, wl_proxy_add_listener(struct wl_proxy *proxy,
void (**implementation)(void), void *data) void (**implementation)(void), void *data)
{ {
struct wl_listener *listener; if (proxy->object.implementation) {
fprintf(stderr, "proxy already has listener\n");
listener = malloc(sizeof *listener);
if (listener == NULL)
return -1; return -1;
}
listener->implementation = (void (**)(void)) implementation; proxy->object.implementation = implementation;
listener->data = data; proxy->user_data = data;
wl_list_insert(proxy->listener_list.prev, &listener->link);
return 0; return 0;
} }
@ -393,13 +378,13 @@ wl_display_connect(const char *name)
display->proxy.object.interface = &wl_display_interface; display->proxy.object.interface = &wl_display_interface;
display->proxy.object.id = 1; display->proxy.object.id = 1;
display->proxy.display = display; display->proxy.display = display;
wl_list_init(&display->proxy.listener_list);
wl_list_init(&display->sync_list); wl_list_init(&display->sync_list);
wl_list_init(&display->frame_list); wl_list_init(&display->frame_list);
display->listener.implementation = (void(**)(void)) &display_listener; display->proxy.object.implementation =
wl_list_insert(display->proxy.listener_list.prev, &display->listener.link); (void(**)(void)) &display_listener;
display->proxy.user_data = display;
display->connection = wl_connection_create(display->fd, display->connection = wl_connection_create(display->fd,
connection_update, connection_update,
@ -473,7 +458,6 @@ 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_listener *listener;
struct wl_proxy *proxy; struct wl_proxy *proxy;
struct wl_closure *closure; struct wl_closure *closure;
const struct wl_message *message; const struct wl_message *message;
@ -484,7 +468,7 @@ handle_event(struct wl_display *display,
else else
proxy = wl_hash_table_lookup(display->objects, id); proxy = wl_hash_table_lookup(display->objects, id);
if (proxy == NULL) { if (proxy == NULL || proxy->object.implementation == NULL) {
wl_connection_consume(display->connection, size); wl_connection_consume(display->connection, size);
return; return;
} }
@ -496,10 +480,9 @@ handle_event(struct wl_display *display,
if (wl_debug) if (wl_debug)
wl_closure_print(closure, &proxy->object); wl_closure_print(closure, &proxy->object);
wl_list_for_each(listener, &proxy->listener_list, link) wl_closure_invoke(closure, &proxy->object,
wl_closure_invoke(closure, &proxy->object, proxy->object.implementation[opcode],
listener->implementation[opcode], proxy->user_data);
listener->data);
wl_closure_destroy(closure); wl_closure_destroy(closure);
} }