uvw  1.3.0
tty.hpp
1 #pragma once
2 
3 
4 #include <utility>
5 #include <memory>
6 #include <uv.h>
7 #include "stream.hpp"
8 #include "util.hpp"
9 
10 
11 namespace uvw {
12 
13 
14 namespace details {
15 
16 
17 struct ResetModeMemo {
18  ~ResetModeMemo() {
19  uv_tty_reset_mode();
20  }
21 };
22 
23 
24 enum class UVTTYModeT: std::underlying_type_t<uv_tty_mode_t> {
25  NORMAL = UV_TTY_MODE_NORMAL,
26  RAW = UV_TTY_MODE_RAW,
27  IO = UV_TTY_MODE_IO
28 };
29 
30 
31 }
32 
33 
52 class TTYHandle final: public StreamHandle<TTYHandle, uv_tty_t> {
53  static auto resetModeMemo() {
54  static std::weak_ptr<details::ResetModeMemo> weak;
55  auto shared = weak.lock();
56  if(!shared) { weak = shared = std::make_shared<details::ResetModeMemo>(); }
57  return shared;
58  }
59 
60 public:
61  using Mode = details::UVTTYModeT;
62 
63  explicit TTYHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, FileHandle desc, bool readable)
64  : StreamHandle{ca, std::move(ref)},
65  memo{resetModeMemo()},
66  fd{desc},
67  rw{readable}
68  {}
69 
74  bool init() {
75  return initialize(&uv_tty_init, fd, rw);
76  }
77 
94  bool mode(Mode m) {
95  return (0 == uv_tty_set_mode(get(), static_cast<std::underlying_type_t<Mode>>(m)));
96  }
97 
102  bool reset() noexcept {
103  return (0 == uv_tty_reset_mode());
104  }
105 
111  WinSize size;
112 
113  if(0 != uv_tty_get_winsize(get(), &size.width, &size.height)) {
114  size.width = -1;
115  size.height = -1;
116  }
117 
118  return size;
119  }
120 
121 private:
122  std::shared_ptr<details::ResetModeMemo> memo;
123  FileHandle::Type fd;
124  int rw;
125 };
126 
127 
128 }
WinSize getWinSize()
Gets the current Window size.
Definition: tty.hpp:110
details::UVTypeWrapper< uv_file > FileHandle
Definition: util.hpp:186
bool mode(Mode m)
Sets the TTY using the specified terminal mode.
Definition: tty.hpp:94
Windows size representation.
Definition: util.hpp:177
bool init()
Initializes the handle.
Definition: tty.hpp:74
The StreamHandle handle.
Definition: stream.hpp:130
bool reset() noexcept
Resets TTY settings to default values.
Definition: tty.hpp:102
int height
Definition: util.hpp:179
int width
Definition: util.hpp:178
The TTYHandle handle.
Definition: tty.hpp:52
uvw default namespace.
Definition: async.hpp:11