# pig-dot-dev/piglet architecture

> Architecture diagram of the GitHub repository pig-dot-dev/piglet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pig-dot-dev/piglet
- Repository: https://github.com/pig-dot-dev/piglet

## Overview

##### Project Type and Purpose
- Piglet is essentially a system-level driver offering an API for desktop automation.
- Its main “business logic” is broken up into modules like display management (screenshot capture, obtaining dimensions, and streaming), input control (keyboard and mouse control), and streaming (which includes H264 encoding and RTMP output).
- The README also outlines future expansion (window management, file system, shell) but the current implementation centers around display and input control.

##### File Structure
- Top-level files such as build.zig and build.zig.zon indicate this is a Zig-based build system.
- The "src" folder is the heart of the project. Within it, different submodules such as:
- Computer.zig – likely acting as the central coordinator for the driver.
- / – contains components like Capture, Display, Encoder, Frame, and Image. There’s also a C file for interfacing logic.
- input/ – houses Keyboard and Mouse logic. Within keyboard, additional support for key sequences, events, mapping, etc.
- stream/ – implements modules like EncoderConfig, H264Encoder, and RTMPOutput for managing screen streaming.
- Other files like overlay.zig, server.zig, and tunnel.zig indicate additional components for UI overlays, server handling, and possibly tunneling remote connections.
- The assets directory holds image files (like various resolutions for a cursor), which are likely used for UI feedback or for simulating the Windows environment.

##### Insights from the README
- The README clearly defines the API endpoints, which match with the components in the file structure. For example, the display functions and input functions have direct correspondence with the modules under display/ and input/.
- It explains how the API maps to the internal modules (for screenshots, dimensions, keyboard typing, key presses, mouse moves/clicks).

a. Main Components to Include:
- API Server: Represented by server.zig; this is the network gateway through which API requests are handled.
- Core Controller (Computer.zig): Acts as the orchestrator coordinating calls to various modules.
- Module: Subdivide into components for screenshot capture, dimension fetching, encoding (Encoder, Frame, and image processing) and integration with streaming.
- Input Module: Separate blocks for keyboard (with key sequences, events, mappings) and mouse (handling positions, moves, and clicks).
- Streaming Module: Include components like H264Encoder and RTMPOutput which enable screen streaming.
- Tunnel/Overlay: These modules (tunnel.zig and overlay.zig) may handle extra connections or on-screen overlays.
- Assets: (Cursor images) used by display and possibly UI modules.
- Configuration: Represented by config.zig, which sets up operational parameters.

b. Relationships and Interactions:
- The API Server receives external API calls and routes them to the Core Controller.
- The Controller then calls specific modules: e.g., a request for a screenshot is passed to the display capture component.
- For input operations, the Controller directs keyboard and mouse API requests to the proper logic in the input module.
- The streaming component works by encoding the display feed (H264Encoder) and sending it out via RTMPOutput.
- Tunnel and overlay might interconnect with the server for remote control or enhance the display output.

c. Architectural Patterns and Design Principles:
- Modularity: Each functionality (display, input, stream) exists as separate modules, encouraging separation of concerns.
- Layered Architecture: There is a clear separation between the API layer (server), business logic (computer and modules), and the underlying hardware interaction.
- Extensibility: The planned roadmap sections suggest plug-in or expandable modules for window management, filesystem, and shell operations.

d. Relevant Technologies:
- Zig language is at the core.
- Low-level Windows API integration is implicit for desktop automation.
- For streaming, libraries supporting H264 encoding and RTMP protocols are key.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Components
    API("API Server"):::api
    CT("Core Controller"):::controller

    %% Subgraph: Display Module
    subgraph "Display Module"
        DC1("Capture"):::module
        DC2("Display Management"):::module
        DC3("Encoder"):::module
        DC4("Frame"):::module
        DC5("Image"):::module
        DC6("Display C Interop"):::module
    end

    %% Subgraph: Input Module
    subgraph "Input Module"
        IM1("Keyboard Management"):::module
        subgraph "Keyboard Subsystem"
            IMK1("KeySequence"):::subsystem
            IMK2("Key Events"):::subsystem
            IMK3("Key"):::subsystem
            IMK4("Key Mapping"):::subsystem
        end
        IM2("Mouse Control"):::module
        IM3("Input C Interop"):::module
    end

    %% Subgraph: Streaming Module
    subgraph "Streaming Module"
        SM1("Streaming Main"):::module
        SM2("H264 Encoding"):::module
        SM3("RTMP Output"):::module
        SM4("Encoder Configurations"):::module
    end

    %% Other Components
    T("Tunnel"):::module
    O("Overlay"):::module
    A("Assets"):::asset
    CFG("Configuration"):::module

    %% Connections
    API -->|"routes_to"| CT

    %% Core Controller dispatches commands
    CT -->|"display_cmd"| DC1
    CT -->|"input_cmd"| IM1
    CT -->|"input_cmd"| IM2
    CT -->|"stream_cmd"| SM1
    CT -->|"tunnel_cmd"| T
    CT -->|"overlay_cmd"| O
    CT -->|"config_access"| CFG

    %% Display Module internal flow
    DC1 -->|"process_to"| DC2
    DC2 -->|"forward_to"| DC3
    DC3 -->|"frame_output"| DC4
    DC3 -->|"image_processing"| DC5
    DC1 -->|"interop"| DC6

    %% Streaming Module integration with Display
    DC3 -->|"encodes"| SM2
    SM2 -->|"outputs_via"| SM3
    SM4 -->|"configures"| SM2

    %% Input Module internal breakdown
    IM1 -->|"handles"| IMK1
    IM1 -->|"handles"| IMK2
    IM1 -->|"handles"| IMK3
    IM1 -->|"handles"| IMK4
    CT -->|"interop"| IM3

    %% Overlay integration with Display
    O -->|"enhances"| DC2

    %% Class Definitions for Colors
    classDef api fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef controller fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef module fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef subsystem fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef asset fill:#f8d7da,stroke:#721c24,stroke-width:2px;

    %% Click Events
    click API "https://github.com/pig-dot-dev/piglet/blob/main/src/server.zig"
    click CT "https://github.com/pig-dot-dev/piglet/blob/main/src/Computer.zig"
    click DC1 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/Capture.zig"
    click DC2 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/Display.zig"
    click DC3 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/Encoder.zig"
    click DC4 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/Frame.zig"
    click DC5 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/Image.zig"
    click DC6 "https://github.com/pig-dot-dev/piglet/blob/main/src/display/c.zig"
    click IM1 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/Keyboard.zig"
    click IMK1 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/key/KeySequence.zig"
    click IMK2 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/key/events.zig"
    click IMK3 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/key/key.zig"
    click IMK4 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/key/mapping.zig"
    click IM2 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/Mouse.zig"
    click IM3 "https://github.com/pig-dot-dev/piglet/blob/main/src/input/c.zig"
    click SM1 "https://github.com/pig-dot-dev/piglet/blob/main/src/stream.zig"
    click SM2 "https://github.com/pig-dot-dev/piglet/blob/main/src/stream/H264Encoder.zig"
    click SM3 "https://github.com/pig-dot-dev/piglet/blob/main/src/stream/RTMPOutput.zig"
    click SM4 "https://github.com/pig-dot-dev/piglet/blob/main/src/stream/EncoderConfig.zig"
    click T "https://github.com/pig-dot-dev/piglet/blob/main/src/tunnel.zig"
    click O "https://github.com/pig-dot-dev/piglet/blob/main/src/overlay.zig"
    click A "https://github.com/pig-dot-dev/piglet/tree/main/src/assets"
    click CFG "https://github.com/pig-dot-dev/piglet/blob/main/src/config.zig"
```
