Skip to content

Commit

Permalink
Some cleanup of things.
Browse files Browse the repository at this point in the history
  • Loading branch information
miniksa committed Sep 10, 2020
1 parent 7f54db2 commit a510cd5
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/cascadia/CascadiaPackage/Package-Dev.appxmanifest
Expand Up @@ -93,7 +93,7 @@
</uap3:Extension>
<com:Extension Category="windows.comInterface">
<com:ComInterface>
<com:ProxyStub Id="DEC4804D-56D1-4F73-9FBE-6828E7C85C56" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleDll.dll"/>
<com:ProxyStub Id="DEC4804D-56D1-4F73-9FBE-6828E7C85C56" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleProxy.dll"/>
<com:Interface Id="2B607BC1-43EB-40C3-95AE-2856ADDB7F23" ProxyStubClsid="DEC4804D-56D1-4F73-9FBE-6828E7C85C56"/>
<com:Interface Id="FA1E3AB4-9AEC-4A3C-96CA-E6078C30BD74" ProxyStubClsid="DEC4804D-56D1-4F73-9FBE-6828E7C85C56"/>
</com:ComInterface>
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/CascadiaPackage/Package-Pre.appxmanifest
Expand Up @@ -94,7 +94,7 @@
</uap3:Extension>
<com:Extension Category="windows.comInterface">
<com:ComInterface>
<com:ProxyStub Id="1833E661-CC81-4DD0-87C6-C2F74BD39EFA" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleDll.dll"/>
<com:ProxyStub Id="1833E661-CC81-4DD0-87C6-C2F74BD39EFA" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleProxy.dll"/>
<com:Interface Id="2B607BC1-43EB-40C3-95AE-2856ADDB7F23" ProxyStubClsid="1833E661-CC81-4DD0-87C6-C2F74BD39EFA"/>
<com:Interface Id="FA1E3AB4-9AEC-4A3C-96CA-E6078C30BD74" ProxyStubClsid="1833E661-CC81-4DD0-87C6-C2F74BD39EFA"/>
</com:ComInterface>
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/CascadiaPackage/Package.appxmanifest
Expand Up @@ -94,7 +94,7 @@
</uap3:Extension>
<com:Extension Category="windows.comInterface">
<com:ComInterface>
<com:ProxyStub Id="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleDll.dll"/>
<com:ProxyStub Id="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleProxy.dll"/>
<com:Interface Id="2B607BC1-43EB-40C3-95AE-2856ADDB7F23" ProxyStubClsid="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F"/>
<com:Interface Id="FA1E3AB4-9AEC-4A3C-96CA-E6078C30BD74" ProxyStubClsid="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F"/>
</com:ComInterface>
Expand Down
11 changes: 0 additions & 11 deletions src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Expand Up @@ -174,17 +174,6 @@ void AppCommandlineArgs::_buildParser()
};
_app.add_flag_function("-v,--version", versionCallback, RS_A(L"CmdVersionDesc"));

// Started explicitly as a COM server for incoming PTY flags
// /Embedding (we don't pick this, it's just how COM does it.)
auto comEmbeddingCallback = [this](std::string param) {
if ("mbedding" == param)
{
auto embeddingAction = winrt::make_self<implementation::ActionAndArgs>();
embeddingAction->Action(ShortcutAction::ToggleInboundPty);
_startupActions.push_back(*embeddingAction);
}
};

// Maximized and Fullscreen flags
// -M,--maximized: Maximizes the window on launch
// -F,--fullscreen: Fullscreens the window on launch
Expand Down
2 changes: 0 additions & 2 deletions src/cascadia/TerminalApp/AppCommandlineArgs.h
Expand Up @@ -6,8 +6,6 @@

#include "Commandline.h"

#include <winrt/Microsoft.Terminal.TerminalConnection.h>

#ifdef UNIT_TESTING
// fwdecl unittest classes
namespace TerminalAppLocalTests
Expand Down
1 change: 0 additions & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Expand Up @@ -9,7 +9,6 @@
#include "Profile.h"

#include <winrt/Microsoft.Terminal.TerminalControl.h>
//#include <winrt/Microsoft.Terminal.TerminalConnection.h>

#include "AppCommandlineArgs.h"

Expand Down
43 changes: 22 additions & 21 deletions src/cascadia/TerminalConnection/CTerminalHandoff.cpp
Expand Up @@ -2,25 +2,25 @@

#include "CTerminalHandoff.h"

// The callback function when a connection is received
static NewHandoff _pfnHandoff = nullptr;

// The registration ID of the class object for deregistration later
static DWORD g_cTerminalHandoffRegistration = 0;

// Routine Description:
// - Called back when COM says there is nothing left for our server to do and we can tear down.
void _releaseNotifier() noexcept
{
CTerminalHandoff::StopListening();
}

HRESULT CTerminalHandoff::StartListening(NewHandoff pfnHandoff)
// - Starts listening for TerminalHandoff requests by registering
// our class and interface with COM.
// Arguments:
// - pfnHandoff - Function to callback when a handoff is received
// Return Value:
// - S_OK, E_NOT_VALID_STATE (start called when already started) or relevant COM registration error.
HRESULT CTerminalHandoff::s_StartListening(NewHandoff pfnHandoff)
{
// has to be because the rest of TerminalConnection is apartment threaded already
// also is it really necessary if the rest of it already is?
RETURN_IF_FAILED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED));
RETURN_HR_IF(E_NOT_VALID_STATE, _pfnHandoff != nullptr);

// We could probably hold this in a static...
auto classFactory = Make<SimpleClassFactory<CTerminalHandoff>>();

RETURN_IF_NULL_ALLOC(classFactory);

ComPtr<IUnknown> unk;
Expand All @@ -33,8 +33,17 @@ HRESULT CTerminalHandoff::StartListening(NewHandoff pfnHandoff)
return S_OK;
}

HRESULT CTerminalHandoff::StopListening()
// Routine Description:
// - Stops listening for TerminalHandoff requests by deregistering
// our class and interface with COM
// Arguments:
// - <none>
// Return Value:
// - S_OK, E_NOT_VALID_STATE (stop called when not started), or relevant COM deregistration error
HRESULT CTerminalHandoff::s_StopListening()
{
RETURN_HR_IF(E_NOT_VALID_STATE, _pfnHandoff == nullptr);

_pfnHandoff = nullptr;

if (g_cTerminalHandoffRegistration)
Expand All @@ -43,8 +52,6 @@ HRESULT CTerminalHandoff::StopListening()
g_cTerminalHandoffRegistration = 0;
}

CoUninitialize();

return S_OK;
}

Expand All @@ -58,7 +65,7 @@ HRESULT CTerminalHandoff::StopListening()
// - S_OK or Win32 error from `::DuplicateHandle`
HRESULT _duplicateHandle(const HANDLE in, HANDLE& out)
{
RETURN_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), in, GetCurrentProcess(), &out, 0, FALSE, DUPLICATE_SAME_ACCESS));
RETURN_IF_WIN32_BOOL_FALSE(::DuplicateHandle(GetCurrentProcess(), in, GetCurrentProcess(), &out, 0, FALSE, DUPLICATE_SAME_ACCESS));
return S_OK;
}

Expand All @@ -74,11 +81,5 @@ HRESULT CTerminalHandoff::EstablishHandoff(HANDLE in, HANDLE out, HANDLE signal)

// Call registered handler from when we started listening.
return _pfnHandoff(in, out, signal);

//return S_OK;
}

HRESULT CTerminalHandoff::DoNothing()
{
return S_FALSE;
}
5 changes: 2 additions & 3 deletions src/cascadia/TerminalConnection/CTerminalHandoff.h
Expand Up @@ -14,11 +14,10 @@ struct __declspec(uuid(__CLSID_CTerminalHandoff))
HANDLE out,
HANDLE signal);

STDMETHODIMP DoNothing();
#pragma endregion

static HRESULT StartListening(NewHandoff pfnHandoff);
static HRESULT StopListening();
static HRESULT s_StartListening(NewHandoff pfnHandoff);
static HRESULT s_StopListening();
};

CoCreatableClass(CTerminalHandoff);
4 changes: 2 additions & 2 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Expand Up @@ -499,11 +499,11 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

void ConptyConnection::StartInboundListener()
{
THROW_IF_FAILED(CTerminalHandoff::StartListening(&ConptyConnection::NewHandoff));
THROW_IF_FAILED(CTerminalHandoff::s_StartListening(&ConptyConnection::NewHandoff));
}

void ConptyConnection::StopInboundListener()
{
THROW_IF_FAILED(CTerminalHandoff::StopListening());
THROW_IF_FAILED(CTerminalHandoff::s_StopListening());
}
}
8 changes: 4 additions & 4 deletions src/host/dll/Host.DLL.vcxproj
Expand Up @@ -4,8 +4,8 @@
<ProjectGuid>{E437B604-3E98-4F40-A927-E173E818EA4B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>host</RootNamespace>
<ProjectName>Host.DLL</ProjectName>
<TargetName>OpenConsoleDll</TargetName>
<ProjectName>Host.ProxyDLL</ProjectName>
<TargetName>OpenConsoleProxy</TargetName>
<ConfigurationType>DynamicLibrary</ConfigurationType>
</PropertyGroup>
<Import Project="$(SolutionDir)src\common.build.pre.props" />
Expand All @@ -20,7 +20,7 @@
<ResourceCompile Include="Host.DLL.rc" />
</ItemGroup>
<ItemGroup>
<None Include="OpenConsoleDll.def" />
<None Include="OpenConsoleProxy.def" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ConsoleComGuids.h" />
Expand All @@ -43,7 +43,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<Link>
<ModuleDefinitionFile>OpenConsoleDll.def</ModuleDefinitionFile>
<ModuleDefinitionFile>OpenConsoleProxy.def</ModuleDefinitionFile>
<AllowIsolation>true</AllowIsolation>
<AdditionalManifestDependencies>type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'</AdditionalManifestDependencies>
</Link>
Expand Down
10 changes: 5 additions & 5 deletions src/host/dll/Host.DLL.vcxproj.filters
Expand Up @@ -39,11 +39,6 @@
<ItemGroup>
<Natvis Include="$(SolutionDir)tools\ConsoleTypes.natvis" />
</ItemGroup>
<ItemGroup>
<None Include="OpenConsoleDll.def">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="IConsoleHandoff_h.h">
<Filter>Header Files</Filter>
Expand All @@ -63,4 +58,9 @@
<Filter>Source Files</Filter>
</Midl>
</ItemGroup>
<ItemGroup>
<None Include="OpenConsoleProxy.def">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/host/dll/ITerminalHandoff.idl
Expand Up @@ -9,8 +9,6 @@ cpp_quote("#include \"ConsoleComGuids.h\"")
uuid(__IID_ITerminalHandoff)
] interface ITerminalHandoff : IUnknown
{
HRESULT DoNothing();

HRESULT EstablishHandoff([in, system_handle(sh_pipe)] HANDLE in,
[in, system_handle(sh_pipe)] HANDLE out,
[in, system_handle(sh_pipe)] HANDLE signal);
Expand Down
File renamed without changes.
71 changes: 28 additions & 43 deletions src/host/exe/CConsoleHandoff.cpp
Expand Up @@ -4,6 +4,31 @@

#include "srvinit.h"

// Define a specialization of WRL::Module so we can specify a REGCLS_SINGLEUSE type server.
// We would like to use all the conveniences afforded to us by WRL::Module<T>, but it only
// creates REGCLS_MULTIPLEUSE with no override. This makes an override for it by taking advantage
// of its existing virtual declarations.
#pragma region Single Use Out of Proc Specialization
template<int RegClsType>
class DefaultOutOfProcModuleWithRegistrationFlag;

template<int RegClsType, typename ModuleT = DefaultOutOfProcModuleWithRegistrationFlag<RegClsType>>
class OutOfProcModuleWithRegistrationFlag : public Microsoft::WRL::Module<Microsoft::WRL::ModuleType::OutOfProc, ModuleT>
{
public:
STDMETHOD(RegisterCOMObject)
(_In_opt_z_ const wchar_t* serverName, _In_reads_(count) IID* clsids, _In_reads_(count) IClassFactory** factories, _Inout_updates_(count) DWORD* cookies, unsigned int count)
{
return Microsoft::WRL::Details::RegisterCOMObject<RegClsType>(serverName, clsids, factories, cookies, count);
}
};

template<int RegClsType>
class DefaultOutOfProcModuleWithRegistrationFlag : public OutOfProcModuleWithRegistrationFlag<RegClsType, DefaultOutOfProcModuleWithRegistrationFlag<RegClsType>>
{
};
#pragma endregion

// Holds the wwinmain open until COM tells us there are no more server connections
wil::unique_event _exitEvent;

Expand All @@ -14,32 +39,6 @@ void _releaseNotifier() noexcept
_exitEvent.SetEvent();
}

DWORD g_cTerminalHandoffRegistration = 0;

//template<int RegClsType>
//class DefaultOutOfProcModuleWithRegistrationFlag;
//
//template<int RegClsType, typename ModuleT = DefaultOutOfProcModuleWithRegistrationFlag<RegClsType>>
//class OutOfProcModuleWithRegistrationFlag : public Microsoft::WRL::Module<Microsoft::WRL::ModuleType::OutOfProc, ModuleT>
//{
//public:
// STDMETHOD(RegisterCOMObject)
// (_In_opt_z_ const wchar_t* serverName, _In_reads_(count) IID* clsids, _In_reads_(count) IClassFactory** factories, _Inout_updates_(count) DWORD* cookies, unsigned int count)
// {
// return Microsoft::WRL::Details::RegisterCOMObject<RegClsType>(serverName, clsids, factories, cookies, count);
// }
//};
//
//template<int RegClsType>
//class DefaultOutOfProcModuleWithRegistrationFlag : public OutOfProcModuleWithRegistrationFlag<RegClsType, DefaultOutOfProcModuleWithRegistrationFlag<RegClsType>>
//{
//};
//
//int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
//{
// OutOfProcModuleWithRegistrationFlag<REGCLS_SINGLEUSE>::Create();
//}

// Routine Description:
// - Performs registrations for our COM types and waits until COM tells us we're done being a server.
// Return Value:
Expand All @@ -49,27 +48,13 @@ try
{
_exitEvent.create();


// has to be because the rest of TerminalConnection is apartment threaded already
// also is it really necessary if the rest of it already is?
RETURN_IF_FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED));

// We could probably hold this in a static...
auto classFactory = Make<SimpleClassFactory<CConsoleHandoff>>();
RETURN_IF_NULL_ALLOC(classFactory);

ComPtr<IUnknown> unk;
RETURN_IF_FAILED(classFactory.As(&unk));

RETURN_IF_FAILED(CoRegisterClassObject(__uuidof(CConsoleHandoff), unk.Get(), CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &g_cTerminalHandoffRegistration));

/* auto uninit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
auto& module = Module<OutOfProc>::Create(&_releaseNotifier);
RETURN_IF_FAILED(module.RegisterObjects());*/
auto& module = OutOfProcModuleWithRegistrationFlag<REGCLS_SINGLEUSE>::Create(&_releaseNotifier);

RETURN_IF_FAILED(module.RegisterObjects());
_exitEvent.wait();

//RETURN_IF_FAILED(module.UnregisterObjects());
RETURN_IF_FAILED(module.UnregisterObjects());

return S_OK;
}
Expand Down
10 changes: 4 additions & 6 deletions src/host/srvinit.h
Expand Up @@ -28,11 +28,9 @@ PWSTR TranslateConsoleTitle(_In_ PCWSTR pwszConsoleTitle, const BOOL fUnexpand,

[[nodiscard]] bool ConsoleConnectionDeservesVisibleWindow(PCONSOLE_API_CONNECTINFO p);

extern "C" __declspec(dllexport) HRESULT ConsoleEstablishHandoff(_In_ HANDLE Server,
const ConsoleArguments* const args, // this can't stay like this because ConsoleArguments could change...
HANDLE driverInputEvent,
PCONSOLE_API_MSG connectMessage);


HRESULT ConsoleEstablishHandoff(_In_ HANDLE Server,
const ConsoleArguments* const args,
HANDLE driverInputEvent,
PCONSOLE_API_MSG connectMessage);

void ConsoleCheckDebug();
9 changes: 3 additions & 6 deletions src/server/IoDispatchers.cpp
Expand Up @@ -18,8 +18,6 @@

#include "..\interactivity\inc\ServiceLocator.hpp"

#include "..\host\exe\CConsoleHandoff.h"

using namespace Microsoft::Console::Interactivity;

// From ntstatus.h, which we cannot include without causing a bunch of other conflicts. So we just include the one code we need.
Expand Down Expand Up @@ -187,12 +185,11 @@ PCONSOLE_API_MSG IoDispatchers::ConsoleHandleConnectionRequest(_In_ PCONSOLE_API
Globals.hInputEvent.get(),
Globals.launchArgs.GetVtInHandle(),
Globals.launchArgs.GetVtOutHandle(),
L"test", // TODO: this and the VT args aren't right yet.
Globals.launchArgs.GetOriginalCommandLine().data(),
&msg));

/*ConsoleArguments args;
THROW_IF_FAILED(ConsoleEstablishHandoff(Globals.pDeviceComm->_Server.get(), &args, Globals.hInputEvent.get(), pReceiveMsg));
UnlockConsole();*/
// TODO: anymore cleanup of stuff we're holding onto like handles?
UnlockConsole();

ExitThread(S_OK);
}
Expand Down

1 comment on commit a510cd5

@github-actions

This comment was marked as outdated.

Please sign in to comment.