terminal.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Terminal: prompt, busy indicator, blinking cursor, echo loop. */
  2. #include "io.h"
  3. #include "terminal.h"
  4. #include "timer.h"
  5. static unsigned int busy_idx;
  6. static unsigned int last_busy_tick;
  7. static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
  8. static int cursor_visible;
  9. static unsigned int last_cursor_tick;
  10. static void update_busy(void)
  11. {
  12. unsigned int t = get_ticks_ms();
  13. if (t - last_busy_tick >= BUSY_MS) {
  14. last_busy_tick = t;
  15. busy_idx = (busy_idx + 1) % 4;
  16. }
  17. io_vga_write(IO_BUSY_POS, (unsigned short)((0x0A << 8) | busy_chars[busy_idx]));
  18. }
  19. static void update_cursor_blink(void)
  20. {
  21. unsigned int t = get_ticks_ms();
  22. if (t - last_cursor_tick >= CURSOR_MS) {
  23. last_cursor_tick = t;
  24. cursor_visible = !cursor_visible;
  25. }
  26. if (cursor_visible)
  27. io_cursor_show_block();
  28. else
  29. io_cursor_hide_block();
  30. }
  31. static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
  32. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  33. void terminal_run(void)
  34. {
  35. last_cursor_tick = get_ticks_ms();
  36. last_busy_tick = get_ticks_ms();
  37. cursor_visible = 1;
  38. busy_idx = 0;
  39. io_putstr(msg);
  40. io_putstr(prompt);
  41. for (;;) {
  42. update_busy();
  43. update_cursor_blink();
  44. {
  45. char c = io_poll_key();
  46. if (c) {
  47. io_putchar(c);
  48. if (c == '\n')
  49. io_putstr(prompt);
  50. } else {
  51. __asm__ volatile ("hlt");
  52. }
  53. }
  54. }
  55. }