-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
SystemConfigurationProvider.cpp
200 lines (170 loc) · 7.62 KB
/
SystemConfigurationProvider.cpp
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "SystemConfigurationProvider.hpp"
#include "icon.hpp"
#include "..\inc\ServiceLocator.hpp"
using namespace Microsoft::Console::Interactivity::Win32;
UINT SystemConfigurationProvider::GetCaretBlinkTime()
{
return ::GetCaretBlinkTime();
}
bool SystemConfigurationProvider::IsCaretBlinkingEnabled()
{
return GetSystemMetrics(SM_CARETBLINKINGENABLED) ? true : false;
}
int SystemConfigurationProvider::GetNumberOfMouseButtons()
{
return GetSystemMetrics(SM_CMOUSEBUTTONS);
}
ULONG SystemConfigurationProvider::GetCursorWidth()
{
ULONG width;
if (SystemParametersInfoW(SPI_GETCARETWIDTH, 0, &width, FALSE))
{
return width;
}
else
{
LOG_LAST_ERROR();
return s_DefaultCursorWidth;
}
}
ULONG SystemConfigurationProvider::GetNumberOfWheelScrollLines()
{
ULONG lines;
SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &lines, FALSE);
return lines;
}
ULONG SystemConfigurationProvider::GetNumberOfWheelScrollCharacters()
{
ULONG characters;
SystemParametersInfoW(SPI_GETWHEELSCROLLCHARS, 0, &characters, FALSE);
return characters;
}
void SystemConfigurationProvider::GetSettingsFromLink(
_Inout_ Settings* pLinkSettings,
_Inout_updates_bytes_(*pdwTitleLength) LPWSTR pwszTitle,
_Inout_ PDWORD pdwTitleLength,
_In_ PCWSTR pwszCurrDir,
_In_ PCWSTR pwszAppName)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
WCHAR wszLinkTarget[MAX_PATH] = { 0 };
WCHAR wszIconLocation[MAX_PATH] = { 0 };
int iIconIndex = 0;
pLinkSettings->SetCodePage(ServiceLocator::LocateGlobals().uiOEMCP);
// Did we get started from a link?
if (pLinkSettings->GetStartupFlags() & STARTF_TITLEISLINKNAME)
{
if (SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)))
{
size_t const cch = *pdwTitleLength / sizeof(wchar_t);
gci.SetLinkTitle(std::wstring(pwszTitle, cch));
wchar_t* const linkNameForCsi = new (std::nothrow) wchar_t[gci.GetLinkTitle().length() + 1]{ 0 };
if (linkNameForCsi)
{
gci.GetLinkTitle().copy(linkNameForCsi, gci.GetLinkTitle().length());
}
CONSOLE_STATE_INFO csi = pLinkSettings->CreateConsoleStateInfo();
csi.LinkTitle = linkNameForCsi;
WCHAR wszShortcutTitle[MAX_PATH];
BOOL fReadConsoleProperties;
WORD wShowWindow = pLinkSettings->GetShowWindow();
DWORD dwHotKey = pLinkSettings->GetHotKey();
int iShowWindow;
WORD wHotKey;
NTSTATUS Status = ShortcutSerialization::s_GetLinkValues(&csi,
&fReadConsoleProperties,
wszShortcutTitle,
ARRAYSIZE(wszShortcutTitle),
wszLinkTarget,
ARRAYSIZE(wszLinkTarget),
wszIconLocation,
ARRAYSIZE(wszIconLocation),
&iIconIndex,
&iShowWindow,
&wHotKey);
// Convert results back to appropriate types and set.
if (SUCCEEDED(IntToWord(iShowWindow, &wShowWindow)))
{
pLinkSettings->SetShowWindow(wShowWindow);
}
dwHotKey = wHotKey;
pLinkSettings->SetHotKey(dwHotKey);
if (wszLinkTarget[0] != L'\0')
{
// guarantee null termination to make OACR happy.
wszLinkTarget[ARRAYSIZE(wszLinkTarget) - 1] = L'\0';
}
// if we got a title, use it. even on overall link value load failure, the title will be correct if
// filled out.
if (wszShortcutTitle[0] != L'\0')
{
// guarantee null termination to make OACR happy.
wszShortcutTitle[ARRAYSIZE(wszShortcutTitle) - 1] = L'\0';
StringCbCopyW(pwszTitle, *pdwTitleLength, wszShortcutTitle);
// OACR complains about the use of a DWORD here, so roundtrip through a size_t
size_t cbTitleLength;
if (SUCCEEDED(StringCbLengthW(pwszTitle, *pdwTitleLength, &cbTitleLength)))
{
// don't care about return result -- the buffer is guaranteed null terminated to at least
// the length of Title
(void)SizeTToDWord(cbTitleLength, pdwTitleLength);
}
}
if (NT_SUCCESS(Status) && fReadConsoleProperties)
{
// copy settings
pLinkSettings->InitFromStateInfo(&csi);
// since we were launched via shortcut, make sure we don't let the invoker's STARTUPINFO pollute the
// shortcut's settings
pLinkSettings->UnsetStartupFlag(STARTF_USESIZE | STARTF_USECOUNTCHARS);
}
else
{
// if we didn't find any console properties, or otherwise failed to load link properties, pretend
// like we weren't launched from a shortcut -- this allows us to at least try to find registry
// settings based on title.
pLinkSettings->UnsetStartupFlag(STARTF_TITLEISLINKNAME);
}
CoUninitialize();
}
}
// Go get the icon
if (wszIconLocation[0] == L'\0')
{
if (PathFileExists(wszLinkTarget))
{
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), wszLinkTarget);
}
else
{
// search for the application along the path so that we can load its icons (if we didn't find one explicitly in
// the shortcut)
const DWORD dwLinkLen = SearchPathW(pwszCurrDir, pwszAppName, nullptr, ARRAYSIZE(wszIconLocation), wszIconLocation, nullptr);
// If we cannot find the application in the path, then try to fall back and see if the window title is a valid path and use that.
if (dwLinkLen <= 0 || dwLinkLen > sizeof(wszIconLocation))
{
if (PathFileExistsW(pwszTitle) && (wcslen(pwszTitle) < sizeof(wszIconLocation)))
{
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszTitle);
}
else
{
// If all else fails, just stick the app name into the path and try to resolve just the app name.
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszAppName);
}
}
}
}
if (wszIconLocation[0] != L'\0')
{
LOG_IF_FAILED(Icon::Instance().LoadIconsFromPath(wszIconLocation, iIconIndex));
}
if (!IsValidCodePage(pLinkSettings->GetCodePage()))
{
// make sure we don't leave this function with an invalid codepage
pLinkSettings->SetCodePage(ServiceLocator::LocateGlobals().uiOEMCP);
}
}