2017-09-07 17:31:30 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-07 12:22:48 +02:00
|
|
|
#include <string.h>
|
2017-09-07 17:31:30 +02:00
|
|
|
#include <wayland-server.h>
|
2017-10-08 02:11:56 +02:00
|
|
|
#include <wlr/render.h>
|
2017-09-07 17:31:30 +02:00
|
|
|
#include <wlr/types/wlr_screenshooter.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include "screenshooter-protocol.h"
|
|
|
|
|
2017-10-07 18:25:27 +02:00
|
|
|
static void copy_yflip(uint8_t *dst, uint8_t *src, int32_t height,
|
2017-10-07 12:22:48 +02:00
|
|
|
int32_t stride) {
|
|
|
|
uint8_t *end = dst + height * stride;
|
|
|
|
while (dst < end) {
|
|
|
|
memcpy(dst, src, stride);
|
|
|
|
dst += stride;
|
|
|
|
src -= stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:09:09 +02:00
|
|
|
struct screenshot_state {
|
2017-10-07 12:22:48 +02:00
|
|
|
int32_t width, height, stride;
|
|
|
|
uint8_t *pixels;
|
2017-09-08 18:09:09 +02:00
|
|
|
struct wl_shm_buffer *shm_buffer;
|
|
|
|
struct wlr_screenshot *screenshot;
|
|
|
|
struct wl_listener frame_listener;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void output_frame_notify(struct wl_listener *listener, void *_data) {
|
2017-10-07 12:22:48 +02:00
|
|
|
struct screenshot_state *state = wl_container_of(listener, state,
|
|
|
|
frame_listener);
|
2017-10-08 02:11:56 +02:00
|
|
|
struct wlr_renderer *renderer = state->screenshot->screenshooter->renderer;
|
|
|
|
struct wlr_output *output = state->screenshot->output;
|
2017-10-07 12:22:48 +02:00
|
|
|
|
2017-10-08 02:11:56 +02:00
|
|
|
wlr_output_make_current(output);
|
|
|
|
wlr_renderer_read_pixels(renderer, 0, 0, output->width, output->height,
|
|
|
|
state->pixels);
|
2017-09-08 18:09:09 +02:00
|
|
|
|
|
|
|
void *data = wl_shm_buffer_get_data(state->shm_buffer);
|
|
|
|
wl_shm_buffer_begin_access(state->shm_buffer);
|
2017-10-07 18:25:27 +02:00
|
|
|
copy_yflip(data, state->pixels + state->stride * (state->height - 1),
|
2017-10-07 12:22:48 +02:00
|
|
|
state->height, state->stride);
|
2017-09-08 18:09:09 +02:00
|
|
|
wl_shm_buffer_end_access(state->shm_buffer);
|
|
|
|
|
2017-10-07 12:22:48 +02:00
|
|
|
free(state->pixels);
|
2017-09-08 18:09:09 +02:00
|
|
|
wl_list_remove(&listener->link);
|
2017-10-07 12:22:48 +02:00
|
|
|
|
2017-09-08 18:09:09 +02:00
|
|
|
orbital_screenshot_send_done(state->screenshot->resource);
|
|
|
|
|
2017-10-07 13:09:55 +02:00
|
|
|
free(state);
|
2017-09-08 18:09:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 17:31:30 +02:00
|
|
|
static void screenshooter_shoot(struct wl_client *client,
|
2017-11-02 23:14:24 +01:00
|
|
|
struct wl_resource *screenshooter_resource, uint32_t id,
|
|
|
|
struct wl_resource *output_resource,
|
|
|
|
struct wl_resource *buffer_resource) {
|
2017-10-08 02:11:56 +02:00
|
|
|
struct wlr_screenshooter *screenshooter =
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_get_user_data(screenshooter_resource);
|
|
|
|
struct wlr_output *output = wl_resource_get_user_data(output_resource);
|
|
|
|
if (!wl_shm_buffer_get(buffer_resource)) {
|
2017-09-08 18:09:09 +02:00
|
|
|
wlr_log(L_ERROR, "Invalid buffer: not a shared memory buffer");
|
|
|
|
return;
|
|
|
|
}
|
2017-11-02 23:14:24 +01:00
|
|
|
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(buffer_resource);
|
2017-09-08 18:09:09 +02:00
|
|
|
int32_t width = wl_shm_buffer_get_width(shm_buffer);
|
|
|
|
int32_t height = wl_shm_buffer_get_height(shm_buffer);
|
2017-10-07 12:22:48 +02:00
|
|
|
int32_t stride = wl_shm_buffer_get_stride(shm_buffer);
|
2017-09-08 18:09:09 +02:00
|
|
|
if (width < output->width || height < output->height) {
|
|
|
|
wlr_log(L_ERROR, "Invalid buffer: too small");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 18:25:27 +02:00
|
|
|
uint32_t format = wl_shm_buffer_get_format(shm_buffer);
|
|
|
|
if (format != WL_SHM_FORMAT_XRGB8888) {
|
|
|
|
wlr_log(L_ERROR, "Invalid buffer: unsupported format");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 12:22:48 +02:00
|
|
|
uint8_t *pixels = malloc(stride * height);
|
|
|
|
if (pixels == NULL) {
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 13:09:55 +02:00
|
|
|
struct wlr_screenshot *screenshot =
|
|
|
|
calloc(1, sizeof(struct wlr_screenshot));
|
|
|
|
if (!screenshot) {
|
2017-11-16 10:30:54 +01:00
|
|
|
free(pixels);
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
2017-09-07 17:31:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-11-02 23:14:24 +01:00
|
|
|
screenshot->output_resource = output_resource;
|
2017-10-08 02:11:56 +02:00
|
|
|
screenshot->output = output;
|
|
|
|
screenshot->screenshooter = screenshooter;
|
2017-09-07 17:31:30 +02:00
|
|
|
screenshot->resource = wl_resource_create(client,
|
2017-11-02 23:14:24 +01:00
|
|
|
&orbital_screenshot_interface,
|
|
|
|
wl_resource_get_version(screenshooter_resource), id);
|
|
|
|
if (screenshot->resource == NULL) {
|
|
|
|
free(screenshot);
|
2017-11-16 10:30:54 +01:00
|
|
|
free(pixels);
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-07 12:22:48 +02:00
|
|
|
wl_resource_set_implementation(screenshot->resource, NULL, screenshot,
|
|
|
|
NULL);
|
2017-09-08 18:09:09 +02:00
|
|
|
|
2017-11-02 23:14:24 +01:00
|
|
|
wlr_log(L_DEBUG, "new screenshot %p (res %p)", screenshot,
|
|
|
|
screenshot->resource);
|
|
|
|
|
2017-09-08 18:09:09 +02:00
|
|
|
struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
|
2017-10-08 02:11:56 +02:00
|
|
|
if (!state) {
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_destroy(screenshot->resource);
|
|
|
|
free(screenshot);
|
2017-11-16 10:30:54 +01:00
|
|
|
free(pixels);
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_post_no_memory(screenshooter_resource);
|
2017-10-08 02:11:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-07 12:22:48 +02:00
|
|
|
state->width = width;
|
|
|
|
state->height = height;
|
|
|
|
state->stride = stride;
|
|
|
|
state->pixels = pixels;
|
2017-09-08 18:09:09 +02:00
|
|
|
state->shm_buffer = shm_buffer;
|
|
|
|
state->screenshot = screenshot;
|
|
|
|
state->frame_listener.notify = output_frame_notify;
|
2017-10-07 12:01:21 +02:00
|
|
|
wl_signal_add(&output->events.swap_buffers, &state->frame_listener);
|
2017-09-07 17:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct orbital_screenshooter_interface screenshooter_impl = {
|
|
|
|
.shoot = screenshooter_shoot,
|
|
|
|
};
|
|
|
|
|
2017-11-02 23:14:24 +01: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 17:31:30 +02:00
|
|
|
assert(wl_client && screenshooter);
|
2017-10-22 11:18:27 +02:00
|
|
|
|
2017-09-07 17:31:30 +02:00
|
|
|
struct wl_resource *wl_resource = wl_resource_create(wl_client,
|
|
|
|
&orbital_screenshooter_interface, version, id);
|
2017-11-02 23:14:24 +01:00
|
|
|
if (wl_resource == NULL) {
|
|
|
|
wl_client_post_no_memory(wl_client);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-07 17:31:30 +02:00
|
|
|
wl_resource_set_implementation(wl_resource, &screenshooter_impl,
|
|
|
|
screenshooter, NULL);
|
|
|
|
}
|
|
|
|
|
2017-10-08 02:11:56 +02:00
|
|
|
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
|
|
|
|
struct wlr_renderer *renderer) {
|
2017-09-07 17:31:30 +02:00
|
|
|
struct wlr_screenshooter *screenshooter =
|
|
|
|
calloc(1, sizeof(struct wlr_screenshooter));
|
|
|
|
if (!screenshooter) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-08 02:11:56 +02:00
|
|
|
screenshooter->renderer = renderer;
|
|
|
|
|
2017-11-02 23:14:24 +01:00
|
|
|
screenshooter->wl_global = wl_global_create(display,
|
2017-09-07 17:31:30 +02:00
|
|
|
&orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
|
2017-11-02 23:14:24 +01:00
|
|
|
if (screenshooter->wl_global == NULL) {
|
2017-09-07 17:31:30 +02:00
|
|
|
free(screenshooter);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-08 02:11:56 +02:00
|
|
|
|
2017-09-07 17:31:30 +02:00
|
|
|
return screenshooter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) {
|
|
|
|
if (!screenshooter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: this segfault (wl_display->registry_resource_list is not init)
|
|
|
|
// wl_global_destroy(screenshooter->wl_global);
|
|
|
|
free(screenshooter);
|
|
|
|
}
|