|
| 1 | +extern crate libc; |
| 2 | + |
| 3 | +use array::Array; |
| 4 | +use defines::AfError; |
| 5 | +use defines::ColorMap; |
| 6 | +use self::libc::{c_int, c_uint, c_double}; |
| 7 | + |
| 8 | +type MutWndHandle = *mut self::libc::c_ulonglong; |
| 9 | +type WndHandle = self::libc::c_ulonglong; |
| 10 | +type AfArray = self::libc::c_longlong; |
| 11 | +type CellPtr = *const self::libc::c_void; |
| 12 | + |
| 13 | +#[allow(dead_code)] |
| 14 | +extern { |
| 15 | + fn af_create_window(out: MutWndHandle, w: c_int, h: c_int, title: *const u8) -> c_int; |
| 16 | + fn af_set_position(wnd: WndHandle, x: c_uint, y: c_uint) -> c_int; |
| 17 | + fn af_set_title(wnd: WndHandle, title: *const u8) -> c_int; |
| 18 | + fn af_draw_image(wnd: WndHandle, arr: AfArray, props: CellPtr) -> c_int; |
| 19 | + fn af_draw_plot(wnd: WndHandle, x: AfArray, y: AfArray, props: CellPtr) -> c_int; |
| 20 | + fn af_grid(wnd: WndHandle, rows: c_int, cols: c_int) -> c_int; |
| 21 | + fn af_show(wnd: WndHandle) -> c_int; |
| 22 | + fn af_is_window_closed(out: *mut c_int, wnd: WndHandle) -> c_int; |
| 23 | + fn af_destroy_window(wnd: WndHandle) -> c_int; |
| 24 | + |
| 25 | + fn af_draw_hist(wnd: WndHandle, x: AfArray, |
| 26 | + minval: c_double, maxval: c_double, props: CellPtr) -> c_int; |
| 27 | +} |
| 28 | + |
| 29 | +#[repr(C)] |
| 30 | +pub struct Cell { |
| 31 | + pub row: i32, |
| 32 | + pub col: i32, |
| 33 | + pub title: String, |
| 34 | + pub cmap: ColorMap, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Clone)] |
| 38 | +pub struct Window { |
| 39 | + handle: u64, |
| 40 | + row: i32, |
| 41 | + col: i32, |
| 42 | + cmap: ColorMap, |
| 43 | +} |
| 44 | + |
| 45 | +impl From<u64> for Window { |
| 46 | + fn from(t: u64) -> Window { |
| 47 | + Window {handle: t, row: -1, col: -1, cmap: ColorMap::DEFAULT} |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl Drop for Window { |
| 52 | + fn drop(&mut self) { |
| 53 | + unsafe { |
| 54 | + let err_val = af_destroy_window(self.handle); |
| 55 | + match err_val { |
| 56 | + 0 => (), |
| 57 | + _ => panic!("Window object destruction failed with error code: {}", err_val), |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Window { |
| 64 | + #[allow(unused_mut)] |
| 65 | + pub fn new(width: i32, height: i32, title: String) -> Result<Window, AfError> { |
| 66 | + unsafe { |
| 67 | + let mut temp: u64 = 0; |
| 68 | + let err_val = af_create_window(&mut temp as MutWndHandle, |
| 69 | + width as c_int, height as c_int, |
| 70 | + title.as_bytes().as_ptr() as *const u8); |
| 71 | + match err_val { |
| 72 | + 0 => Ok(Window::from(temp)), |
| 73 | + _ => Err(AfError::from(err_val)), |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn set_position(&self, x: u32, y: u32) -> Result<(), AfError> { |
| 79 | + unsafe { |
| 80 | + let err_val = af_set_position(self.handle as WndHandle, x as c_uint, y as c_uint); |
| 81 | + match err_val { |
| 82 | + 0 => Ok(()), |
| 83 | + _ => Err(AfError::from(err_val)), |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn set_title(&self, title: String) -> Result<(), AfError> { |
| 89 | + unsafe { |
| 90 | + let err_val = af_set_title(self.handle as WndHandle, |
| 91 | + title.as_bytes().as_ptr() as *const u8); |
| 92 | + match err_val { |
| 93 | + 0 => Ok(()), |
| 94 | + _ => Err(AfError::from(err_val)), |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + pub fn set_colormap(&mut self, cmap: ColorMap) { |
| 100 | + self.cmap = cmap; |
| 101 | + } |
| 102 | + |
| 103 | + pub fn is_closed(&self) -> Result<bool, AfError> { |
| 104 | + unsafe { |
| 105 | + let mut temp: i32 = 1; |
| 106 | + let err_val = af_is_window_closed(&mut temp as *mut c_int, self.handle as WndHandle); |
| 107 | + match err_val { |
| 108 | + 0 => Ok(temp > 0), |
| 109 | + _ => Err(AfError::from(err_val)), |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn grid(&self, rows: i32, cols: i32) -> Result<(), AfError> { |
| 115 | + unsafe { |
| 116 | + let err_val = af_grid(self.handle as WndHandle, rows as c_int, cols as c_int); |
| 117 | + match err_val { |
| 118 | + 0 => Ok(()), |
| 119 | + _ => Err(AfError::from(err_val)), |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + pub fn show(&mut self) -> Result<(), AfError> { |
| 125 | + unsafe { |
| 126 | + let err_val = af_show(self.handle as WndHandle); |
| 127 | + if err_val != 0 { |
| 128 | + return Err(AfError::from(err_val)); |
| 129 | + } |
| 130 | + self.row = -1; |
| 131 | + self.col = -1; |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + pub fn set_view(&mut self, r: i32, c: i32) { |
| 137 | + self.row = r; |
| 138 | + self.col = c; |
| 139 | + } |
| 140 | + |
| 141 | + pub fn draw_image(&self, input: &Array, title: Option<String>) { |
| 142 | + let tstr = match title { |
| 143 | + Some(s) => s, |
| 144 | + None => format!("Cell({},{}))", self.col, self.row) |
| 145 | + }; |
| 146 | + let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap}; |
| 147 | + unsafe { |
| 148 | + let err_val = af_draw_image(self.handle as WndHandle, input.get() as AfArray, |
| 149 | + cprops as *const Cell as CellPtr); |
| 150 | + match err_val { |
| 151 | + 0 => (), |
| 152 | + _ => panic!("Rendering the image failed: {}", err_val), |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + pub fn draw_plot(&self, x: &Array, y: &Array, title: Option<String>) { |
| 158 | + let tstr = match title { |
| 159 | + Some(s) => s, |
| 160 | + None => format!("Cell({},{}))", self.col, self.row) |
| 161 | + }; |
| 162 | + let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap}; |
| 163 | + unsafe { |
| 164 | + let err_val = af_draw_plot(self.handle as WndHandle, |
| 165 | + x.get() as AfArray, |
| 166 | + y.get() as AfArray, |
| 167 | + cprops as *const Cell as CellPtr); |
| 168 | + match err_val { |
| 169 | + 0 => (), |
| 170 | + _ => panic!("Rendering the image failed: {}", err_val), |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + pub fn draw_hist(&self, hst: &Array, minval: f64, maxval: f64, title: Option<String>) { |
| 176 | + let tstr = match title { |
| 177 | + Some(s) => s, |
| 178 | + None => format!("Cell({},{}))", self.col, self.row) |
| 179 | + }; |
| 180 | + let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap}; |
| 181 | + unsafe { |
| 182 | + let err_val = af_draw_hist(self.handle as WndHandle, hst.get() as AfArray, |
| 183 | + minval as c_double, maxval as c_double, |
| 184 | + cprops as *const Cell as CellPtr); |
| 185 | + match err_val { |
| 186 | + 0 => (), |
| 187 | + _ => panic!("Rendering the image failed: {}", err_val), |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments