extern crate ncurses;
#[macro_use] extern crate const_cstr;
use core::ops::{BitOr, BitAnd};
use core::convert::TryInto;
mod imp_ncurses;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Modifiers(u8);
impl BitOr for Modifiers {
type Output = Modifiers;
fn bitor(self, other: Modifiers) -> Modifiers {
Modifiers(self.0 | other.0)
}
}
impl BitAnd for Modifiers {
type Output = Modifiers;
fn bitand(self, other: Modifiers) -> Modifiers {
Modifiers(self.0 & other.0)
}
}
impl Modifiers {
pub const NONE: Modifiers = Modifiers(0);
pub const SHIFT: Modifiers = Modifiers(0b1);
pub const ALT: Modifiers = Modifiers(0b10);
pub const CTRL: Modifiers = Modifiers(0b100);
pub const fn remove(self, other: Modifiers) -> Modifiers {
Modifiers(self.0 & !other.0)
}
pub const fn bitor(self, other: Modifiers) -> Modifiers {
Modifiers(self.0 | other.0)
}
pub const fn bitand(self, other: Modifiers) -> Modifiers {
Modifiers(self.0 & other.0)
}
pub const fn eq(&self, other: &Modifiers) -> bool {
self.0 == other.0
}
}
#[derive(Copy, Clone, Debug)]
pub enum Event {
KeyPress {
modifiers: Modifiers,
key: KeyInput,
is_repeat: bool,
},
KeyRelease {
modifiers: Modifiers,
key: KeyInput,
},
Mouse {
device_id: u16,
modifiers: Modifiers,
buttons: ncurses::ll::mmask_t,
x: u32,
y: u32,
},
PasteBegin,
PasteEnd,
Resize {
width: u32,
height: u32
}
}
#[derive(Copy, Clone, Debug)]
pub enum KeyInput {
Codepoint(char),
Byte(u8),
Special(i32),
}
pub struct InputStream<'a> {
inner: imp_ncurses::InputStream,
screen: ncurses::ll::WINDOW,
_stdin_lock: std::io::StdinLock<'a>,
}
impl<'a> InputStream<'a> {
pub unsafe fn init_with_ncurses(data: std::io::StdinLock<'a>, screen: ncurses::ll::WINDOW) -> InputStream<'a> {
InputStream {
inner: imp_ncurses::InputStream::init(screen),
screen: screen,
_stdin_lock: data
}
}
pub fn next_event(&mut self) -> Result<Event, ()> {
self.inner.next_event(self.screen)
}
pub fn set_escdelay(&mut self, escdelay: core::time::Duration) {
unsafe {
ncurses::ll::set_escdelay(escdelay.as_millis().try_into().unwrap_or(i32::MAX));
}
}
}