Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The app never names a libnx call or a Vulkan handle; the binary hands it a `Plat
every worker thread has joined (a thread still running holds a locked stack the loader faults on).
- Every thread is a std thread; libnx's `threadCreate` is wrapped by the target (`toolchain/`).
- Face buttons go by position on the wire: Nintendo A (east) is `BTN_B`. A confirms, B backs.
The one exception is what reaches the host for an Xbox pad: there A/B and X/Y go by label,
unless `nx.face_layout` is `"position"` (`runtime::pads::by_label`).
- libnx has no suspend hook: sleep is `FocusState::Background`, wake is `Resume`.
- Threads are never named from inside themselves.
- A console claim in `docs/NOTES.md` cites a bundle id. No workaround without a probe line.
Expand Down
11 changes: 11 additions & 0 deletions crates/pf-nx-app/src/core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ pub enum HandheldGyro {
Controller,
}

/// Which face buttons the host gets for A, B, X and Y.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FaceLayout {
/// By label on an Xbox pad, so the A on screen is the A in hand; by position on the rest,
/// where the labels already agree (Nintendo) or there are none (a `PlayStation` pad).
#[default]
Auto,
/// By position on every pad: the Switch's south button, labelled B, is the Xbox A.
Position,
}

/// Codec preference selectable in Settings — a *preference*, not a demand. The host
/// resolves the session codec from the client's advertised set via its own precedence
/// ladder (HEVC > H.264), honouring the preference only when its encoder can
Expand Down
45 changes: 43 additions & 2 deletions crates/pf-nx-app/src/core/settings.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
//! This client's typed view of the shared settings document (`pf_client_core::trust::Settings`),
//! the one settings type it stores and edits. Every row here is a shared field but one: a row
//! only the console has lives in `extra` under an `nx.` prefix, and `nx.handheld_gyro` does.
//! only the console has lives in `extra` under an `nx.` prefix, as `nx.handheld_gyro` and
//! `nx.face_layout` do.

use pf_client_core::trust::Settings;
use punktfunk_core::config::GamepadPref;

use crate::core::model::{CodecPref, GamepadType, HandheldGyro};
use crate::core::model::{CodecPref, FaceLayout, GamepadType, HandheldGyro};

/// The console-only row picking the face-button layout: `"auto"` (the default) or
/// `"position"`. No screen sets it yet; the player edits `settings.json` with the app closed.
pub const FACE_LAYOUT_KEY: &str = "nx.face_layout";

fn layout_name(l: FaceLayout) -> &'static str {
match l {
FaceLayout::Auto => "auto",
FaceLayout::Position => "position",
}
}

/// The console-only row picking the handheld gyro: `"console"` (the default) or `"controller"`.
/// No screen sets it yet; the shared settings screen is to grow a row writing these names.
Expand Down Expand Up @@ -52,6 +64,9 @@ pub trait NxSettings {
/// else [`HANDHELD_GYRO_KEY`]'s pick; an unknown name reads as the default.
fn handheld_gyro(&self, key_file: bool) -> HandheldGyro;
fn set_handheld_gyro(&mut self, gyro: HandheldGyro);
/// The face-button layout [`FACE_LAYOUT_KEY`] picks; an unknown name reads as the default.
fn face_layout(&self) -> FaceLayout;
fn set_face_layout(&mut self, layout: FaceLayout);
/// Normalise to what this console can present (`core::caps`). Called on load and on every
/// launch's resolved settings, so a profile override cannot put an unpresentable pick on
/// the wire. HDR stays as the player set it: the display can change between launches, and
Expand Down Expand Up @@ -92,6 +107,17 @@ impl NxSettings for Settings {
self.extra.insert(HANDHELD_GYRO_KEY.into(), gyro_name(gyro).into());
}

fn face_layout(&self) -> FaceLayout {
match self.extra.get(FACE_LAYOUT_KEY).and_then(|v| v.as_str()) {
Some(name) if name == layout_name(FaceLayout::Position) => FaceLayout::Position,
_ => FaceLayout::Auto,
}
}

fn set_face_layout(&mut self, layout: FaceLayout) {
self.extra.insert(FACE_LAYOUT_KEY.into(), layout_name(layout).into());
}

fn clamp_to_caps(&mut self) {
let caps = crate::core::caps::video_caps();
let codecs = caps.codec_prefs();
Expand Down Expand Up @@ -146,6 +172,21 @@ mod tests {
assert_eq!(s.handheld_gyro(false), HandheldGyro::Console);
}

#[test]
fn the_face_layout_is_auto_unless_picked() {
let mut s = default_document();
assert_eq!(s.face_layout(), FaceLayout::Auto);
s.set_face_layout(FaceLayout::Position);
assert_eq!(s.extra[FACE_LAYOUT_KEY], "position");
let back: Settings = serde_json::from_value(serde_json::to_value(&s).unwrap()).unwrap();
assert_eq!(back.face_layout(), FaceLayout::Position);
// The key sits beside the shared fields, where a player editing the file finds it.
let doc: Settings = serde_json::from_str(r#"{"nx.face_layout": "position"}"#).unwrap();
assert_eq!(doc.face_layout(), FaceLayout::Position);
s.extra.insert(FACE_LAYOUT_KEY.into(), "label".into());
assert_eq!(s.face_layout(), FaceLayout::Auto);
}

#[test]
fn clamp_keeps_hdr_and_narrows_channels() {
let mut s = default_document();
Expand Down
154 changes: 144 additions & 10 deletions crates/pf-nx-app/src/runtime/pads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use pf_console_ui::PadTestState;
use punktfunk_core::input::{gamepad, key_vk, InputEvent, InputKind};

use crate::core::dial::{PadDial, PadRoute};
use crate::core::model::GamepadType;
use crate::core::model::{FaceLayout, GamepadType};
use crate::platform::{PadKind, PadSample};

/// Slots the console reports: one per player.
Expand Down Expand Up @@ -67,13 +67,16 @@ struct Slot {
last: PadSample,
dial: PadDial,
engine: Engine,
/// Whether the host gets this pad's face buttons by label ([`by_label`]).
by_label: bool,
}

/// Per-slot state across samples, and the shortcuts every slot's engine runs.
pub struct PadRouter {
slots: [Option<Slot>; SLOTS],
bindings: Vec<Binding>,
keys: Vec<(String, Vec<u8>)>,
layout: FaceLayout,
}

impl PadRouter {
Expand All @@ -99,9 +102,16 @@ impl PadRouter {
slots: Default::default(),
bindings,
keys,
layout: FaceLayout::default(),
}
}

/// The face-button layout the player picked; [`FaceLayout::Auto`] until set.
pub fn with_face_layout(mut self, layout: FaceLayout) -> Self {
self.layout = layout;
self
}

/// Diffs one batch against the last. `dial_open` is whether the dial is up now; `kind` is
/// the pad kind the session asked for (`Auto` mirrors the pad). Wire events land in `wire`.
pub fn step(
Expand All @@ -122,8 +132,10 @@ impl PadRouter {
}
continue;
};
let layout = self.layout;
let state = slot.get_or_insert_with(|| {
wire.extend(arrival_event(resolve_kind(kind, sample.kind), pad));
let resolved = resolve_kind(kind, sample.kind);
wire.extend(arrival_event(resolved, pad));
Slot {
last: PadSample {
slot: sample.slot,
Expand All @@ -132,6 +144,7 @@ impl PadRouter {
},
dial: PadDial::with_face(FACE),
engine: Engine::new(&self.bindings, Device::Pad),
by_label: by_label(resolved, layout),
}
});
let due = state.engine.tick(now);
Expand All @@ -146,7 +159,7 @@ impl PadRouter {
if let Some(slot) = slot {
let sent = slot.dial.opened();
for bit in BUTTONS.into_iter().filter(|b| sent & b != 0) {
wire.push(bit_event(bit, false, index as u8));
wire.push(bit_event(to_host(bit, slot.by_label), false, index as u8));
}
}
}
Expand All @@ -165,9 +178,11 @@ impl PadRouter {
for (index, slot) in self.slots.iter_mut().enumerate() {
if let Some(slot) = slot {
let pad = index as u8;
release_into(pad, slot.dial.take_sent(), &slot.last, wire);
release_into(pad, slot.dial.take_sent(), &slot.last, slot.by_label, wire);
wire.push(remove_event(pad));
wire.extend(arrival_event(resolve_kind(kind, slot.last.kind), pad));
let resolved = resolve_kind(kind, slot.last.kind);
wire.extend(arrival_event(resolved, pad));
slot.by_label = by_label(resolved, self.layout);
}
}
}
Expand All @@ -193,6 +208,28 @@ pub(crate) fn resolve_kind(setting: GamepadType, pad: PadKind) -> GamepadType {
}
}

/// Whether the host gets face buttons by label for a pad built as `kind`. Only an Xbox pad
/// needs it: its letters sit transposed from Nintendo's, so by position the A on screen is the
/// button labelled B. A Nintendo pad's letters already agree, and a `PlayStation` pad has shapes.
pub(crate) fn by_label(kind: GamepadType, layout: FaceLayout) -> bool {
layout == FaceLayout::Auto && kind == GamepadType::XboxOne
}

/// The bit the host gets for `bit`: by label, A trades with B and X with Y. Only what reaches
/// the host turns; the shortcuts and the dial keep reading the pad by position.
fn to_host(bit: u32, by_label: bool) -> u32 {
if !by_label {
return bit;
}
match bit {
gamepad::BTN_A => gamepad::BTN_B,
gamepad::BTN_B => gamepad::BTN_A,
gamepad::BTN_X => gamepad::BTN_Y,
gamepad::BTN_Y => gamepad::BTN_X,
other => other,
}
}

impl Slot {
/// One sample: button edges through the engine, axes straight on, the left stick aiming
/// the open dial instead.
Expand Down Expand Up @@ -267,14 +304,14 @@ impl Slot {
for o in out {
match o {
Out::Forward(bit, down) => match self.dial.button(bit, down, dial_open) {
PadRoute::Forward => wire.push(bit_event(bit, down, pad)),
PadRoute::Forward => wire.push(bit_event(to_host(bit, self.by_label), down, pad)),
PadRoute::Drop => {}
PadRoute::Menu(MenuEvent::Back) => routed.push(Routed::ToggleDial),
PadRoute::Menu(ev) => routed.push(Routed::Dial(ev)),
},
Out::Lift(bits) => {
for bit in bits.into_iter().filter(|&b| self.dial.lift(b)) {
wire.push(bit_event(bit, false, pad));
wire.push(bit_event(to_host(bit, self.by_label), false, pad));
}
}
Out::Fire(action, phase) => fire(&action, phase, keys, wire, routed),
Expand All @@ -287,7 +324,7 @@ impl Slot {
fn let_go(&mut self, pad: u8, keys: &Keys, wire: &mut Vec<InputEvent>) {
let out = self.engine.flush();
self.apply(pad, out, false, keys, wire, &mut Vec::new());
release_into(pad, self.dial.take_sent(), &self.last, wire);
release_into(pad, self.dial.take_sent(), &self.last, self.by_label, wire);
}
}

Expand All @@ -313,9 +350,9 @@ fn fire(action: &Action, phase: Phase, keys: &Keys, wire: &mut Vec<InputEvent>,
}

/// Releases for `sent`, the buttons the host holds, and centred axes for what `last` held.
fn release_into(pad: u8, sent: u32, last: &PadSample, wire: &mut Vec<InputEvent>) {
fn release_into(pad: u8, sent: u32, last: &PadSample, by_label: bool, wire: &mut Vec<InputEvent>) {
for bit in BUTTONS.into_iter().filter(|b| sent & b != 0) {
wire.push(bit_event(bit, false, pad));
wire.push(bit_event(to_host(bit, by_label), false, pad));
}
let axes = [
(gamepad::AXIS_LS_X, i32::from(last.lx)),
Expand Down Expand Up @@ -744,6 +781,103 @@ mod tests {
);
}

fn step_as(
router: &mut PadRouter,
batch: &[PadSample],
at: Instant,
kind: GamepadType,
) -> (Vec<InputEvent>, Vec<Routed>) {
let (mut wire, mut routed) = (Vec::new(), Vec::new());
router.step(batch, at, false, kind, &mut wire, &mut routed);
(wire, routed)
}

/// The Switch's face buttons by label, on the wire's positional bits.
const LABEL_A: u32 = gamepad::BTN_B;
const LABEL_B: u32 = gamepad::BTN_A;
const LABEL_X: u32 = gamepad::BTN_Y;
const LABEL_Y: u32 = gamepad::BTN_X;

#[test]
fn only_an_xbox_pad_takes_the_labels_and_position_turns_it_off() {
assert!(by_label(GamepadType::XboxOne, FaceLayout::Auto));
assert!(!by_label(GamepadType::XboxOne, FaceLayout::Position));
for kind in [
GamepadType::SwitchPro,
GamepadType::JoyConPair,
GamepadType::DualShock4,
GamepadType::DualSense,
GamepadType::DualSenseEdge,
] {
assert!(!by_label(kind, FaceLayout::Auto), "{kind:?} keeps the positions");
}
}

#[test]
fn an_xbox_pad_gets_the_letter_pressed() {
let mut r = router();
let t = Instant::now();
step_as(&mut r, &[pad(0)], t, GamepadType::XboxOne);
for (held, host) in [
(LABEL_A, gamepad::BTN_A),
(LABEL_B, gamepad::BTN_B),
(LABEL_X, gamepad::BTN_X),
(LABEL_Y, gamepad::BTN_Y),
] {
let (wire, _) = step_as(&mut r, &[pad(held)], t, GamepadType::XboxOne);
assert_eq!(wire, [bit_event(host, true, 0)]);
let (wire, _) = step_as(&mut r, &[pad(0)], t, GamepadType::XboxOne);
assert_eq!(wire, [bit_event(host, false, 0)]);
}
let (wire, _) = step_as(&mut r, &[pad(gamepad::BTN_LB)], t, GamepadType::XboxOne);
assert_eq!(wire, [bit_event(gamepad::BTN_LB, true, 0)], "the rest stay put");
}

#[test]
fn position_keeps_an_xbox_pad_positional() {
let mut r = router().with_face_layout(FaceLayout::Position);
let t = Instant::now();
step_as(&mut r, &[pad(0)], t, GamepadType::XboxOne);
let (wire, _) = step_as(&mut r, &[pad(LABEL_A)], t, GamepadType::XboxOne);
assert_eq!(wire, [bit_event(gamepad::BTN_B, true, 0)]);
}

#[test]
fn automatic_keeps_a_nintendo_pad_positional() {
let mut r = router();
let t = Instant::now();
step(&mut r, &[pad(0)], t, false);
let (wire, _) = step(&mut r, &[pad(LABEL_A)], t, false);
assert_eq!(wire, [bit_event(gamepad::BTN_B, true, 0)]);
}

#[test]
fn the_dial_still_opens_on_minus_and_a_with_an_xbox_pad() {
let mut r = router();
let t = Instant::now();
step_as(&mut r, &[pad(0)], t, GamepadType::XboxOne);
step_as(&mut r, &[pad(MODIFIER)], t, GamepadType::XboxOne);
let (_, routed) = step_as(&mut r, &[pad(MODIFIER | LABEL_A)], t, GamepadType::XboxOne);
assert_eq!(routed, [Routed::ToggleDial]);
}

#[test]
fn replug_lets_go_by_the_old_kind_and_presses_by_the_new() {
let mut r = router();
let t = Instant::now();
step(&mut r, &[pad(0)], t, false);
step(&mut r, &[pad(LABEL_A)], t, false);
let mut wire = Vec::new();
r.replug_all(GamepadType::XboxOne, &mut wire);
assert_eq!(wire[0], bit_event(gamepad::BTN_B, false, 0), "what Automatic pressed");
step_as(&mut r, &[pad(0)], t, GamepadType::XboxOne);
let (wire, _) = step_as(&mut r, &[pad(LABEL_A)], t, GamepadType::XboxOne);
assert_eq!(wire, [bit_event(gamepad::BTN_A, true, 0)]);
let mut wire = Vec::new();
r.release_all(&mut wire);
assert!(wire.contains(&bit_event(gamepad::BTN_A, false, 0)), "and lets go by it");
}

#[test]
fn release_all_centres_sticks_and_lifts_buttons() {
let mut r = router();
Expand Down
5 changes: 4 additions & 1 deletion crates/pf-nx-app/src/runtime/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,13 @@ fn stream(
end = %hint(Action::Slot(SlotId::EndStream)),
"pad shortcuts"
);
let face_layout = settings.face_layout();
tracing::info!("face layout: {face_layout:?}");
let router = PadRouter::new(
&shortcuts,
&OverlayConfig::parse(&settings.overlay_actions, RingPlatform::Desktop),
);
)
.with_face_layout(face_layout);
let key_file = paths::app_dir().join(GYRO_CONTROLLER_KEY).exists();
let gyro = settings.handheld_gyro(key_file);
tracing::info!(key_file, "handheld gyro: {gyro:?}");
Expand Down
17 changes: 17 additions & 0 deletions docs/tester.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ In a stream, the pads, the touch screen and gyro go to the PC. These do not:
Joy-Cons attached to the console show up on the PC as one Joy-Con pair, and a Pro Controller
as a Switch Pro controller. HD rumble works on both.

Some PC games only know Xbox controllers. For those, set the controller type to Xbox One. The
PC then gets the letter you press: the Switch's A is the Xbox A, B is B, X is X and Y is Y, so
the button a game names is the one with that letter in your hand. The Switch's menus keep A to
confirm and B to go back. On every other controller type, the buttons go by position, as
before.

To keep the Xbox type going by position instead (the Switch's lower button, labelled B, as the
Xbox A), close the app and add this line inside the `"settings"` block of
`/switch/punktfunk/settings.json` on the SD card:

```json
"nx.face_layout": "position",
```

Remove it, or set it to `"auto"`, to go back. There is no menu row for this yet. The log shows
which layout a stream uses: `face layout: Auto` or `face layout: Position`.

## Handheld and docked

Handheld streams at 1280×720, docked at 1920×1080, both at 60 frames per second. Docking or
Expand Down
Loading