2019-02-02 00:25:12 +00:00
|
|
|
#include <common/setTerminalEcho.h>
|
2020-12-17 18:08:42 +00:00
|
|
|
#include <common/errnoToString.h>
|
2018-05-21 17:27:18 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <cstring>
|
2018-05-23 18:28:23 +00:00
|
|
|
#include <string>
|
2021-03-29 21:39:38 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2018-05-21 17:27:18 +00:00
|
|
|
|
|
|
|
|
2019-02-02 00:25:12 +00:00
|
|
|
void setTerminalEcho(bool enable)
|
2018-05-21 17:27:18 +00:00
|
|
|
{
|
2021-03-29 21:37:27 +00:00
|
|
|
/// Obtain terminal attributes,
|
|
|
|
/// toggle the ECHO flag
|
|
|
|
/// and set them back.
|
2018-05-21 17:27:18 +00:00
|
|
|
|
2021-03-29 21:37:27 +00:00
|
|
|
struct termios tty{};
|
2018-05-21 17:27:18 +00:00
|
|
|
|
2021-03-29 21:37:27 +00:00
|
|
|
if (0 != tcgetattr(STDIN_FILENO, &tty))
|
2020-12-17 18:08:42 +00:00
|
|
|
throw std::runtime_error(std::string("setTerminalEcho failed get: ") + errnoToString(errno));
|
2021-03-29 21:37:27 +00:00
|
|
|
|
|
|
|
if (enable)
|
2018-05-21 17:27:18 +00:00
|
|
|
tty.c_lflag |= ECHO;
|
2021-03-29 21:37:27 +00:00
|
|
|
else
|
|
|
|
tty.c_lflag &= ~ECHO;
|
2018-05-21 17:27:18 +00:00
|
|
|
|
2021-03-29 21:37:27 +00:00
|
|
|
if (0 != tcsetattr(STDIN_FILENO, TCSANOW, &tty))
|
2020-12-17 18:08:42 +00:00
|
|
|
throw std::runtime_error(std::string("setTerminalEcho failed set: ") + errnoToString(errno));
|
2018-05-21 17:27:18 +00:00
|
|
|
}
|