Skip to content

Commit

Permalink
Move part of screenshooter.c to weston-screenshooter.c
Browse files Browse the repository at this point in the history
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@gmail.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
  • Loading branch information
giucam authored and ppaalanen committed Jun 3, 2016
1 parent 0358af4 commit 26f62d4
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 170 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ weston_SOURCES = \
src/main.c \
src/linux-dmabuf.c \
src/linux-dmabuf.h \
src/weston-screenshooter.c \
shared/helpers.h \
shared/matrix.c \
shared/matrix.h \
Expand Down Expand Up @@ -221,6 +222,7 @@ westoninclude_HEADERS = \
src/compositor-wayland.h \
src/compositor-x11.h \
src/timeline-object.h \
src/weston.h \
shared/matrix.h \
shared/config-parser.h \
shared/zalloc.h \
Expand Down
1 change: 1 addition & 0 deletions desktop-shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions src/compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
187 changes: 20 additions & 167 deletions src/screenshooter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Loading

0 comments on commit 26f62d4

Please sign in to comment.