use std::error::Error;
use brlapi::{BrlApiError, Connection, TtyMode, text};
fn main() -> Result<(), Box<dyn Error>> {
println!("BrlAPI Rust Tutorial Example");
println!("============================");
println!("Connecting to BrlAPI server...");
let connection = match Connection::open() {
Ok(conn) => {
println!("Connected successfully!");
conn
}
Err(BrlApiError::ConnectionRefused) => {
eprintln!("Connection refused - is brltty/xbrlapi running?");
return Err("BrlAPI connection failed".into());
}
Err(e) => {
eprintln!("Connection failed: {e}");
return Err(e.into());
}
};
let fd = connection.file_descriptor();
println!("File descriptor: {fd}");
println!("\nGetting display information...");
let (width, height) = connection.display_size()?;
println!("Display size: {width} columns x {height} rows");
let driver_name = connection
.display_driver()
.unwrap_or_else(|_| "unknown".to_string());
println!("Driver: {driver_name}");
let model = connection
.display_model()
.unwrap_or_else(|_| "unknown".to_string());
println!("Model: {model}");
if width == 0 || height == 0 {
eprintln!("Warning: Display reports zero size - this might be a dummy display");
eprintln!("Text writing may not work properly with driver: {driver_name}");
}
println!("\nEntering TTY mode...");
let (tty_mode, tty_num) = TtyMode::enter_auto(&connection, None)?;
println!("TTY mode entered for virtual console {tty_num}");
if driver_name == "NoBraille" {
println!(
"\nSkipping text write operations for NoBraille driver (testing/development mode)"
);
println!("In real usage, this would write to your braille display");
} else {
println!("\nWriting text to braille display...");
let writer = tty_mode.writer();
match writer.write_contracted_en_us_g2(
"Hello from Rust! Press any key to continue...",
text::CursorPosition::Off,
) {
Ok(()) => println!("Contracted text written to display"),
Err(_) => {
writer.write_text("Hello from Rust! Press any key to continue...")?;
println!("Text written to display (uncontracted fallback)");
}
}
println!("\nText should now be visible on the braille display.");
println!("Press Enter to continue...");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
println!("Writing second message...");
match writer
.write_contracted_en_us_g2("BrlAPI + Rust = Perfect match!", text::CursorPosition::Off)
{
Ok(()) => {} Err(_) => {
let _ = writer.write_text("BrlAPI + Rust = Perfect match!");
}
}
println!("Second message written");
}
println!("\nPress Enter to exit...");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
println!("TTY mode and connection will be cleaned up automatically");
println!("\nTutorial completed successfully!");
Ok(())
}