Skip to main content

strat9_kernel/arch/x86_64/vga/
cursor.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Cursor manager for the framebuffer terminal.
24//!
25//! Owns the mouse-cursor and text-cursor state and provides methods to
26//! save/restore/draw the text cursor directly to the hardware framebuffer via
27//! [`CanvasBuffer`]. The mouse cursor is a present-time overlay in
28//! `writer.rs`; only its state lives here.
29
30use crate::framebuffer::CanvasBuffer;
31
32// ---------------------------------------------------------------------------
33// Constants
34// ---------------------------------------------------------------------------
35
36/// Mouse-cursor sprite width (pixels).
37pub(crate) const CURSOR_W: usize = 12;
38/// Mouse-cursor sprite height (pixels).
39pub(crate) const CURSOR_H: usize = 16;
40/// Maximum text-cursor width or height (pixels).
41pub(crate) const TEXT_CURSOR_MAX_DIM: usize = 32;
42
43/// Mouse cursor sprite: 0=transparent, 1=black outline, 2=white fill.
44#[rustfmt::skip]
45pub(crate) const CURSOR_PIXELS: [u8; CURSOR_W * CURSOR_H] = [
46    1,0,0,0,0,0,0,0,0,0,0,0,
47    1,1,0,0,0,0,0,0,0,0,0,0,
48    1,2,1,0,0,0,0,0,0,0,0,0,
49    1,2,2,1,0,0,0,0,0,0,0,0,
50    1,2,2,2,1,0,0,0,0,0,0,0,
51    1,2,2,2,2,1,0,0,0,0,0,0,
52    1,2,2,2,2,2,1,0,0,0,0,0,
53    1,2,2,2,2,2,2,1,0,0,0,0,
54    1,2,2,2,2,2,2,2,1,0,0,0,
55    1,2,2,2,2,2,1,1,0,0,0,0,
56    1,2,2,2,1,0,0,0,0,0,0,0,
57    1,2,1,1,2,2,1,0,0,0,0,0,
58    1,1,0,0,1,2,2,1,0,0,0,0,
59    0,0,0,0,0,1,2,1,0,0,0,0,
60    0,0,0,0,0,0,1,1,0,0,0,0,
61    0,0,0,0,0,0,0,0,0,0,0,0,
62];
63
64// ---------------------------------------------------------------------------
65// CursorManager
66// ---------------------------------------------------------------------------
67
68/// Holds all cursor-related state for the framebuffer terminal.
69///
70/// **Mouse cursor** – state only; the sprite is an overlay drawn at present.
71/// **Text cursor** – a blinking underline / block that follows the text input
72/// position.
73pub(crate) struct CursorManager {
74    // -- Mouse cursor -------------------------------------------------------
75    pub mc_x: i32,
76    pub mc_y: i32,
77    pub mc_visible: bool,
78
79    // -- Text cursor --------------------------------------------------------
80    pub tc_col: usize,
81    pub tc_row: usize,
82    pub tc_w: usize,
83    pub tc_h: usize,
84    pub tc_visible: bool,
85    pub tc_dirty: bool,
86    pub tc_color: u32,
87    pub tc_save: [u32; TEXT_CURSOR_MAX_DIM * TEXT_CURSOR_MAX_DIM],
88}
89
90impl CursorManager {
91    /// Create a new cursor manager with all cursors hidden.
92    pub const fn new() -> Self {
93        Self {
94            mc_x: 0,
95            mc_y: 0,
96            mc_visible: false,
97            tc_col: 0,
98            tc_row: 0,
99            tc_w: 0,
100            tc_h: 0,
101            tc_visible: false,
102            tc_dirty: false,
103            tc_color: 0,
104            tc_save: [0u32; TEXT_CURSOR_MAX_DIM * TEXT_CURSOR_MAX_DIM],
105        }
106    }
107
108    // ========================================================================
109    // Text cursor
110    // ========================================================================
111
112    /// Calculate the pixel rectangle for the text cursor at `(col, row)`.
113    #[allow(dead_code)] // Retained hardware-cursor geometry helper.
114    pub fn text_cursor_rect(
115        &self,
116        col: usize,
117        row: usize,
118        glyph_w: usize,
119        glyph_h: usize,
120    ) -> (usize, usize, usize, usize) {
121        let tw = glyph_w.min(TEXT_CURSOR_MAX_DIM);
122        let th = glyph_h.min(TEXT_CURSOR_MAX_DIM);
123        (col * glyph_w, row * glyph_h, tw, th)
124    }
125
126    /// Save the pixels under the text cursor.
127    #[allow(dead_code)] // Retained hardware-cursor save helper.
128    pub fn text_cursor_save_hw(
129        &mut self,
130        col: usize,
131        row: usize,
132        glyph_w: usize,
133        glyph_h: usize,
134        canvas: &CanvasBuffer,
135    ) {
136        let (tx, ty, tw, th) = self.text_cursor_rect(col, row, glyph_w, glyph_h);
137        for dy in 0..th {
138            for dx in 0..tw {
139                let px = tx + dx;
140                let py = ty + dy;
141                if px < canvas.width && py < canvas.height {
142                    self.tc_save[dy * tw + dx] = canvas.read_hw_pixel(px, py);
143                }
144            }
145        }
146    }
147
148    /// Draw the text cursor (inverted-colour block) on the hardware framebuffer.
149    /// Resets `tc_dirty = false` after drawing.
150    #[allow(dead_code)] // Retained hardware-cursor draw helper.
151    pub fn text_cursor_draw_hw(
152        &mut self,
153        col: usize,
154        row: usize,
155        glyph_w: usize,
156        glyph_h: usize,
157        color: u32,
158        canvas: &mut CanvasBuffer,
159    ) {
160        self.tc_dirty = false;
161        let (tx, ty, tw, th) = self.text_cursor_rect(col, row, glyph_w, glyph_h);
162        for dy in 0..th {
163            for dx in 0..tw {
164                let px = tx + dx;
165                let py = ty + dy;
166                if px < canvas.width && py < canvas.height {
167                    canvas.write_hw_pixel(px, py, color);
168                }
169            }
170        }
171    }
172
173    /// Erase the text cursor by restoring the saved pixels.
174    /// Sets `tc_dirty = true` so the next `present()` flushes.
175    #[allow(dead_code)] // Retained hardware-cursor erase helper.
176    pub fn text_cursor_erase_hw(
177        &mut self,
178        col: usize,
179        row: usize,
180        glyph_w: usize,
181        glyph_h: usize,
182        canvas: &mut CanvasBuffer,
183    ) {
184        let (tx, ty, tw, th) = self.text_cursor_rect(col, row, glyph_w, glyph_h);
185        for dy in 0..th {
186            for dx in 0..tw {
187                let px = tx + dx;
188                let py = ty + dy;
189                if px < canvas.width && py < canvas.height {
190                    canvas.write_hw_pixel(px, py, self.tc_save[dy * tw + dx]);
191                }
192            }
193        }
194    }
195
196    /// Draw the text cursor as a solid-colour overlay at the given position.
197    #[allow(dead_code)] // Retained hardware-cursor overlay helper.
198    pub fn draw_text_cursor_overlay(
199        &mut self,
200        col: usize,
201        row: usize,
202        glyph_w: usize,
203        glyph_h: usize,
204        color: u32,
205        canvas: &mut CanvasBuffer,
206    ) {
207        self.text_cursor_draw_hw(col, row, glyph_w, glyph_h, color, canvas);
208    }
209
210    /// Hide the text cursor.
211    #[allow(dead_code)] // Retained hardware-cursor hide helper.
212    pub fn hide_text_cursor(
213        &mut self,
214        col: usize,
215        row: usize,
216        glyph_w: usize,
217        glyph_h: usize,
218        canvas: &mut CanvasBuffer,
219    ) {
220        if self.tc_visible {
221            self.text_cursor_erase_hw(col, row, glyph_w, glyph_h, canvas);
222            self.tc_visible = false;
223        }
224    }
225}