DualSense output: rumble, LEDs and Bluetooth checked against SDL and the kernel
- Every report carries the motors; still motors send no rumble flags, as SDL stops rumble (a report without them ends the emulation and re-arms the audio haptics), so a trigger change no longer cuts a vibration. - Legacy vibration at half strength (SDL); the Edge always takes V2; an unreadable firmware version falls back to legacy. - The first report after opening clears the motor/trigger attenuation (flags 1 bit 6, byte 36; dualsensectl, pydualsense). - The LED reset goes in a report of its own before any colour (hid-playstation's dualsense_reset_leds); over Bluetooth the colour waits for the connection animation (SDL's sensor clock check). - Bluetooth output only where gilrs keeps reading the pad (Linux), else with HORIZON_DUALSENSE_BT=1: any output switches the pad to its full report. - USB is preferred when the pad is on both. - Tests pin the reset report and its CRC (zlib), the flags and the clock offset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -144,7 +144,7 @@ fn rumble(pad: &mut DualSense) {
|
||||
write(
|
||||
pad,
|
||||
Output {
|
||||
rumble: Some(motors),
|
||||
rumble: motors,
|
||||
..Output::default()
|
||||
},
|
||||
);
|
||||
@@ -153,7 +153,7 @@ fn rumble(pad: &mut DualSense) {
|
||||
write(
|
||||
pad,
|
||||
Output {
|
||||
rumble: Some([0, 0]),
|
||||
rumble: [0, 0],
|
||||
..Output::default()
|
||||
},
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use crate::{Output, TRIGGER_OFF, TriggerEffect, Vibration, is_dualsense};
|
||||
use hidapi::{BusType, HidApi, HidDevice, HidResult};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||
use std::sync::mpsc::{self, RecvTimeoutError, Sender};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -23,13 +23,35 @@ pub struct DualSense {
|
||||
seq: u8,
|
||||
}
|
||||
|
||||
/// Whether a pad on Bluetooth may be written to. Any output report, and even reading a feature
|
||||
/// report, switches it to its full input report (0x31) until it is turned off (SDL_hints.h,
|
||||
/// `SDL_HINT_JOYSTICK_ENHANCED_REPORTS`; SDL_hidapi_ps5.c). Linux's `hid-playstation` has
|
||||
/// already switched it and reads that report, but gilrs on macOS and Windows parses the simple
|
||||
/// report 0x01 and would stop seeing the pad: there it waits for `HORIZON_DUALSENSE_BT=1`.
|
||||
pub fn bluetooth_allowed() -> bool {
|
||||
cfg!(target_os = "linux") || std::env::var_os("HORIZON_DUALSENSE_BT").is_some_and(|v| v == "1")
|
||||
}
|
||||
|
||||
impl DualSense {
|
||||
/// The first DualSense hidapi lists; `None` when there is none, an error when it can't be
|
||||
/// opened (Linux without access to its hidraw node).
|
||||
/// The first DualSense hidapi lists, USB before Bluetooth (as SDL); `None` when there is
|
||||
/// none, an error when it can't be opened (Linux without access to its hidraw node).
|
||||
pub fn open(api: &HidApi) -> Option<HidResult<Self>> {
|
||||
let info = api
|
||||
static SKIPPED: AtomicBool = AtomicBool::new(false);
|
||||
let bluetooth = |d: &&hidapi::DeviceInfo| matches!(d.bus_type(), BusType::Bluetooth);
|
||||
let pads = api
|
||||
.device_list()
|
||||
.find(|d| is_dualsense(d.vendor_id(), d.product_id()))?;
|
||||
.filter(|d| is_dualsense(d.vendor_id(), d.product_id()));
|
||||
let (wireless, wired): (Vec<_>, Vec<_>) = pads.partition(bluetooth);
|
||||
let info = match wired.first() {
|
||||
Some(info) => info,
|
||||
None if bluetooth_allowed() => wireless.first()?,
|
||||
None => {
|
||||
if !wireless.is_empty() && !SKIPPED.swap(true, Ordering::Relaxed) {
|
||||
log::info!("Horizon: no DualSense output over Bluetooth on this system");
|
||||
}
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let transport = match info.bus_type() {
|
||||
BusType::Bluetooth => Transport::Bluetooth,
|
||||
_ => Transport::Usb,
|
||||
@@ -37,14 +59,11 @@ impl DualSense {
|
||||
Some(info.open_device(api).map(|device| {
|
||||
let mut firmware = [0; 64];
|
||||
firmware[0] = 0x20;
|
||||
// The update version decides how vibration is asked for; without it, the kernel's
|
||||
// default.
|
||||
let vibration = match device.get_feature_report(&mut firmware) {
|
||||
Ok(n) if n >= 46 => {
|
||||
Vibration::of_update_version(u16::from_le_bytes([firmware[44], firmware[45]]))
|
||||
}
|
||||
_ => Vibration::Legacy,
|
||||
let version = match device.get_feature_report(&mut firmware) {
|
||||
Ok(n) if n >= 46 => Some(u16::from_le_bytes([firmware[44], firmware[45]])),
|
||||
_ => None,
|
||||
};
|
||||
let vibration = Vibration::of(info.product_id(), version);
|
||||
DualSense {
|
||||
device,
|
||||
transport,
|
||||
@@ -182,6 +201,9 @@ struct Writer {
|
||||
state: State,
|
||||
/// What the pad was last sent; `None` after (re)opening.
|
||||
sent: Option<(State, [u8; 2])>,
|
||||
/// Whether the lights may be set: over Bluetooth, not before the pad's connection
|
||||
/// animation is over, or it paints over them.
|
||||
lights: Lights,
|
||||
rumble: Rumble,
|
||||
/// When the vibration's current step ends.
|
||||
step_end: Option<Instant>,
|
||||
@@ -189,6 +211,32 @@ struct Writer {
|
||||
next_scan: Instant,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Lights {
|
||||
/// Over Bluetooth, until the pad's clock says its animation is over (or this long after
|
||||
/// opening, when it sends no clock).
|
||||
Waiting(Instant),
|
||||
/// They may be set, after the reset report that hands them to the host.
|
||||
Ready,
|
||||
Reset,
|
||||
}
|
||||
|
||||
/// The pad's sensor clock once its Bluetooth connection animation is over (SDL_hidapi_ps5.c's
|
||||
/// `HIDAPI_DriverPS5_CheckPendingLEDReset`).
|
||||
const ANIMATION_OVER: u32 = 10_200_000;
|
||||
|
||||
/// The sensor clock of a Bluetooth input report 0x31: its state block starts at byte 2 and
|
||||
/// holds the clock at 27..31, little-endian (SDL's `PS5StatePacketCommon_t`, the kernel's
|
||||
/// `dualsense_input_report`).
|
||||
fn bluetooth_clock(report: &[u8]) -> Option<u32> {
|
||||
match report {
|
||||
[0x31, _, state @ ..] if state.len() >= 31 => {
|
||||
Some(u32::from_le_bytes(state[27..31].try_into().ok()?))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
fn new(open: Arc<AtomicU8>) -> Self {
|
||||
Writer {
|
||||
@@ -197,6 +245,7 @@ impl Writer {
|
||||
open,
|
||||
state: State::default(),
|
||||
sent: None,
|
||||
lights: Lights::Ready,
|
||||
rumble: Vec::new(),
|
||||
step_end: None,
|
||||
scans_left: 1,
|
||||
@@ -210,9 +259,11 @@ impl Writer {
|
||||
if self.pad.is_none() && self.scans_left > 0 && now >= self.next_scan {
|
||||
self.scan();
|
||||
}
|
||||
let waiting = matches!(self.lights, Lights::Waiting(_)) && self.pad.is_some();
|
||||
let wake = [
|
||||
self.step_end,
|
||||
(self.pad.is_none() && self.scans_left > 0).then_some(self.next_scan),
|
||||
waiting.then(|| now + Duration::from_millis(250)),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
@@ -247,6 +298,7 @@ impl Writer {
|
||||
Err(RecvTimeoutError::Timeout) => {}
|
||||
Err(RecvTimeoutError::Disconnected) => break,
|
||||
}
|
||||
self.watch_animation();
|
||||
self.advance_rumble();
|
||||
self.flush();
|
||||
}
|
||||
@@ -274,6 +326,12 @@ impl Writer {
|
||||
log::info!("DualSense output over {:?}", pad.transport());
|
||||
self.open
|
||||
.store(1 + pad.transport() as u8, Ordering::Relaxed);
|
||||
self.lights = match pad.transport() {
|
||||
Transport::Usb => Lights::Ready,
|
||||
Transport::Bluetooth => {
|
||||
Lights::Waiting(Instant::now() + Duration::from_secs(1))
|
||||
}
|
||||
};
|
||||
self.pad = Some(pad);
|
||||
self.sent = None;
|
||||
self.scans_left = 0;
|
||||
@@ -288,6 +346,28 @@ impl Writer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Over Bluetooth, read the pad's clock until its connection animation is over.
|
||||
fn watch_animation(&mut self) {
|
||||
let (Lights::Waiting(deadline), Some(pad)) = (self.lights, self.pad.as_ref()) else {
|
||||
return;
|
||||
};
|
||||
let mut buf = [0; 80];
|
||||
let mut clock = None;
|
||||
while let Ok(n) = pad.read(&mut buf, 0)
|
||||
&& n > 0
|
||||
{
|
||||
clock = bluetooth_clock(&buf[..n]).or(clock);
|
||||
}
|
||||
let over = match clock {
|
||||
Some(clock) => clock >= ANIMATION_OVER,
|
||||
// No full report to read the clock from: as SDL, take it as over.
|
||||
None => Instant::now() >= deadline,
|
||||
};
|
||||
if over {
|
||||
self.lights = Lights::Ready;
|
||||
}
|
||||
}
|
||||
|
||||
/// Move to the vibration's next step once this one is over.
|
||||
fn advance_rumble(&mut self) {
|
||||
let now = Instant::now();
|
||||
@@ -307,13 +387,36 @@ impl Writer {
|
||||
return;
|
||||
};
|
||||
let motors = self.rumble.last().map_or([0, 0], |(m, _)| *m);
|
||||
let (state, sent) = (self.state, self.sent);
|
||||
let mut state = self.state;
|
||||
if matches!(self.lights, Lights::Waiting(_)) {
|
||||
state.lightbar = None;
|
||||
state.player_leds = None;
|
||||
}
|
||||
let sent = self.sent;
|
||||
if sent == Some((state, motors)) {
|
||||
return;
|
||||
}
|
||||
// Every report carries the motors: one without them stops the rumble.
|
||||
let mut first = Output {
|
||||
rumble: motors,
|
||||
..Output::default()
|
||||
};
|
||||
if sent.is_none() {
|
||||
first.full_power = true;
|
||||
}
|
||||
let lit = state.lightbar.is_some() || state.player_leds.is_some();
|
||||
if lit && self.lights == Lights::Ready {
|
||||
first.release_leds = true;
|
||||
self.lights = Lights::Reset;
|
||||
}
|
||||
// The reset goes alone, before any colour (hid-playstation.c).
|
||||
if first.full_power || first.release_leds {
|
||||
if let Err(e) = pad.write(&first) {
|
||||
return self.lost(&e.to_string());
|
||||
}
|
||||
}
|
||||
let mut out = Output {
|
||||
rumble: (sent.is_none_or(|(_, m)| m != motors)).then_some(motors),
|
||||
release_leds: sent.is_none() && state.lightbar.is_some(),
|
||||
rumble: motors,
|
||||
..Output::default()
|
||||
};
|
||||
let old = sent.map(|(s, _)| s);
|
||||
@@ -339,3 +442,20 @@ impl Writer {
|
||||
self.open.store(0, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bluetooth_clock_offset() {
|
||||
let mut report = [0; 78];
|
||||
report[0] = 0x31;
|
||||
report[2 + 27..2 + 31].copy_from_slice(&ANIMATION_OVER.to_le_bytes());
|
||||
assert_eq!(bluetooth_clock(&report), Some(10_200_000));
|
||||
// The simple report, or a USB one, has no clock here.
|
||||
report[0] = 0x01;
|
||||
assert_eq!(bluetooth_clock(&report), None);
|
||||
assert_eq!(bluetooth_clock(&[0x31, 0, 0]), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ const MOTOR_RIGHT: usize = 2;
|
||||
const MOTOR_LEFT: usize = 3;
|
||||
const RIGHT_TRIGGER: usize = 10;
|
||||
const LEFT_TRIGGER: usize = 21;
|
||||
/// `reduce_motor_power` (dualsensectl): the motors' attenuation in bits 0..2, the triggers' in
|
||||
/// bits 4..6, 0 (none) to 7.
|
||||
const ATTENUATION: usize = 36;
|
||||
const FLAGS2: usize = 38;
|
||||
const LIGHTBAR_SETUP: usize = 41;
|
||||
const PLAYER_LEDS: usize = 43;
|
||||
@@ -49,6 +52,7 @@ const FLAG0_LEFT_TRIGGER: u8 = 1 << 3;
|
||||
const FLAG1_LIGHTBAR: u8 = 1 << 2;
|
||||
const FLAG1_RELEASE_LEDS: u8 = 1 << 3;
|
||||
const FLAG1_PLAYER_LEDS: u8 = 1 << 4;
|
||||
const FLAG1_ATTENUATION: u8 = 1 << 6;
|
||||
const FLAG2_LIGHTBAR_SETUP: u8 = 1 << 1;
|
||||
const FLAG2_VIBRATION_V2: u8 = 1 << 2;
|
||||
/// `lightbar_setup`: fade the firmware's own blue light out, so the host's colour shows.
|
||||
@@ -65,19 +69,27 @@ pub const TRIGGER_OFF: TriggerEffect = [0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Output {
|
||||
/// The vibration motors, 0..255: the left (heavy, low) and the right (light, high) one.
|
||||
pub rumble: Option<[u8; 2]>,
|
||||
/// Every report carries them: one without the rumble flags ends the motors' emulation and
|
||||
/// hands the actuators back to the audio haptics (SDL_hidapi_ps5.c: "Leaving emulated
|
||||
/// rumble bits off will restore audio haptics"), so stopping sends no flags at all.
|
||||
pub rumble: [u8; 2],
|
||||
pub left_trigger: Option<TriggerEffect>,
|
||||
pub right_trigger: Option<TriggerEffect>,
|
||||
/// Red, green, blue.
|
||||
pub lightbar: Option<[u8; 3]>,
|
||||
/// The five LEDs under the touchpad, left to right in bits 0..4.
|
||||
pub player_leds: Option<u8>,
|
||||
/// Hand the lightbar to the host: the firmware's blue light fades out.
|
||||
/// Hand the lightbar to the host: the firmware's blue light fades out. Sent alone, before
|
||||
/// any colour (hid-playstation.c's `dualsense_reset_leds`).
|
||||
pub release_leds: bool,
|
||||
/// Undo the motor and trigger attenuation another host (a PS5's intensity settings) left in
|
||||
/// the pad, which would otherwise scale every effect down.
|
||||
pub full_power: bool,
|
||||
}
|
||||
|
||||
/// How the pad takes vibration: firmware 2.24 and later has a better rumble emulation, picked
|
||||
/// with its own flag (`hid-playstation.c`'s `use_vibration_v2`).
|
||||
/// How the pad takes vibration: newer firmware has a better rumble emulation, picked with its
|
||||
/// own flag (`hid-playstation.c`'s `use_vibration_v2`, SDL's `enhanced_rumble`); the legacy one
|
||||
/// still works on every firmware.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Vibration {
|
||||
Legacy,
|
||||
@@ -86,9 +98,12 @@ pub enum Vibration {
|
||||
}
|
||||
|
||||
impl Vibration {
|
||||
/// From the firmware's update version (feature report 0x20, bytes 44..46).
|
||||
pub fn of_update_version(version: u16) -> Self {
|
||||
if version >= 0x0224 {
|
||||
/// From the product and the firmware's update version (feature report 0x20, bytes 44..46;
|
||||
/// `None` when it can't be read). The Edge always has it. SDL picks it from 0x0224, the
|
||||
/// kernel from 0x0215 (`DS_FEATURE_VERSION(2, 21)`): we take SDL's, as both agree from
|
||||
/// there, and legacy below it and when the version is unknown, as it works everywhere.
|
||||
pub fn of(product: u16, update_version: Option<u16>) -> Self {
|
||||
if product == DUALSENSE_EDGE || update_version.is_some_and(|v| v >= 0x0224) {
|
||||
Vibration::V2
|
||||
} else {
|
||||
Vibration::Legacy
|
||||
@@ -100,7 +115,7 @@ impl Output {
|
||||
/// Everything off: triggers free, motors still.
|
||||
pub fn release() -> Self {
|
||||
Output {
|
||||
rumble: Some([0, 0]),
|
||||
rumble: [0, 0],
|
||||
left_trigger: Some(TRIGGER_OFF),
|
||||
right_trigger: Some(TRIGGER_OFF),
|
||||
..Output::default()
|
||||
@@ -110,16 +125,27 @@ impl Output {
|
||||
/// The block both transports carry.
|
||||
pub fn common(&self, vibration: Vibration) -> [u8; COMMON_LEN] {
|
||||
let mut c = [0; COMMON_LEN];
|
||||
if let Some([left, right]) = self.rumble {
|
||||
if self.rumble != [0, 0] {
|
||||
// Classic rumble rather than the audio-driven haptics.
|
||||
c[FLAGS0] |= FLAG0_HAPTICS_SELECT;
|
||||
match vibration {
|
||||
Vibration::V2 => c[FLAGS2] |= FLAG2_VIBRATION_V2,
|
||||
Vibration::Legacy => c[FLAGS0] |= FLAG0_COMPATIBLE_VIBRATION,
|
||||
}
|
||||
let [left, right] = match vibration {
|
||||
Vibration::V2 => {
|
||||
c[FLAGS2] |= FLAG2_VIBRATION_V2;
|
||||
self.rumble
|
||||
}
|
||||
Vibration::Legacy => {
|
||||
c[FLAGS0] |= FLAG0_COMPATIBLE_VIBRATION;
|
||||
// The old emulation shakes about twice as hard: SDL halves it.
|
||||
self.rumble.map(|m| m >> 1)
|
||||
}
|
||||
};
|
||||
c[MOTOR_LEFT] = left;
|
||||
c[MOTOR_RIGHT] = right;
|
||||
}
|
||||
if self.full_power {
|
||||
c[FLAGS1] |= FLAG1_ATTENUATION;
|
||||
c[ATTENUATION] = 0;
|
||||
}
|
||||
if let Some(effect) = self.right_trigger {
|
||||
c[FLAGS0] |= FLAG0_RIGHT_TRIGGER;
|
||||
c[RIGHT_TRIGGER..RIGHT_TRIGGER + 11].copy_from_slice(&effect);
|
||||
@@ -201,7 +227,7 @@ mod tests {
|
||||
#[test]
|
||||
fn usb_report_layout() {
|
||||
let out = Output {
|
||||
rumble: Some([200, 40]),
|
||||
rumble: [200, 40],
|
||||
right_trigger: Some([0x25, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
|
||||
lightbar: Some([255, 128, 0]),
|
||||
player_leds: Some(0b00100),
|
||||
@@ -225,42 +251,75 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_vibration_selects_haptics() {
|
||||
let out = Output {
|
||||
rumble: Some([1, 2]),
|
||||
fn legacy_vibration() {
|
||||
// SDL's pre-2.24 path: rumble emulation (0x01) and audio haptics off (0x02), the
|
||||
// motors halved, nothing in flags 2.
|
||||
let c = Output {
|
||||
rumble: [200, 41],
|
||||
..Output::default()
|
||||
};
|
||||
let c = out.common(Vibration::Legacy);
|
||||
assert_eq!(c[FLAGS0], FLAG0_COMPATIBLE_VIBRATION | FLAG0_HAPTICS_SELECT);
|
||||
}
|
||||
.common(Vibration::Legacy);
|
||||
assert_eq!(c[FLAGS0], 0x03);
|
||||
assert_eq!(c[MOTOR_RIGHT..=MOTOR_LEFT], [20, 100]);
|
||||
assert_eq!(c[FLAGS2], 0);
|
||||
assert_eq!(Vibration::of_update_version(0x0223), Vibration::Legacy);
|
||||
assert_eq!(Vibration::of_update_version(0x0641), Vibration::V2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vibration_mode_of_the_firmware() {
|
||||
assert_eq!(Vibration::of(DUALSENSE, Some(0x0223)), Vibration::Legacy);
|
||||
assert_eq!(Vibration::of(DUALSENSE, Some(0x0224)), Vibration::V2);
|
||||
assert_eq!(Vibration::of(DUALSENSE, Some(0x0641)), Vibration::V2);
|
||||
assert_eq!(Vibration::of(DUALSENSE, None), Vibration::Legacy);
|
||||
assert_eq!(Vibration::of(DUALSENSE_EDGE, None), Vibration::V2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn still_motors_send_no_rumble_flags() {
|
||||
// The all-zero report, which also re-arms the audio haptics (nondebug/dualsense).
|
||||
assert_eq!(Output::default().common(Vibration::V2), [0; COMMON_LEN]);
|
||||
assert_eq!(Output::default().common(Vibration::Legacy), [0; COMMON_LEN]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_frees_both_triggers_and_stops_the_motors() {
|
||||
let c = Output::release().common(Vibration::V2);
|
||||
assert_eq!(
|
||||
c[FLAGS0],
|
||||
FLAG0_HAPTICS_SELECT | FLAG0_LEFT_TRIGGER | FLAG0_RIGHT_TRIGGER
|
||||
);
|
||||
assert_eq!(c[MOTOR_LEFT..MOTOR_LEFT + 1], [0]);
|
||||
assert_eq!(c[FLAGS0], FLAG0_LEFT_TRIGGER | FLAG0_RIGHT_TRIGGER);
|
||||
assert_eq!(c[FLAGS0], 0b1100);
|
||||
assert_eq!(c[MOTOR_RIGHT..=MOTOR_LEFT], [0, 0]);
|
||||
assert_eq!(c[RIGHT_TRIGGER..RIGHT_TRIGGER + 11], TRIGGER_OFF);
|
||||
assert_eq!(c[LEFT_TRIGGER..LEFT_TRIGGER + 11], TRIGGER_OFF);
|
||||
// Nothing else changes.
|
||||
assert_eq!(c[FLAGS1], 0);
|
||||
assert_eq!(c[FLAGS2], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_leds_fades_the_firmware_light() {
|
||||
fn full_power_clears_the_attenuation() {
|
||||
let c = Output {
|
||||
full_power: true,
|
||||
..Output::default()
|
||||
}
|
||||
.common(Vibration::V2);
|
||||
// dualsensectl's VIBRATION_ATTENUATION_ENABLE and `reduce_motor_power`.
|
||||
assert_eq!(c[FLAGS1], 0x40);
|
||||
assert_eq!(c[36], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_leds_resets_the_lights() {
|
||||
// hid-playstation.c's dualsense_reset_leds (lightbar setup: light out) and SDL's
|
||||
// "Reset LED state" bit, over Bluetooth with sequence number 5; the CRC is
|
||||
// zlib.crc32(b"\xa2" + report[:74]).
|
||||
let mut expected = [0; BT_REPORT_LEN];
|
||||
expected[..5].copy_from_slice(&[0x31, 0x50, 0x10, 0x00, 0x08]);
|
||||
expected[3 + 38] = 0x02;
|
||||
expected[3 + 41] = 0x02;
|
||||
expected[74..].copy_from_slice(&0xc72d_0f94u32.to_le_bytes());
|
||||
let out = Output {
|
||||
release_leds: true,
|
||||
..Output::default()
|
||||
};
|
||||
let c = out.common(Vibration::V2);
|
||||
assert_eq!(c[FLAGS1], FLAG1_RELEASE_LEDS);
|
||||
assert_eq!(c[FLAGS2], FLAG2_LIGHTBAR_SETUP);
|
||||
assert_eq!(c[LIGHTBAR_SETUP], LIGHTBAR_LIGHT_OUT);
|
||||
assert_eq!(out.bluetooth(Vibration::V2, 5), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -271,6 +330,7 @@ mod tests {
|
||||
};
|
||||
let r = out.bluetooth(Vibration::V2, 0x13);
|
||||
assert_eq!(r.len(), 78);
|
||||
// The sequence number in the high nibble, the tag 0x10 (hid-playstation.c).
|
||||
assert_eq!(r[..3], [0x31, 0x30, 0x10]);
|
||||
assert_eq!(r[3..50], out.common(Vibration::V2));
|
||||
assert_eq!(r[50..74], [0; 24]);
|
||||
|
||||
+45
-11
@@ -264,9 +264,34 @@ shared, so gilrs keeps reading the same pad), the HID API on Windows.
|
||||
(three flag bytes), the two compatible-vibration motors, both adaptive
|
||||
triggers' effects, the lightbar colour and the player LEDs. Over USB it is
|
||||
report 0x02; over Bluetooth report 0x31, led by a sequence number and a tag
|
||||
and closed by a CRC-32 of the seed byte 0xA2 and the report. Firmware with
|
||||
update version 0x0224 or later (feature report 0x20) takes the newer rumble
|
||||
emulation flag. Unit tests check the bytes and the CRC.
|
||||
and closed by a CRC-32 of the seed byte 0xA2 and the report. Unit tests pin
|
||||
the bytes to the references below (the CRC to zlib's, the LED reset to the
|
||||
kernel's).
|
||||
- **Rumble.** Every report carries the motors: a report without the rumble
|
||||
flags ends the motors' emulation and hands the actuators back to the audio
|
||||
haptics (that is how SDL stops rumble, and how the HD haptics are re-armed),
|
||||
so a trigger change mid-rumble would otherwise cut it. A pad with update
|
||||
version 0x0224 or later (feature report 0x20), and every Edge, takes the
|
||||
newer emulation (flags 2, bit 2); older or unknown firmware the legacy one
|
||||
(flags 0, bit 0), at half strength as SDL does. The kernel takes the newer
|
||||
one from 0x0215; between the two, legacy works on every firmware. The first
|
||||
report after opening clears the motor and trigger attenuation another host
|
||||
(a PS5's intensity settings) may have left (flags 1, bit 6, byte 36 = 0).
|
||||
- **Lights.** Before the first colour, a report of its own resets the lights
|
||||
(flags 1 bit 3, and the lightbar setup "light out"; the kernel does the
|
||||
same). Over Bluetooth the pad plays a connection animation that paints over
|
||||
the lightbar: like SDL, the colour waits until the pad's sensor clock
|
||||
(input report 0x31) passes 10 200 000.
|
||||
- **Bluetooth.** Any output report, even reading a feature report, switches a
|
||||
pad on Bluetooth to its full input report (0x31) until it is turned off. On
|
||||
Linux `hid-playstation` has done that already and gilrs reads its events,
|
||||
so output goes ahead. Gilrs on macOS and Windows parses the simple report
|
||||
0x01 instead and would stop seeing the pad (SDL's
|
||||
`SDL_HINT_JOYSTICK_ENHANCED_REPORTS`: "break Windows DirectInput for other
|
||||
applications"), so there a pad on Bluetooth gets no output unless
|
||||
`HORIZON_DUALSENSE_BT=1`. Doing it for everyone means reading the pad's
|
||||
input ourselves over HID, as SDL does (a later PR). A pad on both cables is
|
||||
written over USB.
|
||||
- **Writer thread.** Writes block (a Bluetooth write can take milliseconds),
|
||||
so a thread owns the pad: the game hands it the state to show and timed
|
||||
vibrations, and it sends only what changed, and everything again when the
|
||||
@@ -298,14 +323,23 @@ shared, so gilrs keeps reading the same pad), the HID API on Windows.
|
||||
shake while the motors run (`watch` logs the shake while the game drives
|
||||
the pad).
|
||||
|
||||
Byte layout from public references; no code is copied from them: the Linux
|
||||
kernel's [`hid-playstation.c`](https://github.com/torvalds/linux/blob/master/drivers/hid/hid-playstation.c)
|
||||
(report IDs and sizes, valid flags, motor, lightbar and player LED offsets,
|
||||
the Bluetooth CRC, the firmware's vibration version), SDL's
|
||||
[`SDL_hidapi_ps5.c`](https://github.com/libsdl-org/SDL/blob/main/src/joystick/hidapi/SDL_hidapi_ps5.c)
|
||||
(the trigger effect blocks and the input report), and the
|
||||
[dualsensectl](https://github.com/nowrep/dualsensectl) and DualSenseX
|
||||
documentation.
|
||||
Byte layout from public references, read for facts; no code is copied from
|
||||
them (the kernel and dualsensectl are GPL):
|
||||
|
||||
| Reference | Read at | What it settles |
|
||||
| --- | --- | --- |
|
||||
| SDL [`SDL_hidapi_ps5.c`](https://github.com/libsdl-org/SDL/blob/6defbc0129b5ba90bab3f6c67bbc1e9329e24ff1/src/joystick/hidapi/SDL_hidapi_ps5.c) and [`SDL_hints.h`](https://github.com/libsdl-org/SDL/blob/6defbc0129b5ba90bab3f6c67bbc1e9329e24ff1/include/SDL3/SDL_hints.h) (zlib) | `6defbc0` | the 47-byte block, rumble flags and stop, 0x0224, legacy at half, LED reset and the Bluetooth animation clock, USB report length 48, the enhanced-mode switch |
|
||||
| Linux [`hid-playstation.c`](https://github.com/torvalds/linux/blob/045958acaa75205b31578cb22c4345e28dd2e4bf/drivers/hid/hid-playstation.c) (GPL) | `045958a` | valid flags, the Bluetooth sequence and tag 0x10, CRC seed 0xA2, `dualsense_reset_leds`, `use_vibration_v2` from 2.21, player LED patterns, the default blue |
|
||||
| [dualsensectl](https://github.com/nowrep/dualsensectl/blob/d3ae2fa272e1bf9a5c08a0ded3f374b40f7c73ac/main.c) (GPL) | `d3ae2fa` | trigger valid bits (flags 0 bits 2, 3), `reduce_motor_power` (byte 36) and its flag, the trigger effect packing |
|
||||
| [pydualsense](https://github.com/flok/pydualsense/blob/01445f5ed6ba/pydualsense/pydualsense.py) (MIT) | `01445f5` | sets the attenuation flag with 0 on every report |
|
||||
| [nondebug/dualsense](https://github.com/nondebug/dualsense) | `b87450e` | an all-zero report 0x02 leaves rumble emulation for the audio haptics |
|
||||
|
||||
Verified on hardware (USB, dualsense-1, Mac mini): the motors with the V2
|
||||
flags (the IMU shook, and settled after a stop that then still sent the
|
||||
flags), the pad reporting a feedback and a weapon trigger effect as active,
|
||||
and the writer thread. Verified against the references only: everything
|
||||
else, the flag-less stop, the attenuation and LED reset reports, and all of
|
||||
Bluetooth.
|
||||
|
||||
## Steam Deck
|
||||
|
||||
|
||||
Reference in New Issue
Block a user