Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Monarch/Peasant sample app #8171

Merged
29 commits merged into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5dcaae1
The base of something that compiles, let's start there
zadjii-msft Oct 23, 2020
ee58bbf
This is some cross-proc communication between M&P
zadjii-msft Oct 23, 2020
c5f2350
A bit of cleanup - monarch actually tracks the peasants that attach
zadjii-msft Oct 23, 2020
53e4237
Hey the peasant can wait on the monarch process now! That's neat
zadjii-msft Oct 23, 2020
6ffba71
THE KING IS DEAD. LONG LIVE THE KING
zadjii-msft Oct 23, 2020
d786150
Merge remote-tracking branch 'origin/main' into dev/migrie/oop-monarc…
zadjii-msft Nov 3, 2020
0e28b79
Use the actual project template, because it makes our lives 1000% easier
zadjii-msft Nov 3, 2020
defce70
Separate out monarch and peasant, so they aren't so tightly coupled
zadjii-msft Nov 3, 2020
8e1ebe8
refactor for a little bit of clarity while reading
zadjii-msft Nov 3, 2020
78aaf32
this passes commandlins to the monarch
zadjii-msft Nov 3, 2020
6f0ed9a
Toss commandlines correctly between processes
zadjii-msft Nov 4, 2020
891d0bd
Add different windowing modes for the monarch
zadjii-msft Nov 4, 2020
b8aea85
Some code cleanup, commenting
zadjii-msft Nov 4, 2020
80a7f12
Actually track the last activated window
zadjii-msft Nov 4, 2020
f2ca9fa
Lots of cleanup, add a readme, fix a bug
zadjii-msft Nov 4, 2020
1eb883c
more notes
zadjii-msft Nov 4, 2020
fb442f1
a picture says 1000 words, a gif says 60000 words per second
zadjii-msft Nov 5, 2020
1ec8046
good bot
zadjii-msft Nov 5, 2020
5668438
okay bot
zadjii-msft Nov 5, 2020
5dbf042
Merge remote-tracking branch 'origin/main' into dev/migrie/oop-monarc…
zadjii-msft Dec 14, 2020
3351e01
PR nits
zadjii-msft Dec 14, 2020
0b9cd30
Turns out that packaging is making my life a nightmare
zadjii-msft Dec 14, 2020
25a7463
Thanks Brian!
zadjii-msft Dec 14, 2020
80e8413
remove dead code, and add comments
zadjii-msft Dec 15, 2020
1052508
Merge remote-tracking branch 'origin/main' into dev/migrie/oop-monarc…
zadjii-msft Jan 8, 2021
67c4ff0
okay VS
zadjii-msft Jan 8, 2021
1a34e20
save my own future sanity
zadjii-msft Jan 8, 2021
d5d0794
Merge remote-tracking branch 'origin/main' into dev/migrie/oop-monarc…
zadjii-msft Jan 19, 2021
5554818
remove some dead code
zadjii-msft Jan 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/actions/spell-check/dictionary/apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ IExplorer
IInheritable
IMap
IObject
IPeasant
IStorage
LCID
llabs
llu
lround
LSHIFT
MULTIPLEUSE
NCHITTEST
NCLBUTTONDBLCLK
NCRBUTTONDBLCLK
Expand All @@ -46,6 +49,7 @@ otms
OUTLINETEXTMETRICW
PAGESCROLL
RETURNCMD
REGCLS
rfind
roundf
RSHIFT
Expand Down
30 changes: 30 additions & 0 deletions OpenConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LocalTests_SettingsModel",
{CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907} = {CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MonarchPeasantSample", "src\tools\MonarchPeasantSample\MonarchPeasantSample.vcxproj", "{21b7ea5e-1ef8-49b6-ac07-11714af0e37d}"
ProjectSection(ProjectDependencies) = postProject
{18D09A24-8240-42D6-8CB6-236EEE820263} = {18D09A24-8240-42D6-8CB6-236EEE820263}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
AuditMode|Any CPU = AuditMode|Any CPU
Expand Down Expand Up @@ -2090,6 +2095,30 @@ Global
{CA5CAD1A-9B68-456A-B13E-C8218070DC42}.Release|x64.Build.0 = Release|x64
{CA5CAD1A-9B68-456A-B13E-C8218070DC42}.Release|x86.ActiveCfg = Release|Win32
{CA5CAD1A-9B68-456A-B13E-C8218070DC42}.Release|x86.Build.0 = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|Any CPU.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|ARM64.ActiveCfg = Release|ARM64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|DotNet_x64Test.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|DotNet_x86Test.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|x64.ActiveCfg = Release|x64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.AuditMode|x86.ActiveCfg = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|Any CPU.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|ARM64.ActiveCfg = Debug|ARM64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|ARM64.Build.0 = Debug|ARM64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|DotNet_x64Test.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|DotNet_x86Test.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|x64.ActiveCfg = Debug|x64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|x64.Build.0 = Debug|x64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|x86.ActiveCfg = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Debug|x86.Build.0 = Debug|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|Any CPU.ActiveCfg = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|ARM64.ActiveCfg = Release|ARM64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|ARM64.Build.0 = Release|ARM64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|DotNet_x64Test.ActiveCfg = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|DotNet_x86Test.ActiveCfg = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|x64.ActiveCfg = Release|x64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|x64.Build.0 = Release|x64
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|x86.ActiveCfg = Release|Win32
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2124,6 +2153,7 @@ Global
{ED82003F-FC5D-4E94-8B36-F480018ED064} = {A10C4720-DCA4-4640-9749-67F4314F527C}
{06EC74CB-9A12-429C-B551-8532EC964726} = {E8F24881-5E37-4362-B191-A3BA0ED7F4EB}
{ED82003F-FC5D-4E94-8B47-F480018ED064} = {A10C4720-DCA4-4640-9749-67F4314F527C}
{21B7EA5E-1EF8-49B6-AC07-11714AF0E37D} = {A10C4720-DCA4-4640-9749-67F4314F527C}
{06EC74CB-9A12-429C-B551-8562EC964846} = {E8F24881-5E37-4362-B191-A3BA0ED7F4EB}
{D3B92829-26CB-411A-BDA2-7F5DA3D25DD4} = {E8F24881-5E37-4362-B191-A3BA0ED7F4EB}
{C7A6A5D9-60BE-4AEB-A5F6-AFE352F86CBB} = {A10C4720-DCA4-4640-9749-67F4314F527C}
Expand Down
5 changes: 3 additions & 2 deletions consolegit2gitfilters.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"/res/terminal/",
"/doc/specs/",
"/doc/cascadia/",
"/doc/user-docs/"
"/doc/user-docs/",
"/src/tools/MonarchPeasantSample",
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
],
"SuffixFilters": [
".dbb",
Expand All @@ -38,5 +39,5 @@
".rec",
".err",
".xlsx"
]
]
}
113 changes: 113 additions & 0 deletions src/tools/MonarchPeasantSample/AppState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "pch.h"
#include <conio.h>
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
#include "Monarch.h"
#include "Peasant.h"
#include "AppState.h"
#include "../../types/inc/utils.hpp"

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

void AppState::_setupConsole()
{
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
hInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOutput, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOutput, dwMode);
}

void AppState::initializeState()
{
// Initialize the console handles
_setupConsole();

// Set up WinRT
init_apartment();
}

bool AppState::areWeTheKing(const bool logPIDs)
{
auto kingPID = monarch.GetPID();
auto ourPID = GetCurrentProcessId();
if (logPIDs)
{
if (ourPID == kingPID)
{
printf(fmt::format("We're the\x1b[33m king\x1b[m - our PID is {}\n", ourPID).c_str());
}
else
{
printf(fmt::format("We're a lowly peasant - the king is {}\n", kingPID).c_str());
}
}
return (ourPID == kingPID);
}

void AppState::remindKingWhoTheyAre(const winrt::MonarchPeasantSample::IPeasant& iPeasant)
{
winrt::com_ptr<MonarchPeasantSample::implementation::Monarch> monarchImpl;
monarchImpl.copy_from(winrt::get_self<MonarchPeasantSample::implementation::Monarch>(monarch));
if (monarchImpl)
{
auto ourID = iPeasant.GetID();
monarchImpl->SetSelfID(ourID);
monarchImpl->AddPeasant(iPeasant);
printf("The king is peasant #%lld\n", ourID);
}
else
{
// printf("Shoot, we wanted to be able to get the impl here but couldnt\n");
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}

winrt::MonarchPeasantSample::Monarch AppState::instantiateMonarch()
{
auto monarch = create_instance<winrt::MonarchPeasantSample::Monarch>(Monarch_clsid, CLSCTX_LOCAL_SERVER);
return monarch;
}

MonarchPeasantSample::IPeasant AppState::_createOurPeasant()
{
auto peasant = winrt::make_self<MonarchPeasantSample::implementation::Peasant>();
auto ourID = monarch.AddPeasant(*peasant);
printf("The monarch assigned us the ID %llu\n", ourID);

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

return *peasant;
}

void AppState::createMonarch()
{
monarch = AppState::instantiateMonarch();
}

// return true to exit early, false if we should continue into the main loop
bool AppState::processCommandline()
{
const bool isKing = areWeTheKing(false);
// 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 }, { L"placeholder CWD" });

if (createNewWindow)
{
peasant = _createOurPeasant();
peasant.ExecuteCommandline({ args }, { L"placeholder CWD" });
return false;
}
else
{
printf("The Monarch instructed us to not create a new window. We'll be exiting now.\n");
}

return true;
}
33 changes: 33 additions & 0 deletions src/tools/MonarchPeasantSample/AppState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <conio.h>
#include "Monarch.h"
#include "Peasant.h"
#include "../../types/inc/utils.hpp"

class AppState
{
public:
bool areWeTheKing(const bool logPIDs = false);
void initializeState();

static winrt::MonarchPeasantSample::Monarch instantiateMonarch();

void createMonarch();
bool processCommandline();
void remindKingWhoTheyAre(const winrt::MonarchPeasantSample::IPeasant& peasant);

HANDLE hInput{ INVALID_HANDLE_VALUE };
HANDLE hOutput{ INVALID_HANDLE_VALUE };
winrt::MonarchPeasantSample::IPeasant peasant{ nullptr };
winrt::MonarchPeasantSample::Monarch monarch{ nullptr };
std::vector<winrt::hstring> args;

private:
void _setupConsole();
int _appLoop();

winrt::MonarchPeasantSample::IPeasant _createOurPeasant();
};

bool monarchAppLoop(AppState& state); // Defined in MonarchMain.cpp
bool peasantAppLoop(AppState& state); // Defined in PeasantMain.cpp
Loading