terminal.c 907 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Terminal: prompt, blinking cursor, echo loop. */
  2. #include "io.h"
  3. #include "terminal.h"
  4. #include "timer.h"
  5. static int cursor_visible;
  6. static unsigned int last_cursor_tick;
  7. static void update_cursor_blink(void)
  8. {
  9. unsigned int t = get_ticks_ms();
  10. if (t - last_cursor_tick >= CURSOR_MS) {
  11. last_cursor_tick = t;
  12. cursor_visible = !cursor_visible;
  13. }
  14. if (cursor_visible)
  15. io_cursor_show_block();
  16. else
  17. io_cursor_hide_block();
  18. }
  19. static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
  20. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  21. void terminal_run(void)
  22. {
  23. last_cursor_tick = get_ticks_ms();
  24. cursor_visible = 1;
  25. io_putstr(msg);
  26. io_putstr(prompt);
  27. for (;;) {
  28. update_cursor_blink();
  29. {
  30. char c = io_poll_key();
  31. if (c) {
  32. io_putchar(c);
  33. if (c == '\n')
  34. io_putstr(prompt);
  35. } else {
  36. __asm__ volatile ("hlt");
  37. }
  38. }
  39. }
  40. }