Skip to content

feat: add automatic peripheral switching on monitor input change - #207

Merged
MegaManSec merged 43 commits into
MegaManSec:mainfrom
sumanth1989:feature/auto-display-detect
Dec 2, 2026
Merged

MegaManSec merged 43 commits into
MegaManSec:mainfrom
sumanth1989:feature/auto-display-detect

Conversation

@sumanth1989

@sumanth1989 sumanth1989 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What it does

Adds Take Peripherals When a Monitor Switches Input to Settings → Other, for one monitor cabled to both Macs and switched with its input button. When the monitor switches to this Mac's input, this Mac takes the peripherals. Optionally, when it switches to the input chosen for the other Mac, this Mac sends them there.

It sits next to Take Peripherals When a Display Connects, which fires when a display connects to this Mac. A monitor that stays connected to both Macs never connects or disconnects when its input changes, so only the new section sees that switch.

Off by default. Apple silicon only, and not in the App Store build.

Settings

  • Use a monitor cabled to both Macs turns the feature on and lists the connected monitors.
  • Each monitor has a row. Turn it on while the monitor shows this Mac, and that input becomes the trigger. The row shows the input it triggers on and the current one, with a Use … button when they differ. A monitor that doesn't answer DDC/CI says DDC/CI input unavailable. A monitor that isn't connected keeps its row and trigger, with a button to forget it.
  • Send peripherals when a monitor switches to your other Mac adds a Your other Mac's input picker to each row, listing the inputs this Mac has read from that monitor since launch. If both Macs run the take side, each takes on its own; the send is for when the other Mac can't read the monitor, for example because it runs the App Store version.
  • The refresh button in the header re-reads every connected monitor's input.

How it works

  • MonitorInputService reads VCP 0x60 (input source) over DDC/CI through the private IOAVService API, loaded with dlsym, on the DCPAVServiceProxy services whose location is External. Each service is matched to its CoreGraphics display by EDID, read at I2C address 0x50. Replies are checksum-checked.
  • DisplayMonitor polls every 1.5 s, only while the feature is on and a connected monitor has a trigger. Reads run on a serial background queue.
  • A switch is a change of input. The first read after a trigger is set, at launch, or after the monitor reconnects only records the input. Three failed reads in a row count as "unreadable", for monitors that stop answering while they show another input, so reading this Mac's input after that counts as a switch back.
  • A take or send that can't start is retried on each poll for up to two minutes while the monitor stays on that input. That covers a transfer already in progress, Bluetooth not being ready and, for a send, the other Mac being unreachable. Switching the input elsewhere drops the retry, and a send with nothing on this Mac counts as done. So switching back and forth quickly leaves the peripherals on the Mac the monitor shows, and switching to the other Mac and then waking it sends them once it answers. After two minutes the switch is dropped, so the other Mac answering hours later, for example during a Power Nap wake with its Bluetooth off, can't pull the peripherals off this Mac.
  • The take uses the same engine as the display trigger and the take shortcut, so it works while the other Mac sleeps. The send uses the same health-checked path as the send shortcut.
  • After a wake, polling waits 5 s so peripherals still bonded to this Mac can reconnect first, matching the store's wake reclaim delay. The last input is kept through sleep, so a switch made while this Mac slept fires when it wakes.
  • Turning a row on, or clicking Use …, saves the input shown right away and then corrects it with a fresh read, so a stale reading isn't kept. The other Mac's input is remembered when a row is turned off and back on.

Also changed

  • A keyboard-shortcut or URL send with only some peripherals on this Mac used to show "Peripherals Split Between Macs". It now sends the ones that are here, each through the per-peripheral send with its own ping preflight and rollback, so a failed send can't pull back peripherals the other Mac holds.
  • The README describes both display sections, and the Other tab screenshot shows the new one.

Limits

  • A monitor that disconnects from this Mac when it switches to the other Mac never triggers the new section: its last input is forgotten when it disconnects, and the first read after it reconnects only records the input. The README points those monitors to the display section.
  • A send moves the peripherals connected to this Mac. One that's switched off stays paired with this Mac.
  • The retry trusts this Mac's last reachability check, which runs every 30 s. If the other Mac went to sleep since then, the send goes to the live check once, shows "Couldn't Reach Your Other Mac" and isn't retried.
  • The send needs the other Mac's input. A monitor that this Mac can't read while it shows the other Mac never offers that input, and the row says so.

Permissions

The direct build's sandbox gets com.apple.security.temporary-exception.iokit-user-client-class for DCPAVServiceProxyUserClient, which IOAVService needs to reach the display. The App Store build leaves the code out.

Testing

  • @sumanth1989 tested it on a Mac mini and a MacBook Pro sharing one monitor (comment). On the Mac mini, the take and the send both work. The MacBook Pro gets no DDC/CI response from the monitor, so the Mac mini's send covers that direction, which is what the send option is for.
  • Debug build and swift-format lint pass, and every new string has a Spanish translation.
  • The Settings UI was also checked on a monitor that answers EDID reads but rejects VCP reads.

…/CI VCP 0x60)

Add DDC/CI monitor input source (VESA MCCS VCP 0x60) detection and automatic
peripheral switching for non-App-Store builds:

- Add MonitorInputService (gated behind #if !APP_STORE) using IOAVService
  (DCPAVServiceProxy) to read VCP 0x60 from external displays on Apple Silicon,
  matching CGDirectDisplayID to its IOAVService via EDID vendor/model/serial
  with a single-external-display fallback.
- Update Magic_Switch_Direct.entitlements to disable App Sandbox so local and
  Direct builds can access DCPAVServiceProxyUserClient for I2C DDC/CI reads
  (AppStore entitlements remain unchanged).
- Extend DisplayMonitor with persisted input-switching settings
  (isInputSwitchingEnabled, sendOnInputSwitchAway, and per-display
  inputTriggers keyed by CoreGraphics display UUID), background polling on a
  dedicated queue while awake, and launch/wake seeding to avoid spurious switches.
- Fire onTriggerDisplayInputSelected when VCP 0x60 transitions to the
  configured input (taking peripherals via takeAllPeripheralsIfAway) and
  optionally onTriggerDisplayInputDeselected when switching away to another
  valid input (sending peripherals to the trusted peer via
  performFullSetCommand(.send)), supporting monitors where only one Mac can
  read DDC/CI.
- Add the Monitor Input Switching section in OtherSettingsView (#if !APP_STORE)
  with a master toggle, a 'Send peripherals to the other Mac when switching
  away' toggle, and per-display toggles that capture the live VCP 0x60 input.

@MegaManSec MegaManSec left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Notes on the original commit. Each is fixed in a later commit on this branch.

@MegaManSec MegaManSec changed the title feat: add automatic peripheral switching on monitor input change feat: add automatic peripheral switching on monitor input change Nov 29, 2026
@MegaManSec

Copy link
Copy Markdown
Owner

@sumanth1989 Thanks! I've made some signiificant changes in your changes; think you could test for me?

@sumanth1989

Copy link
Copy Markdown
Contributor Author

Works perfectly with your changes @MegaManSec.
PS - I could only test the auto hand off from oneside i.e from my mac mini, and could only set up the "take peripherals when a monitor input switches input" -> Your other mac's input from one side , since my Macbook Pro isn't getting a DDC/CI response

@MegaManSec

Copy link
Copy Markdown
Owner

Thanks, will merge and publish into the official brew/gh one soon.

@sumanth1989

Copy link
Copy Markdown
Contributor Author

Thanks, you have built an amazing piece of software btw.

@MegaManSec
MegaManSec merged commit 3b53347 into MegaManSec:main Dec 2, 2026
4 checks passed
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 3.4.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants