Skip to content

Route volume media keys to the default audio output device - #19

Merged
aquitaine merged 2 commits into
mainfrom
fix-media-key-volume-routing
Jul 19, 2026
Merged

Route volume media keys to the default audio output device#19
aquitaine merged 2 commits into
mainfrom
fix-media-key-volume-routing

Conversation

@aquitaine

@aquitaine aquitaine commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Problem

The media-key interception (Settings → "Use the brightness & volume keys to control displays") routed the hardware volume/mute keys to whichever display the brightness target mode resolved (under cursor / main / built-in). If that display was a DDC-audio-capable external, the keys changed the monitor's DDC volume even when system sound was playing through the MacBook speakers, AirPods, a USB DAC, etc.

Real scenario: built-in display off, external monitor is main, default audio output = MacBook speakers → volume keys wrongly drove the monitor's DDC volume while sound kept coming from the speakers.

Fix

Volume/mute keys now follow the system default audio output device, decoupled from the brightness target mode:

  • Not a display (built-in speakers, headphones, AirPods, USB DAC, aggregate/multi-output) → pass through so macOS adjusts that device natively with its own HUD.
  • An external monitor's HDMI/DP audio, active and DDC-volume-capable → drive that monitor's DDC volume/mute, even if the cursor/main display is elsewhere.
  • Inactive, not DDC-volume-capable, or unmatchable device → pass through (fail safe, never guess).

Brightness keys are unchanged — they still follow MediaKeyTargetMode.

Implementation (pure-core / macOS-glue split)

  • AudioOutputInfo.swift (new, macOS glue) — reads the CoreAudio default output device name + transport type per keypress (single property reads, microseconds; no listener/cache).
  • AudioOutputMatch.swift (new, pure TopologyCore, no CoreAudio/IOKit) — AudioOutputTransport enum + AudioOutputDisplayMatcher: transport must be hdmi/displayPort else nil; case-insensitive exact→contains name match to a display; single-active-external fallback; ambiguous → nil.
  • MediaKey.swiftMediaKeyTargetPolicy.target gains an audioTarget: parameter; the volume branch ignores mode/cursor and routes only to that display when it's active + volume-capable. Updated doc comments on the policy and isVolume.
  • AppModel.swifthandleMediaKey resolves audioTarget (via new audioOutputDisplayID(), reusing displayName(for:)) for volume actions only; mute's preMuteVolume logic and volumeCapableDisplayIDs() fail-open behavior unchanged.
  • SettingsView.swift — picker relabeled "Brightness keys control the…", added "Volume keys follow the current sound output device." No new user setting.

Tests / verification

  • make test green — added 12 matcher tests (name exact/prefix/case-insensitive, wrong transport, single-external fallback, two-external ambiguity); migrated policy tests to the new signature (volume routes to audioTarget regardless of mode; passes through when nil/inactive/not-capable; brightness unchanged).
  • Both OpenDisplay and OpenDisplay-PublicAPIOnly schemes build (ran make xcode — two new source files).

Manual live checks

  1. Sound output = MacBook speakers, cursor/main on external → volume keys change speaker volume with the native macOS HUD.
  2. Sound output = the monitor (HDMI/DP) → volume keys change monitor DDC volume with OpenDisplay's OSD.
  3. Brightness keys still follow the configured target mode in both cases.

Volume/mute media keys followed the brightness MediaKeyTargetMode
(under cursor / main / built-in). If that display was a DDC-audio
external, the keys drove the monitor's DDC volume even when system
sound was on the MacBook speakers, AirPods, a USB DAC, etc.

Volume/mute now follow the system's DEFAULT AUDIO OUTPUT DEVICE:
- Not a display (speakers / headphones / AirPods / USB DAC / aggregate)
  -> pass through so macOS adjusts it natively with its own HUD.
- An external monitor's HDMI/DP audio, active and DDC-volume-capable
  -> drive that monitor's DDC volume/mute, even if the cursor/main
  display is elsewhere.
- Inactive / not volume-capable / unmatchable -> pass through (fail safe).

Brightness keys are unchanged (still follow MediaKeyTargetMode).

- AudioOutputInfo (macOS glue): reads the CoreAudio default output
  device name + transport per keypress.
- AudioOutputDisplayMatcher (pure TopologyCore, no CoreAudio): maps
  device name/transport to a display record; case-insensitive
  exact->contains match, single-active-external fallback, ambiguous->nil.
- MediaKeyTargetPolicy.target gains an audioTarget: parameter; the
  volume branch ignores mode/cursor and routes only to that display.
- SettingsView copy: picker now labeled for brightness keys; adds
  "Volume keys follow the current sound output device."

make test green (+12 matcher tests, policy tests migrated); both app
schemes build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5de1c4a626

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Packages/TopologyCore/Sources/TopologyCore/AudioOutputMatch.swift Outdated
Comment thread Apps/OpenDisplay/Sources/AppModel.swift Outdated
- AudioOutputDisplayMatcher: an exact name match now requires exactly
  one hit. Two identical monitors share a product name and CoreAudio
  routes to only one of them, so two exact hits are ambiguous -> nil
  (fail safe), matching the contains branch's behavior.
- AppModel: match audio devices against the OS/hardware display name,
  not the user alias. displayName(for:) returns the alias first, so an
  aliased monitor ("Desk") never matched its CoreAudio device name
  ("Dell U2720Q") and volume keys wrongly passed through. Factored out
  osDisplayName(for:) (localized name, no alias) for the matcher.
- Test for the two-identical-monitors exact-name case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@aquitaine
aquitaine merged commit 2d4f908 into main Jul 19, 2026
@aquitaine
aquitaine deleted the fix-media-key-volume-routing branch July 19, 2026 22:48
aquitaine added a commit that referenced this pull request Jul 19, 2026
Add menu-bar popover + settings screenshots as the hero image pair,
replacing the placeholder comment. Update the media-keys feature bullet:
volume keys now follow the current sound output device (PR #19), not the
brightness target mode.

Co-authored-by: Claude <noreply@anthropic.com>
@aquitaine aquitaine mentioned this pull request Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants