2017-09-07 15:31:30 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-07 10:22:48 +00:00
|
|
|
#include <string.h>
|
2017-09-07 15:31:30 +00:00
|
|
|
#include <wayland-server.h>
|
2018-01-23 21:06:54 +00:00
|
|
|
#include <wlr/backend.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wlr/render/wlr_renderer.h>
|
2017-09-07 15:31:30 +00:00
|
|
|
#include <wlr/types/wlr_output.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <wlr/types/wlr_screenshooter.h>
|
2017-09-07 15:31:30 +00:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include "screenshooter-protocol.h"
|
2018-08-26 21:23:12 +00:00
|
|
|
#include "util/signal.h"
|
2017-09-07 15:31:30 +00:00
|
|
|
|
2018-02-13 22:48:46 +00:00
|
|
|
static struct wlr_screenshot *screenshot_from_resource(
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
assert(wl_resource_instance_of(resource, &orbital_screenshot_interface,
|
|
|
|
NULL));
|
|
|
|
return wl_resource_get_user_data(resource);
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:09:09 +00:00
|
|
|
struct screenshot_state {
|
|
|
|
struct wl_shm_buffer *shm_buffer;
|
|
|
|
struct wlr_screenshot *screenshot;
|
|
|
|
struct wl_listener frame_listener;
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:13:06 +00:00
|
|
|
static void screenshot_destroy(struct wlr_screenshot *screenshot) {
|
|
|
|
wl_list_remove(&screenshot->link);
|
2017-12-06 23:36:05 +00:00
|
|
|
wl_resource_set_user_data(screenshot->resource, NULL);
|
2017-12-06 23:13:06 +00:00
|
|
|
free(screenshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_screenshot_resource_destroy(
|
|
|
|
struct wl_resource *screenshot_resource) {
|
|
|
|
struct wlr_screenshot *screenshot =
|
2018-02-13 22:48:46 +00:00
|
|
|
screenshot_from_resource(screenshot_resource);
|
2017-12-06 23:36:05 +00:00
|
|
|
if (screenshot != NULL) {
|
|
|
|
screenshot_destroy(screenshot);
|
|
|
|
}
|
2017-12-06 23:13:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
static void output_handle_frame(struct wl_listener *listener, void *_data) {
|
2017-10-07 10:22:48 +00:00
|
|
|
struct screenshot_state *state = wl_container_of(listener, state,
|
|
|
|
frame_listener);
|
2017-10-08 00:11:56 +00:00
|
|
|
struct wlr_output *output = state->screenshot->output;
|
2018-01-23 21:06:54 +00:00
|
|
|
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
|
|
|
|
struct wl_shm_buffer *shm_buffer = state->shm_buffer;
|
2017-10-07 10:22:48 +00:00
|
|
|
|
2019-04-23 16:44:47 +00:00
|
|
|
if (!(output->pending.committed & WLR_OUTPUT_STATE_BUFFER)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
enum wl_shm_format format = wl_shm_buffer_get_format(shm_buffer);
|
|
|
|
int32_t width = wl_shm_buffer_get_width(shm_buffer);
|
|
|
|
int32_t height = wl_shm_buffer_get_height(shm_buffer);
|
|
|
|
int32_t stride = wl_shm_buffer_get_stride(shm_buffer);
|
|
|
|
wl_shm_buffer_begin_access(shm_buffer);
|
2018-01-29 22:24:09 +00:00
|
|
|
void *data = wl_shm_buffer_get_data(shm_buffer);
|
2018-06-23 13:02:43 +00:00
|
|
|
bool ok = wlr_renderer_read_pixels(renderer, format, NULL, stride,
|
|
|
|
width, height, 0, 0, 0, 0, data);
|
2018-01-23 21:06:54 +00:00
|
|
|
wl_shm_buffer_end_access(shm_buffer);
|
|
|
|
|
|
|
|
if (!ok) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Cannot read pixels");
|
2018-01-23 21:06:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-10-07 10:22:48 +00:00
|
|
|
|
2017-09-08 16:09:09 +00:00
|
|
|
orbital_screenshot_send_done(state->screenshot->resource);
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
cleanup:
|
|
|
|
wl_list_remove(&listener->link);
|
2017-10-07 11:09:55 +00:00
|
|
|
free(state);
|
2017-09-08 16:09:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:48:46 +00:00
|
|
|
static const struct orbital_screenshooter_interface screenshooter_impl;
|
|
|
|
|
|
|
|
static struct wlr_screenshooter *screenshooter_from_resource(
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
assert(wl_resource_instance_of(resource, &orbital_screenshooter_interface,
|
|
|
|
&screenshooter_impl));
|
|
|
|
return wl_resource_get_user_data(resource);
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:31:30 +00:00
|
|
|
static void screenshooter_shoot(struct wl_client *client,
|
2017-11-02 22:14:24 +00:00
|
|
|
struct wl_resource *screenshooter_resource, uint32_t id,
|
|
|
|
struct wl_resource *output_resource,
|
|
|
|
struct wl_resource *buffer_resource) {
|
2017-10-08 00:11:56 +00:00
|
|
|
struct wlr_screenshooter *screenshooter =
|
2018-02-13 22:48:46 +00:00
|
|
|
screenshooter_from_resource(screenshooter_resource);
|
|
|
|
struct wlr_output *output = wlr_output_from_resource(output_resource);
|
2018-01-23 21:06:54 +00:00
|
|
|
|
|
|
|
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
|
|
|
|
if (renderer == NULL) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Backend doesn't have a renderer");
|
2017-09-08 16:09:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-23 21:06:54 +00:00
|
|
|
|
2017-11-02 22:14:24 +00:00
|
|
|
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(buffer_resource);
|
2018-01-23 21:06:54 +00:00
|
|
|
if (shm_buffer == NULL) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Invalid buffer: not a shared memory buffer");
|
2018-01-23 21:06:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:09:09 +00:00
|
|
|
int32_t width = wl_shm_buffer_get_width(shm_buffer);
|
|
|
|
int32_t height = wl_shm_buffer_get_height(shm_buffer);
|
|
|
|
if (width < output->width || height < output->height) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Invalid buffer: too small");
|
2017-09-08 16:09:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:25:27 +00:00
|
|
|
uint32_t format = wl_shm_buffer_get_format(shm_buffer);
|
2018-01-23 21:06:54 +00:00
|
|
|
if (!wlr_renderer_format_supported(renderer, format)) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Invalid buffer: unsupported format");
|
2017-10-07 16:25:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 11:09:55 +00:00
|
|
|
struct wlr_screenshot *screenshot =
|
|
|
|
calloc(1, sizeof(struct wlr_screenshot));
|
|
|
|
if (!screenshot) {
|
2017-11-02 22:14:24 +00:00
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
2017-09-07 15:31:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-02 22:14:24 +00:00
|
|
|
screenshot->output_resource = output_resource;
|
2017-10-08 00:11:56 +00:00
|
|
|
screenshot->output = output;
|
|
|
|
screenshot->screenshooter = screenshooter;
|
2017-09-07 15:31:30 +00:00
|
|
|
screenshot->resource = wl_resource_create(client,
|
2017-11-02 22:14:24 +00:00
|
|
|
&orbital_screenshot_interface,
|
|
|
|
wl_resource_get_version(screenshooter_resource), id);
|
|
|
|
if (screenshot->resource == NULL) {
|
|
|
|
free(screenshot);
|
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-07 10:22:48 +00:00
|
|
|
wl_resource_set_implementation(screenshot->resource, NULL, screenshot,
|
2017-12-06 23:13:06 +00:00
|
|
|
handle_screenshot_resource_destroy);
|
|
|
|
wl_list_insert(&screenshooter->screenshots, &screenshot->link);
|
2017-09-08 16:09:09 +00:00
|
|
|
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_DEBUG, "new screenshot %p (res %p)", screenshot,
|
2017-11-02 22:14:24 +00:00
|
|
|
screenshot->resource);
|
|
|
|
|
2017-09-08 16:09:09 +00:00
|
|
|
struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
|
2017-10-08 00:11:56 +00:00
|
|
|
if (!state) {
|
2017-11-02 22:14:24 +00:00
|
|
|
wl_resource_destroy(screenshot->resource);
|
|
|
|
free(screenshot);
|
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
2017-10-08 00:11:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 16:09:09 +00:00
|
|
|
state->shm_buffer = shm_buffer;
|
|
|
|
state->screenshot = screenshot;
|
2018-01-23 21:06:54 +00:00
|
|
|
state->frame_listener.notify = output_handle_frame;
|
2019-04-23 16:44:47 +00:00
|
|
|
wl_signal_add(&output->events.precommit, &state->frame_listener);
|
2018-01-26 21:41:19 +00:00
|
|
|
|
2019-04-23 16:26:21 +00:00
|
|
|
// Schedule a buffer commit
|
2019-04-23 17:22:42 +00:00
|
|
|
output->needs_frame = true;
|
2018-01-26 21:41:19 +00:00
|
|
|
wlr_output_schedule_frame(output);
|
2017-09-07 15:31:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:48:46 +00:00
|
|
|
static const struct orbital_screenshooter_interface screenshooter_impl = {
|
2017-09-07 15:31:30 +00:00
|
|
|
.shoot = screenshooter_shoot,
|
|
|
|
};
|
|
|
|
|
2017-11-02 22:14:24 +00:00
|
|
|
static void screenshooter_bind(struct wl_client *wl_client, void *data,
|
|
|
|
uint32_t version, uint32_t id) {
|
|
|
|
struct wlr_screenshooter *screenshooter = data;
|
2017-09-07 15:31:30 +00:00
|
|
|
assert(wl_client && screenshooter);
|
2017-10-22 09:18:27 +00:00
|
|
|
|
2017-09-07 15:31:30 +00:00
|
|
|
struct wl_resource *wl_resource = wl_resource_create(wl_client,
|
|
|
|
&orbital_screenshooter_interface, version, id);
|
2017-11-02 22:14:24 +00:00
|
|
|
if (wl_resource == NULL) {
|
|
|
|
wl_client_post_no_memory(wl_client);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-07 15:31:30 +00:00
|
|
|
wl_resource_set_implementation(wl_resource, &screenshooter_impl,
|
|
|
|
screenshooter, NULL);
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:13:06 +00:00
|
|
|
void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) {
|
|
|
|
if (!screenshooter) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-06 23:44:45 +00:00
|
|
|
wl_list_remove(&screenshooter->display_destroy.link);
|
2017-12-06 23:13:06 +00:00
|
|
|
struct wlr_screenshot *screenshot, *tmp;
|
|
|
|
wl_list_for_each_safe(screenshot, tmp, &screenshooter->screenshots, link) {
|
|
|
|
screenshot_destroy(screenshot);
|
|
|
|
}
|
2018-08-26 21:23:12 +00:00
|
|
|
wlr_signal_emit_safe(&screenshooter->events.destroy, screenshooter);
|
2018-07-08 18:21:31 +00:00
|
|
|
wl_global_destroy(screenshooter->global);
|
2017-12-06 23:13:06 +00:00
|
|
|
free(screenshooter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_screenshooter *screenshooter =
|
|
|
|
wl_container_of(listener, screenshooter, display_destroy);
|
|
|
|
wlr_screenshooter_destroy(screenshooter);
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) {
|
2017-09-07 15:31:30 +00:00
|
|
|
struct wlr_screenshooter *screenshooter =
|
|
|
|
calloc(1, sizeof(struct wlr_screenshooter));
|
|
|
|
if (!screenshooter) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-08 00:11:56 +00:00
|
|
|
|
2017-12-06 23:13:06 +00:00
|
|
|
wl_list_init(&screenshooter->screenshots);
|
2018-08-27 16:21:36 +00:00
|
|
|
wl_signal_init(&screenshooter->events.destroy);
|
2017-12-06 23:13:06 +00:00
|
|
|
|
|
|
|
screenshooter->display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(display, &screenshooter->display_destroy);
|
|
|
|
|
2018-07-08 18:21:31 +00:00
|
|
|
screenshooter->global = wl_global_create(display,
|
2017-09-07 15:31:30 +00:00
|
|
|
&orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
|
2018-07-08 18:21:31 +00:00
|
|
|
if (screenshooter->global == NULL) {
|
2017-09-07 15:31:30 +00:00
|
|
|
free(screenshooter);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-08 00:11:56 +00:00
|
|
|
|
2017-09-07 15:31:30 +00:00
|
|
|
return screenshooter;
|
|
|
|
}
|