Skip to content

Commit

Permalink
feat: disable console quick edit mode for Cli (BililiveRecorder#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
kira1928 authored Dec 3, 2023
1 parent 8c9c947 commit fe9bd29
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions BililiveRecorder.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -38,6 +39,11 @@ internal class Program
{
private static int Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BREC_SKIP_DISABLE_QUICK_EDIT")))
{
ConsoleModeHelper.SetQuickEditMode(false);
}
RootCommand root;

using (var entrypointLogger = BuildLogger(LogEventLevel.Fatal, LogEventLevel.Verbose))
Expand Down Expand Up @@ -564,5 +570,72 @@ public enum PortableDanmakuMode
All = Danmaku | SuperChat | Guard | Gift | RawData
}
}

private static class ConsoleModeHelper
{
internal static bool SetQuickEditMode(bool enable = true)
{
return SetMode(ConsoleModes.ENABLE_QUICK_EDIT_MODE, enable);
}

private static bool SetMode(ConsoleModes mode, bool enable = true)
{
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
if (consoleHandle == INVALID_HANDLE_VALUE)
{
return false;
}

uint consoleMode;
if (!GetConsoleMode(consoleHandle, out consoleMode))
{
return false;
}

if (enable)
{
consoleMode |= (uint)mode;
}
else
{
consoleMode &= ~(uint)mode;
}

return SetConsoleMode(consoleHandle, consoleMode);
}

const int STD_INPUT_HANDLE = -10;
static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

[Flags]
private enum ConsoleModes : uint
{
ENABLE_PROCESSED_INPUT = 0x0001,
ENABLE_LINE_INPUT = 0x0002,
ENABLE_ECHO_INPUT = 0x0004,
ENABLE_WINDOW_INPUT = 0x0008,
ENABLE_MOUSE_INPUT = 0x0010,
ENABLE_INSERT_MODE = 0x0020,
ENABLE_QUICK_EDIT_MODE = 0x0040,
ENABLE_EXTENDED_FLAGS = 0x0080,
ENABLE_AUTO_POSITION = 0x0100,
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,

ENABLE_PROCESSED_OUTPUT = 0x0001,
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
ENABLE_LVB_GRID_WORLDWIDE = 0x0010
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
}
}

0 comments on commit fe9bd29

Please sign in to comment.