[weston,v2,09/17] libweston: handle the screenshoot and record bindings in weston.c

Submitted by Giulio Camuffo on Dec. 4, 2014, 9:01 p.m.

Details

Message ID 1417726883-8305-10-git-send-email-giuliocamuffo@gmail.com
State Superseded
Headers show

Not browsing as part of any series.

Commit Message

Giulio Camuffo Dec. 4, 2014, 9:01 p.m.
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.
---
 desktop-shell/shell.c |   2 -
 src/compositor.h      |   5 +-
 src/screenshooter.c   | 176 ++++++++------------------------------------------
 src/weston.c          | 161 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 191 insertions(+), 153 deletions(-)

Patch hide | download patch | download mbox

diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index fcfa904..b84c69f 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -6595,8 +6595,6 @@  module_init(struct weston_compositor *ec,
 	shell->seat_create_listener.notify = handle_seat_created;
 	wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
 
-	screenshooter_create(ec);
-
 	shell_add_bindings(ec, shell);
 
 	shell_fade_init(shell);
diff --git a/src/compositor.h b/src/compositor.h
index 6a091cc..303e4a4 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1357,7 +1357,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/screenshooter.c b/src/screenshooter.c
index 6246cda..90cf4c5 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -31,18 +31,9 @@ 
 #include <sys/uio.h>
 
 #include "compositor.h"
-#include "screenshooter-server-protocol.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;
@@ -212,98 +203,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;
@@ -546,17 +445,13 @@  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_seat *ws = (struct weston_seat *) seat;
-	struct weston_compositor *ec = ws->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, &seat->compositor->output_list, link) {
+	wl_list_for_each(output, &ec->output_list, link) {
 		listener = wl_signal_get(&output->frame_signal,
 					 weston_recorder_frame_notify);
 		if (listener)
@@ -564,59 +459,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.c b/src/weston.c
index 10acf35..d10efe6 100644
--- a/src/weston.c
+++ b/src/weston.c
@@ -34,6 +34,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,6 +45,15 @@ 
 #include "../shared/os-compatibility.h"
 #include "git-version.h"
 #include "version.h"
+#include "screenshooter-server-protocol.h"
+
+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;
@@ -151,6 +161,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);
+}
+
+static 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
 
@@ -991,6 +1150,8 @@  int main(int argc, char *argv[])
 		}
 	}
 
+	screenshooter_create(ec);
+
 	weston_compositor_wake(ec);
 
 	wl_display_run(display);