terminal/src/interactivity/win32/SystemConfigurationProvider.cpp
Malcolm Smith 601286ac69
Find icon from shortcut target if shortcut doesn't specify it (#6277)
Implements what I was suggesting in #6266 where if a shortcut doesn't
specify an icon, the shortcut target full path is used before searching
for a matching executable in the path.

## References

Found due to not getting the right icon in conhost from the Yori
installer.  It's fixed in the installer from
5af366b6a5
for all current users of conhost though, so this PR is just trying to
minimize surprises for the next guy.

## Detailed Description of the Pull Request / Additional comments

I know conhost and shortcut settings aren't really the team's focus
which is why I'm doing this.  I understand though if there's a better
way or there are factors that I hadn't considered.  Note that the path
searching code is used when programs are launched without using a
shortcut, and it will match if the working directory of the shortcut is
the directory containing the executable.

## Validation Steps Performed

Created a shortcut that didn't specify an icon to a binary that wasn't
in the path, and verified that the icon in the upper left of the console
window could resolve correctly when opening the shortcut.  I'm not aware
of a way to get into this path (of launching via a shortcut to a command
line process) without replacing the system conhost, which is what I did
to verify it.  In order to diagnose it, I used hardcoded DebugBreak()
since even ImageFileExecutionOptions didn't like running against conhost-
is there are better way to debug and test these cases without being so
invasive on the system?

Closes #6266
2020-06-01 17:19:05 +00:00

201 lines
7.6 KiB
C++

// 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);
}
}