brlapi 0.4.1

Safe Rust bindings for the BrlAPI library
// SPDX-License-Identifier: LGPL-2.1

//! Simple BrlAPI example demonstrating the high-level API
//!
//! This example shows how to:
//! - Connect to BrlAPI
//! - Get display information
//! - Write text to the braille display using automatic TTY detection

use brlapi::{Connection, TtyMode, text};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Simple BrlAPI Rust Example");
    println!("==========================");

    // Connect to BrlAPI server
    let connection = Connection::open()?;
    println!("Connected to BrlAPI");

    // Get display information
    let (width, height) = connection.display_size()?;
    let driver = connection.display_driver()?;
    let model = connection.display_model()?;

    println!("Display: {width}x{height} cells");
    println!("Driver: {driver}");
    println!("Model: {model}");

    // Enter TTY mode with automatic TTY detection and RAII cleanup
    let (tty_mode, tty_num) = TtyMode::enter_auto(&connection, None)?;
    println!("TTY mode entered (using virtual console {tty_num})");

    // Write text to the braille display (skip for NoBraille driver)
    let driver = connection.display_driver()?;
    if driver == "NoBraille" {
        println!("Skipping text write for NoBraille driver (testing/development mode)");
    } else {
        // Use contracted text to provide proper literary braille output
        let writer = tty_mode.writer();
        match writer.write_contracted_en_us_g2("Hello from Rust BrlAPI!", text::CursorPosition::Off)
        {
            Ok(()) => {
                println!("Contracted text written to braille display");
            }
            Err(_) => {
                // Fallback to uncontracted if contraction fails
                writer.write_text("Hello from Rust BrlAPI!")?;
                println!("Text written to braille display (uncontracted fallback)");
            }
        }
    }

    println!("\nPress Enter to exit...");
    let mut input = String::new();
    std::io::stdin().read_line(&mut input)?;

    // TTY mode is automatically exited when tty_mode goes out of scope
    println!("Cleaning up...");
    Ok(())
}