From 59771c0509c09404ee99132aededa27be2c52820 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Mon, 9 Sep 2024 06:28:10 -0700 Subject: [PATCH] Bring back clear impl Fixes #711 --- src/native.d.ts | 2 +- src/win/conpty.cc | 49 ++++++++++++++++++++++++------------------ src/windowsPtyAgent.ts | 2 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/native.d.ts b/src/native.d.ts index 57c210757..973aca57d 100644 --- a/src/native.d.ts +++ b/src/native.d.ts @@ -6,7 +6,7 @@ interface IConptyNative { startProcess(file: string, cols: number, rows: number, debug: boolean, pipeName: string, conptyInheritCursor: boolean, useConptyDll: boolean): IConptyProcess; connect(ptyId: number, commandLine: string, cwd: string, env: string[], onExitCallback: (exitCode: number) => void): { pid: number }; resize(ptyId: number, cols: number, rows: number, useConptyDll: boolean): void; - clear(ptyId: number): void; + clear(ptyId: number, useConptyDll: boolean): void; kill(ptyId: number, useConptyDll: boolean): void; } diff --git a/src/win/conpty.cc b/src/win/conpty.cc index 69a5df6ea..3d7e9fd44 100644 --- a/src/win/conpty.cc +++ b/src/win/conpty.cc @@ -479,29 +479,36 @@ static Napi::Value PtyClear(const Napi::CallbackInfo& info) { Napi::Env env(info.Env()); Napi::HandleScope scope(env); - if (info.Length() != 1 || - !info[0].IsNumber()) { - throw Napi::Error::New(env, "Usage: pty.clear(id)"); + if (info.Length() != 2 || + !info[0].IsNumber() || + !info[1].IsBoolean()) { + throw Napi::Error::New(env, "Usage: pty.clear(id, useConptyDll)"); + } + + int id = info[0].As().Int32Value(); + const bool useConptyDll = info[1].As().Value(); + + // This API is only supported for conpty.dll as it was introduced in a later version of Windows. + // We could hook it up to point at >= a version of Windows only, but the future is conpty.dll + // anyway. + if (!useConptyDll) { + return env.Undefined(); } - // int id = info[0].As().Int32Value(); - - // const pty_baton* handle = get_pty_baton(id); - - // if (handle != nullptr) { - // HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0); - // bool fLoadedDll = hLibrary != nullptr; - // if (fLoadedDll) - // { - // PFNCLEARPSEUDOCONSOLE const pfnClearPseudoConsole = (PFNCLEARPSEUDOCONSOLE)GetProcAddress( - // (HMODULE)hLibrary, - // useConptyDll ? "ConptyClearPseudoConsole" : "ClearPseudoConsole"); - // if (pfnClearPseudoConsole) - // { - // pfnClearPseudoConsole(handle->hpc); - // } - // } - // } + const pty_baton* handle = get_pty_baton(id); + + if (handle != nullptr) { + HANDLE hLibrary = LoadConptyDll(info, useConptyDll); + bool fLoadedDll = hLibrary != nullptr; + if (fLoadedDll) + { + PFNCLEARPSEUDOCONSOLE const pfnClearPseudoConsole = (PFNCLEARPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary, "ConptyClearPseudoConsole"); + if (pfnClearPseudoConsole) + { + pfnClearPseudoConsole(handle->hpc); + } + } + } return env.Undefined(); } diff --git a/src/windowsPtyAgent.ts b/src/windowsPtyAgent.ts index d039103fe..5e8c062db 100644 --- a/src/windowsPtyAgent.ts +++ b/src/windowsPtyAgent.ts @@ -153,7 +153,7 @@ export class WindowsPtyAgent { public clear(): void { if (this._useConpty) { - (this._ptyNative as IConptyNative).clear(this._pty); + (this._ptyNative as IConptyNative).clear(this._pty, this._useConptyDll); } }