Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions cpp-terminal/private/platform.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#include "platform.hpp"

#ifdef _WIN32
#include <conio.h>
#include <io.h>
#include <windows.h>
#else
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <cerrno>
#endif

#include <stdexcept>

#include "inputOutputModeFlags.hpp"

bool Term::Private::is_stdin_a_tty() {
Expand Down Expand Up @@ -108,10 +121,12 @@ Term::Private::BaseTerminal::~BaseTerminal() noexcept(false) {
}
#else
if (keyboard_enabled) {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, orig_termios.get()) == -1) {
throw std::runtime_error("tcsetattr() failed in destructor");
}
}
// Just to let it living longer
orig_termios.get();
#endif
}

Expand Down Expand Up @@ -160,17 +175,21 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
#else
Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
bool disable_ctrl_c)
: keyboard_enabled{enable_keyboard} {
: orig_termios{std::unique_ptr<termios>(new termios)},
keyboard_enabled{enable_keyboard} {
// Uncomment this to silently disable raw mode for non-tty
// if (keyboard_enabled) keyboard_enabled = is_stdin_a_tty();
if (keyboard_enabled) {
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) {
if (tcgetattr(STDIN_FILENO, orig_termios.get()) == -1) {
throw std::runtime_error("tcgetattr() failed");
}

// Put terminal in raw mode
struct termios raw = orig_termios;

struct termios raw = termios(*orig_termios.get());

raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);

// This disables output post-processing, requiring explicit \r\n. We
// keep it enabled, so that in C++, one can still just use std::endl
// for EOL instead of "\r\n".
Expand Down
30 changes: 21 additions & 9 deletions cpp-terminal/private/platform.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
#pragma once

#ifdef _WIN32
#include <conio.h>
#include <io.h>
#include <windows.h>
#define NOMINMAX
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || \
defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#ifndef _AMD64_
#define _AMD64_
#endif
#elif defined(i386) || defined(__i386) || defined(__i386__) || \
defined(__i386__) || defined(_M_IX86)
#ifndef _X86_
#define _X86_
#endif
#elif defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT)
#ifndef _ARM_
#define _ARM_
#endif
#endif
#include <minwindef.h>
#undef NOMINMAX
#else
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <cerrno>
class termios;
#include <memory>
#endif
#include <stdexcept>

namespace Term::Private {
// Returns true if the standard input is attached to a terminal
Expand Down Expand Up @@ -41,7 +53,7 @@ class BaseTerminal {
DWORD dwOriginalInMode{};
UINT in_code_page;
#else
struct termios orig_termios {};
std::unique_ptr<termios> orig_termios{nullptr};
#endif
bool keyboard_enabled{};

Expand Down
3 changes: 0 additions & 3 deletions cpp-terminal/prompt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <cpp-terminal/window.hpp>
#include <functional>

// TODO: remove windows.h include and this undefine
#undef ERROR

namespace Term {
/* Basic prompt */

Expand Down