Skip to content
Merged
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
49 changes: 20 additions & 29 deletions src/input/target/touchscreen.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::{error::Error, os::fd::AsFd, time::Duration};
use std::{
error::Error,
os::fd::AsFd,
sync::mpsc::{channel, Receiver, TryRecvError},
time::Duration,
};

use evdev::{
uinput::{VirtualDevice, VirtualDeviceBuilder},
AbsInfo, AbsoluteAxisCode, AttributeSet, BusType, EventType, InputEvent, InputId, KeyCode,
MiscCode, PropType, UinputAbsSetup,
};
use nix::fcntl::{FcntlArg, OFlag};
use tokio::sync::mpsc::{channel, Receiver};

use crate::{
input::{
Expand Down Expand Up @@ -370,7 +374,7 @@ impl TargetInputDevice for TouchscreenDevice {
&mut self,
composite_device: CompositeDeviceClient,
) -> Result<(), InputError> {
let (tx, rx) = channel(1);
let (tx, rx) = channel();
let mut device_config = self.config.clone();

// Spawn a task to wait for the composite device config. This is done
Expand Down Expand Up @@ -514,7 +518,7 @@ impl TargetInputDevice for TouchscreenDevice {
}

log::debug!("Sending touchscreen configuration to target device");
if let Err(e) = tx.send(device_config).await {
if let Err(e) = tx.send(device_config) {
log::error!("Failed to send touchscreen config: {e:?}");
}
});
Expand Down Expand Up @@ -548,35 +552,22 @@ impl TargetOutputDevice for TouchscreenDevice {
// should be sent continuously during active touches.
fn poll(&mut self, _: &Option<CompositeDeviceClient>) -> Result<Vec<OutputEvent>, OutputError> {
// Create and start the device if needed
if self.config_rx.is_some() {
if let Some(rx) = self.config_rx.as_ref() {
if rx.is_empty() {
// If the queue is empty, we're still waiting for a response from
// the composite device.
return Ok(vec![]);
}
}
let Some(mut rx) = self.config_rx.take() else {
return Ok(vec![]);
if let Some(rx) = self.config_rx.as_ref() {
let config = match rx.try_recv() {
Ok(config) => config,
Err(e) => match e {
TryRecvError::Empty => {
// If the queue is empty, we're still waiting for a response from
// the composite device.
return Ok(vec![]);
}
TryRecvError::Disconnected => self.config.clone(),
},
};

let (sync_tx, sync_rx) = std::sync::mpsc::channel();
let default_config = self.config.clone();
tokio::spawn(async move {
let config = match rx.recv().await {
Some(config) => config,
None => default_config,
};
if let Err(e) = sync_tx.send(config) {
log::error!("Failed to send config to device thread: {e}");
}
});

let config = sync_rx.recv().unwrap_or(self.config.clone());

let device = TouchscreenDevice::create_virtual_device(&config)?;
self.device = Some(device);
self.config = config;
self.config_rx = None;
}

let Some(device) = self.device.as_mut() else {
Expand Down