@@ -47,6 +47,121 @@ static mut SERIAL_CONSOLE: Option<SerialConsole> = None;
4747/// If so, don't panic if a serial write fails.
4848static IS_PANIC : AtomicBool = AtomicBool :: new ( false ) ;
4949
50+ /// Our keyboard controller
51+ static mut STD_INPUT : StdInput = StdInput :: new ( ) ;
52+
53+ struct StdInput {
54+ keyboard : pc_keyboard:: EventDecoder < pc_keyboard:: layouts:: AnyLayout > ,
55+ buffer : heapless:: spsc:: Queue < u8 , 16 > ,
56+ }
57+
58+ impl StdInput {
59+ const fn new ( ) -> StdInput {
60+ StdInput {
61+ keyboard : pc_keyboard:: EventDecoder :: new (
62+ pc_keyboard:: layouts:: AnyLayout :: Uk105Key ( pc_keyboard:: layouts:: Uk105Key ) ,
63+ pc_keyboard:: HandleControl :: MapLettersToUnicode ,
64+ ) ,
65+ buffer : heapless:: spsc:: Queue :: new ( ) ,
66+ }
67+ }
68+
69+ fn get_buffered_data ( & mut self , buffer : & mut [ u8 ] ) -> usize {
70+ // If there is some data, get it.
71+ let mut count = 0 ;
72+ for slot in buffer. iter_mut ( ) {
73+ if let Some ( n) = self . buffer . dequeue ( ) {
74+ * slot = n;
75+ count += 1 ;
76+ }
77+ }
78+ count
79+ }
80+
81+ /// Gets a raw event from the keyboard
82+ fn get_raw ( & mut self ) -> Option < pc_keyboard:: DecodedKey > {
83+ let api = API . get ( ) ;
84+ match ( api. hid_get_event ) ( ) {
85+ bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: KeyPress ( code) ) ) => {
86+ let pckb_ev = pc_keyboard:: KeyEvent {
87+ code,
88+ state : pc_keyboard:: KeyState :: Down ,
89+ } ;
90+ self . keyboard . process_keyevent ( pckb_ev)
91+ }
92+ bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: KeyRelease ( code) ) ) => {
93+ let pckb_ev = pc_keyboard:: KeyEvent {
94+ code,
95+ state : pc_keyboard:: KeyState :: Up ,
96+ } ;
97+ self . keyboard . process_keyevent ( pckb_ev)
98+ }
99+ bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: MouseInput (
100+ _ignore,
101+ ) ) ) => None ,
102+ bios:: ApiResult :: Ok ( bios:: FfiOption :: None ) => {
103+ // Do nothing
104+ None
105+ }
106+ bios:: ApiResult :: Err ( _e) => None ,
107+ }
108+ }
109+
110+ /// Gets some input bytes, as UTF-8.
111+ ///
112+ /// The data you get might be cut in the middle of a UTF-8 character.
113+ fn get_data ( & mut self , buffer : & mut [ u8 ] ) -> usize {
114+ let count = self . get_buffered_data ( buffer) ;
115+ if buffer. len ( ) == 0 || count > 0 {
116+ return count;
117+ }
118+
119+ // Nothing buffered - ask the keyboard for something
120+ let decoded_key = self . get_raw ( ) ;
121+
122+ match decoded_key {
123+ Some ( pc_keyboard:: DecodedKey :: Unicode ( mut ch) ) => {
124+ if ch == '\n' {
125+ ch = '\r' ;
126+ }
127+ let mut buffer = [ 0u8 ; 6 ] ;
128+ let s = ch. encode_utf8 ( & mut buffer) ;
129+ for b in s. as_bytes ( ) {
130+ // This will always fit
131+ self . buffer . enqueue ( * b) . unwrap ( ) ;
132+ }
133+ }
134+ Some ( pc_keyboard:: DecodedKey :: RawKey ( pc_keyboard:: KeyCode :: ArrowRight ) ) => {
135+ // Load the ANSI sequence for a right arrow
136+ for b in b"\x1b [0;77b" {
137+ // This will always fit
138+ self . buffer . enqueue ( * b) . unwrap ( ) ;
139+ }
140+ }
141+ _ => {
142+ // Drop anything else
143+ }
144+ }
145+
146+ // if let Some((uart_dev, _serial_conf)) = menu.context.config.get_serial_console() {
147+ // while !self.buffer.is_full() {
148+ // let mut buffer = [0u8];
149+ // let wrapper = neotron_common_bios::FfiBuffer::new(&mut buffer);
150+ // match (api.serial_read)(uart_dev, wrapper, neotron_common_bios::FfiOption::None) {
151+ // neotron_common_bios::ApiResult::Ok(n) if n >= 0 => {
152+ // self.buffer.enqueue(buffer[0]).unwrap();
153+ // }
154+ // _ => {
155+ // break;
156+ // }
157+ // }
158+ // }
159+ // }
160+
161+ self . get_buffered_data ( buffer)
162+ }
163+ }
164+
50165// ===========================================================================
51166// Macros
52167// ===========================================================================
@@ -176,7 +291,6 @@ impl core::fmt::Write for SerialConsole {
176291
177292pub struct Ctx {
178293 config : config:: Config ,
179- keyboard : pc_keyboard:: EventDecoder < pc_keyboard:: layouts:: AnyLayout > ,
180294 tpa : program:: TransientProgramArea ,
181295}
182296
@@ -287,10 +401,6 @@ pub extern "C" fn os_main(api: &bios::Api) -> ! {
287401
288402 let mut ctx = Ctx {
289403 config,
290- keyboard : pc_keyboard:: EventDecoder :: new (
291- pc_keyboard:: layouts:: AnyLayout :: Uk105Key ( pc_keyboard:: layouts:: Uk105Key ) ,
292- pc_keyboard:: HandleControl :: MapLettersToUnicode ,
293- ) ,
294404 tpa : unsafe {
295405 // We have to trust the values given to us by the BIOS. If it lies, we will crash.
296406 program:: TransientProgramArea :: new ( tpa_start, tpa_size)
@@ -310,68 +420,10 @@ pub extern "C" fn os_main(api: &bios::Api) -> ! {
310420 let mut menu = menu:: Runner :: new ( & commands:: OS_MENU , & mut buffer, ctx) ;
311421
312422 loop {
313- match ( api. hid_get_event ) ( ) {
314- bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: KeyPress ( code) ) ) => {
315- let pckb_ev = pc_keyboard:: KeyEvent {
316- code,
317- state : pc_keyboard:: KeyState :: Down ,
318- } ;
319- if let Some ( pc_keyboard:: DecodedKey :: Unicode ( mut ch) ) =
320- menu. context . keyboard . process_keyevent ( pckb_ev)
321- {
322- if ch == '\n' {
323- ch = '\r' ;
324- }
325- let mut buffer = [ 0u8 ; 6 ] ;
326- let s = ch. encode_utf8 ( & mut buffer) ;
327- for b in s. as_bytes ( ) {
328- menu. input_byte ( * b) ;
329- }
330- }
331- }
332- bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: KeyRelease ( code) ) ) => {
333- let pckb_ev = pc_keyboard:: KeyEvent {
334- code,
335- state : pc_keyboard:: KeyState :: Up ,
336- } ;
337- if let Some ( pc_keyboard:: DecodedKey :: Unicode ( ch) ) =
338- menu. context . keyboard . process_keyevent ( pckb_ev)
339- {
340- let mut buffer = [ 0u8 ; 6 ] ;
341- let s = ch. encode_utf8 ( & mut buffer) ;
342- for b in s. as_bytes ( ) {
343- menu. input_byte ( * b) ;
344- }
345- }
346- }
347- bios:: ApiResult :: Ok ( bios:: FfiOption :: Some ( bios:: hid:: HidEvent :: MouseInput (
348- _ignore,
349- ) ) ) => { }
350- bios:: ApiResult :: Ok ( bios:: FfiOption :: None ) => {
351- // Do nothing
352- }
353- bios:: ApiResult :: Err ( e) => {
354- osprintln ! ( "Failed to get HID events: {:?}" , e) ;
355- }
356- }
357- if let Some ( ( uart_dev, _serial_conf) ) = menu. context . config . get_serial_console ( ) {
358- loop {
359- let mut buffer = [ 0u8 ; 8 ] ;
360- let wrapper = neotron_common_bios:: FfiBuffer :: new ( & mut buffer) ;
361- match ( api. serial_read ) ( uart_dev, wrapper, neotron_common_bios:: FfiOption :: None ) {
362- neotron_common_bios:: ApiResult :: Ok ( n) if n == 0 => {
363- break ;
364- }
365- neotron_common_bios:: ApiResult :: Ok ( n) => {
366- for b in & buffer[ 0 ..n] {
367- menu. input_byte ( * b) ;
368- }
369- }
370- _ => {
371- break ;
372- }
373- }
374- }
423+ let mut buffer = [ 0u8 ; 16 ] ;
424+ let count = unsafe { STD_INPUT . get_data ( & mut buffer) } ;
425+ for b in & buffer[ 0 ..count] {
426+ menu. input_byte ( * b) ;
375427 }
376428 ( api. power_idle ) ( ) ;
377429 }
0 commit comments