tty.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <types/lock.hpp>
  9. #include <kernel/async/waitlist.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. size_t read(char* buf, size_t buf_size, size_t n);
  23. // characters committed to buffer will be handled
  24. // by the input line discipline (N_TTY)
  25. void commit_char(int c);
  26. // print character to the output
  27. // characters will be handled by the output line discipline
  28. void show_char(int c);
  29. void clear_read_buf(void);
  30. constexpr void set_pgrp(pid_t pgid)
  31. {
  32. fg_pgroup = pgid;
  33. }
  34. constexpr pid_t get_pgrp(void) const
  35. {
  36. return fg_pgroup;
  37. }
  38. char name[NAME_SIZE];
  39. termios termio;
  40. protected:
  41. types::mutex mtx_buf;
  42. types::buffer buf;
  43. kernel::async::wait_list waitlist;
  44. pid_t fg_pgroup;
  45. };
  46. class vga_tty : public virtual tty {
  47. public:
  48. vga_tty();
  49. virtual void putchar(char c) override;
  50. };
  51. class serial_tty : public virtual tty {
  52. public:
  53. serial_tty(int id);
  54. virtual void putchar(char c) override;
  55. public:
  56. uint16_t id;
  57. };
  58. inline tty* console;