tty.hpp 1.6 KB

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