Skip to content

Commit

Permalink
Windows: Set DISABLE_NEWLINE_AUTO_RETURN in raw mode, rather than coo…
Browse files Browse the repository at this point in the history
…ked mode.

The docs are super wrong: MicrosoftDocs/Console-Docs#132

Closes #156.
  • Loading branch information
alexrp committed Mar 31, 2024
1 parent fa1e5aa commit 97f962a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
41 changes: 18 additions & 23 deletions src/native/driver-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ TerminalResult cathode_set_mode(bool raw, bool flush)
termios.c_cflag &= (tcflag_t)~(CSTOPB | PARENB | PARODD | HUPCL | CLOCAL | CRTSCTS);
#if defined(ZIG_OS_LINUX)
termios.c_cflag &= (tcflag_t)~CMSPAR;
#endif
#if defined(ZIG_OS_MACOS)
#elif defined(ZIG_OS_MACOS)
termios.c_cflag &= (tcflag_t)~(CDTR_IFLOW | CDSR_OFLOW | MDMBUF);
#endif
termios.c_lflag &= (tcflag_t)~(FLUSHO | EXTPROC);
Expand All @@ -179,44 +178,40 @@ TerminalResult cathode_set_mode(bool raw, bool flush)
termios.c_oflag &= (tcflag_t)~(OCRNL | ONOCR | ONLRET);
#if defined(ZIG_OS_LINUX)
termios.c_oflag &= (tcflag_t)~OLCUC;
#endif
#if defined(ZIG_OS_MACOS)
#elif defined(ZIG_OS_MACOS)
termios.c_oflag &= (tcflag_t)~ONOEOT;
#endif
termios.c_cflag &= (tcflag_t)~CSIZE;
termios.c_cflag |= CS8 | CREAD;
termios.c_lflag &= (tcflag_t)~(ECHONL | NOFLSH | ECHOPRT | PENDIN);
#if defined(ZIG_OS_LINUX)
termios.c_lflag &= (tcflag_t)~XCASE;
#endif
#if defined(ZIG_OS_MACOS)
#elif defined(ZIG_OS_MACOS)
termios.c_lflag &= (tcflag_t)~ALTWERASE;
#endif

tcflag_t iflag = BRKINT | ICRNL | IXON;
tcflag_t oflag = OPOST | ONLCR;
tcflag_t lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
tcflag_t iflag_cooked = BRKINT | ICRNL | IXON;
tcflag_t oflag_cooked = OPOST | ONLCR;
tcflag_t lflag_cooked = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
tcflag_t lflag_raw = TOSTOP;
#if defined(ZIG_OS_MACOS)
lflag_raw |= NOKERNINFO;
#endif

// Finally, enable/disable features that depend on raw/cooked mode.
if (raw)
{
termios.c_iflag &= ~iflag;
termios.c_oflag &= ~oflag;
termios.c_lflag &= ~lflag;
termios.c_lflag |= TOSTOP;
#if defined(ZIG_OS_MACOS)
termios.c_lflag |= NOKERNINFO;
#endif
termios.c_iflag &= ~iflag_cooked;
termios.c_oflag &= ~oflag_cooked;
termios.c_lflag &= ~lflag_cooked;
termios.c_lflag |= lflag_raw;
}
else
{
termios.c_iflag |= iflag;
termios.c_oflag |= oflag;
termios.c_lflag |= lflag;
termios.c_lflag &= (tcflag_t)~TOSTOP;
#if defined(ZIG_OS_MACOS)
termios.c_lflag &= (tcflag_t)~NOKERNINFO;
#endif
termios.c_iflag |= iflag_cooked;
termios.c_oflag |= oflag_cooked;
termios.c_lflag |= lflag_cooked;
termios.c_lflag &= ~lflag_raw;
}

if (!raw)
Expand Down
16 changes: 8 additions & 8 deletions src/native/driver-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,19 @@ TerminalResult cathode_set_mode(bool raw, bool flush)
out_mode &= (DWORD)~ENABLE_LVB_GRID_WORLDWIDE;
out_mode |= ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;

DWORD in_mode_extra = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
DWORD out_mode_extra = DISABLE_NEWLINE_AUTO_RETURN;
DWORD in_mode_cooked = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
DWORD out_mode_raw = DISABLE_NEWLINE_AUTO_RETURN;

// Enable/disable features that depend on cooked/raw mode.
if (!raw)
// Enable/disable features that depend on raw/cooked mode.
if (raw)
{
in_mode |= in_mode_extra;
out_mode |= out_mode_extra;
in_mode &= ~in_mode_cooked;
out_mode |= out_mode_raw;
}
else
{
in_mode &= ~in_mode_extra;
out_mode &= ~out_mode_extra;
in_mode |= in_mode_cooked;
out_mode &= ~out_mode_raw;
}

TerminalResult result;
Expand Down

0 comments on commit 97f962a

Please sign in to comment.