Skip to content

Commit ad1e6bf

Browse files
committed
* Automatically creates an entry with logo in Windows Terminal
* In App Settings, it is possible to set Pengwin launch at startup
1 parent 0ae7587 commit ad1e6bf

18 files changed

+173
-54
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,6 @@ DistroLauncher/messages.h
295295
*.lib
296296
*.dll
297297
*.exe
298-
*.tar.gz
298+
*.tar.gz
299+
/ARM64/install.tar - Copy.gz
300+
/x64/install.tar - Copy.gz

DistroLauncher/DistributionInfo.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ void RunProcess(LPWSTR cmdline)
2626
FALSE, // Set handle inheritance to FALSE
2727
0, // Opens file in a separate console
2828
nullptr, // Use parent's environment block
29-
nullptr, // Use parent's starting directory
29+
nullptr, // Use parent's starting directory
3030
&si, // Pointer to STARTUPINFO structure
3131
&pi // Pointer to PROCESS_INFORMATION structure
3232
);
3333

3434
// Wait until child process exits.
3535
WaitForSingleObject(pi.hProcess, INFINITE);
3636

37-
// Close process and thread handles.
37+
// Close process and thread handles.
3838
CloseHandle(pi.hProcess);
3939
CloseHandle(pi.hThread);
4040
}

DistroLauncher/DistroLauncher.cpp

+63-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55

66
#include "stdafx.h"
77

8-
// Commandline arguments:
8+
// Commandline arguments:
99
#define ARG_CONFIG L"config"
1010
#define ARG_CONFIG_DEFAULT_USER L"--default-user"
1111
#define ARG_INSTALL L"install"
1212
#define ARG_INSTALL_ROOT L"--root"
1313
#define ARG_RUN L"run"
1414
#define ARG_RUN_C L"-c"
1515

16+
#include <winrt/Windows.Foundation.h>
17+
#include <winrt/Windows.Storage.h>
18+
19+
20+
using namespace winrt;
21+
using namespace Windows::UI::ViewManagement;
22+
using namespace Windows::Foundation;
23+
using namespace Windows::System;
24+
using namespace Windows::Storage;
25+
26+
1627
// Helper class for calling WSL Functions:
1728
// https://msdn.microsoft.com/en-us/library/windows/desktop/mt826874(v=vs.85).aspx
1829
WslApiLoader g_wslApi(DistributionInfo::Name);
@@ -70,7 +81,7 @@ HRESULT SetDefaultUser(std::wstring_view userName)
7081
return E_INVALIDARG;
7182
}
7283

73-
auto hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
84+
const auto hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
7485
if (FAILED(hr))
7586
{
7687
return hr;
@@ -81,7 +92,52 @@ HRESULT SetDefaultUser(std::wstring_view userName)
8192
return hr;
8293
}
8394

84-
int wmain(int argc, wchar_t const* argv[])
95+
int RetrieveCurrentTheme()
96+
{
97+
DWORD value = 0;
98+
DWORD size = sizeof(value);
99+
100+
// ReSharper disable once CppTooWideScope
101+
const auto status = RegGetValueW(HKEY_CURRENT_USER,
102+
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
103+
L"AppsUseLightTheme",
104+
RRF_RT_DWORD,
105+
nullptr,
106+
&value,
107+
&size
108+
);
109+
110+
if (status == ERROR_SUCCESS)
111+
{
112+
return value;
113+
}
114+
115+
return -1;
116+
}
117+
118+
fire_and_forget SyncIcons()
119+
{
120+
const int value = RetrieveCurrentTheme();
121+
const hstring nameSuffix = value == 0 ? L"" : L"";
122+
const hstring iconName = L"pengwin";
123+
124+
const hstring extension = L".png";
125+
const hstring composedPath = iconName + nameSuffix + extension;
126+
const auto path = Uri(L"ms-appx:///Assets/" + composedPath);
127+
128+
try
129+
{
130+
const auto iconFile = StorageFile::GetFileFromApplicationUriAsync(path).get();
131+
132+
co_await iconFile.CopyAsync(ApplicationData::Current().LocalFolder(), iconName + extension,
133+
NameCollisionOption::ReplaceExisting);
134+
}
135+
catch (...)
136+
{
137+
}
138+
}
139+
140+
int wmain(int argc, const wchar_t* argv[])
85141
{
86142
// Update the title bar of the console window.
87143
SetConsoleTitleW(DistributionInfo::WindowTitle.c_str());
@@ -107,12 +163,12 @@ int wmain(int argc, wchar_t const* argv[])
107163
}
108164

109165
// Install the distribution if it is not already.
110-
auto installOnly = ((arguments.size() > 0) && (arguments[0] == ARG_INSTALL));
166+
const auto installOnly = ((arguments.size() > 0) && (arguments[0] == ARG_INSTALL));
111167
auto hr = S_OK;
112168
if (!g_wslApi.WslIsDistributionRegistered())
113169
{
114170
// If the "--root" option is specified, do not create a user account.
115-
auto useRoot = ((installOnly) && (arguments.size() > 1) && (arguments[1] == ARG_INSTALL_ROOT));
171+
const auto useRoot = ((installOnly) && (arguments.size() > 1) && (arguments[1] == ARG_INSTALL_ROOT));
116172
hr = InstallDistribution(!useRoot);
117173
if (FAILED(hr))
118174
{
@@ -132,6 +188,8 @@ int wmain(int argc, wchar_t const* argv[])
132188
// Parse the command line arguments.
133189
if ((SUCCEEDED(hr)) && (!installOnly))
134190
{
191+
SyncIcons();
192+
135193
if (arguments.empty())
136194
{
137195
hr = g_wslApi.WslLaunchInteractive(L"", false, &exitCode);

DistroLauncher/DistroLauncher.vcxproj

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props')" />
34
<ItemGroup Label="ProjectConfigurations">
45
<ProjectConfiguration Include="Debug|ARM64">
56
<Configuration>Debug</Configuration>
@@ -22,7 +23,7 @@
2223
<ProjectGuid>{BA627106-E5F7-46EE-B8D7-2D5A760F2FB2}</ProjectGuid>
2324
<Keyword>Win32Proj</Keyword>
2425
<RootNamespace>DistroLauncher</RootNamespace>
25-
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
26+
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
2627
<ProjectName>Launcher</ProjectName>
2728
</PropertyGroup>
2829
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
@@ -181,7 +182,18 @@
181182
<Outputs>%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
182183
</CustomBuild>
183184
</ItemGroup>
185+
<ItemGroup>
186+
<None Include="packages.config" />
187+
</ItemGroup>
184188
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
185189
<ImportGroup Label="ExtensionTargets">
190+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets')" />
186191
</ImportGroup>
192+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
193+
<PropertyGroup>
194+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
195+
</PropertyGroup>
196+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props'))" />
197+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets'))" />
198+
</Target>
187199
</Project>

DistroLauncher/DistroLauncher.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@
6464
<ItemGroup>
6565
<CustomBuild Include="messages.mc" />
6666
</ItemGroup>
67+
<ItemGroup>
68+
<None Include="packages.config" />
69+
</ItemGroup>
6770
</Project>

DistroLauncher/Helpers.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace
1414
std::wstring Helpers::GetUserInput(DWORD promptMsg, DWORD maxCharacters)
1515
{
1616
PrintMessage(promptMsg);
17-
size_t bufferSize = maxCharacters + 1;
18-
std::unique_ptr<wchar_t[]> inputBuffer(new wchar_t[bufferSize]);
17+
const size_t bufferSize = maxCharacters + 1;
18+
const std::unique_ptr<wchar_t[]> inputBuffer(new wchar_t[bufferSize]);
1919
std::wstring input;
2020
if (wscanf_s(L"%s", inputBuffer.get(), static_cast<unsigned>(bufferSize)) == 1)
2121
{
@@ -55,7 +55,7 @@ HRESULT Helpers::PrintMessage(DWORD messageId, ...)
5555
{
5656
va_list argList;
5757
va_start(argList, messageId);
58-
auto hr = PrintMessageVa(messageId, argList);
58+
const auto hr = PrintMessageVa(messageId, argList);
5959
va_end(argList);
6060
return hr;
6161
}
@@ -71,13 +71,13 @@ namespace
7171
HRESULT FormatMessageHelperVa(DWORD messageId, va_list vaList, std::wstring* message)
7272
{
7373
PWSTR buffer = nullptr;
74-
auto written = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER,
75-
nullptr,
76-
messageId,
77-
0,
78-
(PWSTR)&buffer,
79-
10,
80-
&vaList);
74+
const auto written = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER,
75+
nullptr,
76+
messageId,
77+
0,
78+
(PWSTR)&buffer,
79+
10,
80+
&vaList);
8181
*message = buffer;
8282
if (buffer != nullptr)
8383
{
@@ -90,7 +90,7 @@ namespace
9090
HRESULT PrintMessageVa(DWORD messageId, va_list vaList)
9191
{
9292
std::wstring message;
93-
auto hr = FormatMessageHelperVa(messageId, vaList, &message);
93+
const auto hr = FormatMessageHelperVa(messageId, vaList, &message);
9494
if (SUCCEEDED(hr))
9595
{
9696
wprintf(L"%ls", message.c_str());

DistroLauncher/WslApiLoader.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BOOL WslApiLoader::WslIsDistributionRegistered() const
4646

4747
HRESULT WslApiLoader::WslRegisterDistribution() const
4848
{
49-
auto hr = _registerDistribution(_distributionName.c_str(), L"install.tar.gz");
49+
const auto hr = _registerDistribution(_distributionName.c_str(), L"install.tar.gz");
5050
if (FAILED(hr))
5151
{
5252
Helpers::PrintMessage(MSG_WSL_REGISTER_DISTRIBUTION_FAILED, hr);
@@ -57,7 +57,7 @@ HRESULT WslApiLoader::WslRegisterDistribution() const
5757

5858
HRESULT WslApiLoader::WslConfigureDistribution(ULONG defaultUID, WSL_DISTRIBUTION_FLAGS wslDistributionFlags) const
5959
{
60-
auto hr = _configureDistribution(_distributionName.c_str(), defaultUID, wslDistributionFlags);
60+
const auto hr = _configureDistribution(_distributionName.c_str(), defaultUID, wslDistributionFlags);
6161
if (FAILED(hr))
6262
{
6363
Helpers::PrintMessage(MSG_WSL_CONFIGURE_DISTRIBUTION_FAILED, hr);
@@ -68,7 +68,7 @@ HRESULT WslApiLoader::WslConfigureDistribution(ULONG defaultUID, WSL_DISTRIBUTIO
6868

6969
HRESULT WslApiLoader::WslLaunchInteractive(PCWSTR command, BOOL useCurrentWorkingDirectory, DWORD* exitCode) const
7070
{
71-
auto hr = _launchInteractive(_distributionName.c_str(), command, useCurrentWorkingDirectory, exitCode);
71+
const auto hr = _launchInteractive(_distributionName.c_str(), command, useCurrentWorkingDirectory, exitCode);
7272
if (FAILED(hr))
7373
{
7474
Helpers::PrintMessage(MSG_WSL_LAUNCH_INTERACTIVE_FAILED, command, hr);
@@ -80,7 +80,8 @@ HRESULT WslApiLoader::WslLaunchInteractive(PCWSTR command, BOOL useCurrentWorkin
8080
HRESULT WslApiLoader::WslLaunch(PCWSTR command, BOOL useCurrentWorkingDirectory, HANDLE stdIn, HANDLE stdOut,
8181
HANDLE stdErr, HANDLE* process) const
8282
{
83-
auto hr = _launch(_distributionName.c_str(), command, useCurrentWorkingDirectory, stdIn, stdOut, stdErr, process);
83+
const auto hr = _launch(_distributionName.c_str(), command, useCurrentWorkingDirectory, stdIn, stdOut, stdErr,
84+
process);
8485
if (FAILED(hr))
8586
{
8687
Helpers::PrintMessage(MSG_WSL_LAUNCH_FAILED, command, hr);

DistroLauncher/WslApiLoader.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
#define ERROR_LINUX_SUBSYSTEM_NOT_PRESENT 414L
1212
#endif // !ERROR_LINUX_SUBSYSTEM_NOT_PRESENT
1313

14-
typedef BOOL (STDAPICALLTYPE* WSL_IS_DISTRIBUTION_REGISTERED)(PCWSTR);
15-
typedef HRESULT (STDAPICALLTYPE* WSL_REGISTER_DISTRIBUTION)(PCWSTR, PCWSTR);
16-
typedef HRESULT (STDAPICALLTYPE* WSL_CONFIGURE_DISTRIBUTION)(PCWSTR, ULONG, WSL_DISTRIBUTION_FLAGS);
17-
typedef HRESULT (STDAPICALLTYPE* WSL_GET_DISTRIBUTION_CONFIGURATION)(PCWSTR, ULONG*, ULONG*, WSL_DISTRIBUTION_FLAGS*,
18-
PSTR**, ULONG*);
19-
typedef HRESULT (STDAPICALLTYPE* WSL_LAUNCH_INTERACTIVE)(PCWSTR, PCWSTR, BOOL, DWORD*);
20-
typedef HRESULT (STDAPICALLTYPE* WSL_LAUNCH)(PCWSTR, PCWSTR, BOOL, HANDLE, HANDLE, HANDLE, HANDLE*);
14+
using WSL_IS_DISTRIBUTION_REGISTERED = BOOL(STDAPICALLTYPE*)(PCWSTR);
15+
using WSL_REGISTER_DISTRIBUTION = HRESULT(STDAPICALLTYPE*)(PCWSTR, PCWSTR);
16+
using WSL_CONFIGURE_DISTRIBUTION = HRESULT(STDAPICALLTYPE*)(PCWSTR, ULONG, WSL_DISTRIBUTION_FLAGS);
17+
using WSL_GET_DISTRIBUTION_CONFIGURATION = HRESULT(STDAPICALLTYPE*)(PCWSTR, ULONG*, ULONG*, WSL_DISTRIBUTION_FLAGS*,
18+
PSTR**, ULONG*);
19+
using WSL_LAUNCH_INTERACTIVE = HRESULT(STDAPICALLTYPE*)(PCWSTR, PCWSTR, BOOL, DWORD*);
20+
using WSL_LAUNCH = HRESULT(STDAPICALLTYPE*)(PCWSTR, PCWSTR, BOOL, HANDLE, HANDLE, HANDLE, HANDLE*);
2121

2222
class WslApiLoader
2323
{
@@ -32,7 +32,7 @@ class WslApiLoader
3232
HRESULT WslRegisterDistribution() const;
3333

3434
HRESULT WslConfigureDistribution(ULONG defaultUID,
35-
WSL_DISTRIBUTION_FLAGS wslDistributionFlags) const;
35+
WSL_DISTRIBUTION_FLAGS wslDistributionFlags) const;
3636

3737
HRESULT WslLaunchInteractive(PCWSTR command,
3838
BOOL useCurrentWorkingDirectory,

DistroLauncher/messages.mc

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Enter new UNIX username: %0
6969
7070
MessageId=1010 SymbolicName=MSG_CREATE_USER_PROMPT
7171
Language=English
72-
Please create a default UNIX user account. The username does not need to match your Windows username.
72+
Please create a default Linux user account. The username does not need to match your Windows username.
7373
For more information visit: https://aka.ms/wslusers
7474
.
7575

DistroLauncher/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Windows.CppWinRT" version="2.0.210427.2" targetFramework="native" />
4+
</packages>

LauncherTests/LauncherTests.vcxproj

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props')" />
34
<ItemGroup Label="ProjectConfigurations">
45
<ProjectConfiguration Include="Debug|Win32">
56
<Configuration>Debug</Configuration>
@@ -56,6 +57,7 @@
5657
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
5758
<ImportGroup Label="ExtensionTargets">
5859
<Import Project="..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets" Condition="Exists('..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets')" />
60+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets')" />
5961
</ImportGroup>
6062
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
6163
<ClCompile>
@@ -124,5 +126,7 @@
124126
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
125127
</PropertyGroup>
126128
<Error Condition="!Exists('..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets'))" />
129+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.props'))" />
130+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210427.2\build\native\Microsoft.Windows.CppWinRT.targets'))" />
127131
</Target>
128132
</Project>

LauncherTests/packages.config

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
32
<packages>
4-
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.1.3" targetFramework="native" />
3+
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.1.3" targetFramework="native" />
4+
<package id="Microsoft.Windows.CppWinRT" version="2.0.210427.2" targetFramework="native" />
55
</packages>

Pengwin/Assets/pengwin.png

3.66 KB
Loading

0 commit comments

Comments
 (0)