Skip to content

Commit

Permalink
Use generic handle_event
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Feb 24, 2024
1 parent b97991d commit cf8a0a2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion odilia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ odilia-tts = { path = "../tts", version = "0.1.4" }
serde_json.workspace = true
serde_plain.workspace = true
ssip-client-async.workspace = true
tokio.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread"] }
tracing-error.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
Expand Down
39 changes: 32 additions & 7 deletions odilia/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ use tokio::sync::{
};

use crate::state::ScreenReaderState;
use crate::traits::{
IntoOdiliaCommands, Command, IntoStateView, IntoMutableStateView,
};

use atspi_common::events::Event;
use atspi_common::events::{Event, CacheEvents};

use odilia_common::{
errors::OdiliaError,
events::{ScreenReaderEvent},
commands::OdiliaCommand,
};


Expand Down Expand Up @@ -137,15 +142,17 @@ pub async fn process(
rx: &mut Receiver<Event>,
shutdown_rx: &mut broadcast::Receiver<i32>,
) {
let mut event_id: u64 = 0;
loop {
tokio::select! {
event = rx.recv() => {
match event {
Some(good_event) => {
let state_arc = Arc::clone(&state);
tokio::task::spawn(
dispatch_wrapper(state_arc, good_event)
dispatch_wrapper(state_arc, good_event, event_id)
);
event_id += 1;
},
None => {
tracing::debug!("Event was none.");
Expand All @@ -161,8 +168,8 @@ pub async fn process(
}
}

async fn dispatch_wrapper(state: Arc<ScreenReaderState>, good_event: Event) {
match dispatch(&state, good_event).await {
async fn dispatch_wrapper(state: Arc<ScreenReaderState>, good_event: Event, event_id: u64) {
match dispatch(&state, good_event, event_id).await {
Err(e) => {
tracing::error!(error = %e, "Could not handle event");
},
Expand All @@ -173,15 +180,33 @@ async fn dispatch_wrapper(state: Arc<ScreenReaderState>, good_event: Event) {
}
}

async fn dispatch(_state: &ScreenReaderState, event: Event) -> eyre::Result<Vec<ScreenReaderEvent>> {
async fn handle_command<C: IntoMutableStateView + Command + std::fmt::Debug>(command: C, state: &ScreenReaderState, event_id: u64) -> Result<(), OdiliaError> {
tracing::trace!("Command received ({event_id}): {:?}", command);
let mutable_state = command.create_view(&state).await?;
tracing::trace!("Mutable state grab successful ({event_id})");
Ok(command.execute(mutable_state).await?)
}

async fn handle_event<E: IntoStateView + IntoOdiliaCommands + std::fmt::Debug>(event: E, state: &ScreenReaderState, event_id: u64) -> Result<Vec<OdiliaCommand>, OdiliaError> {
tracing::trace!("Event received ({event_id}): {:?}", event);
let state_view = <E as IntoStateView>::create_view(&event, state).await?;
tracing::trace!("Read-only state grab successful ({event_id})");
Ok(<E as IntoOdiliaCommands>::commands(&event, &state_view).await?)
}

async fn dispatch(state: &ScreenReaderState, event: Event, event_id: u64) -> eyre::Result<Vec<OdiliaCommand>> {
// Dispatch based on interface
Ok(match &event {
Ok(match event {
Event::Cache(CacheEvents::Add(add)) => {
let commands = add.commands(&()).await?;
commands
},
other_event => {
tracing::debug!(
"Ignoring event with unknown interface: {:#?}",
other_event
);
vec![]
vec![]
}
})
//let accessible_id = state.new_accessible(&interface).await?.path().try_into()?;
Expand Down
2 changes: 1 addition & 1 deletion odilia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn sigterm_signal_watcher(shutdown_tx: broadcast::Sender<i32>) -> eyre::Re
Ok(())
}

#[tokio::main(flavor = "current_thread")]
#[tokio::main]
async fn main() -> eyre::Result<()> {
logging::init();
// Make sure applications with dynamic accessibility supprt do expose their AT-SPI2 interfaces.
Expand Down
3 changes: 2 additions & 1 deletion odilia/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use odilia_common::{
settings::ApplicationConfig,
events::ScreenReaderEvent,
cache::{AccessiblePrimitive},
commands::OdiliaCommand,
OdiliaResult,
};
use std::sync::Arc;
Expand Down Expand Up @@ -145,7 +146,7 @@ impl ScreenReaderState {
})
}

pub async fn apply_all(&self, _events: Vec<ScreenReaderEvent>) -> OdiliaResult<bool> {
pub async fn apply_all(&self, _events: Vec<OdiliaCommand>) -> OdiliaResult<bool> {
Ok(true)
}

Expand Down

0 comments on commit cf8a0a2

Please sign in to comment.