tty.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // TODO: formal poll support
  31. int poll();
  32. constexpr void set_pgrp(pid_t pgid)
  33. {
  34. fg_pgroup = pgid;
  35. }
  36. constexpr pid_t get_pgrp(void) const
  37. {
  38. return fg_pgroup;
  39. }
  40. char name[NAME_SIZE];
  41. termios termio;
  42. protected:
  43. types::mutex mtx_buf;
  44. types::buffer buf;
  45. kernel::async::wait_list waitlist;
  46. pid_t fg_pgroup;
  47. };
  48. class vga_tty : public virtual tty {
  49. public:
  50. vga_tty();
  51. virtual void putchar(char c) override;
  52. };
  53. class serial_tty : public virtual tty {
  54. public:
  55. serial_tty(int id);
  56. virtual void putchar(char c) override;
  57. public:
  58. uint16_t id;
  59. };
  60. inline tty* console;