Audio: hardware volume keys, chandra-ctl volume, and an OSD pill
CI / test (push) Failing after 51s

- Map VolumeUp, VolumeDown, and Mute keys to wpctl (PipeWire) or pactl, run asynchronously so the compositor never blocks
- Hold a volume key to repeat it; volume keys also work while the screen is off, without waking it
- Keep volume keys as the shutter when chandra-camera has focus; Mute still toggles system audio
- Add `chandra-ctl volume [up|down|mute|unmute|toggle|PCT|status]` and a transient Volume OSD
- Add volume-up, volume-down, and volume-mute entries to the shortcut table
This commit is contained in:
nandi committed 2026-11-28 20:01:58 -08:00
1 parent ebdd330eb5
commit 68912f6d64
11 files changed
+909 -1

No files matched your search

+232
View File
@@ -0,0 +1,232 @@
///| Hardware volume keys, async/sync mixer control, Volume OSD, screen-off routing,
/// and chandra-camera shutter pass-through.
///| Install an in-memory mock mixer on `comp` and return the recorded commands.
fn attach_mock_mixer(
comp : @host.ChandraCompositor,
init_pct? : Int = 50,
init_muted? : Bool = false,
) -> Array[@host.VolCmd] {
let cmds : Array[@host.VolCmd] = []
let pct : Ref[Int] = { val: init_pct }
let muted : Ref[Bool] = { val: init_muted }
comp.vol_runner = Some(fn(cmd : @host.VolCmd) {
cmds.push(cmd)
match cmd {
Up => {
muted.val = false
let next = pct.val + 5
pct.val = if next > 100 { 100 } else { next }
}
Down => {
let next = pct.val - 5
pct.val = if next < 0 { 0 } else { next }
}
MuteToggle => muted.val = !muted.val
SetMute(m) => muted.val = m
SetPercent(p) => pct.val = if p < 0 { 0 } else if p > 150 { 150 } else { p }
Query => ()
}
Some((pct.val, muted.val))
})
cmds
}
///|
test "parse_volume_output handles wpctl and pactl output" {
assert_eq(@host.parse_volume_output("Volume: 0.65\n"), Some((65, false)))
assert_eq(@host.parse_volume_output("Volume: 0.00 [MUTED]\n"), Some((0, true)))
assert_eq(@host.parse_volume_output("Volume: 1.00\n"), Some((100, false)))
assert_eq(@host.parse_volume_output("Volume: 1.25\n"), Some((125, false)))
assert_eq(@host.parse_volume_output("Volume: 0.506\n"), Some((51, false)))
assert_eq(@host.parse_volume_output("Volume: 0.504\n"), Some((50, false)))
assert_eq(@host.parse_volume_output("Volume: 1.99\n"), Some((150, false)))
let pa_ok = "Volume: front-left: 32768 / 50% / -18.06 dB, front-right: 32768 / 50% / -18.06 dB\nMute: no\n"
assert_eq(@host.parse_volume_output(pa_ok), Some((50, false)))
let pa_muted = "Volume: front-left: 49152 / 75% / -7.50 dB\nMute: yes\n"
assert_eq(@host.parse_volume_output(pa_muted), Some((75, true)))
assert_eq(@host.parse_volume_output(""), None)
assert_eq(@host.parse_volume_output("error no sink"), None)
assert_eq(@host.parse_volume_output("Volume: abc"), None)
}
///|
test "volume keys update state, show Volume OSD, and keep has_timed_work false" {
let comp = new_comp("vol-osd")
let cmds = attach_mock_mixer(comp, init_pct=50, init_muted=false)
comp.handle_input(@nested.InputEvent::Key(115U, true), 1U)
comp.handle_input(@nested.InputEvent::Key(115U, false), 2U)
assert_eq(cmds, [@host.VolCmd::Up])
assert_eq(comp.volume_state(), (55, false))
assert_true(comp.volume_osd_shown())
assert_false(comp.has_timed_work())
let wake = comp.wake_timeout(5000)
assert_true(wake >= 0 && wake <= 1800)
let scene = comp.compose_scene().unwrap()
assert_true(scene.panel_ops.iter().any(op => match op {
@scene.DrawOp::Text(_, _, s, _, _, _, _) => s == "Volume" || s == "55%"
_ => false
}))
comp.handle_input(@nested.InputEvent::Key(114U, true), 3U)
comp.handle_input(@nested.InputEvent::Key(114U, false), 4U)
assert_eq(comp.volume_state(), (50, false))
comp.handle_input(@nested.InputEvent::Key(113U, true), 5U)
comp.handle_input(@nested.InputEvent::Key(113U, false), 6U)
assert_eq(comp.volume_state(), (50, true))
assert_eq(cmds, [@host.VolCmd::Up, @host.VolCmd::Down, @host.VolCmd::MuteToggle])
let muted_scene = comp.compose_scene().unwrap()
assert_true(muted_scene.panel_ops.iter().any(op => match op {
@scene.DrawOp::Text(_, _, s, _, _, _, _) => s == "Muted"
_ => false
}))
comp.vol_osd = Some({ pct: 50, muted: true, until: 0UL })
comp.volume_tick()
assert_false(comp.volume_osd_shown())
assert_eq(comp.volume_wake_ms(@sys.monotonic_ms()), -1)
comp.close()
}
///|
test "volume hold-to-repeat arms on press and clears on release" {
let comp = new_comp("vol-repeat")
let cmds = attach_mock_mixer(comp, init_pct=30, init_muted=false)
comp.handle_input(@nested.InputEvent::Key(115U, true), 10U)
assert_eq(comp.vol_repeat_key, 115U)
assert_eq(comp.vol_repeat_action, Some(@shell.ShortcutAction::VolumeUp))
assert_eq(cmds.length(), 1)
// Force repeat deadline to be due and step: should enqueue a repeat Up step.
comp.vol_repeat_next_ms = @sys.monotonic_ms()
comp.volume_tick()
assert_eq(cmds, [@host.VolCmd::Up, @host.VolCmd::Up])
assert_eq(comp.volume_state(), (40, false))
// Release clears hold-to-repeat immediately.
comp.handle_input(@nested.InputEvent::Key(115U, false), 20U)
assert_eq(comp.vol_repeat_key, 0U)
assert_eq(comp.vol_repeat_action, None)
comp.close()
}
///|
test "volume keys work while screen is off without waking it or showing OSD" {
let comp = new_comp("vol-screenoff")
let cmds = attach_mock_mixer(comp, init_pct=40, init_muted=false)
// Manual screen-off (power button / chandra-ctl screen off).
comp.set_manual_screen_off(true)
assert_true(comp.manual_screen_off)
comp.handle_input(@nested.InputEvent::Key(115U, true), 10U)
comp.handle_input(@nested.InputEvent::Key(115U, false), 11U)
comp.handle_input(@nested.InputEvent::Key(114U, true), 12U)
comp.handle_input(@nested.InputEvent::Key(114U, false), 13U)
comp.handle_input(@nested.InputEvent::Key(113U, true), 14U)
comp.handle_input(@nested.InputEvent::Key(113U, false), 15U)
assert_true(comp.manual_screen_off)
assert_false(comp.volume_osd_shown())
assert_eq(cmds, [@host.VolCmd::Up, @host.VolCmd::Down, @host.VolCmd::MuteToggle])
// Idle screen-off (`screen_off = true` without `manual_screen_off`).
comp.set_manual_screen_off(false)
comp.screen_off = true
let saved_input_ms = comp.last_input_ms
comp.handle_input(@nested.InputEvent::Key(115U, true), 20U)
comp.handle_input(@nested.InputEvent::Key(115U, false), 21U)
assert_true(comp.screen_off)
assert_eq(comp.last_input_ms, saved_input_ms)
assert_false(comp.volume_osd_shown())
assert_eq(cmds.length(), 4)
comp.close()
}
///|
test "volume keys pass through to focused chandra-camera but are handled for other apps" {
let socket_path = "/tmp/wayland-moon-volcam-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap()
let cmds = attach_mock_mixer(comp, init_pct=50, init_muted=false)
let (cam, cam_shm, cam_surf, cam_xdg, _cam_tl, _cam_deco) = open_and_configure_client(
comp, socket_path, "Camera", "chandra-camera",
)
commit_shm_frame(comp, cam, cam_shm, cam_surf, cam_xdg, 1UL)
let cam_reg = 2U
let cam_seat = bind_global(cam, cam_reg, "wl_seat", 7U)
let cam_kbd = cam.alloc_id()
let wk1 = @wayland.WireWriter::new()
wk1.u32(cam_kbd)
cam.send(wk1.finish_msg(cam_seat, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(cam))
// Camera is focused: VolumeUp & VolumeDown pass through to its wl_keyboard (shutter).
comp.handle_input(@nested.InputEvent::Key(115U, true), 10U)
comp.handle_input(@nested.InputEvent::Key(115U, false), 11U)
comp.handle_input(@nested.InputEvent::Key(114U, true), 12U)
comp.handle_input(@nested.InputEvent::Key(114U, false), 13U)
assert_eq(cmds, [])
let cam_msgs = drain_msgs(cam)
assert_true(has_msg(cam_msgs, cam_kbd, 3U)) // wl_keyboard.key reached chandra-camera
// Mute still toggles system audio even while camera is focused.
comp.handle_input(@nested.InputEvent::Key(113U, true), 14U)
comp.handle_input(@nested.InputEvent::Key(113U, false), 15U)
assert_eq(cmds, [@host.VolCmd::MuteToggle])
// Open another app (`foot`) which takes focus: VolumeUp/Down now control system audio.
let (foot, foot_shm, foot_surf, foot_xdg, _foot_tl, _foot_deco) = open_and_configure_client(
comp, socket_path, "foot", "foot",
)
commit_shm_frame(comp, foot, foot_shm, foot_surf, foot_xdg, 1UL)
let foot_seat = bind_global(foot, 2U, "wl_seat", 7U)
let foot_kbd = foot.alloc_id()
let wk2 = @wayland.WireWriter::new()
wk2.u32(foot_kbd)
foot.send(wk2.finish_msg(foot_seat, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(foot))
comp.handle_input(@nested.InputEvent::Key(115U, true), 20U)
comp.handle_input(@nested.InputEvent::Key(115U, false), 21U)
assert_eq(cmds, [@host.VolCmd::MuteToggle, @host.VolCmd::Up])
assert_false(has_msg(drain_msgs(foot), foot_kbd, 3U))
comp.close()
}
///|
test "control_command volume subcommands adjust and query volume" {
let comp = new_comp("vol-ctl")
let cmds = attach_mock_mixer(comp, init_pct=50, init_muted=false)
assert_eq(comp.control_command("volume status"), (["ok 50"], true))
assert_eq(comp.control_command("volume up"), (["ok 55"], true))
assert_true(comp.volume_osd_shown())
assert_eq(comp.control_command("volume down"), (["ok 50"], true))
assert_eq(comp.control_command("volume mute"), (["ok 50 muted"], true))
assert_eq(comp.control_command("volume unmute"), (["ok 50"], true))
assert_eq(comp.control_command("volume toggle"), (["ok 50 muted"], true))
assert_eq(comp.control_command("volume 75"), (["ok 75 muted"], true))
assert_eq(comp.control_command("volume unmute"), (["ok 75"], true))
assert_eq(
comp.control_command("volume nope"),
(["error usage: volume [up|down|mute|unmute|toggle|PCT|status]"], false),
)
assert_eq(
comp.control_command("volume -5"),
(["error usage: volume [up|down|mute|unmute|toggle|PCT|status]"], false),
)
assert_eq(cmds.length(), 8)
comp.close()
}