GCC Code Coverage Report


Directory: src/gate/
File: src/gate/console.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 187 236 79.2%
Functions: 33 33 100.0%
Branches: 48 116 41.4%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/console.h"
30 #include "gate/results.h"
31 #include "gate/times.h"
32 #include "gate/debugging.h"
33 #include "gate/inputs.h"
34
35
36 #if defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
37 # define GATE_CORE_CONSOLE_WINAPI 1
38 #elif defined(GATE_SYS_WASM_WEB)
39 # define GATE_CORE_CONSOLE_WASM_IMPL 1
40 #elif defined(GATE_SYS_WASM_WASI)
41 # define GATE_CORE_CONSOLE_POSIX 1
42 #elif defined(GATE_SYS_EFI)
43 # define GATE_CORE_CONSOLE_EFI 1
44 #elif defined(GATE_SYS_BEOS)
45 # define GATE_CORE_CONSOLE_POSIX 1
46 #elif defined(GATE_SYS_DOS)
47 # define GATE_CORE_CONSOLE_POSIX 1
48 #elif defined(GATE_SYS_POSIX)
49 # define GATE_CORE_CONSOLE_POSIX 1
50 #elif defined(GATE_SYS_ARDUINO)
51 # define GATE_CORE_CONSOLE_ARDUINO 1
52 #else
53 # define GATE_CORE_CONSOLE_NOIMPL 1
54 #endif
55
56 static void gate_console_release_impl(void* self);
57 static int gate_console_retain_impl(void* self);
58 1 static char const* gate_console_interface_name_impl(void* self)
59 {
60 (void)self;
61 1 return GATE_INTERFACE_NAME_CONSOLE;
62 }
63 static gate_result_t gate_console_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
64 static gate_result_t gate_console_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
65 static gate_result_t gate_console_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
66 static gate_result_t gate_console_flush_impl(void* self);
67
68 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource);
69
70 static gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms);
71 static gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char);
72 static gate_result_t gate_console_write_err_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
73 static gate_result_t gate_console_flush_err_impl(void* self);
74
75 static GATE_INTERFACE_VTBL(gate_console) gate_console_vtbl;
76
77 96 static void gate_init_console_vtbl()
78 {
79
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 60 times.
96 if (!gate_console_vtbl.get_interface_name)
80 {
81 GATE_INTERFACE_VTBL(gate_console) const vtbl_impl =
82 {
83 &gate_console_interface_name_impl,
84 &gate_console_release_impl,
85 &gate_console_retain_impl,
86
87 &gate_console_read_impl,
88 &gate_console_peek_impl,
89 &gate_console_write_impl,
90 &gate_console_flush_impl,
91
92 &gate_console_get_resource_impl,
93
94 &gate_console_await_char_impl,
95 &gate_console_read_char_impl,
96 &gate_console_write_err_impl,
97 &gate_console_flush_err_impl
98 };
99 36 gate_console_vtbl = vtbl_impl;
100 }
101 96 }
102
103
104 1 static gate_input_keycode_t keycode_from_char32(gate_char32_t chr)
105 {
106 1 gate_input_keycode_t key_code = 0;
107
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (chr >= 'A' && chr <= 'Z')
108 {
109 ✗ key_code = (gate_input_keycode_t)(GATE_KBD_KEY_A + chr - 'A');
110 }
111
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 else if (chr >= 'a' && chr <= 'z')
112 {
113 ✗ key_code = (gate_input_keycode_t)(GATE_KBD_KEY_A + chr - 'a');
114 }
115
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 else if (chr >= '0' && chr <= '9')
116 {
117 ✗ key_code = (gate_input_keycode_t)(GATE_KBD_KEY_0 + chr - '0');
118 }
119 else
120 {
121
1/20
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
1 switch (chr)
122 {
123 ✗ case 0x08: key_code = GATE_KBD_KEY_BACKSPACE; break;
124 ✗ case 0x09: key_code = GATE_KBD_KEY_TAB; break;
125 ✗ case 0x0d: key_code = GATE_KBD_KEY_RETURN; break;
126 ✗ case 0x0a: key_code = GATE_KBD_KEY_RETURN; break;
127 ✗ case 0x1b: key_code = GATE_KBD_KEY_ESCAPE; break;
128 ✗ case 0x2b: key_code = GATE_KBD_KEY_ADD; break;
129 ✗ case 0x2d: key_code = GATE_KBD_KEY_SUBTRACT; break;
130 ✗ case 0x2a: key_code = GATE_KBD_KEY_MULTIPLY; break;
131 ✗ case 0x2f: key_code = GATE_KBD_KEY_DEVIDE; break;
132 ✗ case 0x5c: key_code = GATE_KBD_KEY_BACKSLASH; break;
133 ✗ case 0x7f: key_code = GATE_KBD_KEY_BACKSPACE; break;
134 ✗ case 0x2c: key_code = GATE_KBD_KEY_SEPARATOR; break;
135 ✗ case 0x2e: key_code = GATE_KBD_KEY_DECIMAL; break;
136 ✗ case 0x3d: key_code = GATE_KBD_KEY_EQUAL; break;
137 ✗ case 0x5b: key_code = GATE_KBD_KEY_BRACE_LEFT; break;
138 ✗ case 0x5d: key_code = GATE_KBD_KEY_BRACE_RIGHT; break;
139 ✗ case 0x22: key_code = GATE_KBD_KEY_QUOTE; break;
140 ✗ case 0x7e: key_code = GATE_KBD_KEY_TILDE; break;
141 ✗ case 0x20: key_code = GATE_KBD_KEY_SPACEBAR; break;
142 1 default: break;
143 }
144 }
145 1 return key_code;
146 }
147
148
149 #if defined(GATE_CORE_CONSOLE_WINAPI)
150
151 /****************************
152 * Windows implementation *
153 ****************************/
154
155 #include "gate/platforms.h"
156
157 typedef struct gate_console_class_impl
158 {
159 GATE_INTERFACE_VTBL(gate_console)* vtbl;
160
161 HANDLE read_handle;
162 HANDLE write_handle;
163 HANDLE error_handle;
164
165 } gate_console_impl_t;
166
167 static void gate_console_release_impl(void* self)
168 {
169 GATE_UNUSED_ARG(self);
170 }
171 static int gate_console_retain_impl(void* self)
172 {
173 GATE_UNUSED_ARG(self);
174 return 1;
175 }
176 static gate_result_t gate_console_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
177 {
178 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
179 gate_result_t ret = GATE_RESULT_FAILED;
180 DWORD dwread;
181 if (ptr->read_handle == NULL)
182 {
183 if (returned != NULL)
184 {
185 *returned = 0;
186 }
187 ret = GATE_RESULT_OK;
188 }
189 else
190 {
191 if (FALSE == gate_win32_readfile(ptr->read_handle, buffer, (DWORD)bufferlength, &dwread, NULL))
192 {
193 GATE_DEBUG_TRACE("ReadFile() failed");
194 GATE_DEBUG_TRACE_VALUE(gate_win32_getlasterror());
195 gate_win32_print_lasterror(&ret, NULL, 0);
196 }
197 else
198 {
199 if (returned != NULL)
200 {
201 *returned = (gate_size_t)dwread;
202 }
203 ret = GATE_RESULT_OK;
204 }
205 }
206 return ret;
207 }
208 static gate_result_t gate_console_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
209 {
210 GATE_UNUSED_ARG(self);
211 GATE_UNUSED_ARG(buffer);
212 GATE_UNUSED_ARG(bufferlength);
213 GATE_UNUSED_ARG(returned);
214 return GATE_RESULT_NOTSUPPORTED;
215 }
216 static gate_result_t gate_console_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
217 {
218 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
219 gate_result_t ret = GATE_RESULT_FAILED;
220 DWORD dwwrite = 0;
221 if (ptr->write_handle == NULL)
222 {
223 if (written != NULL)
224 {
225 *written = (gate_result_t)bufferlength;
226 }
227 ret = GATE_RESULT_OK;
228 }
229 else
230 {
231 if (FALSE == gate_win32_writefile(ptr->write_handle, buffer, (DWORD)bufferlength, &dwwrite, NULL))
232 {
233 GATE_DEBUG_TRACE("WriteFile() failed");
234 GATE_DEBUG_TRACE_VALUE(gate_win32_getlasterror());
235 gate_win32_print_lasterror(&ret, NULL, 0);
236 }
237 else
238 {
239 if (written != NULL)
240 {
241 *written = (gate_result_t)dwwrite;
242 }
243 ret = GATE_RESULT_OK;
244 }
245 }
246 return ret;
247 }
248 static gate_result_t gate_console_flush_impl(void* self)
249 {
250 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
251 gate_win32_flushfilebuffers(ptr->write_handle);
252 return GATE_RESULT_OK;
253 }
254
255 static gate_bool_t is_control_key(WORD keyCode)
256 {
257 switch (keyCode)
258 {
259 case VK_SHIFT:
260 case VK_CONTROL:
261 case VK_MENU:
262 case VK_LSHIFT:
263 case VK_RSHIFT:
264 case VK_LCONTROL:
265 case VK_RCONTROL:
266 case VK_LMENU:
267 case VK_RMENU:
268 return true;
269 default:
270 return false;
271 }
272 }
273
274 static gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
275 {
276 gate_result_t ret = GATE_RESULT_FAILED;
277 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
278 #ifdef GATE_SYS_WINCE
279 ret = GATE_RESULT_NOTSUPPORTED;
280 #else
281 gate_timecounter_t tc_start;
282 gate_timecounter_t tc_now;
283 gate_int64_t time_diff;
284 DWORD next_timeout = (timeout_ms == GATE_CONSOLE_TIMEOUT_NEVER) ? INFINITE : (DWORD)timeout_ms;
285 INPUT_RECORD input_record = GATE_INIT_EMPTY;
286 DWORD event_count;
287 DWORD wait_result;
288
289 do
290 {
291 ret = gate_timecounter_now(&tc_start);
292 GATE_BREAK_IF_FAILED(ret);
293
294 ret = GATE_RESULT_FAILED;
295 while (WAIT_OBJECT_0 == (wait_result = WaitForSingleObject(ptr->read_handle, next_timeout)))
296 {
297 event_count = 0;
298 if (PeekConsoleInput(ptr->read_handle, &input_record, 1, &event_count))
299 {
300 if (input_record.EventType != KEY_EVENT)
301 {
302 /* not interested in out events -> remove event from buffer */
303 ReadConsoleInput(ptr->read_handle, &input_record, 1, &event_count);
304 }
305 else if (!input_record.Event.KeyEvent.bKeyDown)
306 {
307 /* not interested in key_up -> remove event */
308 ReadConsoleInput(ptr->read_handle, &input_record, 1, &event_count);
309 }
310 else if (is_control_key(input_record.Event.KeyEvent.wVirtualKeyCode))
311 {
312 /* ignore control keys -> remove event */
313 ReadConsoleInput(ptr->read_handle, &input_record, 1, &event_count);
314 }
315 else
316 {
317 /* next event is a readable key */
318 ret = GATE_RESULT_OK;
319 break;
320 }
321 }
322
323 ret = gate_timecounter_now(&tc_now);
324 GATE_BREAK_IF_FAILED(ret);
325
326 time_diff = gate_timecounter_diff(tc_now, tc_start) / 1000;
327 if (time_diff >= timeout_ms)
328 {
329 ret = GATE_RESULT_TIMEOUT;
330 break;
331 }
332 next_timeout = (DWORD)(timeout_ms - time_diff);
333 }
334 if (wait_result == WAIT_TIMEOUT)
335 {
336 ret = GATE_RESULT_TIMEOUT;
337 }
338 else if (wait_result != WAIT_OBJECT_0)
339 {
340 ret = GATE_RESULT_FAILED;
341 }
342 } while (0);
343 #endif
344 return ret;
345 }
346
347
348 static gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
349 {
350 gate_result_t ret = GATE_RESULT_FAILED;
351 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
352 #ifdef GATE_SYS_WINCE
353 ret = GATE_RESULT_NOTSUPPORTED;
354 #else
355 INPUT_RECORD input_record = GATE_INIT_EMPTY;
356 DWORD event_count = 0;
357 gate_char32_t chr;
358
359 do
360 {
361 ret = gate_console_await_char_impl(self, GATE_CONSOLE_TIMEOUT_NEVER);
362 GATE_BREAK_IF_FAILED(ret);
363
364 event_count = 0;
365 if (!ReadConsoleInput(ptr->read_handle, &input_record, 1, &event_count))
366 {
367 ret = GATE_RESULT_FAILED;
368 break;
369 }
370 if (input_record.EventType != KEY_EVENT)
371 {
372 ret = GATE_RESULT_FAILED;
373 break;
374 }
375 if (!input_record.Event.KeyEvent.bKeyDown)
376 {
377 ret = GATE_RESULT_FAILED;
378 break;
379 }
380
381 #if defined(GATE_WIN32_UNICODE)
382 chr = input_record.Event.KeyEvent.uChar.UnicodeChar;
383 #else
384 chr = input_record.Event.KeyEvent.uChar.AsciiChar;
385 #endif
386 if (chr == 0)
387 {
388 gate_keyboard_native_key_to_unicode(input_record.Event.KeyEvent.wVirtualKeyCode, &chr);
389 }
390 *ptr_char = chr;
391
392 } while (0);
393 #endif
394 return ret;
395 }
396 static gate_result_t gate_console_write_err_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
397 {
398 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
399 gate_result_t ret = GATE_RESULT_FAILED;
400 DWORD dwwrite = 0;
401 if (ptr->error_handle == NULL)
402 {
403 if (written != NULL)
404 {
405 *written = bufferlength;
406 }
407 ret = GATE_RESULT_OK;
408 }
409 else
410 {
411 if (FALSE == gate_win32_writefile(ptr->error_handle, buffer, (DWORD)bufferlength, &dwwrite, NULL))
412 {
413 GATE_DEBUG_TRACE("WriteFile() failed");
414 GATE_DEBUG_TRACE_VALUE(gate_win32_getlasterror());
415 gate_win32_print_lasterror(&ret, NULL, 0);
416 }
417 else
418 {
419 if (written != NULL)
420 {
421 *written = (gate_result_t)dwwrite;
422 }
423 ret = GATE_RESULT_OK;
424 }
425 }
426 return ret;
427 }
428 static gate_result_t gate_console_flush_err_impl(void* self)
429 {
430 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
431 gate_win32_flushfilebuffers(ptr->error_handle);
432 return GATE_RESULT_OK;
433 }
434
435 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
436 {
437 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
438 gate_result_t ret;
439 switch (resource_type)
440 {
441 case GATE_STREAM_RESOURCE_INPUT:
442 {
443 *resource = (gate_uintptr_t)ptr->read_handle;
444 ret = GATE_RESULT_OK;
445 break;
446 }
447 case GATE_STREAM_RESOURCE_OUTPUT:
448 {
449 *resource = (gate_uintptr_t)ptr->write_handle;
450 ret = GATE_RESULT_OK;
451 break;
452 }
453 case GATE_STREAM_RESOURCE_ERROR:
454 {
455 *resource = (gate_uintptr_t)ptr->error_handle;
456 ret = GATE_RESULT_OK;
457 break;
458 }
459 default:
460 {
461 ret = GATE_RESULT_NOTSUPPORTED;
462 break;
463 }
464 }
465 return ret;
466 }
467
468
469
470 static gate_console_impl_t gate_console_impl_instance = {
471 &gate_console_vtbl,
472
473 0,
474 0,
475 0
476 };
477
478
479 static gate_console_t* gate_console_constructor(void);
480
481 static gate_console_t* (*gate_console_function)(void) = &gate_console_constructor;
482
483 static gate_console_t* gate_console_default(void)
484 {
485 return (gate_console_t*)&gate_console_impl_instance;
486 }
487
488 #if defined(GATE_SYS_WINCE)
489
490 typedef FILE* (__cdecl* getstdfilex_func)(int);
491 typedef void* (__cdecl* fileno_func)(FILE*);
492
493 static getstdfilex_func getstdfilex_impl = NULL;
494 static fileno_func fileno_impl = NULL;
495
496 static gate_bool_t load_coredll_cfile_functions()
497 {
498 HMODULE hmod;
499
500 if ((getstdfilex_impl != NULL) && (fileno_impl != NULL))
501 {
502 return true;
503 }
504
505 hmod = gate_win32_get_ntdll_module();
506 if (hmod == NULL)
507 {
508 return false;
509 }
510 if (getstdfilex_impl == NULL)
511 {
512 if (!gate_win32_get_proc_address(hmod, "_getstdfilex", &getstdfilex_impl))
513 {
514 return false;
515 }
516 }
517
518 if (fileno_impl == NULL)
519 {
520 if (!gate_win32_get_proc_address(hmod, "_fileno", &fileno_impl))
521 {
522 return false;
523 }
524 }
525 return true;
526 }
527
528 static HANDLE open_ce_console()
529 {
530 static HANDLE hcon = INVALID_HANDLE_VALUE;
531 if (hcon == INVALID_HANDLE_VALUE)
532 {
533 hcon = CreateFile(_T("CON0:"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
534 }
535 return hcon;
536 }
537
538 #endif
539
540 static gate_console_t* gate_console_constructor(void)
541 {
542 #if defined(GATE_SYS_WINCE)
543 #if defined(GATE_COMPILER_MSVC)
544 gate_console_impl_instance.read_handle = fileno(stdin);
545 gate_console_impl_instance.write_handle = fileno(stdout);
546 gate_console_impl_instance.error_handle = fileno(stderr);
547 #else
548 if (!load_coredll_cfile_functions())
549 {
550 gate_console_impl_instance.read_handle = open_ce_console();
551 gate_console_impl_instance.write_handle = open_ce_console();
552 gate_console_impl_instance.error_handle = open_ce_console();
553 }
554 else
555 {
556 gate_console_impl_instance.read_handle = fileno_impl(getstdfilex_impl(0));
557 gate_console_impl_instance.write_handle = fileno_impl(getstdfilex_impl(1));
558 gate_console_impl_instance.error_handle = fileno_impl(getstdfilex_impl(2));
559 }
560 #endif
561 #else
562 gate_console_impl_instance.read_handle = GetStdHandle(STD_INPUT_HANDLE);
563 gate_console_impl_instance.write_handle = GetStdHandle(STD_OUTPUT_HANDLE);
564 gate_console_impl_instance.error_handle = GetStdHandle(STD_ERROR_HANDLE);
565 #endif
566 gate_init_console_vtbl();
567 gate_console_function = &gate_console_default;
568 return (gate_console_t*)&gate_console_impl_instance;
569 }
570
571
572 gate_console_t* gate_console()
573 {
574 return gate_console_function();
575 }
576
577 gate_result_t gate_console_read_key(gate_console_t* con,
578 gate_uint32_t timeout_ms,
579 gate_input_keycode_t* out_ptr_keycode,
580 gate_char32_t* out_ptr_chr,
581 char* out_buffer, gate_size_t* out_buffer_len)
582 {
583 gate_result_t result;
584 gate_char32_t chr = 0;
585 WORD vkey = 0;
586 gate_input_nativekeycode_t native_key;
587
588 result = gate_console_timed_read_char(con, timeout_ms, &chr);
589 if (GATE_SUCCEEDED(result))
590 {
591 result = gate_keyboard_unicode_to_native_key(chr, &native_key);
592 if (GATE_SUCCEEDED(result))
593 {
594 vkey = (WORD)native_key;
595 }
596 else
597 {
598 result = GATE_RESULT_OK;
599 }
600
601 if (out_ptr_keycode)
602 {
603 *out_ptr_keycode = 0;
604 if (vkey)
605 {
606 gate_keyboard_parse_native_key(vkey, out_ptr_keycode, NULL);
607 }
608 else
609 {
610 *out_ptr_keycode = keycode_from_char32(chr);
611 }
612 }
613
614 if (out_ptr_chr)
615 {
616 *out_ptr_chr = chr;
617 }
618
619 if (out_buffer && out_buffer_len)
620 {
621 *out_buffer_len = gate_char_write_utf8(chr, out_buffer, *out_buffer_len);
622 }
623 }
624 return result;
625 }
626
627
628 #endif /* GATE_CORE_CONSOLE_WINAPI */
629
630
631
632 #if defined(GATE_CORE_CONSOLE_POSIX)
633
634 #include "gate/platforms.h"
635 #if defined(GATE_SYS_BEOS)
636 # include <termios.h>
637 # include <sys/select.h>
638 #elif defined(GATE_SYS_DOS)
639 # include <io.h>
640 #else
641 # include <termios.h>
642 #endif
643
644 #if defined(GATE_SYS_BEOS)
645
646 gate_result_t gate_posix_await_read(int fd, gate_uint32_t timeout_ms)
647 {
648 fd_set read_set;
649 struct timeval timeout;
650
651 if (timeout_ms != 0xffffffff)
652 {
653 timeout.tv_sec = timeout_ms / 1000;
654 timeout.tv_usec = (timeout_ms % 1000) * 1000;
655 }
656 else
657 {
658 timeout.tv_sec = 0x7fffffff;
659 timeout.tv_usec = 0;
660 }
661
662 FD_ZERO(&read_set);
663 FD_SET(fd, &read_set);
664 if (-1 == select(fd + 1, &read_set, NULL, NULL, &timeout))
665 {
666 return GATE_RESULT_FAILED;
667 }
668
669 if (!FD_ISSET(fd, &read_set))
670 {
671 return GATE_RESULT_TIMEOUT;
672 }
673 else
674 {
675 return GATE_RESULT_OK;
676 }
677 }
678
679 #endif
680
681
682 typedef struct gate_console_class_impl
683 {
684 GATE_INTERFACE_VTBL(gate_console)* vtbl;
685
686 int read_handle;
687 int write_handle;
688 int error_handle;
689 } gate_console_impl_t;
690
691
692 56 static void gate_console_release_impl(void* self)
693 {
694 (void)self;
695 56 }
696 19 static int gate_console_retain_impl(void* self)
697 {
698 (void)self;
699 19 return 1;
700 }
701
702 4 gate_result_t gate_console_read_impl(void* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
703 {
704 4 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
705 4 ssize_t result = read(ptr->read_handle, buffer, (int)bufferlength);
706
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (result >= 0)
707 {
708
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (returned != NULL)
709 {
710 1 *returned = (gate_size_t)result;
711 }
712 4 return GATE_RESULT_OK;
713 }
714 else
715 {
716 ✗ return GATE_RESULT_FAILED;
717 }
718 }
719 1 gate_result_t gate_console_peek_impl(void* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
720 {
721 GATE_UNUSED_ARG(stream);
722 GATE_UNUSED_ARG(buffer);
723 GATE_UNUSED_ARG(bufferlength);
724 GATE_UNUSED_ARG(returned);
725 1 return GATE_RESULT_NOTSUPPORTED;
726 }
727 5706 gate_result_t gate_console_write_impl(void* stream, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
728 {
729 5706 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
730 5706 ssize_t result = write(ptr->write_handle, buffer, (int)bufferlength);
731
1/2
✓ Branch 0 taken 5706 times.
✗ Branch 1 not taken.
5706 if (result >= 0)
732 {
733
1/2
✓ Branch 0 taken 5706 times.
✗ Branch 1 not taken.
5706 if (written != NULL)
734 {
735 5706 *written = (gate_size_t)result;
736 }
737 5706 return GATE_RESULT_OK;
738 }
739 else
740 {
741 ✗ return GATE_RESULT_FAILED;
742 }
743 }
744 2 gate_result_t gate_console_flush_impl(void* stream)
745 {
746 2 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
747 2 fsync(ptr->error_handle);
748 2 return GATE_RESULT_OK;
749 }
750
751 #if defined(GATE_SYS_DOS)
752
753 static gate_result_t gate_console_await_and_read_char(void* self, gate_char32_t* ptr_char, gate_uint32_t timeout_ms)
754 {
755 GATE_UNUSED_ARG(self);
756 return gate_dos_await_and_read_kbd(ptr_char, timeout_ms);
757 }
758
759
760 #else /* GATE_SYS_DOS */
761
762 #if defined(GATE_SYS_WASM_WASI)
763 #else
764 # define GATE_CONSOLE_TERMIOS_SUPPORT 1
765 #endif
766
767 7 static gate_result_t gate_console_await_and_read_char(void* self, gate_char32_t* ptr_char, gate_uint32_t timeout_ms)
768 {
769 7 gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
770 7 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
771
772 #if defined(GATE_CONSOLE_TERMIOS_SUPPORT)
773 struct termios termios_attrs_orig;
774 7 gate_bool_t reset_termios_attrs = false;
775 #endif
776
777 do
778 {
779 7 char buffer[8] = { 0 };
780
781 #if defined(GATE_CONSOLE_TERMIOS_SUPPORT)
782 struct termios termios_attrs;
783
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (0 == tcgetattr(ptr->read_handle, &termios_attrs_orig))
784 {
785 //if(GATE_FLAG_ENABLED(termios_attrs_orig.c_lflag, ICANON))
786 {
787 ✗ gate_mem_copy(&termios_attrs, &termios_attrs_orig, sizeof(termios_attrs));
788 ✗ GATE_FLAG_SET(termios_attrs.c_lflag, ICANON, false);
789 ✗ GATE_FLAG_SET(termios_attrs.c_lflag, ECHO, false);
790 ✗ GATE_FLAG_SET(termios_attrs.c_lflag, ECHONL, false);
791 ✗ GATE_FLAG_SET(termios_attrs.c_lflag, ISIG, false);
792 ✗ GATE_FLAG_SET(termios_attrs.c_lflag, IEXTEN, false);
793 ✗ if (0 == tcsetattr(ptr->read_handle, TCSANOW, &termios_attrs))
794 {
795
796 ✗ reset_termios_attrs = true;
797 }
798 }
799 }
800 #endif
801
802 7 ret = gate_posix_await_read(ptr->read_handle, timeout_ms);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 GATE_BREAK_IF_FAILED(ret);
804
805
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (ptr_char)
806 {
807 /* read character */
808 3 ret = gate_console_read_impl(self, buffer, 1, NULL);
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
810 3 *ptr_char = (gate_char32_t)buffer[0];
811 }
812 else
813 {
814 /* only return available status */
815 4 ret = GATE_RESULT_OK;
816 }
817 } while (0);
818
819 #if defined(GATE_CONSOLE_TERMIOS_SUPPORT)
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (reset_termios_attrs)
821 {
822 ✗ tcsetattr(ptr->read_handle, TCSANOW, &termios_attrs_orig);
823 }
824 #endif
825
826 7 return ret;
827 }
828 #endif /* GATE_SYS_DOS */
829
830 4 gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
831 {
832 4 return gate_console_await_and_read_char(self, NULL, timeout_ms);
833 }
834
835 3 gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
836 {
837 3 return gate_console_await_and_read_char(self, ptr_char, GATE_CONSOLE_TIMEOUT_NEVER);
838 }
839
840
841 22 gate_result_t gate_console_write_err_impl(void* stream, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
842 {
843 22 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
844 22 ssize_t result = write(ptr->error_handle, buffer, (int)bufferlength);
845
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (result >= 0)
846 {
847
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (written != NULL)
848 {
849 22 *written = (gate_size_t)result;
850 }
851 22 return GATE_RESULT_OK;
852 }
853 else
854 {
855 ✗ return GATE_RESULT_FAILED;
856 }
857 }
858 2 gate_result_t gate_console_flush_err_impl(void* stream)
859 {
860 2 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
861 2 fsync(ptr->error_handle);
862 2 return GATE_RESULT_OK;
863 }
864 3 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
865 {
866 3 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
867 gate_result_t ret;
868
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 switch (resource_type)
869 {
870 1 case GATE_STREAM_RESOURCE_INPUT:
871 {
872 1 *resource = (gate_uintptr_t)ptr->read_handle;
873 1 ret = GATE_RESULT_OK;
874 1 break;
875 }
876 1 case GATE_STREAM_RESOURCE_OUTPUT:
877 {
878 1 *resource = (gate_uintptr_t)ptr->write_handle;
879 1 ret = GATE_RESULT_OK;
880 1 break;
881 }
882 1 case GATE_STREAM_RESOURCE_ERROR:
883 {
884 1 *resource = (gate_uintptr_t)ptr->error_handle;
885 1 ret = GATE_RESULT_OK;
886 1 break;
887 }
888 ✗ default:
889 {
890 ✗ ret = GATE_RESULT_NOTSUPPORTED;
891 ✗ break;
892 }
893 }
894 3 return ret;
895 }
896
897 static gate_console_impl_t gate_console_impl_instance = {
898 &gate_console_vtbl,
899
900 STDIN_FILENO,
901 STDOUT_FILENO,
902 STDERR_FILENO
903 };
904
905 96 gate_console_t* gate_console()
906 {
907 96 gate_init_console_vtbl();
908 96 return (gate_console_t*)&gate_console_impl_instance;
909 }
910
911
912 #if defined(GATE_SYS_DOS)
913
914 gate_result_t gate_console_read_key(gate_console_t* con,
915 gate_uint32_t timeout_ms,
916 gate_input_keycode_t* out_ptr_keycode,
917 gate_char32_t* out_ptr_chr,
918 char* out_buffer, gate_size_t* out_buffer_len)
919 {
920 gate_result_t ret;
921 char buffer[32];
922 gate_size_t buffer_len = 0;
923 gate_char32_t chr = 0;
924 gate_input_keycode_t key_code = 0;
925
926 do
927 {
928 ret = gate_console_timed_read_char(con, timeout_ms, &chr);
929 GATE_BREAK_IF_FAILED(ret);
930
931 buffer[buffer_len] = (char)chr;
932 ++buffer_len;
933
934 if (chr == 0x00)
935 {
936 /* BIOS extended codes: */
937 while (GATE_RESULT_OK == (ret = gate_console_timed_read_char(con, 1, &chr)))
938 {
939 buffer[buffer_len] = (char)chr;
940 ++buffer_len;
941
942 key_code = gate_dos_kbd_scan_code_to_input_code((unsigned)chr);
943 break;
944 }
945 }
946 else
947 {
948 if (out_ptr_keycode)
949 {
950 key_code = keycode_from_char32(chr);
951 }
952 }
953 if (out_ptr_keycode)
954 {
955 *out_ptr_keycode = key_code;
956 }
957 if (out_ptr_chr)
958 {
959 *out_ptr_chr = chr;
960 }
961 } while (0);
962
963 if (out_buffer && out_buffer_len)
964 {
965 if (buffer_len > *out_buffer_len)
966 {
967 buffer_len = *out_buffer_len;
968 }
969 gate_mem_copy(out_buffer, &buffer[0], buffer_len);
970 }
971
972 return ret;
973 }
974
975 #else /* GATE_SYS_DOS */
976
977 1 gate_result_t gate_console_read_key(gate_console_t* con,
978 gate_uint32_t timeout_ms,
979 gate_input_keycode_t* out_ptr_keycode,
980 gate_char32_t* out_ptr_chr,
981 char* out_buffer, gate_size_t* out_buffer_len)
982 {
983 gate_result_t ret;
984 char buffer[32];
985 1 gate_size_t buffer_len = 0;
986 1 gate_char32_t chr = 0;
987 gate_size_t parsed;
988 1 gate_input_keycode_t key_code = 0;
989 1 gate_input_keystates_t key_states = 0;
990
991 do
992 {
993 1 ret = gate_console_timed_read_char(con, timeout_ms, &chr);
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
995
996 1 buffer[buffer_len] = (char)chr;
997 1 ++buffer_len;
998
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (chr == 0x1b)
1000 {
1001 /* VT 100 escape code: */
1002 ✗ while (GATE_RESULT_OK == (ret = gate_console_timed_read_char(con, 1, &chr)))
1003 {
1004 ✗ buffer[buffer_len] = (char)chr;
1005 ✗ ++buffer_len;
1006
1007 ✗ key_code = 0;
1008 ✗ parsed = gate_keyboard_parse_vt100_symbols(buffer, buffer_len, &key_code, &key_states, &chr);
1009 ✗ if ((parsed != 0) && (key_code != 0))
1010 {
1011 ✗ break;
1012 }
1013 }
1014 }
1015 else
1016 {
1017
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (out_ptr_keycode)
1018 {
1019 1 key_code = keycode_from_char32(chr);
1020 }
1021 }
1022
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (out_ptr_keycode)
1023 {
1024 1 *out_ptr_keycode = key_code;
1025 }
1026
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (out_ptr_chr)
1027 {
1028 1 *out_ptr_chr = chr;
1029 }
1030 } while (0);
1031
1032
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (out_buffer && out_buffer_len)
1033 {
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (buffer_len > *out_buffer_len)
1035 {
1036 ✗ buffer_len = *out_buffer_len;
1037 }
1038
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (buffer_len > 0)
1039 {
1040 1 gate_mem_copy(out_buffer, &buffer[0], buffer_len);
1041 }
1042 }
1043
1044 1 return ret;
1045 }
1046
1047 #endif
1048
1049 #endif /* GATE_CORE_CONSOLE_POSIX */
1050
1051
1052
1053 #if defined(GATE_CORE_CONSOLE_EFI)
1054
1055 /***********************
1056 * EFI implementation *
1057 ************************/
1058
1059 #include "gate/platform/efi/efi_gate.h"
1060
1061 #include <efi.h>
1062
1063 typedef struct gate_console_class_impl
1064 {
1065 GATE_INTERFACE_VTBL(gate_console)* vtbl;
1066
1067 } gate_console_impl_t;
1068
1069 void gate_console_release_impl(void* self)
1070 {
1071 gate_console_impl_t* impl = (gate_console_impl_t*)self;
1072 (void)impl;
1073 }
1074 int gate_console_retain_impl(void* self)
1075 {
1076 gate_console_impl_t* impl = (gate_console_impl_t*)self;
1077 (void)impl;
1078 return 1;
1079 }
1080
1081 static gate_bool_t console_input_buffer_mode = true;
1082 static gate_bool_t console_input_echo_mode = true;
1083 static gate_char32_t console_input_buffer[256];
1084 static char console_buffer[1024];
1085 static gate_size_t console_buffer_length = 0;
1086 static gate_size_t console_buffer_offset = 0;
1087
1088 static gate_result_t gate_console_read_direct(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1089 {
1090 gate_console_impl_t* impl = (gate_console_impl_t*)self;
1091 gate_result_t ret = GATE_RESULT_OK;
1092 gate_char32_t chr32;
1093 gate_size_t bytes_received = 0;
1094 gate_size_t chars_converted;
1095
1096 while (bufferlength > 0)
1097 {
1098 ret = gate_console_read_char_impl(self, &chr32);
1099 if (GATE_FAILED(ret))
1100 {
1101 if (bytes_received > 0)
1102 {
1103 /* we have already received something -> just return that */
1104 ret = GATE_RESULT_OK;
1105 }
1106 break;
1107 }
1108
1109 gate_platform_efi_console_write_char((gate_char16_t)chr32);
1110
1111 chars_converted = gate_char_write_utf8(chr32, buffer, bufferlength);
1112 buffer += chars_converted;
1113 bufferlength -= chars_converted;
1114 bytes_received += chars_converted;
1115 if (/*(chr32 == 13) ||*/ (chr32 == 10))
1116 {
1117 break;
1118 }
1119 };
1120
1121 if (returned)
1122 {
1123 *returned = bytes_received;
1124 }
1125
1126 return ret;
1127 }
1128
1129 static gate_result_t gate_console_read_buffered(void* self)
1130 {
1131 gate_console_impl_t* impl = (gate_console_impl_t*)self;
1132 gate_result_t ret = GATE_RESULT_OK;
1133 gate_char32_t chr32;
1134 gate_size_t chars_converted;
1135
1136 do
1137 {
1138 if (console_buffer_length != 0)
1139 {
1140 /* there is already something in the buffer to be extracted */
1141 ret = GATE_RESULT_OK;
1142 break;
1143 }
1144
1145 console_buffer_offset = 0;
1146
1147 while (console_buffer_length < sizeof(console_buffer) - 8)
1148 {
1149 /* wait for next key */
1150 ret = gate_console_await_char_impl(self, 10);
1151 if (ret == GATE_RESULT_TIMEOUT)
1152 {
1153 continue;
1154 }
1155
1156 if (GATE_SUCCEEDED(ret))
1157 {
1158 /* extract next key */
1159 ret = gate_console_read_char_impl(self, &chr32);
1160 }
1161
1162 if (GATE_FAILED(ret))
1163 {
1164 /* error case */
1165 if (console_buffer_length > 0)
1166 {
1167 /* we have already received something -> just return that */
1168 ret = GATE_RESULT_OK;
1169 }
1170 break;
1171 }
1172
1173 if (chr32 == 8)
1174 {
1175 /* backspace */
1176 if (console_buffer_length > 0)
1177 {
1178 --console_buffer_length;
1179 if (console_input_echo_mode)
1180 {
1181 gate_platform_efi_console_write_char((gate_char16_t)8);
1182 }
1183 }
1184 continue;
1185 }
1186
1187 if (console_input_echo_mode)
1188 {
1189 gate_platform_efi_console_write_char((gate_char16_t)chr32);
1190 }
1191
1192 chars_converted = gate_char_write_utf8(chr32, &console_buffer[console_buffer_length],
1193 sizeof(console_buffer) - console_buffer_length);
1194 console_buffer_length += chars_converted;
1195
1196 if (chr32 == 10)
1197 {
1198 /* newline == ENTER */
1199 if (console_input_echo_mode)
1200 {
1201 gate_platform_efi_console_write_char(L'\r');
1202 }
1203 break;
1204 }
1205 };
1206
1207 } while (0);
1208
1209 return ret;
1210 }
1211
1212 gate_result_t gate_console_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1213 {
1214 gate_result_t ret;
1215 if (console_input_buffer_mode)
1216 {
1217 do
1218 {
1219 ret = gate_console_read_buffered(self);
1220 GATE_BREAK_IF_FAILED(ret);
1221
1222 if (bufferlength > console_buffer_length)
1223 {
1224 bufferlength = console_buffer_length;
1225 }
1226 gate_mem_copy(buffer, &console_buffer[console_buffer_offset], bufferlength);
1227 if (returned)
1228 {
1229 *returned = bufferlength;
1230 }
1231 ret = GATE_RESULT_OK;
1232
1233 console_buffer_length -= bufferlength;
1234 console_buffer_offset += bufferlength;
1235
1236 if (console_buffer_length == 0)
1237 {
1238 /* reset console buffer */
1239 gate_mem_clear(console_buffer, sizeof(console_buffer));
1240 console_buffer_offset = 0;
1241 }
1242 } while (0);
1243 }
1244 else
1245 {
1246 ret = gate_console_read_direct(self, buffer, bufferlength, returned);
1247 }
1248 return ret;
1249 }
1250 gate_result_t gate_console_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1251 {
1252 GATE_UNUSED_ARG(self);
1253 GATE_UNUSED_ARG(buffer);
1254 GATE_UNUSED_ARG(bufferlength);
1255 GATE_UNUSED_ARG(returned);
1256 return GATE_RESULT_NOTSUPPORTED;
1257 }
1258 gate_result_t gate_console_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1259 {
1260 gate_console_impl_t* impl = (gate_console_impl_t*)self;
1261 gate_result_t ret = GATE_RESULT_OK;
1262 gate_char32_t utf32_chr = 0;
1263 gate_char16_t utf16_chr[8] = { 0 };
1264 gate_size_t chars_read = 0;
1265 gate_size_t chars_converted;
1266 gate_size_t chars_written = 0;
1267 gate_size_t ndx;
1268
1269 while (bufferlength > 0)
1270 {
1271 chars_read = gate_char_read_utf8(buffer, bufferlength, &utf32_chr);
1272 if (chars_read == 0)
1273 {
1274 break;
1275 }
1276 buffer += chars_read;
1277 bufferlength -= chars_read;
1278 chars_converted = gate_char_write_utf16(utf32_chr, &utf16_chr[0], 4);
1279
1280 for (ndx = 0; ndx != chars_converted; ++ndx)
1281 {
1282 ret = gate_platform_efi_console_write_char(utf16_chr[ndx]);
1283 GATE_BREAK_IF_FAILED(ret);
1284 }
1285 GATE_BREAK_IF_FAILED(ret);
1286
1287 chars_written += chars_read;
1288 }
1289
1290 if (written)
1291 {
1292 *written = chars_written;
1293 }
1294 return ret;
1295 }
1296 gate_result_t gate_console_flush_impl(void* self)
1297 {
1298 GATE_UNUSED_ARG(self);
1299 return GATE_RESULT_OK;
1300 }
1301
1302 gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
1303 {
1304 return GATE_RESULT_NOTSUPPORTED;
1305 }
1306
1307
1308 gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
1309 {
1310 return gate_platform_efi_console_await_char(timeout_ms);
1311 }
1312
1313 gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
1314 {
1315 gate_result_t ret = GATE_RESULT_FAILED;
1316 gate_char16_t chr16;
1317
1318 do
1319 {
1320 ret = gate_platform_efi_console_read_char(&chr16, NULL, false);
1321 GATE_BREAK_IF_FAILED(ret);
1322
1323 if (ptr_char)
1324 {
1325 if (chr16 == 13)
1326 {
1327 *ptr_char = 10;
1328 }
1329 else
1330 {
1331 *ptr_char = (gate_char32_t)chr16;
1332 }
1333 }
1334 } while (0);
1335
1336 return ret;
1337 }
1338
1339
1340 gate_result_t gate_console_write_err_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1341 {
1342 return gate_console_write_impl(self, buffer, bufferlength, written);
1343 }
1344 gate_result_t gate_console_flush_err_impl(void* self)
1345 {
1346 return gate_console_flush_impl(self);
1347 }
1348
1349 gate_result_t gate_console_read_key(gate_console_t* con,
1350 gate_uint32_t timeout_ms,
1351 gate_input_keycode_t* out_ptr_keycode,
1352 gate_char32_t* out_ptr_chr,
1353 char* out_buffer, gate_size_t* out_buffer_len)
1354 {
1355 gate_result_t ret;
1356 gate_char16_t chr16 = 0;
1357 gate_uint16_t scan_code = 0;
1358
1359 do
1360 {
1361 ret = gate_console_await_char_impl(con, timeout_ms);
1362 GATE_BREAK_IF_FAILED(ret);
1363
1364 ret = gate_platform_efi_console_read_char(&chr16, &scan_code, false);
1365 GATE_BREAK_IF_FAILED(ret);
1366
1367 if (out_ptr_keycode)
1368 {
1369 *out_ptr_keycode = scan_code;
1370 }
1371 if (out_ptr_chr)
1372 {
1373 *out_ptr_chr = chr16;
1374 }
1375 if (out_buffer && out_buffer_len)
1376 {
1377 *out_buffer_len = gate_char_write_utf8(chr16, out_buffer, *out_buffer_len);
1378 }
1379 } while (0);
1380
1381 return ret;
1382 }
1383
1384
1385 static gate_console_impl_t gate_console_impl_instance = {
1386 &gate_console_vtbl
1387 };
1388
1389 gate_console_t* gate_console()
1390 {
1391 gate_init_console_vtbl();
1392 return (gate_console_t*)&gate_console_impl_instance;
1393 }
1394
1395 #endif /* GATE_CORE_CONSOLE_EFI */
1396
1397
1398 #if defined(GATE_CORE_CONSOLE_WASM_IMPL)
1399
1400
1401 #include "gate/platforms.h"
1402 #include "gate/platform/wasm/wasm_gate.h"
1403
1404 typedef struct gate_console_class_impl
1405 {
1406 GATE_INTERFACE_VTBL(gate_console)* vtbl;
1407
1408 gate_platform_stream_t read_handle;
1409 gate_platform_stream_t write_handle;
1410 gate_platform_stream_t error_handle;
1411 } gate_console_impl_t;
1412
1413
1414 static void gate_console_release_impl(void* self)
1415 {
1416 (void)self;
1417 }
1418 static int gate_console_retain_impl(void* self)
1419 {
1420 (void)self;
1421 return 1;
1422 }
1423
1424 gate_result_t gate_console_read_impl(void* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1425 {
1426 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
1427 return gate_platform_stream_read(ptr->read_handle, buffer, bufferlength, returned);
1428 }
1429 gate_result_t gate_console_peek_impl(void* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1430 {
1431 (void)stream;
1432 (void)buffer;
1433 (void)bufferlength;
1434 (void)returned;
1435 return GATE_RESULT_NOTSUPPORTED;
1436 }
1437 gate_result_t gate_console_write_impl(void* stream, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1438 {
1439 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
1440 return gate_platform_stream_write(ptr->write_handle, buffer, bufferlength, written);
1441 }
1442 gate_result_t gate_console_flush_impl(void* stream)
1443 {
1444 (void)stream;
1445 return GATE_RESULT_OK;
1446 }
1447 gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
1448 {
1449 gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
1450 return ret;
1451 }
1452 gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
1453 {
1454 gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
1455 return ret;
1456 }
1457
1458 gate_result_t gate_console_write_err_impl(void* stream, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1459 {
1460 gate_console_impl_t* ptr = (gate_console_impl_t*)stream;
1461 return gate_platform_stream_write(ptr->error_handle, buffer, bufferlength, written);
1462 }
1463 gate_result_t gate_console_flush_err_impl(void* stream)
1464 {
1465 (void)stream;
1466 return GATE_RESULT_OK;
1467 }
1468 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
1469 {
1470 gate_console_impl_t* ptr = (gate_console_impl_t*)self;
1471 gate_result_t ret;
1472 switch (resource_type)
1473 {
1474 case GATE_STREAM_RESOURCE_INPUT:
1475 {
1476 *resource = (gate_uintptr_t)ptr->read_handle;
1477 ret = GATE_RESULT_OK;
1478 break;
1479 }
1480 case GATE_STREAM_RESOURCE_OUTPUT:
1481 {
1482 *resource = (gate_uintptr_t)ptr->write_handle;
1483 ret = GATE_RESULT_OK;
1484 break;
1485 }
1486 case GATE_STREAM_RESOURCE_ERROR:
1487 {
1488 *resource = (gate_uintptr_t)ptr->error_handle;
1489 ret = GATE_RESULT_OK;
1490 break;
1491 }
1492 default:
1493 {
1494 ret = GATE_RESULT_NOTSUPPORTED;
1495 break;
1496 }
1497 }
1498 return ret;
1499 }
1500
1501 gate_result_t gate_console_read_key(gate_console_t* con,
1502 gate_uint32_t timeout_ms,
1503 gate_input_keycode_t* out_ptr_keycode,
1504 gate_char32_t* out_ptr_chr,
1505 char* out_buffer, gate_size_t* out_buffer_len)
1506 {
1507 gate_result_t result;
1508 gate_char32_t chr32 = 0;
1509 gate_input_keycode_t keycode = 0;
1510
1511 do
1512 {
1513 result = gate_console_await_char(con, timeout_ms);
1514 GATE_BREAK_IF_FAILED(result);
1515
1516 result = gate_console_read_char(con, &chr32);
1517 GATE_BREAK_IF_FAILED(result);
1518 } while (0);
1519
1520 if (out_ptr_chr)
1521 {
1522 *out_ptr_chr = chr32;
1523 }
1524 if (out_ptr_keycode)
1525 {
1526 *out_ptr_keycode = keycode;
1527 }
1528 if (out_buffer && out_buffer_len)
1529 {
1530 *out_buffer_len = 0;
1531 }
1532
1533 return result;
1534 }
1535
1536 static gate_console_impl_t gate_console_impl_instance = {
1537 &gate_console_vtbl,
1538
1539 (gate_platform_stream_t)(gate_intptr_t)0,
1540 (gate_platform_stream_t)(gate_intptr_t)1,
1541 (gate_platform_stream_t)(gate_intptr_t)2
1542 };
1543
1544 gate_console_t* gate_console()
1545 {
1546 gate_init_console_vtbl();
1547 gate_wasm_console_init();
1548 return (gate_console_t*)&gate_console_impl_instance;
1549 }
1550
1551 #endif /* GATE_CORE_CONSOLE_WASM_IMPL */
1552
1553
1554
1555 #if defined(GATE_CORE_CONSOLE_ARDUINO)
1556
1557 #include "gate/platform/arduino/arduino_gate.h"
1558
1559 #ifndef GATE_ARDUINO_CONSOLE_INPUT_BUFFER_LENGTH
1560 #define GATE_ARDUINO_CONSOLE_INPUT_BUFFER_LENGTH 8
1561 #endif
1562
1563 typedef struct gate_console_class_impl
1564 {
1565 GATE_INTERFACE_VTBL(gate_console)* vtbl;
1566
1567 unsigned char read_buffer[GATE_ARDUINO_CONSOLE_INPUT_BUFFER_LENGTH];
1568 unsigned int read_buffer_used;
1569 } gate_console_impl_t;
1570
1571 static gate_console_impl_t gate_console_impl_instance = {
1572 &gate_console_vtbl
1573 };
1574
1575 static gate_result_t gate_console_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1576 {
1577 gate_console_impl_t* const impl = (gate_console_impl_t*)self;
1578
1579 if (impl->read_buffer_used > 0)
1580 {
1581 /* copy from read buffer */
1582 gate_size_t remain;
1583 if (bufferlength > impl->read_buffer_used)
1584 {
1585 bufferlength = impl->read_buffer_used;
1586 }
1587 gate_mem_copy(buffer, &impl->read_buffer[0], bufferlength);
1588 remain = impl->read_buffer_used - bufferlength;
1589 if (remain > 0)
1590 {
1591 gate_mem_move(&impl->read_buffer[0], &impl->read_buffer[impl->read_buffer_used], remain);
1592 }
1593 impl->read_buffer_used = remain;
1594 if (returned)
1595 {
1596 *returned = bufferlength;
1597 }
1598 }
1599 else
1600 {
1601 /* direct read */
1602 gate_size_t total_received = 0;
1603 while (total_received < bufferlength)
1604 {
1605 if (!gate_arduino_serial_avail())
1606 {
1607 if (total_received > 0)
1608 {
1609 /* we have something, cancel allowed */
1610 break;
1611 }
1612 }
1613 else
1614 {
1615 int const byte_received = gate_arduino_serial_read();
1616 *buffer = (char)(unsigned char)byte_received;
1617 ++buffer;
1618 ++total_received;
1619 }
1620 }
1621 if (returned)
1622 {
1623 *returned = total_received;
1624 }
1625 }
1626 return GATE_RESULT_OK;
1627 }
1628
1629 static gate_result_t gate_console_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1630 {
1631 gate_console_impl_t* const impl = (gate_console_impl_t*)self;
1632
1633 while (impl->read_buffer_used < sizeof(impl->read_buffer))
1634 {
1635 if (!gate_arduino_serial_avail())
1636 {
1637 break;
1638 }
1639 else
1640 {
1641 int const received_byte = gate_arduino_serial_read();
1642 impl->read_buffer[impl->read_buffer_used] = (unsigned char)received_byte;
1643 ++impl->read_buffer_used;
1644 }
1645 }
1646 if ((buffer == NULL) && (bufferlength == 0))
1647 {
1648 if (returned)
1649 {
1650 *returned = impl->read_buffer_used;
1651 }
1652 return GATE_RESULT_BUFFERTOOSMALL;
1653 }
1654 if (bufferlength > impl->read_buffer_used)
1655 {
1656 bufferlength = impl->read_buffer_used;
1657 }
1658 if (bufferlength > 0)
1659 {
1660 gate_mem_copy(buffer, &impl->read_buffer[0], bufferlength);
1661 }
1662 if (returned)
1663 {
1664 *returned = bufferlength;
1665 }
1666 return GATE_RESULT_OK;
1667 }
1668
1669 static gate_result_t gate_console_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1670 {
1671 gate_size_t sent = 0;
1672 for (sent = 0; sent < bufferlength; ++sent)
1673 {
1674 int const chr = (int)(unsigned char)buffer[sent];
1675 gate_arduino_serial_write(chr);
1676 }
1677 if (written != NULL)
1678 {
1679 *written = sent;
1680 }
1681 return GATE_RESULT_OK;
1682 }
1683
1684 static gate_result_t gate_console_flush_impl(void* self)
1685 {
1686 GATE_UNUSED_ARG(self);
1687 return GATE_RESULT_OK;
1688 }
1689
1690 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
1691 {
1692 GATE_UNUSED_ARG(self);
1693 GATE_UNUSED_ARG(resource_type);
1694 GATE_UNUSED_ARG(resource);
1695 return GATE_RESULT_NOTIMPLEMENTED;
1696 }
1697
1698 static gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
1699 {
1700 gate_console_impl_t* const impl = (gate_console_impl_t*)self;
1701 if (impl->read_buffer_used > 0)
1702 {
1703 return GATE_RESULT_OK;
1704 }
1705 else
1706 {
1707 gate_uint32_t const start = gate_arduino_millisecond_timecounter();
1708 while (!gate_arduino_serial_avail())
1709 {
1710 gate_uint32_t const now = gate_arduino_millisecond_timecounter();
1711 if (now - start > timeout_ms)
1712 {
1713 return GATE_RESULT_TIMEOUT;
1714 }
1715 }
1716 return GATE_RESULT_OK;
1717 }
1718 }
1719
1720 static gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
1721 {
1722 int chr;
1723 while(!gate_arduino_serial_avail())
1724 {
1725 gate_arduino_idle();
1726 }
1727 chr = gate_arduino_serial_read();
1728 if (ptr_char)
1729 {
1730 *ptr_char = (gate_char32_t)(unsigned char)chr;
1731 }
1732 return GATE_RESULT_OK;
1733 }
1734
1735 static gate_result_t gate_console_write_err_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1736 {
1737 return gate_console_write_impl(self, buffer, bufferlength, written);
1738 }
1739
1740 static gate_result_t gate_console_flush_err_impl(void* self)
1741 {
1742 return gate_console_flush_impl(self);
1743 }
1744
1745 static void gate_console_release_impl(void* self)
1746 {
1747 GATE_UNUSED_ARG(self);
1748 }
1749
1750 static int gate_console_retain_impl(void* self)
1751 {
1752 GATE_UNUSED_ARG(self);
1753 return 0;
1754 }
1755
1756 gate_result_t gate_console_read_key(gate_console_t* con,
1757 gate_uint32_t timeout_ms,
1758 gate_input_keycode_t* out_ptr_keycode,
1759 gate_char32_t* out_ptr_chr,
1760 char* out_buffer, gate_size_t* out_buffer_len)
1761 {
1762 gate_char32_t chr32 = 0;
1763 gate_result_t ret = gate_console_await_char_impl(con, timeout_ms);
1764 GATE_RETURN_IF_FAILED(ret);
1765
1766 ret = gate_console_read_char_impl(con, &chr32);
1767 GATE_RETURN_IF_FAILED(ret);
1768
1769 if (out_ptr_chr)
1770 {
1771 *out_ptr_chr = chr32;
1772 }
1773 if (out_ptr_keycode)
1774 {
1775 gate_input_nativekeycode_t native_key = 0;
1776 gate_input_keystates_t key_state = 0;
1777 gate_keyboard_unicode_to_native_key(chr32, &native_key);
1778 gate_keyboard_parse_native_key(native_key, out_ptr_keycode, &key_state);
1779 }
1780 if (out_buffer && out_buffer_len)
1781 {
1782 *out_buffer_len = gate_char_write_utf8(chr32, out_buffer, *out_buffer_len);
1783 }
1784 return GATE_RESULT_OK;
1785 }
1786
1787 gate_console_t* gate_console()
1788 {
1789 gate_arduino_serial_init(0);
1790 gate_init_console_vtbl();
1791 return (gate_console_t*)&gate_console_impl_instance;
1792 }
1793
1794 #endif /* GATE_CORE_CONSOLE_ARDUINO */
1795
1796
1797
1798
1799 #if defined(GATE_CORE_CONSOLE_NOIMPL)
1800
1801 typedef struct gate_console_class_impl
1802 {
1803 GATE_INTERFACE_VTBL(gate_console)* vtbl;
1804 } gate_console_impl_t;
1805
1806 static gate_console_impl_t gate_console_impl_instance = {
1807 &gate_console_vtbl
1808 };
1809
1810 static gate_result_t gate_console_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1811 {
1812 GATE_UNUSED_ARG(self);
1813 GATE_UNUSED_ARG(buffer);
1814 GATE_UNUSED_ARG(bufferlength);
1815 GATE_UNUSED_ARG(returned);
1816 return GATE_RESULT_NOTIMPLEMENTED;
1817 }
1818 static gate_result_t gate_console_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1819 {
1820 GATE_UNUSED_ARG(self);
1821 GATE_UNUSED_ARG(buffer);
1822 GATE_UNUSED_ARG(bufferlength);
1823 GATE_UNUSED_ARG(returned);
1824 return GATE_RESULT_NOTIMPLEMENTED;
1825 }
1826 static gate_result_t gate_console_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1827 {
1828 GATE_UNUSED_ARG(self);
1829 GATE_UNUSED_ARG(buffer);
1830 GATE_UNUSED_ARG(bufferlength);
1831 GATE_UNUSED_ARG(written);
1832 return GATE_RESULT_NOTIMPLEMENTED;
1833 }
1834 static gate_result_t gate_console_flush_impl(void* self)
1835 {
1836 GATE_UNUSED_ARG(self);
1837 return GATE_RESULT_NOTIMPLEMENTED;
1838 }
1839
1840 static gate_result_t gate_console_get_resource_impl(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource)
1841 {
1842 GATE_UNUSED_ARG(self);
1843 GATE_UNUSED_ARG(resource_type);
1844 GATE_UNUSED_ARG(resource);
1845 return GATE_RESULT_NOTIMPLEMENTED;
1846 }
1847
1848 static gate_result_t gate_console_await_char_impl(void* self, gate_uint32_t timeout_ms)
1849 {
1850 GATE_UNUSED_ARG(self);
1851 GATE_UNUSED_ARG(timeout_ms);
1852 return GATE_RESULT_NOTIMPLEMENTED;
1853 }
1854 static gate_result_t gate_console_read_char_impl(void* self, gate_char32_t* ptr_char)
1855 {
1856 GATE_UNUSED_ARG(self);
1857 GATE_UNUSED_ARG(ptr_char);
1858 return GATE_RESULT_NOTIMPLEMENTED;
1859 }
1860 static gate_result_t gate_console_write_err_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1861 {
1862 GATE_UNUSED_ARG(self);
1863 GATE_UNUSED_ARG(buffer);
1864 GATE_UNUSED_ARG(bufferlength);
1865 GATE_UNUSED_ARG(written);
1866 return GATE_RESULT_NOTIMPLEMENTED;
1867 }
1868 static gate_result_t gate_console_flush_err_impl(void* self)
1869 {
1870 GATE_UNUSED_ARG(self);
1871 return GATE_RESULT_NOTIMPLEMENTED;
1872 }
1873 static void gate_console_release_impl(void* self)
1874 {
1875 GATE_UNUSED_ARG(self);
1876
1877 }
1878 static int gate_console_retain_impl(void* self)
1879 {
1880 GATE_UNUSED_ARG(self);
1881 return 0;
1882 }
1883
1884 gate_result_t gate_console_read_key(gate_console_t* con,
1885 gate_uint32_t timeout_ms,
1886 gate_input_keycode_t* out_ptr_keycode,
1887 gate_char32_t* out_ptr_chr,
1888 char* out_buffer, gate_size_t* out_buffer_len)
1889 {
1890 GATE_UNUSED_ARG(con);
1891 GATE_UNUSED_ARG(timeout_ms);
1892 GATE_UNUSED_ARG(out_ptr_keycode);
1893 GATE_UNUSED_ARG(out_ptr_chr);
1894 GATE_UNUSED_ARG(out_buffer);
1895 GATE_UNUSED_ARG(out_buffer_len);
1896 return GATE_RESULT_NOTIMPLEMENTED;
1897 }
1898
1899 gate_console_t* gate_console()
1900 {
1901 gate_init_console_vtbl();
1902 return (gate_console_t*)&gate_console_impl_instance;
1903 }
1904
1905 #endif /* GATE_CORE_CONSOLE_NOIMPL */
1906
1907
1908
1909 /****************************
1910 * Generic implementation *
1911 ****************************/
1912
1913 1 gate_stream_t* gate_console_stream()
1914 {
1915 1 return (gate_stream_t*)gate_console();
1916 }
1917
1918 18 static void gate_console_error_release_impl(void* self)
1919 {
1920 GATE_UNUSED_ARG(self);
1921 18 }
1922 1 static int gate_console_error_retain_impl(void* self)
1923 {
1924 GATE_UNUSED_ARG(self);
1925 1 return 1;
1926 }
1927 1 static char const* gate_console_error_interface_name_impl(void* self)
1928 {
1929 GATE_UNUSED_ARG(self);
1930 1 return GATE_INTERFACE_NAME_STREAM;
1931 }
1932 1 static gate_result_t gate_console_error_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1933 {
1934 GATE_UNUSED_ARG(self);
1935 GATE_UNUSED_ARG(buffer);
1936 GATE_UNUSED_ARG(bufferlength);
1937 GATE_UNUSED_ARG(returned);
1938 1 return GATE_RESULT_NOTSUPPORTED;
1939 }
1940 1 static gate_result_t gate_console_error_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
1941 {
1942 GATE_UNUSED_ARG(self);
1943 GATE_UNUSED_ARG(buffer);
1944 GATE_UNUSED_ARG(bufferlength);
1945 GATE_UNUSED_ARG(returned);
1946 1 return GATE_RESULT_NOTSUPPORTED;
1947 }
1948 11 static gate_result_t gate_console_error_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1949 {
1950 11 gate_console_t* con = gate_console();
1951 GATE_UNUSED_ARG(self);
1952 11 return con->vtbl->write_err(con, buffer, bufferlength, written);
1953 }
1954 1 static gate_result_t gate_console_error_flush_impl(void* self)
1955 {
1956 1 gate_console_t* con = gate_console();
1957 GATE_UNUSED_ARG(self);
1958 1 return con->vtbl->flush_err(con);
1959 }
1960
1961 typedef struct
1962 {
1963 GATE_INTERFACE_VTBL(gate_stream) const* vtbl;
1964 } gate_console_error_stream_impl_t;
1965
1966 static gate_console_error_stream_impl_t gate_console_error_stream_impl;
1967
1968 21 gate_stream_t* gate_console_error_stream()
1969 {
1970 static GATE_INTERFACE_VTBL(gate_stream) gate_console_error_stream_vtbl_impl;
1971
1972
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 if (!gate_console_error_stream_impl.vtbl)
1973 {
1974 19 gate_console_error_stream_vtbl_impl.get_interface_name = &gate_console_error_interface_name_impl;
1975 19 gate_console_error_stream_vtbl_impl.release = &gate_console_error_release_impl;
1976 19 gate_console_error_stream_vtbl_impl.retain = &gate_console_error_retain_impl;
1977 19 gate_console_error_stream_vtbl_impl.read = &gate_console_error_read_impl;
1978 19 gate_console_error_stream_vtbl_impl.peek = &gate_console_error_peek_impl;
1979 19 gate_console_error_stream_vtbl_impl.write = &gate_console_error_write_impl;
1980 19 gate_console_error_stream_vtbl_impl.flush = &gate_console_error_flush_impl;
1981
1982 19 gate_console_error_stream_impl.vtbl = &gate_console_error_stream_vtbl_impl;
1983 }
1984 21 return (gate_stream_t*)&gate_console_error_stream_impl;
1985 }
1986
1987 9 gate_result_t gate_console_write_err(gate_console_t* con, char const* buffer, gate_size_t bufferlen)
1988 {
1989 9 gate_size_t lenwritten = 0;
1990 9 return con->vtbl->write_err(con, buffer, bufferlen, &lenwritten);
1991 }
1992 2 gate_result_t gate_console_print_err(gate_console_t* con, char const* buffer)
1993 {
1994 2 return gate_console_write_err(con, buffer, gate_str_length(buffer));
1995 }
1996 1 gate_result_t gate_console_print_err_num(gate_console_t* con, gate_int64_t num)
1997 {
1998 char buffer[128];
1999 1 gate_size_t len = gate_str_print_int64(buffer, sizeof(buffer), num);
2000 1 return gate_console_write_err(con, buffer, len);
2001 }
2002
2003 1 gate_result_t gate_console_println_err(gate_console_t* con, char const* buffer)
2004 {
2005 gate_result_t ret;
2006 1 ret = gate_console_print_err(con, buffer);
2007
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
2008 {
2009 1 ret = gate_console_write_err(con, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
2010 }
2011 1 return ret;
2012 }
2013
2014 1 gate_result_t gate_console_readln(gate_console_t* con, gate_string_t* line)
2015 {
2016 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
2017 1 gate_size_t retrieved = 0;
2018 1 gate_result_t result = gate_stream_read(con, &buffer[0], sizeof(buffer), &retrieved);
2019
2020
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
2021 {
2022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_string_create(line, buffer, retrieved))
2023 {
2024 ✗ result = GATE_RESULT_OUTOFMEMORY;
2025 }
2026 }
2027 1 return result;
2028 }
2029
2030 1 gate_result_t gate_console_readln_raw(gate_console_t* con, gate_string_t* line, gate_uint32_t timeout_ms)
2031 {
2032 1 gate_result_t ret = GATE_RESULT_FAILED;
2033 gate_char32_t chr32;
2034 char utf8_chars[16];
2035 gate_size_t utf8_chars_used;
2036 gate_strbuilder_t builder;
2037 1 gate_timecounter_t timeout_timepoint = 0;
2038 gate_timecounter_t now;
2039 1 gate_uint32_t next_timeout_ms = GATE_CONSOLE_TIMEOUT_NEVER;
2040 gate_int64_t diff;
2041
2042 do
2043 {
2044
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (timeout_ms != GATE_CONSOLE_TIMEOUT_NEVER)
2045 {
2046 1 ret = gate_timecounter_now(&timeout_timepoint);
2047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
2048 1 timeout_timepoint = gate_timecounter_add(timeout_timepoint, (gate_int64_t)timeout_ms * 1000);
2049 }
2050 else
2051 {
2052 /* we have no timeout */
2053 ✗ next_timeout_ms = GATE_CONSOLE_TIMEOUT_NEVER;
2054 }
2055
2056 1 gate_strbuilder_create(&builder, 16);
2057
2058 for (;;)
2059 {
2060
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (timeout_ms != GATE_CONSOLE_TIMEOUT_NEVER)
2061 {
2062 /* calculate next character max timeout */
2063 2 gate_timecounter_now(&now);
2064 2 diff = gate_timecounter_diff(timeout_timepoint, now);
2065
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (diff < 0)
2066 {
2067 /* timeout reached */
2068 1 break;
2069 }
2070 else
2071 {
2072 1 next_timeout_ms = (gate_uint32_t)(diff / 1000);
2073 }
2074 }
2075
2076 1 ret = gate_console_timed_read_char(con, next_timeout_ms, &chr32);
2077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
2078 1 utf8_chars_used = gate_char_write_utf8(chr32, utf8_chars, sizeof(utf8_chars));
2079 1 gate_strbuilder_append_text(&builder, utf8_chars, utf8_chars_used);
2080
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((chr32 == 13) || (chr32 == 10))
2081 {
2082 break;
2083 }
2084 }
2085
2086 1 ret = GATE_RESULT_OK;
2087
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (line != NULL)
2088 {
2089
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (gate_strbuilder_length(&builder) > 0)
2090 {
2091
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_strbuilder_to_string(&builder, line))
2092 {
2093 ✗ ret = GATE_RESULT_OUTOFMEMORY;
2094 }
2095 }
2096 else
2097 {
2098 ✗ gate_string_create_empty(line);
2099 }
2100 }
2101 } while (0);
2102
2103 1 gate_strbuilder_release(&builder);
2104 1 return ret;
2105 }
2106
2107 3 gate_result_t gate_console_timed_read_char(gate_console_t* con, gate_uint32_t timeout_ms, gate_char32_t* ptr_chr)
2108 {
2109 3 gate_result_t result = gate_console_await_char(con, timeout_ms);
2110
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (GATE_SUCCEEDED(result))
2111 {
2112 3 result = gate_console_read_char(con, ptr_chr);
2113 }
2114 3 return result;
2115 }
2116