[PATCH weston 1/2] compositor: add an exported function to take a screenshot of an output

Giulio Camuffo giuliocamuffo at gmail.com
Wed Dec 11 14:45:11 PST 2013


This allows other screenshooter protocols to use the same code to copy
the screen to a buffer.
---
 src/compositor.h    | 12 ++++++++
 src/screenshooter.c | 82 +++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 70 insertions(+), 24 deletions(-)

diff --git a/src/compositor.h b/src/compositor.h
index 6bd637e..b6bf78d 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1260,6 +1260,18 @@ tty_activate_vt(struct tty *tty, int vt);
 void
 screenshooter_create(struct weston_compositor *ec);
 
+enum weston_screenshooter_outcome {
+	WESTON_SCREENSHOOTER_SUCCESS,
+	WESTON_SCREENSHOOTER_NO_MEMORY,
+	WESTON_SCREENSHOOTER_BAD_BUFFER
+};
+
+typedef void (*weston_screenshooter_done_func_t)(void *data,
+				enum weston_screenshooter_outcome outcome);
+int
+weston_screenshooter_shoot(struct weston_output *output, struct weston_buffer *buffer,
+			   weston_screenshooter_done_func_t done, void *data);
+
 struct clipboard *
 clipboard_create(struct weston_seat *seat);
 
diff --git a/src/screenshooter.c b/src/screenshooter.c
index 0c657bc..14ee35a 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -46,7 +46,8 @@ struct screenshooter {
 struct screenshooter_frame_listener {
 	struct wl_listener listener;
 	struct weston_buffer *buffer;
-	struct wl_resource *resource;
+	weston_screenshooter_done_func_t done;
+	void *data;
 };
 
 static void
@@ -129,7 +130,7 @@ screenshooter_frame_notify(struct wl_listener *listener, void *data)
 	pixels = malloc(stride * l->buffer->height);
 
 	if (pixels == NULL) {
-		wl_resource_post_no_memory(l->resource);
+		l->done(l->data, WESTON_SCREENSHOOTER_NO_MEMORY);
 		free(l);
 		return;
 	}
@@ -167,51 +168,84 @@ screenshooter_frame_notify(struct wl_listener *listener, void *data)
 
 	wl_shm_buffer_end_access(l->buffer->shm_buffer);
 
-	screenshooter_send_done(l->resource);
+	l->done(l->data, WESTON_SCREENSHOOTER_SUCCESS);
 	free(pixels);
 	free(l);
 }
 
-static void
-screenshooter_shoot(struct wl_client *client,
-		    struct wl_resource *resource,
-		    struct wl_resource *output_resource,
-		    struct wl_resource *buffer_resource)
+WL_EXPORT int
+weston_screenshooter_shoot(struct weston_output *output,
+			   struct weston_buffer *buffer,
+			   weston_screenshooter_done_func_t done, void *data)
 {
-	struct weston_output *output =
-		wl_resource_get_user_data(output_resource);
 	struct screenshooter_frame_listener *l;
-	struct weston_buffer *buffer =
-		weston_buffer_from_resource(buffer_resource);
 
-	if (buffer == NULL) {
-		wl_resource_post_no_memory(resource);
-		return;
+	if (!wl_shm_buffer_get(buffer->resource)) {
+		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
+		return -1;
 	}
-	if (!wl_shm_buffer_get(buffer->resource))
-		return;
-	
+
 	buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
 	buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
 	buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
 
 	if (buffer->width < output->current_mode->width ||
-	    buffer->height < output->current_mode->height)
-		return;
+	    buffer->height < output->current_mode->height) {
+		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
+		return -1;
+	}
 
 	l = malloc(sizeof *l);
 	if (l == NULL) {
-		wl_resource_post_no_memory(resource);
-		return;
+		done(data, WESTON_SCREENSHOOTER_NO_MEMORY);
+		return -1;
 	}
 
 	l->buffer = buffer;
-	l->resource = resource;
-
+	l->done = done;
+	l->data = data;
 	l->listener.notify = screenshooter_frame_notify;
 	wl_signal_add(&output->frame_signal, &l->listener);
 	output->disable_planes++;
 	weston_output_schedule_repaint(output);
+
+	return 0;
+}
+
+static void
+screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
+{
+	struct wl_resource *resource = data;
+
+	switch (outcome) {
+	case WESTON_SCREENSHOOTER_SUCCESS:
+		screenshooter_send_done(resource);
+		break;
+	case WESTON_SCREENSHOOTER_NO_MEMORY:
+		wl_resource_post_no_memory(resource);
+		break;
+	default:
+		break;
+	}
+}
+
+static void
+screenshooter_shoot(struct wl_client *client,
+		    struct wl_resource *resource,
+		    struct wl_resource *output_resource,
+		    struct wl_resource *buffer_resource)
+{
+	struct weston_output *output =
+		wl_resource_get_user_data(output_resource);
+	struct weston_buffer *buffer =
+		weston_buffer_from_resource(buffer_resource);
+
+	if (buffer == NULL) {
+		wl_resource_post_no_memory(resource);
+		return;
+	}
+
+	weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
 }
 
 struct screenshooter_interface screenshooter_implementation = {
-- 
1.8.5.1



More information about the wayland-devel mailing list