A minimal C++20 library for capturing screenshots from Wayland compositors, designed specifically for STB image compatibility.
- Minimal Dependencies: Only requires
wayland-client(no Cairo or other heavy dependencies) - STB Image Compatible: Returns RGB24 data that works directly with STB image libraries
- Simple API: Just two functions to capture screenshots
- Memory Efficient: Direct conversion to RGB format without intermediate storage
- Modern C++: Uses C++20 features and RAII principles
#include "wayland_screenshot.h"#include "wayland_screenshot.h"
#include <iostream>
int main() {
int width, height, channels;
auto rgb_data = wayland_screenshot::take_screenshot(width, height, channels);
if (!rgb_data.empty()) {
std::cout << "Captured " << width << "x" << height << " screenshot\n";
std::cout << "RGB data size: " << rgb_data.size() << " bytes\n";
// Use with STB image write functions:
// stbi_write_png("screenshot.png", width, height, 3, rgb_data.data(), 0);
// stbi_write_jpg("screenshot.jpg", width, height, 3, rgb_data.data(), 90);
}
return 0;
}# Build the library
cmake --build build --target wayland_screenshot
# Link your program
g++ -std=c++20 your_program.cpp -lwayland_screenshot -lwayland-clientCaptures a screenshot from the default Wayland output.
- Parameters:
width- Output parameter for image widthheight- Output parameter for image heightchannels- Output parameter for number of channels (always 3 for RGB)
- Returns:
std::vector<uint8_t>containing RGB24 data in format[R,G,B,R,G,B,...] - Returns empty vector on failure
Captures a screenshot from a specific Wayland output.
- Parameters:
output_name- Name of the output (e.g., "DP-1", "HDMI-A-1")width- Output parameter for image widthheight- Output parameter for image heightchannels- Output parameter for number of channels (always 3 for RGB)
- Returns:
std::vector<uint8_t>containing RGB24 data - Returns empty vector on failure
- Format: RGB24 (8 bits per channel)
- Layout: Packed pixel data
[R,G,B,R,G,B,...] - Size:
width * height * 3bytes - Channels: Always 3 (RGB, no alpha)
The returned RGB data is directly compatible with STB image functions:
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "wayland_screenshot.h"
int main() {
int width, height, channels;
auto rgb_data = wayland_screenshot::take_screenshot(width, height, channels);
if (!rgb_data.empty()) {
// Save as PNG
stbi_write_png("screenshot.png", width, height, 3, rgb_data.data(), 0);
// Save as JPEG (90% quality)
stbi_write_jpg("screenshot.jpg", width, height, 3, rgb_data.data(), 90);
// Save as BMP
stbi_write_bmp("screenshot.bmp", width, height, 3, rgb_data.data());
}
return 0;
}- CMake 3.16+
- C++20 compatible compiler
- wayland-client
- wayland-protocols
- wayland-scanner
mkdir build
cd build
cmake ..
cmake --build . --target wayland_screenshotfind_library(WAYLAND_SCREENSHOT wayland_screenshot)
target_link_libraries(your_target ${WAYLAND_SCREENSHOT} wayland-client)add_subdirectory(path/to/wayland-screenshot)
target_link_libraries(your_target wayland_screenshot)Functions return empty vectors on failure. Common failure reasons:
- Not running under a Wayland compositor
- Compositor doesn't support wlr-screencopy protocol
- No outputs available
- Permission denied
auto rgb_data = wayland_screenshot::take_screenshot(width, height, channels);
if (rgb_data.empty()) {
std::cerr << "Screenshot failed - check if running under Wayland\n";
return -1;
}- Tested Compositors: Sway, Hyprland, GNOME (Wayland), KDE (Wayland)
- Protocol: Requires wlr-screencopy-unstable-v1 support
- Platforms: Linux with Wayland
- Memory: Direct RGB conversion, no intermediate formats
- Speed: Single compositor roundtrip
- Dependencies: Minimal - only wayland-client required
🚀 Simple Wayland Screenshot - STB Image Compatible
✅ Screenshot captured successfully!
📏 Dimensions: 2560x1440
🎨 Channels: 3 (RGB)
📦 Data size: 11059200 bytes
🔢 Expected size: 11059200 bytes
💡 This RGB data can now be used with STB image functions:
- stbi_write_png("screenshot.png", width, height, 3, rgb_data.data(), 0)
- stbi_write_jpg("screenshot.jpg", width, height, 3, rgb_data.data(), 90)
This library follows the same license as the parent project.