tty.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/lock.hpp>
  10. #include <kernel/async/waitlist.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) { fg_pgroup = pgid; }
  35. constexpr pid_t get_pgrp(void) const { return fg_pgroup; }
  36. termios termio;
  37. std::string name;
  38. protected:
  39. async::mutex mtx_buf;
  40. types::buffer buf;
  41. async::wait_list waitlist;
  42. pid_t fg_pgroup;
  43. };
  44. class vga_tty : public virtual tty {
  45. public:
  46. vga_tty();
  47. virtual void putchar(char c) override;
  48. };
  49. inline tty* console;
  50. int register_tty(tty* tty_dev);
  51. } // namespace kernel::tty