Skip to content

Commit

Permalink
Windows: issue a warning for PWSH/cmd term only in W10
Browse files Browse the repository at this point in the history
Do not issue the warning over PowerShell or cmd legacy terminal emulators
in Windows 11. In Window the check doesn't work, because the process
tree is different - the Windows Terminal doesn't have its own process
and it is uv.exe->powershell.exe->exporer.exe.

This improves commit bb2a72f.
  • Loading branch information
MartinPulec committed Feb 28, 2024
1 parent d74922e commit 502fb0c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,11 @@ struct init_data *common_preinit(int argc, char *argv[])
}

// warn in W10 "legacy" terminal emulators
if ((win_has_ancestor_process("powershell.exe") ||
if (getenv("TERM") == nullptr &&
get_windows_build() < BUILD_WINDOWS_11_OR_LATER &&
(win_has_ancestor_process("powershell.exe") ||
win_has_ancestor_process("cmd.exe")) &&
!win_has_ancestor_process("WindowsTerminal.exe") &&
getenv("TERM") == nullptr) {
!win_has_ancestor_process("WindowsTerminal.exe")) {
MSG(WARNING, "Running inside PS/cmd terminal is not recommended "
"because scrolling the output freezes the process, "
"consider using Windows Terminal instead!\n");
Expand Down
32 changes: 32 additions & 0 deletions src/utils/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#ifdef _WIN32
#include <audioclient.h>
#include <ntstatus.h>
#include <objbase.h>
#include <tlhelp32.h>
#include <vfwmsgs.h>
Expand Down Expand Up @@ -239,6 +240,37 @@ win_has_ancestor_process(const char *name)
return false;
}

unsigned long
get_windows_build()
{
RTL_OSVERSIONINFOW osVersionInfo = { .dwOSVersionInfoSize =
sizeof(RTL_OSVERSIONINFOW) };

HMODULE hNtDll = GetModuleHandleW(L"ntdll.dll");
if (hNtDll == NULL) {
MSG(VERBOSE, "Cannot load ntdll.dll!\n");
return 0;
}

typedef NTSTATUS(WINAPI * RtlGetVersionFunc)(
PRTL_OSVERSIONINFOW lpVersionInformation);
RtlGetVersionFunc pRtlGetVersion =
(RtlGetVersionFunc) GetProcAddress(hNtDll, "RtlGetVersion");
if (pRtlGetVersion == NULL) {
MSG(VERBOSE, "Cannot get RtlGetVersion from ntdll.dll!\n");
return 0;
}

if (pRtlGetVersion(&osVersionInfo) != STATUS_SUCCESS) {
MSG(VERBOSE, "Cannot get Windows version nfo!\n");
return 0;
}
MSG(DEBUG, "Windows version: %lu.%lu (build %lu)\n",
osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion,
osVersionInfo.dwBuildNumber);
return osVersionInfo.dwBuildNumber;
}

#endif // defined _WIN32

/* vim: set expandtab sw=8 tw=120: */
5 changes: 5 additions & 0 deletions src/utils/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#include <stdbool.h>
#endif

enum {
BUILD_WINDOWS_11_OR_LATER = 22000,
};

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -86,6 +90,7 @@ const char *hresult_to_str(HRESULT res);
const char *get_win32_error(DWORD error);
const char *win_wstr_to_str(const wchar_t *wstr);
bool win_has_ancestor_process(const char *name);
unsigned long get_windows_build(void);
#endif // defined _WIN32

#ifdef __cplusplus
Expand Down

0 comments on commit 502fb0c

Please sign in to comment.