Skip to content

Commit

Permalink
Move code page check into the DoSrvSet functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
j4james committed Sep 30, 2024
1 parent ed33afd commit 8e191f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
24 changes: 16 additions & 8 deletions src/host/getset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,16 +1128,22 @@ void ApiRoutines::GetLargestConsoleWindowSizeImpl(const SCREEN_INFORMATION& cont
CATCH_RETURN();
}

void DoSrvSetConsoleOutputCodePage(const unsigned int codepage)
[[nodiscard]] HRESULT DoSrvSetConsoleOutputCodePage(const unsigned int codepage)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

// Return if it's not known as a valid codepage ID.
RETURN_HR_IF(E_INVALIDARG, !(IsValidCodePage(codepage)));

// Do nothing if no change.
if (gci.OutputCP != codepage)
{
// Set new code page
gci.OutputCP = codepage;
SetConsoleCPInfo(TRUE);
}

return S_OK;
}

// Routine Description:
Expand All @@ -1153,9 +1159,7 @@ void DoSrvSetConsoleOutputCodePage(const unsigned int codepage)
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LockConsole();
auto Unlock = wil::scope_exit([&] { UnlockConsole(); });
// Return if it's not known as a valid codepage ID.
RETURN_HR_IF(E_INVALIDARG, !(IsValidCodePage(codepage)));
DoSrvSetConsoleOutputCodePage(codepage);
RETURN_IF_FAILED(DoSrvSetConsoleOutputCodePage(codepage));
// Setting the code page via the API also updates the default value.
// This is how the initial code page is set to UTF-8 in a WSL shell.
gci.DefaultOutputCP = codepage;
Expand All @@ -1164,16 +1168,22 @@ void DoSrvSetConsoleOutputCodePage(const unsigned int codepage)
CATCH_RETURN();
}

void DoSrvSetConsoleInputCodePage(const unsigned int codepage)
[[nodiscard]] HRESULT DoSrvSetConsoleInputCodePage(const unsigned int codepage)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

// Return if it's not known as a valid codepage ID.
RETURN_HR_IF(E_INVALIDARG, !(IsValidCodePage(codepage)));

// Do nothing if no change.
if (gci.CP != codepage)
{
// Set new code page
gci.CP = codepage;
SetConsoleCPInfo(FALSE);
}

return S_OK;
}

// Routine Description:
Expand All @@ -1189,9 +1199,7 @@ void DoSrvSetConsoleInputCodePage(const unsigned int codepage)
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LockConsole();
auto Unlock = wil::scope_exit([&] { UnlockConsole(); });
// Return if it's not known as a valid codepage ID.
RETURN_HR_IF(E_INVALIDARG, !(IsValidCodePage(codepage)));
DoSrvSetConsoleInputCodePage(codepage);
RETURN_IF_FAILED(DoSrvSetConsoleInputCodePage(codepage));
// Setting the code page via the API also updates the default value.
// This is how the initial code page is set to UTF-8 in a WSL shell.
gci.DefaultCP = codepage;
Expand Down
4 changes: 2 additions & 2 deletions src/host/getset.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Revision History:
#include "../inc/conattrs.hpp"
class SCREEN_INFORMATION;

void DoSrvSetConsoleOutputCodePage(const unsigned int codepage);
void DoSrvSetConsoleInputCodePage(const unsigned int codepage);
[[nodiscard]] HRESULT DoSrvSetConsoleOutputCodePage(const unsigned int codepage);
[[nodiscard]] HRESULT DoSrvSetConsoleInputCodePage(const unsigned int codepage);
11 changes: 4 additions & 7 deletions src/host/outputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,8 @@ void ConhostInternalGetSet::ShowWindow(bool showOrHide)
// - <none>
void ConhostInternalGetSet::SetCodePage(const unsigned int codepage)
{
if (IsValidCodePage(codepage))
{
DoSrvSetConsoleOutputCodePage(codepage);
DoSrvSetConsoleInputCodePage(codepage);
}
LOG_IF_FAILED(DoSrvSetConsoleOutputCodePage(codepage));
LOG_IF_FAILED(DoSrvSetConsoleInputCodePage(codepage));
}

// Routine Description:
Expand All @@ -244,8 +241,8 @@ void ConhostInternalGetSet::SetCodePage(const unsigned int codepage)
void ConhostInternalGetSet::ResetCodePage()
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
DoSrvSetConsoleOutputCodePage(gci.DefaultOutputCP);
DoSrvSetConsoleInputCodePage(gci.DefaultCP);
LOG_IF_FAILED(DoSrvSetConsoleOutputCodePage(gci.DefaultOutputCP));
LOG_IF_FAILED(DoSrvSetConsoleInputCodePage(gci.DefaultCP));
}

// Routine Description:
Expand Down

0 comments on commit 8e191f8

Please sign in to comment.