feat: HushKey 1.0.0 — per-app mute hotkeys for Windows, macOS and Linux
Native Rust tray app that binds global shortcuts to mute/unmute individual applications' audio, plus a Mute All / Restore All shortcut. - AudioController trait with per-OS backends: WASAPI sessions (Windows), Core Audio process taps (macOS 14.2+, mute only), PulseAudio/PipeWire sink inputs (Linux) - Dedicated audio thread; hotkey presses go straight to it - Snapshot-based Mute All that restores exactly the previous state - Slint UI with software renderer; window destroyed on close - Non-activating on-screen confirmation and tray icon flash - TOML config, launch at login, start minimized, dark/light theme - Procedurally drawn icon embedded into the Windows executable - Docs, AGENTS.md, CLAUDE.md, contributing guide and CI for all three OSes
This commit is contained in:
commit
8f476dbe52
40 files changed
+11604
No files matched your search
@@ -0,0 +1,100 @@
|
||||
# Architecture
|
||||
|
||||
HushKey is a single Rust binary with four layers. Only `src/audio/{windows,macos,linux}.rs` contain OS-specific audio code, and everything above the `AudioController` trait is platform-agnostic.
|
||||
|
||||
```
|
||||
┌──────────────────────────────── main thread (Slint event loop) ─────────────────────────────┐
|
||||
│ app.rs ── state, config, UI wiring ──┬── ui/ (Slint window, toast, tray, icon) │
|
||||
│ ├── config/ (TOML load/save) │
|
||||
│ └── hotkeys/ (register / suspend / capture parsing) │
|
||||
└───────────────▲───────────────────────────────────┬─────────────────────────────────────────┘
|
||||
│ AudioEvent (invoke_from_event_loop)│ AudioCommand (mpsc)
|
||||
┌───────────────┴──────────── audio thread ─────────▼─────────────────────────────────────────┐
|
||||
│ audio/worker.rs → MuteManager (toggle / mute-all state) → Box<dyn AudioController> │
|
||||
│ ├─ windows.rs WASAPI sessions │
|
||||
│ ├─ macos.rs process taps │
|
||||
│ └─ linux.rs PulseAudio │
|
||||
└──────────────────────────────────────────────────────────────────────────────────────────────┘
|
||||
▲
|
||||
│ AudioCommand sent directly from the OS hotkey callback (no UI hop)
|
||||
global-hotkey handler
|
||||
```
|
||||
|
||||
## Source layout
|
||||
|
||||
| Path | Responsibility |
|
||||
|---|---|
|
||||
| `src/main.rs` | CLI flags, single-instance guard, Slint backend selection, event loop |
|
||||
| `src/app.rs` | Main-thread controller: owns config, hotkeys, tray, toast, window; reacts to `AudioEvent`s |
|
||||
| `src/audio/mod.rs` | `AudioController` trait, `AudioApp`, `AppKey`, errors, `create_controller()` |
|
||||
| `src/audio/mute.rs` | `MuteManager`: toggle and Mute All / Restore All semantics (unit-tested) |
|
||||
| `src/audio/worker.rs` | Audio thread, command/event enums |
|
||||
| `src/audio/windows.rs` | Windows backend (Core Audio / WASAPI) |
|
||||
| `src/audio/macos.rs` | macOS backend (Core Audio process taps) |
|
||||
| `src/audio/linux.rs` | Linux backend (libpulse, works with PipeWire) |
|
||||
| `src/audio/null.rs` | In-memory backend for tests and unsupported OSes |
|
||||
| `src/hotkeys/accelerator.rs` | `Accelerator` type, string format, Slint key-event → shortcut (unit-tested) |
|
||||
| `src/hotkeys/mod.rs` | `Hotkeys`: registers config bindings with the OS, dispatches presses |
|
||||
| `src/config/mod.rs` | `Config`, `ConfigStore` (atomic TOML writes, corrupt-file recovery) |
|
||||
| `src/ui/` | Tray (`tray.rs`), toast popup (`toast.rs`), procedural icon (`icon.rs`) |
|
||||
| `src/platform.rs` | Launch-at-login, open folder, single-instance socket |
|
||||
| `ui/*.slint` | Declarative UI, compiled into Rust by `build.rs` |
|
||||
|
||||
## The `AudioController` trait
|
||||
|
||||
```rust
|
||||
pub trait AudioController {
|
||||
fn backend_name(&self) -> &'static str;
|
||||
fn capabilities(&self) -> Capabilities; // per_app_volume / per_app_mute
|
||||
fn list_audio_apps(&mut self) -> AudioResult<Vec<AudioApp>>;
|
||||
fn set_app_volume(&mut self, app: &str, volume: f32) -> AudioResult<()>;
|
||||
fn set_app_mute(&mut self, app: &str, muted: bool) -> AudioResult<()>;
|
||||
fn mute_all(&mut self) -> AudioResult<MuteSnapshot>; // default impl
|
||||
fn restore_all(&mut self, snapshot: &MuteSnapshot) -> AudioResult<()>; // default impl
|
||||
fn app_icon(&mut self, app: &AudioApp) -> Option<IconData> { None }
|
||||
fn platform_note(&self) -> Option<&'static str> { None }
|
||||
}
|
||||
```
|
||||
|
||||
Key design points:
|
||||
|
||||
- **Apps are identified by a stable `AppKey`, not by PID.** Bindings must survive restarts, so the key is the executable name on Windows (`spotify.exe`), the process binary on Linux (`firefox`), and the outermost `.app` bundle on macOS (`google chrome.app`). Every session or stream that shares a key is controlled together, so "mute Chrome" mutes every tab and helper process.
|
||||
- **Mute uses the mixer's mute flag, not volume 0.** The OS keeps the user's level. Nothing is lost if HushKey crashes while an app is muted. On macOS the taps die with the process, so muted apps come back automatically.
|
||||
- **`mute_all` returns a snapshot** of what *it* muted. `restore_all` only unmutes those apps, so apps the user had already muted stay muted. `MuteManager` keeps that snapshot and adjusts it when the user toggles an app by hand during Mute All.
|
||||
- **Backends are not `Send`.** They are built *on* the audio thread by `create_controller()`. That allows thread-affine handles: COM objects in the MTA, the PulseAudio mainloop, Core Audio tap IDs.
|
||||
|
||||
## Threads
|
||||
|
||||
| Thread | What runs there | Idle cost |
|
||||
|---|---|---|
|
||||
| main | Slint event loop, tray, hotkey registration, toast | blocked in the OS message loop |
|
||||
| `hushkey-audio` | backend + `MuteManager` | blocked on its channel (polls every 2 s only while Mute All is engaged, to catch late-starting apps) |
|
||||
| `hushkey-instance` | accepts "show window" pings from second launches | blocked in `accept()` (64 KB stack) |
|
||||
| OS / library threads | X11 hotkey thread (Linux), D-Bus tray (Linux), winit helpers | idle |
|
||||
|
||||
### Hotkey press path
|
||||
|
||||
1. The OS delivers the hotkey: `WM_HOTKEY` on Windows, a Carbon event on macOS, an X11 grab on Linux.
|
||||
2. The `global-hotkey` callback looks up the hotkey ID and sends `AudioCommand::ToggleApp { key, from_hotkey: true }` straight to the audio thread.
|
||||
3. The audio thread toggles the app (about 5–20 ms on Windows) and emits `AudioEvent::Toggled`.
|
||||
4. The main thread shows the toast and flashes the tray icon.
|
||||
|
||||
The UI thread is not on the critical path, so a busy UI can't delay muting.
|
||||
|
||||
### Recording a shortcut
|
||||
|
||||
While a chip is in "Press keys…" mode, `Hotkeys::suspend()` unregisters every global hotkey. Otherwise the OS would swallow a combo that's already bound and the window would never see it. The key event goes through `capture_from_key_event`, which maps Slint key text to physical key codes and enforces the "letters need a modifier" rule. Hotkeys are re-registered when the capture ends.
|
||||
|
||||
## Memory strategy
|
||||
|
||||
- **Software renderer** (`renderer-software`) draws into a CPU buffer, with no GPU context or driver allocations.
|
||||
- **The window is destroyed on close**, not just hidden (`App::release_window`). The rows model, icons and settings live in `App`, so reopening the window rebuilds the UI instantly.
|
||||
- The toast window is created on demand and dropped after it fades out.
|
||||
- App icons are extracted once per app (64×64 RGBA, about 16 KB each) and cached.
|
||||
|
||||
## Adding a backend
|
||||
|
||||
1. Create `src/audio/<os>.rs` implementing `AudioController`. Document limitations in its module comment and in `platform_note()`.
|
||||
2. Add a `cfg` arm to `create_controller()` in `src/audio/mod.rs`.
|
||||
3. Add platform dependencies under `[target.'cfg(...)'.dependencies]` in `Cargo.toml`.
|
||||
4. Update the capability table in `README.md` and `docs/design-decisions.md`.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
@@ -0,0 +1,95 @@
|
||||
# Building HushKey
|
||||
|
||||
All platforms use the stable Rust toolchain (MSRV 1.82, check `rust-version` in `Cargo.toml`). Install it with [rustup](https://rustup.rs).
|
||||
|
||||
```bash
|
||||
cargo build # debug build, console logs visible
|
||||
cargo build --release # optimized: fat LTO, stripped, panic=abort
|
||||
cargo test # unit tests (config, accelerators, mute logic, icon)
|
||||
```
|
||||
|
||||
Logging uses `env_logger`. Set `RUST_LOG=hushkey=debug` for more detail. Release builds on Windows have no console, so redirect output to a file if you need logs: `hushkey.exe > log.txt 2>&1`.
|
||||
|
||||
Diagnostics: `hushkey --list` prints every detected audio app with its key, volume and mute state.
|
||||
|
||||
---
|
||||
|
||||
## Windows
|
||||
|
||||
**Requirements:** Windows 10 1809+ or Windows 11, plus the MSVC toolchain (Visual Studio Build Tools with the "Desktop development with C++" workload).
|
||||
|
||||
```powershell
|
||||
cargo build --release
|
||||
.\target\release\hushkey.exe
|
||||
```
|
||||
|
||||
- No runtime dependencies and no installer needed. The exe is self-contained.
|
||||
- Config: `%APPDATA%\HushKey\config\config.toml`
|
||||
- Launch at login writes `HKCU\Software\Microsoft\Windows\CurrentVersion\Run\HushKey`.
|
||||
- **Gotcha:** if a game runs elevated (as administrator) and HushKey does not, hotkeys still fire, but some anti-cheat systems block other processes' audio sessions from being changed. Running HushKey elevated is a workaround, not a requirement.
|
||||
|
||||
## macOS
|
||||
|
||||
**Requirements:** macOS 14.2+ for per-app muting (older versions list apps read-only), plus the Xcode Command Line Tools (`xcode-select --install`).
|
||||
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
### Permissions and bundling
|
||||
|
||||
Per-app muting uses Core Audio process taps, which are gated by the **Screen & System Audio Recording** permission. macOS only shows the permission prompt for an **app bundle** whose `Info.plist` contains a usage string:
|
||||
|
||||
```xml
|
||||
<key>NSAudioCaptureUsageDescription</key>
|
||||
<string>HushKey mutes individual apps by attaching a muted audio tap to them. No audio is recorded.</string>
|
||||
<key>LSUIElement</key>
|
||||
<true/>
|
||||
```
|
||||
|
||||
`LSUIElement` hides the Dock icon (HushKey also sets the accessory activation policy at runtime). One way to bundle is [`cargo-bundle`](https://github.com/burtonageo/cargo-bundle). Add the keys above to the generated plist.
|
||||
|
||||
If muting fails with a permission message, open **System Settings → Privacy & Security → Screen & System Audio Recording** and enable HushKey. Global hotkeys use the Carbon API and need **no** Accessibility permission.
|
||||
|
||||
- Config: `~/Library/Application Support/HushKey/config.toml`
|
||||
- Launch at login installs a LaunchAgent in `~/Library/LaunchAgents/`.
|
||||
|
||||
## Linux
|
||||
|
||||
**Requirements:** a PulseAudio server, or PipeWire with `pipewire-pulse` (the default on Fedora 34+, Ubuntu 22.10+, Debian 12+ and Arch). An X11 session is needed for global hotkeys; see Wayland below.
|
||||
|
||||
Build dependencies:
|
||||
|
||||
| Distro | Command |
|
||||
|---|---|
|
||||
| Debian / Ubuntu | `sudo apt install build-essential pkg-config libpulse-dev libfontconfig1-dev libxkbcommon-dev` |
|
||||
| Fedora | `sudo dnf install gcc pkgconf-pkg-config pulseaudio-libs-devel fontconfig-devel libxkbcommon-devel` |
|
||||
| Arch | `sudo pacman -S base-devel pkgconf libpulse fontconfig libxkbcommon` |
|
||||
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
- Tray: StatusNotifierItem over D-Bus. KDE, XFCE, Cinnamon and others support it out of the box. On GNOME, install the *AppIndicator and KStatusNotifierItem Support* extension. Without a tray, HushKey keeps its window open so it stays reachable.
|
||||
- Config: `$XDG_CONFIG_HOME/hushkey/config.toml` (usually `~/.config/hushkey/`)
|
||||
- Launch at login writes `~/.config/autostart/HushKey.desktop`.
|
||||
|
||||
### Wayland
|
||||
|
||||
Wayland has no protocol that lets an app grab global shortcuts. Under a Wayland session HushKey's hotkeys only fire while an XWayland window has focus. Workarounds:
|
||||
|
||||
1. Log into an X11 session, **or**
|
||||
2. Bind the shortcut in your desktop's own keyboard settings to a command. This is a known gap: HushKey has no CLI toggle command yet. Contributions welcome (see [CONTRIBUTING.md](../CONTRIBUTING.md)).
|
||||
|
||||
---
|
||||
|
||||
## Cross-checking other platforms
|
||||
|
||||
From any OS you can type-check the macOS backend without a Mac:
|
||||
|
||||
```bash
|
||||
rustup target add aarch64-apple-darwin
|
||||
cargo check --target aarch64-apple-darwin
|
||||
```
|
||||
|
||||
Cross-checking Linux from Windows/macOS fails inside native font dependencies (`fontconfig`/`pkg-config`). Use CI, a container, or WSL with the build dependencies above.
|
||||
@@ -0,0 +1,82 @@
|
||||
# Design decisions
|
||||
|
||||
## UI toolkit: Slint, not Tauri / egui / iced
|
||||
|
||||
HushKey is a utility that runs all day in the tray. Idle footprint and responsiveness matter more than anything else.
|
||||
|
||||
| Option | Why not / why |
|
||||
|---|---|
|
||||
| **Tauri** | Ships a system WebView (WebView2 / WebKitGTK). Even an idle window costs tens of MB of RAM across several helper processes. Rejected: too heavy for an always-on tray utility. |
|
||||
| **egui** | Immediate mode: redraws the whole UI each frame while interacting, and usually runs on a GPU context (wgpu/glow) with its driver allocations. Its default look is a "developer tool" aesthetic. |
|
||||
| **iced** | Good architecture, but pulls in wgpu by default and is heavier to theme for this kind of polished card UI. |
|
||||
| **Slint** ✅ | Declarative `.slint` markup compiled to native Rust at build time. Retained mode: only dirty regions repaint. Has a **software renderer**, so no GPU context or driver memory. Animations, rounded corners, shadows and gradients are built in, giving a modern look cheaply. Exposes winit hooks, which we need for the non-activating toast window. |
|
||||
|
||||
Licensing: Slint is GPLv3 / Royalty-free / commercial. HushKey (MIT) uses it under the Royalty-free license, which requires attribution, shown in the app's About section. See [LICENSE.md](../LICENSE.md).
|
||||
|
||||
Further memory choices:
|
||||
|
||||
- The main window is **destroyed** on close and rebuilt on demand. All state lives in `App`.
|
||||
- The toast is created per notification and dropped after it fades.
|
||||
- Nothing polls while hidden. The audio thread blocks on its channel, except for a 2-second check while Mute All is engaged.
|
||||
|
||||
## License: MIT
|
||||
|
||||
MIT is the norm in the Rust ecosystem: most of our dependencies are MIT/Apache-2.0. It's short and lets anyone package or fork the app. Apache-2.0's patent grant matters little for a small desktop utility with no novel algorithms, so the simpler license wins.
|
||||
|
||||
## Mute flag instead of "volume → 0"
|
||||
|
||||
The original spec described muting as "set volume to 0, restore the previous volume". HushKey uses the OS mixer's **mute flag** instead, on every platform that has one:
|
||||
|
||||
- The user's volume level is preserved by the OS itself, so there's no saved state to lose.
|
||||
- If HushKey crashes or is killed while apps are muted, nothing is stuck at 0%. The user unmutes in the system mixer as usual. On macOS, taps are released automatically when the process exits.
|
||||
- The OS mixer UI shows the app as muted, which matches what the user sees in HushKey.
|
||||
|
||||
`set_app_volume` is still implemented on Windows and Linux as part of the backend contract.
|
||||
|
||||
## Stable app identity
|
||||
|
||||
Bindings are keyed by executable name / bundle, never by PID, so they survive app and HushKey restarts. All sessions or streams of one app are controlled together. Bound apps that aren't running stay listed (greyed out) so their shortcut can still be edited.
|
||||
|
||||
## Shortcut rules
|
||||
|
||||
- Shortcuts use **physical key codes** (what the OS hotkey APIs register). On non-US layouts, the stored key is the US-layout position.
|
||||
- Letters, digits, punctuation, Space, Enter and arrows **require Ctrl, Alt or Win/Cmd**, so a binding can't hijack normal typing. Shift alone doesn't count. F1–F24, Pause, Scroll Lock and Insert may be used alone.
|
||||
- While recording a shortcut, all global hotkeys are temporarily unregistered so the combo reaches the window.
|
||||
- Conflicts with another HushKey binding are rejected up front. Conflicts with other programs are reported when registration fails.
|
||||
|
||||
## Feedback without stealing focus
|
||||
|
||||
The toast window is created with winit `active = false`, no decorations, always-on-top, skip-taskbar (Windows) and cursor hit-testing disabled. Taking focus would minimize exclusive-fullscreen games and interrupt typing. Games in true exclusive fullscreen draw over every window, so the toast can't show there. The tray icon still flashes and the result is still applied. Most modern games use borderless fullscreen, where the toast is visible.
|
||||
|
||||
---
|
||||
|
||||
## Platform limitations
|
||||
|
||||
### macOS per-app audio
|
||||
|
||||
macOS has **no public API to change another process's volume**. The CoreAudio HAL exposes device volume only. The established approaches:
|
||||
|
||||
1. **Virtual audio driver** (SoundSource, Background Music, eqMac): route all audio through a HAL plug-in that mixes per-app. This needs an installer, admin rights and a system extension. It's invasive, fragile across macOS updates, and far beyond a hotkey utility.
|
||||
2. **Core Audio process taps** (`AudioHardwareCreateProcessTap`, macOS 14.2+): a tap created with `muteBehavior = CATapMuted` silences the tapped process while the tap exists.
|
||||
|
||||
**HushKey uses option 2.** As a result:
|
||||
|
||||
- ✅ Per-app **mute/unmute** works (that's all HushKey's shortcuts need).
|
||||
- ❌ Per-app **volume levels** are not possible. The backend reports `per_app_volume: false`, the UI hides volume bars, and `set_app_volume` returns `Unsupported` for values between 0 and 1. It never fakes a level.
|
||||
- Requires **macOS 14.2+**. The tap functions are resolved with `dlsym` at runtime, so the binary still launches on older macOS. There, apps are listed read-only and a banner explains why.
|
||||
- Requires the **Screen & System Audio Recording** permission. The app bundle must declare `NSAudioCaptureUsageDescription` (see [building.md](building.md#macos)).
|
||||
- Apps are grouped by their outermost `.app` bundle, so helper processes (e.g. Chrome's renderer helpers) are muted together. When an app spawns a new helper while muted, the tap is rebuilt on the next refresh.
|
||||
|
||||
> ⚠️ The macOS backend was written and compile-checked (`cargo check --target aarch64-apple-darwin`) but **not yet run on real hardware**. Please report results.
|
||||
|
||||
### Wayland global shortcuts
|
||||
|
||||
Wayland deliberately has no API for one client to grab keys globally. The `global-hotkey` crate uses X11 `XGrabKey`, which under Wayland only sees XWayland clients. The XDG `GlobalShortcuts` portal exists but isn't supported by the hotkey crate and isn't implemented by every compositor. Registration appears to succeed through XWayland but never fires for native Wayland windows, so HushKey checks `XDG_SESSION_TYPE` at startup and shows a warning banner. A future version could adopt the portal and add a CLI (`hushkey toggle <app>`) for desktop-level bindings.
|
||||
|
||||
### Linux tray on GNOME
|
||||
|
||||
GNOME Shell doesn't show StatusNotifierItem icons without an extension. If no tray host is found, HushKey keeps its window open so the app stays reachable.
|
||||
|
||||
### Windows
|
||||
|
||||
The Windows backend is fully native. Sessions are enumerated on every active output device, not just the default one, so apps routed to a headset or virtual device are found. Protected processes HushKey can't open are still controllable; they're listed as "Process <pid>" with a PID-based key, which can't be bound persistently.
|
||||
Reference in new issue
Block a user