diff --git a/crates/pf-nx-app/src/core/model.rs b/crates/pf-nx-app/src/core/model.rs index b654628..c515fc5 100644 --- a/crates/pf-nx-app/src/core/model.rs +++ b/crates/pf-nx-app/src/core/model.rs @@ -254,10 +254,16 @@ impl GamepadType { }) } - /// The dial's Controller type step: the shared order (`overlay_actions::next_pad_type`), - /// skipping the kinds this client offers no row for. + /// The dial's Controller type step: Automatic, the two Nintendo kinds this console's pads + /// are, then the shared order (`overlay_actions::next_pad_type`) over the kinds this client + /// offers. The shared order names no Nintendo kind yet (`docs/improvements.md`). pub fn next_on_dial(self) -> Self { - let mut pref = self.to_core(); + let mut pref = match self { + Self::Auto => return Self::JoyConPair, + Self::JoyConPair => return Self::SwitchPro, + Self::SwitchPro => punktfunk_core::config::GamepadPref::Auto, + other => other.to_core(), + }; loop { pref = pf_client_core::overlay_actions::next_pad_type(pref); if let Some(next) = Self::from_core(pref) { @@ -356,7 +362,7 @@ pub struct GameEntry { mod tests { use super::*; - /// The dial steps the shared order over this client's rows; a Settings-only kind steps back. + /// The dial steps the Nintendo kinds, then the shared order over this client's rows. #[test] fn the_dial_steps_the_types_this_client_offers() { let mut seen = vec![GamepadType::Auto]; @@ -369,12 +375,13 @@ mod tests { seen, [ GamepadType::Auto, + GamepadType::JoyConPair, + GamepadType::SwitchPro, GamepadType::XboxOne, GamepadType::DualSense, GamepadType::DualShock4 ] ); - assert_eq!(GamepadType::SwitchPro.next_on_dial(), GamepadType::Auto); } /// The saved record is the shared one plus this console's fields, in one flat object (plan D8): diff --git a/crates/pf-nx-app/src/platform/fake.rs b/crates/pf-nx-app/src/platform/fake.rs index 2908949..3b8b333 100644 --- a/crates/pf-nx-app/src/platform/fake.rs +++ b/crates/pf-nx-app/src/platform/fake.rs @@ -8,8 +8,8 @@ use std::sync::{Arc, Mutex}; use anyhow::Result; use super::{ - AudioOut, AudioPort, Display, Fit, FrameReport, Lifecycle, PadFeedback, PadSample, Pads, Refresh, Rgba, Storage, - StreamSpec, SystemEvent, VideoDecoders, + AudioOut, AudioPort, Display, Fit, FrameReport, Lifecycle, Motion, PadFeedback, PadSample, Pads, Refresh, Rgba, + Storage, StreamSpec, SystemEvent, TouchPoint, VideoDecoders, }; use crate::core::caps::VideoCaps; use crate::core::media::{VideoSink, VideoSinkCaps}; @@ -179,6 +179,10 @@ pub struct FakePads { pub script: Arc>>>, pub feedback: Arc>>, pub released: Arc>, + /// Every pad's motion, held until changed. + pub motion: Arc>>, + /// The fingers down, held until changed. + pub touches: Arc>>, /// The last batch, repeated while the script is empty: a held pad stays held. last: Vec, } @@ -201,6 +205,14 @@ impl Pads for FakePads { fn release_all(&mut self) { *self.released.lock().expect("fake pads poisoned") += 1; } + + fn motion(&mut self, _slot: u8) -> Option { + *self.motion.lock().expect("fake pads poisoned") + } + + fn touches(&mut self, out: &mut Vec) { + out.clone_from(&self.touches.lock().expect("fake pads poisoned")); + } } /// A directory the test owns. diff --git a/crates/pf-nx-app/src/platform/mod.rs b/crates/pf-nx-app/src/platform/mod.rs index 99a572d..d9432f6 100644 --- a/crates/pf-nx-app/src/platform/mod.rs +++ b/crates/pf-nx-app/src/platform/mod.rs @@ -42,6 +42,8 @@ pub struct DeviceInfo { pub gpu: String, /// How the UI layer draws: backend, size, and why it fell back to the CPU. pub ui: String, + /// Launched as an applet (from the album), with a fraction of the title's memory (plan D13). + pub applet_mode: bool, } /// Refresh rate of the scan-out, in millihertz (60 000, 59 940). @@ -202,6 +204,25 @@ pub enum PadKind { SwitchPro, } +/// One motion sample in the wire's frame, SDL's: +x right, +y up, +z toward the player. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Motion { + /// Rotation about each axis, rad/s. + pub gyro: [f32; 3], + /// In g; a pad lying still reads +1 on y. + pub accel: [f32; 3], +} + +/// One finger on the console's own screen. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct TouchPoint { + /// Stable while the finger stays down. + pub id: u32, + /// 0.0..=1.0 across the screen from its top-left corner. + pub x: f32, + pub y: f32, +} + /// What the host asked a pad to do. #[derive(Clone, Debug, PartialEq, Eq)] pub enum PadFeedback { @@ -218,6 +239,18 @@ pub trait Pads: Send { /// Stops every pad's motors. fn release_all(&mut self); + + /// The latest motion sample of the pad in `slot`, after a [`Self::poll`]; `None` without + /// a sensor. + fn motion(&mut self, _slot: u8) -> Option { + None + } + + /// The fingers on the screen now, replacing `out`'s contents. Nothing without a touch + /// screen, or while the screen is off in the dock. + fn touches(&mut self, out: &mut Vec) { + out.clear(); + } } /// Where identity, settings, logs and bundles live. diff --git a/crates/pf-nx-app/src/runtime/mod.rs b/crates/pf-nx-app/src/runtime/mod.rs index c66952c..20828ce 100644 --- a/crates/pf-nx-app/src/runtime/mod.rs +++ b/crates/pf-nx-app/src/runtime/mod.rs @@ -6,9 +6,11 @@ mod connect; mod hud; mod menu; +mod motion; pub mod pads; mod stream; pub mod testrun; +mod touch; use std::sync::atomic::{AtomicBool, Ordering}; @@ -92,13 +94,14 @@ pub fn run(platform: Platform) -> ! { let flush = crate::logger::init_subscriber(&crate::services::paths::logs_dir(), log).ok(); install_panic_hook(); tracing::info!( - "punktfunk-switch {} on {} ({} firmware {}), gpu {}, ui {}", + "punktfunk-switch {} on {} ({} firmware {}), gpu {}, ui {}{}", crate::core::build_id(), device.platform, device.model, device.firmware, device.gpu, device.ui, + if device.applet_mode { ", applet mode" } else { "" }, ); punktfunk_core::client::set_client_label(&client_label(&device)); crate::core::caps::install(video.caps()); @@ -144,9 +147,13 @@ pub fn run(platform: Platform) -> ! { lifecycle.exit() } +/// What the first menu says when the title runs as an applet (plan D13). +const APPLET_MODE_NOTICE: &str = + "Opened from the Album, punktfunk has little memory and may close — hold R while starting a game to open it with full memory."; + /// The menu and the stream in turn, until one of them quits. fn alternate(app: &mut App) -> Option { - let mut notice = None; + let mut notice = app.device.applet_mode.then(|| APPLET_MODE_NOTICE.to_string()); loop { let outcome = match menu::run(app, notice.take()) { Ok(outcome) => outcome, @@ -170,14 +177,17 @@ fn client_label(device: &crate::platform::DeviceInfo) -> String { format!("Switch {}", device.firmware).trim_end().to_string() } -/// A panic on any thread goes to the log with its frames, then the process aborts. +/// A panic on any thread goes to the log, then its frames where the target can walk them, then +/// the process aborts. The message is flushed before the walk: on the console, which builds +/// without unwind tables, the walk itself aborts. fn install_panic_hook() { std::panic::set_hook(Box::new(|info| { + let thread = std::thread::current(); + let name = thread.name().unwrap_or("unnamed"); + tracing::error!("PANIC on thread {name:?}: {info}"); + crate::logger::flush(); let backtrace = std::backtrace::Backtrace::force_capture(); - tracing::error!( - "PANIC on thread {:?}: {info}\n{backtrace}", - std::thread::current().name().unwrap_or("unnamed"), - ); + tracing::error!("PANIC on thread {name:?}, frames:\n{backtrace}"); crate::logger::flush(); })); } diff --git a/crates/pf-nx-app/src/runtime/motion.rs b/crates/pf-nx-app/src/runtime/motion.rs new file mode 100644 index 0000000..536d37b --- /dev/null +++ b/crates/pf-nx-app/src/runtime/motion.rs @@ -0,0 +1,130 @@ +//! Each pad's gyro and accelerometer onto its pad on the host, in the wire's units. Pure: samples +//! in, rich input out. + +use punktfunk_core::input::gamepad::{MOTION_ACCEL_LSB_PER_G, MOTION_GYRO_LSB_PER_DEG_S}; +use punktfunk_core::quic::RichInput; + +use super::pads::SLOTS; +use crate::platform::Motion; + +/// The wire's gyro counts per rad/s. +const GYRO_LSB_PER_RAD_S: f32 = MOTION_GYRO_LSB_PER_DEG_S as f32 * 180.0 / std::f32::consts::PI; + +/// A sample in the wire's counts, `(gyro, accel)`. +type Counts = ([i16; 3], [i16; 3]); + +fn counts(m: &Motion) -> Counts { + let scale = |v: [f32; 3], per: f32| v.map(|c| (c * per).clamp(-32768.0, 32767.0) as i16); + ( + scale(m.gyro, GYRO_LSB_PER_RAD_S), + scale(m.accel, MOTION_ACCEL_LSB_PER_G as f32), + ) +} + +/// What each pad last sent. +#[derive(Default)] +pub struct MotionRouter { + sent: [Option; SLOTS], +} + +impl MotionRouter { + /// One pad's latest sample, `None` when the pad is gone: the rich input to send when it + /// changed. A pad's first sample is logged, so a run shows the frame hid reports in. + pub fn step(&mut self, pad: u8, sample: Option) -> Option { + let slot = self.sent.get_mut(usize::from(pad))?; + let Some(m) = sample else { + *slot = None; + return None; + }; + let now = counts(&m); + if *slot == Some(now) { + return None; + } + if slot.is_none() { + tracing::info!("pad {pad}: motion, gyro {:?} rad/s, accel {:?} g", m.gyro, m.accel); + } + *slot = Some(now); + Some(RichInput::Motion { + pad, + gyro: now.0, + accel: now.1, + }) + } + + /// Stops every pad turning on the host, gravity kept: the host holds the last rate it got. + pub fn still_all(&mut self, out: &mut Vec) { + for (pad, slot) in self.sent.iter_mut().enumerate() { + if let Some((gyro, accel)) = slot { + if *gyro != [0; 3] { + *gyro = [0; 3]; + out.push(RichInput::Motion { + pad: pad as u8, + gyro: [0; 3], + accel: *accel, + }); + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn still() -> Motion { + Motion { + gyro: [0.0; 3], + accel: [0.0, 1.0, 0.0], + } + } + + #[test] + fn samples_go_out_in_the_wire_units_once_each() { + let mut r = MotionRouter::default(); + let turning = Motion { + gyro: [std::f32::consts::PI, 0.0, -100.0], + ..still() + }; + let Some(RichInput::Motion { pad, gyro, accel }) = r.step(1, Some(turning)) else { + panic!("a first sample goes out"); + }; + // π rad/s is 180 °/s, 20 counts each; past the range clamps. + assert_eq!((pad, gyro, accel), (1, [3600, 0, -32768], [0, 10_000, 0])); + assert!(r.step(1, Some(turning)).is_none(), "an unchanged sample stays home"); + } + + #[test] + fn still_all_stops_the_turning_and_keeps_gravity() { + let mut r = MotionRouter::default(); + let turning = Motion { + gyro: [1.0, 0.0, 0.0], + ..still() + }; + r.step(0, Some(turning)); + r.step(2, Some(still())); + let mut out = Vec::new(); + r.still_all(&mut out); + assert_eq!( + out, + [RichInput::Motion { + pad: 0, + gyro: [0; 3], + accel: [0, 10_000, 0] + }] + ); + out.clear(); + r.still_all(&mut out); + assert!(out.is_empty()); + assert!(r.step(0, Some(turning)).is_some(), "turning again goes out"); + } + + #[test] + fn a_pad_that_leaves_starts_over() { + let mut r = MotionRouter::default(); + r.step(0, Some(still())); + assert!(r.step(0, None).is_none()); + assert!(r.step(0, Some(still())).is_some()); + assert!(r.step(9, Some(still())).is_none(), "no such slot"); + } +} diff --git a/crates/pf-nx-app/src/runtime/pads.rs b/crates/pf-nx-app/src/runtime/pads.rs index 639923e..d91ff4e 100644 --- a/crates/pf-nx-app/src/runtime/pads.rs +++ b/crates/pf-nx-app/src/runtime/pads.rs @@ -136,6 +136,20 @@ impl PadRouter { } } + /// Unplugs every pad from the host and plugs it in again as `kind`: the dial's Controller + /// type, for this stream only. The slots stay, so a button held through it is not pressed + /// again: the A that picked the type would pick the next one, a frame later, until let go. + pub fn replug_all(&mut self, kind: GamepadType, wire: &mut Vec) { + for (index, slot) in self.slots.iter_mut().enumerate() { + if let Some(slot) = slot { + let pad = index as u8; + release_into(pad, &slot.last, wire); + wire.push(remove_event(pad)); + wire.extend(arrival_event(resolve_kind(kind, slot.last.kind), pad)); + } + } + } + /// Lets go of everything the host holds and forgets the pads, so the next step re-declares /// them. For focus loss and every way out of a stream. pub fn release_all(&mut self, wire: &mut Vec) { @@ -383,6 +397,46 @@ mod tests { assert!(routed.is_empty(), "re-arms only after a release"); } + #[test] + fn replug_plugs_in_as_the_new_kind_and_a_held_button_stays_held() { + let mut r = PadRouter::default(); + let (mut wire, mut routed) = (Vec::new(), Vec::new()); + let now = Instant::now(); + r.step( + &[pad(gamepad::BTN_A)], + now, + false, + GamepadType::Auto, + &mut wire, + &mut routed, + ); + wire.clear(); + r.replug_all(GamepadType::DualSense, &mut wire); + let kinds: Vec = wire.iter().map(|e| e.kind).collect(); + assert_eq!( + kinds, + [ + InputKind::GamepadButton, + InputKind::GamepadRemove, + InputKind::GamepadArrival + ] + ); + assert_eq!(wire[2].code, u32::from(GamepadType::DualSense.to_core().to_u8())); + wire.clear(); + r.step( + &[pad(gamepad::BTN_A)], + now, + false, + GamepadType::DualSense, + &mut wire, + &mut routed, + ); + assert!( + wire.is_empty() && routed.is_empty(), + "the held button is not a new press" + ); + } + #[test] fn release_all_centres_sticks_and_lifts_buttons() { let mut r = PadRouter::default(); diff --git a/crates/pf-nx-app/src/runtime/stream.rs b/crates/pf-nx-app/src/runtime/stream.rs index 22abcf7..8231fde 100644 --- a/crates/pf-nx-app/src/runtime/stream.rs +++ b/crates/pf-nx-app/src/runtime/stream.rs @@ -1,9 +1,10 @@ //! The stream: the picture on the display's stream layer, the dial and the stats over it, and //! the pads on an input thread of their own. //! -//! The main thread presents at the display's rate. The input thread polls the pads at 250 Hz, -//! sends their input straight to the host, applies the host's rumble, and hands the main -//! thread only what it must act on (the dial, the escape chord). +//! The main thread presents at the display's rate. The input thread polls the pads, their +//! motion and the touch screen at 250 Hz, sends their input straight to the host, applies the +//! host's rumble, and hands the main thread only what it must act on (the dial, the escape +//! chord). use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, Receiver, Sender}; @@ -12,17 +13,20 @@ use std::time::{Duration, Instant}; use pf_client_core::ring::{RingCommand, RingFacts, RingInput}; use pf_console_ui::theme; -use punktfunk_core::client::PunktfunkEndReason; +use punktfunk_core::client::{NativeClient, PunktfunkEndReason}; use punktfunk_core::hud::StatsVerbosity; +use punktfunk_core::input::PadMouseMode; use super::connect::{self, ConnectOutcome}; use super::hud::StatsOverlay; -use super::pads::{PadRouter, Routed}; +use super::motion::MotionRouter; +use super::pads::{resolve_kind, PadRouter, Routed, SLOTS}; use super::testrun::Scenario; +use super::touch::{Screen, TouchRouter}; use super::{App, StreamOutcome}; use crate::core::model::GamepadType; use crate::core::settings::NxSettings; -use crate::platform::{Fit, PadFeedback, PadSample, Pads, SystemEvent}; +use crate::platform::{Fit, PadFeedback, PadSample, Pads, SystemEvent, TouchPoint}; use crate::session::{Connected, InputSender}; /// Input thread cadence: 250 Hz, under the pad's own report rate. @@ -37,6 +41,10 @@ enum ToInput { DialOpened, DialClosed, ReleaseAll, + /// The output's new size, after a dock change. + Output((u32, u32)), + /// The dial's Controller type: every pad plugs in again as this kind. + PadType(GamepadType), } /// The input thread and its channels. Dropping it without [`Self::stop`] leaks the pads. @@ -48,14 +56,14 @@ struct InputThread { } impl InputThread { - fn start(pads: Box, input: InputSender, kind: GamepadType) -> Self { + fn start(pads: Box, input: InputSender, kind: GamepadType, output: (u32, u32)) -> Self { let stop = Arc::new(AtomicBool::new(false)); let (to, rx) = mpsc::channel(); let (tx, from) = mpsc::channel(); let thread_stop = stop.clone(); let handle = std::thread::Builder::new() .name("pf-input".into()) - .spawn(move || input_loop(pads, &input, kind, &thread_stop, &rx, &tx)) + .spawn(move || input_loop(pads, &input, kind, output, &thread_stop, &rx, &tx)) .expect("spawn the input thread"); Self { stop, to, from, handle } } @@ -70,39 +78,85 @@ impl InputThread { fn input_loop( mut pads: Box, input: &InputSender, - kind: GamepadType, + mut kind: GamepadType, + mut output: (u32, u32), stop: &AtomicBool, rx: &Receiver, tx: &Sender, ) -> Box { let mut router = PadRouter::default(); + let mut motion = MotionRouter::default(); + let mut touch = TouchRouter::default(); let mut batch: Vec = Vec::new(); + let mut fingers: Vec = Vec::new(); let mut wire = Vec::new(); + let mut rich = Vec::new(); let mut routed = Vec::new(); let mut dial_open = false; + // Pads whose motion the host's pad cannot take, logged once each. + let mut no_motion = [false; SLOTS]; let mut next = Instant::now(); while !stop.load(Ordering::Relaxed) { wire.clear(); + rich.clear(); routed.clear(); for msg in rx.try_iter() { match msg { ToInput::DialOpened => { dial_open = true; router.dial_opened(&mut wire); + touch.release_all(&mut wire); + motion.still_all(&mut rich); } ToInput::DialClosed => { dial_open = false; router.dial_closed(); } - ToInput::ReleaseAll => router.release_all(&mut wire), + ToInput::ReleaseAll => { + router.release_all(&mut wire); + touch.release_all(&mut wire); + motion.still_all(&mut rich); + } + ToInput::Output(size) => output = size, + ToInput::PadType(next) => { + kind = next; + router.replug_all(kind, &mut wire); + motion.still_all(&mut rich); + } } } batch.clear(); pads.poll(&mut batch); router.step(&batch, Instant::now(), dial_open, kind, &mut wire, &mut routed); + if !dial_open { + let client = input.client(); + for pad in 0..SLOTS as u8 { + let Some(sample) = batch.iter().find(|s| s.slot == pad) else { + motion.step(pad, None); + continue; + }; + if !motion_reaches(kind, sample, client) { + if !std::mem::replace(&mut no_motion[usize::from(pad)], true) { + tracing::info!("pad {pad}: the host's pad takes no motion; the gyro stays home"); + } + continue; + } + rich.extend(motion.step(pad, pads.motion(pad))); + } + pads.touches(&mut fingers); + let mode = client.mode(); + let screen = Screen { + output, + frame: (mode.width, mode.height), + }; + touch.step(&fingers, screen, &mut wire); + } for ev in &wire { input.send(ev); } + for r in rich.drain(..) { + let _ = input.client().send_rich_input(r); + } for r in routed.drain(..) { let _ = tx.send(r); } @@ -117,14 +171,30 @@ fn input_loop( } wire.clear(); router.release_all(&mut wire); + touch.release_all(&mut wire); for ev in &wire { input.send(ev); } + motion.still_all(&mut rich); + for r in rich.drain(..) { + let _ = input.client().send_rich_input(r); + } input.release(); pads.release_all(); pads } +/// Whether the host's pad for `sample` takes motion. A pad that declared no kind of its own +/// (`Auto`) is the session's pad. +fn motion_reaches(kind: GamepadType, sample: &PadSample, client: &punktfunk_core::client::NativeClient) -> bool { + use punktfunk_core::config::{pad_motion_reaches, GamepadPref}; + let declared = match resolve_kind(kind, sample.kind).to_core() { + GamepadPref::Auto => client.requested_gamepad, + pref => pref, + }; + pad_motion_reaches(declared, client.requested_gamepad, client.resolved_gamepad) +} + /// The host's rumble, onto the pads. The plane drains every tick: a bounded plane left unread /// drops its newest events, including the zero that stops a motor. fn apply_feedback(pads: &mut dyn Pads, input: &InputSender) { @@ -235,7 +305,10 @@ fn stream( tracing::error!("stream: the pads are still held by another loop"); return Next::End(StreamOutcome::ReturnToMenu(None)); }; - let input = InputThread::start(pads, connected.input(), settings.gamepad_type()); + // The dial's Controller type steps it for this stream; the next connect reads the setting. + let mut pad_type = settings.gamepad_type(); + let mut plugged = pad_type; + let input = InputThread::start(pads, connected.input(), pad_type, app.display.size()); let fonts = match pf_console_ui::theme::build_fonts() { Ok(f) => Some(f), Err(e) => { @@ -255,8 +328,11 @@ fn stream( // The output size the overlay and the stream mode were last fitted to. let mut fitted = (output_w, output_h); let (mut width, mut height) = app.display.ui_size(); - ring.set_facts(&RingFacts { + let mut facts = RingFacts { overlay_actions: settings.overlay_actions.clone(), + // The screen passes touches straight through; the trackpad model is not here yet. + touch_mode: "touch".into(), + host_accepts_touch: true, stats_tier: settings.stats_verbosity().label().into(), mode: (mode.width, mode.height, mode.refresh_hz), native_mode: (output_w, output_h, app.display.refresh().hz()), @@ -264,7 +340,8 @@ fn stream( fp_hex: fp_hex(&target.fingerprint), pad_type: settings.gamepad_type().to_core(), ..RingFacts::default() - }); + }; + ring.set_facts(&facts); // A test run records everything; otherwise the player's stats setting. let initial_tier = if app.test_run.is_some() { StatsVerbosity::Detailed @@ -299,13 +376,22 @@ fn stream( fitted = app.display.size(); (width, height) = app.display.ui_size(); scale = overlay_scale(height); + let _ = input.to.send(ToInput::Output(fitted)); follow_output(app, connected, settings); } if let Some(n) = take_routed(&input, &mut ring, width, height) { break n; } + // What the dial shows follows what its slots changed. + let tier = hud.tier.label(); + let kind = pad_type.to_core(); + if follow_pad_mouse(connected.client(), &mut facts) || facts.stats_tier != tier || facts.pad_type != kind { + facts.stats_tier = tier.into(); + facts.pad_type = kind; + ring.set_facts(&facts); + } ring.tick(); - if let Some(n) = take_ring_commands(&mut ring, &mut hud) { + if let Some(n) = take_ring_commands(&mut ring, &mut hud, connected.client(), &mut pad_type) { break n; } for cmd in ring.take_cmds() { @@ -314,6 +400,12 @@ fn stream( let open = ring.open(); if open != dial_was_open { dial_was_open = open; + // The host re-plugs the pad once, for the type the dial closed on, not at every step. + if !open && pad_type != plugged { + plugged = pad_type; + tracing::info!("dial: controller type {:?} for this stream", pad_type); + let _ = input.to.send(ToInput::PadType(pad_type)); + } let _ = input .to .send(if open { ToInput::DialOpened } else { ToInput::DialClosed }); @@ -419,19 +511,55 @@ fn take_routed(input: &InputThread, ring: &mut pf_console_ui::Ring, width: u32, None } -fn take_ring_commands(ring: &mut pf_console_ui::Ring, hud: &mut StatsOverlay) -> Option { +fn take_ring_commands( + ring: &mut pf_console_ui::Ring, + hud: &mut StatsOverlay, + client: &NativeClient, + pad_type: &mut GamepadType, +) -> Option { while let Some(cmd) = ring.take_command() { match cmd { RingCommand::EndStream | RingCommand::DisconnectLinger => { return Some(Next::End(StreamOutcome::ReturnToMenu(None))); } RingCommand::CycleStats => hud.cycle(), + RingCommand::CyclePadMouse => toggle_pad_mouse(client), + // Only the slot steps here; the pad is re-plugged when the dial closes. + RingCommand::CyclePadType => *pad_type = pad_type.next_on_dial(), + // Touch is the screen's one model so far: nothing to step to. + RingCommand::CycleTouchMode => {} other => tracing::info!("dial: {other:?} does nothing on this console"), } } None } +/// Copies the controller-mouse facts the dial shows from the session; whether they changed. +fn follow_pad_mouse(client: &NativeClient, facts: &mut RingFacts) -> bool { + let target = client.live_pads(); + let now = ( + client.access_grants() & punktfunk_core::quic::GRANT_POINTER != 0, + target, + client.pad_mouse_mode(target), + ); + if (facts.pointer_granted, facts.pad_mouse_target, facts.pad_mouse) == now { + return false; + } + (facts.pointer_granted, facts.pad_mouse_target, facts.pad_mouse) = now; + true +} + +/// Controller mouse on every pad, or off. The dial's touchpad step is skipped: no Switch pad +/// has a touchpad to drive the pointer with. +fn toggle_pad_mouse(client: &NativeClient) { + let pads = client.live_pads(); + let on = client.pad_mouse_mode(pads) != PadMouseMode::Full; + match client.set_pad_mouse(if on { pads } else { 0 }) { + Ok(()) => tracing::info!("dial: controller mouse {}", if on { "on" } else { "off" }), + Err(e) => tracing::warn!("dial: controller mouse: {e}"), + } +} + /// What the presenter reported over the session, for a test run's `perf.json`, and over the /// current window, for the `stats:` log line. #[derive(Default)] diff --git a/crates/pf-nx-app/src/runtime/touch.rs b/crates/pf-nx-app/src/runtime/touch.rs new file mode 100644 index 0000000..945acd6 --- /dev/null +++ b/crates/pf-nx-app/src/runtime/touch.rs @@ -0,0 +1,180 @@ +//! The console's screen as a touch screen on the host: fingers in, `Touch*` events out, placed on +//! the stream's picture. Pure: fingers in, events out. + +use punktfunk_core::input::{InputEvent, InputKind}; +use punktfunk_core::video_fit::{self, VideoFit}; + +use crate::platform::TouchPoint; + +/// Where the picture sits: the output it is drawn in and the stream frame, both in pixels. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Screen { + pub output: (u32, u32), + pub frame: (u32, u32), +} + +impl Screen { + /// A finger, across the screen, in frame pixels. Off the picture it lands on its edge. + fn to_frame(self, x: f32, y: f32) -> (i32, i32) { + let place = video_fit::place(VideoFit::Fit, self.output, self.frame); + let (fx, fy) = place.to_frame( + f64::from(x) * f64::from(self.output.0), + f64::from(y) * f64::from(self.output.1), + ); + let last = |n: u32| f64::from(n.saturating_sub(1)); + ( + fx.round().min(last(self.frame.0)) as i32, + fy.round().min(last(self.frame.1)) as i32, + ) + } + + /// The frame size as the touch events carry it. + fn flags(self) -> u32 { + ((self.frame.0 & 0xffff) << 16) | (self.frame.1 & 0xffff) + } +} + +/// A finger the host holds: the console's id, the host's slot, where it was last sent. +struct Held { + id: u32, + slot: u32, + at: (i32, i32), +} + +/// The fingers the host holds, by the compact slots the wire reuses after an up. +#[derive(Default)] +pub struct TouchRouter { + held: Vec, +} + +impl TouchRouter { + /// Diffs the fingers down now against the last step: ups first, then downs and moves. + pub fn step(&mut self, fingers: &[TouchPoint], screen: Screen, wire: &mut Vec) { + self.held.retain(|h| { + let stays = fingers.iter().any(|f| f.id == h.id); + if !stays { + wire.push(up(h.slot)); + } + stays + }); + if screen.frame.0 == 0 || screen.frame.1 == 0 { + return; + } + for f in fingers { + let at = screen.to_frame(f.x, f.y); + if let Some(h) = self.held.iter_mut().find(|h| h.id == f.id) { + if h.at != at { + h.at = at; + wire.push(event(InputKind::TouchMove, h.slot, at, screen.flags())); + } + continue; + } + let slot = (0..).find(|s| self.held.iter().all(|h| h.slot != *s)).unwrap_or(0); + self.held.push(Held { id: f.id, slot, at }); + wire.push(event(InputKind::TouchDown, slot, at, screen.flags())); + } + } + + /// Lifts every finger the host holds. + pub fn release_all(&mut self, wire: &mut Vec) { + wire.extend(self.held.drain(..).map(|h| up(h.slot))); + } +} + +fn event(kind: InputKind, slot: u32, (x, y): (i32, i32), flags: u32) -> InputEvent { + InputEvent { + kind, + _pad: [0; 3], + code: slot, + x, + y, + flags, + } +} + +fn up(slot: u32) -> InputEvent { + event(InputKind::TouchUp, slot, (0, 0), 0) +} + +#[cfg(test)] +mod tests { + use super::*; + + const HANDHELD: Screen = Screen { + output: (1280, 720), + frame: (1280, 720), + }; + + fn finger(id: u32, x: f32, y: f32) -> TouchPoint { + TouchPoint { id, x, y } + } + + fn kinds(wire: &[InputEvent]) -> Vec<(InputKind, u32, i32, i32)> { + wire.iter().map(|e| (e.kind, e.code, e.x, e.y)).collect() + } + + #[test] + fn a_tap_goes_down_and_up_on_the_picture() { + let mut r = TouchRouter::default(); + let mut wire = Vec::new(); + r.step(&[finger(7, 0.5, 0.25)], HANDHELD, &mut wire); + r.step(&[finger(7, 0.5, 0.25)], HANDHELD, &mut wire); + r.step(&[], HANDHELD, &mut wire); + assert_eq!( + kinds(&wire), + [(InputKind::TouchDown, 0, 640, 180), (InputKind::TouchUp, 0, 0, 0)] + ); + assert_eq!(wire[0].flags, (1280 << 16) | 720); + } + + #[test] + fn a_second_finger_takes_the_next_slot_and_slots_are_reused() { + let mut r = TouchRouter::default(); + let mut wire = Vec::new(); + r.step(&[finger(3, 0.0, 0.0), finger(9, 1.0, 1.0)], HANDHELD, &mut wire); + r.step(&[finger(9, 0.5, 0.5)], HANDHELD, &mut wire); + r.step(&[finger(9, 0.5, 0.5), finger(4, 0.25, 0.5)], HANDHELD, &mut wire); + assert_eq!( + kinds(&wire), + [ + (InputKind::TouchDown, 0, 0, 0), + (InputKind::TouchDown, 1, 1279, 719), + (InputKind::TouchUp, 0, 0, 0), + (InputKind::TouchMove, 1, 640, 360), + (InputKind::TouchDown, 0, 320, 360), + ] + ); + } + + #[test] + fn a_smaller_frame_scales_and_bars_clamp() { + let mut r = TouchRouter::default(); + let mut wire = Vec::new(); + // A 4:3 frame on the 16:9 screen: bars left and right, the middle is the frame's. + let screen = Screen { + output: (1280, 720), + frame: (960, 720), + }; + r.step(&[finger(1, 0.5, 0.5), finger(2, 0.0, 0.5)], screen, &mut wire); + assert_eq!( + kinds(&wire), + [(InputKind::TouchDown, 0, 480, 360), (InputKind::TouchDown, 1, 0, 360)] + ); + } + + #[test] + fn release_lifts_everything() { + let mut r = TouchRouter::default(); + let mut wire = Vec::new(); + r.step(&[finger(1, 0.5, 0.5), finger(2, 0.1, 0.1)], HANDHELD, &mut wire); + wire.clear(); + r.release_all(&mut wire); + assert_eq!( + kinds(&wire), + [(InputKind::TouchUp, 0, 0, 0), (InputKind::TouchUp, 1, 0, 0)] + ); + wire.clear(); + r.release_all(&mut wire); + assert!(wire.is_empty()); + } +} diff --git a/crates/pf-nx-app/src/session/audio.rs b/crates/pf-nx-app/src/session/audio.rs index 545e20c..01ac2d4 100644 --- a/crates/pf-nx-app/src/session/audio.rs +++ b/crates/pf-nx-app/src/session/audio.rs @@ -32,6 +32,34 @@ pub struct AudioStage { last: usize, /// libopus's output, one frame at the widest layout. A field so it is not zeroed per packet. pcm: Box<[f32; SAMPLES_PER_FRAME * MAX_CHANNELS]>, + /// The level of the host's packets since [`AudioStage::take_level`]. + meter: Meter, +} + +/// Peak and RMS of decoded samples, full scale 1.0. +#[derive(Default)] +struct Meter { + peak: f32, + sum_sq: f64, + samples: u64, +} + +/// The audio's level over a window, in dBFS: 0 is full scale. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Level { + pub peak_db: f32, + pub rms_db: f32, +} + +/// Silence reads as this rather than minus infinity. +const FLOOR_DB: f32 = -120.0; + +fn dbfs(amplitude: f64) -> f32 { + if amplitude > 0.0 { + ((20.0 * amplitude.log10()) as f32).max(FLOOR_DB) + } else { + FLOOR_DB + } } impl AudioStage { @@ -60,6 +88,7 @@ impl AudioStage { f32: Vec::with_capacity(SAMPLES_PER_FRAME * MAX_CHANNELS * 2), last: 0, pcm: Box::new([0.0; SAMPLES_PER_FRAME * MAX_CHANNELS]), + meter: Meter::default(), }) } @@ -78,6 +107,7 @@ impl AudioStage { channels, gaps, sink, + meter, } = self; let channels = *channels; let missing = gaps.missing_before(seq); @@ -103,6 +133,11 @@ impl AudioStage { f32.extend_from_slice(decoded); } } + for &s in &pcm[..*last] { + meter.peak = meter.peak.max(s.abs()); + meter.sum_sq += f64::from(s) * f64::from(s); + } + meter.samples += *last as u64; if missing == 0 { return sink.feed(Samples::F32(&pcm[..*last]), stamp(0)); } @@ -119,9 +154,19 @@ impl AudioStage { self.sink.underruns() } - /// Peak sample of the last decoded frame: tells "the host sends silence" from "no sound". - pub fn peak(&self) -> f32 { - self.pcm[..self.last].iter().fold(0f32, |m, &s| m.max(s.abs())) + /// The level of the host's packets since the last call, concealment left out: tells "the + /// host sends it quiet" from "the console plays it quiet". + pub fn take_level(&mut self) -> Level { + let m = std::mem::take(&mut self.meter); + let mean_sq = if m.samples == 0 { + 0.0 + } else { + m.sum_sq / m.samples as f64 + }; + Level { + peak_db: dbfs(f64::from(m.peak)), + rms_db: dbfs(mean_sq.sqrt()), + } } } @@ -172,7 +217,15 @@ mod tests { let fed = sink.0.lock().unwrap().clone(); assert_eq!(fed[0], (SAMPLES_PER_FRAME * 2, 100_000_000)); assert_eq!(fed[1], (SAMPLES_PER_FRAME * 2 * 2, 105_000_000)); - assert!(stage.peak() > 0.0); + assert!(stage.take_level().peak_db > FLOOR_DB); + assert_eq!(stage.take_level().peak_db, FLOOR_DB, "the window starts over"); + } + + #[test] + fn levels_read_in_dbfs() { + assert_eq!(dbfs(1.0), 0.0); + assert!((dbfs(0.5) + 6.02).abs() < 0.01); + assert_eq!(dbfs(0.0), FLOOR_DB); } /// A width the sink does not play, or a layout this build does not know, is refused. diff --git a/crates/pf-nx-app/src/session/pump.rs b/crates/pf-nx-app/src/session/pump.rs index 69a49ea..cc3bde4 100644 --- a/crates/pf-nx-app/src/session/pump.rs +++ b/crates/pf-nx-app/src/session/pump.rs @@ -488,15 +488,17 @@ pub(super) fn audio_pump(client: &NativeClient, stage: &mut AudioStage, stop: &A packets = packets.wrapping_add(1); // ~15s, matching the video heartbeat (packets are 5ms each). if packets % 3_000 == 0 { - tracing::debug!( - "audio: {what}, depth={}, underruns={}, peak={:.4}", + let level = stage.take_level(); + tracing::info!( + "audio: {what}, depth={}, underruns={}, level peak={:.1} dBFS rms={:.1} dBFS", stage .depth_ms() .map_or_else(|| "n/a".to_string(), |ms| format!("{ms}ms")), stage .underruns() .map_or_else(|| "n/a".to_string(), |count| count.to_string()), - stage.peak(), + level.peak_db, + level.rms_db, ); } }); diff --git a/crates/pf-nx-host/src/lib.rs b/crates/pf-nx-host/src/lib.rs index 13c055b..4955a57 100644 --- a/crates/pf-nx-host/src/lib.rs +++ b/crates/pf-nx-host/src/lib.rs @@ -347,6 +347,7 @@ pub fn platform(opts: &Options) -> Result { firmware: String::new(), gpu, ui, + applet_mode: false, }, }) } diff --git a/crates/pf-nx-platform/src/console/audio.rs b/crates/pf-nx-platform/src/console/audio.rs index 8cbb1a8..08b1bee 100644 --- a/crates/pf-nx-platform/src/console/audio.rs +++ b/crates/pf-nx-platform/src/console/audio.rs @@ -18,7 +18,10 @@ impl AudioOut for NxAudio { "audout plays 48 kHz stereo, not {channels} channels at {rate} Hz" ); let out = audout::AudioOut::open(BUFFERS, GRAIN_FRAMES)?; - tracing::info!("audio: audout 48 kHz stereo S16, {BUFFERS} buffers of {GRAIN_FRAMES} frames"); + tracing::info!( + "audio: audout 48 kHz stereo S16, {BUFFERS} buffers of {GRAIN_FRAMES} frames, volume {:?}", + out.volume() + ); Ok(Box::new(Port { out, pcm: vec![0; GRAIN_FRAMES * 2], diff --git a/crates/pf-nx-platform/src/console/mod.rs b/crates/pf-nx-platform/src/console/mod.rs index a2a39be..9e64f75 100644 --- a/crates/pf-nx-platform/src/console/mod.rs +++ b/crates/pf-nx-platform/src/console/mod.rs @@ -116,6 +116,10 @@ pub fn platform(kind: DisplayKind) -> Result { firmware: setsys::firmware_version().unwrap_or_default(), gpu, ui, + applet_mode: !matches!( + applet::applet_type(), + applet::AppletType::Application | applet::AppletType::SystemApplication + ), }; Ok(Platform { display, diff --git a/crates/pf-nx-platform/src/console/pads.rs b/crates/pf-nx-platform/src/console/pads.rs index c1fd62f..c076d8b 100644 --- a/crates/pf-nx-platform/src/console/pads.rs +++ b/crates/pf-nx-platform/src/console/pads.rs @@ -1,7 +1,7 @@ //! `Pads` over hid: four player slots, the handheld Joy-Cons merged into the first, HD rumble //! on every pad's pair of actuators. -use pf_nx_app::platform::{PadFeedback, PadKind, PadSample, Pads}; +use pf_nx_app::platform::{Motion, PadFeedback, PadKind, PadSample, Pads, TouchPoint}; use pf_nx_sys::hid::{self, Style, Vibration}; use crate::pad_map; @@ -10,12 +10,19 @@ const PLAYERS: u8 = 4; pub struct NxPads { pads: hid::Pads, + /// Slots the host has rumbled since the last [`Pads::release_all`]: the log names the first + /// rumble of each, so a still pad tells "nothing asked" from "asked and not felt". + rumbled: u8, + /// hid's fingers, reused across polls. + fingers: Vec, } impl NxPads { pub fn new() -> anyhow::Result { Ok(Self { pads: hid::Pads::init(PLAYERS)?, + rumbled: 0, + fingers: Vec::new(), }) } @@ -44,8 +51,6 @@ fn kind(style: Style) -> PadKind { } } -// TODO(seam): gyro + touch. hid reads both (`Pads::sixaxis`, `hid::touches`); the seams carry -// neither at this pin. impl Pads for NxPads { fn poll(&mut self, out: &mut Vec) { self.pads.update(); @@ -73,12 +78,33 @@ impl Pads for NxPads { fn feedback(&mut self, slot: u8, feedback: &PadFeedback) { let &PadFeedback::Rumble { low, high } = feedback; + let bit = 1u8.checked_shl(u32::from(slot)).unwrap_or(0); + if (low, high) != (0, 0) && self.rumbled & bit == 0 { + self.rumbled |= bit; + tracing::info!("pad {slot}: the host rumbles it, low {low} high {high}"); + } self.rumble(slot, low, high); } fn release_all(&mut self) { + self.rumbled = 0; for slot in 0..PLAYERS { self.rumble(slot, 0, 0); } } + + fn motion(&mut self, slot: u8) -> Option { + let s = self.pads.sixaxis(usize::from(slot))?; + let (gyro, accel) = pad_map::motion(s.accel, s.gyro); + Some(Motion { gyro, accel }) + } + + fn touches(&mut self, out: &mut Vec) { + hid::touches(&mut self.fingers); + out.clear(); + out.extend(self.fingers.iter().map(|f| { + let (x, y) = pad_map::touch(f.x, f.y); + TouchPoint { id: f.id, x, y } + })); + } } diff --git a/crates/pf-nx-platform/src/pad_map.rs b/crates/pf-nx-platform/src/pad_map.rs index c81a18f..388d816 100644 --- a/crates/pf-nx-platform/src/pad_map.rs +++ b/crates/pf-nx-platform/src/pad_map.rs @@ -80,6 +80,29 @@ pub fn rumble(low: u16, high: u16) -> (f32, f32) { (amp(low), amp(high)) } +/// One hid motion sample into the wire's frame, as `(gyro in rad/s, accel in g)`. hid's frame, +/// for a pad held level: +x right, +y away from the player, +z up; the wire's is SDL's: +x +/// right, +y up, +z toward the player. Both are right-handed, so a rotation turns with its +/// axis. hid's gyro counts rotations per second. hid's accel reads gravity's pull, -1 on z +/// lying still; the wire's reads the table's push, +1 on y, so it changes sign. +// TODO(verify): the gyro's units; its axes turn the game's camera the right way (2026-10-31). +pub fn motion(accel: [f32; 3], gyro: [f32; 3]) -> ([f32; 3], [f32; 3]) { + let frame = |[x, y, z]: [f32; 3]| [x, z, -y]; + (frame(gyro).map(|v| v * std::f32::consts::TAU), frame(accel).map(|v| -v)) +} + +/// The touch screen's size in the pixels hid reports. +// TODO(verify): with `hid::Touch`, that these are panel pixels. +pub const PANEL: (f32, f32) = (1280.0, 720.0); + +/// One hid finger's position across the screen, 0.0..=1.0. +pub fn touch(x: u32, y: u32) -> (f32, f32) { + ( + (x as f32 / PANEL.0).clamp(0.0, 1.0), + (y as f32 / PANEL.1).clamp(0.0, 1.0), + ) +} + #[cfg(test)] mod tests { use super::*; @@ -110,4 +133,27 @@ mod tests { assert_eq!(stick(-1200), -1200); assert_eq!(rumble(0, u16::MAX), (0.0, 1.0)); } + + #[test] + fn motion_turns_into_the_wire_frame() { + // Lying still: hid reads gravity's pull on -z, the wire the table's push on +y. + assert_eq!(motion([0.0, 0.0, -1.0], [0.0; 3]).1, [-0.0, 1.0, 0.0]); + // Standing on its bottom edge: the push is along hid's +y, away from the player. + assert_eq!(motion([0.0, -1.0, 0.0], [0.0; 3]).1, [-0.0, -0.0, -1.0]); + // Tipping the far edge up turns about +x; a quarter turn a second is π/2 rad/s. + assert_eq!( + motion([0.0; 3], [0.25, 0.0, 0.0]).0, + [std::f32::consts::FRAC_PI_2, 0.0, 0.0] + ); + // A turn to the left: about hid's +z, the wire's +y. + assert_eq!(motion([0.0; 3], [0.0, 0.0, 1.0]).0, [0.0, std::f32::consts::TAU, 0.0]); + // About hid's +y, the axis away from the player: the wire's -z. + assert_eq!(motion([0.0; 3], [0.0, 1.0, 0.0]).0, [0.0, 0.0, -std::f32::consts::TAU]); + } + + #[test] + fn touches_span_the_screen() { + assert_eq!(touch(640, 360), (0.5, 0.5)); + assert_eq!(touch(1400, 0), (1.0, 0.0)); + } } diff --git a/crates/pf-nx-sys/src/audout.rs b/crates/pf-nx-sys/src/audout.rs index 7665177..d563591 100644 --- a/crates/pf-nx-sys/src/audout.rs +++ b/crates/pf-nx-sys/src/audout.rs @@ -46,6 +46,7 @@ mod ffi { pub fn audoutGetSampleRate() -> u32; pub fn audoutGetChannelCount() -> u32; pub fn audoutGetPcmFormat() -> u32; + pub fn audoutGetAudioOutVolume(volume: *mut f32) -> u32; } } @@ -159,6 +160,14 @@ impl AudioOut { Ok(()) } + /// The gain audout applies to this title's output; `None` before firmware 6.0.0. + pub fn volume(&self) -> Option { + let mut volume = 0f32; + // SAFETY: audout is open while `self` lives; the out-pointer is a valid local. + let rc = unsafe { ffi::audoutGetAudioOutVolume(&mut volume) }; + (rc == 0).then_some(volume) + } + /// Frames appended and not yet reclaimed. A buffer audout has played counts until the next /// [`AudioOut::push`] reclaims it. pub fn queued_frames(&self) -> usize { diff --git a/docs/NOTES.md b/docs/NOTES.md index ffe101c..9413796 100644 --- a/docs/NOTES.md +++ b/docs/NOTES.md @@ -175,3 +175,33 @@ record their clocks; the numbers below are under that profile unless an item say latency intent on the same Wi-Fi presented 47–58 a second at 5–6 ms decode to present, and in the earlier runs the automatic bitrate's climbs to 22–25 Mb/s preceded every 60–110 ms delay spike (title at `b8e2986` and `517083f`, 2026-10-31). +- Sleep mid-stream crashes the title on wake. Nothing reached the log between the last stats + line and the wake 40 s later, so the title went to sleep without a word from the applet. On + wake the stream ended on a present error, and the teardown's first wake of tokio's I/O + driver failed: the loopback waker's send errs after sleep, tokio's `expect` panicked, the + panic hook's backtrace aborted (the title builds without unwind tables, so it never logged + the panic), and the logger thread wrote to `sdmc:` after `__appExit` had unmounted it: a data + abort at 0x20, `_write_r` through a null devoptab. The host saw the session drop and gave the + PC its display back (title at `b8e2986`, 2026-10-31; Atmosphère report + `01793482713_05446530aca7e000`). The hook now flushes the message before the walk, and the + waker leg treats a failed send as a wake that reached no one. +- Launched from the album (hbloader in applet mode) the title died the same way, a data abort + at 0x20 in `_write_r` from the logger thread after `__appExit`, so something panicked first; + the title log was lost to the next launch, which truncates it. The console needed a reboot + into the CFW afterwards. A second sleep mid-stream on the same build ended as the first + (title at `b8e2986`, 2026-10-31; Atmosphère reports `01793488062_010000000000100d` and + `01793488512_05446530aca7e000`). Applet mode is left untested until a build that logs its + panics runs there. +- On the WP4 build two sleeps mid-stream (37 s and 28 s) no longer crash the title. Again no + applet message or hook arrived before sleep; on wake the session's socket read failed with + "Network interface is not configured" (os error 115), the session ended as lost, and the + stream redialled the host and was back in 0.3 s, so the player lands in the game, not on + Hosts (plan D12 expected Hosts). The touch screen drives the host's desktop as touch, the + host's rumble reaches the Joy-Cons, and the gyro turns a game's camera the right way on the + pitch and yaw axes. hid's accelerometer reads gravity's pull, [0, 0, -1] g lying still, + which the first build sent upside down as [0, -1, 0]. audout reports volume 1.0; the host's + audio peaked at -4 to -12 dBFS in play, so the level arrives near full scale. Audio + underruns reached 47 in the first session's three minutes and 38 in the last one's twenty + (title at `be98e16`, 2026-10-31). +- With the accelerometer's sign flipped, a pad lying still reads [0, 1, 0] g on the wire, as + SDL's frame has it (title at `8a18316`, 2026-10-31). diff --git a/docs/improvements.md b/docs/improvements.md index 0fe10c3..63f120c 100644 --- a/docs/improvements.md +++ b/docs/improvements.md @@ -1,7 +1,7 @@ # Improvements Changes the console's runs point to, for later. Each needs the maintainer's yes before it lands: -the first changes what a player sees by default, the second lives in punktfunk's core. +the first changes what a player sees by default, the others live in punktfunk's shared crates. ## The smoothness buffer's Automatic on the Switch @@ -28,3 +28,25 @@ on congestion but never probes past what a handheld's Wi-Fi carries. The control (`punktfunk-core::abr`), so this is a change upstream, not here. Open: what the ceiling should be docked at 1080p, which needs more than 720p. + +## The Nintendo controller types in the shared lists + +On the Switch a player holding Joy-Cons can ask the host for a Pro Controller only through the +dial: the Switch's own step (`GamepadType::next_on_dial`) goes Automatic, Joy-Con pair, Switch +Pro, then the shared order. Everything else about the two kinds lives in the shared crates and +leaves them out: + +- Settings' Controller type offers Automatic, Xbox 360, Xbox One, DualSense, DualShock 4, Steam + Deck and Steam Controller 2 (`pf-console-ui`, `screens/settings/rows.rs`, `PAD_TYPES`), so the + console cannot save either Nintendo kind. +- The dial names a kind through `pf-client-core::overlay_actions::pad_type_label`, which has no + name for either, so the slot reads `joyconpair` and `switchpro`. +- The dial's shared order (`overlay_actions::PAD_TYPE_CYCLE`) has neither, which is why the + Switch steps its own. + +The proposal is a monorepo PR that adds "Joy-Con pair" and "Switch Pro" to the labels, to the +Settings list on the platforms that present them, and to the shared order there, after which +the Switch's own step goes. The Windows host built a Joy-Con pair in every console session +(NOTES, 2026-10-31); a Switch Pro there is untested. + +Open: whether every host can build a Switch Pro, or the lists should offer it per host. diff --git a/docs/plan.md b/docs/plan.md index f2f289b..f90ec75 100644 --- a/docs/plan.md +++ b/docs/plan.md @@ -1,7 +1,7 @@ # Nintendo Switch client — a Rust homebrew title on NVDEC > **Status: designed 2026-10-28; WP0's probe passed on the console and WP1 closed on 2026-10-29 -> (`docs/NOTES.md`, §4). WP2 is next.** Target: the Switch 1 (Erista and Mariko, Tegra +> (`docs/NOTES.md`, §4); WP2–WP4 closed by 2026-11-01. WP5 is next.** Target: the Switch 1 (Erista and Mariko, Tegra > X1), on Atmosphère, as an `.nro`. The Switch 2 is a later leg behind the same seams (§9); no > modded one exists to test on. The maintainer's fellow developer holds the only test device, so > every work package is written for a developer who never holds the console — the PS5 client's @@ -260,6 +260,21 @@ developer gets as "the initial version". **Exit (G4):** an hour of a game on glass with pad, rumble and audio in sync; a second pad joins; sleep and wake return to Hosts; the tester's report answered. +**Outcome (2026-11-01).** On the tester's Switch 1 a game plays handheld with audio, the pad's +buttons and sticks, HD rumble, gyro and the touch screen as the host's touch; the dial steps the +statistics tier, the controller mouse and the controller type, which re-plugs the pad once when +the dial closes (`docs/NOTES.md`, title at `be98e16` and `8a18316`). The tester closed the +package there. Departures: + +- Sleep and wake redial into the stream (0.3 s) rather than return to Hosts; the tester kept it. + A wake no longer crashes the title (the waker patch in `toolchain/patches/mio`). +- The applet-mode warning is in, but a launch from the Album crashed the console before the + first menu. Players are told to hold R over a game (WP6's docs); no investigation now. +- The hour of play, the second pad and the ten-minute docked 1080p run move to the very end, + after the last package. +- The Nintendo controller types in the shared Settings list and labels wait for a monorepo PR + (`docs/improvements.md`). Audio underruns climb in silence with nothing audible; left for WP5. + ### WP5 — Reliability and latency (two weeks) Measured, not guessed: the picture copy (D7) and nvmap import if it shows; the audio sync cell diff --git a/toolchain/patches/mio/src/sys/unix/waker/udp.rs b/toolchain/patches/mio/src/sys/unix/waker/udp.rs index f392412..bb5e3f9 100644 --- a/toolchain/patches/mio/src/sys/unix/waker/udp.rs +++ b/toolchain/patches/mio/src/sys/unix/waker/udp.rs @@ -27,7 +27,11 @@ impl Waker { Ok(()) } Err(ref err) if err.kind() == io::ErrorKind::Interrupted => self.wake(), - Err(err) => Err(err), + // After the console sleeps a send here fails, and tokio aborts the title on a failed + // wake (Atmosphère report `01793482713_05446530aca7e000`). A dead waker wakes no one: + // the driver runs again on its next event or timer. + // TODO(verify): the error sleep leaves, and that the driver's poll survives it. + Err(_) => Ok(()), } }