1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#[macro_use]
extern crate bitflags;
#[cfg(windows)]
extern crate kernel32;
#[cfg(not(windows))]
#[macro_use]
extern crate lazy_static;
extern crate libc;
#[cfg(not(windows))]
#[macro_use]
extern crate tinf;
extern crate tvis_util;
#[cfg(windows)]
extern crate user32;
#[cfg(windows)]
extern crate winapi;

use std::{error, fmt, io, result};
use std::sync::mpsc::SendError;

pub mod term;
pub mod input;

/////////////////////////////////////////////////////////////////
pub type Coords = (u16, u16);

#[derive(Debug)]
pub struct Error {
    inner: ErrorImpl,
}

impl Error {
    fn ffi_err<T>(msg: &str) -> Result<T> {
        Err(Error {
            inner: ErrorImpl::FFI(msg.to_owned(), io::Error::last_os_error()),
        })
    }
}

#[derive(Debug)]
enum ErrorImpl {
    Io(io::Error),
    FFI(String, io::Error),
    TX,
    #[cfg(not(windows))] Cap(::tinf::CapError),
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error {
            inner: ErrorImpl::Io(err),
        }
    }
}

impl From<SendError<Box<input::Event>>> for Error {
    fn from(_: SendError<Box<input::Event>>) -> Error {
        Error {
            inner: ErrorImpl::TX,
        }
    }
}

#[cfg(not(windows))]
impl From<::tinf::CapError> for Error {
    fn from(err: ::tinf::CapError) -> Error {
        Error {
            inner: ErrorImpl::Cap(err),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.inner {
            ErrorImpl::Io(ref err) => err.fmt(f),
            ErrorImpl::FFI(ref msg, _) => write!(f, "{}", msg),
            ErrorImpl::TX => write!(f, "channel send error"),
            #[cfg(not(windows))]
            ErrorImpl::Cap(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match self.inner {
            ErrorImpl::Io(ref err) => err.description(),
            ErrorImpl::FFI(..) => "terminal FFI error",
            ErrorImpl::TX => "channel send error",
            #[cfg(not(windows))]
            ErrorImpl::Cap(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match self.inner {
            ErrorImpl::Io(ref err) | ErrorImpl::FFI(_, ref err) => Some(err),
            _ => None,
        }
    }
}

/// Either success or failure.
pub type Result<T> = result::Result<T, Error>;

#[allow(dead_code)]
#[cfg(not(windows))]
const SILENCE_WARNING_FOR_TEST_ONLY_MACRO_USE: [tinf::Param; 0] = params!();

#[cfg(not(windows))]
fn is_rxvt(desc: &::tinf::Desc) -> bool {
    !desc.names().is_empty() && desc.names()[0].starts_with("rxvt-unicode")
}