terminal.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Terminal-in-a-window: per-instance state and the prompt/echo loop. */
  2. #ifndef TERMINAL_H
  3. #define TERMINAL_H
  4. #include "screen.h"
  5. #include "vfs.h"
  6. #define CURSOR_MS 500
  7. #define LINE_MAX 64
  8. #define HISTORY_SIZE 32
  9. #define KEY_BUF_SIZE 16
  10. struct term {
  11. struct text_screen screen;
  12. char history[HISTORY_SIZE][LINE_MAX];
  13. unsigned int history_count;
  14. unsigned int history_next;
  15. char line[LINE_MAX];
  16. unsigned int line_len;
  17. unsigned int line_pos;
  18. unsigned int line_start;
  19. unsigned int history_browse;
  20. int fg_pid; /* foreground child pid, or -1 when none */
  21. char cwd[VFS_PATH_MAX]; /* absolute current working directory */
  22. char path[VFS_PATH_MAX]; /* command search path (colon separated) */
  23. /* Per-terminal key queue: WM (slot 0) pushes, terminal process pops. */
  24. int keys[KEY_BUF_SIZE];
  25. unsigned int key_head;
  26. unsigned int key_tail;
  27. };
  28. /* Entry point for a terminal process (arg = struct term *). */
  29. void term_entry(void *arg);
  30. void term_run(struct term *t);
  31. /* Ring-3 terminal: run a privileged kernel command as a kernel process with
  32. * output on `tty` (the command table and handlers stay in terminal.c). */
  33. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty);
  34. /* Single-producer (WM) / single-consumer (terminal) key queue. */
  35. static inline void term_key_push(struct term *t, int k)
  36. {
  37. unsigned int next = (t->key_head + 1) % KEY_BUF_SIZE;
  38. if (next != t->key_tail) {
  39. t->keys[t->key_head] = k;
  40. t->key_head = next;
  41. }
  42. }
  43. static inline int term_key_pop(struct term *t)
  44. {
  45. int k;
  46. if (t->key_tail == t->key_head)
  47. return 0;
  48. k = t->keys[t->key_tail];
  49. t->key_tail = (t->key_tail + 1) % KEY_BUF_SIZE;
  50. return k;
  51. }
  52. #endif