Skip to content

feat: allow zero monitors and add ATTACH/DETACH pipe commands - #533

Open
iaminpwd wants to merge 2 commits into
VirtualDrivers:masterfrom
iaminpwd:feat/zero-monitors-attach-detach
Open

iaminpwd wants to merge 2 commits into
VirtualDrivers:masterfrom
iaminpwd:feat/zero-monitors-attach-detach

Conversation

@iaminpwd

Copy link
Copy Markdown

Motivation

Applications that only need a virtual display for the length of a session — game-streaming hosts, apps that turn a
tablet or another device into a secondary monitor — currently have two options with VDD:

  1. Leave a virtual monitor plugged in all the time. While the app is idle, Windows still lists that monitor in
    Display settings, it can be enabled again with Win+P ("Extend"), and windows can end up on a screen nobody is
    looking at.
  2. Change <count> and reinitialize the adapter (SETDISPLAYCOUNT / RELOAD_DRIVER) or disable/enable the device.
    That is heavier than plugging a monitor in, and <count>0</count> is currently turned into 1, so "no monitor"
    cannot be expressed at all.

In #487 the Sunshine maintainers mentioned they are considering shipping VDD with Sunshine. The usual pattern for a
streaming host is "create the display when a client connects, remove it when the session ends", so this PR adds a
small, opt-in way to do exactly that.

Changes

  • <count>0</count> in vdd_settings.xml is now honoured: the driver starts with no monitor. The adapter still
    declares MaxMonitorsSupported = 1 in that case so a monitor can be added later.
  • New pipe command ATTACH: if no monitor is attached, creates <count> monitors (one when <count> is 0) through
    the existing CreateMonitor path. Idempotent. Replies ATTACHED or ERROR.
  • New pipe command DETACH: calls IddCxMonitorDeparture for every attached monitor. Idempotent. Replies DETACHED
    or ERROR.
  • IndirectDeviceContext tracks the monitors that have arrived (m_Monitors). A global pointer to the device context
    is set in FinishInit and cleared in the destructor, guarded by a mutex that also serializes ATTACH/DETACH with
    FinishInit. (The pipe thread has no WDF object to look the context up from.)
  • README: short section on <count>0</count> and the two commands. vdd_settings.xml: one comment line next to
    <count>; the default value stays 1.

ATTACH/DETACH do not re-read vdd_settings.xml; they use the settings loaded at startup. Re-reading settings on
ATTACH (e.g. to pick up a client's resolution) could be a follow-up if wanted.

Backward compatibility

  • Default behaviour is unchanged: with <count> >= 1 the same monitors are created at startup as before.
  • The only behaviour change for existing configs is <count>0</count>, which previously meant 1 monitor and now
    means 0. The shipped vdd_settings.xml uses 1.
  • ATTACH / DETACH are new names; no existing pipe command is changed. Clients that do not send them see no
    difference.

Notes for reviewers

  • This touches CreateMonitor, FinishInit and the destructor, which Harden monitor lifetime and mode handling #514 (monitor lifetime) and Fix IddCx watchdog crashes in swap-chain assign/unassign #532 (swap-chain
    assign/unassign) also change. Happy to rebase on whichever lands first. With Harden monitor lifetime and mode handling #514, m_Monitors could move into the
    per-monitor context it introduces.
  • DETACH makes IddCx call EvtIddCxMonitorUnassignSwapChain, so it goes through the same unassign path that Fix IddCx watchdog crashes in swap-chain assign/unassign #532
    hardens.
  • If pipe logging is enabled, log lines can arrive on the pipe before the ATTACHED / DETACHED reply (same as for
    other commands).
  • The adapter is re-initialized on every D0 entry (InitAdapter in EvtDeviceD0Entry), e.g. after sleep, which
    drops all monitors. The driver keeps the last ATTACH/DETACH request and FinishInit restores that state, so an
    attached monitor comes back after resume and a detached one does not reappear. Clients that never send
    ATTACH/DETACH keep the old <count> behaviour. The same applies after RELOAD_DRIVER.
  • If ATTACH arrives before the adapter has finished initializing, the reply is ERROR, but the request is still
    remembered and applied when initialization finishes.

Testing

  • Built MttVDD.vcxproj x64 Release locally (VS 2022, WDK 10.0.26100 NuGet packages, UMDF 2.35 headers). CI build
    not run yet.

  • Runtime test of this branch: not done yet. Suggested manual test:

    1. Set <count>0</count>, restart the device. Display settings shows no virtual monitor; Win+P does not bring one
      back.
    2. Send ATTACH → reply ATTACHED, a monitor appears. Send ATTACH again → ATTACHED, no second monitor.
    3. Send DETACH → reply DETACHED, the monitor disappears and windows move to the physical display. Send DETACH
      again → DETACHED.
    4. Repeat ATTACH/DETACH a number of times, including while something is rendering to the virtual monitor.
    5. ATTACH, put the PC to sleep and resume: the monitor is back. DETACH, sleep and resume: no monitor.
    6. With <count>1</count> (default): one monitor at startup as before; DETACH then ATTACH restores it.

    Example client (PowerShell):

    $pipe = New-Object System.IO.Pipes.NamedPipeClientStream('.', 'MTTVirtualDisplayPipe', 'InOut')
    $pipe.Connect(2000)
    $bytes = [Text.Encoding]::Unicode.GetBytes('ATTACH')
    $pipe.Write($bytes, 0, $bytes.Length); $pipe.Flush()
    $buf = New-Object byte[] 512; $n = $pipe.Read($buf, 0, $buf.Length)
    [Text.Encoding]::ASCII.GetString($buf, 0, $n)   # ATTACHED
    $pipe.Dispose()

Let the driver start with <count>0</count> so no virtual monitor is
reported while it is not in use, and add two pipe commands to plug the
configured monitor(s) in and out at runtime without reinitializing the
adapter:

- ATTACH: if no monitor is attached, create <count> monitors (one when
  <count> is 0). Replies ATTACHED or ERROR.
- DETACH: report departure of every attached monitor. Replies DETACHED
  or ERROR.

The adapter still declares at least one supported monitor so ATTACH
works when starting from zero. Existing configurations (count >= 1) and
existing pipe commands behave as before.
The adapter is re-initialized on every D0 entry (e.g. after sleep), which
drops all monitors. Remember the last ATTACH/DETACH request and restore
it in FinishInit, so an attached monitor comes back after resume and a
detached one does not reappear. Without ATTACH/DETACH, <count> decides
as before.
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.

1 participant