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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Shells
//!
//! This module provides daemons that serve a forth shell

use core::time::Duration;

use crate::{
    forth::Params,
    services::{
        emb_display::EmbDisplayClient,
        serial_mux::{PortHandle, WellKnown},
    },
    tracing, Kernel,
};
use embedded_graphics::{
    mono_font::{MonoFont, MonoTextStyle},
    pixelcolor::Gray8,
    prelude::{GrayColor, Point},
    primitives::{Line, Primitive, PrimitiveStyle},
    text::Text,
    Drawable,
};

use futures::FutureExt;
use input_mgr::RingLine;
use profont::PROFONT_12_POINT;

use crate::forth::Forth;

/// Settings for the [sermux_shell] daemon
#[derive(Debug)]
pub struct SermuxShellSettings {
    /// Sermux port to serve the shell on
    ///
    /// Defaults to [WellKnown::ForthShell0]
    pub port: u16,
    /// Number of bytes used for the sermux buffer
    ///
    /// Defaults to 256
    pub capacity: usize,
    /// Forth parameters for the shell
    ///
    /// Uses the default value of [Params]
    pub forth_settings: Params,
    /// Hidden for forwards compat
    _priv: (),
}

impl Default for SermuxShellSettings {
    fn default() -> Self {
        Self {
            port: WellKnown::ForthShell0.into(),
            capacity: 256,
            forth_settings: Default::default(),
            _priv: (),
        }
    }
}

/// Spawns a forth shell on the given port
#[tracing::instrument(skip(k))]
pub async fn sermux_shell(k: &'static Kernel, settings: SermuxShellSettings) {
    let SermuxShellSettings {
        port,
        capacity,
        forth_settings,
        _priv,
    } = settings;
    let port = PortHandle::open(k, port, capacity).await.unwrap();
    let (task, tid_io) = Forth::new(k, forth_settings)
        .await
        .expect("Forth spawning must succeed");
    k.spawn(task.run()).await;
    k.spawn(async move {
        loop {
            futures::select_biased! {
                rgr = port.consumer().read_grant().fuse() => {
                    let needed = rgr.len();
                    let mut tid_io_wgr = tid_io.producer().send_grant_exact(needed).await;
                    tid_io_wgr.copy_from_slice(&rgr);
                    tid_io_wgr.commit(needed);
                    rgr.release(needed);
                },
                output = tid_io.consumer().read_grant().fuse() => {
                    let needed = output.len();
                    port.send(&output).await;
                    output.release(needed);
                }
            }
        }
    })
    .await;
}

/// Settings for the [graphical_shell_mono] daemon
///
/// This does NOT implement [Default]. Instead use [GraphicalShellSettings::with_display_size].
///
/// For example:
/// ```
/// use kernel::daemons::shells::GraphicalShellSettings;
/// let shell = GraphicalShellSettings {
///     // override the capacity with a larger value:
///     capacity: 512,
///    ..GraphicalShellSettings::with_display_size(420, 69), // nice!
/// };
/// # drop(shell);
/// ```
#[derive(Debug)]
pub struct GraphicalShellSettings {
    /// Sermux port to use as a PseudoKeyboard.
    ///
    /// Defaults to [WellKnown::PseudoKeyboard]
    pub port: u16,
    /// Number of bytes used for the sermux buffer
    ///
    /// Defaults to 256
    pub capacity: usize,
    /// Forth parameters for the shell
    ///
    /// Uses the default value of [Params]
    pub forth_settings: Params,
    /// Display width in pixels
    pub disp_width_px: u32,
    /// Display height in pixels
    pub disp_height_px: u32,
    /// Font used for the shell
    ///
    /// Defaults to [PROFONT_12_POINT]
    pub font: MonoFont<'static>,
    /// Hidden for forwards compat
    _priv: (),
}

impl GraphicalShellSettings {
    pub fn with_display_size(width_px: u32, height_px: u32) -> Self {
        Self {
            port: WellKnown::PseudoKeyboard.into(),
            capacity: 256,
            forth_settings: Default::default(),
            disp_width_px: width_px,
            disp_height_px: height_px,
            font: PROFONT_12_POINT,
            _priv: (),
        }
    }
}

/// Spawns a graphical shell using the [EmbDisplayService](crate::services::emb_display::EmbDisplayService) service
#[tracing::instrument(skip(k))]
pub async fn graphical_shell_mono(k: &'static Kernel, settings: GraphicalShellSettings) {
    let GraphicalShellSettings {
        port,
        capacity,
        forth_settings,
        disp_width_px,
        disp_height_px,
        font,
        _priv,
    } = settings;

    // TODO: Reconsider using a sermux port here once we have a more real keyboard thing
    let port = PortHandle::open(k, port, capacity).await.unwrap();

    let mut disp_hdl = EmbDisplayClient::from_registry(k).await;
    let char_y = font.character_size.height;
    let char_x = font.character_size.width + font.character_spacing;

    // Draw titlebar
    {
        let mut fc_0 = disp_hdl
            .get_framechunk(0, 0, disp_width_px, char_y)
            .await
            .unwrap();
        let text_style = MonoTextStyle::new(&font, Gray8::WHITE);
        let text1 = Text::new("mnemOS", Point::new(0, font.baseline as i32), text_style);
        text1.draw(&mut fc_0).unwrap();

        let title = "forth shell";
        let text2 = Text::new(
            title,
            Point::new(
                (disp_width_px as i32) - ((title.len() as u32) * char_x) as i32,
                font.baseline as i32,
            ),
            text_style,
        );
        text2.draw(&mut fc_0).unwrap();

        let line_style = PrimitiveStyle::with_stroke(Gray8::WHITE, 1);
        Line::new(
            Point {
                x: 0,
                y: font.underline.offset as i32,
            },
            Point {
                x: disp_width_px as i32,
                y: font.underline.offset as i32,
            },
        )
        .into_styled(line_style)
        .draw(&mut fc_0)
        .unwrap();
        disp_hdl.draw_framechunk(fc_0).await.unwrap();
    }

    let style = ring_drawer::BwStyle {
        background: Gray8::BLACK,
        font: MonoTextStyle::new(&font, Gray8::WHITE),
    };

    // At 12-pt font, there is enough room for 16 lines, with 50 chars/line.
    //
    // Leave out 4 for the implicit margin of two characters on each gutter.
    let mut rline = RingLine::<16, 46>::new();

    let (task, tid_io) = Forth::new(k, forth_settings)
        .await
        .expect("Forth spawning must succeed");

    // Spawn the forth task
    k.spawn(task.run()).await;

    loop {
        // Wait until there is a frame buffer ready. There wouldn't be if we've spammed frames
        // before they've been consumed.
        let mut fc_0 = loop {
            let fc = disp_hdl
                .get_framechunk(0, char_y as i32, disp_width_px, disp_height_px - char_y)
                .await;
            if let Some(fc) = fc {
                break fc;
            } else {
                k.sleep(Duration::from_millis(10)).await;
            }
        };
        ring_drawer::drawer_bw(&mut fc_0, &rline, style.clone()).unwrap();
        disp_hdl.draw_framechunk(fc_0).await.unwrap();

        futures::select_biased! {
            rgr = port.consumer().read_grant().fuse() => {
                let mut used = 0;
                'input: for &b in rgr.iter() {
                    used += 1;
                    match rline.append_local_char(b) {
                        Ok(_) => {}
                        // backspace
                        Err(_) if b == 0x7F => {
                            rline.pop_local_char();
                        }
                        Err(_) if b == b'\n' => {
                            let needed = rline.local_editing_len();
                            if needed != 0 {
                                let mut tid_io_wgr = tid_io.producer().send_grant_exact(needed).await;
                                rline.copy_local_editing_to(&mut tid_io_wgr).unwrap();
                                tid_io_wgr.commit(needed);
                                rline.submit_local_editing();
                                break 'input;
                            }
                        }
                        Err(error) => {
                            tracing::warn!(?error, "Error appending char: {:02X}", b);
                        }
                    }
                }

                rgr.release(used);
            },
            output = tid_io.consumer().read_grant().fuse() => {
                let len = output.len();
                tracing::trace!(len, "Received output from tid_io");
                for &b in output.iter() {
                    // TODO(eliza): what if this errors lol
                    if b == b'\n' {
                        rline.submit_remote_editing();
                    } else {
                        let _ = rline.append_remote_char(b);
                    }
                }
                output.release(len);
            }
        }
    }
}