endbasic_sdl/
lib.rs

1// EndBASIC
2// Copyright 2021 Julio Merino
3//
4// Licensed under the Apache License, Version 2.0 (the "License"); you may not
5// use this file except in compliance with the License.  You may obtain a copy
6// of the License at:
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13// License for the specific language governing permissions and limitations
14// under the License.
15
16//! SDL2-based graphics terminal emulator.
17
18// Keep these in sync with other top-level files.
19#![allow(clippy::await_holding_refcell_ref)]
20#![allow(clippy::collapsible_else_if)]
21#![warn(anonymous_parameters, bad_style, missing_docs)]
22#![warn(unused, unused_extern_crates, unused_import_braces, unused_qualifications)]
23#![warn(unsafe_code)]
24
25use async_channel::Sender;
26use endbasic_core::exec::Signal;
27use endbasic_std::console::Console;
28use std::cell::RefCell;
29use std::io;
30use std::rc::Rc;
31
32mod console;
33mod font;
34mod host;
35mod spec;
36
37/// Converts a flat string error message to an `io::Error`.
38fn string_error_to_io_error(e: String) -> io::Error {
39    io::Error::new(io::ErrorKind::Other, e)
40}
41
42/// Creates the graphical console based on the given `spec`.
43pub fn setup(spec: &str, signals_tx: Sender<Signal>) -> io::Result<Rc<RefCell<dyn Console>>> {
44    let spec = spec::parse_graphics_spec(spec)?;
45    let console = match spec.1 {
46        None => {
47            let default_font = spec::TempFont::default_font()?;
48            console::SdlConsole::new(spec.0, default_font.path(), spec.2, signals_tx)?
49            // The console has been created at this point, so it should be safe to drop
50            // default_font and clean up the on-disk file backing it up.
51        }
52        Some(font_path) => {
53            console::SdlConsole::new(spec.0, font_path.to_owned(), spec.2, signals_tx)?
54        }
55    };
56    Ok(Rc::from(RefCell::from(console)))
57}