wayshot

repository·main·Indexed 41 days ago

https://github.com/waycrate/wayshot

A native, high-performance screenshot tool and library (libwayshot 0.9.0) written in Rust for wlroots-based Wayland compositors like Sway and River. It supports fullscreen, region, output, and window captures, as well as a color picker. libwayshot provides a programmatic API for screenshots with support for SHM, DMA-BUF, and EGL-accelerated GPU capture paths.

Tokens
13.4K
Snippets
51
Records
71
Agent score
52%

What's inside wayshot

  1. Use the EGL capture backend for GPU screenshots

    main

    When the egl feature is enabled, libwayshot provides a zero-copy GPU screen capture path (DMA-BUF → EGLImage → GL texture). This is useful for high-performance capture requirements.

    Available EGL-specific APIs:

    • WayshotConnection::capture_target_frame_eglimage: Capture to an EGLImage.
    • WayshotConnection::capture_target_frame_eglimage_on_display: Capture to an EGLImage on a specific EGLDisplay.
    • WayshotConnection::bind_target_frame_to_gl_texture: Capture and bind directly to the current GL texture.
    • WayshotConnection::create_screencast_with_egl: Set up a screencast session with EGL display binding.
    • EGLImageGuard: An RAII wrapper that manages the lifecycle of the EGLImage, ensuring it is destroyed when dropped.
  2. Generate shell completions for wayshot

    main

    You can generate completion scripts for various shells using the --completions <SHELL> flag:

    • Fish: wayshot --completions fish | source
    • Zsh: wayshot --completions zsh > ~/.zfunc/_wayshot
    • Bash: wayshot --completions bash > /etc/bash_completion.d/wayshot
    • Elvish: wayshot --completions elvish >> ~/.config/elvish/rc.elv
    • PowerShell: wayshot --completions pwsh >> $PROFILE
    • Nushell: wayshot --completions nushell | save -f ~/.config/nushell/completions/wayshot.nu
    wayshot --completions fish | source
  3. Capture screenshots with wayshot

    main

    wayshot is a fast screenshot tool for wlroots-based compositors (like sway or river). Use the following commands for common capture tasks:

    • Fullscreen: Run wayshot without arguments.
    • Interactive Region Selection: Use the -g flag (requires the selector feature). You can also specify custom background and foreground colors using --geometry-background-color and --geometry-foreground-color with hex values.
    • Geometry Strings: Capture a specific area using a geometry string (compatible with slurp or waysip) via wayshot -g "GEOMETRY".
    • Specific Output: List available outputs with --list-outputs, then capture a specific one with -o <output_name>.
    • Window Capture: Interactively choose a window to capture using --choose-toplevel.
    • Clipboard: Capture a screenshot and copy it directly to the Wayland clipboard using --clipboard (requires the clipboard feature).
    # Fullscreen
    wayshot
    
    # Interactive region selection
    wayshot -g
    
    # Capture using slurp geometry
    wayshot FILE -g "$(slurp)"
    
    # Capture specific output
    wayshot --list-outputs
    wayshot -o eDP-1
    
    # Capture window
    wayshot --choose-toplevel
    
    # Copy to clipboard
    wayshot --clipboard
  4. Use libwayshot to take screenshots

    main

    To take a screenshot using libwayshot, create a new WayshotConnection and call screenshot_all(). This returns an image buffer containing the screenshot of all outputs.

    use libwayshot::WayshotConnection;
    
    let wayshot_connection = WayshotConnection::new()?;
    let image_buffer = wayshot_connection.screenshot_all()?;
  5. Install wayshot

    main

    AUR

    Install via wayshot-git or wayshot-bin.

    Nix

    Release builds are available in nixpkgs.

    Compiling from source

    Compile-time dependencies:

    • scdoc (optional, for man-pages)
    • rustup
    • make
    • pkg-config

    Build steps:

    git clone https://github.com/waycrate/wayshot && cd wayshot
    make setup
    make
    sudo make install
  6. Configure libwayshot features

    main

    By default, all features are enabled. If you want to reduce dependencies, you can disable default features and selectively enable specific encoding or backend features in your Cargo.toml.

    Supported features:

    • png: PNG encoding/decoding
    • jpeg: JPEG encoding/decoding
    • qoi: QOI encoding/decoding
    • webp: WebP encoding/decoding
    • avif: AVIF encoding/decoding (requires rav1e via image crate)
    • egl: EGL/OpenGL GPU capture backend (requires gl and r-egl-wayland)
    libwayshot = { version = "...", default-features = false, features = ["png", "egl"] }
  7. Pipe wayshot output to stdout or clipboard without built-in features

    main

    If you build wayshot without the clipboard feature, you can still pipe the image data to wl-copy:

    wayshot - | wl-copy

    Alternatively, you can configure wayshot to always write to stdout by setting stdout = true in your config.toml file.

  8. Use wayshot as a Nix Package Overlay

    main

    You can manage wayshot manually by adding its default overlay to your Nix configuration. This allows you to include wayshot in your environment.systemPackages (for system-wide installation) or home.packages (for Home Manager users).

    To toggle specific build features (like webpSupport or avifSupport), use the .override method on the package. Note that the default feature set matches the crate's defaults.

    { inputs, pkgs, ... }:
    {
      nixpkgs.overlays = [ inputs.wayshot.overlays.default ];
    
      environment.systemPackages = [ pkgs.wayshot ];
      # or in home-manager:
      # home.packages = [ pkgs.wayshot ];
    }

    Example of overriding features:

    pkgs.wayshot.override {
      webpSupport = false;
      avifSupport = false;
    }
  9. Wayshot CLI Usage Overview

    main

    Wayshot is a Wayland screenshot utility. It can capture specific outputs, windows, or screen regions, and supports various output formats and features like clipboard integration and color picking.

    Basic Capture Patterns

    • Capture a file: wayshot [FILE]
    • Capture a region: wayshot -g "x,y widthxheight" or wayshot -g "$(slurp)" (requires selector feature).
    • Capture an output: wayshot -o [OUTPUT_NAME] or wayshot --choose-output.
    • Capture a window: wayshot --toplevel [IDENTIFIER] or wayshot --choose-window.
    • Capture to stdout: wayshot - (writes raw image bytes).
  10. Define screenshot capture methods with `RegionCapturer`

    main

    The RegionCapturer enum defines the different ways a screenshot region can be specified:

    • Outputs(Vec<OutputInfo>): Captures all specified outputs.
    • Region(LogicalRegion): Captures a specific area defined by a LogicalRegion.
    • Freeze(FreezeCallback): Freezes the outputs to the user and executes a callback to determine the region. This is typically used for interactive region selection (e.g., a user dragging a box on the screen).

    The FreezeCallback type is a boxed closure: Box<dyn Fn(&WayshotConnection) -> Result<LogicalRegion>>.

    use crate::RegionCapturer;
    
    // Example of using a specific region
    let capturer = RegionCapturer::Region(my_logical_region);
    
    // Example of using the freeze callback for user interaction
    let capturer = RegionCapturer::Freeze(Box::new(|_conn| {
        // Logic to let user select a region goes here
        Ok(selected_logical_region)
    }));
  11. Configure screenshot encoding and output

    main

    wayshot determines how to save or display the captured image based on the following precedence:

    Encoding Resolution

    1. Explicit --encoding flag.
    2. Format inferred from the provided file extension.
    3. Config file [file] encoding setting.
    4. Built-in default (PNG).

    Note: If the requested encoding does not match the file extension, wayshot will prioritize the requested encoding and issue a warning.

    Output Destination

    • File: Save to a path provided via CLI or defined in the config. If a directory is provided in the config, wayshot uses it as the base.
    • Stdout: Write image bytes directly to stdout. This is triggered if the file path is set to - or if the stdout config is enabled.
    • Filename Formatting: Filenames are generated using a pattern (default: wayshot-%Y_%m_%d-%H_%M_%S). You can customize this via CLI or config.