Skip to content

Commit

Permalink
Shockingly, this works, it works elevated, and it works unpackaged
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Dec 16, 2020
1 parent 1f52d35 commit 5a9cdc8
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 108 deletions.
192 changes: 91 additions & 101 deletions OpenConsole.sln

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/cascadia/Remoting/Microsoft.Terminal.RemotingLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<ClInclude Include="Peasant.h">
<DependentUpon>Peasant.idl</DependentUpon>
</ClInclude>
<ClInclude Include="WindowManager.h">
<DependentUpon>WindowManager .idl</DependentUpon>
</ClInclude>
</ItemGroup>
<!-- ========================= Cpp Files ======================== -->
<ItemGroup>
Expand All @@ -36,12 +39,16 @@
<ClCompile Include="Peasant.cpp">
<DependentUpon>Peasant.idl</DependentUpon>
</ClCompile>
<ClCompile Include="WindowManager.cpp">
<DependentUpon>WindowManager.idl</DependentUpon>
</ClCompile>
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
</ItemGroup>
<!-- ========================= idl Files ======================== -->
<ItemGroup>
<Midl Include="Monarch.idl" />
<Midl Include="Peasant.idl" />
<Midl Include="WindowManager.idl" />
</ItemGroup>
<!-- ========================= Misc Files ======================== -->
<ItemGroup>
Expand Down
67 changes: 67 additions & 0 deletions src/cascadia/Remoting/WindowManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "pch.h"
#include "WindowManager.h"
#include "MonarchFactory.h"

#include "WindowManager.g.cpp"
#include "../../types/inc/utils.hpp"

using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace ::Microsoft::Console;

namespace winrt::Microsoft::Terminal::Remoting::implementation
{
WindowManager::WindowManager()
{
_RegisterAsMonarch();
_CreateMonarch();
}
WindowManager::~WindowManager()
{
// IMPORTANT! Tear down the registration as soon as we exit. If we're not a
// real peasant window (the monarch passed our commandline to someone else),
// then the monarch dies, we don't want our registration becoming the active
// monarch!
CoRevokeClassObject(_registrationHostClass);
_registrationHostClass = 0;
}

void WindowManager::ProposeCommandline()
{
_shouldCreateWindow = true;
}

bool WindowManager::ShouldCreateWindow()
{
return _shouldCreateWindow;
}

void WindowManager::_RegisterAsMonarch()
{
winrt::check_hresult(CoRegisterClassObject(Monarch_clsid,
winrt::make<::MonarchFactory>().get(),
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE,
&_registrationHostClass));
}

void WindowManager::_CreateMonarch()
{
// Heads up! This only works because we're using
// "metadata-based-marshalling" for our WinRT types. THat means the OS is
// using the .winmd file we generate to figure out the proxy/stub
// definitions for our types automatically. This only works in the following
// cases:
//
// * If we're running unpackaged: the .winmd but be a sibling of the .exe
// * If we're running packaged: the .winmd must be in the package root
_monarch = create_instance<winrt::Microsoft::Terminal::Remoting::Monarch>(Monarch_clsid,
CLSCTX_LOCAL_SERVER);
}

// bool AppHost::_ProposeCommandlineToMonarch()
// {
// // returns true if we should create a new window
// return true;
// }
}
31 changes: 31 additions & 0 deletions src/cascadia/Remoting/WindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "WindowManager.g.h"
#include "Peasant.h"
#include "Monarch.h"
#include "../cascadia/inc/cppwinrt_utils.h"

namespace winrt::Microsoft::Terminal::Remoting::implementation
{
struct WindowManager : public WindowManagerT<WindowManager>
{
WindowManager();
~WindowManager();

void ProposeCommandline();
bool ShouldCreateWindow();

private:
bool _shouldCreateWindow{ false };
DWORD _registrationHostClass{ 0 };
winrt::Microsoft::Terminal::Remoting::Monarch _monarch{ nullptr };

void _RegisterAsMonarch();
void _CreateMonarch();
};
}

namespace winrt::Microsoft::Terminal::Remoting::factory_implementation
{
BASIC_FACTORY(WindowManager);
}
12 changes: 12 additions & 0 deletions src/cascadia/Remoting/WindowManager.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


namespace Microsoft.Terminal.Remoting
{

[default_interface] runtimeclass WindowManager
{
WindowManager();
void ProposeCommandline();
Boolean ShouldCreateWindow { get; };
};
}
41 changes: 37 additions & 4 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

#include "pch.h"
#include "AppHost.h"
// #include "MonarchFactory.h"
#include "../types/inc/Viewport.hpp"
#include "../types/inc/utils.hpp"
#include "../types/inc/User32Utils.hpp"
#include "resource.h"

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

using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Composition;
using namespace winrt::Windows::UI::Xaml;
Expand All @@ -25,17 +24,26 @@ static constexpr short KeyPressed{ gsl::narrow_cast<short>(0x8000) };

AppHost::AppHost() noexcept :
_app{},
_windowManager{},
_logic{ nullptr }, // don't make one, we're going to take a ref on app's
_window{ nullptr }
{
_logic = _app.Logic(); // get a ref to app's logic

_useNonClientArea = _logic.GetShowTabsInTitlebar();
_windowManager.ProposeCommandline();
// _RegisterAsMonarch();
// _CreateMonarch();
_shouldCreateWindow = _windowManager.ShouldCreateWindow();
if (!_shouldCreateWindow)
{
return;
}

// If there were commandline args to our process, try and process them here.
// Do this before AppLogic::Create, otherwise this will have no effect
_HandleCommandlineArgs();
_HandleCommandlineArgs(); // TODO:MG <-- This probably needs to move into _ProposeCommandlineToMonarch

_useNonClientArea = _logic.GetShowTabsInTitlebar();
if (_useNonClientArea)
{
_window = std::make_unique<NonClientIslandWindow>(_logic.GetRequestedTheme());
Expand Down Expand Up @@ -65,6 +73,7 @@ AppHost::AppHost() noexcept :
AppHost::~AppHost()
{
// destruction order is important for proper teardown here

_window = nullptr;
_app.Close();
_app = nullptr;
Expand Down Expand Up @@ -462,3 +471,27 @@ void AppHost::_WindowMouseWheeled(const til::point coord, const int32_t delta)
}
}
}

bool AppHost::HasWindow()
{
return _shouldCreateWindow;
}

// void AppHost::_RegisterAsMonarch()
// {
// winrt::check_hresult(CoRegisterClassObject(Monarch_clsid,
// winrt::make<::MonarchFactory>().get(),
// CLSCTX_LOCAL_SERVER,
// REGCLS_MULTIPLEUSE,
// &_registrationHostClass));
// }

// void AppHost::_CreateMonarch()
// {
// }

// bool AppHost::_ProposeCommandlineToMonarch()
// {
// // returns true if we should create a new window
// return true;
// }
12 changes: 9 additions & 3 deletions src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#include "pch.h"

#include <winrt/TerminalApp.h>
#include <winrt/Microsoft.Terminal.Settings.Model.h>

#include "NonClientIslandWindow.h"

class AppHost
Expand All @@ -20,12 +17,17 @@ class AppHost
bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down);
void SetTaskbarProgress(const winrt::Windows::Foundation::IInspectable& sender, const winrt::Windows::Foundation::IInspectable& args);

bool HasWindow();

private:
bool _useNonClientArea;

std::unique_ptr<IslandWindow> _window;
winrt::TerminalApp::App _app;
winrt::TerminalApp::AppLogic _logic;
bool _shouldCreateWindow{ false };
// DWORD _registrationHostClass{ 0 };
winrt::Microsoft::Terminal::Remoting::WindowManager _windowManager{ nullptr };

void _HandleCommandlineArgs();

Expand All @@ -43,4 +45,8 @@ class AppHost
void _RaiseVisualBell(const winrt::Windows::Foundation::IInspectable& sender,
const winrt::Windows::Foundation::IInspectable& arg);
void _WindowMouseWheeled(const til::point coord, const int32_t delta);

// void _RegisterAsMonarch();
// void _CreateMonarch();
// bool _ProposeCommandlineToMonarch();
};
4 changes: 4 additions & 0 deletions src/cascadia/WindowsTerminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
// Terminal App. This MUST BE constructed before the Xaml manager as TermApp
// provides an implementation of Windows.UI.Xaml.Application.
AppHost host;
if (!host.HasWindow())
{
return 0;
}

// Initialize the xaml content. This must be called AFTER the
// WindowsXamlManager is initialized.
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/WindowsTerminal/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ Module Name:
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.ui.xaml.media.h>

#include <winrt/TerminalApp.h>
#include <winrt/Microsoft.Terminal.Settings.Model.h>
#include <winrt/Microsoft.Terminal.Remoting.h>
#include <winrt/Microsoft.Terminal.TerminalControl.h>

#include <wil/resource.h>
#include <wil/win32_helpers.h>

Expand Down

0 comments on commit 5a9cdc8

Please sign in to comment.