|
1 | 1 | #include "platform.hpp" |
2 | 2 |
|
| 3 | +#ifdef _WIN32 |
| 4 | +#include <conio.h> |
| 5 | +#include <io.h> |
| 6 | +#include <windows.h> |
| 7 | +#else |
| 8 | +#include <sys/ioctl.h> |
| 9 | +#include <termios.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <cerrno> |
| 12 | +#endif |
| 13 | + |
| 14 | +#include <stdexcept> |
| 15 | + |
3 | 16 | #include "inputOutputModeFlags.hpp" |
4 | 17 |
|
5 | 18 | bool Term::Private::is_stdin_a_tty() { |
@@ -108,10 +121,12 @@ Term::Private::BaseTerminal::~BaseTerminal() noexcept(false) { |
108 | 121 | } |
109 | 122 | #else |
110 | 123 | if (keyboard_enabled) { |
111 | | - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) { |
| 124 | + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, orig_termios.get()) == -1) { |
112 | 125 | throw std::runtime_error("tcsetattr() failed in destructor"); |
113 | 126 | } |
114 | 127 | } |
| 128 | + // Just to let it living longer |
| 129 | + orig_termios.get(); |
115 | 130 | #endif |
116 | 131 | } |
117 | 132 |
|
@@ -160,17 +175,21 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard, |
160 | 175 | #else |
161 | 176 | Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard, |
162 | 177 | bool disable_ctrl_c) |
163 | | - : keyboard_enabled{enable_keyboard} { |
| 178 | + : orig_termios{std::unique_ptr<termios>(new termios)}, |
| 179 | + keyboard_enabled{enable_keyboard} { |
164 | 180 | // Uncomment this to silently disable raw mode for non-tty |
165 | 181 | // if (keyboard_enabled) keyboard_enabled = is_stdin_a_tty(); |
166 | 182 | if (keyboard_enabled) { |
167 | | - if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) { |
| 183 | + if (tcgetattr(STDIN_FILENO, orig_termios.get()) == -1) { |
168 | 184 | throw std::runtime_error("tcgetattr() failed"); |
169 | 185 | } |
170 | 186 |
|
171 | 187 | // Put terminal in raw mode |
172 | | - struct termios raw = orig_termios; |
| 188 | + |
| 189 | + struct termios raw = termios(*orig_termios.get()); |
| 190 | + |
173 | 191 | raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); |
| 192 | + |
174 | 193 | // This disables output post-processing, requiring explicit \r\n. We |
175 | 194 | // keep it enabled, so that in C++, one can still just use std::endl |
176 | 195 | // for EOL instead of "\r\n". |
|
0 commit comments