# canonical/mir architecture

> Architecture diagram of the GitHub repository canonical/mir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/canonical/mir
- Repository: https://github.com/canonical/mir

## Overview

##### Project Type and Purpose
- Mir is a platform for building Wayland-based shells. It provides a set of libraries and infrastructure that abstracts and manages the hardware complexities behind graphics and input devices.
- Its goal is to offer window management, input processing (touch, mouse, tablet), multi-display support, and secure communication between client and server components. Essentially, it is the backbone for developing a compositing manager on Linux systems (and potentially beyond).

##### Analyzing the File Structure
- The file tree shows many well-organized directories:
– "src" holds the core implementation in modules such as "miral", "miroil", "server", "platform", and "gl".
– "include" mirrors the source structure (with common, core, miral, and platform directories) exposing the API and internal interfaces.
– "tests" and "tools" help in verifying, testing, and automating processes.
- The project uses CMake for build configuration, so many CMakeLists.txt files are present in each directory. This shows a modular design that is built to support multiple different subsystems.

##### Identifying the Main Components
- Server/Compositor Core: Found in directories like src/server and include/server. This is where the main event loop, scene management, and window/compositor management logic reside.
- Frontend/Protocol Layers: There are dedicated directories for frontends such as "server/frontend_wayland", "server/frontend_xwayland", and the "wayland" directory. These handle communication with Wayland clients and protocol translations.
- Input Handling: Look at directories like src/server/input, src/common/input, and the various platform-specific input implementations (evdev, libinput, for example). This layer abstracts various input devices and orchestrates event handling.
- Graphics/Rendering Layers: In directories such as src/gl, src/platform/graphics, src/renderer, and the platforms (atomic-kms, eglstream-kms, gbm-kms, etc.). They are responsible for rendering, hardware acceleration, and buffer management.
- Configuration and Abstraction Layers: With components like miral (for window management, shell interaction, and client-server behavior) and miroil (for interfacing with low-level compositor details) as well as several “configuration” files and modules.
- System Integration and Platform Abstraction: The “platform” directory and various subdirectories for different backends (X11, Wayland-specific, virtual, etc.) show the abstraction over different hardware and system components.
- Utility/Support Layers: Logging, event dispatch, thread management, and shared library loading (identified in the "common" directories) are key cross-cutting concerns.

##### Relationships and Interactions
– Client applications (via the Wayland protocol) communicate with the compositor’s frontend layers (Wayland, XWayland) which then interface with the compositor/core.
– The compositor core handles events (input, window management, configuration changes) and dispatches rendering calls to the appropriate graphics abstraction layer.
– The graphics layer communicates with platform-specific components to render content (e.g., atomic-kms, gbm-kms, egl, etc.).
– Input devices are abstracted through a dedicated input manager which collects events from multiple sources (evdev, libinput), passes them to the compositor, and then further to application clients.

##### Architectural Patterns and Design Principles
- Modular Design: Each major subsystem (input, rendering, protocol handling, configuration) is isolated into its own directory and set of libraries. This separation supports easier testing, maintenance, and scalability.
- Abstraction and Hardware Independence: The design encapsulates hardware-specific logic in platform-specific modules, allowing the core compositor logic to remain hardware agnostic.
- Event-Driven Architecture: The compositor and input layers are event-driven, clearly visible in directories handling event dispatch and message queues.
- Client/Server Model: A central idea of separate client applications interacting with a compositor server (which then delegates to the window manager/shell) should be illustrated.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Layer"
        Clients("Wayland Clients"):::client
    end

    %% Frontend/Protocol Layer
    subgraph "Frontend/Protocol Layer"
        WaylandFE("Wayland Frontend"):::frontend
        XWaylandFE("XWayland Frontend"):::frontend
        WaylandProtocol("Wayland Protocol Interfaces"):::frontend
    end

    %% Core/Server Layer
    subgraph "Core/Server Layer"
        CoreSrc("Server Source"):::core
        CoreHdr("Server Headers"):::core
        InputSrc("Input Sources"):::input
        InputHdr("Input Headers"):::input
    end

    %% Configuration & Abstraction Layer
    subgraph "Configuration & Abstraction Layer"
        MiralSrc("High-level API (miral) Source"):::config
        MiralHdr("High-level API (miral) Headers"):::config
        MiroilSrc("Low-level Integration (miroil) Source"):::config
        MiroilHdr("Low-level Integration (miroil) Headers"):::config
    end

    %% Hardware Abstraction/Graphics Layer
    subgraph "Hardware Abstraction/Graphics Layer"
        GL("Core Rendering/GL"):::graphics
        RendererLib("Renderer Libraries"):::graphics
        Platform("Platform Implementations"):::graphics
    end

    %% Utility/Support Layer
    subgraph "Utility/Support Layer"
        Utilities("Utilities/Logging/Threading"):::utility
    end

    %% Connections from Clients to Frontend
    Clients -->|"interacts"| WaylandFE
    Clients -->|"interacts"| XWaylandFE
    Clients -->|"interacts"| WaylandProtocol

    %% Frontend to Core/Server Layer
    WaylandFE -->|"forwards"| CoreSrc
    XWaylandFE -->|"forwards"| CoreSrc
    WaylandProtocol -->|"forwards"| CoreSrc

    %% Internal Core/Server connections
    CoreSrc -->|"integrates"| CoreHdr
    CoreSrc -->|"processes"| InputSrc
    InputSrc -->|"defines"| InputHdr

    %% Core to Hardware/Graphics Layer
    CoreSrc -->|"dispatches"| GL
    GL -->|"utilizes"| RendererLib
    RendererLib -->|"interfaces"| Platform

    %% Core to Configuration & Abstraction Layer
    CoreSrc -->|"configures"| MiralSrc
    MiralSrc -->|"exposes"| MiralHdr
    MiralHdr -->|"hooksInto"| MiroilSrc
    MiroilSrc -->|"integrates"| MiroilHdr

    %% Utility support connections
    CoreSrc -->|"logsVia"| Utilities
    InputSrc -->|"logsVia"| Utilities
    GL -->|"logsVia"| Utilities
    MiralSrc -->|"logsVia"| Utilities
    Platform -->|"logsVia"| Utilities

    %% Click Events
    click CoreSrc "https://github.com/canonical/mir/tree/main/src/server"
    click CoreHdr "https://github.com/canonical/mir/tree/main/include/server/mir"
    click WaylandFE "https://github.com/canonical/mir/tree/main/src/server/frontend_wayland"
    click XWaylandFE "https://github.com/canonical/mir/tree/main/src/server/frontend_xwayland"
    click WaylandProtocol "https://github.com/canonical/mir/tree/main/include/wayland/mir/wayland"
    click InputSrc "https://github.com/canonical/mir/tree/main/src/server/input"
    click InputHdr "https://github.com/canonical/mir/tree/main/include/server/mir/input"
    click GL "https://github.com/canonical/mir/tree/main/src/gl"
    click RendererLib "https://github.com/canonical/mir/tree/main/src/renderer"
    click RendererLib "https://github.com/canonical/mir/tree/main/src/renderers"
    click MiralSrc "https://github.com/canonical/mir/tree/main/src/miral"
    click MiralHdr "https://github.com/canonical/mir/tree/main/include/miral/miral"
    click MiroilSrc "https://github.com/canonical/mir/tree/main/src/miroil"
    click MiroilHdr "https://github.com/canonical/mir/tree/main/include/miroil/miroil"
    click Platform "https://github.com/canonical/mir/tree/main/src/platform"
    click Platform "https://github.com/canonical/mir/tree/main/src/platforms"
    click Utilities "https://github.com/canonical/mir/tree/main/src/common"
    click Utilities "https://github.com/canonical/mir/tree/main/include/common/mir"

    %% Styles
    classDef client fill:#aqua,stroke:#333,stroke-width:2px;
    classDef frontend fill:#b2f2bb,stroke:#333,stroke-width:2px;
    classDef core fill:#ffadad,stroke:#333,stroke-width:2px;
    classDef input fill:#ffd6a5,stroke:#333,stroke-width:2px;
    classDef config fill:#caffbf,stroke:#333,stroke-width:2px;
    classDef graphics fill:#9bf6ff,stroke:#333,stroke-width:2px;
    classDef utility fill:#d0afff,stroke:#333,stroke-width:2px;
```
