use brlapi::Result;
use brlapi::cooperative::{self, AppType, ContentQuality, CooperativeDisplay};
use std::thread;
use std::time::Duration;
fn main() -> Result<()> {
println!("Cooperative Braille Display Sharing Demo");
println!("=====================================");
println!("This example demonstrates the new cooperative API for polite");
println!("braille display sharing with screen readers like Orca.\n");
println!("1. Simple notification functions...");
demo_simple_notifications()?;
thread::sleep(Duration::from_secs(1));
println!("\n2. Application-type-based cooperation...");
demo_app_type_cooperation()?;
thread::sleep(Duration::from_secs(1));
println!("\n3. Content-quality-aware priority adjustment...");
demo_content_quality_cooperation()?;
thread::sleep(Duration::from_secs(1));
println!("\n4. Message queuing for busy displays...");
demo_message_queuing()?;
println!("\n[SUCCESS] Cooperative display sharing demo completed!");
println!("\nKey benefits of the cooperative API:");
println!("- Automatic priority management based on Orca research");
println!("- Content-aware priority adjustment like BRLTTY 6.1+");
println!("- Polite yielding to screen readers");
println!("- Simple API for common notification use cases");
println!("- No complex retry logic needed - handled automatically");
Ok(())
}
fn demo_simple_notifications() -> Result<()> {
println!(" Testing simple notification functions...");
cooperative::notify("Build completed successfully")?;
println!(" [SUCCESS] Sent: Build notification");
thread::sleep(Duration::from_millis(1500));
cooperative::alert("Critical error occurred!")?;
println!(" [SUCCESS] Sent: Critical alert");
thread::sleep(Duration::from_millis(1500));
cooperative::debug("Breakpoint hit at line 42")?;
println!(" [SUCCESS] Sent: Debug message");
println!(" Simple notifications completed - these use automatic cooperation");
Ok(())
}
fn demo_app_type_cooperation() -> Result<()> {
println!(" Testing different application types with research-based priorities...");
println!(" Creating background app (priority 35)...");
let mut background = CooperativeDisplay::background_app()?;
background.show_status("Processing file 5 of 20")?;
println!(" [SUCCESS] Background app: Status update sent");
thread::sleep(Duration::from_millis(1000));
println!(" Creating user app (priority 45)...");
let user_app = CooperativeDisplay::open(AppType::UserApp)?;
user_app.show_message("Operation completed successfully")?;
println!(" [SUCCESS] User app: Completion message sent");
thread::sleep(Duration::from_millis(1000));
println!(" Creating system alert (priority 65)...");
let system_alert = CooperativeDisplay::open(AppType::SystemAlert)?;
system_alert.show_message("System maintenance required")?;
println!(" [SUCCESS] System alert: Maintenance notice sent");
println!(" Application type cooperation completed");
Ok(())
}
fn demo_content_quality_cooperation() -> Result<()> {
println!(" Testing content-quality-aware priority adjustment...");
let display = CooperativeDisplay::open(AppType::UserApp)?;
println!(" Showing poor quality content (base priority)...");
display.show_content("Background task is running", ContentQuality::Poor)?;
println!(" [SUCCESS] Poor quality: Background info displayed");
thread::sleep(Duration::from_millis(1000));
println!(" Showing fair quality content (priority +10)...");
display.show_content("File saved successfully", ContentQuality::Fair)?;
println!(" [SUCCESS] Fair quality: Status message displayed");
thread::sleep(Duration::from_millis(1000));
println!(" Showing good quality content (priority +20)...");
display.show_content("Press Enter to continue", ContentQuality::Good)?;
println!(" [SUCCESS] Good quality: Interactive prompt displayed");
println!(" Content quality cooperation completed");
println!(" Note: Good quality content (priority +20) exceeds screen reader base (50)");
Ok(())
}
fn demo_message_queuing() -> Result<()> {
println!(" Testing message queuing for busy displays...");
let mut display = CooperativeDisplay::open(AppType::UserApp)?;
println!(" Queueing multiple messages...");
display.queue_message("Task 1 started", ContentQuality::Fair)?;
display.queue_message("Task 2 started", ContentQuality::Fair)?;
display.queue_message("ERROR: Task 3 failed!", ContentQuality::Good)?;
display.queue_message("All tasks completed", ContentQuality::Fair)?;
println!(
" [SUCCESS] Queued {} messages",
display.pending_messages()
);
println!(" Processing message queue cooperatively...");
let processed = display.process_queue()?;
println!(" [SUCCESS] Processed {} messages from queue", processed);
if display.pending_messages() > 0 {
println!(
" Note: {} messages remain queued (display may be busy)",
display.pending_messages()
);
}
println!(" Message queuing completed");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cooperative_example_functions() {
println!("Testing cooperative notification example functions...");
let result1 = demo_simple_notifications();
let result2 = demo_app_type_cooperation();
let result3 = demo_content_quality_cooperation();
let result4 = demo_message_queuing();
match (result1, result2, result3, result4) {
(Ok(()), Ok(()), Ok(()), Ok(())) => {
println!("[SUCCESS] All cooperative demo functions completed");
}
_ => {
println!("[INFO] Some demo functions failed (expected if no BrlAPI daemon)");
}
}
}
#[test]
fn test_priority_relationships() {
let user_base = AppType::UserApp.base_priority();
let screen_reader_base = AppType::ScreenReader.base_priority();
let good_boost = ContentQuality::Good.priority_offset();
assert!(user_base + good_boost as u32 > screen_reader_base);
assert!(AppType::SystemAlert.base_priority() > screen_reader_base);
assert!(AppType::BackgroundTask.base_priority() < user_base);
println!("[SUCCESS] Priority relationships verified");
}
}