Skip to content

Commit

Permalink
cleanup server code + fix a lost update case (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber authored Sep 5, 2024
1 parent 6cd1901 commit 39fed03
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 80 deletions.
8 changes: 8 additions & 0 deletions lan-mouse-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn run() -> Result<(), IpcError> {

struct Cli {
clients: Vec<(ClientHandle, ClientConfig, ClientState)>,
changed: Option<ClientHandle>,
rx: AsyncFrontendEventReader,
tx: AsyncFrontendRequestWriter,
}
Expand All @@ -38,6 +39,7 @@ impl Cli {
fn new(rx: AsyncFrontendEventReader, tx: AsyncFrontendRequestWriter) -> Cli {
Self {
clients: vec![],
changed: None,
rx,
tx,
}
Expand Down Expand Up @@ -80,9 +82,14 @@ impl Cli {
event = self.rx.next() => {
if let Some(event) = event {
self.handle_event(event?);
} else {
break Ok(());
}
}
}
if let Some(handle) = self.changed.take() {
self.update_client(handle).await?;
}
}
}

Expand Down Expand Up @@ -202,6 +209,7 @@ impl Cli {

fn handle_event(&mut self, event: FrontendEvent) {
match event {
FrontendEvent::Changed(h) => self.changed = Some(h),
FrontendEvent::Created(h, c, s) => {
eprint!("client added ({h}): ");
print_config(&c);
Expand Down
5 changes: 4 additions & 1 deletion lan-mouse-gtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{env, process, str};

use window::Window;

use lan_mouse_ipc::FrontendEvent;
use lan_mouse_ipc::{FrontendEvent, FrontendRequest};

use adw::Application;
use gtk::{
Expand Down Expand Up @@ -92,6 +92,9 @@ fn build_ui(app: &Application) {
loop {
let notify = receiver.recv().await.unwrap_or_else(|_| process::exit(1));
match notify {
FrontendEvent::Changed(handle) => {
window.request(FrontendRequest::GetState(handle));
}
FrontendEvent::Created(handle, client, state) => {
window.new_client(handle, client, state);
}
Expand Down
6 changes: 5 additions & 1 deletion lan-mouse-gtk/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ impl Window {
}

pub fn request_client_state(&self, client: &ClientObject) {
self.request(FrontendRequest::GetState(client.handle()));
self.request_client_state_for(client.handle());
}

pub fn request_client_state_for(&self, handle: ClientHandle) {
self.request(FrontendRequest::GetState(handle));
}

pub fn request_client_create(&self) {
Expand Down
2 changes: 2 additions & 0 deletions lan-mouse-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub struct ClientState {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FrontendEvent {
/// client state has changed, new state must be requested via [`FrontendRequest::GetState`]
Changed(ClientHandle),
/// a client was created
Created(ClientHandle, ClientConfig, ClientState),
/// no such client
Expand Down
Loading

0 comments on commit 39fed03

Please sign in to comment.