[PATCH weston v3 09/11] libweston: handle the screenshoot and record bindings in weston.c

Giulio Camuffo giuliocamuffo at gmail.com
Sat Jun 27 04:07:49 PDT 2015


Move the code launching the screenshooter client and implementing
the screenshot protocol out of libweston, and make screenshooter.c
a generic way to hook screenshoot protocols into weston.
A new header weston.h is added, with an entry point for the shells
to init the screenshooter.
---
 Makefile.am           |   9 ++-
 desktop-shell/shell.c |   1 +
 src/compositor.h      |   5 +-
 src/main.c            | 160 ++++++++++++++++++++++++++++++++++++++++++++++
 src/screenshooter.c   | 173 ++++++++------------------------------------------
 src/weston.h          |  36 +++++++++++
 6 files changed, 233 insertions(+), 151 deletions(-)
 create mode 100644 src/weston.h

diff --git a/Makefile.am b/Makefile.am
index a79a15d..6cb24a0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -96,8 +96,6 @@ libweston_ at ABI_VERSION@_la_SOURCES =			\
 	src/weston-egl-ext.h
 
 nodist_libweston_ at ABI_VERSION@_la_SOURCES =		\
-	protocol/screenshooter-protocol.c		\
-	protocol/screenshooter-server-protocol.h	\
 	protocol/text-cursor-position-protocol.c	\
 	protocol/text-cursor-position-server-protocol.h	\
 	protocol/text-protocol.c			\
@@ -124,6 +122,7 @@ weston_LDADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) \
 
 weston_SOURCES =					\
 	src/main.c					\
+	src/weston.h					\
 	src/compositor-x11.h				\
 	src/compositor-wayland.h			\
 	src/compositor-drm.h				\
@@ -133,6 +132,12 @@ weston_SOURCES =					\
 	src/compositor-rpi.h				\
 	shared/helpers.h
 
+nodist_weston_SOURCES =					\
+	protocol/screenshooter-protocol.c		\
+	protocol/screenshooter-server-protocol.h
+
+BUILT_SOURCES += $(nodist_weston_SOURCES)
+
 # Track this dependency explicitly instead of using BUILT_SOURCES.  We
 # add BUILT_SOURCES to CLEANFILES, but we want to keep git-version.h
 # in case we're building from tarballs.
diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index 165d8a5..3375295 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 "desktop-shell-server-protocol.h"
 #include "workspaces-server-protocol.h"
 #include "shared/config-parser.h"
diff --git a/src/compositor.h b/src/compositor.h
index 828e40b..e322416 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1466,7 +1466,10 @@ int
 tty_activate_vt(struct tty *tty, int vt);
 
 void
-screenshooter_create(struct weston_compositor *ec);
+weston_recorder_start(struct weston_compositor *ec,
+		      struct weston_output *output);
+void
+weston_recorder_stop(struct weston_compositor *ec);
 
 enum weston_screenshooter_outcome {
 	WESTON_SCREENSHOOTER_SUCCESS,
diff --git a/src/main.c b/src/main.c
index ad20fea..870d9f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -37,6 +37,7 @@
 #include <sys/wait.h>
 #include <sys/time.h>
 #include <sys/socket.h>
+#include <linux/input.h>
 
 #ifdef HAVE_LIBUNWIND
 #define UNW_LOCAL_ONLY
@@ -44,10 +45,12 @@
 #endif
 
 #include "compositor.h"
+#include "weston.h"
 #include "../shared/os-compatibility.h"
 #include "../shared/helpers.h"
 #include "git-version.h"
 #include "version.h"
+#include "screenshooter-server-protocol.h"
 
 #include "compositor-wayland.h"
 #include "compositor-x11.h"
@@ -59,6 +62,14 @@
 
 #define WINDOW_TITLE "Weston Compositor"
 
+struct screenshooter {
+	struct weston_compositor *ec;
+	struct wl_global *global;
+	struct wl_client *client;
+	struct weston_process process;
+	struct wl_listener destroy_listener;
+};
+
 static struct wl_list child_process_list;
 static struct weston_compositor *segv_compositor;
 
@@ -165,6 +176,155 @@ vlog_continue(const char *fmt, va_list argp)
 	return vfprintf(weston_logfile, fmt, argp);
 }
 
+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 = {
+	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,
+				      &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_seat *seat, 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_seat *seat, uint32_t time, uint32_t key, void *data)
+{
+	struct weston_compositor *ec = seat->compositor;
+	struct weston_output *output;
+	int *running = data;
+
+	if (*running == 1) {
+		weston_recorder_stop(ec);
+		*running = 0;
+	} else {
+		if (seat->keyboard && seat->keyboard->focus &&
+		    seat->keyboard->focus->output)
+			output = seat->keyboard->focus->output;
+		else
+			output = container_of(ec->output_list.next,
+					      struct weston_output, link);
+
+		weston_recorder_start(ec, output);
+		*running = 1;
+	}
+}
+
+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)
+{
+	static int recorder_running = 0;
+	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,
+					   &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, &recorder_running);
+
+	shooter->destroy_listener.notify = screenshooter_destroy;
+	wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
+}
 
 #ifdef HAVE_LIBUNWIND
 
diff --git a/src/screenshooter.c b/src/screenshooter.c
index b7b8dce..efe7c8f 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -34,19 +34,10 @@
 #include <sys/uio.h>
 
 #include "compositor.h"
-#include "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:
-		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 = {
-	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,
-				      &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_seat *seat, 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;
@@ -559,14 +458,11 @@ weston_recorder_destroy(struct weston_recorder *recorder)
 	weston_recorder_free(recorder);
 }
 
-static void
-recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
+static struct weston_recorder *
+get_recorder(struct weston_compositor *ec)
 {
-	struct weston_compositor *ec = 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,
@@ -576,59 +472,40 @@ recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *da
 	}
 
 	if (listener) {
-		recorder = container_of(listener, struct weston_recorder,
-					frame_listener);
+		return container_of(listener, struct weston_recorder,
+				    frame_listener);
+	}
+	return NULL;
+}
 
-		weston_log(
-			"stopping recorder, total file size %dM, %d frames\n",
-			recorder->total / (1024 * 1024), recorder->count);
+WL_EXPORT void
+weston_recorder_start(struct weston_compositor *ec,
+		      struct weston_output *output)
+{
+	struct weston_recorder *recorder = get_recorder(ec);
+	static const char filename[] = "capture.wcap";
 
-		recorder->destroying = 1;
-		weston_output_schedule_repaint(recorder->output);
+	if (recorder) {
+		// Already running, do nothing;
+		return;
 	} else {
-		if (seat->keyboard && seat->keyboard->focus &&
-		    seat->keyboard->focus->output)
-			output = seat->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);
 	}
 }
 
-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)
+weston_recorder_stop(struct weston_compositor *ec)
 {
-	struct screenshooter *shooter;
+	struct weston_recorder *recorder = get_recorder(ec);
 
-	shooter = malloc(sizeof *shooter);
-	if (shooter == NULL)
-		return;
-
-	shooter->ec = ec;
-	shooter->client = NULL;
-
-	shooter->global = wl_global_create(ec->wl_display,
-					   &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);
+	if (recorder) {
+		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.h b/src/weston.h
new file mode 100644
index 0000000..52929cd
--- /dev/null
+++ b/src/weston.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2010-2011 Intel Corporation
+ * Copyright © 2008-2011 Kristian Høgsberg
+ * Copyright © 2012-2015 Collabora, Ltd.
+ *
+ * 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_WESTON_H
+#define WESTON_WESTON_H
+
+struct weston_compositor;
+
+void
+screenshooter_create(struct weston_compositor *ec);
+
+#endif
-- 
2.4.4



More information about the wayland-devel mailing list