Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 9 additions & 53 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ internal sealed partial class ConsoleHost
internal const int ExitCodeCtrlBreak = 128 + 21; // SIGBREAK
internal const int ExitCodeInitFailure = 70; // Internal Software Error
internal const int ExitCodeBadCommandLineParameter = 64; // Command Line Usage Error
private const uint SPI_GETSCREENREADER = 0x0046;
#if UNIX
internal const string DECCKM_ON = "\x1b[?1h";
internal const string DECCKM_OFF = "\x1b[?1l";
#endif

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);

/// <summary>
/// Internal Entry point in msh console host implementation.
/// </summary>
Expand Down Expand Up @@ -1680,35 +1675,6 @@ private void CreateRunspace(RunspaceCreationEventArgs runspaceCreationArgs)
}
}

/// <summary>
/// Check if a screen reviewer utility is running.
/// When a screen reader is running, we don't auto-load the PSReadLine module at startup,
/// since PSReadLine is not accessibility-friendly enough as of today.
/// </summary>
private bool IsScreenReaderActive()
{
if (_screenReaderActive.HasValue)
{
return _screenReaderActive.Value;
}

_screenReaderActive = false;
if (Platform.IsWindowsDesktop)
{
// Note: this API can detect if a third-party screen reader is active, such as NVDA, but not the in-box Windows Narrator.
// Quoted from https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-systemparametersinfoa about the
// accessibility parameter 'SPI_GETSCREENREADER':
// "Narrator, the screen reader that is included with Windows, does not set the SPI_SETSCREENREADER or SPI_GETSCREENREADER flags."
bool enabled = false;
if (SystemParametersInfo(SPI_GETSCREENREADER, 0, ref enabled, 0))
Comment thread
andyleejordan marked this conversation as resolved.
{
_screenReaderActive = enabled;
}
}

return _screenReaderActive.Value;
}

private static bool LoadPSReadline()
{
// Don't load PSReadline if:
Expand Down Expand Up @@ -1759,7 +1725,6 @@ private void DoCreateRunspace(RunspaceCreationEventArgs args)
bool psReadlineFailed = false;

// Load PSReadline by default unless there is no use:
// - screen reader is active, such as NVDA, indicating non-visual access
// - we're running a command/file and just exiting
// - stdin is redirected by a parent process
// - we're not interactive
Expand All @@ -1770,26 +1735,18 @@ private void DoCreateRunspace(RunspaceCreationEventArgs args)
ReadOnlyCollection<ModuleSpecification> defaultImportModulesList = null;
if (!customConfigurationProvided && LoadPSReadline())
{
if (IsScreenReaderActive())
// Create and open Runspace with PSReadline.
defaultImportModulesList = DefaultInitialSessionState.Modules;
DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" });
consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState);
try
{
s_theConsoleHost.UI.WriteLine(ManagedEntranceStrings.PSReadLineDisabledWhenScreenReaderIsActive);
s_theConsoleHost.UI.WriteLine();
OpenConsoleRunspace(consoleRunspace, args.StaMode);
}
else
catch (Exception)
{
// Create and open Runspace with PSReadline.
defaultImportModulesList = DefaultInitialSessionState.Modules;
DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" });
consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState);
try
{
OpenConsoleRunspace(consoleRunspace, args.StaMode);
}
catch (Exception)
{
consoleRunspace = null;
psReadlineFailed = true;
}
consoleRunspace = null;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why don't we dispose the runspace?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a question I don't have an answer to.

psReadlineFailed = true;
}
}

Expand Down Expand Up @@ -3102,7 +3059,6 @@ private sealed class ConsoleHostStartupException : Exception
private bool _setShouldExitCalled;
private bool _isRunningPromptLoop;
private bool _wasInitialCommandEncoded;
private bool? _screenReaderActive;

// hostGlobalLock is used to sync public method calls (in case multiple threads call into the host) and access to
// state that persists across method calls, like progress data. It's internal because the ui object also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@
<data name="ShellBannerRLMode" xml:space="preserve">
<value>[Restricted Language Mode]</value>
</data>
<data name="PSReadLineDisabledWhenScreenReaderIsActive" xml:space="preserve">
<value>Warning: PowerShell detected that you might be using a screen reader and has disabled PSReadLine for compatibility purposes. If you want to re-enable it, run 'Import-Module PSReadLine'.</value>
</data>
<data name="PreviewUpdateNotificationMessage" xml:space="preserve">
<value> {1} A new PowerShell preview release is available: v{0} {2}
{1} Upgrade now, or check out the release page at:{3}{2}
Expand Down Expand Up @@ -162,9 +159,9 @@
[-CustomPipeName &lt;string&gt;] [-EncodedCommand &lt;Base64EncodedCommand&gt;]
[-ExecutionPolicy &lt;ExecutionPolicy&gt;] [-InputFormat {Text | XML}]
[-Interactive] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
[-NoProfileLoadTime] [-OutputFormat {Text | XML}]
[-SettingsFile &lt;filePath&gt;] [-SSHServerMode] [-STA]
[-Version] [-WindowStyle &lt;style&gt;]
[-NoProfileLoadTime] [-OutputFormat {Text | XML}]
[-SettingsFile &lt;filePath&gt;] [-SSHServerMode] [-STA]
[-Version] [-WindowStyle &lt;style&gt;]
[-WorkingDirectory &lt;directoryPath&gt;]

pwsh[.exe] -h | -Help | -? | /?
Expand Down
58 changes: 0 additions & 58 deletions test/powershell/Host/ScreenReader.Tests.ps1

This file was deleted.

Loading