From 81cc983aaec80608e41330c93703119a05a95806 Mon Sep 17 00:00:00 2001 From: Tom Ballard Date: Tue, 1 Dec 2026 18:32:51 +0000 Subject: [PATCH 1/2] mute-switch: back off on missing audio sink and curb idle wakeups When no default audio sink exists (boards without audio brought up yet), omarchy-mute-switch retried wpctl every 2 s forever, burning CPU and writing hundreds of kilobytes of identical 'Translate ID error: -1' lines to the RAM-backed runtime log. It also scanned /sys/class/input every 2 s and woke every 1 s to check compositor liveness. Back off consecutive set_mute failures exponentially (2 s up to 60 s, reset immediately on any switch move or success), deduplicate repeated stderr lines, and lengthen idle wake/keyboard-scan intervals to 5 s / 10 s. Assisted-by: LLM --- desktop/configuration/mute-switch | 39 +++++++++++++++++++++++++------ tests/test_mute_switch.py | 33 +++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/desktop/configuration/mute-switch b/desktop/configuration/mute-switch index ebeefed2..0c909a04 100755 --- a/desktop/configuration/mute-switch +++ b/desktop/configuration/mute-switch @@ -42,6 +42,9 @@ KEY_BYTES = 96 # KEY_CNT / 8 EVIOCGKEY = 2 << 30 | KEY_BYTES << 16 | ord('E') << 8 | 0x18 # _IOC(_IOC_READ, 'E', 0x18, len) SETTLE = 0.3 # seconds after the last change before the switch is read back RETRY = 2.0 +MAX_RETRY = 60.0 # cap failure backoff (e.g. boards with no audio sink yet) +KEYS_SCAN = 10.0 # seconds between scans for newly connected keyboards +IDLE_WAIT = 5.0 # seconds between compositor-liveness checks when idle def log(*args): @@ -112,10 +115,11 @@ class Follower: are no change of position. """ - def __init__(self, settle=SETTLE, retry=RETRY): - self.settle, self.retry = settle, retry + def __init__(self, settle=SETTLE, retry=RETRY, max_retry=MAX_RETRY): + self.settle, self.retry, self.max_retry = settle, retry, max_retry self.applied = None # the mute last set; None before the first self.due = 0.0 # when to read the switch: at once at the start + self.failures = 0 def feed(self, data, now): """The events in evdev bytes.""" @@ -123,6 +127,7 @@ class Follower: for _, _, kind, code, value in EVENT.iter_unpack(data[:whole]): if (kind == EV_KEY and code == KEY_MUTE and value in (0, 1)) or (kind == EV_SYN and code == SYN_DROPPED): self.due = now + self.settle + self.failures = 0 def wait(self, now): """Seconds until the switch is to be read, or None.""" @@ -137,9 +142,12 @@ class Follower: def done(self, muted): self.applied = muted + self.failures = 0 def failed(self, now): - self.due = now + self.retry + delay = min(self.max_retry, self.retry * (2 ** self.failures)) + self.failures += 1 + self.due = now + delay def presses(data): @@ -160,16 +168,33 @@ def toggle_mute(): return False +_last_mute_error = None + + def set_mute(muted, quiet): """Mute or unmute the default output; with the OSD unless QUIET.""" + global _last_mute_error plain = ['wpctl', 'set-mute', '@DEFAULT_AUDIO_SINK@', '1' if muted else '0'] tries = [plain] if quiet else [['omarchy-volume', 'mute-on' if muted else 'mute-off'], plain] + last_err = None for argv in tries: try: - if subprocess.run(argv, stdin=subprocess.DEVNULL, timeout=10).returncode == 0: + result = subprocess.run( + argv, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, + text=True, timeout=10, + ) + if result.returncode == 0: + _last_mute_error = None return True + msg = (result.stderr or '').strip() + if msg: + last_err = f'{argv[0]}: {msg}' except (OSError, subprocess.SubprocessError) as error: - log(f'{argv[0]}: {error}') + last_err = f'{argv[0]}: {error}' + if last_err and last_err != _last_mute_error: + log(last_err) + _last_mute_error = last_err return False @@ -266,9 +291,9 @@ def main(): next_scan = now + RETRY if devices and now >= next_keys: follow_keyboards() - next_keys = now + RETRY + next_keys = now + KEYS_SCAN wait = follower.wait(now) if devices else None - timeout = 1.0 if wait is None else min(1.0, wait) + timeout = IDLE_WAIT if wait is None else min(IDLE_WAIT, wait) if not devices: time.sleep(timeout) continue diff --git a/tests/test_mute_switch.py b/tests/test_mute_switch.py index 5395ea27..7aad69c9 100644 --- a/tests/test_mute_switch.py +++ b/tests/test_mute_switch.py @@ -127,11 +127,27 @@ def test_dropped_events_read_the_switch_again(self): self.assertIsNotNone(follower.wait(10.0)) def test_a_failure_is_tried_again(self): - follower = m.Follower(retry=2.0) + follower = m.Follower(retry=2.0, max_retry=16.0) self.assertEqual(follower.decide(True), (True, True)) follower.failed(5.0) self.assertEqual(follower.wait(5.0), 2.0) self.assertEqual(follower.decide(True), (True, True)) # still quiet: nothing was set yet + follower.failed(7.0) + self.assertEqual(follower.wait(7.0), 4.0) + follower.failed(11.0) + self.assertEqual(follower.wait(11.0), 8.0) + follower.failed(19.0) + self.assertEqual(follower.wait(19.0), 16.0) + follower.failed(35.0) + self.assertEqual(follower.wait(35.0), 16.0) # capped at max_retry + # A physical switch move or a successful set resets the backoff. + follower.feed(key(0) + SYN, 40.0) + self.assertAlmostEqual(follower.wait(40.0), follower.settle) + follower.failed(41.0) + self.assertEqual(follower.wait(41.0), 2.0) + follower.done(False) + follower.failed(42.0) + self.assertEqual(follower.wait(42.0), 2.0) def test_a_partial_read_is_ignored(self): follower = m.Follower() @@ -161,6 +177,21 @@ def test_without_the_osd_the_mute_is_still_set(self): self.assertEqual(calls, [['omarchy-volume', 'mute-on'], ['wpctl', 'set-mute', '@DEFAULT_AUDIO_SINK@', '1']]) self.assertFalse(self.run_with([1, 1], True, False)[0]) + def test_repeated_identical_stderr_is_logged_once(self): + m._last_mute_error = None + logged = [] + + def run(argv, **_): + return subprocess.CompletedProcess(argv, 1, stderr="Translate ID error: '-1'\n") + + with patch.object(m.subprocess, 'run', side_effect=run), \ + patch.object(m, 'log', side_effect=lambda *a: logged.append(' '.join(a))): + self.assertFalse(m.set_mute(True, True)) + self.assertFalse(m.set_mute(True, True)) + self.assertFalse(m.set_mute(True, True)) + self.assertEqual(len(logged), 1) + self.assertIn("Translate ID error: '-1'", logged[0]) + class KeyboardMute(unittest.TestCase): """A keyboard's Mute key on a device with the switch: labwc leaves it alone, the helper toggles.""" From 7e03fa129f10f2014e9c715a560eba29167fb95b Mon Sep 17 00:00:00 2001 From: Tom Ballard Date: Wed, 2 Dec 2026 06:13:26 +0000 Subject: [PATCH 2/2] Changelog: mute-switch backoff without an audio output Assisted-by: LLM --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19ead836..3431e6e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,11 @@ release is checked here and released on by hand. - iPhone X: the desktop starts at the size it has under iOS, instead of everything drawn at a third of it. +### System + +- On devices without an audio output yet, the side mute-switch helper backs off + retries instead of retrying every two seconds and filling its runtime log. + ## [0.4.6] - 2026-12-02 Built on orchard 0.3.20.