morpheus_bootloader/tui/
renderer.rs

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::SimpleTextOutputProtocol;
use alloc::vec;
use alloc::vec::Vec;

// EFI text colors
pub const EFI_BLACK: usize = 0x00;
pub const EFI_BLUE: usize = 0x01; // Very dark, good for dim rain
pub const EFI_DARKGREEN: usize = 0x02; // Dim green for background rain
pub const EFI_GREEN: usize = 0x02;
pub const EFI_CYAN: usize = 0x03; // Alternative dim color
pub const EFI_RED: usize = 0x04; // For errors
pub const EFI_MAGENTA: usize = 0x05;
pub const EFI_BROWN: usize = 0x06;
pub const EFI_LIGHTGRAY: usize = 0x07;
pub const EFI_DARKGRAY: usize = 0x08;
pub const EFI_LIGHTBLUE: usize = 0x09;
pub const EFI_LIGHTGREEN: usize = 0x0A;
pub const EFI_LIGHTCYAN: usize = 0x0B;
pub const EFI_LIGHTRED: usize = 0x0C;
pub const EFI_LIGHTMAGENTA: usize = 0x0D;
pub const EFI_YELLOW: usize = 0x0E;
pub const EFI_WHITE: usize = 0x0F;

fn str_to_ucs2(s: &str, buf: &mut [u16]) {
    let mut i = 0;
    for ch in s.chars() {
        if i >= buf.len() - 1 {
            break;
        }
        buf[i] = ch as u16;
        i += 1;
    }
    buf[i] = 0;
}

pub struct Screen {
    con_out: *mut SimpleTextOutputProtocol,
    width: usize,
    height: usize,
    pub mask: Vec<Vec<bool>>,
}

impl Screen {
    pub fn new(con_out: *mut SimpleTextOutputProtocol) -> Self {
        let (width, height) = Self::get_screen_size(con_out);

        // Create dynamic mask based on actual screen size
        let mut mask = Vec::new();
        for _ in 0..height {
            let row = vec![false; width];
            mask.push(row);
        }

        Self {
            con_out,
            width,
            height,
            mask,
        }
    }

    fn get_screen_size(con_out: *mut SimpleTextOutputProtocol) -> (usize, usize) {
        unsafe {
            let protocol = &mut *con_out;

            if protocol.mode.is_null() {
                return (80, 25); // fallback
            }

            let mode_info = &*protocol.mode;
            let current_mode = mode_info.mode;

            if current_mode < 0 {
                return (80, 25); // fallback
            }

            // Query current mode dimensions
            let mut cols: usize = 80;
            let mut rows: usize = 25;

            let status = (protocol.query_mode)(
                protocol,
                current_mode as usize,
                &mut cols as *mut usize,
                &mut rows as *mut usize,
            );

            // Status 0 = success
            if status == 0 && cols > 0 && rows > 0 {
                (cols, rows)
            } else {
                (80, 25) // fallback
            }
        }
    }

    pub fn width(&self) -> usize {
        self.width
    }

    pub fn height(&self) -> usize {
        self.height
    }

    // Calculate centered X position for content of given width
    pub fn center_x(&self, content_width: usize) -> usize {
        if self.width > content_width {
            (self.width - content_width) / 2
        } else {
            0
        }
    }

    // Calculate centered Y position for content of given height
    pub fn center_y(&self, content_height: usize) -> usize {
        if self.height > content_height {
            (self.height - content_height) / 2
        } else {
            0
        }
    }

    // Get centered coordinates for content box
    pub fn center_xy(&self, content_width: usize, content_height: usize) -> (usize, usize) {
        (self.center_x(content_width), self.center_y(content_height))
    }

    pub fn clear(&mut self) {
        unsafe {
            let con_out = &mut *self.con_out;
            (con_out.clear_screen)(con_out);
        }
        // Reset mask when clearing
        for row in &mut self.mask {
            for cell in row {
                *cell = false;
            }
        }
    }

    pub fn set_color(&mut self, fg: usize, bg: usize) {
        let attr = fg | (bg << 4);
        unsafe {
            let con_out = &mut *self.con_out;
            (con_out.set_attribute)(con_out, attr);
        }
    }

    pub fn set_cursor(&mut self, x: usize, y: usize) {
        unsafe {
            let con_out = &mut *self.con_out;
            (con_out.set_cursor_position)(con_out, x, y);
        }
    }

    pub fn put_char(&mut self, ch: char) {
        let buf = [ch as u16, 0];
        unsafe {
            let con_out = &mut *self.con_out;
            (con_out.output_string)(con_out, buf.as_ptr());
        }
    }

    pub fn put_str(&mut self, s: &str) {
        let mut buffer = [0u16; 256];
        str_to_ucs2(s, &mut buffer);
        unsafe {
            let con_out = &mut *self.con_out;
            (con_out.output_string)(con_out, buffer.as_ptr());
        }
    }

    pub fn put_char_at(&mut self, x: usize, y: usize, ch: char, fg: usize, bg: usize) {
        self.set_cursor(x, y);
        self.set_color(fg, bg);
        self.put_char(ch);
    }

    pub fn put_str_at(&mut self, x: usize, y: usize, s: &str, fg: usize, bg: usize) {
        self.set_cursor(x, y);
        self.set_color(fg, bg);
        self.put_str(s);

        // Update mask for written content
        if y < self.height {
            for (i, _ch) in s.chars().enumerate() {
                let pos = x + i;
                if pos < self.width {
                    self.mask[y][pos] = true;
                }
            }
        }
    }

    // Draw multi-line text block with proper spacing
    pub fn draw_block(&mut self, lines: &[&str]) {
        let mut buffer = [0u16; 512];

        for line in lines {
            str_to_ucs2(line, &mut buffer);
            unsafe {
                let con_out = &mut *self.con_out;
                (con_out.output_string)(con_out, buffer.as_ptr());
            }

            // Add newline
            str_to_ucs2("\r\n", &mut buffer);
            unsafe {
                let con_out = &mut *self.con_out;
                (con_out.output_string)(con_out, buffer.as_ptr());
            }
        }
    }

    pub fn draw_block_colored(&mut self, lines: &[&str], fg: usize, bg: usize) {
        self.set_color(fg, bg);
        self.draw_block(lines);
        self.set_color(EFI_WHITE, EFI_BLACK);
    }

    // Draw centered text block
    pub fn draw_centered_block(
        &mut self,
        lines: &[&str],
        width: usize,
        start_y: usize,
        fg: usize,
        bg: usize,
    ) {
        let x_offset = if self.width > width {
            (self.width - width) / 2
        } else {
            0
        };

        for (i, line) in lines.iter().enumerate() {
            let y = start_y + i;
            if y < self.height {
                self.put_str_at(x_offset, y, line, fg, bg);
            }
        }
    }

    // Alias methods for compatibility with different naming conventions

    /// Alias for set_color
    #[inline]
    pub fn set_colors(&mut self, fg: usize, bg: usize) {
        self.set_color(fg, bg);
    }

    /// Alias for put_str
    #[inline]
    pub fn print(&mut self, s: &str) {
        self.put_str(s);
    }

    /// Alias for put_char
    #[inline]
    pub fn print_char(&mut self, ch: char) {
        self.put_char(ch);
    }
}