mirror of
https://github.com/hyprwm/wlroots-hyprland.git
synced 2024-11-26 14:45:58 +01:00
commit
9e1ff2dce9
15 changed files with 553 additions and 14 deletions
|
@ -22,3 +22,9 @@ executable(
|
||||||
dependencies: wlroots,
|
dependencies: wlroots,
|
||||||
link_with: lib_shared,
|
link_with: lib_shared,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
executable(
|
||||||
|
'screenshot',
|
||||||
|
'screenshot.c',
|
||||||
|
dependencies: [wayland_client, wlr_protos],
|
||||||
|
)
|
||||||
|
|
288
examples/screenshot.c
Normal file
288
examples/screenshot.c
Normal file
|
@ -0,0 +1,288 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2008 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define _XOPEN_SOURCE 700
|
||||||
|
#define _POSIX_C_SOURCE 199309L
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <screenshooter-client-protocol.h>
|
||||||
|
#include "../backend/wayland/os-compatibility.c"
|
||||||
|
|
||||||
|
static struct wl_shm *shm = NULL;
|
||||||
|
static struct orbital_screenshooter *screenshooter = NULL;
|
||||||
|
static struct wl_list output_list;
|
||||||
|
int min_x, min_y, max_x, max_y;
|
||||||
|
int buffer_copy_done;
|
||||||
|
|
||||||
|
struct screenshooter_output {
|
||||||
|
struct wl_output *output;
|
||||||
|
struct wl_buffer *buffer;
|
||||||
|
int width, height, offset_x, offset_y;
|
||||||
|
void *data;
|
||||||
|
struct wl_list link;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void output_handle_geometry(void *data, struct wl_output *wl_output,
|
||||||
|
int x, int y, int physical_width, int physical_height, int subpixel,
|
||||||
|
const char *make, const char *model, int transform) {
|
||||||
|
struct screenshooter_output *output = wl_output_get_user_data(wl_output);
|
||||||
|
|
||||||
|
if (wl_output == output->output) {
|
||||||
|
output->offset_x = x;
|
||||||
|
output->offset_y = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void output_handle_mode(void *data, struct wl_output *wl_output,
|
||||||
|
uint32_t flags, int width, int height, int refresh) {
|
||||||
|
struct screenshooter_output *output = wl_output_get_user_data(wl_output);
|
||||||
|
|
||||||
|
if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
|
||||||
|
output->width = width;
|
||||||
|
output->height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void output_handle_done(void *data, struct wl_output *wl_output) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_output_listener output_listener = {
|
||||||
|
.geometry = output_handle_geometry,
|
||||||
|
.mode = output_handle_mode,
|
||||||
|
.done = output_handle_done,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void screenshot_done(void *data, struct orbital_screenshot *screenshot) {
|
||||||
|
buffer_copy_done = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct orbital_screenshot_listener screenshot_listener = {
|
||||||
|
.done = screenshot_done,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void handle_global(void *data, struct wl_registry *registry,
|
||||||
|
uint32_t name, const char *interface, uint32_t version) {
|
||||||
|
static struct screenshooter_output *output;
|
||||||
|
|
||||||
|
if (strcmp(interface, "wl_output") == 0) {
|
||||||
|
output = calloc(1, sizeof *output);
|
||||||
|
output->output = wl_registry_bind(registry, name, &wl_output_interface,
|
||||||
|
1);
|
||||||
|
wl_list_insert(&output_list, &output->link);
|
||||||
|
wl_output_add_listener(output->output, &output_listener, output);
|
||||||
|
} else if (strcmp(interface, "wl_shm") == 0) {
|
||||||
|
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
||||||
|
} else if (strcmp(interface, "orbital_screenshooter") == 0) {
|
||||||
|
screenshooter = wl_registry_bind(registry, name,
|
||||||
|
&orbital_screenshooter_interface, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_global_remove(void *data, struct wl_registry *registry,
|
||||||
|
uint32_t name) {
|
||||||
|
// Unimplemented
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_registry_listener registry_listener = {
|
||||||
|
.global = handle_global,
|
||||||
|
.global_remove = handle_global_remove,
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct wl_buffer *create_shm_buffer(int width, int height,
|
||||||
|
void **data_out) {
|
||||||
|
int stride = width * 4;
|
||||||
|
int size = stride * height;
|
||||||
|
|
||||||
|
int fd = os_create_anonymous_file(size);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (data == MAP_FAILED) {
|
||||||
|
fprintf(stderr, "mmap failed: %m\n");
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
|
||||||
|
close(fd);
|
||||||
|
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height,
|
||||||
|
stride, WL_SHM_FORMAT_XRGB8888);
|
||||||
|
wl_shm_pool_destroy(pool);
|
||||||
|
|
||||||
|
*data_out = data;
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void argb_to_rgba(uint32_t *data, size_t height, size_t stride) {
|
||||||
|
size_t n = height*stride/4;
|
||||||
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
uint32_t v = data[i];
|
||||||
|
uint32_t rgb = v & 0x00ffffff;
|
||||||
|
uint32_t a = (v & 0xff000000) >> 24;
|
||||||
|
data[i] = (rgb << 8) | a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_image(const char *filename, int width, int height) {
|
||||||
|
int buffer_stride = width * 4;
|
||||||
|
|
||||||
|
void *data = calloc(1, buffer_stride * height);
|
||||||
|
if (!data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct screenshooter_output *output, *next;
|
||||||
|
wl_list_for_each_safe(output, next, &output_list, link) {
|
||||||
|
int output_stride = output->width * 4;
|
||||||
|
void *s = output->data;
|
||||||
|
void *d = data + (output->offset_y - min_y) * buffer_stride +
|
||||||
|
(output->offset_x - min_x) * 4;
|
||||||
|
|
||||||
|
for (int i = 0; i < output->height; i++) {
|
||||||
|
memcpy(d, s, output_stride);
|
||||||
|
d += buffer_stride;
|
||||||
|
s += output_stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
argb_to_rgba(data, height, buffer_stride);
|
||||||
|
|
||||||
|
char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
|
||||||
|
sprintf(size, "%dx%d+0", width, height);
|
||||||
|
|
||||||
|
int fd[2];
|
||||||
|
pipe(fd);
|
||||||
|
|
||||||
|
pid_t child = fork();
|
||||||
|
if (child < 0) {
|
||||||
|
fprintf(stderr, "fork() failed\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else if (child != 0) {
|
||||||
|
close(fd[0]);
|
||||||
|
write(fd[1], data, buffer_stride * height);
|
||||||
|
close(fd[1]);
|
||||||
|
free(data);
|
||||||
|
waitpid(child, NULL, 0);
|
||||||
|
} else {
|
||||||
|
close(fd[1]);
|
||||||
|
if (dup2(fd[0], 0) != 0) {
|
||||||
|
fprintf(stderr, "cannot dup the pipe\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
close(fd[0]);
|
||||||
|
execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-",
|
||||||
|
"-alpha", "opaque", filename, NULL);
|
||||||
|
fprintf(stderr, "cannot execute convert\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int set_buffer_size(int *width, int *height) {
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
min_x = min_y = INT_MAX;
|
||||||
|
max_x = max_y = INT_MIN;
|
||||||
|
int position = 0;
|
||||||
|
|
||||||
|
wl_list_for_each_reverse(output, &output_list, link) {
|
||||||
|
output->offset_x = position;
|
||||||
|
position += output->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_for_each(output, &output_list, link) {
|
||||||
|
min_x = MIN(min_x, output->offset_x);
|
||||||
|
min_y = MIN(min_y, output->offset_y);
|
||||||
|
max_x = MAX(max_x, output->offset_x + output->width);
|
||||||
|
max_y = MAX(max_y, output->offset_y + output->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_x <= min_x || max_y <= min_y) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*width = max_x - min_x;
|
||||||
|
*height = max_y - min_y;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
struct wl_display * display = wl_display_connect(NULL);
|
||||||
|
if (display == NULL) {
|
||||||
|
fprintf(stderr, "failed to create display: %m\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_init(&output_list);
|
||||||
|
struct wl_registry *registry = wl_display_get_registry(display);
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
||||||
|
wl_display_dispatch(display);
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
|
if (screenshooter == NULL) {
|
||||||
|
fprintf(stderr, "display doesn't support screenshooter\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (set_buffer_size(&width, &height)) {
|
||||||
|
fprintf(stderr, "cannot set buffer size\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
wl_list_for_each(output, &output_list, link) {
|
||||||
|
if (output->width == 0 || output->height == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
output->buffer = create_shm_buffer(output->width, output->height, &output->data);
|
||||||
|
if (output->buffer == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
struct orbital_screenshot *screenshot = orbital_screenshooter_shoot(
|
||||||
|
screenshooter, output->output, output->buffer);
|
||||||
|
orbital_screenshot_add_listener(screenshot, &screenshot_listener, screenshot);
|
||||||
|
buffer_copy_done = 0;
|
||||||
|
while (!buffer_copy_done)
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
}
|
||||||
|
|
||||||
|
write_image("wayland-screenshot.png", width, height);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -8,6 +8,7 @@
|
||||||
#include <wlr/types/wlr_wl_shell.h>
|
#include <wlr/types/wlr_wl_shell.h>
|
||||||
#include <wlr/types/wlr_xdg_shell_v6.h>
|
#include <wlr/types/wlr_xdg_shell_v6.h>
|
||||||
#include <wlr/types/wlr_gamma_control.h>
|
#include <wlr/types/wlr_gamma_control.h>
|
||||||
|
#include <wlr/types/wlr_screenshooter.h>
|
||||||
#include <wlr/util/list.h>
|
#include <wlr/util/list.h>
|
||||||
#include "rootston/view.h"
|
#include "rootston/view.h"
|
||||||
#include "rootston/config.h"
|
#include "rootston/config.h"
|
||||||
|
@ -36,6 +37,7 @@ struct roots_desktop {
|
||||||
struct wlr_xdg_shell_v6 *xdg_shell_v6;
|
struct wlr_xdg_shell_v6 *xdg_shell_v6;
|
||||||
struct wlr_xwayland *xwayland;
|
struct wlr_xwayland *xwayland;
|
||||||
struct wlr_gamma_control_manager *gamma_control_manager;
|
struct wlr_gamma_control_manager *gamma_control_manager;
|
||||||
|
struct wlr_screenshooter *screenshooter;
|
||||||
|
|
||||||
struct wl_listener output_add;
|
struct wl_listener output_add;
|
||||||
struct wl_listener output_remove;
|
struct wl_listener output_remove;
|
||||||
|
|
|
@ -51,6 +51,11 @@ const enum wl_shm_format *wlr_renderer_get_formats(
|
||||||
*/
|
*/
|
||||||
bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer,
|
bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer,
|
||||||
struct wl_resource *buffer);
|
struct wl_resource *buffer);
|
||||||
|
/**
|
||||||
|
* Reads pixels and stores them in out_data as ARGB8888.
|
||||||
|
*/
|
||||||
|
void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
|
||||||
|
int width, int height, void *out_data);
|
||||||
/**
|
/**
|
||||||
* Destroys this wlr_renderer. Textures must be destroyed separately.
|
* Destroys this wlr_renderer. Textures must be destroyed separately.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -28,6 +28,8 @@ struct wlr_renderer_impl {
|
||||||
struct wlr_renderer *renderer, size_t *len);
|
struct wlr_renderer *renderer, size_t *len);
|
||||||
bool (*buffer_is_drm)(struct wlr_renderer *renderer,
|
bool (*buffer_is_drm)(struct wlr_renderer *renderer,
|
||||||
struct wl_resource *buffer);
|
struct wl_resource *buffer);
|
||||||
|
void (*read_pixels)(struct wlr_renderer *renderer, int x, int y, int width,
|
||||||
|
int height, void *out_data);
|
||||||
void (*destroy)(struct wlr_renderer *renderer);
|
void (*destroy)(struct wlr_renderer *renderer);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ struct wlr_output {
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
struct wl_signal frame;
|
struct wl_signal frame;
|
||||||
|
struct wl_signal swap_buffers;
|
||||||
struct wl_signal resolution;
|
struct wl_signal resolution;
|
||||||
struct wl_signal destroy;
|
struct wl_signal destroy;
|
||||||
} events;
|
} events;
|
||||||
|
|
26
include/wlr/types/wlr_screenshooter.h
Normal file
26
include/wlr/types/wlr_screenshooter.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef _WLR_SCREENSHOOTER_H
|
||||||
|
#define _WLR_SCREENSHOOTER_H
|
||||||
|
#include <wayland-server.h>
|
||||||
|
|
||||||
|
struct wlr_screenshooter {
|
||||||
|
struct wl_global *wl_global;
|
||||||
|
struct wlr_renderer *renderer;
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_screenshot {
|
||||||
|
struct wl_resource *resource;
|
||||||
|
struct wl_resource *output_resource;
|
||||||
|
|
||||||
|
struct wlr_output *output;
|
||||||
|
struct wlr_screenshooter *screenshooter;
|
||||||
|
|
||||||
|
void* data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
|
||||||
|
struct wlr_renderer *renderer);
|
||||||
|
void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter);
|
||||||
|
|
||||||
|
#endif
|
|
@ -22,11 +22,14 @@ wayland_scanner_client = generator(
|
||||||
|
|
||||||
protocols = [
|
protocols = [
|
||||||
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
|
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
|
||||||
'gamma-control.xml'
|
'gamma-control.xml',
|
||||||
|
'screenshooter.xml',
|
||||||
]
|
]
|
||||||
|
|
||||||
client_protocols = [
|
client_protocols = [
|
||||||
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml']
|
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
|
||||||
|
'gamma-control.xml',
|
||||||
|
'screenshooter.xml',
|
||||||
]
|
]
|
||||||
|
|
||||||
wl_protos_src = []
|
wl_protos_src = []
|
||||||
|
|
16
protocol/screenshooter.xml
Normal file
16
protocol/screenshooter.xml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<protocol name="orbital_screenshooter">
|
||||||
|
|
||||||
|
<interface name="orbital_screenshooter" version="1">
|
||||||
|
<request name="shoot">
|
||||||
|
<arg name="id" type="new_id" interface="orbital_screenshot"/>
|
||||||
|
<arg name="output" type="object" interface="wl_output"/>
|
||||||
|
<arg name="buffer" type="object" interface="wl_buffer"/>
|
||||||
|
</request>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
<interface name="orbital_screenshot" version="1">
|
||||||
|
<event name="done">
|
||||||
|
</event>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
</protocol>
|
|
@ -211,6 +211,22 @@ static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer,
|
||||||
EGL_TEXTURE_FORMAT, &format);
|
EGL_TEXTURE_FORMAT, &format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void rgba_to_argb(uint32_t *data, size_t height, size_t stride) {
|
||||||
|
size_t n = height*stride/4;
|
||||||
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
uint32_t v = data[i];
|
||||||
|
uint32_t rgb = (v & 0xffffff00) >> 8;
|
||||||
|
uint32_t a = v & 0x000000ff;
|
||||||
|
data[i] = rgb | (a << 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wlr_gles2_read_pixels(struct wlr_renderer *renderer, int x, int y,
|
||||||
|
int width, int height, void *out_data) {
|
||||||
|
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
|
||||||
|
rgba_to_argb(out_data, height, width*4);
|
||||||
|
}
|
||||||
|
|
||||||
static struct wlr_renderer_impl wlr_renderer_impl = {
|
static struct wlr_renderer_impl wlr_renderer_impl = {
|
||||||
.begin = wlr_gles2_begin,
|
.begin = wlr_gles2_begin,
|
||||||
.end = wlr_gles2_end,
|
.end = wlr_gles2_end,
|
||||||
|
@ -220,6 +236,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
|
||||||
.render_ellipse = wlr_gles2_render_ellipse,
|
.render_ellipse = wlr_gles2_render_ellipse,
|
||||||
.formats = wlr_gles2_formats,
|
.formats = wlr_gles2_formats,
|
||||||
.buffer_is_drm = wlr_gles2_buffer_is_drm,
|
.buffer_is_drm = wlr_gles2_buffer_is_drm,
|
||||||
|
.read_pixels = wlr_gles2_read_pixels,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) {
|
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) {
|
||||||
|
|
|
@ -51,3 +51,8 @@ bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r,
|
||||||
struct wl_resource *buffer) {
|
struct wl_resource *buffer) {
|
||||||
return r->impl->buffer_is_drm(r, buffer);
|
return r->impl->buffer_is_drm(r, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
|
||||||
|
int width, int height, void *out_data) {
|
||||||
|
r->impl->read_pixels(r, x, y, width, height, out_data);
|
||||||
|
}
|
||||||
|
|
|
@ -208,16 +208,15 @@ struct roots_desktop *desktop_create(struct roots_server *server,
|
||||||
wl_list_init(&desktop->output_remove.link);
|
wl_list_init(&desktop->output_remove.link);
|
||||||
desktop->output_remove.notify = output_remove_notify;
|
desktop->output_remove.notify = output_remove_notify;
|
||||||
|
|
||||||
wl_signal_add(&server->backend->events.output_add,
|
wl_signal_add(&server->backend->events.output_add, &desktop->output_add);
|
||||||
&desktop->output_add);
|
|
||||||
wl_signal_add(&server->backend->events.output_remove,
|
wl_signal_add(&server->backend->events.output_remove,
|
||||||
&desktop->output_remove);
|
&desktop->output_remove);
|
||||||
|
|
||||||
desktop->server = server;
|
desktop->server = server;
|
||||||
desktop->config = config;
|
desktop->config = config;
|
||||||
desktop->layout = wlr_output_layout_create();
|
desktop->layout = wlr_output_layout_create();
|
||||||
desktop->compositor = wlr_compositor_create(
|
desktop->compositor = wlr_compositor_create(server->wl_display,
|
||||||
server->wl_display, server->renderer);
|
server->renderer);
|
||||||
|
|
||||||
desktop->xdg_shell_v6 = wlr_xdg_shell_v6_create(server->wl_display);
|
desktop->xdg_shell_v6 = wlr_xdg_shell_v6_create(server->wl_display);
|
||||||
wl_signal_add(&desktop->xdg_shell_v6->events.new_surface,
|
wl_signal_add(&desktop->xdg_shell_v6->events.new_surface,
|
||||||
|
@ -237,6 +236,8 @@ struct roots_desktop *desktop_create(struct roots_server *server,
|
||||||
|
|
||||||
desktop->gamma_control_manager = wlr_gamma_control_manager_create(
|
desktop->gamma_control_manager = wlr_gamma_control_manager_create(
|
||||||
server->wl_display);
|
server->wl_display);
|
||||||
|
desktop->screenshooter = wlr_screenshooter_create(server->wl_display,
|
||||||
|
server->renderer);
|
||||||
|
|
||||||
return desktop;
|
return desktop;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ lib_wlr_types = static_library(
|
||||||
'wlr_compositor.c',
|
'wlr_compositor.c',
|
||||||
'wlr_box.c',
|
'wlr_box.c',
|
||||||
'wlr_gamma_control.c',
|
'wlr_gamma_control.c',
|
||||||
|
'wlr_screenshooter.c',
|
||||||
),
|
),
|
||||||
include_directories: wlr_inc,
|
include_directories: wlr_inc,
|
||||||
dependencies: [wayland_server, pixman, wlr_protos],
|
dependencies: [wayland_server, pixman, wlr_protos],
|
||||||
|
|
|
@ -105,6 +105,7 @@ void wlr_output_init(struct wlr_output *output,
|
||||||
output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
|
output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
|
||||||
output->scale = 1;
|
output->scale = 1;
|
||||||
wl_signal_init(&output->events.frame);
|
wl_signal_init(&output->events.frame);
|
||||||
|
wl_signal_init(&output->events.swap_buffers);
|
||||||
wl_signal_init(&output->events.resolution);
|
wl_signal_init(&output->events.resolution);
|
||||||
wl_signal_init(&output->events.destroy);
|
wl_signal_init(&output->events.destroy);
|
||||||
}
|
}
|
||||||
|
@ -231,6 +232,8 @@ void wlr_output_swap_buffers(struct wlr_output *output) {
|
||||||
wlr_render_with_matrix(output->cursor.renderer, output->cursor.texture, &matrix);
|
wlr_render_with_matrix(output->cursor.renderer, output->cursor.texture, &matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wl_signal_emit(&output->events.swap_buffers, &output);
|
||||||
|
|
||||||
output->impl->swap_buffers(output);
|
output->impl->swap_buffers(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
163
types/wlr_screenshooter.c
Normal file
163
types/wlr_screenshooter.c
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wayland-server.h>
|
||||||
|
#include <wlr/render.h>
|
||||||
|
#include <wlr/types/wlr_screenshooter.h>
|
||||||
|
#include <wlr/types/wlr_output.h>
|
||||||
|
#include <wlr/util/log.h>
|
||||||
|
#include "screenshooter-protocol.h"
|
||||||
|
|
||||||
|
static void copy_yflip(uint8_t *dst, uint8_t *src, int32_t height,
|
||||||
|
int32_t stride) {
|
||||||
|
uint8_t *end = dst + height * stride;
|
||||||
|
while (dst < end) {
|
||||||
|
memcpy(dst, src, stride);
|
||||||
|
dst += stride;
|
||||||
|
src -= stride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct screenshot_state {
|
||||||
|
int32_t width, height, stride;
|
||||||
|
uint8_t *pixels;
|
||||||
|
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) {
|
||||||
|
struct screenshot_state *state = wl_container_of(listener, state,
|
||||||
|
frame_listener);
|
||||||
|
struct wlr_renderer *renderer = state->screenshot->screenshooter->renderer;
|
||||||
|
struct wlr_output *output = state->screenshot->output;
|
||||||
|
|
||||||
|
wlr_output_make_current(output);
|
||||||
|
wlr_renderer_read_pixels(renderer, 0, 0, output->width, output->height,
|
||||||
|
state->pixels);
|
||||||
|
|
||||||
|
void *data = wl_shm_buffer_get_data(state->shm_buffer);
|
||||||
|
wl_shm_buffer_begin_access(state->shm_buffer);
|
||||||
|
copy_yflip(data, state->pixels + state->stride * (state->height - 1),
|
||||||
|
state->height, state->stride);
|
||||||
|
wl_shm_buffer_end_access(state->shm_buffer);
|
||||||
|
|
||||||
|
free(state->pixels);
|
||||||
|
wl_list_remove(&listener->link);
|
||||||
|
|
||||||
|
orbital_screenshot_send_done(state->screenshot->resource);
|
||||||
|
|
||||||
|
free(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void screenshooter_shoot(struct wl_client *client,
|
||||||
|
struct wl_resource *_screenshooter, uint32_t id,
|
||||||
|
struct wl_resource *_output, struct wl_resource *_buffer) {
|
||||||
|
struct wlr_screenshooter *screenshooter =
|
||||||
|
wl_resource_get_user_data(_screenshooter);
|
||||||
|
struct wlr_output *output = wl_resource_get_user_data(_output);
|
||||||
|
if (!wl_shm_buffer_get(_buffer)) {
|
||||||
|
wlr_log(L_ERROR, "Invalid buffer: not a shared memory buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(_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);
|
||||||
|
if (width < output->width || height < output->height) {
|
||||||
|
wlr_log(L_ERROR, "Invalid buffer: too small");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *pixels = malloc(stride * height);
|
||||||
|
if (pixels == NULL) {
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_screenshot *screenshot =
|
||||||
|
calloc(1, sizeof(struct wlr_screenshot));
|
||||||
|
if (!screenshot) {
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
screenshot->output_resource = _output;
|
||||||
|
screenshot->output = output;
|
||||||
|
screenshot->screenshooter = screenshooter;
|
||||||
|
screenshot->resource = wl_resource_create(client,
|
||||||
|
&orbital_screenshot_interface, wl_resource_get_version(_screenshooter),
|
||||||
|
id);
|
||||||
|
wlr_log(L_DEBUG, "new screenshot %p (res %p)", screenshot,
|
||||||
|
screenshot->resource);
|
||||||
|
wl_resource_set_implementation(screenshot->resource, NULL, screenshot,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
|
||||||
|
if (!state) {
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state->width = width;
|
||||||
|
state->height = height;
|
||||||
|
state->stride = stride;
|
||||||
|
state->pixels = pixels;
|
||||||
|
state->shm_buffer = shm_buffer;
|
||||||
|
state->screenshot = screenshot;
|
||||||
|
state->frame_listener.notify = output_frame_notify;
|
||||||
|
wl_signal_add(&output->events.swap_buffers, &state->frame_listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct orbital_screenshooter_interface screenshooter_impl = {
|
||||||
|
.shoot = screenshooter_shoot,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void screenshooter_bind(struct wl_client *wl_client,
|
||||||
|
void *_screenshooter, uint32_t version, uint32_t id) {
|
||||||
|
struct wlr_screenshooter *screenshooter = _screenshooter;
|
||||||
|
assert(wl_client && screenshooter);
|
||||||
|
if (version > 1) {
|
||||||
|
wlr_log(L_ERROR, "Client requested unsupported screenshooter version,"
|
||||||
|
"disconnecting");
|
||||||
|
wl_client_destroy(wl_client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct wl_resource *wl_resource = wl_resource_create(wl_client,
|
||||||
|
&orbital_screenshooter_interface, version, id);
|
||||||
|
wl_resource_set_implementation(wl_resource, &screenshooter_impl,
|
||||||
|
screenshooter, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
|
||||||
|
struct wlr_renderer *renderer) {
|
||||||
|
struct wlr_screenshooter *screenshooter =
|
||||||
|
calloc(1, sizeof(struct wlr_screenshooter));
|
||||||
|
if (!screenshooter) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
screenshooter->renderer = renderer;
|
||||||
|
|
||||||
|
struct wl_global *wl_global = wl_global_create(display,
|
||||||
|
&orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
|
||||||
|
if (!wl_global) {
|
||||||
|
free(screenshooter);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
screenshooter->wl_global = wl_global;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in a new issue