terminal.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /* Single-producer (WM) / single-consumer (terminal) key queue. */
  32. static inline void term_key_push(struct term *t, int k)
  33. {
  34. unsigned int next = (t->key_head + 1) % KEY_BUF_SIZE;
  35. if (next != t->key_tail) {
  36. t->keys[t->key_head] = k;
  37. t->key_head = next;
  38. }
  39. }
  40. static inline int term_key_pop(struct term *t)
  41. {
  42. int k;
  43. if (t->key_tail == t->key_head)
  44. return 0;
  45. k = t->keys[t->key_tail];
  46. t->key_tail = (t->key_tail + 1) % KEY_BUF_SIZE;
  47. return k;
  48. }
  49. #endif