Skip to content

Commit

Permalink
Windows: always load ConPTY dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Oct 25, 2024
1 parent 22c232f commit dad5460
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A terminal emulator that runs in your terminal. Powered by Turbo Vision.

`tvterm` relies on Paul Evan's [libvterm](http://www.leonerd.org.uk/code/libvterm/) terminal emulator, also used by [Neovim](https://github.com/neovim/libvterm) and [Emacs](https://github.com/akermu/emacs-libvterm).

As of now, `tvterm` can only be compiled for Unix systems.
Additionally, `tvterm` supports both Unix and Windows (Windows 10 1809 or later).

The original location of this project is https://github.com/magiblot/tvterm.

Expand Down
48 changes: 26 additions & 22 deletions source/tvterm-core/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,27 @@ namespace tvterm

struct ConPtyApi
{
decltype(::CreatePseudoConsole) *CreatePseudoConsole;
decltype(::ResizePseudoConsole) *ResizePseudoConsole;
decltype(::ClosePseudoConsole) *ClosePseudoConsole;
decltype(::CreatePseudoConsole) *CreatePseudoConsole {nullptr};
decltype(::ResizePseudoConsole) *ResizePseudoConsole {nullptr};
decltype(::ClosePseudoConsole) *ClosePseudoConsole {nullptr};

void init() noexcept;
} conPty;
};

static ConPtyApi conPty;

void ConPtyApi::init() noexcept
{
if (HMODULE mod = LoadLibraryA("conpty.dll"))
{
CreatePseudoConsole =
(decltype(CreatePseudoConsole)) GetProcAddress(mod, "CreatePseudoConsole");
ResizePseudoConsole =
(decltype(ResizePseudoConsole)) GetProcAddress(mod, "ResizePseudoConsole");
ClosePseudoConsole =
(decltype(ClosePseudoConsole)) GetProcAddress(mod, "ClosePseudoConsole");
}
else
{
CreatePseudoConsole = &::CreatePseudoConsole;
ResizePseudoConsole = &::ResizePseudoConsole;
ClosePseudoConsole = &::ClosePseudoConsole;
}
HMODULE mod = LoadLibraryA("conpty");
if (!mod)
mod = GetModuleHandleA("kernel32");

CreatePseudoConsole =
(decltype(CreatePseudoConsole)) GetProcAddress(mod, "CreatePseudoConsole");
ResizePseudoConsole =
(decltype(ResizePseudoConsole)) GetProcAddress(mod, "ResizePseudoConsole");
ClosePseudoConsole =
(decltype(ClosePseudoConsole)) GetProcAddress(mod, "ClosePseudoConsole");
}

static COORD toCoord(TPoint point) noexcept
Expand Down Expand Up @@ -308,11 +305,12 @@ bool createPty( PtyDescriptor &ptyDescriptor,
TSpan<const EnvironmentVar> customEnvironment,
void (&onError)(const char *) ) noexcept
{
static int initConPty = [] ()
static bool conPtyAvailable = [] ()
{
conPty.init();
(void) initConPty;
return 0;
return conPty.CreatePseudoConsole &&
conPty.ResizePseudoConsole &&
conPty.ClosePseudoConsole;
}();

HANDLE hMasterRead {},
Expand All @@ -331,6 +329,12 @@ bool createPty( PtyDescriptor &ptyDescriptor,

do
{
if (!conPtyAvailable)
{
failedAction = "Loading ConPTY";
break;
}

if (!CreatePipe(&hMasterRead, &hClientWrite, nullptr, 0))
{
failedAction = "CreatePipe";
Expand Down

0 comments on commit dad5460

Please sign in to comment.