2 // System.ConsoleDriver
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.CompilerServices;
35 static class ConsoleDriver {
36 internal static IConsoleDriver driver;
37 static bool is_console;
38 static bool called_isatty;
40 static ConsoleDriver ()
42 // Put the actual new statements into separate methods to avoid initalizing
43 // three classes when only one is needed.
45 driver = CreateNullConsoleDriver ();
46 } else if (Environment.IsRunningOnWindows) {
47 driver = CreateWindowsConsoleDriver ();
49 string term = Environment.GetEnvironmentVariable ("TERM");
51 // Perhaps we should let the Terminfo driver return a
52 // success/failure flag based on the terminal properties
55 driver = CreateNullConsoleDriver ();
57 driver = CreateTermInfoDriver (term);
61 [MethodImplAttribute (MethodImplOptions.NoInlining)]
62 static IConsoleDriver CreateNullConsoleDriver () {
63 return new NullConsoleDriver ();
66 [MethodImplAttribute (MethodImplOptions.NoInlining)]
67 static IConsoleDriver CreateWindowsConsoleDriver () {
68 return new WindowsConsoleDriver ();
71 [MethodImplAttribute (MethodImplOptions.NoInlining)]
72 static IConsoleDriver CreateTermInfoDriver (string term) {
73 return new TermInfoDriver (term);
76 public static bool Initialized {
77 get { return driver.Initialized; }
80 public static ConsoleColor BackgroundColor {
81 get { return driver.BackgroundColor; }
83 if (value < ConsoleColor.Black || value > ConsoleColor.White)
84 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
86 driver.BackgroundColor = value;
90 public static int BufferHeight {
91 get { return driver.BufferHeight; }
92 set { driver.BufferHeight = value; }
95 public static int BufferWidth {
96 get { return driver.BufferWidth; }
97 set { driver.BufferWidth = value; }
100 public static bool CapsLock {
101 get { return driver.CapsLock; }
104 public static int CursorLeft {
105 get { return driver.CursorLeft; }
106 set { driver.CursorLeft = value; }
109 public static int CursorSize {
110 get { return driver.CursorSize; }
111 set { driver.CursorSize = value; }
114 public static int CursorTop {
115 get { return driver.CursorTop; }
116 set { driver.CursorTop = value; }
119 public static bool CursorVisible {
120 get { return driver.CursorVisible; }
121 set { driver.CursorVisible = value; }
124 public static bool KeyAvailable {
125 get { return driver.KeyAvailable; }
128 public static ConsoleColor ForegroundColor {
129 get { return driver.ForegroundColor; }
131 if (value < ConsoleColor.Black || value > ConsoleColor.White)
132 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
134 driver.ForegroundColor = value;
138 public static int LargestWindowHeight {
139 get { return driver.LargestWindowHeight; }
142 public static int LargestWindowWidth {
143 get { return driver.LargestWindowWidth; }
146 public static bool NumberLock {
147 get { return driver.NumberLock; }
150 public static string Title {
151 get { return driver.Title; }
152 set { driver.Title = value; }
155 public static bool TreatControlCAsInput {
156 get { return driver.TreatControlCAsInput; }
157 set { driver.TreatControlCAsInput = value; }
160 public static int WindowHeight {
161 get { return driver.WindowHeight; }
162 set { driver.WindowHeight = value; }
165 public static int WindowLeft {
166 get { return driver.WindowLeft; }
167 set { driver.WindowLeft = value; }
170 public static int WindowTop {
171 get { return driver.WindowTop; }
172 set { driver.WindowTop = value; }
175 public static int WindowWidth {
176 get { return driver.WindowWidth; }
177 set { driver.WindowWidth = value; }
180 public static bool IsErrorRedirected {
182 return !Isatty (MonoIO.ConsoleError);
186 public static bool IsOutputRedirected {
188 return !Isatty (MonoIO.ConsoleOutput);
192 public static bool IsInputRedirected {
194 return !Isatty (MonoIO.ConsoleInput);
198 public static void Beep (int frequency, int duration)
200 driver.Beep (frequency, duration);
203 public static void Clear ()
208 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
209 int targetLeft, int targetTop)
211 MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
212 targetLeft, targetTop, ' ', 0, 0);
215 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
216 int targetLeft, int targetTop, char sourceChar,
217 ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
219 driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
220 targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
223 public static void Init ()
228 public static int Read ()
230 return ReadKey (false).KeyChar;
233 public static string ReadLine ()
235 return driver.ReadLine ();
238 public static ConsoleKeyInfo ReadKey (bool intercept)
240 return driver.ReadKey (intercept);
243 public static void ResetColor ()
245 driver.ResetColor ();
248 public static void SetBufferSize (int width, int height)
250 driver.SetBufferSize (width, height);
253 public static void SetCursorPosition (int left, int top)
255 driver.SetCursorPosition (left, top);
258 public static void SetWindowPosition (int left, int top)
260 driver.SetWindowPosition (left, top);
263 public static void SetWindowSize (int width, int height)
265 driver.SetWindowSize (width, height);
268 public static bool IsConsole {
273 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
274 called_isatty = true;
279 [MethodImplAttribute(MethodImplOptions.InternalCall)]
280 static extern bool Isatty (IntPtr handle);
282 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283 internal static extern int InternalKeyAvailable (int ms_timeout);
285 [MethodImplAttribute(MethodImplOptions.InternalCall)]
286 unsafe internal static extern bool TtySetup (string keypadXmit, string teardown, out byte [] control_characters, out int *address);
288 [MethodImplAttribute(MethodImplOptions.InternalCall)]
289 internal static extern bool SetEcho (bool wantEcho);
291 [MethodImplAttribute(MethodImplOptions.InternalCall)]
292 internal static extern bool SetBreak (bool wantBreak);