Skip to content

Commit

Permalink
fix(ForceFeedback): add default implementation for FF that does not e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
ShadowApex committed Aug 2, 2024
1 parent 5a530ff commit 2a56359
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
15 changes: 11 additions & 4 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,9 @@ impl CompositeDevice {
continue;
};
log::debug!("Updating effect {source_effect_id} from {source_id}");
source.update_effect(*source_effect_id, *data).await?;
if let Err(e) = source.update_effect(*source_effect_id, *data).await {
log::error!("Error updating effect '{id}' on {source_id}: {e:?}");
}
}
target_dev.send(Some(*id))?;
return Ok(());
Expand All @@ -659,11 +661,16 @@ impl CompositeDevice {
log::debug!("Uploading effect to {source_id}");
match source.upload_effect(*data).await {
Ok(source_effect_id) => {
log::debug!("Successfully uploaded effect with source effect id {source_effect_id}");
// An effect ID of -1 indicates the device does not support
// FF events.
if source_effect_id == -1 {
continue;
}
log::debug!("Successfully uploaded effect to {source_id} with source effect id {source_effect_id}");
source_effect_ids.insert(source_id.clone(), source_effect_id);
}
Err(e) => {
log::error!("Error uploading effect: {:?}", e);
log::error!("Error uploading effect to {source_id}: {e:?}");
}
}
}
Expand Down Expand Up @@ -696,7 +703,7 @@ impl CompositeDevice {
};
log::debug!("Erasing effect from {source_id}");
if let Err(e) = source.erase_effect(*source_effect_id).await {
log::debug!("Failed to erase FF effect from {source_id}: {:?}", e);
log::warn!("Failed to erase FF effect from {source_id}: {:?}", e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/input/source/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl SourceDeviceClient {
pub async fn upload_effect(&self, effect: FFEffectData) -> Result<i16, ClientError> {
let (tx, rx) = channel();
self.tx.try_send(SourceCommand::UploadEffect(effect, tx))?;
match rx.recv_timeout(Duration::from_secs(1)) {
match rx.recv_timeout(Duration::from_millis(200)) {
Ok(result) => match result {
Ok(id) => Ok(id),
Err(err) => Err(ClientError::ServiceError(err)),
Expand Down
12 changes: 12 additions & 0 deletions src/input/source/evdev/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ impl SourceOutputDevice for GamepadEventDevice {
/// a device-specific id of the uploaded effect if it is successful.
fn upload_effect(&mut self, effect: FFEffectData) -> Result<i16, OutputError> {
log::debug!("Uploading FF effect data");
if self.device.supported_ff().is_none() {
log::debug!("Device does not support FF effects");
return Ok(-1);
}
match self.device.upload_ff_effect(effect) {
Ok(effect) => {
let id = effect.id() as i16;
Expand All @@ -338,6 +342,10 @@ impl SourceOutputDevice for GamepadEventDevice {
/// Update the effect with the given id using the given effect data.
fn update_effect(&mut self, effect_id: i16, effect: FFEffectData) -> Result<(), OutputError> {
log::debug!("Update FF effect {effect_id}");
if self.device.supported_ff().is_none() {
log::debug!("Device does not support FF effects");
return Ok(());
}
let Some(current_effect) = self.ff_effects.get_mut(&effect_id) else {
log::warn!("Unable to find existing FF effect with id {effect_id}");
return Ok(());
Expand All @@ -353,6 +361,10 @@ impl SourceOutputDevice for GamepadEventDevice {
/// Erase the effect with the given id from the source device.
fn erase_effect(&mut self, effect_id: i16) -> Result<(), OutputError> {
log::debug!("Erasing FF effect data");
if self.device.supported_ff().is_none() {
log::debug!("Device does not support FF effects");
return Ok(());
}
self.ff_effects.remove(&effect_id);
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions src/input/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ pub trait SourceOutputDevice {
}

/// Upload the given force feedback effect data to the source device. Returns
/// a device-specific id of the uploaded effect if it is successful.
/// a device-specific id of the uploaded effect if it is successful. Return
/// -1 if this device does not support FF events.
fn upload_effect(&mut self, effect: FFEffectData) -> Result<i16, OutputError> {
//log::trace!("Received upload effect: {effect:?}");
let _ = effect;
Err(OutputError::NotImplemented)
Ok(-1)
}

/// Update the effect with the given id using the given effect data.
Expand Down
5 changes: 3 additions & 2 deletions src/input/target/xb360.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl TargetOutputDevice for XBox360Controller {
let Some(composite_device) = composite_device else {
log::debug!("No composite device to upload effect to!");
event.set_retval(-1);
return Ok(vec![]);
continue;
};

// Send the effect data to be uploaded to the device and wait
Expand All @@ -274,7 +274,8 @@ impl TargetOutputDevice for XBox360Controller {
Ok(id) => id,
Err(e) => {
event.set_retval(-1);
return Err(e.to_string().into());
log::error!("Failed to receive FF upload response: {e:?}");
continue;
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/input/target/xbox_elite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl TargetOutputDevice for XboxEliteController {
let Some(composite_device) = composite_device else {
log::debug!("No composite device to upload effect to!");
event.set_retval(-1);
return Ok(vec![]);
continue;
};

// Send the effect data to be uploaded to the device and wait
Expand All @@ -285,7 +285,8 @@ impl TargetOutputDevice for XboxEliteController {
Ok(id) => id,
Err(e) => {
event.set_retval(-1);
return Err(e.to_string().into());
log::error!("Failed to receive FF upload response: {e:?}");
continue;
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/input/target/xbox_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl TargetOutputDevice for XboxSeriesController {
let Some(composite_device) = composite_device else {
log::debug!("No composite device to upload effect to!");
event.set_retval(-1);
return Ok(vec![]);
continue;
};

// Send the effect data to be uploaded to the device and wait
Expand All @@ -276,7 +276,8 @@ impl TargetOutputDevice for XboxSeriesController {
Ok(id) => id,
Err(e) => {
event.set_retval(-1);
return Err(e.to_string().into());
log::error!("Failed to receive FF upload response: {e:?}");
continue;
}
};

Expand Down

0 comments on commit 2a56359

Please sign in to comment.