[PATCH 1/3] Install structuring for ping-pong protocol

Scott Moreau oreaus at gmail.com
Mon Apr 16 10:25:40 PDT 2012


---
 clients/window.c |    8 ++++
 src/compositor.c |   17 ++++++++
 src/compositor.h |    2 +
 src/shell.c      |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/clients/window.c b/clients/window.c
index a033130..e79747b 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -1947,6 +1947,13 @@ widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
 }
 
 static void
+handle_ping(void *data, struct wl_shell_surface *shell_surface,
+							uint32_t serial)
+{
+	wl_shell_surface_pong(shell_surface, serial);
+}
+
+static void
 handle_configure(void *data, struct wl_shell_surface *shell_surface,
 		 uint32_t edges, int32_t width, int32_t height)
 {
@@ -1984,6 +1991,7 @@ handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
 }
 
 static const struct wl_shell_surface_listener shell_surface_listener = {
+	handle_ping,
 	handle_configure,
 	handle_popup_done
 };
diff --git a/src/compositor.c b/src/compositor.c
index 3e0139d..6ee9879 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1529,8 +1529,13 @@ notify_button(struct wl_input_device *device,
 {
 	struct weston_input_device *wd = (struct weston_input_device *) device;
 	struct weston_compositor *compositor = wd->compositor;
+	struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+	uint32_t serial = wl_display_next_serial(compositor->wl_display);
 
 	if (state) {
+		if (compositor->ping_handler && focus)
+			compositor->ping_handler(focus, serial);
+
 		weston_compositor_idle_inhibit(compositor);
 		if (device->button_count == 0) {
 			device->grab_button = button;
@@ -1559,6 +1564,11 @@ notify_axis(struct wl_input_device *device,
 {
 	struct weston_input_device *wd = (struct weston_input_device *) device;
 	struct weston_compositor *compositor = wd->compositor;
+	struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+	uint32_t serial = wl_display_next_serial(compositor->wl_display);
+
+	if (compositor->ping_handler && focus)
+		compositor->ping_handler(focus, serial);
 
 	weston_compositor_activity(compositor);
 
@@ -1612,6 +1622,8 @@ notify_key(struct wl_input_device *device,
 {
 	struct weston_input_device *wd = (struct weston_input_device *) device;
 	struct weston_compositor *compositor = wd->compositor;
+	struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+	uint32_t serial = wl_display_next_serial(compositor->wl_display);
 	uint32_t *k, *end;
 
 	if (state) {
@@ -1630,6 +1642,9 @@ notify_key(struct wl_input_device *device,
 	}
 	device->keys.size = (void *) end - device->keys.data;
 	if (state) {
+		if (compositor->ping_handler && focus)
+			compositor->ping_handler(focus, serial);
+
 		k = wl_array_add(&device->keys, sizeof *k);
 		*k = key;
 	}
@@ -2399,6 +2414,8 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
 
 	screenshooter_create(ec);
 
+	ec->ping_handler = NULL;
+
 	wl_data_device_manager_init(ec->wl_display);
 
 	glActiveTexture(GL_TEXTURE0);
diff --git a/src/compositor.h b/src/compositor.h
index 8fc5ab0..4dd1d3c 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -227,6 +227,8 @@ struct weston_compositor {
 	void (*destroy)(struct weston_compositor *ec);
 	int (*authenticate)(struct weston_compositor *c, uint32_t id);
 
+	void (*ping_handler)(struct weston_surface *surface, uint32_t serial);
+
 	struct screenshooter *screenshooter;
 	int launcher_sock;
 };
diff --git a/src/shell.c b/src/shell.c
index cfce25e..d8374fa 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -106,6 +106,7 @@ struct shell_surface {
 	enum shell_surface_type type;
 	int32_t saved_x, saved_y;
 	bool saved_position_valid;
+	int is_not_responsive;
 
 	struct {
 		struct weston_transform transform;
@@ -128,6 +129,8 @@ struct shell_surface {
 		struct weston_surface *black_surface;
 	} fullscreen;
 
+	struct wl_list ping_timer_list;
+
 	struct weston_output *fullscreen_output;
 	struct weston_output *output;
 	struct wl_list link;
@@ -155,6 +158,14 @@ struct rotate_grab {
 	} center;
 };
 
+struct ping_timer {
+	struct wl_event_source *source;
+	struct shell_surface *shsurf;
+	uint32_t pong_received;
+	uint32_t serial;
+	struct wl_list link;
+};
+
 static void
 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
 {
@@ -265,6 +276,82 @@ static const struct wl_pointer_grab_interface move_grab_interface = {
 };
 
 static int
+ping_timeout_handler(void *data)
+{
+	struct ping_timer *timer = data, *timer_tmp, *next;
+
+	if (!timer)
+		return 1;
+
+	if (!timer->shsurf) {
+		/* Surface no longer exists */
+		free(timer);
+		return 1;
+	}
+
+	if (timer->pong_received) {
+		wl_list_for_each_safe(timer_tmp, next,
+			&timer->shsurf->ping_timer_list, link) {
+
+			if (timer_tmp->serial == timer->serial) {
+				wl_list_remove(&timer_tmp->link);
+				break;
+			}
+		}
+		free(timer);
+	}
+	else {
+		/* Client is not responding */
+		timer->shsurf->is_not_responsive = 1;
+	}
+
+	return 1;
+}
+
+static void
+ping_handler(struct weston_surface *surface, uint32_t serial)
+{
+	struct shell_surface *shsurf;
+	shsurf = get_shell_surface(surface);
+	struct wl_event_loop *loop;
+	struct ping_timer *timer;
+	int ping_timeout = 15000;
+
+	timer = malloc(sizeof *timer);
+	if (!timer || ! shsurf)
+		return;
+
+	loop = wl_display_get_event_loop(surface->compositor->wl_display);
+	timer->source =
+		wl_event_loop_add_timer(loop, ping_timeout_handler, timer);
+	timer->serial = serial;
+	timer->shsurf = shsurf;
+	timer->pong_received = 0;
+	wl_list_insert(&timer->shsurf->ping_timer_list, &timer->link);
+	wl_event_source_timer_update(timer->source, ping_timeout);
+
+	wl_shell_surface_send_ping(&shsurf->resource, serial);
+}
+
+static void
+shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
+							uint32_t serial)
+{
+	struct shell_surface *shsurf = resource->data;
+	struct ping_timer *timer;
+
+	if (!shsurf)
+		return;
+
+	wl_list_for_each(timer, &shsurf->ping_timer_list, link) {
+		if (timer->serial == serial) {
+			timer->pong_received = 1;
+			timer->shsurf->is_not_responsive = 0;
+		}
+	}
+}
+
+static int
 weston_surface_move(struct weston_surface *es,
 		    struct weston_input_device *wd)
 {
@@ -826,6 +913,7 @@ shell_surface_set_popup(struct wl_client *client,
 }
 
 static const struct wl_shell_surface_interface shell_surface_implementation = {
+	shell_surface_pong,
 	shell_surface_move,
 	shell_surface_resize,
 	shell_surface_set_toplevel,
@@ -839,6 +927,7 @@ static void
 destroy_shell_surface(struct wl_resource *resource)
 {
 	struct shell_surface *shsurf = resource->data;
+	struct ping_timer *timer, *next;
 
 	if (shsurf->popup.grab.input_device)
 		wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
@@ -852,6 +941,12 @@ destroy_shell_surface(struct wl_resource *resource)
 	if (shsurf->fullscreen.black_surface)
 		weston_surface_destroy(shsurf->fullscreen.black_surface);
 
+	if (!wl_list_empty(&shsurf->ping_timer_list)) {
+		wl_list_for_each_safe(timer, next, &shsurf->ping_timer_list, link) {
+			timer->shsurf = NULL;
+		}
+	}
+
 	wl_list_remove(&shsurf->link);
 	free(shsurf);
 }
@@ -915,6 +1010,8 @@ shell_get_shell_surface(struct wl_client *client,
 
 	surface->configure = shell_surface_configure;
 
+	shsurf->is_not_responsive = 0;
+
 	shsurf->resource.destroy = destroy_shell_surface;
 	shsurf->resource.object.id = id;
 	shsurf->resource.object.interface = &wl_shell_surface_interface;
@@ -929,6 +1026,7 @@ shell_get_shell_surface(struct wl_client *client,
 	shsurf->fullscreen.framerate = 0;
 	shsurf->fullscreen.black_surface = NULL;
 	wl_list_init(&shsurf->fullscreen.transform.link);
+	wl_list_init(&shsurf->ping_timer_list);
 
 	shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
 	wl_signal_add(&surface->surface.resource.destroy_signal,
@@ -2171,12 +2269,27 @@ debug_repaint_binding(struct wl_input_device *device, uint32_t time,
 static void
 shell_destroy(struct wl_listener *listener, void *data)
 {
+	struct weston_surface *surface;
+	struct shell_surface *shsurf;
+	struct ping_timer *timer, *next;
 	struct wl_shell *shell =
 		container_of(listener, struct wl_shell, destroy_listener);
 
 	if (shell->child.client)
 		wl_client_destroy(shell->child.client);
 
+	wl_list_for_each(surface, &shell->compositor->surface_list, link) {
+		shsurf = get_shell_surface(surface);
+		if (!shsurf)
+			continue;
+		if (!wl_list_empty(&shsurf->ping_timer_list))
+			wl_list_for_each_safe(timer, next,
+				&shsurf->ping_timer_list, link) {
+				if (timer)
+					free(timer);
+		}
+	}
+
 	free(shell->screensaver.path);
 	free(shell);
 }
@@ -2202,6 +2315,7 @@ shell_init(struct weston_compositor *ec)
 	wl_signal_add(&ec->lock_signal, &shell->lock_listener);
 	shell->unlock_listener.notify = unlock;
 	wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
+	ec->ping_handler = ping_handler;
 
 	wl_list_init(&shell->backgrounds);
 	wl_list_init(&shell->panels);
-- 
1.7.4.1



More information about the wayland-devel mailing list