2016-06-02 21:48:09 +03:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2016-07-19 14:16:27 +03:00
|
|
|
#include <stdint.h>
|
2016-06-02 21:48:09 +03:00
|
|
|
#include <linux/input.h>
|
|
|
|
|
2019-03-28 17:28:47 +03:00
|
|
|
#include <libweston/libweston.h>
|
2016-06-02 21:48:09 +03:00
|
|
|
#include "weston.h"
|
|
|
|
#include "weston-screenshooter-server-protocol.h"
|
|
|
|
#include "shared/helpers.h"
|
2019-06-21 22:49:18 +03:00
|
|
|
#include <libweston/weston-log.h>
|
2016-06-02 21:48:09 +03:00
|
|
|
|
|
|
|
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
|
2020-10-17 19:16:37 +03:00
|
|
|
screenshooter_take_shot(struct wl_client *client,
|
|
|
|
struct wl_resource *resource,
|
|
|
|
struct wl_resource *output_resource,
|
|
|
|
struct wl_resource *buffer_resource)
|
2016-06-02 21:48:09 +03:00
|
|
|
{
|
|
|
|
struct weston_output *output =
|
2017-03-27 16:31:25 +03:00
|
|
|
weston_head_from_resource(output_resource)->output;
|
2022-01-14 04:02:21 +03:00
|
|
|
struct weston_compositor *ec = output->compositor;
|
2016-06-02 21:48:09 +03:00
|
|
|
struct weston_buffer *buffer =
|
2022-01-14 04:02:21 +03:00
|
|
|
weston_buffer_from_resource(ec, buffer_resource);
|
2016-06-02 21:48:09 +03:00
|
|
|
|
|
|
|
if (buffer == NULL) {
|
|
|
|
wl_resource_post_no_memory(resource);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct weston_screenshooter_interface screenshooter_implementation = {
|
2020-10-17 19:16:37 +03:00
|
|
|
screenshooter_take_shot
|
2016-06-02 21:48:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
bind_shooter(struct wl_client *client,
|
|
|
|
void *data, uint32_t version, uint32_t id)
|
|
|
|
{
|
|
|
|
struct screenshooter *shooter = data;
|
|
|
|
struct wl_resource *resource;
|
2018-12-14 00:04:52 +03:00
|
|
|
bool debug_enabled =
|
|
|
|
weston_compositor_is_debug_protocol_enabled(shooter->ec);
|
2016-06-02 21:48:09 +03:00
|
|
|
|
|
|
|
resource = wl_resource_create(client,
|
|
|
|
&weston_screenshooter_interface, 1, id);
|
|
|
|
|
2018-12-14 00:04:52 +03:00
|
|
|
if (!debug_enabled && !shooter->client) {
|
2016-06-02 21:48:09 +03:00
|
|
|
wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
|
2018-12-14 00:04:52 +03:00
|
|
|
"screenshooter failed: permission denied. "\
|
|
|
|
"Debug protocol must be enabled");
|
|
|
|
return;
|
|
|
|
} else if (!debug_enabled && client != shooter->client) {
|
|
|
|
wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
|
|
|
|
"screenshooter failed: permission denied.");
|
2016-06-02 21:48:09 +03:00
|
|
|
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
|
2017-11-16 19:20:57 +03:00
|
|
|
screenshooter_binding(struct weston_keyboard *keyboard,
|
|
|
|
const struct timespec *time, uint32_t key, void *data)
|
2016-06-02 21:48:09 +03:00
|
|
|
{
|
|
|
|
struct screenshooter *shooter = data;
|
|
|
|
char *screenshooter_exe;
|
|
|
|
|
2016-11-24 23:45:45 +03:00
|
|
|
|
2018-12-15 16:57:19 +03:00
|
|
|
screenshooter_exe = wet_get_bindir_path("weston-screenshooter");
|
2016-11-24 23:45:45 +03:00
|
|
|
if (!screenshooter_exe) {
|
2016-06-02 21:48:09 +03:00
|
|
|
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
|
2017-11-16 19:20:57 +03:00
|
|
|
recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
|
2016-06-02 21:48:09 +03:00
|
|
|
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);
|
|
|
|
|
2018-08-07 16:35:02 +03:00
|
|
|
wl_list_remove(&shooter->destroy_listener.link);
|
|
|
|
|
2016-06-02 21:48:09 +03:00
|
|
|
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);
|
|
|
|
}
|