commit
b799ea9c2a
@ -0,0 +1,11 @@
|
||||
Copyright 2024 Tranquillity Codes
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1,27 @@
|
||||
.PHONY: all clean
|
||||
|
||||
CC = gcc
|
||||
CLIBS = -lwayland-client
|
||||
CFLAGS = $(CLIBS)
|
||||
|
||||
WLR_PROTOCOL_IMPL = wlr-screencopy-protocol.c
|
||||
WLR_PROTOCOL_HEADER = wlr-screencopy-client-protocol.h
|
||||
|
||||
SRC = main.c
|
||||
OUT = screencopy
|
||||
|
||||
all: $(OUT)
|
||||
|
||||
clean:
|
||||
rm -f $(OUT)
|
||||
rm -f $(WLR_PROTOCOL_IMPL)
|
||||
rm -f $(WLR_PROTOCOL_HEADER)
|
||||
|
||||
$(OUT): $(WLR_PROTOCOL_IMPL) $(WLR_PROTOCOL_HEADER)
|
||||
$(CC) $(WLR_PROTOCOL_IMPL) $(SRC) $(CFLAGS) -o $(OUT)
|
||||
|
||||
$(WLR_PROTOCOL_IMPL):
|
||||
wayland-scanner private-code ./wlr-screencopy-unstable-v1.xml $(WLR_PROTOCOL_IMPL)
|
||||
|
||||
$(WLR_PROTOCOL_HEADER):
|
||||
wayland-scanner client-header ./wlr-screencopy-unstable-v1.xml $(WLR_PROTOCOL_HEADER)
|
@ -0,0 +1,11 @@
|
||||
A simple Wayland screenshotting utility that utilizes `wlr-screencopy`.
|
||||
|
||||
Runtime Dependencies:
|
||||
|
||||
- `libwayland-client`
|
||||
|
||||
Make Dependencies:
|
||||
|
||||
- `wayland-scanner`
|
||||
- `make`
|
||||
- `gcc`
|
@ -0,0 +1,137 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <wayland-client-core.h>
|
||||
#include <wayland-client-protocol.h>
|
||||
#include <wayland-client.h>
|
||||
|
||||
#include "wlr-screencopy-client-protocol.h"
|
||||
|
||||
#include "rif.h"
|
||||
|
||||
#define ASSERT(cond, msg)\
|
||||
if(!(cond)) {\
|
||||
fprintf(stderr, "Runtime assertion %s at %s:%d failed: %s\n", #cond, __FILE__, __LINE__, msg);\
|
||||
exit(-1);\
|
||||
}
|
||||
|
||||
// Not included in any standard header
|
||||
// Thus, declaring manually.
|
||||
// See memfd_create(2).
|
||||
int memfd_create(const char* name, unsigned int flags);
|
||||
|
||||
struct wl_output* wl_output = NULL;
|
||||
struct wl_shm* wl_shm = NULL;
|
||||
struct zwlr_screencopy_manager_v1* zwlr_manager = NULL;
|
||||
|
||||
int8_t ready = 0;
|
||||
uint32_t buf_size = 0;
|
||||
void* shm_data = NULL;
|
||||
uint32_t image_width = 0;
|
||||
uint32_t image_height = 0;
|
||||
uint32_t image_format = 0;
|
||||
|
||||
void handle_global(void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
|
||||
if(strcmp(interface, wl_shm_interface.name) == 0) {
|
||||
wl_shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
||||
} else if(strcmp(interface, wl_output_interface.name) == 0) {
|
||||
wl_output = wl_registry_bind(registry, name, &wl_output_interface, 3);
|
||||
} else if(strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) {
|
||||
zwlr_manager = wl_registry_bind(registry, name, &zwlr_screencopy_manager_v1_interface, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_global_remove(void* data, struct wl_registry* registry, uint32_t name) {
|
||||
// Who cares
|
||||
}
|
||||
|
||||
const struct wl_registry_listener reg_callbacks = {
|
||||
.global = handle_global,
|
||||
.global_remove = handle_global_remove,
|
||||
};
|
||||
|
||||
void frame_handle_buffer(void* data, struct zwlr_screencopy_frame_v1* frame, uint32_t format, uint32_t width, uint32_t height, uint32_t stride) {
|
||||
printf("Handling buffer.\n");
|
||||
image_width = width;
|
||||
image_height = height;
|
||||
image_format = format;
|
||||
buf_size = height * stride;
|
||||
|
||||
printf("Format: %010p\n", format);
|
||||
ASSERT(format == WL_SHM_FORMAT_XBGR8888 || format == WL_SHM_FORMAT_ABGR8888, "Unsupported buffer format copied.");
|
||||
|
||||
int shm_fd = memfd_create("wl_shm data", 0);
|
||||
ASSERT(shm_fd != -1, "Failed to create memfd.");
|
||||
|
||||
ASSERT(ftruncate(shm_fd, buf_size) != -1, "Failed to ftruncate the memfd.");
|
||||
|
||||
shm_data = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
|
||||
ASSERT(shm_data != MAP_FAILED, "Failed to mmap the memfd.");
|
||||
|
||||
struct wl_shm_pool* pool = wl_shm_create_pool(wl_shm, shm_fd, buf_size);
|
||||
ASSERT(pool != NULL, "Failed to create the wl_shm_pool.");
|
||||
|
||||
struct wl_buffer* buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format);
|
||||
ASSERT(buffer != NULL, "Failed to create the wl_buffer.");
|
||||
|
||||
zwlr_screencopy_frame_v1_copy(frame, buffer);
|
||||
}
|
||||
|
||||
void frame_handle_flags(void* data, struct zwlr_screencopy_frame_v1* frame, uint32_t flags) {
|
||||
// Ignored for now
|
||||
}
|
||||
|
||||
void frame_handle_ready(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) {
|
||||
printf("Ready.\n");
|
||||
ready = 1;
|
||||
}
|
||||
|
||||
void frame_handle_failed(void* data, struct zwlr_screencopy_frame_v1* frame) {
|
||||
fprintf(stderr, "Failed to copy output\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static const struct zwlr_screencopy_frame_v1_listener frame_listener = {
|
||||
.buffer = frame_handle_buffer,
|
||||
.flags = frame_handle_flags,
|
||||
.ready = frame_handle_ready,
|
||||
.failed = frame_handle_failed,
|
||||
};
|
||||
|
||||
int main() {
|
||||
struct wl_display* dpy = wl_display_connect(NULL);
|
||||
ASSERT(dpy != NULL, "Unable to connect to Wayland");
|
||||
|
||||
struct wl_registry* registry = wl_display_get_registry(dpy);
|
||||
wl_registry_add_listener(registry, ®_callbacks, NULL);
|
||||
wl_display_roundtrip(dpy);
|
||||
|
||||
ASSERT(wl_shm != NULL, "core.wl_shm not supported.");
|
||||
|
||||
ASSERT(zwlr_manager != NULL, "wlr-screencopy-unstable-v1.zwlr_screencopy_manager_v1 not supported.");
|
||||
|
||||
ASSERT(wl_output != NULL, "no core.wl_output found.");
|
||||
int32_t with_cursor = 1;
|
||||
|
||||
struct zwlr_screencopy_frame_v1* frame = zwlr_screencopy_manager_v1_capture_output(zwlr_manager, with_cursor, wl_output);
|
||||
ASSERT(frame != NULL, "wlr-screencopy-unstable-v1.zwlr_screencopy_manager_v1::capture_output has failed");
|
||||
|
||||
zwlr_screencopy_frame_v1_add_listener(frame, &frame_listener, wl_output);
|
||||
wl_display_roundtrip(dpy);
|
||||
|
||||
while(ready == 0 && wl_display_dispatch(dpy) != -1) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
printf("Saving screenshot.\n");
|
||||
|
||||
// void write_rif_little(char* path, uint32_t width, uint32_t size, uint8_t format, void* data) {
|
||||
// Thanks to endianess shenanigans, ABGR/XBGR is RGBA.
|
||||
write_rif_little("out.rif", image_width, buf_size, RIF_FORMAT_R8G8B8A8, shm_data);
|
||||
|
||||
printf("Saved screenshot.\n");
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
typedef struct rif {
|
||||
// "lifV0001"
|
||||
// "rifV0001"
|
||||
char magic_num[8]; // Big Endian. UTF-8 Encoded. 6C 69 66 56 30 30 30 31
|
||||
uint64_t width; // Big Endian
|
||||
uint8_t format;
|
||||
uint8_t data[];
|
||||
} rif_t;
|
||||
|
||||
#define RIF_MAGIC_LITTLE {'l', 'i', 'f', 'V', '0', '0', '0', '1'}
|
||||
#define RIF_MAGIC_BIG {'r', 'i', 'f', 'V', '0', '0', '0', '1'}
|
||||
|
||||
#define RIF_FORMAT_R8G8B8 0
|
||||
#define RIF_FORMAT_R8G8B8A8 1
|
||||
|
||||
void write_rif_little(char* path, uint32_t width, uint32_t size, uint8_t format, void* data) {
|
||||
#define IMG_SIZE 17 + size // 17 is the size of raw_img excluding the data field
|
||||
rif_t* img = malloc(IMG_SIZE);
|
||||
char magic[] = RIF_MAGIC_LITTLE;
|
||||
memcpy(((char*)img)+0, magic, 8);
|
||||
img->width = width;
|
||||
img->format = format;
|
||||
memcpy(((char*)img)+17, data, size);
|
||||
FILE* output_file = fopen(path, "w");
|
||||
fwrite(img, 1, IMG_SIZE, output_file);
|
||||
}
|
@ -0,0 +1,232 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<protocol name="wlr_screencopy_unstable_v1">
|
||||
<copyright>
|
||||
Copyright © 2018 Simon Ser
|
||||
Copyright © 2019 Andri Yngvason
|
||||
|
||||
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.
|
||||
</copyright>
|
||||
|
||||
<description summary="screen content capturing on client buffers">
|
||||
This protocol allows clients to ask the compositor to copy part of the
|
||||
screen content to a client buffer.
|
||||
|
||||
Warning! The protocol described in this file is experimental and
|
||||
backward incompatible changes may be made. Backward compatible changes
|
||||
may be added together with the corresponding interface version bump.
|
||||
Backward incompatible changes are done by bumping the version number in
|
||||
the protocol and interface names and resetting the interface version.
|
||||
Once the protocol is to be declared stable, the 'z' prefix and the
|
||||
version number in the protocol and interface names are removed and the
|
||||
interface version number is reset.
|
||||
</description>
|
||||
|
||||
<interface name="zwlr_screencopy_manager_v1" version="3">
|
||||
<description summary="manager to inform clients and begin capturing">
|
||||
This object is a manager which offers requests to start capturing from a
|
||||
source.
|
||||
</description>
|
||||
|
||||
<request name="capture_output">
|
||||
<description summary="capture an output">
|
||||
Capture the next frame of an entire output.
|
||||
</description>
|
||||
<arg name="frame" type="new_id" interface="zwlr_screencopy_frame_v1"/>
|
||||
<arg name="overlay_cursor" type="int"
|
||||
summary="composite cursor onto the frame"/>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
</request>
|
||||
|
||||
<request name="capture_output_region">
|
||||
<description summary="capture an output's region">
|
||||
Capture the next frame of an output's region.
|
||||
|
||||
The region is given in output logical coordinates, see
|
||||
xdg_output.logical_size. The region will be clipped to the output's
|
||||
extents.
|
||||
</description>
|
||||
<arg name="frame" type="new_id" interface="zwlr_screencopy_frame_v1"/>
|
||||
<arg name="overlay_cursor" type="int"
|
||||
summary="composite cursor onto the frame"/>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
<arg name="x" type="int"/>
|
||||
<arg name="y" type="int"/>
|
||||
<arg name="width" type="int"/>
|
||||
<arg name="height" type="int"/>
|
||||
</request>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the manager">
|
||||
All objects created by the manager will still remain valid, until their
|
||||
appropriate destroy request has been called.
|
||||
</description>
|
||||
</request>
|
||||
</interface>
|
||||
|
||||
<interface name="zwlr_screencopy_frame_v1" version="3">
|
||||
<description summary="a frame ready for copy">
|
||||
This object represents a single frame.
|
||||
|
||||
When created, a series of buffer events will be sent, each representing a
|
||||
supported buffer type. The "buffer_done" event is sent afterwards to
|
||||
indicate that all supported buffer types have been enumerated. The client
|
||||
will then be able to send a "copy" request. If the capture is successful,
|
||||
the compositor will send a "flags" followed by a "ready" event.
|
||||
|
||||
For objects version 2 or lower, wl_shm buffers are always supported, ie.
|
||||
the "buffer" event is guaranteed to be sent.
|
||||
|
||||
If the capture failed, the "failed" event is sent. This can happen anytime
|
||||
before the "ready" event.
|
||||
|
||||
Once either a "ready" or a "failed" event is received, the client should
|
||||
destroy the frame.
|
||||
</description>
|
||||
|
||||
<event name="buffer">
|
||||
<description summary="wl_shm buffer information">
|
||||
Provides information about wl_shm buffer parameters that need to be
|
||||
used for this frame. This event is sent once after the frame is created
|
||||
if wl_shm buffers are supported.
|
||||
</description>
|
||||
<arg name="format" type="uint" enum="wl_shm.format" summary="buffer format"/>
|
||||
<arg name="width" type="uint" summary="buffer width"/>
|
||||
<arg name="height" type="uint" summary="buffer height"/>
|
||||
<arg name="stride" type="uint" summary="buffer stride"/>
|
||||
</event>
|
||||
|
||||
<request name="copy">
|
||||
<description summary="copy the frame">
|
||||
Copy the frame to the supplied buffer. The buffer must have a the
|
||||
correct size, see zwlr_screencopy_frame_v1.buffer and
|
||||
zwlr_screencopy_frame_v1.linux_dmabuf. The buffer needs to have a
|
||||
supported format.
|
||||
|
||||
If the frame is successfully copied, a "flags" and a "ready" events are
|
||||
sent. Otherwise, a "failed" event is sent.
|
||||
</description>
|
||||
<arg name="buffer" type="object" interface="wl_buffer"/>
|
||||
</request>
|
||||
|
||||
<enum name="error">
|
||||
<entry name="already_used" value="0"
|
||||
summary="the object has already been used to copy a wl_buffer"/>
|
||||
<entry name="invalid_buffer" value="1"
|
||||
summary="buffer attributes are invalid"/>
|
||||
</enum>
|
||||
|
||||
<enum name="flags" bitfield="true">
|
||||
<entry name="y_invert" value="1" summary="contents are y-inverted"/>
|
||||
</enum>
|
||||
|
||||
<event name="flags">
|
||||
<description summary="frame flags">
|
||||
Provides flags about the frame. This event is sent once before the
|
||||
"ready" event.
|
||||
</description>
|
||||
<arg name="flags" type="uint" enum="flags" summary="frame flags"/>
|
||||
</event>
|
||||
|
||||
<event name="ready">
|
||||
<description summary="indicates frame is available for reading">
|
||||
Called as soon as the frame is copied, indicating it is available
|
||||
for reading. This event includes the time at which presentation happened
|
||||
at.
|
||||
|
||||
The timestamp is expressed as tv_sec_hi, tv_sec_lo, tv_nsec triples,
|
||||
each component being an unsigned 32-bit value. Whole seconds are in
|
||||
tv_sec which is a 64-bit value combined from tv_sec_hi and tv_sec_lo,
|
||||
and the additional fractional part in tv_nsec as nanoseconds. Hence,
|
||||
for valid timestamps tv_nsec must be in [0, 999999999]. The seconds part
|
||||
may have an arbitrary offset at start.
|
||||
|
||||
After receiving this event, the client should destroy the object.
|
||||
</description>
|
||||
<arg name="tv_sec_hi" type="uint"
|
||||
summary="high 32 bits of the seconds part of the timestamp"/>
|
||||
<arg name="tv_sec_lo" type="uint"
|
||||
summary="low 32 bits of the seconds part of the timestamp"/>
|
||||
<arg name="tv_nsec" type="uint"
|
||||
summary="nanoseconds part of the timestamp"/>
|
||||
</event>
|
||||
|
||||
<event name="failed">
|
||||
<description summary="frame copy failed">
|
||||
This event indicates that the attempted frame copy has failed.
|
||||
|
||||
After receiving this event, the client should destroy the object.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="delete this object, used or not">
|
||||
Destroys the frame. This request can be sent at any time by the client.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<!-- Version 2 additions -->
|
||||
<request name="copy_with_damage" since="2">
|
||||
<description summary="copy the frame when it's damaged">
|
||||
Same as copy, except it waits until there is damage to copy.
|
||||
</description>
|
||||
<arg name="buffer" type="object" interface="wl_buffer"/>
|
||||
</request>
|
||||
|
||||
<event name="damage" since="2">
|
||||
<description summary="carries the coordinates of the damaged region">
|
||||
This event is sent right before the ready event when copy_with_damage is
|
||||
requested. It may be generated multiple times for each copy_with_damage
|
||||
request.
|
||||
|
||||
The arguments describe a box around an area that has changed since the
|
||||
last copy request that was derived from the current screencopy manager
|
||||
instance.
|
||||
|
||||
The union of all regions received between the call to copy_with_damage
|
||||
and a ready event is the total damage since the prior ready event.
|
||||
</description>
|
||||
<arg name="x" type="uint" summary="damaged x coordinates"/>
|
||||
<arg name="y" type="uint" summary="damaged y coordinates"/>
|
||||
<arg name="width" type="uint" summary="current width"/>
|
||||
<arg name="height" type="uint" summary="current height"/>
|
||||
</event>
|
||||
|
||||
<!-- Version 3 additions -->
|
||||
<event name="linux_dmabuf" since="3">
|
||||
<description summary="linux-dmabuf buffer information">
|
||||
Provides information about linux-dmabuf buffer parameters that need to
|
||||
be used for this frame. This event is sent once after the frame is
|
||||
created if linux-dmabuf buffers are supported.
|
||||
</description>
|
||||
<arg name="format" type="uint" summary="fourcc pixel format"/>
|
||||
<arg name="width" type="uint" summary="buffer width"/>
|
||||
<arg name="height" type="uint" summary="buffer height"/>
|
||||
</event>
|
||||
|
||||
<event name="buffer_done" since="3">
|
||||
<description summary="all buffer types reported">
|
||||
This event is sent once after all buffer events have been sent.
|
||||
|
||||
The client should proceed to create a buffer of one of the supported
|
||||
types, and send a "copy" request.
|
||||
</description>
|
||||
</event>
|
||||
</interface>
|
||||
</protocol>
|
Loading…
Reference in new issue