Skip to content

Commit 41c7147

Browse files
sunshineinaboxpastaq
authored andcommitted
fix(DualSense): address feedback add comments
1 parent f1b0d7b commit 41c7147

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/input/target/dualsense.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ pub struct DualSenseDevice {
124124
context: u8,
125125
hardware: DualSenseHardware,
126126
queued_events: Vec<ScheduledNativeEvent>,
127+
/// Last report written, used to skip re-emitting identical reports.
127128
last_written: Option<Vec<u8>>,
129+
/// Whether a consumer has the device open, writes are skipped while closed.
130+
is_open: bool,
128131
}
129132

130133
impl DualSenseDevice {
@@ -139,6 +142,7 @@ impl DualSenseDevice {
139142
hardware,
140143
queued_events: Vec::new(),
141144
last_written: None,
145+
is_open: false,
142146
})
143147
}
144148

@@ -195,11 +199,18 @@ impl DualSenseDevice {
195199

196200
/// Write the current device state to the device
197201
fn write_state(&mut self) -> Result<(), Box<dyn Error>> {
202+
// No consumer so don't bother emitting.
203+
if !self.is_open {
204+
return Ok(());
205+
}
206+
198207
let data = match self.state {
199208
PackedInputDataReport::Usb(state) => state.pack()?.to_vec(),
200209
PackedInputDataReport::Bluetooth(state) => state.pack()?.to_vec(),
201210
};
202211

212+
// Skip re-emitting an identical report: runs every poll, so an
213+
// idle controller would otherwise send the same bytes every cycle.
203214
if self.last_written.as_ref() == Some(&data) {
204215
return Ok(());
205216
}
@@ -1076,13 +1087,16 @@ impl TargetOutputDevice for DualSenseDevice {
10761087
// send UHID_INPUT events to the kernel.
10771088
uhid_virt::OutputEvent::Open => {
10781089
log::debug!("Open event received");
1079-
self.last_written = None;
1090+
self.is_open = true;
10801091
Ok(vec![])
10811092
}
10821093
// This is sent when there are no more processes which read the HID data. It is
10831094
// the counterpart of UHID_OPEN and you may as well ignore this event.
10841095
uhid_virt::OutputEvent::Close => {
10851096
log::debug!("Close event received");
1097+
// Drop the cached report so the next consumer to open is synced.
1098+
self.is_open = false;
1099+
self.last_written = None;
10861100
Ok(vec![])
10871101
}
10881102
// This is sent if the HID device driver wants to send raw data to the I/O

0 commit comments

Comments
 (0)