Skip to content

Commit

Permalink
This won't work, but I'm committing this becaus I finally got it to c…
Browse files Browse the repository at this point in the history
…ompile a String[]
  • Loading branch information
zadjii-msft committed Dec 16, 2020
1 parent 5a9cdc8 commit 36539cf
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 41 deletions.
20 changes: 20 additions & 0 deletions src/cascadia/Remoting/CommandlineArgs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "pch.h"

#include "CommandlineArgs.h"
#include "CommandlineArgs.g.cpp"
using namespace winrt;
using namespace winrt::Microsoft::Terminal;
using namespace winrt::Windows::Foundation;

namespace winrt::Microsoft::Terminal::Remoting::implementation
{
void CommandlineArgs::Args(winrt::array_view<const winrt::hstring> const& value)
{
_args = { value.begin(), value.end() };
}

winrt::com_array<winrt::hstring> CommandlineArgs::Args()
{
return winrt::com_array<winrt::hstring>{ _args.begin(), _args.end() };
}
}
39 changes: 39 additions & 0 deletions src/cascadia/Remoting/CommandlineArgs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "CommandlineArgs.g.h"
#include "../cascadia/inc/cppwinrt_utils.h"

namespace winrt::Microsoft::Terminal::Remoting::implementation
{
struct CommandlineArgs : public CommandlineArgsT<CommandlineArgs>
{
public:
CommandlineArgs() :
_args{},
_cwd{ L"" }
{
}

CommandlineArgs(const winrt::array_view<const winrt::hstring>& args,
winrt::hstring currentDirectory) :
_args{ args.begin(), args.end() },
_cwd{ currentDirectory }
{
}

winrt::hstring CurrentDirectory() { return _cwd; };

void Args(winrt::array_view<const winrt::hstring> const& value);
winrt::com_array<winrt::hstring> Args();

private:
winrt::com_array<winrt::hstring> _args;
winrt::hstring _cwd;
};

}

namespace winrt::Microsoft::Terminal::Remoting::factory_implementation
{
BASIC_FACTORY(CommandlineArgs);
}
8 changes: 7 additions & 1 deletion src/cascadia/Remoting/Microsoft.Terminal.RemotingLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
<DependentUpon>Peasant.idl</DependentUpon>
</ClInclude>
<ClInclude Include="WindowManager.h">
<DependentUpon>WindowManager .idl</DependentUpon>
<DependentUpon>WindowManager.idl</DependentUpon>
</ClInclude>
<ClInclude Include="CommandlineArgs.h">
<DependentUpon>Peasant.idl</DependentUpon>
</ClInclude>
</ItemGroup>
<!-- ========================= Cpp Files ======================== -->
Expand All @@ -42,6 +45,9 @@
<ClCompile Include="WindowManager.cpp">
<DependentUpon>WindowManager.idl</DependentUpon>
</ClCompile>
<ClCompile Include="CommandlineArgs.cpp">
<DependentUpon>Peasant.idl</DependentUpon>
</ClCompile>
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
</ItemGroup>
<!-- ========================= idl Files ======================== -->
Expand Down
13 changes: 9 additions & 4 deletions src/cascadia/Remoting/Monarch.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pch.h"
#include "Monarch.h"
#include "CommandlineArgs.h"

#include "Monarch.g.cpp"
#include "../../types/inc/utils.hpp"
Expand Down Expand Up @@ -91,7 +92,8 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
_setMostRecentPeasant(_thisPeasantID);
}

bool Monarch::ProposeCommandline(array_view<const winrt::hstring> args, winrt::hstring cwd)
bool Monarch::ProposeCommandline(array_view<const winrt::hstring> args,
winrt::hstring cwd)
{
auto argsProcessed = 0;
std::wstring fullCmdline;
Expand Down Expand Up @@ -125,15 +127,17 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
printf("Session 0 is actually #%llu\n", _mostRecentPeasant);
if (auto mruPeasant = _getPeasant(_mostRecentPeasant))
{
mruPeasant.ExecuteCommandline(args, cwd);
auto eventArgs = winrt::make_self<implementation::CommandlineArgs>(args, cwd);
mruPeasant.ExecuteCommandline(*eventArgs);
createNewWindow = false;
}
}
else
{
if (auto otherPeasant = _getPeasant(sessionId))
{
otherPeasant.ExecuteCommandline(args, cwd);
auto eventArgs = winrt::make_self<implementation::CommandlineArgs>(args, cwd);
otherPeasant.ExecuteCommandline(*eventArgs);
createNewWindow = false;
}
else
Expand All @@ -147,7 +151,8 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
{
if (auto mruPeasant = _getPeasant(_mostRecentPeasant))
{
mruPeasant.ExecuteCommandline(args, cwd);
auto eventArgs = winrt::make_self<implementation::CommandlineArgs>(args, cwd);
mruPeasant.ExecuteCommandline(*eventArgs);
createNewWindow = false;
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/cascadia/Remoting/Peasant.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "pch.h"
#include "Peasant.h"

#include "CommandlineArgs.h"
#include "Peasant.g.cpp"
#include "../../types/inc/utils.hpp"

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

Expand All @@ -28,18 +29,25 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
return GetCurrentProcessId();
}

bool Peasant::ExecuteCommandline(winrt::array_view<const winrt::hstring> args, winrt::hstring currentDirectory)
bool Peasant::ExecuteCommandline(const Remoting::CommandlineArgs& args)
{
auto argsProcessed = 0;
std::wstring fullCmdline;
for (const auto& arg : args)
if (_initialArgs == nullptr)
{
fullCmdline += argsProcessed++ == 0 ? L"sample.exe" : arg;
fullCmdline += L" ";
_initialArgs = args;
}
wprintf(L"\x1b[32mExecuted Commandline\x1b[m: \"");
wprintf(fullCmdline.c_str());
wprintf(L"\"\n");

_ExecuteCommandlineRequestedHandlers(*this, args);

// auto argsProcessed = 0;
// std::wstring fullCmdline;
// for (const auto& arg : args)
// {
// fullCmdline += argsProcessed++ == 0 ? L"sample.exe" : arg;
// fullCmdline += L" ";
// }
// wprintf(L"\x1b[32mExecuted Commandline\x1b[m: \"");
// wprintf(fullCmdline.c_str());
// wprintf(L"\"\n");
return true;
}

Expand All @@ -48,4 +56,9 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
_WindowActivatedHandlers(*this, nullptr);
}

Remoting::CommandlineArgs Peasant::InitialArgs()
{
return _initialArgs;
}

}
11 changes: 8 additions & 3 deletions src/cascadia/Remoting/Peasant.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
uint64_t GetID();
uint64_t GetPID();

bool ExecuteCommandline(winrt::array_view<const winrt::hstring> args,
winrt::hstring currentDirectory);
bool ExecuteCommandline(const winrt::Microsoft::Terminal::Remoting::CommandlineArgs& args);

void raiseActivatedEvent();

winrt::Microsoft::Terminal::Remoting::CommandlineArgs InitialArgs();
TYPED_EVENT(WindowActivated, winrt::Windows::Foundation::IInspectable, winrt::Windows::Foundation::IInspectable);
TYPED_EVENT(ExecuteCommandlineRequested, winrt::Windows::Foundation::IInspectable, winrt::Microsoft::Terminal::Remoting::CommandlineArgs);

private:
uint64_t _id{ 0 };

winrt::Microsoft::Terminal::Remoting::CommandlineArgs _initialArgs; //{ nullptr };

// array_view<const winrt::hstring> _args;
// winrt::hstring _cwd;
};
}

Expand Down
15 changes: 14 additions & 1 deletion src/cascadia/Remoting/Peasant.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

namespace Microsoft.Terminal.Remoting
{

runtimeclass CommandlineArgs
{
CommandlineArgs();
CommandlineArgs(String[] args, String cwd);

String[] Args { get; set; };
String CurrentDirectory();
};

interface IPeasant
{
CommandlineArgs InitialArgs { get; };

void AssignID(UInt64 id);
UInt64 GetID();
UInt64 GetPID();
Boolean ExecuteCommandline(String[] args, String currentDirectory);
Boolean ExecuteCommandline(CommandlineArgs args);
event Windows.Foundation.TypedEventHandler<Object, Object> WindowActivated;
event Windows.Foundation.TypedEventHandler<Object, CommandlineArgs> ExecuteCommandlineRequested;
};

[default_interface] runtimeclass Peasant : IPeasant
Expand Down
70 changes: 58 additions & 12 deletions src/cascadia/Remoting/WindowManager.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#include "pch.h"
#include "WindowManager.h"
#include "MonarchFactory.h"
#include "CommandlineArgs.h"

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

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

namespace winrt::Microsoft::Terminal::Remoting::implementation
{
WindowManager::WindowManager()
{
_RegisterAsMonarch();
_CreateMonarch();
_registerAsMonarch();
_createMonarch();
}
WindowManager::~WindowManager()
{
Expand All @@ -26,8 +28,29 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
_registrationHostClass = 0;
}

void WindowManager::ProposeCommandline()
void WindowManager::ProposeCommandline(array_view<const winrt::hstring> args, const winrt::hstring cwd)
{
const bool isKing = _areWeTheKing();
// If we're the king, we _definitely_ want to process the arguments, we were
// launched with them!
//
// Otherwise, the King will tell us if we should make a new window
const bool createNewWindow = isKing ||
_monarch.ProposeCommandline(args, cwd);

if (createNewWindow)
{
_createOurPeasant();

auto eventArgs = winrt::make_self<implementation::CommandlineArgs>(args, cwd);
_peasant.ExecuteCommandline(*eventArgs);
_shouldCreateWindow = false;
}
else
{
// printf("The Monarch instructed us to not create a new window. We'll be exiting now.\n");
}

_shouldCreateWindow = true;
}

Expand All @@ -36,7 +59,7 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
return _shouldCreateWindow;
}

void WindowManager::_RegisterAsMonarch()
void WindowManager::_registerAsMonarch()
{
winrt::check_hresult(CoRegisterClassObject(Monarch_clsid,
winrt::make<::MonarchFactory>().get(),
Expand All @@ -45,7 +68,7 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
&_registrationHostClass));
}

void WindowManager::_CreateMonarch()
void WindowManager::_createMonarch()
{
// Heads up! This only works because we're using
// "metadata-based-marshalling" for our WinRT types. THat means the OS is
Expand All @@ -55,13 +78,36 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
//
// * 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);
_monarch = create_instance<Remoting::Monarch>(Monarch_clsid,
CLSCTX_LOCAL_SERVER);
}

bool WindowManager::_areWeTheKing()
{
auto kingPID = _monarch.GetPID();
auto ourPID = GetCurrentProcessId();
return (ourPID == kingPID);
}

Remoting::IPeasant WindowManager::_createOurPeasant()
{
auto p = winrt::make_self<Remoting::implementation::Peasant>();
_peasant = *p;
auto ourID = _monarch.AddPeasant(_peasant);
ourID;
// printf("The monarch assigned us the ID %llu\n", ourID);

// if (areWeTheKing())
// {
// remindKingWhoTheyAre(*peasant);
// }

return _peasant;
}

Remoting::Peasant WindowManager::CurrentWindow()
{
return _peasant;
}

// bool AppHost::_ProposeCommandlineToMonarch()
// {
// // returns true if we should create a new window
// return true;
// }
}
11 changes: 8 additions & 3 deletions src/cascadia/Remoting/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
WindowManager();
~WindowManager();

void ProposeCommandline();
void ProposeCommandline(array_view<const winrt::hstring> args, const winrt::hstring cwd);
bool ShouldCreateWindow();

winrt::Microsoft::Terminal::Remoting::Peasant CurrentWindow();

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

void _RegisterAsMonarch();
void _CreateMonarch();
void _registerAsMonarch();
void _createMonarch();
bool _areWeTheKing();
winrt::Microsoft::Terminal::Remoting::IPeasant _createOurPeasant();
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/Remoting/WindowManager.idl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "Peasant.idl";


namespace Microsoft.Terminal.Remoting
Expand All @@ -6,7 +7,8 @@ namespace Microsoft.Terminal.Remoting
[default_interface] runtimeclass WindowManager
{
WindowManager();
void ProposeCommandline();
void ProposeCommandline(String[] commands, String cwd);
Boolean ShouldCreateWindow { get; };
Peasant CurrentWindow();
};
}
Loading

0 comments on commit 36539cf

Please sign in to comment.