Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
elpekenin committed Sep 3, 2023
1 parent c7ce2fb commit 0f2e4a1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src-tauri/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Serialize;
use ts_rs::TS;
use uuid::Uuid;
use xap_specs::protocol::xap::XAPSecureStatus;
use xap_specs::protocol::{xap::XAPSecureStatus, BroadcastRaw};

use crate::aggregation::XAPDevice as XAPDeviceDTO;

Expand Down Expand Up @@ -39,5 +39,9 @@ pub(crate) enum XAPEvent {
RemovedDevice(Uuid),
AnnounceAllDevices,
RxError,
ReceivedUserBroadcast {
id: Uuid,
broadcast: BroadcastRaw,
},
Exit,
}
39 changes: 35 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod commands;
mod aggregation;
mod events;
mod user;
mod xap;

use std::sync::Arc;
Expand All @@ -26,6 +27,7 @@ use tauri::{AppHandle, Manager};

use commands::*;
use events::{FrontendEvent, XAPEvent};
use user::UserData;
use xap::hid::XAPClient;
use xap::ClientResult;
use xap_specs::constants::XAPConstants;
Expand All @@ -44,6 +46,7 @@ fn start_event_loop(
app: AppHandle,
state: Arc<Mutex<XAPClient>>,
event_channel: Receiver<XAPEvent>,
user_data: Arc<Mutex<UserData>>,
) {
let _ = std::thread::spawn(move || {
let ticker = tick(Duration::from_millis(500));
Expand All @@ -55,6 +58,9 @@ fn start_event_loop(
match msg {
Ok(XAPEvent::Exit) => {
info!("received shutdown signal, exiting!");
let client = state.lock();
let mut user_data = user_data.lock();
user::on_close(&client, &mut user_data);
break 'event_loop;
},
Ok(XAPEvent::LogReceived{id, log}) => {
Expand All @@ -70,11 +76,17 @@ fn start_event_loop(
info!("detected new device - notifying frontend!");

app.emit_all("new-device", FrontendEvent::NewDevice{ device: device.as_dto() }).unwrap();

let mut user_data = user_data.lock();
user::new_device(device, &mut user_data);
}
},
Ok(XAPEvent::RemovedDevice(id)) => {
info!("removed device - notifying frontend!");
app.emit_all("removed-device", FrontendEvent::RemovedDevice{ id }).unwrap();

let mut user_data = user_data.lock();
user::removed_device(&id, &mut user_data);
},
Ok(XAPEvent::AnnounceAllDevices) => {
let mut state = state.lock();
Expand All @@ -90,6 +102,12 @@ fn start_event_loop(
error!("failed to enumerate XAP devices: {err}");
}
},
Ok(XAPEvent::ReceivedUserBroadcast{id, broadcast}) => {
let state = state.lock();
let device = state.get_device(&id).unwrap();
let mut user_data = user_data.lock();
user::broadcast_callback(broadcast, device, &mut user_data);
},
Err(err) => {
error!("error receiving event {err}");
},
Expand All @@ -99,9 +117,15 @@ fn start_event_loop(
recv(ticker) -> msg => {
match msg {
Ok(_) => {
if let Err(err) = state.lock().enumerate_xap_devices() {
let mut state = state.lock();

if let Err(err) = state.enumerate_xap_devices() {
error!("failed to enumerate XAP devices: {err}");
return;
}

let mut user_data = user_data.lock();
user::housekeeping(&state, &mut user_data);
},
Err(err) => {
error!("failed receiving tick {err}");
Expand All @@ -118,6 +142,10 @@ fn main() -> ClientResult<()> {
.format_timestamp(None)
.init();

let user_data = Arc::new(Mutex::new(UserData::default()));

user::pre_init();

let (event_channel_tx, event_channel_rx): (Sender<XAPEvent>, Receiver<XAPEvent>) = unbounded();

tauri::Builder::default()
Expand Down Expand Up @@ -151,12 +179,15 @@ fn main() -> ClientResult<()> {
.expect("unable to find XAP specifications");

let state = Arc::new(Mutex::new(
XAPClient::new(Sender::clone(&event_channel_tx), XAPConstants::new(xap_specs)?)
.expect("failed to initialize XAP state"),
XAPClient::new(
Sender::clone(&event_channel_tx),
XAPConstants::new(xap_specs)?,
)
.expect("failed to initialize XAP state"),
));
app.manage(Arc::clone(&state));

start_event_loop(app.handle(), state, event_channel_rx);
start_event_loop(app.handle(), state, event_channel_rx, user_data);

app.listen_global("frontend-loaded", move |_| {
let event_tx = event_channel_tx.clone();
Expand Down
26 changes: 26 additions & 0 deletions src-tauri/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Empty placeholders where users can add their logic
use crate::xap::hid::{XAPClient, XAPDevice};

use uuid::Uuid;
use xap_specs::protocol::BroadcastRaw;

// Storage for user data
#[derive(Default)]
pub struct UserData {}

pub(crate) fn pre_init() {}

pub(crate) fn on_close(_client: &XAPClient, _user_data: &mut UserData) {}

pub(crate) fn new_device(_device: &XAPDevice, _user_data: &mut UserData) {}

pub(crate) fn removed_device(_id: &Uuid, _user_data: &mut UserData) {}

pub(crate) fn broadcast_callback(
_broadcast: BroadcastRaw,
_device: &XAPDevice,
_user_data: &mut UserData,
) {
}

pub(crate) fn housekeeping(_client: &XAPClient, _user_data: &mut UserData) {}
1 change: 1 addition & 0 deletions src-tauri/src/xap/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 5 additions & 1 deletion src-tauri/src/xap/hid/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ fn handle_report(
.expect("failed to send broadcast event!");
}
BroadcastType::Keyboard => info!("keyboard broadcasts are not implemented!"),
BroadcastType::User => info!("keyboard broadcasts are not implemented!"),
BroadcastType::User => {
event_channel
.send(XAPEvent::ReceivedUserBroadcast { id, broadcast })
.expect("failed to send user broadcast event!");
}
}
} else {
let response = RawResponse::from_raw_report(&report)?;
Expand Down
4 changes: 2 additions & 2 deletions xap-specs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod constants;
pub mod error;
pub mod protocol;
pub mod error;
pub mod token;
pub mod request;
pub mod response;
pub mod token;
5 changes: 5 additions & 0 deletions xap-specs/src/protocol/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ impl XAPBroadcast for LogBroadcast {}
pub struct SecureStatusBroadcast(pub XAPSecureStatus);

impl XAPBroadcast for SecureStatusBroadcast {}

#[derive(BinRead, Debug, Clone)]
pub struct UserBroadcast {}

impl XAPBroadcast for UserBroadcast {}

0 comments on commit 0f2e4a1

Please sign in to comment.