Bar Daemon

Rust Rust
Tokio Tokio

Project Summary

  • Built a system resource daemon in Rust using Tokio.
  • Uses UnixStream sockets for IPC between daemon and clients.
  • Push-based update system which broadcasts state changes to all listeners.
  • Sends desktop notifications via dunstify when resources change.
  • Default polling interval of 2.0s for resources that require polling.

Technical Challenges

  • Designing a push-based system to eliminate client-side polling.
  • Maintaining consistent state across multiple concurrent listeners.
  • Balancing polling and event-driven updates across different resource types.
  • Broadcasting full system state efficiently without excessive overhead.
  • Ensuring reliable IPC communication using UnixStream sockets.
  • Separating data acquisition from transformation, for increased maintainability.
  • Designing a modular system to support multiple interchangable data sources.

Daemon Architecture

  • Central daemon handles all resource monitoring and state changes.
  • Pushes full state as JSON to all connected listeners, triggered when any resource changes.
  • Combines event-driven updates with interval-based polling.
  • Listeners receive updates instantly without polling delays.

Command Line Interface

  • Built using Clap for structured command parsing.
  • Supports commands: daemon, listen, get, and set.
  • Full help support for all commands and subcommands.
  • Provides aliases for faster command usage.
  • Usage:
    Command Description
    bar_daemon daemon
    • Runs the daemon.
    • Only allows one daemon to be active on the socket at a time.
    • Will respond to get, set, and listen commands.
    • Outputs errors and logs to a file.
    bar_daemon listen
    • Listens for changes in resources.
    • Receives JSON output of all resources when there's a change.
    bar_daemon get [RESOURCE] [VALUE_TYPE]
    bar_daemon get volume percent
    bar_daemon get ram icon
    bar_daemon get brightness
    • Gets the value of the RESOURCE's VALUE_TYPE.
    • Gets all values for the RESOURCE if no VALUE_TYPE is supplied.
    • If no RESOURCE is provided, gets all values for all resources.
    bar_daemon set <RESOURCE> <VALUE_TYPE> <VALUE>
    bar_daemon set volume percent -20
    bar_daemon set fan profile next
    bar_daemon set brightness monitor 50
    • Sets the value of the requested RESOURCE's VALUE_TYPE.
    • For brightness and volume delta values can be provided:
      +10, -50.
    • For brightness percentages are accepted: +50%.

Resource System

  • All resources implement a shared Monitored trait.
  • Polling behaviour defined via Polled trait.
  • Optional notification behaviour via Notify trait.
  • Supports interchangeable backends (e.g: wpctlpactl).
  • Separates logic into:
    • source.rs → data acquisition.
    • value.rs → data processing and representation.

Monitored Resources

  • Mix of event-driven and polled resources, depending on resource acquisition method.
  • Supports multiple system resources:
    Resource System Command Description Notifies Polled
    Volume wpctl
    • Monitors volume state.
    • Controls volume state.
    • Perceptual scaling applied.
    Yes No
    Brightness brightnessctl
    • Uses hard-coded device IDs for now.
    • Monitors display and keyboard brightness.
    • Controls display and keyboard brightness.
    Yes No
    Battery acpi
    • Polls battery state.
    • Provides threshold-based notifications.
    Yes Yes
    Bluetooth bluetooth
    (bluetoothctl)
    • Monitors Bluetooth state.
    • Controls Bluetooth state.
    Yes No
    Fan Speed asusctl
    • Monitors fan profile.
    • Controls fan profile.
    Yes No
    Memory free
    • Polls system RAM usage.
    No Yes

Data Flow

  • Resource updates trigger full-state JSON broadcast.
  • Listeners receive complete system snapshot on each update.
  • Simplifies client logic by avoiding partial updates.
  • Ensures all clients remain synchronised.

Volume Scaling (Perceptual Scaling)

  • Implements perceptual (logarithmic) volume scaling.
  • Fixes non-linear behaviour of Pipewire volume.
  • Uses square root mapping for consistent perceived increments.
  • Maintains user-facing range of [0-100].

Current Limitations

  • Brightness device IDs are currently hardcoded.
  • Full-state broadcasts may be inefficient for high-frequency updates.

Phone

I am not comfortable putting my personal phone number on this public site, please use my email, or contact via LinkedIn.

Address

Newcastle Upon Tyne, United Kingdom