|
| 1 | +use {Status, Result, proto}; |
| 2 | + |
| 3 | +/// Interface for text-based output devices. |
| 4 | +#[repr(C)] |
| 5 | +pub struct Output { |
| 6 | + reset: extern fn(this: &Output, extended: bool) -> Status, |
| 7 | + output_string: extern fn(this: &Output, string: *const u16) -> Status, |
| 8 | + test_string: extern fn(this: &Output, string: *const u16) -> Status, |
| 9 | + query_mode: extern fn(this: &Output, mode: i32, columns: &mut usize, rows: &mut usize) -> Status, |
| 10 | + set_mode: extern fn(this: &mut Output, mode: i32) -> Status, |
| 11 | + _pad: [usize; 4], |
| 12 | + data: &'static OutputData, |
| 13 | +} |
| 14 | + |
| 15 | +impl Output { |
| 16 | + /// Resets the text output device hardware. |
| 17 | + #[inline] |
| 18 | + pub fn reset(&mut self, extended: bool) -> Result<()> { |
| 19 | + (self.reset)(self, extended).into() |
| 20 | + } |
| 21 | + |
| 22 | + /// Writes a string to the output device. |
| 23 | + #[inline] |
| 24 | + pub fn output_string(&mut self, string: *const u16) -> Result<()> { |
| 25 | + (self.output_string)(self, string).into() |
| 26 | + } |
| 27 | + |
| 28 | + /// Checks if a string contains only supported characters. |
| 29 | + /// True indicates success. |
| 30 | + /// |
| 31 | + /// UEFI applications are encouraged to try to print a string even if it contains |
| 32 | + /// some unsupported characters. |
| 33 | + #[inline] |
| 34 | + pub fn test_string(&mut self, string: *const u16) -> bool { |
| 35 | + match (self.test_string)(self, string) { |
| 36 | + Status::Success => true, |
| 37 | + _ => false, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns an iterator of all supported text modes. |
| 42 | + // TODO: fix the ugly lifetime parameter. |
| 43 | + #[inline] |
| 44 | + pub fn modes<'a>(&'a mut self) -> impl Iterator<Item = OutputMode> + 'a { |
| 45 | + let max = self.data.max_mode; |
| 46 | + OutputModeIter { |
| 47 | + output: self, |
| 48 | + current: 0, |
| 49 | + max, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns the width (column count) and height (row count) of this mode. |
| 54 | + fn query_mode(&self, index: i32) -> Result<(usize, usize)> { |
| 55 | + let (mut columns, mut rows) = (0, 0); |
| 56 | + (self.query_mode)(self, index, &mut columns, &mut rows)?; |
| 57 | + Ok((columns, rows)) |
| 58 | + } |
| 59 | + |
| 60 | + /// Sets a mode as current. |
| 61 | + #[inline] |
| 62 | + pub fn set_mode(&mut self, mode: OutputMode) -> Result<()> { |
| 63 | + (self.set_mode)(self, mode.index).into() |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns the the current text mode. |
| 67 | + #[inline] |
| 68 | + pub fn current_mode(&self) -> Result<OutputMode> { |
| 69 | + let index = self.data.mode; |
| 70 | + let dims = self.query_mode(index)?; |
| 71 | + Ok(OutputMode { index, dims } ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// The text mode (resolution) of the output device. |
| 76 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] |
| 77 | +pub struct OutputMode { |
| 78 | + index: i32, |
| 79 | + dims: (usize, usize), |
| 80 | +} |
| 81 | + |
| 82 | +impl OutputMode { |
| 83 | + /// Returns the index of this mode. |
| 84 | + #[inline] |
| 85 | + pub fn index(&self) -> i32 { |
| 86 | + self.index |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns the width in columns. |
| 90 | + #[inline] |
| 91 | + pub fn columns(&self) -> usize { |
| 92 | + self.dims.0 |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns the height in rows. |
| 96 | + #[inline] |
| 97 | + pub fn rows(&self) -> usize { |
| 98 | + self.dims.1 |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// An iterator of the text modes (possibly) supported by a device. |
| 103 | +struct OutputModeIter<'a> { |
| 104 | + output: &'a mut Output, |
| 105 | + current: i32, |
| 106 | + max: i32, |
| 107 | +} |
| 108 | + |
| 109 | +impl<'a> Iterator for OutputModeIter<'a> { |
| 110 | + type Item = OutputMode; |
| 111 | + |
| 112 | + fn next(&mut self) -> Option<Self::Item> { |
| 113 | + let index = self.current; |
| 114 | + if index < self.max { |
| 115 | + self.current += 1; |
| 116 | + |
| 117 | + if let Ok(dims) = self.output.query_mode(index) { |
| 118 | + Some(OutputMode { index, dims }) |
| 119 | + } else { |
| 120 | + self.next() |
| 121 | + } |
| 122 | + } else { |
| 123 | + None |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Additional data of the output device. |
| 129 | +#[derive(Debug)] |
| 130 | +#[repr(C)] |
| 131 | +struct OutputData { |
| 132 | + /// The number of modes supported by the device. |
| 133 | + max_mode: i32, |
| 134 | + /// The current output mode. |
| 135 | + mode: i32, |
| 136 | + /// The current character output attribute. |
| 137 | + attribute: i32, |
| 138 | + /// The cursor’s column. |
| 139 | + cursor_column: i32, |
| 140 | + /// The cursor’s row. |
| 141 | + cursor_row: i32, |
| 142 | + /// Whether the cursor is currently visible or not. |
| 143 | + cursor_visible: bool, |
| 144 | +} |
| 145 | + |
| 146 | +proto::impl_proto! { |
| 147 | + protocol Output { |
| 148 | + GUID = 0x387477c2, 0x69c7, 0x11d2, [0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b]; |
| 149 | + } |
| 150 | +} |
0 commit comments