tty.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <stdint.h>
  3. #include <sys/types.h>
  4. #include <termios.h>
  5. #include <types/allocator.hpp>
  6. #include <types/buffer.hpp>
  7. #include <types/cplusplus.hpp>
  8. #include <kernel/async/waitlist.hpp>
  9. #include <kernel/async/lock.hpp>
  10. class tty : public types::non_copyable {
  11. public:
  12. static constexpr size_t BUFFER_SIZE = 4096;
  13. static constexpr size_t NAME_SIZE = 32;
  14. private:
  15. void _real_commit_char(int c);
  16. void _echo_char(int c);
  17. int _do_erase(bool should_echo);
  18. public:
  19. tty();
  20. virtual void putchar(char c) = 0;
  21. void print(const char* str);
  22. ssize_t read(char* buf, size_t buf_size, size_t n);
  23. ssize_t write(const char* buf, size_t n);
  24. // characters committed to buffer will be handled
  25. // by the input line discipline (N_TTY)
  26. void commit_char(int c);
  27. // print character to the output
  28. // characters will be handled by the output line discipline
  29. void show_char(int c);
  30. void clear_read_buf(void);
  31. // TODO: formal poll support
  32. int poll();
  33. constexpr void set_pgrp(pid_t pgid)
  34. {
  35. fg_pgroup = pgid;
  36. }
  37. constexpr pid_t get_pgrp(void) const
  38. {
  39. return fg_pgroup;
  40. }
  41. char name[NAME_SIZE];
  42. termios termio;
  43. protected:
  44. kernel::async::mutex mtx_buf;
  45. types::buffer buf;
  46. kernel::async::wait_list waitlist;
  47. pid_t fg_pgroup;
  48. };
  49. class vga_tty : public virtual tty {
  50. public:
  51. vga_tty();
  52. virtual void putchar(char c) override;
  53. };
  54. class serial_tty : public virtual tty {
  55. public:
  56. serial_tty(int id);
  57. virtual void putchar(char c) override;
  58. public:
  59. uint16_t id;
  60. };
  61. inline tty* console;