Skip to content

Commit

Permalink
[api] Added pipy.tty.raw property
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Jul 31, 2024
1 parent e391b8d commit 2eb94cc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/api/pipy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
#include <sys/wait.h>
#endif

#if defined(__linux__)
#include <pty.h>
#elif defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#include <termios.h>
#endif

namespace pipy {

thread_local static pjs::Ref<pjs::Array> s_argv;
Expand Down Expand Up @@ -607,6 +616,37 @@ void Pipy::FileWatcher::on_file_changed(bool changed) {
}
}

//
// Pipy::TTY
//

#ifndef _WIN32

struct InitialTTYState {
struct termios term;
InitialTTYState() { tcgetattr(0, &term); }
};

static InitialTTYState s_initial_tty_state;

#endif // !_WIN32

std::mutex Pipy::TTY::m_mutex;
bool Pipy::TTY::m_raw = false;

void Pipy::TTY::set_raw(bool b) {
std::lock_guard<std::mutex> lock(m_mutex);
if (b != m_raw) {
#ifndef _WIN32
struct termios term = s_initial_tty_state.term;
if (b) cfmakeraw(&term);
term.c_oflag = s_initial_tty_state.term.c_oflag;
tcsetattr(0, TCSANOW, &term);
#endif // !_WIN32
m_raw = b;
}
}

} // namespace pipy

namespace pjs {
Expand All @@ -617,6 +657,7 @@ template<> void ClassDef<Pipy>::init() {
super<Function>();
ctor();

variable("tty", class_of<Pipy::TTY>());
variable("inbound", class_of<Pipy::Inbound>());
variable("outbound", class_of<Pipy::Outbound>());

Expand Down Expand Up @@ -844,6 +885,15 @@ template<> void ClassDef<Pipy>::init() {
});
}

template<> void ClassDef<Pipy::TTY>::init() {
ctor();

accessor("raw",
[](Object *, Value &ret) { ret.set(Pipy::TTY::get_raw()); },
[](Object *, const Value &ret) { Pipy::TTY::set_raw(ret.to_boolean()); }
);
}

template<> void ClassDef<Pipy::Inbound>::init() {
ctor();

Expand Down
15 changes: 15 additions & 0 deletions src/api/pipy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <functional>
#include <string>
#include <vector>
#include <mutex>

namespace pipy {

Expand Down Expand Up @@ -122,6 +123,20 @@ class Pipy : public pjs::FunctionTemplate<Pipy> {

void operator()(pjs::Context &ctx, pjs::Object *obj, pjs::Value &ret);

//
// Pipy::TTY
//

class TTY : public pjs::ObjectTemplate<TTY> {
public:
static void set_raw(bool b);
static bool get_raw() { return m_raw; }

private:
static std::mutex m_mutex;
static bool m_raw;
};

//
// Pipy::Inbound
//
Expand Down
2 changes: 1 addition & 1 deletion src/filters/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bool Exec::exec_argv(const std::list<std::string> &args) {
term.c_iflag = ICRNL | IXON | IXANY | IMAXBEL | BRKINT | IUTF8;
term.c_oflag = OPOST | ONLCR;
term.c_cflag = CREAD | CS8 | HUPCL;
term.c_lflag = ICANON | ISIG | IEXTEN | ECHOE | ECHOK | ECHOKE | ECHOCTL;
term.c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHOKE | ECHOCTL;
term.c_cc[VEOF] = 4;
term.c_cc[VEOL] = -1;
term.c_cc[VEOL2] = -1;
Expand Down

0 comments on commit 2eb94cc

Please sign in to comment.