tty.hpp 1.5 KB

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