[PATCH weston v2 2/8] Move part of screenshooter.c to weston-screenshooter.c

Giulio Camuffo giuliocamuffo at gmail.com
Wed Jun 1 18:43:24 UTC 2016


This patch splits screensooter.c so that the code implementing
the private screenshooter protocol and launching the client is
moved to a weston specific file, leaving only the code that can
be shared between compositors in screenshooter.c.
Two exported functions are added in screenshooter.c to start and
stop the recorder.

Signed-off-by: Giulio Camuffo <giuliocamuffo at gmail.com>
---

v2

 Makefile.am                |   2 +
 desktop-shell/shell.c      |   1 +
 src/compositor.h           |   8 +-
 src/screenshooter.c        | 187 +++++---------------------------------------
 src/weston-screenshooter.c | 191 +++++++++++++++++++++++++++++++++++++++++++++
 src/weston.h               |  42 ++++++++++
 6 files changed, 261 insertions(+), 170 deletions(-)
 create mode 100644 src/weston-screenshooter.c
 create mode 100644 src/weston.h

diff --git a/Makefile.am b/Makefile.am
index 00b74e5..b32084f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -95,6 +95,8 @@ weston_SOURCES =					\
 	src/main.c					\
 	src/linux-dmabuf.c				\
 	src/linux-dmabuf.h				\
+	src/weston.h					\
+	src/weston-screenshooter.c			\
 	shared/helpers.h				\
 	shared/matrix.c					\
 	shared/matrix.h					\
diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index 7d5bca9..ec71cd1 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -36,6 +36,7 @@
 #include <sys/types.h>
 
 #include "shell.h"
+#include "weston.h"
 #include "weston-desktop-shell-server-protocol.h"
 #include "shared/config-parser.h"
 #include "shared/helpers.h"
diff --git a/src/compositor.h b/src/compositor.h
index c19f991..1eaa6bf 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -58,6 +58,7 @@ struct weston_output;
 struct input_method;
 struct weston_pointer;
 struct linux_dmabuf_buffer;
+struct weston_recorder;
 
 enum weston_keyboard_modifier {
 	MODIFIER_CTRL = (1 << 0),
@@ -1577,9 +1578,6 @@ tty_reset(struct tty *tty);
 int
 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,
@@ -1591,6 +1589,10 @@ typedef void (*weston_screenshooter_done_func_t)(void *data,
 int
 weston_screenshooter_shoot(struct weston_output *output, struct weston_buffer *buffer,
 			   weston_screenshooter_done_func_t done, void *data);
+struct weston_recorder *
+weston_recorder_start(struct weston_output *output, const char *filename);
+void
+weston_recorder_stop(struct weston_recorder *recorder);
 
 struct clipboard *
 clipboard_create(struct weston_seat *seat);
diff --git a/src/screenshooter.c b/src/screenshooter.c
index e8b9090..fc14ad3 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -34,19 +34,10 @@
 #include <sys/uio.h>
 
 #include "compositor.h"
-#include "weston-screenshooter-server-protocol.h"
 #include "shared/helpers.h"
 
 #include "wcap/wcap-decode.h"
 
-struct screenshooter {
-	struct weston_compositor *ec;
-	struct wl_global *global;
-	struct wl_client *client;
-	struct weston_process process;
-	struct wl_listener destroy_listener;
-};
-
 struct screenshooter_frame_listener {
 	struct wl_listener listener;
 	struct weston_buffer *buffer;
@@ -216,98 +207,6 @@ weston_screenshooter_shoot(struct weston_output *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:
-		weston_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 weston_screenshooter_interface screenshooter_implementation = {
-	screenshooter_shoot
-};
-
-static void
-bind_shooter(struct wl_client *client,
-	     void *data, uint32_t version, uint32_t id)
-{
-	struct screenshooter *shooter = data;
-	struct wl_resource *resource;
-
-	resource = wl_resource_create(client,
-				      &weston_screenshooter_interface, 1, id);
-
-	if (client != shooter->client) {
-		wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
-				       "screenshooter failed: permission denied");
-		return;
-	}
-
-	wl_resource_set_implementation(resource, &screenshooter_implementation,
-				       data, NULL);
-}
-
-static void
-screenshooter_sigchld(struct weston_process *process, int status)
-{
-	struct screenshooter *shooter =
-		container_of(process, struct screenshooter, process);
-
-	shooter->client = NULL;
-}
-
-static void
-screenshooter_binding(struct weston_keyboard *keyboard, uint32_t time,
-		      uint32_t key, void *data)
-{
-	struct screenshooter *shooter = data;
-	char *screenshooter_exe;
-	int ret;
-
-	ret = asprintf(&screenshooter_exe, "%s/%s",
-		       weston_config_get_libexec_dir(),
-		       "/weston-screenshooter");
-	if (ret < 0) {
-		weston_log("Could not construct screenshooter path.\n");
-		return;
-	}
-
-	if (!shooter->client)
-		shooter->client = weston_client_launch(shooter->ec,
-					&shooter->process,
-					screenshooter_exe, screenshooter_sigchld);
-	free(screenshooter_exe);
-}
-
 struct weston_recorder {
 	struct weston_output *output;
 	uint32_t *frame, *rect;
@@ -475,7 +374,7 @@ weston_recorder_free(struct weston_recorder *recorder)
 	free(recorder);
 }
 
-static void
+static struct weston_recorder *
 weston_recorder_create(struct weston_output *output, const char *filename)
 {
 	struct weston_compositor *compositor = output->compositor;
@@ -489,7 +388,7 @@ weston_recorder_create(struct weston_output *output, const char *filename)
 	recorder = zalloc(sizeof *recorder);
 	if (recorder == NULL) {
 		weston_log("%s: out of memory\n", __func__);
-		return;
+		return NULL;
 	}
 
 	stride = output->current_mode->width;
@@ -543,11 +442,11 @@ weston_recorder_create(struct weston_output *output, const char *filename)
 	output->disable_planes++;
 	weston_output_damage(output);
 
-	return;
+	return recorder;
 
 err_recorder:
 	weston_recorder_free(recorder);
-	return;
+	return NULL;
 }
 
 static void
@@ -559,76 +458,30 @@ weston_recorder_destroy(struct weston_recorder *recorder)
 	weston_recorder_free(recorder);
 }
 
-static void
-recorder_binding(struct weston_keyboard *keyboard, uint32_t time,
-		 uint32_t key, void *data)
+WL_EXPORT struct weston_recorder *
+weston_recorder_start(struct weston_output *output, const char *filename)
 {
-	struct weston_compositor *ec = keyboard->seat->compositor;
-	struct weston_output *output;
-	struct wl_listener *listener = NULL;
-	struct weston_recorder *recorder;
-	static const char filename[] = "capture.wcap";
-
-	wl_list_for_each(output, &ec->output_list, link) {
-		listener = wl_signal_get(&output->frame_signal,
-					 weston_recorder_frame_notify);
-		if (listener)
-			break;
-	}
+	struct wl_listener *listener;
 
+	listener = wl_signal_get(&output->frame_signal,
+				 weston_recorder_frame_notify);
 	if (listener) {
-		recorder = container_of(listener, struct weston_recorder,
-					frame_listener);
-
-		weston_log(
-			"stopping recorder, total file size %dM, %d frames\n",
-			recorder->total / (1024 * 1024), recorder->count);
-
-		recorder->destroying = 1;
-		weston_output_schedule_repaint(recorder->output);
-	} else {
-		if (keyboard->focus && keyboard->focus->output)
-			output = keyboard->focus->output;
-		else
-			output = container_of(ec->output_list.next,
-					      struct weston_output, link);
-
-		weston_log("starting recorder for output %s, file %s\n",
-			   output->name, filename);
-		weston_recorder_create(output, filename);
+		weston_log("a recorder on output %s is already running\n",
+			   output->name);
+		return NULL;
 	}
-}
 
-static void
-screenshooter_destroy(struct wl_listener *listener, void *data)
-{
-	struct screenshooter *shooter =
-		container_of(listener, struct screenshooter, destroy_listener);
-
-	wl_global_destroy(shooter->global);
-	free(shooter);
+	weston_log("starting recorder for output %s, file %s\n",
+		   output->name, filename);
+	return weston_recorder_create(output, filename);
 }
 
 WL_EXPORT void
-screenshooter_create(struct weston_compositor *ec)
+weston_recorder_stop(struct weston_recorder *recorder)
 {
-	struct screenshooter *shooter;
-
-	shooter = malloc(sizeof *shooter);
-	if (shooter == NULL)
-		return;
-
-	shooter->ec = ec;
-	shooter->client = NULL;
-
-	shooter->global = wl_global_create(ec->wl_display,
-					   &weston_screenshooter_interface, 1,
-					   shooter, bind_shooter);
-	weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
-					  screenshooter_binding, shooter);
-	weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
-					  recorder_binding, shooter);
+	weston_log("stopping recorder, total file size %dM, %d frames\n",
+		   recorder->total / (1024 * 1024), recorder->count);
 
-	shooter->destroy_listener.notify = screenshooter_destroy;
-	wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
+	recorder->destroying = 1;
+	weston_output_schedule_repaint(recorder->output);
 }
diff --git a/src/weston-screenshooter.c b/src/weston-screenshooter.c
new file mode 100644
index 0000000..234ee0b
--- /dev/null
+++ b/src/weston-screenshooter.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright © 2008-2011 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <linux/input.h>
+
+#include "compositor.h"
+#include "weston.h"
+#include "weston-screenshooter-server-protocol.h"
+#include "shared/helpers.h"
+
+struct screenshooter {
+	struct weston_compositor *ec;
+	struct wl_global *global;
+	struct wl_client *client;
+	struct weston_process process;
+	struct wl_listener destroy_listener;
+	struct weston_recorder *recorder;
+};
+
+static void
+screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
+{
+	struct wl_resource *resource = data;
+
+	switch (outcome) {
+	case WESTON_SCREENSHOOTER_SUCCESS:
+		weston_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 weston_screenshooter_interface screenshooter_implementation = {
+	screenshooter_shoot
+};
+
+static void
+bind_shooter(struct wl_client *client,
+	     void *data, uint32_t version, uint32_t id)
+{
+	struct screenshooter *shooter = data;
+	struct wl_resource *resource;
+
+	resource = wl_resource_create(client,
+				      &weston_screenshooter_interface, 1, id);
+
+	if (client != shooter->client) {
+		wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
+				       "screenshooter failed: permission denied");
+		return;
+	}
+
+	wl_resource_set_implementation(resource, &screenshooter_implementation,
+				       data, NULL);
+}
+
+static void
+screenshooter_sigchld(struct weston_process *process, int status)
+{
+	struct screenshooter *shooter =
+		container_of(process, struct screenshooter, process);
+
+	shooter->client = NULL;
+}
+
+static void
+screenshooter_binding(struct weston_keyboard *keyboard, uint32_t time,
+		      uint32_t key, void *data)
+{
+	struct screenshooter *shooter = data;
+	char *screenshooter_exe;
+	int ret;
+
+	ret = asprintf(&screenshooter_exe, "%s/%s",
+		       weston_config_get_libexec_dir(),
+		       "/weston-screenshooter");
+	if (ret < 0) {
+		weston_log("Could not construct screenshooter path.\n");
+		return;
+	}
+
+	if (!shooter->client)
+		shooter->client = weston_client_launch(shooter->ec,
+					&shooter->process,
+					screenshooter_exe, screenshooter_sigchld);
+	free(screenshooter_exe);
+}
+
+static void
+recorder_binding(struct weston_keyboard *keyboard, uint32_t time,
+		 uint32_t key, void *data)
+{
+	struct weston_compositor *ec = keyboard->seat->compositor;
+	struct weston_output *output;
+	struct screenshooter *shooter = data;
+	struct weston_recorder *recorder = shooter->recorder;;
+	static const char filename[] = "capture.wcap";
+
+	if (recorder) {
+		weston_recorder_stop(recorder);
+		shooter->recorder = NULL;
+	} else {
+		if (keyboard->focus && keyboard->focus->output)
+			output = keyboard->focus->output;
+		else
+			output = container_of(ec->output_list.next,
+					      struct weston_output, link);
+
+		shooter->recorder = weston_recorder_start(output, filename);
+	}
+}
+
+static void
+screenshooter_destroy(struct wl_listener *listener, void *data)
+{
+	struct screenshooter *shooter =
+		container_of(listener, struct screenshooter, destroy_listener);
+
+	wl_global_destroy(shooter->global);
+	free(shooter);
+}
+
+WL_EXPORT void
+screenshooter_create(struct weston_compositor *ec)
+{
+	struct screenshooter *shooter;
+
+	shooter = zalloc(sizeof *shooter);
+	if (shooter == NULL)
+		return;
+
+	shooter->ec = ec;
+
+	shooter->global = wl_global_create(ec->wl_display,
+					   &weston_screenshooter_interface, 1,
+					   shooter, bind_shooter);
+	weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
+					  screenshooter_binding, shooter);
+	weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
+					  recorder_binding, shooter);
+
+	shooter->destroy_listener.notify = screenshooter_destroy;
+	wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
+}
diff --git a/src/weston.h b/src/weston.h
new file mode 100644
index 0000000..88cece9
--- /dev/null
+++ b/src/weston.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2016 Giulio Camuffo
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_H
+#define WESTON_H
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+#include "compositor.h"
+
+void
+screenshooter_create(struct weston_compositor *ec);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif
-- 
2.8.3



More information about the wayland-devel mailing list