forked from microsoft/WSL-DistroLauncher
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathDistroLauncher.cpp
More file actions
320 lines (270 loc) · 9.11 KB
/
DistroLauncher.cpp
File metadata and controls
320 lines (270 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//
#include "stdafx.h"
// Commandline arguments:
constexpr auto ARG_CONFIG = L"config";
constexpr auto ARG_CONFIG_DEFAULT_USER = L"--default-user";
constexpr auto ARG_INSTALL = L"install";
constexpr auto ARG_INSTALL_ROOT = L"--root";
constexpr auto ARG_RUN = L"run";
constexpr auto ARG_RUN_C = L"-c";
constexpr auto ARG_DISTRO = L"--distribution";
constexpr auto ARG_DISTRO_D = L"-d";
constexpr auto ARG_UNREGISTER = L"unregister";
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.Storage.h>
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::System;
using namespace Windows::Storage;
// Helper class for calling WSL Functions:
// https://msdn.microsoft.com/en-us/library/windows/desktop/mt826874(v=vs.85).aspx
// ReSharper disable once CppInconsistentNaming
WslApiLoader g_wslApi(DistributionInfo::NAME, DistributionInfo::NAME_OLD);
static HRESULT InstallDistribution(bool createUser);
static HRESULT SetDefaultUser(std::wstring_view userName);
HRESULT InstallDistribution(const bool createUser)
{
// Register the distribution.
Helpers::PrintMessage(MSG_STATUS_INSTALLING);
auto hr = g_wslApi.WslRegisterDistribution();
if (FAILED(hr))
{
return hr;
}
// Delete /etc/resolv.conf to allow WSL to generate a version based on Windows networking information.
DWORD exitCode;
hr = g_wslApi.WslLaunchInteractive(L"/bin/rm /etc/resolv.conf", true, &exitCode);
if (FAILED(hr))
{
return hr;
}
// Create a user account.
if (createUser)
{
Helpers::PrintMessage(MSG_CREATE_USER_PROMPT);
std::wstring userName;
do
{
userName = Helpers::GetUserInput(MSG_ENTER_USERNAME, 32);
}
while (!DistributionInfo::CreateUser(userName));
// Set this user account as the default.
hr = SetDefaultUser(userName);
if (FAILED(hr))
{
return hr;
}
}
return hr;
}
HRESULT SetDefaultUser(std::wstring_view userName)
{
// Query the UID of the given user name and configure the distribution
// to use this UID as the default.
const ULONG uid = DistributionInfo::QueryUid(userName);
if (uid == UID_INVALID)
{
return E_INVALIDARG;
}
HRESULT hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
if (FAILED(hr))
{
return hr;
}
return hr;
}
int RetrieveCurrentTheme()
{
DWORD value = 0;
DWORD size = sizeof value;
// ReSharper disable once CppTooWideScope
// ReSharper disable once CppTooWideScopeInitStatement
const auto status = RegGetValueW(HKEY_CURRENT_USER,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
L"AppsUseLightTheme",
RRF_RT_DWORD,
nullptr,
&value,
&size
);
if (status == ERROR_SUCCESS)
{
return static_cast<int>(value);
}
return -1;
}
fire_and_forget SyncIcon(const hstring& iconName)
{
const hstring nameSuffix = L""; //value == 0 ? L"" : L"";
const hstring extension = L".png";
const hstring composedPath = iconName + nameSuffix + extension;
const auto path = Uri(L"ms-appx:///Assets/" + composedPath);
try
{
const auto iconFile = StorageFile::GetFileFromApplicationUriAsync(path).get();
co_await iconFile.CopyAsync(ApplicationData::Current().LocalFolder(), iconName + extension,
NameCollisionOption::FailIfExists);
}
catch (...)
{
}
}
int RetrieveWindowsVersion();
// ReSharper disable once IdentifierTypo
fire_and_forget ShowPengwinUi()
{
// ReSharper disable once CppTooWideScope
// ReSharper disable once CppTooWideScopeInitStatement
const auto& file =
co_await ApplicationData::Current().LocalFolder().TryGetItemAsync(L"MicrosoftStoreEngagementSDKId.txt");
if (!file)
{
// ReSharper disable once StringLiteralTypo
const hstring str = L"pengwinui://";
const auto uri = Uri(str);
co_await Launcher::LaunchUriAsync(uri);
}
}
static bool is_current_dir_not_system32()
{
wchar_t system_32dir[MAX_PATH];
GetSystemDirectoryW(system_32dir, MAX_PATH);
wchar_t current_dir[MAX_PATH];
GetCurrentDirectoryW(MAX_PATH, current_dir);
return _wcsicmp(system_32dir, current_dir) != 0;
}
// ReSharper disable once IdentifierTypo
int wmain(const int argc, const wchar_t* argv[])
{
// Update the title bar of the console window.
SetConsoleTitleW(DistributionInfo::WINDOW_TITLE.c_str());
// Initialize a vector of arguments.
std::vector<std::wstring_view> arguments;
for (auto index = 1; index < argc; index += 1)
{
arguments.push_back(argv[index]);
}
// Ensure that the Windows Subsystem for Linux optional component is installed.
DWORD exitCode = 1;
if (!g_wslApi.WslIsOptionalComponentInstalled())
{
Helpers::PrintMessage(MSG_MISSING_OPTIONAL_COMPONENT);
if (arguments.empty())
{
Helpers::PromptForInput();
}
return static_cast<int>(exitCode);
}
// Install the distribution if it is not already.
const auto installOnly = arguments.size() > 0 && arguments[0] == ARG_INSTALL;
auto hr = S_OK;
if (!g_wslApi.WslIsDistributionRegistered())
{
// If the "--root" option is specified, do not create a user account.
const auto useRoot = installOnly && arguments.size() > 1 && arguments[1] == ARG_INSTALL_ROOT;
hr = InstallDistribution(!useRoot);
if (FAILED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
{
Helpers::PrintMessage(MSG_INSTALL_ALREADY_EXISTS);
}
}
else
{
Helpers::PrintMessage(MSG_INSTALL_SUCCESS);
}
exitCode = SUCCEEDED(hr) ? 0 : 1;
}
// Parse the command line arguments.
if (SUCCEEDED(hr) && !installOnly)
{
// ReSharper disable once StringLiteralTypo
SyncIcon(L"pengwin");
SyncIcon(L"background1");
SyncIcon(L"background2");
if (arguments.size() >= 2 && (arguments[0] == ARG_DISTRO ||
arguments[0] == ARG_DISTRO_D))
{
g_wslApi.SetDistributionName(arguments[1]);
arguments.erase(arguments.begin(), arguments.begin() + 2);
}
if (arguments.empty())
{
/* If the current working dir is not System32 then it was called from Open with Terminal
option or from command line. In this case is better to start the distro in the current directory */
const bool useCurrentWorkingDirectory = is_current_dir_not_system32();
hr = g_wslApi.WslLaunchInteractive(L"", useCurrentWorkingDirectory, &exitCode);
// Check exitCode to see if wsl.exe returned that it could not start the Linux process
// then prompt users for input so they can view the error message.
if (SUCCEEDED(hr) && exitCode == UINT_MAX)
{
Helpers::PromptForInput();
}
}
else if (arguments[0] == ARG_RUN ||
arguments[0] == ARG_RUN_C)
{
std::wstring command;
for (size_t index = 1; index < arguments.size(); index += 1)
{
command += L" ";
command += arguments[index];
}
hr = g_wslApi.WslLaunchInteractive(command.c_str(), true, &exitCode);
}
else if (arguments[0] == ARG_CONFIG)
{
hr = E_INVALIDARG;
if (arguments.size() == 3)
{
if (arguments[1] == ARG_CONFIG_DEFAULT_USER)
{
hr = SetDefaultUser(arguments[2]);
}
}
if (SUCCEEDED(hr))
{
exitCode = 0;
}
}
else if (arguments[0] == ARG_UNREGISTER)
{
hr = g_wslApi.WslUnregisterDistribution();
if (SUCCEEDED(hr))
{
exitCode = 0;
}
}
else
{
Helpers::PrintMessage(MSG_USAGE, DistributionInfo::WINDOW_TITLE.c_str());
return static_cast<int>(exitCode);
}
}
// If an error was encountered, print an error message.
if (FAILED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_LINUX_SUBSYSTEM_NOT_PRESENT))
{
Helpers::PrintMessage(MSG_MISSING_OPTIONAL_COMPONENT);
}
else if (hr == HCS_E_HYPERV_NOT_INSTALLED)
{
Helpers::PrintMessage(MSG_ENABLE_VIRTUALIZATION);
}
else
{
Helpers::PrintErrorMessage(hr);
}
if (arguments.empty())
{
Helpers::PromptForInput();
}
}
return SUCCEEDED(hr) ? exitCode : 1;
}