Compare commits
No commits in common. "8d532ffd82c963524c98eac7d93d59b7bf739f9a" and "cf094dd746e42ea2afa9a100cecb2bf6014c06aa" have entirely different histories.
8d532ffd82
...
cf094dd746
6 changed files with 76 additions and 88 deletions
|
@ -1,7 +1,7 @@
|
||||||
use config::{Config, FileFormat};
|
use config::{Config, FileFormat};
|
||||||
use log::{error, info};
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use log::{error, info};
|
||||||
|
|
||||||
use crate::ui_code::AppState;
|
use crate::ui_code::AppState;
|
||||||
|
|
||||||
|
@ -23,19 +23,23 @@ pub fn load_config() -> AppState {
|
||||||
|
|
||||||
pub fn save_config(config: &AppState) {
|
pub fn save_config(config: &AppState) {
|
||||||
match toml::to_string(&config) {
|
match toml::to_string(&config) {
|
||||||
Ok(toml_str) => match File::create("./settings.toml") {
|
Ok(toml_str) => {
|
||||||
Ok(mut file) => match file.write_all(toml_str.as_bytes()) {
|
match File::create("./settings.toml") {
|
||||||
Ok(_) => {
|
Ok(mut file) => {
|
||||||
info!("Config file saved succesfully");
|
match file.write_all(toml_str.as_bytes()) {
|
||||||
|
Ok(_) => {
|
||||||
|
info!("Config file saved succesfully");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Couldn't write config file contents to open file: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Couldn't write config file contents to open file: {e}");
|
error!("Couldn't open settings file: {e}");
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
error!("Couldn't open settings file: {e}");
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Could not serialize app state: {e}");
|
error!("Could not serialize app state: {e}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
|
use std::sync::{atomic::{AtomicBool, Ordering}, Arc};
|
||||||
use std::pin::{pin, Pin};
|
use std::pin::{pin, Pin};
|
||||||
use std::sync::{
|
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use async_channel::{Receiver, Sender};
|
use async_channel::{Receiver, Sender};
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
|
@ -37,13 +34,8 @@ struct CoordState<'a> {
|
||||||
pub rt: Handle,
|
pub rt: Handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CoordState<'a> {
|
impl <'a> CoordState<'a> {
|
||||||
pub fn new(
|
pub fn new(mec: Pin<&'a mut Receiver<ApplicationEvent>>, to_mec: Sender<ApplicationEvent>, to_gui: Sender<GuiUpdate>, rt: Handle) -> Self {
|
||||||
mec: Pin<&'a mut Receiver<ApplicationEvent>>,
|
|
||||||
to_mec: Sender<ApplicationEvent>,
|
|
||||||
to_gui: Sender<GuiUpdate>,
|
|
||||||
rt: Handle,
|
|
||||||
) -> Self {
|
|
||||||
CoordState {
|
CoordState {
|
||||||
to_socket: None,
|
to_socket: None,
|
||||||
|
|
||||||
|
@ -51,10 +43,7 @@ impl<'a> CoordState<'a> {
|
||||||
sck_recv_alive: Arc::new(AtomicBool::new(false)),
|
sck_recv_alive: Arc::new(AtomicBool::new(false)),
|
||||||
joystick_loop_alive: Arc::new(AtomicBool::new(false)),
|
joystick_loop_alive: Arc::new(AtomicBool::new(false)),
|
||||||
|
|
||||||
mec,
|
mec, to_mec, to_gui, rt
|
||||||
to_mec,
|
|
||||||
to_gui,
|
|
||||||
rt,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +60,8 @@ impl<'a> CoordState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn socket_start(&mut self, conn: String) {
|
pub async fn socket_start(&mut self, conn: String) {
|
||||||
if !(self.sck_recv_alive.load(Ordering::SeqCst)
|
if !(self.sck_recv_alive.load(Ordering::SeqCst) && !self.sck_send_alive.load(Ordering::SeqCst)) {
|
||||||
&& self.sck_send_alive.load(Ordering::SeqCst))
|
|
||||||
{
|
|
||||||
info!("Starting socket");
|
info!("Starting socket");
|
||||||
let (to_socket, socket_sender_rx) = async_channel::bounded::<Message>(10);
|
let (to_socket, socket_sender_rx) = async_channel::bounded::<Message>(10);
|
||||||
|
|
||||||
|
@ -85,31 +73,27 @@ impl<'a> CoordState<'a> {
|
||||||
socket_sender_rx,
|
socket_sender_rx,
|
||||||
self.sck_send_alive.clone(),
|
self.sck_send_alive.clone(),
|
||||||
self.sck_recv_alive.clone(),
|
self.sck_recv_alive.clone(),
|
||||||
self.rt.clone(),
|
self.rt.clone()
|
||||||
)
|
).await;
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check_states(&mut self) {
|
pub async fn check_states(&mut self) {
|
||||||
if !self.joystick_loop_alive.load(Ordering::SeqCst) {
|
if !self.joystick_loop_alive.load(Ordering::SeqCst) {
|
||||||
self.rt.spawn(joystick_loop(
|
self.rt.spawn(joystick_loop(self.to_mec.clone(), self.joystick_loop_alive.clone()));
|
||||||
self.to_mec.clone(),
|
|
||||||
self.joystick_loop_alive.clone(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.sck_recv_alive.load(Ordering::SeqCst)
|
if !self.sck_recv_alive.load(Ordering::SeqCst) || !self.sck_send_alive.load(Ordering::SeqCst) {
|
||||||
|| !self.sck_send_alive.load(Ordering::SeqCst)
|
|
||||||
{
|
|
||||||
self.socket_close().await;
|
self.socket_close().await;
|
||||||
|
|
||||||
if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(false)).await {
|
if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(false)).await {
|
||||||
error!("Cannot send message to gui thread: {e}");
|
error!("Cannot send message to gui thread: {e}");
|
||||||
}
|
}
|
||||||
} else if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(true)).await {
|
} else {
|
||||||
error!("Cannot send message to gui thread: {e}");
|
if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(true)).await {
|
||||||
self.close().await;
|
error!("Cannot send message to gui thread: {e}");
|
||||||
|
self.close().await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,28 +107,29 @@ impl<'a> CoordState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn socket_close(&mut self) {
|
pub async fn socket_close(&mut self) {
|
||||||
self.sck_send_alive.store(false, Ordering::SeqCst);
|
self.sck_send_alive.store(false, Ordering::SeqCst);
|
||||||
self.sck_recv_alive.store(false, Ordering::SeqCst);
|
self.sck_recv_alive.store(false, Ordering::SeqCst);
|
||||||
if let Some(tx) = self.to_socket.take() {
|
if let Some(tx) = self.to_socket.take() {
|
||||||
tx.close();
|
tx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main_Event_Channel
|
// Main_Event_Channel
|
||||||
pub async fn start_coordinator(
|
pub async fn start_coordinator(mec: Receiver<ApplicationEvent>, to_mec: Sender<ApplicationEvent>, to_gui: Sender<GuiUpdate>, runtime: Handle) {
|
||||||
mec: Receiver<ApplicationEvent>,
|
|
||||||
to_mec: Sender<ApplicationEvent>,
|
|
||||||
to_gui: Sender<GuiUpdate>,
|
|
||||||
runtime: Handle,
|
|
||||||
) {
|
|
||||||
info!("Starting coordinator!");
|
info!("Starting coordinator!");
|
||||||
|
|
||||||
let mec = pin!(mec);
|
let mec = pin!(mec);
|
||||||
|
|
||||||
let mut state = CoordState::new(mec, to_mec, to_gui, runtime);
|
let mut state = CoordState::new(
|
||||||
|
mec,
|
||||||
|
to_mec,
|
||||||
|
to_gui,
|
||||||
|
runtime
|
||||||
|
);
|
||||||
|
|
||||||
while let Some(msg) = state.mec.next().await {
|
while let Some(msg) = state.mec.next().await {
|
||||||
|
|
||||||
state.check_states().await;
|
state.check_states().await;
|
||||||
|
|
||||||
match msg {
|
match msg {
|
||||||
|
@ -186,4 +171,5 @@ pub async fn start_coordinator(
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Stopping Coordinator");
|
info!("Stopping Coordinator");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,11 @@ use crate::coordinator::{ApplicationEvent, MoveEvent};
|
||||||
|
|
||||||
use async_channel::Sender;
|
use async_channel::Sender;
|
||||||
use gilrs::{ev::filter::FilterFn, Axis, Button, Event, EventType, Filter, Gilrs, GilrsBuilder};
|
use gilrs::{ev::filter::FilterFn, Axis, Button, Event, EventType, Filter, Gilrs, GilrsBuilder};
|
||||||
use log::{info, warn};
|
use log::{debug, info, warn};
|
||||||
use std::{
|
use std::{
|
||||||
panic::{self, AssertUnwindSafe},
|
panic::{self, AssertUnwindSafe},
|
||||||
sync::{atomic::AtomicBool, Arc},
|
sync::{atomic::AtomicBool, Arc},
|
||||||
time::Duration,
|
time::Duration
|
||||||
};
|
};
|
||||||
|
|
||||||
static MAX_SENT_ZEROS: u32 = 10;
|
static MAX_SENT_ZEROS: u32 = 10;
|
||||||
|
@ -35,7 +35,10 @@ impl FilterFn for UnknownSlayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn joystick_loop(tx: Sender<ApplicationEvent>, is_alive: Arc<AtomicBool>) {
|
pub async fn joystick_loop(
|
||||||
|
tx: Sender<ApplicationEvent>,
|
||||||
|
is_alive: Arc<AtomicBool>
|
||||||
|
) {
|
||||||
let mut gilrs = GilrsBuilder::new().set_update_state(false).build().unwrap();
|
let mut gilrs = GilrsBuilder::new().set_update_state(false).build().unwrap();
|
||||||
|
|
||||||
is_alive.store(true, std::sync::atomic::Ordering::SeqCst);
|
is_alive.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
@ -92,11 +95,9 @@ pub async fn joystick_loop(tx: Sender<ApplicationEvent>, is_alive: Arc<AtomicBoo
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(async_channel::TrySendError::Closed(_)) => {
|
Err(async_channel::TrySendError::Closed(_)) => {
|
||||||
info!("MEC is closed, stopping Joystick loop");
|
info!("MEC is closed, stopping Joystick loop");
|
||||||
break;
|
break
|
||||||
}
|
|
||||||
Err(async_channel::TrySendError::Full(_)) => {
|
|
||||||
warn!("[joystick loop] The MEC is full!")
|
|
||||||
}
|
}
|
||||||
|
Err(async_channel::TrySendError::Full(_)) => {warn!("[joystick loop] The MEC is full!")}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ mod socket_loop;
|
||||||
mod ui_code;
|
mod ui_code;
|
||||||
const APP_ID: &str = "net.nickiel.joystick-controller-client";
|
const APP_ID: &str = "net.nickiel.joystick-controller-client";
|
||||||
|
|
||||||
|
|
||||||
fn main() -> glib::ExitCode {
|
fn main() -> glib::ExitCode {
|
||||||
env::set_var("gtk_csd", "0");
|
env::set_var("gtk_csd", "0");
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use std::sync::{
|
use std::sync::{atomic::{AtomicBool, Ordering}, Arc};
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use async_channel::{Receiver, Sender};
|
use async_channel::{Receiver, Sender};
|
||||||
use futures_util::{
|
use futures_util::{
|
||||||
|
@ -20,7 +17,7 @@ async fn socket_listen(
|
||||||
mut reader: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
|
mut reader: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
|
||||||
) {
|
) {
|
||||||
if socket_recv_is_alive.load(std::sync::atomic::Ordering::SeqCst) {
|
if socket_recv_is_alive.load(std::sync::atomic::Ordering::SeqCst) {
|
||||||
while let Some(msg) = reader.next().await {
|
while let Some(msg) = reader.next().await {
|
||||||
match msg {
|
match msg {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
if let Err(e) = mec.send(ApplicationEvent::SocketMessage(val)).await {
|
if let Err(e) = mec.send(ApplicationEvent::SocketMessage(val)).await {
|
||||||
|
@ -28,11 +25,11 @@ async fn socket_listen(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Websocket error: {:#?}", e);
|
error!("Websocket error: {:#?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
socket_recv_is_alive.store(false, Ordering::SeqCst);
|
socket_recv_is_alive.store(false, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
@ -72,24 +69,24 @@ pub async fn socket_loop(
|
||||||
send_to_socket: Receiver<Message>,
|
send_to_socket: Receiver<Message>,
|
||||||
socket_send_is_alive: Arc<AtomicBool>,
|
socket_send_is_alive: Arc<AtomicBool>,
|
||||||
socket_recv_is_alive: Arc<AtomicBool>,
|
socket_recv_is_alive: Arc<AtomicBool>,
|
||||||
rt: Handle,
|
rt: Handle
|
||||||
) {
|
) {
|
||||||
info!("Starting Socket Loop");
|
info!("Starting Socket Loop");
|
||||||
|
|
||||||
|
|
||||||
socket_send_is_alive.store(true, Ordering::SeqCst);
|
socket_send_is_alive.store(true, Ordering::SeqCst);
|
||||||
socket_recv_is_alive.store(true, Ordering::SeqCst);
|
socket_recv_is_alive.store(true, Ordering::SeqCst);
|
||||||
|
|
||||||
let socket: Option<WebSocketStream<MaybeTlsStream<TcpStream>>> =
|
let socket: Option<WebSocketStream<MaybeTlsStream<TcpStream>>> = match connect_async(connection_string).await {
|
||||||
match connect_async(connection_string).await {
|
Ok((val, _)) => {
|
||||||
Ok((val, _)) => {
|
info!("Socket connection made successfully");
|
||||||
info!("Socket connection made successfully");
|
Some(val)
|
||||||
Some(val)
|
}
|
||||||
}
|
Err(_) => {
|
||||||
Err(_) => {
|
error!("Couldn't connect to URL");
|
||||||
error!("Couldn't connect to URL");
|
None
|
||||||
None
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(sckt) = socket {
|
if let Some(sckt) = socket {
|
||||||
let (outbound, inbound) = sckt.split();
|
let (outbound, inbound) = sckt.split();
|
||||||
|
|
|
@ -9,6 +9,7 @@ use crate::config::{load_config, save_config};
|
||||||
// use crate::{joystick_loop, JoystickThreadUpdate};
|
// use crate::{joystick_loop, JoystickThreadUpdate};
|
||||||
use crate::coordinator::{start_coordinator, ApplicationEvent, MoveEvent};
|
use crate::coordinator::{start_coordinator, ApplicationEvent, MoveEvent};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub ip: String,
|
pub ip: String,
|
||||||
|
@ -37,12 +38,8 @@ pub fn build_ui(app: &Application, runtime: Handle) {
|
||||||
let (to_mec, mec) = async_channel::unbounded::<ApplicationEvent>();
|
let (to_mec, mec) = async_channel::unbounded::<ApplicationEvent>();
|
||||||
let (to_gui, gui_recv) = async_channel::bounded::<GuiUpdate>(10);
|
let (to_gui, gui_recv) = async_channel::bounded::<GuiUpdate>(10);
|
||||||
|
|
||||||
runtime.spawn(start_coordinator(
|
runtime.spawn(start_coordinator(mec, to_mec.clone(), to_gui, runtime.clone()));
|
||||||
mec,
|
|
||||||
to_mec.clone(),
|
|
||||||
to_gui,
|
|
||||||
runtime.clone(),
|
|
||||||
));
|
|
||||||
|
|
||||||
// let conn_status_label = Label::new(Some(&"No Connection".to_string()));
|
// let conn_status_label = Label::new(Some(&"No Connection".to_string()));
|
||||||
let conn_status_label = Label::builder()
|
let conn_status_label = Label::builder()
|
||||||
|
@ -160,7 +157,9 @@ pub fn build_ui(app: &Application, runtime: Handle) {
|
||||||
.child(&main_box)
|
.child(&main_box)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
window.connect_close_request(move |_| glib::Propagation::Proceed);
|
window.connect_close_request(move |_| {
|
||||||
|
glib::Propagation::Proceed
|
||||||
|
});
|
||||||
|
|
||||||
// Present window
|
// Present window
|
||||||
window.present();
|
window.present();
|
||||||
|
|
Loading…
Reference in a new issue