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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Simulated display driver
//!
//! This is an early attempt at a "frame buffer" style display driver. It uses the
//! embedded-graphics simulator crate to act as a display in simulated environments.
//!
//! This implementation is sort of a work in progress, it isn't really a *great*
//! long-term solution, but rather "okay for now".
//!
//! A framebuffer of pixels is allocated for the entire display on registration.
//! This could be, for example, 400x240 pixels.
//!
//! The driver will then allow for a certain number of "sub frames" to be requested.
//!
//! These sub frames could be for the entire display (400x240), or a portion of it,
//! for example 200x120 pixels.
//!
//! Clients of the driver can draw into the sub-frames that they receive, then send
//! them back to be rendered into the total frame. Any data in the client's sub-frame
//! will replace the current contents of the whole frame buffer.

use std::{process::exit, time::Duration};

use embedded_graphics::{
    image::{Image, ImageRaw},
    pixelcolor::Gray8,
    prelude::*,
};
use embedded_graphics_simulator::{
    sdl2::{Keycode, Mod},
    BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use maitake::sync::Mutex;
use melpo_config::DisplayConfig;
use mnemos_alloc::containers::{Arc, HeapArray};
use mnemos_kernel::{
    registry,
    services::{
        emb_display::{
            DisplayMetadata, EmbDisplayService, FrameChunk, FrameKind, MonoChunk, Request, Response,
        },
        keyboard::{
            key_event::{self, KeyCode, Modifiers},
            mux::KeyboardMuxClient,
            KeyEvent,
        },
    },
    Kernel,
};

/// Implements the [`EmbDisplayService`] driver using the `embedded-graphics`
/// simulator.
pub struct SimDisplay;

impl SimDisplay {
    /// Register the driver instance
    ///
    /// Registration will also start the simulated display, meaning that the display
    /// window will appear.
    #[tracing::instrument(skip(kernel))]
    pub async fn register(
        kernel: &'static Kernel,
        settings: DisplayConfig,
        width: u32,
        height: u32,
    ) -> Result<(), registry::RegistrationError> {
        tracing::debug!("initializing SimDisplay server ({width}x{height})...");
        let cmd = kernel
            .registry()
            .bind_konly(settings.kchannel_depth)
            .await?
            .into_request_stream(settings.kchannel_depth)
            .await;

        let commander = CommanderTask {
            kernel,
            cmd,
            width,
            height,
        };

        kernel.spawn(commander.run(width, height, settings)).await;

        tracing::info!("SimDisplayServer initialized!");

        Ok(())
    }
}

//////////////////////////////////////////////////////////////////////////////
// CommanderTask - This is the "driver server"
//////////////////////////////////////////////////////////////////////////////

/// This task is spawned by the call to [`SimDisplay::register`]. It is a single
/// async function that will process requests, and periodically redraw the
/// framebuffer.
struct CommanderTask {
    kernel: &'static Kernel,
    cmd: registry::listener::RequestStream<EmbDisplayService>,
    width: u32,
    height: u32,
}

struct Context {
    sdisp: SimulatorDisplay<Gray8>,
    framebuf: HeapArray<u8>,
    window: Window,
    dirty: bool,
}

impl CommanderTask {
    /// The entrypoint for the driver execution
    async fn run(self, width: u32, height: u32, settings: DisplayConfig) {
        let output_settings = OutputSettingsBuilder::new()
            .theme(BinaryColorTheme::OledBlue)
            .scale(settings.scaling)
            .build();

        let bytes = (width * height) as usize;

        // Create a mutex for the embedded graphics simulator objects.
        //
        // We do this because if we don't call "update" regularly, the window just
        // sort of freezes. We also make the update loop check for "quit" events,
        // because otherwise the gui window just swallows all the control-c events,
        // which means you have to send a sigkill to actually get the simulator to
        // fully stop.
        //
        // The update loop *needs* to drop the egsim items, otherwise they just exist
        // in the mutex until the next time a frame is displayed, which right now is
        // only whenever line characters actually arrive.
        let sdisp = SimulatorDisplay::<Gray8>::new(Size::new(width, height));
        let window = Window::new("mnemOS", &output_settings);
        let framebuf = HeapArray::new(bytes, 0x00).await;
        let mutex = Arc::new(Mutex::new(Some(Context {
            sdisp,
            framebuf,
            window,
            dirty: true,
        })))
        .await;

        // Spawn a task that draws the framebuffer at a regular rate of 15Hz.
        self.kernel
            .spawn({
                let mutex = mutex.clone();
                render_loop(self.kernel, mutex, settings.frames_per_second)
            })
            .await;

        self.message_loop(mutex).await;
    }

    /// This loop services incoming client requests.
    ///
    /// Generally, don't handle errors when replying to clients, this indicates that they
    /// sent us a message and "hung up" without waiting for a response.
    async fn message_loop(&self, mutex: Arc<Mutex<Option<Context>>>) {
        loop {
            let msg = self.cmd.next_request().await;
            let (req, env, reply_tx) = msg.split();
            match req {
                Request::Draw(FrameChunk::Mono(fc)) => {
                    if self.draw_mono(&fc, &mutex).await.is_err() {
                        break;
                    } else {
                        let response = env.fill(Ok(Response::DrawComplete(fc.into())));
                        let _ = reply_tx.reply_konly(response).await;
                    }
                }
                Request::GetMeta => {
                    let meta = DisplayMetadata {
                        kind: FrameKind::Mono,
                        width: self.width,
                        height: self.height,
                    };
                    let response = env.fill(Ok(Response::FrameMeta(meta)));
                    let _ = reply_tx.reply_konly(response).await;
                }
                _ => todo!(),
            }
        }
    }

    /// Draw the given MonoChunk to the persistent framebuffer
    async fn draw_mono(&self, fc: &MonoChunk, mutex: &Mutex<Option<Context>>) -> Result<(), ()> {
        let mut guard = mutex.lock().await;
        let ctx = if let Some(c) = (*guard).as_mut() {
            c
        } else {
            return Err(());
        };

        let Context {
            sdisp,
            dirty,
            framebuf,
            ..
        } = ctx;

        draw_to(framebuf, fc, self.width, self.height);
        let raw_img = frame_display(framebuf, self.width).unwrap();
        let image = Image::new(&raw_img, Point::new(0, 0));
        image.draw(sdisp).unwrap();
        *dirty = true;

        Ok(())
    }
}

async fn handle_key_event(kmc: &mut KeyboardMuxClient, evt: SimulatorEvent) -> bool {
    match evt {
        SimulatorEvent::KeyDown {
            keycode,
            keymod,
            repeat,
        } => {
            tracing::trace!(?evt, "Got key event from Simulator");
            if let Some(k) = sim_key_to_key_event(keycode, keymod, repeat, true) {
                kmc.publish_key(k).await.is_err()
            } else {
                false
            }
        }
        SimulatorEvent::KeyUp {
            keycode,
            keymod,
            repeat,
        } => {
            tracing::trace!(?evt, "Got key event from Simulator");
            if let Some(k) = sim_key_to_key_event(keycode, keymod, repeat, false) {
                kmc.publish_key(k).await.is_err()
            } else {
                false
            }
        }
        SimulatorEvent::Quit => true,
        _ => false,
    }
}

async fn render_loop(
    kernel: &'static Kernel,
    mutex: Arc<Mutex<Option<Context>>>,
    frames_per_second: usize,
) {
    let mut idle_ticks = 0;
    let mut keymux = KeyboardMuxClient::from_registry(kernel)
        .await
        .expect("no keyboard mux service!");
    let mut first_done = false;
    let sleep_time = Duration::from_micros(1_000_000 / (frames_per_second as u64));
    loop {
        kernel.sleep(sleep_time).await;
        let mut guard = mutex.lock().await;
        let mut done = false;
        if let Some(Context {
            sdisp,
            window,
            dirty,
            ..
        }) = (*guard).as_mut()
        {
            // We can't poll the events until the first draw, or we'll panic.
            // But once we have: we want to always process events, even if there
            // is nothing to draw, to potentially feed the keymux or catch
            // a "time to die" event.
            if first_done {
                for evt in window.events() {
                    if handle_key_event(&mut keymux, evt).await {
                        done = true;
                    }
                }
            }

            // If nothing has been drawn, only update the frame at 5Hz to save
            // CPU usage
            if *dirty || idle_ticks >= 4 {
                idle_ticks = 0;
                *dirty = false;
                window.update(sdisp);
                first_done = true;
            } else {
                idle_ticks += 1;
            }
        } else {
            done = true;
        }
        if done {
            tracing::warn!("Display closed, stopping melpomene");
            kernel.sleep(Duration::from_millis(100)).await;
            exit(0);
        }
    }
}

// TODO: move to shared helper module - https://github.com/tosc-rs/mnemos/issues/260
// TODO: blocked on e-g update https://github.com/tosc-rs/mnemos/issues/259
fn draw_to(dest: &mut HeapArray<u8>, src: &MonoChunk, width: u32, height: u32) {
    let meta = src.meta();
    let data = src.data();
    let mask = src.mask();

    let start_x = meta.start_x();
    let start_y = meta.start_y();
    let src_width = meta.width();

    if start_y >= height {
        return;
    }
    if start_x >= width {
        return;
    }

    // Take all destination rows, starting at the start_y line
    let all_dest_rows = dest.chunks_exact_mut(width as usize);
    let dest_rows = all_dest_rows.skip(start_y as usize);

    // Then take all source rows, and zip together the mask bits
    let all_src_rows = data.chunks(src_width as usize);
    let all_src_mask_rows = mask.chunks(src_width as usize);
    let all_src = all_src_rows.zip(all_src_mask_rows);

    // Combine them together, this gives us automatic "early return"
    // when either we run out of source rows, or destination rows
    let zip_rows = dest_rows.zip(all_src);
    for (dest_row, (src_data, src_mask)) in zip_rows {
        // Zip the data and mask lines together so we can use them
        let src = src_data.iter().zip(src_mask.iter());

        dest_row
            .iter_mut()
            // Skip to the start of the subframe
            .skip(start_x as usize)
            // Again, zipping means we stop as soon as we run out of
            // source OR destination pixesl on this line
            .zip(src)
            .filter_map(|(d, (s_d, s_m))| {
                // look at the mask, to see if the subframe should modify
                // the total frame
                if *s_m != 0 {
                    Some((d, s_d))
                } else {
                    None
                }
            })
            .for_each(|(d, s)| {
                *d = *s;
            });
    }
}

// TODO: move to shared helper module - https://github.com/tosc-rs/mnemos/issues/260
// TODO: blocked on e-g update https://github.com/tosc-rs/mnemos/issues/259

/// Create and return a Simulator display object from raw pixel data.
///
/// Pixel data is turned into a raw image, and then drawn onto a SimulatorDisplay object
/// This is necessary as a e-g Window only accepts SimulatorDisplay object
/// On a physical display, the raw pixel data can be sent over to the display directly
/// Using the display's device interface
fn frame_display(fc: &HeapArray<u8>, width: u32) -> Result<ImageRaw<Gray8>, ()> {
    // TODO: We use Gray8 instead of BinaryColor here because BinaryColor bitpacks to 1bpp,
    // while we are currently doing 8bpp.
    Ok(ImageRaw::<Gray8>::new(fc, width))
}

fn sim_key_to_key_event(
    keycode: Keycode,
    keymod: Mod,
    repeat: bool,
    is_down: bool,
) -> Option<KeyEvent> {
    let key_kind = match (repeat, is_down) {
        (true, true) => key_event::Kind::Held,
        (false, true) => key_event::Kind::Pressed,
        (_, false) => key_event::Kind::Released,
    };

    let mut modi = Modifiers::new();

    if (keymod & Mod::LSHIFTMOD) != Mod::NOMOD {
        modi.set(Modifiers::SHIFT, true);
    }
    if (keymod & Mod::RSHIFTMOD) != Mod::NOMOD {
        modi.set(Modifiers::SHIFT, true);
    }
    if (keymod & Mod::LCTRLMOD) != Mod::NOMOD {
        modi.set(Modifiers::CTRL, true);
    }
    if (keymod & Mod::RCTRLMOD) != Mod::NOMOD {
        modi.set(Modifiers::CTRL, true);
    }
    if (keymod & Mod::LALTMOD) != Mod::NOMOD {
        modi.set(Modifiers::ALT, true);
    }
    if (keymod & Mod::RALTMOD) != Mod::NOMOD {
        modi.set(Modifiers::ALT, true);
    }
    if (keymod & Mod::LGUIMOD) != Mod::NOMOD {
        modi.set(Modifiers::META, true);
    }
    if (keymod & Mod::RGUIMOD) != Mod::NOMOD {
        modi.set(Modifiers::META, true);
    }
    if (keymod & Mod::NUMMOD) != Mod::NOMOD {
        modi.set(Modifiers::NUMLOCK, true);
    }
    if (keymod & Mod::CAPSMOD) != Mod::NOMOD {
        modi.set(Modifiers::CAPSLOCK, true);
    }
    if (keymod & Mod::MODEMOD) != Mod::NOMOD {
        tracing::warn!("Modemod not supported");
    }
    if (keymod & Mod::RESERVEDMOD) != Mod::NOMOD {
        tracing::warn!("Reservedmod not supported");
    }

    let upper = modi.get(Modifiers::SHIFT) || modi.get(Modifiers::CAPSLOCK);

    // Whew this is something.
    //
    // TODO(eliza): Fix this once keymux handles meta characters!
    let code: KeyCode = match keycode {
        Keycode::Backspace => KeyCode::Backspace,
        Keycode::Tab => KeyCode::Tab,
        Keycode::Return => KeyCode::Enter,
        Keycode::Escape => KeyCode::Esc,
        Keycode::Space => KeyCode::Char(' '),
        Keycode::Semicolon => KeyCode::Char(if upper { ':' } else { ';' }),
        Keycode::Less => KeyCode::Char('<'),
        Keycode::Equals => KeyCode::Char(if upper { '+' } else { '=' }),
        Keycode::Greater => KeyCode::Char('>'),
        Keycode::Question => KeyCode::Char('?'),
        Keycode::At => KeyCode::Char('@'),
        Keycode::Caret => KeyCode::Char('^'),
        Keycode::Underscore => KeyCode::Char('_'),
        Keycode::A => KeyCode::Char(if upper { 'A' } else { 'a' }),
        Keycode::B => KeyCode::Char(if upper { 'B' } else { 'b' }),
        Keycode::C => KeyCode::Char(if upper { 'C' } else { 'c' }),
        Keycode::D => KeyCode::Char(if upper { 'D' } else { 'd' }),
        Keycode::E => KeyCode::Char(if upper { 'E' } else { 'e' }),
        Keycode::F => KeyCode::Char(if upper { 'F' } else { 'f' }),
        Keycode::G => KeyCode::Char(if upper { 'G' } else { 'g' }),
        Keycode::H => KeyCode::Char(if upper { 'H' } else { 'h' }),
        Keycode::I => KeyCode::Char(if upper { 'I' } else { 'i' }),
        Keycode::J => KeyCode::Char(if upper { 'J' } else { 'j' }),
        Keycode::K => KeyCode::Char(if upper { 'K' } else { 'k' }),
        Keycode::L => KeyCode::Char(if upper { 'L' } else { 'l' }),
        Keycode::M => KeyCode::Char(if upper { 'M' } else { 'm' }),
        Keycode::N => KeyCode::Char(if upper { 'N' } else { 'n' }),
        Keycode::O => KeyCode::Char(if upper { 'O' } else { 'o' }),
        Keycode::P => KeyCode::Char(if upper { 'P' } else { 'p' }),
        Keycode::Q => KeyCode::Char(if upper { 'Q' } else { 'q' }),
        Keycode::R => KeyCode::Char(if upper { 'R' } else { 'r' }),
        Keycode::S => KeyCode::Char(if upper { 'S' } else { 's' }),
        Keycode::T => KeyCode::Char(if upper { 'T' } else { 't' }),
        Keycode::U => KeyCode::Char(if upper { 'U' } else { 'u' }),
        Keycode::V => KeyCode::Char(if upper { 'V' } else { 'v' }),
        Keycode::W => KeyCode::Char(if upper { 'W' } else { 'w' }),
        Keycode::X => KeyCode::Char(if upper { 'X' } else { 'x' }),
        Keycode::Y => KeyCode::Char(if upper { 'Y' } else { 'y' }),
        Keycode::Z => KeyCode::Char(if upper { 'Z' } else { 'z' }),
        Keycode::Delete => KeyCode::Delete,
        Keycode::F1 => KeyCode::F(1),
        Keycode::F2 => KeyCode::F(2),
        Keycode::F3 => KeyCode::F(3),
        Keycode::F4 => KeyCode::F(4),
        Keycode::F5 => KeyCode::F(5),
        Keycode::F6 => KeyCode::F(6),
        Keycode::F7 => KeyCode::F(7),
        Keycode::F8 => KeyCode::F(8),
        Keycode::F9 => KeyCode::F(9),
        Keycode::F10 => KeyCode::F(10),
        Keycode::F11 => KeyCode::F(11),
        Keycode::F12 => KeyCode::F(12),
        Keycode::F13 => KeyCode::F(13),
        Keycode::F14 => KeyCode::F(14),
        Keycode::F15 => KeyCode::F(15),
        Keycode::F16 => KeyCode::F(16),
        Keycode::F17 => KeyCode::F(17),
        Keycode::F18 => KeyCode::F(18),
        Keycode::F19 => KeyCode::F(19),
        Keycode::F20 => KeyCode::F(20),
        Keycode::F21 => KeyCode::F(21),
        Keycode::F22 => KeyCode::F(22),
        Keycode::F23 => KeyCode::F(23),
        Keycode::F24 => KeyCode::F(24),
        Keycode::PrintScreen => KeyCode::PrintScreen,
        Keycode::Pause => KeyCode::Pause,
        Keycode::Insert => KeyCode::Insert,
        Keycode::Home => KeyCode::Home,
        Keycode::PageUp => KeyCode::PageUp,
        Keycode::End => KeyCode::End,
        Keycode::PageDown => KeyCode::PageDown,
        Keycode::Right => KeyCode::Right,
        Keycode::Left => KeyCode::Left,
        Keycode::Down => KeyCode::Down,
        Keycode::Up => KeyCode::Up,
        Keycode::Num0 => KeyCode::Char(if upper { ')' } else { '0' }),
        Keycode::Num1 => KeyCode::Char(if upper { '!' } else { '1' }),
        Keycode::Num2 => KeyCode::Char(if upper { '@' } else { '2' }),
        Keycode::Num3 => KeyCode::Char(if upper { '#' } else { '3' }),
        Keycode::Num4 => KeyCode::Char(if upper { '$' } else { '4' }),
        Keycode::Num5 => KeyCode::Char(if upper { '%' } else { '5' }),
        Keycode::Num6 => KeyCode::Char(if upper { '^' } else { '6' }),
        Keycode::Num7 => KeyCode::Char(if upper { '&' } else { '7' }),
        Keycode::Num8 => KeyCode::Char(if upper { '*' } else { '8' }),
        Keycode::Num9 => KeyCode::Char(if upper { '(' } else { '9' }),
        Keycode::Quote => KeyCode::Char(if upper { '"' } else { '\'' }),
        Keycode::LeftBracket => KeyCode::Char(if upper { '{' } else { '[' }),
        Keycode::Backslash => KeyCode::Char(if upper { '|' } else { '\\' }),
        Keycode::RightBracket => KeyCode::Char(if upper { '}' } else { ']' }),
        Keycode::Backquote => KeyCode::Char(if upper { '~' } else { '`' }),
        Keycode::Plus => KeyCode::Char('+'),
        Keycode::Comma => KeyCode::Char(if upper { '<' } else { ',' }),
        Keycode::Minus => KeyCode::Char(if upper { '_' } else { '-' }),
        Keycode::Period => KeyCode::Char(if upper { '>' } else { '.' }),
        Keycode::Slash => KeyCode::Char(if upper { '?' } else { '/' }),

        // Ignored - these show up as meta keys
        Keycode::LCtrl => return None,
        Keycode::LShift => return None,
        Keycode::LAlt => return None,
        Keycode::LGui => return None,
        Keycode::RCtrl => return None,
        Keycode::RShift => return None,
        Keycode::RAlt => return None,
        Keycode::RGui => return None,
        Keycode::Mode => return None,

        // TODO
        // Keycode::Exclaim => todo!(),
        // Keycode::Quotedbl => todo!(),
        // Keycode::Hash => todo!(),
        // Keycode::Dollar => todo!(),
        // Keycode::Percent => todo!(),
        // Keycode::Ampersand => todo!(),
        // Keycode::LeftParen => todo!(),
        // Keycode::RightParen => todo!(),
        // Keycode::Asterisk => todo!(),
        // Keycode::Colon => todo!(),
        // Keycode::CapsLock => todo!(),
        // Keycode::ScrollLock => todo!(),
        // Keycode::NumLockClear => todo!(),
        // Keycode::KpDivide => todo!(),
        // Keycode::KpMultiply => todo!(),
        // Keycode::KpMinus => todo!(),
        // Keycode::KpPlus => todo!(),
        // Keycode::KpEnter => todo!(),
        // Keycode::Kp1 => todo!(),
        // Keycode::Kp2 => todo!(),
        // Keycode::Kp3 => todo!(),
        // Keycode::Kp4 => todo!(),
        // Keycode::Kp5 => todo!(),
        // Keycode::Kp6 => todo!(),
        // Keycode::Kp7 => todo!(),
        // Keycode::Kp8 => todo!(),
        // Keycode::Kp9 => todo!(),
        // Keycode::Kp0 => todo!(),
        // Keycode::KpPeriod => todo!(),
        // Keycode::Application => todo!(),
        // Keycode::Power => todo!(),
        // Keycode::KpEquals => todo!(),
        // Keycode::Execute => todo!(),
        // Keycode::Help => todo!(),
        // Keycode::Menu => todo!(),
        // Keycode::Select => todo!(),
        // Keycode::Stop => todo!(),
        // Keycode::Again => todo!(),
        // Keycode::Undo => todo!(),
        // Keycode::Cut => todo!(),
        // Keycode::Copy => todo!(),
        // Keycode::Paste => todo!(),
        // Keycode::Find => todo!(),
        // Keycode::Mute => todo!(),
        // Keycode::VolumeUp => todo!(),
        // Keycode::VolumeDown => todo!(),
        // Keycode::KpComma => todo!(),
        // Keycode::KpEqualsAS400 => todo!(),
        // Keycode::AltErase => todo!(),
        // Keycode::Sysreq => todo!(),
        // Keycode::Cancel => todo!(),
        // Keycode::Clear => todo!(),
        // Keycode::Prior => todo!(),
        // Keycode::Return2 => todo!(),
        // Keycode::Separator => todo!(),
        // Keycode::Out => todo!(),
        // Keycode::Oper => todo!(),
        // Keycode::ClearAgain => todo!(),
        // Keycode::CrSel => todo!(),
        // Keycode::ExSel => todo!(),
        // Keycode::Kp00 => todo!(),
        // Keycode::Kp000 => todo!(),
        // Keycode::ThousandsSeparator => todo!(),
        // Keycode::DecimalSeparator => todo!(),
        // Keycode::CurrencyUnit => todo!(),
        // Keycode::CurrencySubUnit => todo!(),
        // Keycode::KpLeftParen => todo!(),
        // Keycode::KpRightParen => todo!(),
        // Keycode::KpLeftBrace => todo!(),
        // Keycode::KpRightBrace => todo!(),
        // Keycode::KpTab => todo!(),
        // Keycode::KpBackspace => todo!(),
        // Keycode::KpA => todo!(),
        // Keycode::KpB => todo!(),
        // Keycode::KpC => todo!(),
        // Keycode::KpD => todo!(),
        // Keycode::KpE => todo!(),
        // Keycode::KpF => todo!(),
        // Keycode::KpXor => todo!(),
        // Keycode::KpPower => todo!(),
        // Keycode::KpPercent => todo!(),
        // Keycode::KpLess => todo!(),
        // Keycode::KpGreater => todo!(),
        // Keycode::KpAmpersand => todo!(),
        // Keycode::KpDblAmpersand => todo!(),
        // Keycode::KpVerticalBar => todo!(),
        // Keycode::KpDblVerticalBar => todo!(),
        // Keycode::KpColon => todo!(),
        // Keycode::KpHash => todo!(),
        // Keycode::KpSpace => todo!(),
        // Keycode::KpAt => todo!(),
        // Keycode::KpExclam => todo!(),
        // Keycode::KpMemStore => todo!(),
        // Keycode::KpMemRecall => todo!(),
        // Keycode::KpMemClear => todo!(),
        // Keycode::KpMemAdd => todo!(),
        // Keycode::KpMemSubtract => todo!(),
        // Keycode::KpMemMultiply => todo!(),
        // Keycode::KpMemDivide => todo!(),
        // Keycode::KpPlusMinus => todo!(),
        // Keycode::KpClear => todo!(),
        // Keycode::KpClearEntry => todo!(),
        // Keycode::KpBinary => todo!(),
        // Keycode::KpOctal => todo!(),
        // Keycode::KpDecimal => todo!(),
        // Keycode::KpHexadecimal => todo!(),
        // Keycode::AudioNext => todo!(),
        // Keycode::AudioPrev => todo!(),
        // Keycode::AudioStop => todo!(),
        // Keycode::AudioPlay => todo!(),
        // Keycode::AudioMute => todo!(),
        // Keycode::MediaSelect => todo!(),
        // Keycode::Www => todo!(),
        // Keycode::Mail => todo!(),
        // Keycode::Calculator => todo!(),
        // Keycode::Computer => todo!(),
        // Keycode::AcSearch => todo!(),
        // Keycode::AcHome => todo!(),
        // Keycode::AcBack => todo!(),
        // Keycode::AcForward => todo!(),
        // Keycode::AcStop => todo!(),
        // Keycode::AcRefresh => todo!(),
        // Keycode::AcBookmarks => todo!(),
        // Keycode::BrightnessDown => todo!(),
        // Keycode::BrightnessUp => todo!(),
        // Keycode::DisplaySwitch => todo!(),
        // Keycode::KbdIllumToggle => todo!(),
        // Keycode::KbdIllumDown => todo!(),
        // Keycode::KbdIllumUp => todo!(),
        // Keycode::Eject => todo!(),
        // Keycode::Sleep => todo!(),
        other => {
            tracing::error!(key = other.to_string(), "Key not supported!",);
            return None;
        }
    };

    Some(KeyEvent {
        kind: key_kind,
        modifiers: modi,
        code,
    })
}