Skip to content

Commit

Permalink
Merge remote-tracking branch 'openconsole/inbox' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett committed Sep 16, 2022
2 parents bea13bd + fba4e22 commit 6567201
Show file tree
Hide file tree
Showing 68 changed files with 628 additions and 101 deletions.
20 changes: 20 additions & 0 deletions dep/Console/ConIoSrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Licensed under the MIT license.
#define CIS_EVENT_TYPE_FOCUS (1)
#define CIS_EVENT_TYPE_FOCUS_ACK (2)

#define CIS_MSG_TYPE_MAPVIRTUALKEY (0)
#define CIS_MSG_TYPE_VKKEYSCAN (1)
#define CIS_MSG_TYPE_GETKEYSTATE (2)

#define CIS_MSG_TYPE_GETDISPLAYSIZE (3)
#define CIS_MSG_TYPE_GETFONTSIZE (4)
#define CIS_MSG_TYPE_SETCURSOR (5)
Expand All @@ -31,6 +35,22 @@ typedef struct {
UCHAR Type;

union {
struct {
UINT Code;
UINT MapType;
UINT ReturnValue;
} MapVirtualKeyParams;

struct {
WCHAR Character;
SHORT ReturnValue;
} VkKeyScanParams;

struct {
int VirtualKey;
SHORT ReturnValue;
} GetKeyStateParams;

struct {
CD_IO_DISPLAY_SIZE DisplaySize;

Expand Down
14 changes: 10 additions & 4 deletions src/audio/midi/MidiAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <dsound.h>

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dsound.lib")

using Microsoft::WRL::ComPtr;
using namespace std::chrono_literals;
Expand All @@ -20,11 +19,18 @@ constexpr auto WAVE_DATA = std::array<byte, WAVE_SIZE>{ 128, 159, 191, 223, 255,

MidiAudio::MidiAudio(HWND windowHandle)
{
if (SUCCEEDED(DirectSoundCreate8(nullptr, &_directSound, nullptr)))
_directSoundModule.reset(LoadLibraryExW(L"dsound.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32));
if (_directSoundModule)
{
if (SUCCEEDED(_directSound->SetCooperativeLevel(windowHandle, DSSCL_NORMAL)))
if (auto createFunction = GetProcAddressByFunctionDeclaration(_directSoundModule.get(), DirectSoundCreate8))
{
_createBuffers();
if (SUCCEEDED(createFunction(nullptr, &_directSound, nullptr)))
{
if (SUCCEEDED(_directSound->SetCooperativeLevel(windowHandle, DSSCL_NORMAL)))
{
_createBuffers();
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/audio/midi/MidiAudio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MidiAudio
private:
void _createBuffers() noexcept;

wil::unique_hmodule _directSoundModule;
Microsoft::WRL::ComPtr<IDirectSound8> _directSound;
std::array<Microsoft::WRL::ComPtr<IDirectSoundBuffer>, 2> _buffers;
size_t _activeBufferIndex = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/audio/midi/lib/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

3 changes: 3 additions & 0 deletions src/buffer/out/lib/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

3 changes: 3 additions & 0 deletions src/buffer/out/ut_textbuffer/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

20 changes: 20 additions & 0 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,3 +1712,23 @@ til::point Terminal::GetViewportRelativeCursorPosition() const noexcept
const auto viewport{ _GetMutableViewport() };
return absoluteCursorPosition - viewport.Origin();
}

// These functions are used by TerminalInput, which must build in conhost
// against OneCore compatible signatures. See the definitions in
// VtApiRedirection.hpp (which we cannot include cross-project.)
// Since we do nto run on OneCore, we can dispense with the compatibility
// shims.
extern "C" UINT OneCoreSafeMapVirtualKeyW(_In_ UINT uCode, _In_ UINT uMapType)
{
return MapVirtualKeyW(uCode, uMapType);
}

extern "C" SHORT OneCoreSafeVkKeyScanW(_In_ WCHAR ch)
{
return VkKeyScanW(ch);
}

extern "C" SHORT OneCoreSafeGetKeyState(_In_ int nVirtKey)
{
return GetKeyState(nVirtKey);
}
3 changes: 3 additions & 0 deletions src/host/exe/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

3 changes: 3 additions & 0 deletions src/host/ft_host/sources.dep
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

BUILD_PASS2_CONSUMES= \
onecore\windows\core\console\open\src\tools\nihilist|PASS2 \

16 changes: 8 additions & 8 deletions src/host/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,35 @@ ULONG GetControlKeyState(const LPARAM lParam)
{
ULONG ControlKeyState = 0;

if (GetKeyState(VK_LMENU) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_LMENU) & KEY_PRESSED)
{
ControlKeyState |= LEFT_ALT_PRESSED;
}
if (GetKeyState(VK_RMENU) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_RMENU) & KEY_PRESSED)
{
ControlKeyState |= RIGHT_ALT_PRESSED;
}
if (GetKeyState(VK_LCONTROL) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_LCONTROL) & KEY_PRESSED)
{
ControlKeyState |= LEFT_CTRL_PRESSED;
}
if (GetKeyState(VK_RCONTROL) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_RCONTROL) & KEY_PRESSED)
{
ControlKeyState |= RIGHT_CTRL_PRESSED;
}
if (GetKeyState(VK_SHIFT) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_SHIFT) & KEY_PRESSED)
{
ControlKeyState |= SHIFT_PRESSED;
}
if (GetKeyState(VK_NUMLOCK) & KEY_TOGGLED)
if (OneCoreSafeGetKeyState(VK_NUMLOCK) & KEY_TOGGLED)
{
ControlKeyState |= NUMLOCK_ON;
}
if (GetKeyState(VK_SCROLL) & KEY_TOGGLED)
if (OneCoreSafeGetKeyState(VK_SCROLL) & KEY_TOGGLED)
{
ControlKeyState |= SCROLLLOCK_ON;
}
if (GetKeyState(VK_CAPITAL) & KEY_TOGGLED)
if (OneCoreSafeGetKeyState(VK_CAPITAL) & KEY_TOGGLED)
{
ControlKeyState |= CAPSLOCK_ON;
}
Expand Down
3 changes: 3 additions & 0 deletions src/host/lib/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

6 changes: 3 additions & 3 deletions src/host/selectionInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Selection::KeySelectionEventResult Selection::HandleKeySelectionEvent(const INPU
FAIL_FAST_IF(!IsInSelectingState());

const auto wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
const auto ctrlPressed = WI_IsFlagSet(GetKeyState(VK_CONTROL), KEY_PRESSED);
const auto ctrlPressed = WI_IsFlagSet(OneCoreSafeGetKeyState(VK_CONTROL), KEY_PRESSED);

// if escape or ctrl-c, cancel selection
if (!IsMouseButtonDown())
Expand Down Expand Up @@ -611,7 +611,7 @@ bool Selection::HandleKeyboardLineSelectionEvent(const INPUT_KEY_INFO* const pIn
// - <none>
void Selection::CheckAndSetAlternateSelection()
{
_fUseAlternateSelection = !!(GetKeyState(VK_MENU) & KEY_PRESSED);
_fUseAlternateSelection = !!(OneCoreSafeGetKeyState(VK_MENU) & KEY_PRESSED);
}

// Routine Description:
Expand Down Expand Up @@ -904,7 +904,7 @@ bool Selection::_HandleMarkModeSelectionNav(const INPUT_KEY_INFO* const pInputKe
}

// see if shift is down. if so, we're extending the selection. otherwise, we're resetting the anchor
if (GetKeyState(VK_SHIFT) & KEY_PRESSED)
if (OneCoreSafeGetKeyState(VK_SHIFT) & KEY_PRESSED)
{
// if we're just starting to "extend" our selection from moving around as a cursor
// then attempt to set the alternate selection state based on the ALT key right now
Expand Down
2 changes: 1 addition & 1 deletion src/host/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ using Microsoft::Console::Interactivity::ServiceLocator;
}
else
{
const auto zeroVkeyData = VkKeyScanW(0);
const auto zeroVkeyData = OneCoreSafeVkKeyScanW(0);
const auto zeroVKey = LOBYTE(zeroVkeyData);
const auto zeroControlKeyState = HIBYTE(zeroVkeyData);

Expand Down
18 changes: 10 additions & 8 deletions src/host/ut_host/ClipboardTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include <cctype>

#include "../../interactivity/inc/VtApiRedirection.hpp"

#include "../../inc/consoletaeftemplates.hpp"

using namespace WEX::Common;
Expand Down Expand Up @@ -167,9 +169,9 @@ class ClipboardTests
keyEvent.reset(static_cast<KeyEvent* const>(events.front().release()));
events.pop_front();

const auto keyState = VkKeyScanW(wch);
const auto keyState = OneCoreSafeVkKeyScanW(wch);
VERIFY_ARE_NOT_EQUAL(-1, keyState);
const auto virtualScanCode = static_cast<WORD>(MapVirtualKeyW(LOBYTE(keyState), MAPVK_VK_TO_VSC));
const auto virtualScanCode = static_cast<WORD>(OneCoreSafeMapVirtualKeyW(LOBYTE(keyState), MAPVK_VK_TO_VSC));

VERIFY_ARE_EQUAL(wch, keyEvent->GetCharData());
VERIFY_ARE_EQUAL(isKeyDown, keyEvent->IsKeyDown());
Expand Down Expand Up @@ -206,9 +208,9 @@ class ClipboardTests
events.pop_front();

const short keyScanError = -1;
const auto keyState = VkKeyScanW(wch);
const auto keyState = OneCoreSafeVkKeyScanW(wch);
VERIFY_ARE_NOT_EQUAL(keyScanError, keyState);
const auto virtualScanCode = static_cast<WORD>(MapVirtualKeyW(LOBYTE(keyState), MAPVK_VK_TO_VSC));
const auto virtualScanCode = static_cast<WORD>(OneCoreSafeMapVirtualKeyW(LOBYTE(keyState), MAPVK_VK_TO_VSC));

if (std::isupper(wch))
{
Expand All @@ -222,9 +224,9 @@ class ClipboardTests
keyEvent2.reset(static_cast<KeyEvent* const>(events.front().release()));
events.pop_front();

const auto keyState2 = VkKeyScanW(wch);
const auto keyState2 = OneCoreSafeVkKeyScanW(wch);
VERIFY_ARE_NOT_EQUAL(keyScanError, keyState2);
const auto virtualScanCode2 = static_cast<WORD>(MapVirtualKeyW(LOBYTE(keyState2), MAPVK_VK_TO_VSC));
const auto virtualScanCode2 = static_cast<WORD>(OneCoreSafeMapVirtualKeyW(LOBYTE(keyState2), MAPVK_VK_TO_VSC));

if (isKeyDown)
{
Expand Down Expand Up @@ -258,9 +260,9 @@ class ClipboardTests
{
const std::wstring wstr = L"\x20ac"; // € char U+20AC

const auto keyState = VkKeyScanW(wstr[0]);
const auto keyState = OneCoreSafeVkKeyScanW(wstr[0]);
const WORD virtualKeyCode = LOBYTE(keyState);
const auto virtualScanCode = static_cast<WORD>(MapVirtualKeyW(virtualKeyCode, MAPVK_VK_TO_VSC));
const auto virtualScanCode = static_cast<WORD>(OneCoreSafeMapVirtualKeyW(virtualKeyCode, MAPVK_VK_TO_VSC));

if (keyState == -1 || HIBYTE(keyState) == 0 /* no modifiers required */)
{
Expand Down
3 changes: 3 additions & 0 deletions src/host/ut_host/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

3 changes: 3 additions & 0 deletions src/host/ut_lib/sources.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUILD_PASS1_CONSUMES= \
onecore\windows\vcpkg|PASS1 \

8 changes: 8 additions & 0 deletions src/inc/HostAndPropsheetIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ typedef NTSTATUS *PNTSTATUS;

#include <winuser.h>

#define VkKeyScanW DO_NOT_USE_VkKeyScanW_USE_OneCoreSafeVkKeyScanW
#define MapVirtualKeyW DO_NOT_USE_MapVirtualKeyW_USE_OneCoreSafeMapVirtualKeyW
#define GetKeyState DO_NOT_USE_GetKeyState_USE_OneCoreSafeGetKeyState

// This header contains some overrides for win32 APIs
// that cannot exist on OneCore
#include "../interactivity/inc/VtApiRedirection.hpp"

#include <cwchar>

// Only remaining item from w32gdip.h. There's probably a better way to do this as well.
Expand Down
7 changes: 4 additions & 3 deletions src/interactivity/base/EventSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "precomp.h"
#include "../inc/EventSynthesis.hpp"
#include "../../types/inc/convert.hpp"
#include "../inc/VtApiRedirection.hpp"

#pragma hdrstop

Expand Down Expand Up @@ -44,7 +45,7 @@ std::deque<std::unique_ptr<KeyEvent>> Microsoft::Console::Interactivity::CharToK
const unsigned int codepage)
{
const short invalidKey = -1;
auto keyState = VkKeyScanW(wch);
auto keyState = OneCoreSafeVkKeyScanW(wch);

if (keyState == invalidKey)
{
Expand Down Expand Up @@ -110,7 +111,7 @@ std::deque<std::unique_ptr<KeyEvent>> Microsoft::Console::Interactivity::Synthes
}

const auto vk = LOBYTE(keyState);
const auto virtualScanCode = gsl::narrow<WORD>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));
const auto virtualScanCode = gsl::narrow<WORD>(OneCoreSafeMapVirtualKeyW(vk, MAPVK_VK_TO_VSC));
KeyEvent keyEvent{ true, 1, LOBYTE(keyState), virtualScanCode, wch, 0 };

// add modifier flags if necessary
Expand Down Expand Up @@ -200,7 +201,7 @@ std::deque<std::unique_ptr<KeyEvent>> Microsoft::Console::Interactivity::Synthes
break;
}
const WORD virtualKey = ch - '0' + VK_NUMPAD0;
const auto virtualScanCode = gsl::narrow<WORD>(MapVirtualKeyW(virtualKey, MAPVK_VK_TO_VSC));
const auto virtualScanCode = gsl::narrow<WORD>(OneCoreSafeMapVirtualKeyW(virtualKey, MAPVK_VK_TO_VSC));

keyEvents.push_back(std::make_unique<KeyEvent>(true,
1ui16,
Expand Down
75 changes: 75 additions & 0 deletions src/interactivity/base/VtApiRedirection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license.

#include "precomp.h"
#include "../inc/VtApiRedirection.hpp"
#include "../onecore/ConIoSrvComm.hpp"
#pragma hdrstop

// The project include file defines these to be invalid symbols
// to clue in developers across the project not to use them.
//
// We have to use them here.
#undef VkKeyScanW
#undef MapVirtualKeyW
#undef GetKeyState

UINT OneCoreSafeMapVirtualKeyW(_In_ UINT uCode, _In_ UINT uMapType)
{
auto ret{ MapVirtualKeyW(uCode, uMapType) };
#ifdef BUILD_ONECORE_INTERACTIVITY
if (ret == 0)
{
const auto lastError{ GetLastError() };
if (lastError == ERROR_PROC_NOT_FOUND || lastError == ERROR_DELAY_LOAD_FAILED)
{
if (auto conIoSrvComm{ Microsoft::Console::Interactivity::OneCore::ConIoSrvComm::GetConIoSrvComm() })
{
SetLastError(0);
ret = conIoSrvComm->ConIoMapVirtualKeyW(uCode, uMapType);
}
}
}
#endif
return ret;
}

SHORT OneCoreSafeVkKeyScanW(_In_ WCHAR ch)
{
auto ret{ VkKeyScanW(ch) };
#ifdef BUILD_ONECORE_INTERACTIVITY
if (ret == -1)
{
const auto lastError{ GetLastError() };
if (lastError == ERROR_PROC_NOT_FOUND || lastError == ERROR_DELAY_LOAD_FAILED)
{
if (auto conIoSrvComm{ Microsoft::Console::Interactivity::OneCore::ConIoSrvComm::GetConIoSrvComm() })
{
SetLastError(0);
ret = conIoSrvComm->ConIoVkKeyScanW(ch);
}
}
}
#endif
return ret;
}

SHORT OneCoreSafeGetKeyState(_In_ int nVirtKey)
{
auto ret{ GetKeyState(nVirtKey) };
#ifdef BUILD_ONECORE_INTERACTIVITY
if (ret == 0)
{
const auto lastError{ GetLastError() };
if (lastError == ERROR_PROC_NOT_FOUND || lastError == ERROR_DELAY_LOAD_FAILED)
{
if (auto conIoSrvComm{ Microsoft::Console::Interactivity::OneCore::ConIoSrvComm::GetConIoSrvComm() })
{
SetLastError(0);
ret = conIoSrvComm->ConIoGetKeyState(nVirtKey);
}
}
}
#endif
return ret;
}
Loading

0 comments on commit 6567201

Please sign in to comment.