merge(core): reconcile the W7/W8 client refactor with origin's shared-clipboard feature

origin/main landed the shared clipboard (design/clipboard-and-file-transfer.md) while
this branch split quic/msgs.rs -> quic/{caps,control,...} and client.rs ->
client/{mod,control,worker,pump,planes,...} (W7) and deleted the two monoliths. The
feature had modified both deleted files, so its delta is re-applied onto the split
instead of resurrecting the monoliths:

  - HOST_CAP_CLIPBOARD                         -> quic/caps.rs
  - MSG_CLIP_* / CLIP_* consts, the six Clip*
    structs, and their encode/decode impls     -> quic/control.rs (beside the clock codecs)
  - CtrlRequest::{ClipControl,ClipOffer} +
    Negotiated.host_caps                        -> client/control.rs
  - WorkerArgs.{clip_event_tx,clip_cmd_rx}      -> client/worker.rs
  - CLIP_EVENT_QUEUE                            -> client/planes.rs
  - NativeClient clip fields, the 7 clip_* /
    host_caps / next_clip methods, connect()
    channel wiring                              -> client/mod.rs
  - the control-task encode/decode arms and
    the clipboard-task spawn                     -> client/pump.rs

Cargo.lock reconciled (adds pf-clipboard), punktfunk-host/Cargo.toml unions the W6
pf-* subsystem deps with pf-clipboard, and include/punktfunk_core.h is the cbindgen
union (clipboard + rumble C-ABI). punktfunk-core builds --all-features and its 174
lib tests pass, including quic::tests::clip_loopback.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-17 15:38:11 +02:00
36 changed files with 5864 additions and 25 deletions
+85 -2
View File
@@ -6,10 +6,13 @@
//! the data-plane thread over the session's mpsc bridges.
use super::*;
use pf_clipboard::ClipCoordCmd;
use punktfunk_core::quic::{ClipControl, ClipOffer, ClipState};
/// Run the control task for one live session. Owns the control streams (`serve_session` hands them
/// off after negotiation) plus every channel end that bridges to the data-plane thread. Returns
/// when the control stream closes or a data-plane channel drops.
/// off after negotiation) plus every channel end that bridges to the data-plane thread, and the
/// [`pf_clipboard::ClipCoord`] handle bridging to the clipboard coordinator. Returns when the
/// control stream closes or a data-plane channel drops.
#[allow(clippy::too_many_arguments)]
pub(super) async fn run(
mut ctrl_send: quinn::SendStream,
@@ -27,7 +30,17 @@ pub(super) async fn run(
probe_tx: std::sync::mpsc::Sender<ProbeRequest>,
mut probe_result_rx: tokio::sync::mpsc::UnboundedReceiver<ProbeResult>,
mut reconfig_result_rx: tokio::sync::mpsc::UnboundedReceiver<Reconfigured>,
clip_enabled: Arc<AtomicBool>,
clip: pf_clipboard::ClipCoord,
) {
let pf_clipboard::ClipCoord {
available: clip_available,
cmd_tx: clip_cmd_tx,
offer_rx: mut clip_offer_rx,
} = clip;
// Set once `clip_offer_rx` closes (coordinator gone / inert handle) so its `select!` branch
// stops firing on a perpetually-ready `None`.
let mut clip_offer_closed = false;
let mut active = initial_mode;
// Host-side switch rate limit (a backstop against a hostile/broken client spamming
// Reconfigure into pipeline-rebuild churn — the drain-to-newest in the data plane already
@@ -180,6 +193,61 @@ pub(super) async fn run(
if io::write_msg(&mut ctrl_send, &echo.encode()).await.is_err() {
break;
}
} else if let Ok(ctl) = ClipControl::decode(&msg) {
// Shared clipboard enable/disable (design/clipboard-and-file-transfer.md
// §3.1). Reply with the resolved state; the operator policy is authoritative
// over the client's request. When the policy allows it but no backend bound
// (gamescope / older GNOME), enable is refused with BACKEND_UNAVAILABLE so the
// client can say *why*. The resolved `enabled` gates the coordinator.
let policy = pf_clipboard::policy();
let (enabled, resolved_policy, reason) = match policy {
None => (false, 0, punktfunk_core::quic::CLIP_REASON_POLICY_DISABLED),
Some(p) if ctl.enabled && !clip_available => {
(false, p, punktfunk_core::quic::CLIP_REASON_BACKEND_UNAVAILABLE)
}
Some(p) => {
let files_ok = p & punktfunk_core::quic::CLIP_POLICY_FILES != 0;
let wants_files =
ctl.flags & punktfunk_core::quic::CLIP_FLAG_FILES != 0;
let reason = if wants_files && !files_ok {
punktfunk_core::quic::CLIP_REASON_NO_FILES
} else {
punktfunk_core::quic::CLIP_REASON_OK
};
(ctl.enabled, p, reason)
}
};
clip_enabled.store(enabled, Ordering::SeqCst);
// Drive the coordinator: enable re-announces the current host clipboard,
// disable drops any selection we own. A dropped send (inert handle) is fine.
let _ = clip_cmd_tx.send(ClipCoordCmd::SetEnabled(enabled));
tracing::info!(
enabled,
files = enabled
&& resolved_policy & punktfunk_core::quic::CLIP_POLICY_FILES != 0,
"clipboard control"
);
let state = ClipState {
enabled,
policy: resolved_policy,
reason,
};
if io::write_msg(&mut ctrl_send, &state.encode()).await.is_err() {
break;
}
} else if let Ok(offer) = ClipOffer::decode(&msg) {
// The client copied: hand its lazy format list to the coordinator, which
// installs a host-side source that fetches from the client on host paste.
tracing::debug!(
seq = offer.seq,
kinds = offer.kinds.len(),
"clipboard offer from client"
);
let mimes = offer.kinds.iter().map(|k| k.mime.clone()).collect();
let _ = clip_cmd_tx.send(ClipCoordCmd::RemoteOffer {
seq: offer.seq,
mimes,
});
} else {
tracing::warn!("unknown control message — ignoring");
}
@@ -190,6 +258,21 @@ pub(super) async fn run(
break;
}
}
offer = clip_offer_rx.recv(), if !clip_offer_closed => {
// Host copied → the coordinator minted a `ClipOffer`; forward it to the client
// (only while sync is on — a race with a just-received disable would otherwise
// leak a stale offer). `None` = coordinator gone; disable this branch.
match offer {
Some(offer) => {
if clip_enabled.load(Ordering::SeqCst)
&& io::write_msg(&mut ctrl_send, &offer.encode()).await.is_err()
{
break;
}
}
None => clip_offer_closed = true,
}
}
correction = reconfig_result_rx.recv() => {
// H2 rollback/correction ack: the data plane reports the mode ACTUALLY live
// after a rebuild that failed (stayed at the old mode) or that the backend
+10 -2
View File
@@ -365,8 +365,16 @@ pub(super) async fn negotiate(
// assuming HEVC.
codec: codec_bit,
// This host applies sequence-gated gamepad-state snapshots (InputKind::GamepadState),
// so capable clients send those instead of the loss-fragile per-transition events.
host_caps: punktfunk_core::quic::HOST_CAP_GAMEPAD_STATE,
// so capable clients send those instead of the loss-fragile per-transition events. The
// clipboard bit is advertised only when the operator policy enables it (design
// clipboard-and-file-transfer.md §3.1) AND this platform has a backend — see
// `pf_clipboard::cap_advertised` for the deliberate compositor-lacks-data-control case.
host_caps: punktfunk_core::quic::HOST_CAP_GAMEPAD_STATE
| if pf_clipboard::cap_advertised() {
punktfunk_core::quic::HOST_CAP_CLIPBOARD
} else {
0
},
};
io::write_msg(send, &welcome.encode()).await?;