Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 32 additions & 7 deletions desktop/configuration/mute-switch
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -112,17 +115,19 @@ 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."""
whole = len(data) // EVENT.size * EVENT.size
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."""
Expand All @@ -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):
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion tests/test_mute_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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."""
Expand Down
Loading