Skip to content

Commit

Permalink
Console beep: perform the sys-call directly (#104966)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik authored Jul 17, 2024
1 parent bbcab23 commit 8602bb3
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/libraries/System.Console/src/System/ConsolePal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -677,28 +677,17 @@ public static unsafe string Title

public static void Beep()
{
const char BellCharacter = '\u0007'; // Windows doesn't use terminfo, so the codepoint is hardcoded.

if (!Console.IsOutputRedirected)
{
Console.Out.Write(BellCharacter);
return;
}

if (!Console.IsErrorRedirected)
{
Console.Error.Write(BellCharacter);
return;
ReadOnlySpan<byte> bell = "\u0007"u8; // Windows doesn't use terminfo, so the codepoint is hardcoded.
int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: Console.OutputEncoding.CodePage != UnicodeCodePage);
if (errorCode == Interop.Errors.ERROR_SUCCESS)
{
return;
}
}

BeepFallback();
}

private static void BeepFallback()
{
const int BeepFrequencyInHz = 800;
const int BeepDurationInMs = 200;
Interop.Kernel32.Beep(BeepFrequencyInHz, BeepDurationInMs);
Interop.Kernel32.Beep(frequency: 800, duration: 200);
}

public static void Beep(int frequency, int duration)
Expand Down Expand Up @@ -1226,7 +1215,7 @@ private static unsafe int ReadFileNative(IntPtr hFile, Span<byte> buffer, bool i
return errorCode;
}

private static unsafe int WriteFileNative(IntPtr hFile, ReadOnlySpan<byte> bytes, bool useFileAPIs)
internal static unsafe int WriteFileNative(IntPtr hFile, ReadOnlySpan<byte> bytes, bool useFileAPIs)
{
if (bytes.IsEmpty)
return Interop.Errors.ERROR_SUCCESS;
Expand Down

0 comments on commit 8602bb3

Please sign in to comment.