Skip to content

Commit

Permalink
PR 7106673: [Git2Git] Remove the rest of IInputServices
Browse files Browse the repository at this point in the history
Since RS1 (CL 3427806), gdi32 has been available on OneCoreUAP-based
editions of Windows. Therefore, we no longer need the indirect call to
TranslateCharsetInfo and without that, IInputServices doesn't have a
reason to exist.

In an ideal world.

Unfortunately, we actually do use IInputServices as a backhanded way to
get at the ConIoSrvComm from other OneCoreInteractivity components...
we also use it in a trick we play in RundownAndExit to make sure that
the ConIoSrv connection tears down at the right time.

I've replaced that trick with an equally dirty trick, but one that is
*very explicit* about what it's doing.

This change would break CJK+Grid Lines and GetConsoleLangID on
OneCore-based editions that do not host the extension apiset
ext-ms-win-gdi-font-l1... so we're falling back to the old ConIoSrvComm
implementation directly in `dbcs.cpp`.

Retrieved from https://microsoft.visualstudio.com os.2020 OS official/rs_wdx_dxp_windev 74ca635710701c45cda9eefd13dafc6f180feb49

Related work items: MSFT-38632962
  • Loading branch information
DHowett committed Mar 24, 2022
1 parent d44117c commit a638e01
Show file tree
Hide file tree
Showing 23 changed files with 69 additions and 239 deletions.
32 changes: 28 additions & 4 deletions src/host/dbcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,36 @@ bool IsDBCSLeadByteConsole(const CHAR ch, const CPINFO* const pCPInfo)

BYTE CodePageToCharSet(const UINT uiCodePage)
{
CHARSETINFO csi;
CHARSETINFO csi{};

const auto inputServices = ServiceLocator::LocateInputServices();
if (nullptr == inputServices || !inputServices->TranslateCharsetInfo((DWORD*)IntToPtr(uiCodePage), &csi, TCI_SRCCODEPAGE))
if (!TranslateCharsetInfo((DWORD*)IntToPtr(uiCodePage), &csi, TCI_SRCCODEPAGE))
{
csi.ciCharset = OEM_CHARSET;
// On OneCore-based editions of Windows, the extension APIset containing
// TranslateCharsetInfo is not hosted. OneCoreUAP hosts it, but the lower
// editions do not. If we find that we failed to delay-load it, fall back
// to our "simple" OneCore-OK implementation.
if (GetLastError() == ERROR_PROC_NOT_FOUND)
{
switch (uiCodePage)
{
case CP_JAPANESE:
csi.ciCharset = SHIFTJIS_CHARSET;
break;
case CP_CHINESE_SIMPLIFIED:
csi.ciCharset = GB2312_CHARSET;
break;
case CP_KOREAN:
csi.ciCharset = HANGEUL_CHARSET;
break;
case CP_CHINESE_TRADITIONAL:
csi.ciCharset = CHINESEBIG5_CHARSET;
break;
}
}
else
{
csi.ciCharset = OEM_CHARSET;
}
}

return (BYTE)csi.ciCharset;
Expand Down
43 changes: 0 additions & 43 deletions src/interactivity/base/InteractivityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "../win32/AccessibilityNotifier.hpp"
#include "../win32/ConsoleControl.hpp"
#include "../win32/ConsoleInputThread.hpp"
#include "../win32/InputServices.hpp"
#include "../win32/WindowDpiApi.hpp"
#include "../win32/WindowMetrics.hpp"
#include "../win32/SystemConfigurationProvider.hpp"
Expand Down Expand Up @@ -282,48 +281,6 @@ using namespace Microsoft::Console::Interactivity;
return status;
}

[[nodiscard]] NTSTATUS InteractivityFactory::CreateInputServices(_Inout_ std::unique_ptr<IInputServices>& services)
{
NTSTATUS status = STATUS_SUCCESS;

ApiLevel level;
status = ApiDetector::DetectNtUserWindow(&level);

if (NT_SUCCESS(status))
{
std::unique_ptr<IInputServices> newServices;
try
{
switch (level)
{
case ApiLevel::Win32:
newServices = std::make_unique<Microsoft::Console::Interactivity::Win32::InputServices>();
break;

#ifdef BUILD_ONECORE_INTERACTIVITY
case ApiLevel::OneCore:
newServices = std::make_unique<Microsoft::Console::Interactivity::OneCore::ConIoSrvComm>();
break;
#endif
default:
status = STATUS_INVALID_LEVEL;
break;
}
}
catch (...)
{
status = NTSTATUS_FROM_HRESULT(wil::ResultFromCaughtException());
}

if (NT_SUCCESS(status))
{
services.swap(newServices);
}
}

return status;
}

// Method Description:
// - Attempts to instantiate a "pseudo window" for when we're operating in
// pseudoconsole mode. There are some tools (cygwin & derivatives) that use
Expand Down
1 change: 0 additions & 1 deletion src/interactivity/base/InteractivityFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace Microsoft::Console::Interactivity
[[nodiscard]] NTSTATUS CreateWindowMetrics(_Inout_ std::unique_ptr<IWindowMetrics>& metrics);
[[nodiscard]] NTSTATUS CreateAccessibilityNotifier(_Inout_ std::unique_ptr<IAccessibilityNotifier>& notifier);
[[nodiscard]] NTSTATUS CreateSystemConfigurationProvider(_Inout_ std::unique_ptr<ISystemConfigurationProvider>& provider);
[[nodiscard]] NTSTATUS CreateInputServices(_Inout_ std::unique_ptr<IInputServices>& services);

[[nodiscard]] NTSTATUS CreatePseudoWindow(HWND& hwnd);
};
Expand Down
40 changes: 12 additions & 28 deletions src/interactivity/base/ServiceLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ std::unique_ptr<IWindowMetrics> ServiceLocator::s_windowMetrics;
std::unique_ptr<IAccessibilityNotifier> ServiceLocator::s_accessibilityNotifier;
std::unique_ptr<IHighDpiApi> ServiceLocator::s_highDpiApi;
std::unique_ptr<ISystemConfigurationProvider> ServiceLocator::s_systemConfigurationProvider;
std::unique_ptr<IInputServices> ServiceLocator::s_inputServices;

IConsoleWindow* ServiceLocator::s_consoleWindow = nullptr;

void (*ServiceLocator::s_oneCoreTeardownFunction)() = nullptr;

Globals ServiceLocator::s_globals;

bool ServiceLocator::s_pseudoWindowInitialized = false;
Expand All @@ -34,6 +35,12 @@ wil::unique_hwnd ServiceLocator::s_pseudoWindow = nullptr;

#pragma region Public Methods

void ServiceLocator::SetOneCoreTeardownFunction(void (*pfn)()) noexcept
{
FAIL_FAST_IF(nullptr != s_oneCoreTeardownFunction);
s_oneCoreTeardownFunction = pfn;
}

[[noreturn]] void ServiceLocator::RundownAndExit(const HRESULT hr)
{
// MSFT:15506250
Expand All @@ -59,14 +66,13 @@ wil::unique_hwnd ServiceLocator::s_pseudoWindow = nullptr;
// We don't want to have other execution in the system get stuck, so this is a great
// place to clean up and notify any objects or threads in the system that have to cleanup safely before
// we head into TerminateProcess and tear everything else down less gracefully.

// TODO: MSFT: 14397093 - Expand graceful rundown beyond just the Hot Bug input services case.

if (s_inputServices.get() != nullptr)
if (s_oneCoreTeardownFunction)
{
s_inputServices.reset(nullptr);
s_oneCoreTeardownFunction();
}

// TODO: MSFT: 14397093 - Expand graceful rundown beyond just the Hot Bug input services case.

ExitProcess(hr);
}

Expand Down Expand Up @@ -267,28 +273,6 @@ ISystemConfigurationProvider* ServiceLocator::LocateSystemConfigurationProvider(
return s_systemConfigurationProvider.get();
}

IInputServices* ServiceLocator::LocateInputServices()
{
NTSTATUS status = STATUS_SUCCESS;

if (!s_inputServices)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}

if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateInputServices(s_inputServices);
}
}

LOG_IF_NTSTATUS_FAILED(status);

return s_inputServices.get();
}

Globals& ServiceLocator::LocateGlobals()
{
return s_globals;
Expand Down
1 change: 0 additions & 1 deletion src/interactivity/base/lib/InteractivityBase.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<ClInclude Include="..\..\inc\IConsoleInputThread.hpp" />
<ClInclude Include="..\..\inc\IConsoleWindow.hpp" />
<ClInclude Include="..\..\inc\IHighDpiApi.hpp" />
<ClInclude Include="..\..\inc\IInputServices.hpp" />
<ClInclude Include="..\..\inc\IInteractivityFactory.hpp" />
<ClInclude Include="..\..\inc\ISystemConfigurationProvider.hpp" />
<ClInclude Include="..\..\inc\IWindowMetrics.hpp" />
Expand Down
3 changes: 0 additions & 3 deletions src/interactivity/base/lib/InteractivityBase.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@
<ClInclude Include="..\..\inc\IHighDpiApi.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\inc\IInputServices.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\inc\IInteractivityFactory.hpp">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
33 changes: 0 additions & 33 deletions src/interactivity/inc/IInputServices.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/interactivity/inc/IInteractivityFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Author(s):
#include "IWindowMetrics.hpp"
#include "IAccessibilityNotifier.hpp"
#include "ISystemConfigurationProvider.hpp"
#include "IInputServices.hpp"

#include <memory>

Expand All @@ -40,7 +39,6 @@ namespace Microsoft::Console::Interactivity
[[nodiscard]] virtual NTSTATUS CreateWindowMetrics(_Inout_ std::unique_ptr<IWindowMetrics>& metrics) = 0;
[[nodiscard]] virtual NTSTATUS CreateAccessibilityNotifier(_Inout_ std::unique_ptr<IAccessibilityNotifier>& notifier) = 0;
[[nodiscard]] virtual NTSTATUS CreateSystemConfigurationProvider(_Inout_ std::unique_ptr<ISystemConfigurationProvider>& provider) = 0;
[[nodiscard]] virtual NTSTATUS CreateInputServices(_Inout_ std::unique_ptr<IInputServices>& services) = 0;

[[nodiscard]] virtual NTSTATUS CreatePseudoWindow(HWND& hwnd) = 0;
};
Expand Down
1 change: 0 additions & 1 deletion src/interactivity/inc/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Microsoft::Console::Interactivity
ConsoleInputThread,
ConsoleWindowMetrics,
HighDpiApi,
InputServices,
SystemConfigurationProvider
};
}
13 changes: 5 additions & 8 deletions src/interactivity/inc/ServiceLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Microsoft::Console::Interactivity
class ServiceLocator final
{
public:
static void SetOneCoreTeardownFunction(void (*pfn)()) noexcept;

[[noreturn]] static void RundownAndExit(const HRESULT hr);

// N.B.: Location methods without corresponding creation methods
Expand Down Expand Up @@ -78,13 +80,6 @@ namespace Microsoft::Console::Interactivity
return static_cast<T*>(LocateHighDpiApi());
}

static IInputServices* LocateInputServices();
template<typename T>
static T* LocateInputServices()
{
return static_cast<T*>(LocateInputServices());
}

static ISystemConfigurationProvider* LocateSystemConfigurationProvider();

static Globals& LocateGlobals();
Expand All @@ -110,7 +105,9 @@ namespace Microsoft::Console::Interactivity
static std::unique_ptr<IWindowMetrics> s_windowMetrics;
static std::unique_ptr<IHighDpiApi> s_highDpiApi;
static std::unique_ptr<ISystemConfigurationProvider> s_systemConfigurationProvider;
static std::unique_ptr<IInputServices> s_inputServices;

// See the big block comment in RundownAndExit for more info.
static void (*s_oneCoreTeardownFunction)();

static Globals s_globals;
static bool s_pseudoWindowInitialized;
Expand Down
4 changes: 2 additions & 2 deletions src/interactivity/onecore/BgfxEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ BgfxEngine::BgfxEngine(PVOID SharedViewBase, LONG DisplayHeight, LONG DisplayWid
PVOID OldRunBase;
PVOID NewRunBase;

Status = ServiceLocator::LocateInputServices<ConIoSrvComm>()->RequestUpdateDisplay(0);
Status = ConIoSrvComm::GetConIoSrvComm()->RequestUpdateDisplay(0);

if (NT_SUCCESS(Status))
{
Expand Down Expand Up @@ -189,7 +189,7 @@ BgfxEngine::BgfxEngine(PVOID SharedViewBase, LONG DisplayHeight, LONG DisplayWid
CursorInfo.Height = options.ulCursorHeightPercent;
CursorInfo.IsVisible = TRUE;

NTSTATUS Status = ServiceLocator::LocateInputServices<ConIoSrvComm>()->RequestSetCursor(&CursorInfo);
NTSTATUS Status = ConIoSrvComm::GetConIoSrvComm()->RequestSetCursor(&CursorInfo);

return HRESULT_FROM_NT(Status);
}
Expand Down
47 changes: 13 additions & 34 deletions src/interactivity/onecore/ConIoSrvComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ extern void UnlockConsole();
using namespace Microsoft::Console::Render;
using namespace Microsoft::Console::Interactivity::OneCore;

static std::unique_ptr<ConIoSrvComm> s_conIoSrvComm;
ConIoSrvComm* ConIoSrvComm::GetConIoSrvComm()
{
static bool initialized = []() {
s_conIoSrvComm = std::make_unique<ConIoSrvComm>();
ServiceLocator::SetOneCoreTeardownFunction([] {
s_conIoSrvComm.reset(nullptr);
});
return true;
}();
return s_conIoSrvComm.get();
}

ConIoSrvComm::ConIoSrvComm() :
_inputPipeThreadHandle(nullptr),
_pipeReadHandle(INVALID_HANDLE_VALUE),
Expand Down Expand Up @@ -536,40 +549,6 @@ PVOID ConIoSrvComm::GetSharedViewBase() const

#pragma endregion

#pragma region IInputServices Members

BOOL ConIoSrvComm::TranslateCharsetInfo(DWORD* lpSrc, LPCHARSETINFO lpCs, DWORD dwFlags)
{
SetLastError(ERROR_SUCCESS);

if (TCI_SRCCODEPAGE == dwFlags)
{
*lpCs = { 0 };

DWORD dwSrc = (DWORD)lpSrc;
switch (dwSrc)
{
case CP_JAPANESE:
lpCs->ciCharset = SHIFTJIS_CHARSET;
return TRUE;
case CP_CHINESE_SIMPLIFIED:
lpCs->ciCharset = GB2312_CHARSET;
return TRUE;
case CP_KOREAN:
lpCs->ciCharset = HANGEUL_CHARSET;
return TRUE;
case CP_CHINESE_TRADITIONAL:
lpCs->ciCharset = CHINESEBIG5_CHARSET;
return TRUE;
}
}

SetLastError(ERROR_NOT_SUPPORTED);
return FALSE;
}

#pragma endregion

[[nodiscard]] NTSTATUS ConIoSrvComm::InitializeBgfx()
{
NTSTATUS Status;
Expand Down
Loading

0 comments on commit a638e01

Please sign in to comment.