Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Simple Wayland Screenshot Library

A minimal C++20 library for capturing screenshots from Wayland compositors, designed specifically for STB image compatibility.

Features

  • 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

Quick Start

1. Include the header

#include "wayland_screenshot.h"

2. Capture a screenshot

#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;
}

3. Build and link

# Build the library
cmake --build build --target wayland_screenshot

# Link your program
g++ -std=c++20 your_program.cpp -lwayland_screenshot -lwayland-client

API Reference

Functions

take_screenshot(int& width, int& height, int& channels)

Captures a screenshot from the default Wayland output.

  • Parameters:
    • width - Output parameter for image width
    • height - Output parameter for image height
    • channels - 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

take_screenshot(const std::string& output_name, int& width, int& height, int& channels)

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 width
    • height - Output parameter for image height
    • channels - Output parameter for number of channels (always 3 for RGB)
  • Returns: std::vector<uint8_t> containing RGB24 data
  • Returns empty vector on failure

Data Format

  • Format: RGB24 (8 bits per channel)
  • Layout: Packed pixel data [R,G,B,R,G,B,...]
  • Size: width * height * 3 bytes
  • Channels: Always 3 (RGB, no alpha)

Integration with STB Image

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;
}

Building

Dependencies

  • CMake 3.16+
  • C++20 compatible compiler
  • wayland-client
  • wayland-protocols
  • wayland-scanner

Build Steps

mkdir build
cd build
cmake ..
cmake --build . --target wayland_screenshot

Using in Your Project

Option 1: Link the static library

find_library(WAYLAND_SCREENSHOT wayland_screenshot)
target_link_libraries(your_target ${WAYLAND_SCREENSHOT} wayland-client)

Option 2: Add as subdirectory

add_subdirectory(path/to/wayland-screenshot)
target_link_libraries(your_target wayland_screenshot)

Error Handling

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;
}

Compatibility

  • Tested Compositors: Sway, Hyprland, GNOME (Wayland), KDE (Wayland)
  • Protocol: Requires wlr-screencopy-unstable-v1 support
  • Platforms: Linux with Wayland

Performance

  • Memory: Direct RGB conversion, no intermediate formats
  • Speed: Single compositor roundtrip
  • Dependencies: Minimal - only wayland-client required

Example Output

🚀 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)

License

This library follows the same license as the parent project.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages