strat9_kernel/arch/x86_64/
vgabuf.rs1#[path = "vga/log_queue.rs"]
27mod log_queue;
28use core::sync::atomic::{AtomicBool, Ordering};
29use log_queue::{Line, LogQueue};
30
31static CONSOLE_TASK_ACTIVE: AtomicBool = AtomicBool::new(false);
32
33pub fn console_task_active() -> bool {
34 CONSOLE_TASK_ACTIVE.load(Ordering::Acquire)
35}
36
37pub const VGABUF_CAPACITY: usize = log_queue::CAPACITY;
38pub const VGABUF_LINE_LEN: usize = log_queue::LINE_LEN;
39static QUEUE: LogQueue = LogQueue::new();
40const FLUSH_BATCH: usize = 30;
41
42pub fn vgabuf_write(line: &[u8]) {
43 QUEUE.push(line);
44}
45pub fn vgabuf_total_written() -> usize {
46 QUEUE.written()
47}
48pub fn vgabuf_total_dropped() -> usize {
49 QUEUE.dropped()
50}
51
52fn write_line(writer: &mut impl core::fmt::Write, line: &Line) {
53 if let Ok(s) = core::str::from_utf8(line.as_bytes()) {
54 let _ = writer.write_str(s);
55 if !s.ends_with('\n') {
56 let _ = writer.write_str("\n");
57 }
58 } else {
59 let _ = writer.write_str("<non-utf8>\n");
60 }
61}
62
63pub fn vgabuf_flush_to_framebuffer() {
66 if !crate::arch::x86_64::vga::is_available() || !crate::vasistas_state::kernel_fb_allowed() {
67 vgabuf_drain_discard();
68 return;
69 }
70 let Some(mut writer) = crate::arch::x86_64::vga::VGA_WRITER.try_lock() else {
71 return;
72 };
73 let mut flushed = 0;
74 for _ in 0..FLUSH_BATCH {
75 let Some(line) = QUEUE.pop() else {
76 break;
77 };
78 write_line(&mut *writer, &line);
79 flushed += 1;
80 }
81 if flushed > 0 {
82 writer.flush_display();
83 } else {
84 writer.present_if_due(false);
85 }
86}
87
88fn vgabuf_drain_discard() {
89 for _ in 0..VGABUF_CAPACITY {
90 if QUEUE.pop().is_none() {
91 break;
92 }
93 }
94}
95
96pub fn vgabuf_flush_all() {
97 if !crate::arch::x86_64::vga::is_available() || !crate::vasistas_state::kernel_fb_allowed() {
98 vgabuf_drain_discard();
99 return;
100 }
101 let Some(mut writer) = crate::arch::x86_64::vga::VGA_WRITER.try_lock() else {
102 vgabuf_flush_all_direct();
103 return;
104 };
105 for _ in 0..VGABUF_CAPACITY {
106 let Some(line) = QUEUE.pop() else {
107 break;
108 };
109 write_line(&mut *writer, &line);
110 }
111 writer.present_if_due(true);
112}
113
114fn vgabuf_flush_all_direct() {
115 if !crate::vasistas_state::kernel_fb_allowed() {
116 vgabuf_drain_discard();
117 return;
118 }
119 const MAX_LINES: usize = 16;
122 let mut owned = [Line::EMPTY; MAX_LINES];
123 let mut count = 0;
124 for slot in &mut owned {
125 let Some(line) = QUEUE.pop() else {
126 break;
127 };
128 *slot = line;
129 count += 1;
130 }
131 let mut lines = [""; MAX_LINES];
132 for i in 0..count {
133 lines[i] = core::str::from_utf8(owned[i].as_bytes()).unwrap_or("<non-utf8>");
134 }
135 let mut lines = [""; MAX_LINES];
136 for i in 0..count {
137 lines[i] = core::str::from_utf8(owned[i].as_bytes()).unwrap_or("<non-utf8>");
138 }
139}
140
141pub extern "C" fn console_task_main() -> ! {
146 CONSOLE_TASK_ACTIVE.store(true, Ordering::Release);
147 let mut input_state = crate::shell::vga_input::InputState::new();
148 let mut last_tick = u64::MAX;
149 loop {
150 if !crate::vasistas_state::kernel_fb_allowed() {
153 crate::process::sleep_current_task_ticks(1);
154 continue;
155 }
156 let tick = crate::process::scheduler::ticks();
157 if tick != last_tick {
158 last_tick = tick;
159 console_render_tick(&mut input_state);
160 }
161 crate::process::sleep_current_task_ticks(1);
164 }
165}
166
167fn console_render_tick(input_state: &mut crate::shell::vga_input::InputState) {
170 if !crate::arch::x86_64::vga::is_available() || !crate::vasistas_state::kernel_fb_allowed() {
171 vgabuf_drain_discard();
172 crate::shell::vga_input::discard();
173 return;
174 }
175 let Some(mut writer) = crate::arch::x86_64::vga::VGA_WRITER.try_lock() else {
176 return;
177 };
178
179 let mut applied = crate::shell::vga_input::take_pending();
180 applied |= crate::shell::vga_input::apply_input(&mut *writer, input_state);
183 if !crate::debug_cfg::is_quiet() {
184 for _ in 0..FLUSH_BATCH {
185 let Some(line) = QUEUE.pop() else {
186 break;
187 };
188 write_line(&mut *writer, &line);
189 applied = true;
190 }
191 }
192
193 applied |= writer.tick_scroll_anim();
195
196 if applied {
197 writer.flush_display();
198 } else {
199 writer.present_if_due(false);
200 }
201}