Skip to content

Commit e869f84

Browse files
BDisptig
andauthored
Fixes #4241. v2net driver isn't suspending on Unix platforms (#4242)
Co-authored-by: Tig <[email protected]>
1 parent ad8ebc9 commit e869f84

File tree

3 files changed

+113
-82
lines changed

3 files changed

+113
-82
lines changed

Terminal.Gui/Drivers/CursesDriver/CursesDriver.cs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,84 +1082,3 @@ public static bool Is_WSL_Platform ()
10821082
/// <inheritdoc/>
10831083
public override void WriteRaw (string ansi) { _mainLoopDriver?.WriteRaw (ansi); }
10841084
}
1085-
1086-
// TODO: One type per file - move to another file
1087-
internal static class Platform
1088-
{
1089-
private static int _suspendSignal;
1090-
1091-
/// <summary>Suspends the process by sending SIGTSTP to itself</summary>
1092-
/// <returns>True if the suspension was successful.</returns>
1093-
public static bool Suspend ()
1094-
{
1095-
int signal = GetSuspendSignal ();
1096-
1097-
if (signal == -1)
1098-
{
1099-
return false;
1100-
}
1101-
1102-
killpg (0, signal);
1103-
1104-
return true;
1105-
}
1106-
1107-
private static int GetSuspendSignal ()
1108-
{
1109-
if (_suspendSignal != 0)
1110-
{
1111-
return _suspendSignal;
1112-
}
1113-
1114-
nint buf = Marshal.AllocHGlobal (8192);
1115-
1116-
if (uname (buf) != 0)
1117-
{
1118-
Marshal.FreeHGlobal (buf);
1119-
_suspendSignal = -1;
1120-
1121-
return _suspendSignal;
1122-
}
1123-
1124-
try
1125-
{
1126-
switch (Marshal.PtrToStringAnsi (buf))
1127-
{
1128-
case "Darwin":
1129-
case "DragonFly":
1130-
case "FreeBSD":
1131-
case "NetBSD":
1132-
case "OpenBSD":
1133-
_suspendSignal = 18;
1134-
1135-
break;
1136-
case "Linux":
1137-
// TODO: should fetch the machine name and
1138-
// if it is MIPS return 24
1139-
_suspendSignal = 20;
1140-
1141-
break;
1142-
case "Solaris":
1143-
_suspendSignal = 24;
1144-
1145-
break;
1146-
default:
1147-
_suspendSignal = -1;
1148-
1149-
break;
1150-
}
1151-
1152-
return _suspendSignal;
1153-
}
1154-
finally
1155-
{
1156-
Marshal.FreeHGlobal (buf);
1157-
}
1158-
}
1159-
1160-
[DllImport ("libc")]
1161-
private static extern int killpg (int pgrp, int pid);
1162-
1163-
[DllImport ("libc")]
1164-
private static extern int uname (nint buf);
1165-
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Terminal.Gui.Drivers;
4+
5+
internal static class Platform
6+
{
7+
private static int _suspendSignal;
8+
9+
/// <summary>Suspends the process by sending SIGTSTP to itself</summary>
10+
/// <returns>True if the suspension was successful.</returns>
11+
public static bool Suspend ()
12+
{
13+
int signal = GetSuspendSignal ();
14+
15+
if (signal == -1)
16+
{
17+
return false;
18+
}
19+
20+
killpg (0, signal);
21+
22+
return true;
23+
}
24+
25+
private static int GetSuspendSignal ()
26+
{
27+
if (_suspendSignal != 0)
28+
{
29+
return _suspendSignal;
30+
}
31+
32+
nint buf = Marshal.AllocHGlobal (8192);
33+
34+
if (uname (buf) != 0)
35+
{
36+
Marshal.FreeHGlobal (buf);
37+
_suspendSignal = -1;
38+
39+
return _suspendSignal;
40+
}
41+
42+
try
43+
{
44+
switch (Marshal.PtrToStringAnsi (buf))
45+
{
46+
case "Darwin":
47+
case "DragonFly":
48+
case "FreeBSD":
49+
case "NetBSD":
50+
case "OpenBSD":
51+
_suspendSignal = 18;
52+
53+
break;
54+
case "Linux":
55+
// TODO: should fetch the machine name and
56+
// if it is MIPS return 24
57+
_suspendSignal = 20;
58+
59+
break;
60+
case "Solaris":
61+
_suspendSignal = 24;
62+
63+
break;
64+
default:
65+
_suspendSignal = -1;
66+
67+
break;
68+
}
69+
70+
return _suspendSignal;
71+
}
72+
finally
73+
{
74+
Marshal.FreeHGlobal (buf);
75+
}
76+
}
77+
78+
[DllImport ("libc")]
79+
private static extern int killpg (int pgrp, int pid);
80+
81+
[DllImport ("libc")]
82+
private static extern int uname (nint buf);
83+
}

Terminal.Gui/Drivers/V2/ConsoleDriverFacade.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,36 @@ public bool GetCursorVisibility (out CursorVisibility current)
326326
}
327327

328328
/// <inheritdoc/>
329-
public void Suspend () { }
329+
public void Suspend ()
330+
{
331+
if (Environment.OSVersion.Platform != PlatformID.Unix)
332+
{
333+
return;
334+
}
335+
336+
Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
337+
338+
if (!ConsoleDriver.RunningUnitTests)
339+
{
340+
Console.ResetColor ();
341+
Console.Clear ();
342+
343+
//Disable alternative screen buffer.
344+
Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
345+
346+
//Set cursor key to cursor.
347+
Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
348+
349+
Platform.Suspend ();
350+
351+
//Enable alternative screen buffer.
352+
Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
353+
354+
Application.LayoutAndDraw ();
355+
}
356+
357+
Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
358+
}
330359

331360
/// <summary>
332361
/// Sets the position of the terminal cursor to <see cref="ConsoleDriver.Col"/> and

0 commit comments

Comments
 (0)