vcs-controller/src/joystick_loop.rs

234 lines
7 KiB
Rust
Raw Normal View History

2024-03-25 16:28:13 -07:00
use crate::config::save_config;
use crate::ui_code::{AppState, SocketConnectionUpdate};
use crate::JoystickThreadUpdate;
use async_channel::{Receiver, Sender};
use gilrs::{ev::filter::FilterFn, Axis, Button, Event, EventType, Filter, Gilrs, GilrsBuilder};
2024-03-26 18:56:33 -07:00
use std::net::TcpStream;
2024-03-25 16:28:13 -07:00
use std::panic::{self, AssertUnwindSafe};
use std::{
sync::{atomic::AtomicBool, Arc},
time::{Duration, Instant},
};
use websocket::client::{sync::Client, ClientBuilder};
use websocket::Message;
2024-03-26 18:56:33 -07:00
static MAX_RETRY_ATTEMPTS: u32 = 10;
struct SocketState {
pub ip: String,
pub port: i32,
pub socket: Option<Client<TcpStream>>,
}
impl SocketState {
fn is_connected(&self ) -> bool {
self.socket.is_some()
}
fn close_websocket(&mut self) {
if let Some(ref mut x) = self.socket {
println!("closing websocket");
x.send_message(&Message::close()).unwrap();
x.shutdown().unwrap();
self.socket = None;
}
}
fn reconnect_websocket(&mut self) -> bool {
if self.ip.is_empty() {
self.socket = None;
return false
}
if let Ok(mut val) =
ClientBuilder::new(format!("ws://{}:{}", &self.ip, self.port).as_str())
{
if let Ok(val2) = val.connect_insecure() {
self.socket = Some(val2);
true
} else {
println!("couldn't connect websocket! : Step 1");
self.socket = None;
false
}
} else {
println!("couldn't connect websocket! : Step 2");
self.socket = None;
false
}
}
}
struct JTState {
pub socket: SocketState,
pub try_reconnect: bool,
pub retry_attempts: u32,
2024-03-25 16:28:13 -07:00
2024-03-26 18:56:33 -07:00
pub curr_x: i32,
pub curr_y: i32,
pub last_update_time: Instant,
}
struct UnknownSlayer;
2024-03-25 16:28:13 -07:00
impl FilterFn for UnknownSlayer {
fn filter(&self, ev: Option<Event>, _gilrs: &mut Gilrs) -> Option<Event> {
match ev {
Some(Event {
event: EventType::ButtonPressed(Button::Unknown, ..),
id,
..
})
| Some(Event {
event: EventType::ButtonReleased(Button::Unknown, ..),
id,
..
})
| Some(Event {
event: EventType::AxisChanged(Axis::Unknown, ..),
id,
..
}) => Some(Event::new(id, EventType::Dropped)),
_ => ev,
}
}
}
2024-03-26 18:56:33 -07:00
2024-03-25 16:28:13 -07:00
pub fn joystick_websocket_loop(
tx: Sender<JoystickThreadUpdate>,
do_run: Arc<AtomicBool>,
rx: Receiver<SocketConnectionUpdate>,
) {
let mut gilrs = GilrsBuilder::new().set_update_state(false).build().unwrap();
2024-03-26 18:56:33 -07:00
let mut state = JTState {
socket: SocketState {
ip: String::new(),
port: 0,
socket: None
},
try_reconnect: false,
retry_attempts: 0,
curr_x: 0,
curr_y: 0,
2024-03-25 16:28:13 -07:00
2024-03-26 18:56:33 -07:00
last_update_time: Instant::now(),
};
2024-03-25 16:28:13 -07:00
loop {
match rx.try_recv() {
Ok(msg) => {
2024-03-26 18:56:33 -07:00
state.socket.ip = msg.ip;
state.socket.port = msg.port as i32;
2024-03-25 16:28:13 -07:00
save_config(&AppState {
2024-03-26 18:56:33 -07:00
ip: state.socket.ip.clone(),
port: state.socket.port as u32,
2024-03-25 16:28:13 -07:00
});
2024-03-26 18:56:33 -07:00
println!("ws://{}:{}", state.socket.ip, state.socket.port);
2024-03-25 16:28:13 -07:00
2024-03-26 18:56:33 -07:00
if msg.start_websocket {
if !state.socket.is_connected() {
state.socket.reconnect_websocket();
}
} else if state.socket.is_connected() {
state.socket.close_websocket();
2024-03-25 16:28:13 -07:00
}
}
Err(async_channel::TryRecvError::Closed) => break,
Err(async_channel::TryRecvError::Empty) => {}
}
2024-03-26 18:56:33 -07:00
if state.try_reconnect {
if state.retry_attempts > MAX_RETRY_ATTEMPTS { state.try_reconnect = false; }
if state.socket.is_connected() {
state.try_reconnect = false;
} else if state.socket.reconnect_websocket() {
state.try_reconnect = false;
state.retry_attempts = 0;
}
else {
state.retry_attempts += 1;
}
}
// catch unwind because some buttons on the joystick will panic the gilrs object
2024-03-25 16:28:13 -07:00
match panic::catch_unwind(AssertUnwindSafe(|| {
2024-03-26 18:56:33 -07:00
// get the next event, and if it is an axis we are interested in, update the
// corresponding variable
2024-03-25 16:28:13 -07:00
while let Some(evt) = gilrs.next_event().filter_ev(&UnknownSlayer {}, &mut gilrs) {
match evt.event {
gilrs::EventType::AxisChanged(gilrs::Axis::LeftStickY, val, _) => {
2024-03-26 18:56:33 -07:00
state.curr_y = (val * 100.0) as i32;
if state.curr_y > -10 && state.curr_y < 10 {
state.curr_y = 0;
2024-03-25 16:28:13 -07:00
}
}
gilrs::EventType::AxisChanged(gilrs::Axis::LeftStickX, val, _) => {
2024-03-26 18:56:33 -07:00
state.curr_x = (val * 100.0) as i32;
if state.curr_x > -10 && state.curr_x < 10 {
state.curr_x = 0;
2024-03-25 16:28:13 -07:00
}
}
_ => {}
}
}
})) {
2024-03-26 18:56:33 -07:00
Ok(_) => { }
2024-03-25 16:28:13 -07:00
Err(_) => {
2024-03-26 18:56:33 -07:00
println!("panic-causing event captured in gilrs event handler")
2024-03-25 16:28:13 -07:00
}
}
2024-03-26 18:56:33 -07:00
if state.socket.is_connected()
&& Instant::now().duration_since(state.last_update_time) >= Duration::from_millis(150)
2024-03-25 16:28:13 -07:00
{
let mut message: String;
2024-03-26 18:56:33 -07:00
if state.curr_y > 0 {
message = format!("D{}:", state.curr_y);
2024-03-25 16:28:13 -07:00
} else {
2024-03-26 18:56:33 -07:00
message = format!("U{}:", state.curr_y.abs());
2024-03-25 16:28:13 -07:00
}
2024-03-26 18:56:33 -07:00
if state.curr_x > 0 {
message.push_str(&format!("R{}", state.curr_x));
2024-03-25 16:28:13 -07:00
} else {
2024-03-26 18:56:33 -07:00
message.push_str(&format!("L{}", state.curr_x.abs()));
2024-03-25 16:28:13 -07:00
}
2024-03-26 18:56:33 -07:00
if let Some(ref mut websocket_tx) = state.socket.socket {
if websocket_tx.send_message(&Message::text(message)).is_ok() {
} else {
state.socket.close_websocket();
}
2024-03-25 16:28:13 -07:00
}
2024-03-26 18:56:33 -07:00
state.last_update_time = Instant::now();
2024-03-25 16:28:13 -07:00
continue;
}
match tx.try_send(JoystickThreadUpdate {
2024-03-26 18:56:33 -07:00
connected: state.socket.is_connected(),
x_axis: Some(state.curr_x.to_string()),
y_axis: Some(state.curr_y.to_string()),
2024-03-25 16:28:13 -07:00
}) {
Ok(_) => {}
Err(async_channel::TrySendError::Closed(_)) => break,
Err(async_channel::TrySendError::Full(_)) => {}
}
if !do_run.load(std::sync::atomic::Ordering::SeqCst) {
println!("Exiting thread");
break;
}
std::thread::sleep(Duration::from_millis(25));
}
}