# def3r/haka architecture

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

- Interactive diagram: https://gitdiagram.com/def3r/haka
- Repository: https://github.com/def3r/haka

## Overview

##### Project Type and Purpose
- Haka is a native, CLI‐based user‐level daemon for Wayland that listens to global keyboard events, captures selected text, and appends it to organized note files without polluting the clipboard.
- It’s not a full-stack web app but a system utility: a monolithic C program with clearly separated layers (input Listener → core Logic → Event Handlers → Action Executors → external tools & file storage).

##### Main Components
a. Input Layer
- Linux evdev devices (/dev/input/*)
- libevdev library
b. Core Haka Daemon
- Main loop (src/haka.c)
- Configuration parser (haka.cfg via hakaUtils)
- Capability / privileges manager (setcap approach)
c. Event Dispatch & Binding
- Event handler module (include/hakaEventHandler.h + src/hakaEventHandler.c)
- Binding definitions (src/bindings.c)
d. Action Executors
- Notes file manager (openNotesFile, write/unordered-list macros)
- UI launcher for file selection (tofi)
- Clipboard integration (wl-clipboard)
- External editor launcher (neovim via configured terminal)
e. Storage
- Notes directory (file system storage)
- Configuration files (haka.cfg, tofi.cfg)
f. Service Integration
- systemd user service (generated by daemon.sh)
- Shell scripts (build.sh, daemon.sh)

##### Interactions & Data Flow
- Write to notes file (file I/O)
- If “open menu”: launch tofi (subprocess) → user picks file → return path
- If “paste selection”: invoke wl-copy / wl-paste → append text
- If “open editor”: spawn terminal → neovim on selected file

##### Architectural Patterns & Principles
- Layered design: Input → Core → Handlers → Actions → External
- Separation of concerns: config parsing, event dispatch, action implementation in separate modules
- Minimal-privilege approach: use of Linux capabilities to drop root while retaining input-group access
- Plugin-like extensibility: new handlers bound via a macro (Bind) and header/function registration

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "System & Hardware"
        EvdevDevices["/dev/input/*"]:::hardware
        Libevdev["libevdev"]:::external
    end

    subgraph "Core Daemon"
        HakaMain["Haka Main Loop"]:::core
        ConfigParserSrc["hakaUtils.c"]:::core
        ConfigParserHdr["hakaUtils.h"]:::core
        PrivMgr["Privileges Manager"]:::service
    end

    subgraph "Event Binding & Dispatch"
        EventHandlerHdr["hakaEventHandler.h"]:::core
        EventHandlerSrc["hakaEventHandler.c"]:::core
        BindDefs["bindings.c"]:::core
    end

    subgraph "Action Plugins"
        NotesManagerHdr["hakaBase.h"]:::core
        NotesManagerUtil["hakaUtils.c"]:::core
        UItofi["tofi integration"]:::external
        Clipboard["wl-clipboard"]:::external
        Editor["Editor Launcher"]:::external
    end

    subgraph "Storage"
        NotesDir[(Notes Directory)]:::storage
        HakaCfg["haka.cfg"]:::config
        TofiCfg["tofi.cfg"]:::config
    end

    subgraph "Service & Scripts"
        Build["build.sh"]:::service
        DaemonScript["daemon.sh"]:::service
    end

    EvdevDevices -->|"EV_KEY events"| Libevdev
    Libevdev -->|"input_read()"| HakaMain
    HakaMain -->|"parse Config"| ConfigParserSrc
    ConfigParserSrc --> ConfigParserHdr
    ConfigParserSrc --> HakaCfg
    HakaMain -->|"dispatch Event"| EventHandlerSrc
    EventHandlerSrc --> BindDefs
    BindDefs --> EventHandlerHdr
    EventHandlerSrc -->|"execute action"| NotesManagerUtil
    NotesManagerUtil --> NotesManagerHdr
    EventHandlerSrc -->|"spawn tofi"| UItofi
    UItofi -->|"menu selection"| NotesDir
    EventHandlerSrc -->|"invoke wl-copy/paste"| Clipboard
    EventHandlerSrc -->|"spawn editor"| Editor
    NotesManagerUtil --> NotesDir
    HakaMain -->|"systemd signal"| DaemonScript
    DaemonScript -->|"setup service"| Build

    click HakaMain "https://github.com/def3r/haka/blob/main/src/haka.c"
    click ConfigParserSrc "https://github.com/def3r/haka/blob/main/src/hakaUtils.c"
    click ConfigParserHdr "https://github.com/def3r/haka/blob/main/include/hakaUtils.h"
    click PrivMgr "https://github.com/def3r/haka/blob/main/daemon.sh"
    click EventHandlerHdr "https://github.com/def3r/haka/blob/main/include/hakaEventHandler.h"
    click EventHandlerSrc "https://github.com/def3r/haka/blob/main/src/hakaEventHandler.c"
    click BindDefs "https://github.com/def3r/haka/blob/main/src/bindings.c"
    click NotesManagerHdr "https://github.com/def3r/haka/blob/main/include/hakaBase.h"
    click UItofi "https://github.com/def3r/haka/blob/main/tofi.cfg"
    click Clipboard "https://github.com/def3r/haka/blob/main/src/hakaUtils.c"
    click Editor "https://github.com/def3r/haka/blob/main/src/hakaUtils.c"
    click HakaCfg "https://github.com/def3r/haka/blob/main/haka.cfg"
    click Build "https://github.com/def3r/haka/blob/main/build.sh"
    click DaemonScript "https://github.com/def3r/haka/blob/main/daemon.sh"

    classDef core fill:#bbdefb,stroke:#000
    classDef external fill:#c8e6c9,stroke:#000
    classDef config fill:#fff9c4,stroke:#000
    classDef service fill:#d1c4e9,stroke:#000
    classDef hardware fill:#eceff1,stroke:#000
    classDef storage fill:#dcedc8,stroke:#000
```
