|
1 | 1 | //! Screen-related commands for Neotron OS |
2 | 2 |
|
3 | | -use crate::{osprint, Ctx}; |
| 3 | +use crate::{ |
| 4 | + bios::{ |
| 5 | + video::{Format, Mode}, |
| 6 | + ApiResult, |
| 7 | + }, |
| 8 | + osprint, osprintln, Ctx, |
| 9 | +}; |
4 | 10 |
|
5 | 11 | pub static CLS_ITEM: menu::Item<Ctx> = menu::Item { |
6 | 12 | item_type: menu::ItemType::Callback { |
7 | | - function: cls, |
| 13 | + function: cls_cmd, |
8 | 14 | parameters: &[], |
9 | 15 | }, |
10 | 16 | command: "cls", |
11 | 17 | help: Some("Clear the screen"), |
12 | 18 | }; |
13 | 19 |
|
| 20 | +pub static MODE_ITEM: menu::Item<Ctx> = menu::Item { |
| 21 | + item_type: menu::ItemType::Callback { |
| 22 | + function: mode_cmd, |
| 23 | + parameters: &[menu::Parameter::Optional { |
| 24 | + parameter_name: "new_mode", |
| 25 | + help: Some("The new text mode to change to"), |
| 26 | + }], |
| 27 | + }, |
| 28 | + command: "mode", |
| 29 | + help: Some("List possible video modes"), |
| 30 | +}; |
| 31 | + |
14 | 32 | /// Called when the "cls" command is executed. |
15 | | -fn cls(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) { |
| 33 | +fn cls_cmd(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) { |
16 | 34 | // Reset SGR, go home, clear screen, |
17 | | - let _ = osprint!("\u{001b}[0m\u{001b}[1;1H\u{001b}[2J"); |
| 35 | + osprint!("\u{001b}[0m\u{001b}[1;1H\u{001b}[2J"); |
18 | 36 | } |
| 37 | + |
| 38 | +/// Called when the "mode" command is executed |
| 39 | +fn mode_cmd(_menu: &menu::Menu<Ctx>, item: &menu::Item<Ctx>, args: &[&str], _ctx: &mut Ctx) { |
| 40 | + if let Some(new_mode) = menu::argument_finder(item, args, "new_mode").unwrap() { |
| 41 | + let Ok(mode_num) = new_mode.parse::<u8>() else { |
| 42 | + osprintln!("Invalid integer {:?}", new_mode); |
| 43 | + return; |
| 44 | + }; |
| 45 | + let Some(mode) = Mode::try_from_u8(mode_num) else { |
| 46 | + osprintln!("Invalid mode {:?}", new_mode); |
| 47 | + return; |
| 48 | + }; |
| 49 | + let has_vga = { |
| 50 | + let mut guard = crate::VGA_CONSOLE.lock(); |
| 51 | + guard.as_mut().is_some() |
| 52 | + }; |
| 53 | + if !has_vga { |
| 54 | + osprintln!("No VGA console."); |
| 55 | + return; |
| 56 | + } |
| 57 | + let api = crate::API.get(); |
| 58 | + match mode.format() { |
| 59 | + Format::Text8x16 => {} |
| 60 | + Format::Text8x8 => {} |
| 61 | + _ => { |
| 62 | + osprintln!("Not a text mode?"); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + match (api.video_set_mode)(mode) { |
| 67 | + ApiResult::Ok(_) => { |
| 68 | + let mut guard = crate::VGA_CONSOLE.lock(); |
| 69 | + if let Some(console) = guard.as_mut() { |
| 70 | + console.change_mode(mode); |
| 71 | + } |
| 72 | + osprintln!("Now in mode {}", mode.as_u8()); |
| 73 | + } |
| 74 | + ApiResult::Err(e) => { |
| 75 | + osprintln!("Failed to change mode: {:?}", e); |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + print_modes(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Print out all supported video modes |
| 84 | +fn print_modes() { |
| 85 | + let api = crate::API.get(); |
| 86 | + let current_mode = (api.video_get_mode)(); |
| 87 | + let mut any_mode = false; |
| 88 | + for mode_no in 0..255 { |
| 89 | + // Note (unsafe): we'll test if it's right before we try and use it |
| 90 | + let Some(m) = Mode::try_from_u8(mode_no) else { |
| 91 | + continue; |
| 92 | + }; |
| 93 | + let is_supported = (api.video_is_valid_mode)(m); |
| 94 | + if is_supported { |
| 95 | + any_mode = true; |
| 96 | + let is_current = if current_mode == m { "*" } else { " " }; |
| 97 | + let text_rows = m.text_height(); |
| 98 | + let text_cols = m.text_width(); |
| 99 | + let f = m.format(); |
| 100 | + let width = m.horizontal_pixels(); |
| 101 | + let height = m.vertical_lines(); |
| 102 | + let hz = m.frame_rate_hz(); |
| 103 | + if let (Some(text_rows), Some(text_cols)) = (text_rows, text_cols) { |
| 104 | + // It's a text mode |
| 105 | + osprintln!("{mode_no:3}{is_current}: {width} x {height} @ {hz} Hz {f} ({text_cols} x {text_rows})"); |
| 106 | + } else { |
| 107 | + // It's a framebuffer mode |
| 108 | + let f = m.format(); |
| 109 | + osprintln!("{mode_no:3}{is_current}: {width} x {height} @ {hz} Hz {f}"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + if !any_mode { |
| 114 | + osprintln!("No valid modes found"); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// End of file |
0 commit comments