Commits

Anonymous committed beb2776

First commit. Adding a README.

Comments (0)

Files changed (3)

+# This is my README
+extern mod tb(name="termbox");
+
+// =======================================================
+fn main() {
+  unsafe {
+    tb::init();
+    tb::print(1, 1, tb::Bold, tb::White, tb::Black, ~"Hello, world!"); 
+    tb::present(); 
+
+    loop {
+       let tmp = tb::poll_event();
+        match tmp {
+            tb::KeyEvent{md:_,key:_,ch:_ } => break,
+          _            => ()
+        } 
+    }
+/*
+       match tmp {
+            
+            tb::KeyEvent(_ , _ , 113) => break,
+            tb::KeyEvent(md, key, ch ) => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
+           
+           tb::KeyEvent{md:_ , key:_ , ch:113} => break,
+                        tb::KeyEvent{md, key, ch } => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
+            _ => ()
+        }
+    }
+*/
+    tb::shutdown();
+  }
+}
+#[link(name = "termbox", vers = "0.1.0", author = "josemaria.alkala")];
+#[crate_type = "lib"];
+//pub mod tb {
+extern mod std;
+
+use core::libc::{c_uint,c_int};
+
+extern mod ffi {
+    // Declaramos la API del módulo externo
+    fn tb_init() -> c_int;
+    fn tb_shutdown();
+
+    fn tb_width() -> c_uint;
+    fn tb_height() -> c_uint;
+
+    fn tb_clear();
+    fn tb_present();
+
+    fn tb_set_cursor(cx: c_int, cy: c_int);
+
+    fn tb_change_cell(x: c_uint, y: c_uint, ch: u32, fg: u16, bg: u16);
+
+    fn tb_select_input_mode(mode: c_int) -> c_int;
+    fn tb_set_clear_attributes(fg: u16, bg: u16);
+
+    //fn tb_peek_event(ev: *mut tb_event, timeout: c_uint) -> c_int;
+    fn tb_peek_event(ev: *tb_event, timeout: c_uint) -> c_int;
+    fn tb_poll_event(ev: *tb_event) -> c_int;
+}
+
+///////// to_mut_unsafe_ptr(&mut ev)
+// en lugar de: fn peek_event
+// Definimos las elementos que se van a definir 
+
+struct tb_event {
+  t:u8, // type
+  m:u8, // mod
+  key:u16,
+  ch:u32,
+  w:i32,
+  h:i32
+}
+
+
+// Comodity functions ("Rusty" interface)
+// - no conviene meterno dentro de bloques unsafe, para que sea 
+// más explícito para quien use la librería
+pub enum Color {
+     Black   = 0x00,
+     Red     = 0x01,
+     Green   = 0x02,
+     Yellow  = 0x03,
+     Blue    = 0x04,
+     Magenta = 0x05,
+     Cyan    = 0x06,
+     White   = 0x07
+}
+
+pub enum Style {
+     Normal        = 0x00,
+     Bold          = 0x10,
+     Underline     = 0x20,
+     BoldUnderline = 0x30
+}
+
+pub unsafe fn init() -> int { 
+  ffi::tb_init() as int
+}
+
+pub unsafe fn shutdown() { 
+  ffi::tb_shutdown()
+}
+
+unsafe fn width() -> uint { 
+  ffi::tb_width() as uint
+}
+
+unsafe fn height() -> uint { 
+  ffi::tb_height() as uint
+}
+
+/**
+ * Clear buffer.
+ */
+unsafe fn clear() { 
+  ffi::tb_clear()
+}
+
+/**
+ * Write buffer to terminal.
+ */
+pub unsafe fn present() { 
+  ffi::tb_present()
+}
+
+unsafe fn set_cursor(cx: int, cy: int) { 
+  ffi::tb_set_cursor(cx as c_int, cy as c_int); 
+}
+
+
+// low-level wrapper
+unsafe fn change_cell(x: uint, y: uint, ch: u32, fg: Color, bg: Color) { 
+  ffi::tb_change_cell(x as c_uint, y as c_uint, ch, fg as u16, bg as u16); 
+}
+
+/**
+ * Print a string to the buffer.  Leftmost charater is at (x, y).
+ */
+pub unsafe fn print(x: uint, y: uint, sty: Style, fg: Color, bg: Color, s: ~str) {
+    let fg: u16 = (fg as u16) | (sty as u16);
+    let bg = bg as u16;
+    for s.each_chari |i, ch| {
+        ffi::tb_change_cell((x + i) as c_uint, y as c_uint, ch as u32, fg, bg);
+    }
+}
+
+/**
+ * Print a charater to the buffer.
+ */
+unsafe fn print_ch(x: uint, y: uint, sty: Style, fg: Color, bg: Color, ch: char) {
+    let fg: u16 = fg as u16 | sty as u16;
+    let bg: u16 = bg as u16;
+    ffi::tb_change_cell(x as c_uint, y as c_uint, ch as u32, fg, bg);
+}
+
+pub enum Event {
+    pub KeyEvent{md: u8, key: u16, ch: u32},  
+    pub ResizeEvent{w: i32, h: i32},
+    NoEvent,
+    NotManagedEvent
+}
+
+fn unpack_event(ev_type: c_int, ev: &tb_event) -> Event {
+    match ev_type {
+        0 => NoEvent,
+        1 => KeyEvent{md: ev.m, key: ev.key, ch: ev.ch},
+        2 => ResizeEvent{w: ev.w, h: ev.h},
+        _ => NotManagedEvent
+    }
+}
+
+/**
+ * Get an event if within timeout milliseconds, otherwise return no_event.
+ */
+unsafe fn peek_event(timeout: uint) -> Event {
+    let mut ev = tb_event{  t:0, m:0, key:0, ch:0,  w:0, h:0 };
+    let rc = ffi::tb_peek_event(ptr::addr_of(&ev), timeout as c_uint);
+      unpack_event(rc, &ev)
+}
+
+/**
+ * Blocking function to return next event.
+ */
+pub unsafe fn poll_event() -> Event {
+    let ev = tb_event{  t:0, m:0, key:0, ch:0,  w:0, h:0 };
+    let rc = ffi::tb_poll_event(ptr::addr_of(&ev));
+    unpack_event(rc, &ev)
+}
+
+
+/*
+// Convenience functions
+fn with_term(-f: fn~()) {
+    init();
+    let res = task::try(f);
+    shutdown();
+    if result::is_err(res) {
+        #error("with_term: An error occured.");
+    }
+}
+*/
+
+//}
+
+
+// =======================================================
+fn main() {
+  unsafe {
+    init();
+    print(1, 1, Bold, White, Black, ~"Hello, world!"); 
+    present(); 
+
+    loop {
+       let tmp = poll_event();
+       match tmp {
+            KeyEvent{ md:_ , key:_ , ch:113} => break,
+            KeyEvent{md, key, ch } => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
+            _ => ()
+        }
+    }
+    shutdown();
+  }
+}