2020-01-24 23:31:01 +01:00
|
|
|
#include "wlr_screencast.h"
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
#include "wlr-screencopy-unstable-v1-client-protocol.h"
|
|
|
|
#include "xdg-output-unstable-v1-client-protocol.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-client-protocol.h>
|
|
|
|
|
|
|
|
#include "screencast.h"
|
|
|
|
#include "pipewire_screencast.h"
|
|
|
|
#include "xdpw.h"
|
|
|
|
#include "logger.h"
|
|
|
|
|
2020-08-14 10:47:13 +02:00
|
|
|
static void wlr_frame_buffer_destroy(struct xdpw_screencast_instance *cast) {
|
2020-08-14 01:47:44 +02:00
|
|
|
// This check isn't needed
|
2020-08-04 23:12:52 +02:00
|
|
|
if (cast->simple_frame.data != NULL) {
|
|
|
|
munmap(cast->simple_frame.data, cast->simple_frame.size);
|
|
|
|
cast->simple_frame.data = NULL;
|
|
|
|
}
|
2020-08-14 01:47:44 +02:00
|
|
|
// This one is
|
2020-08-04 23:12:52 +02:00
|
|
|
if (cast->simple_frame.buffer != NULL) {
|
|
|
|
wl_buffer_destroy(cast->simple_frame.buffer);
|
|
|
|
cast->simple_frame.buffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
void xdpw_wlr_frame_free(struct xdpw_screencast_instance *cast) {
|
|
|
|
zwlr_screencopy_frame_v1_destroy(cast->wlr_frame);
|
|
|
|
cast->wlr_frame = NULL;
|
2020-08-04 02:32:53 +02:00
|
|
|
if (cast->quit || cast->err) {
|
2020-08-14 10:47:13 +02:00
|
|
|
wlr_frame_buffer_destroy(cast);
|
2020-08-04 17:47:02 +02:00
|
|
|
logprint(TRACE, "xdpw: simple_frame buffer destroyed");
|
2020-08-04 02:32:53 +02:00
|
|
|
}
|
2020-08-04 17:47:02 +02:00
|
|
|
logprint(TRACE, "wlroots: frame destroyed");
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
if (cast->quit || cast->err) {
|
2020-04-20 17:05:08 +02:00
|
|
|
// TODO: revisit the exit condition (remove quit?)
|
|
|
|
// and clean up sessions that still exist if err
|
|
|
|
// is the cause of the instance_destroy call
|
2020-04-16 10:21:55 +02:00
|
|
|
xdpw_screencast_instance_destroy(cast);
|
|
|
|
return ;
|
2020-03-10 20:46:37 +01:00
|
|
|
}
|
2020-04-16 10:21:55 +02:00
|
|
|
|
|
|
|
xdpw_wlr_register_cb(cast);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anonymous_shm_open(void) {
|
|
|
|
char name[] = "/xdpw-shm-XXXXXX";
|
|
|
|
int retries = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
randname(name + strlen(name) - 6);
|
|
|
|
|
|
|
|
--retries;
|
|
|
|
// shm_open guarantees that O_CLOEXEC is set
|
|
|
|
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd >= 0) {
|
|
|
|
shm_unlink(name);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
} while (retries > 0 && errno == EEXIST);
|
|
|
|
|
|
|
|
return -1;
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
static struct wl_buffer *create_shm_buffer(struct xdpw_screencast_instance *cast,
|
2020-03-13 17:43:24 +01:00
|
|
|
enum wl_shm_format fmt, int width, int height, int stride,
|
|
|
|
void **data_out) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_context *ctx = cast->ctx;
|
2020-01-24 23:31:01 +01:00
|
|
|
int size = stride * height;
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
int fd = anonymous_shm_open();
|
2020-01-24 23:31:01 +01:00
|
|
|
if (fd < 0) {
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "wlroots: shm_open failed");
|
2020-01-24 23:31:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
2020-03-10 20:46:37 +01:00
|
|
|
while ((ret = ftruncate(fd, size)) == EINTR);
|
|
|
|
|
2020-01-24 23:31:01 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
close(fd);
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "wlroots: ftruncate failed");
|
2020-01-24 23:31:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (data == MAP_FAILED) {
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "wlroots: mmap failed: %m");
|
2020-01-24 23:31:01 +01:00
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_shm_pool *pool = wl_shm_create_pool(ctx->shm, fd, size);
|
|
|
|
close(fd);
|
|
|
|
struct wl_buffer *buffer =
|
2020-03-13 17:43:24 +01:00
|
|
|
wl_shm_pool_create_buffer(pool, 0, width, height, stride, fmt);
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_shm_pool_destroy(pool);
|
|
|
|
|
|
|
|
*data_out = data;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-08-04 23:12:52 +02:00
|
|
|
static void wlr_frame_buffer_chparam(struct xdpw_screencast_instance *cast,
|
|
|
|
uint32_t format, uint32_t width, uint32_t height, uint32_t stride) {
|
|
|
|
|
|
|
|
logprint(DEBUG, "wlroots: reset buffer");
|
|
|
|
cast->simple_frame.width = width;
|
|
|
|
cast->simple_frame.height = height;
|
|
|
|
cast->simple_frame.stride = stride;
|
|
|
|
cast->simple_frame.size = stride * height;
|
|
|
|
cast->simple_frame.format = format;
|
2020-08-14 10:47:13 +02:00
|
|
|
wlr_frame_buffer_destroy(cast);
|
2020-08-04 23:12:52 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 23:31:01 +01:00
|
|
|
static void wlr_frame_buffer(void *data, struct zwlr_screencopy_frame_v1 *frame,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t format, uint32_t width, uint32_t height, uint32_t stride) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(TRACE, "wlroots: buffer event handler");
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->wlr_frame = frame;
|
2020-08-04 18:51:09 +02:00
|
|
|
if (cast->simple_frame.width != width || cast->simple_frame.height != height || cast->simple_frame.stride != stride || cast->simple_frame.format != format) {
|
2020-08-04 17:27:37 +02:00
|
|
|
logprint(TRACE, "wlroots: buffer properties changed");
|
2020-08-04 23:12:52 +02:00
|
|
|
wlr_frame_buffer_chparam(cast, format, width, height, stride);
|
2020-08-04 18:51:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 23:07:32 +02:00
|
|
|
if (cast->simple_frame.buffer == NULL) {
|
2020-08-04 18:51:09 +02:00
|
|
|
logprint(DEBUG, "wlroots: create shm buffer");
|
2020-08-04 17:27:37 +02:00
|
|
|
cast->simple_frame.buffer = create_shm_buffer(cast, format, width, height,
|
2020-08-04 18:51:09 +02:00
|
|
|
stride, &cast->simple_frame.data);
|
|
|
|
} else {
|
2020-08-14 01:32:50 +02:00
|
|
|
logprint(TRACE,"wlroots: shm buffer exists");
|
2020-08-04 17:27:37 +02:00
|
|
|
}
|
2020-04-16 10:21:55 +02:00
|
|
|
|
|
|
|
if (cast->simple_frame.buffer == NULL) {
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "wlroots: failed to create buffer");
|
2020-03-13 17:43:24 +01:00
|
|
|
abort();
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
zwlr_screencopy_frame_v1_copy_with_damage(frame, cast->simple_frame.buffer);
|
2020-03-10 20:46:37 +01:00
|
|
|
logprint(TRACE, "wlroots: frame copied");
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_frame_flags(void *data, struct zwlr_screencopy_frame_v1 *frame,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t flags) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(TRACE, "wlroots: flags event handler");
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->simple_frame.y_invert = flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT;
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_frame_ready(void *data, struct zwlr_screencopy_frame_v1 *frame,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(TRACE, "wlroots: ready event handler");
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->simple_frame.tv_sec = ((((uint64_t)tv_sec_hi) << 32) | tv_sec_lo);
|
|
|
|
cast->simple_frame.tv_nsec = tv_nsec;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
if (!cast->quit && !cast->err && cast->pwr_stream_state) {
|
|
|
|
pw_loop_signal_event(cast->ctx->state->pw_loop, cast->event);
|
2020-03-10 20:46:37 +01:00
|
|
|
return ;
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
2020-03-10 20:46:37 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
xdpw_wlr_frame_free(cast);
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_frame_failed(void *data,
|
2020-03-13 17:43:24 +01:00
|
|
|
struct zwlr_screencopy_frame_v1 *frame) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(TRACE, "wlroots: failed event handler");
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->err = true;
|
2020-03-10 20:46:37 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
xdpw_wlr_frame_free(cast);
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_frame_damage(void *data, struct zwlr_screencopy_frame_v1 *frame,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast = data;
|
2020-03-10 20:46:37 +01:00
|
|
|
|
|
|
|
logprint(TRACE, "wlroots: damage event handler");
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->simple_frame.damage.x = x;
|
|
|
|
cast->simple_frame.damage.y = y;
|
|
|
|
cast->simple_frame.damage.width = width;
|
|
|
|
cast->simple_frame.damage.height = height;
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zwlr_screencopy_frame_v1_listener wlr_frame_listener = {
|
2020-03-13 17:43:24 +01:00
|
|
|
.buffer = wlr_frame_buffer,
|
|
|
|
.flags = wlr_frame_flags,
|
|
|
|
.ready = wlr_frame_ready,
|
|
|
|
.failed = wlr_frame_failed,
|
|
|
|
.damage = wlr_frame_damage,
|
2020-01-24 23:31:01 +01:00
|
|
|
};
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
void xdpw_wlr_register_cb(struct xdpw_screencast_instance *cast) {
|
2020-03-13 17:43:24 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
cast->frame_callback = zwlr_screencopy_manager_v1_capture_output(
|
|
|
|
cast->ctx->screencopy_manager, cast->with_cursor, cast->target_output->output);
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
zwlr_screencopy_frame_v1_add_listener(cast->frame_callback,
|
|
|
|
&wlr_frame_listener, cast);
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(TRACE, "wlroots: callbacks registered");
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_output_handle_geometry(void *data, struct wl_output *wl_output,
|
2020-03-13 17:43:24 +01:00
|
|
|
int32_t x, int32_t y, int32_t phys_width, int32_t phys_height,
|
|
|
|
int32_t subpixel, const char *make, const char *model, int32_t transform) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
output->make = strdup(make);
|
|
|
|
output->model = strdup(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_output_handle_mode(void *data, struct wl_output *wl_output,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
|
2020-01-24 23:31:01 +01:00
|
|
|
if (flags & WL_OUTPUT_MODE_CURRENT) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output = data;
|
2020-02-19 17:48:36 +01:00
|
|
|
output->framerate = (float)refresh/1000;
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_output_handle_done(void *data, struct wl_output *wl_output) {
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_output_handle_scale(void *data, struct wl_output *wl_output,
|
2020-03-13 17:43:24 +01:00
|
|
|
int32_t factor) {
|
2020-01-24 23:31:01 +01:00
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_output_listener wlr_output_listener = {
|
2020-03-13 17:43:24 +01:00
|
|
|
.geometry = wlr_output_handle_geometry,
|
|
|
|
.mode = wlr_output_handle_mode,
|
|
|
|
.done = wlr_output_handle_done,
|
|
|
|
.scale = wlr_output_handle_scale,
|
2020-01-24 23:31:01 +01:00
|
|
|
};
|
|
|
|
|
2020-02-19 22:55:51 +01:00
|
|
|
static void wlr_xdg_output_name(void* data, struct zxdg_output_v1* xdg_output,
|
2020-03-13 17:43:24 +01:00
|
|
|
const char* name) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output = data;
|
2020-02-19 22:55:51 +01:00
|
|
|
|
|
|
|
output->name = strdup(name);
|
|
|
|
};
|
|
|
|
|
2020-03-13 17:43:24 +01:00
|
|
|
static void noop() {
|
|
|
|
// This space intentionally left blank
|
2020-02-19 22:55:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zxdg_output_v1_listener wlr_xdg_output_listener = {
|
2020-03-13 17:43:24 +01:00
|
|
|
.logical_position = noop,
|
|
|
|
.logical_size = noop,
|
2020-02-19 22:55:51 +01:00
|
|
|
.done = NULL, /* Deprecated */
|
2020-03-13 17:43:24 +01:00
|
|
|
.description = noop,
|
2020-02-19 22:55:51 +01:00
|
|
|
.name = wlr_xdg_output_name,
|
|
|
|
};
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
static void wlr_add_xdg_output_listener(struct xdpw_wlr_output *output,
|
2020-03-13 17:43:24 +01:00
|
|
|
struct zxdg_output_v1* xdg_output) {
|
2020-02-19 22:55:51 +01:00
|
|
|
output->xdg_output = xdg_output;
|
|
|
|
zxdg_output_v1_add_listener(output->xdg_output, &wlr_xdg_output_listener,
|
2020-03-13 17:43:24 +01:00
|
|
|
output);
|
2020-02-19 22:55:51 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
static void wlr_init_xdg_outputs(struct xdpw_screencast_context *ctx) {
|
|
|
|
struct xdpw_wlr_output *output, *tmp;
|
2020-02-19 22:55:51 +01:00
|
|
|
wl_list_for_each_safe(output, tmp, &ctx->output_list, link) {
|
|
|
|
struct zxdg_output_v1 *xdg_output =
|
2020-04-16 10:21:55 +02:00
|
|
|
zxdg_output_manager_v1_get_xdg_output(ctx->xdg_output_manager,
|
2020-03-13 17:43:24 +01:00
|
|
|
output->output);
|
2020-02-19 22:55:51 +01:00
|
|
|
wlr_add_xdg_output_listener(output, xdg_output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *xdpw_wlr_output_first(struct wl_list *output_list) {
|
|
|
|
struct xdpw_wlr_output *output, *tmp;
|
2020-02-19 22:55:51 +01:00
|
|
|
wl_list_for_each_safe(output, tmp, output_list, link) {
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *xdpw_wlr_output_find_by_name(struct wl_list *output_list,
|
2020-03-13 17:43:24 +01:00
|
|
|
const char* name) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output, *tmp;
|
2020-03-13 17:43:24 +01:00
|
|
|
wl_list_for_each_safe(output, tmp, output_list, link) {
|
|
|
|
if (strcmp(output->name, name) == 0) {
|
2020-02-19 22:55:51 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *xdpw_wlr_output_find(struct xdpw_screencast_context *ctx,
|
2020-03-13 17:43:24 +01:00
|
|
|
struct wl_output *out, uint32_t id) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output, *tmp;
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_list_for_each_safe(output, tmp, &ctx->output_list, link) {
|
|
|
|
if ((output->output == out) || (output->id == id)) {
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
static void wlr_remove_output(struct xdpw_wlr_output *out) {
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_list_remove(&out->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_registry_handle_add(void *data, struct wl_registry *reg,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t id, const char *interface, uint32_t ver) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_context *ctx = data;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
|
|
|
if (!strcmp(interface, wl_output_interface.name)) {
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_wlr_output *output = malloc(sizeof(*output));
|
2020-01-24 23:31:01 +01:00
|
|
|
|
|
|
|
output->id = id;
|
|
|
|
output->output = wl_registry_bind(reg, id, &wl_output_interface, 1);
|
|
|
|
|
|
|
|
wl_output_add_listener(output->output, &wlr_output_listener, output);
|
|
|
|
wl_list_insert(&ctx->output_list, &output->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(interface, zwlr_screencopy_manager_v1_interface.name)) {
|
|
|
|
ctx->screencopy_manager = wl_registry_bind(
|
2020-03-13 17:43:24 +01:00
|
|
|
reg, id, &zwlr_screencopy_manager_v1_interface, SC_MANAGER_VERSION);
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
|
|
|
ctx->shm = wl_registry_bind(reg, id, &wl_shm_interface, 1);
|
|
|
|
}
|
2020-02-19 22:55:51 +01:00
|
|
|
|
|
|
|
if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
|
|
|
|
ctx->xdg_output_manager =
|
|
|
|
wl_registry_bind(reg, id, &zxdg_output_manager_v1_interface, 3);
|
|
|
|
}
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_registry_handle_remove(void *data, struct wl_registry *reg,
|
2020-03-13 17:43:24 +01:00
|
|
|
uint32_t id) {
|
2020-01-24 23:31:01 +01:00
|
|
|
wlr_remove_output(
|
2020-04-16 10:21:55 +02:00
|
|
|
xdpw_wlr_output_find((struct xdpw_screencast_context *)data, NULL, id));
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_registry_listener wlr_registry_listener = {
|
2020-03-13 17:43:24 +01:00
|
|
|
.global = wlr_registry_handle_add,
|
|
|
|
.global_remove = wlr_registry_handle_remove,
|
2020-01-24 23:31:01 +01:00
|
|
|
};
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
int xdpw_wlr_screencopy_init(struct xdpw_state *state) {
|
|
|
|
struct xdpw_screencast_context *ctx = &state->screencast;
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
// initialize a list of outputs
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_list_init(&ctx->output_list);
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
// initialize a list of active screencast instances
|
|
|
|
wl_list_init(&ctx->screencast_instances);
|
|
|
|
|
2020-01-24 23:31:01 +01:00
|
|
|
// retrieve registry
|
2020-03-10 20:46:37 +01:00
|
|
|
ctx->registry = wl_display_get_registry(state->wl_display);
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_registry_add_listener(ctx->registry, &wlr_registry_listener, ctx);
|
|
|
|
|
2020-03-10 20:46:37 +01:00
|
|
|
wl_display_dispatch(state->wl_display);
|
|
|
|
wl_display_roundtrip(state->wl_display);
|
|
|
|
|
2020-04-20 17:05:08 +02:00
|
|
|
logprint(DEBUG, "wayland: registry listeners run");
|
2020-02-19 22:55:51 +01:00
|
|
|
|
|
|
|
wlr_init_xdg_outputs(ctx);
|
|
|
|
|
2020-03-10 20:46:37 +01:00
|
|
|
wl_display_dispatch(state->wl_display);
|
|
|
|
wl_display_roundtrip(state->wl_display);
|
2020-01-24 23:31:01 +01:00
|
|
|
|
2020-04-20 17:05:08 +02:00
|
|
|
logprint(DEBUG, "wayland: xdg output listeners run");
|
2020-03-10 20:46:37 +01:00
|
|
|
|
|
|
|
// make sure our wlroots supports shm protocol
|
2020-01-24 23:31:01 +01:00
|
|
|
if (!ctx->shm) {
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "Compositor doesn't support %s!", "wl_shm");
|
2020-01-24 23:31:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure our wlroots supports screencopy protocol
|
|
|
|
if (!ctx->screencopy_manager) {
|
2020-02-20 20:59:46 +01:00
|
|
|
logprint(ERROR, "Compositor doesn't support %s!",
|
2020-03-13 17:43:24 +01:00
|
|
|
zwlr_screencopy_manager_v1_interface.name);
|
2020-01-24 23:31:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
void xdpw_wlr_screencopy_finish(struct xdpw_screencast_context *ctx) {
|
|
|
|
struct xdpw_wlr_output *output, *tmp_o;
|
2020-01-24 23:31:01 +01:00
|
|
|
wl_list_for_each_safe(output, tmp_o, &ctx->output_list, link) {
|
|
|
|
wl_list_remove(&output->link);
|
2020-02-19 22:55:51 +01:00
|
|
|
zxdg_output_v1_destroy(output->xdg_output);
|
|
|
|
wl_output_destroy(output->output);
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:21:55 +02:00
|
|
|
struct xdpw_screencast_instance *cast, *tmp_c;
|
|
|
|
wl_list_for_each_safe(cast, tmp_c, &ctx->screencast_instances, link) {
|
|
|
|
cast->quit = true;
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:31:01 +01:00
|
|
|
if (ctx->screencopy_manager) {
|
|
|
|
zwlr_screencopy_manager_v1_destroy(ctx->screencopy_manager);
|
|
|
|
}
|
2020-04-16 10:21:55 +02:00
|
|
|
if (ctx->shm) {
|
|
|
|
wl_shm_destroy(ctx->shm);
|
|
|
|
}
|
|
|
|
if (ctx->xdg_output_manager) {
|
|
|
|
zxdg_output_manager_v1_destroy(ctx->xdg_output_manager);
|
|
|
|
}
|
|
|
|
if (ctx->registry) {
|
|
|
|
wl_registry_destroy(ctx->registry);
|
|
|
|
}
|
2020-01-24 23:31:01 +01:00
|
|
|
}
|