Bar Daemon
Project Summary
- Built a system resource daemon in
RustusingTokio. - Uses
UnixStreamsockets for IPC between daemon and clients. - Push-based update system which broadcasts state changes to all listeners.
- Sends desktop notifications via
dunstifywhen 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
UnixStreamsockets. - 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
Clapfor structured command parsing. - Supports commands:
daemon,listen,get, andset. - 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, andlistencommands. - 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'sVALUE_TYPE. - Gets all values for the
RESOURCEif noVALUE_TYPEis supplied. - If no
RESOURCEis 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'sVALUE_TYPE. - For
brightnessandvolumedelta values can be provided:+10,-50. - For
brightnesspercentages are accepted:+50%.
Resource System
- All resources implement a shared
Monitoredtrait. - Polling behaviour defined via
Polledtrait. - Optional notification behaviour via
Notifytrait. - Supports interchangeable backends (e.g:
wpctl→pactl). - 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
Pipewirevolume. - 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.