How pig-dot-dev/piglet works
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.