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;
36 internal static IConsoleDriver driver;
37 static bool is_console;
38 static bool called_isatty;
40 static ConsoleDriver ()
43 driver = new NullConsoleDriver ();
44 } else if (Environment.IsRunningOnWindows) {
45 driver = new WindowsConsoleDriver ();
47 driver = new TermInfoDriver (Environment.GetEnvironmentVariable ("TERM"));
51 public static bool Echo {
52 get { return driver.Echo; }
53 set { driver.Echo = value; }
56 public static bool Initialized {
57 get { return driver.Initialized; }
60 public static ConsoleColor BackgroundColor {
61 get { return driver.BackgroundColor; }
63 if (value < ConsoleColor.Black || value > ConsoleColor.White)
64 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
66 driver.BackgroundColor = value;
70 public static int BufferHeight {
71 get { return driver.BufferHeight; }
72 set { driver.BufferHeight = value; }
75 public static int BufferWidth {
76 get { return driver.BufferWidth; }
77 set { driver.BufferWidth = value; }
80 public static bool CapsLock {
81 get { return driver.CapsLock; }
84 public static int CursorLeft {
85 get { return driver.CursorLeft; }
86 set { driver.CursorLeft = value; }
89 public static int CursorSize {
90 get { return driver.CursorSize; }
91 set { driver.CursorSize = value; }
94 public static int CursorTop {
95 get { return driver.CursorTop; }
96 set { driver.CursorTop = value; }
99 public static bool CursorVisible {
100 get { return driver.CursorVisible; }
101 set { driver.CursorVisible = value; }
104 public static bool KeyAvailable {
105 get { return driver.KeyAvailable; }
108 public static ConsoleColor ForegroundColor {
109 get { return driver.ForegroundColor; }
111 if (value < ConsoleColor.Black || value > ConsoleColor.White)
112 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
114 driver.ForegroundColor = value;
118 public static int LargestWindowHeight {
119 get { return driver.LargestWindowHeight; }
122 public static int LargestWindowWidth {
123 get { return driver.LargestWindowWidth; }
126 public static bool NumberLock {
127 get { return driver.NumberLock; }
130 public static string Title {
131 get { return driver.Title; }
132 set { driver.Title = value; }
135 public static bool TreatControlCAsInput {
136 get { return driver.TreatControlCAsInput; }
137 set { driver.TreatControlCAsInput = value; }
140 public static int WindowHeight {
141 get { return driver.WindowHeight; }
142 set { driver.WindowHeight = value; }
145 public static int WindowLeft {
146 get { return driver.WindowLeft; }
147 set { driver.WindowLeft = value; }
150 public static int WindowTop {
151 get { return driver.WindowTop; }
152 set { driver.WindowTop = value; }
155 public static int WindowWidth {
156 get { return driver.WindowWidth; }
157 set { driver.WindowWidth = value; }
160 public static void Beep (int frequency, int duration)
162 driver.Beep (frequency, duration);
165 public static void Clear ()
170 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
171 int targetLeft, int targetTop)
173 MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
174 targetLeft, targetTop, ' ', 0, 0);
177 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
178 int targetLeft, int targetTop, char sourceChar,
179 ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
181 driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
182 targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
185 public static void Init ()
190 public static int Read ()
192 return ReadKey (false).KeyChar;
195 public static string ReadLine ()
197 return driver.ReadLine ();
200 public static ConsoleKeyInfo ReadKey (bool intercept)
202 return driver.ReadKey (intercept);
205 public static void ResetColor ()
207 driver.ResetColor ();
210 public static void SetBufferSize (int width, int height)
212 driver.SetBufferSize (width, height);
215 public static void SetCursorPosition (int left, int top)
217 driver.SetCursorPosition (left, top);
220 public static void SetWindowPosition (int left, int top)
222 driver.SetWindowPosition (left, top);
225 public static void SetWindowSize (int width, int height)
227 driver.SetWindowSize (width, height);
230 public static bool IsConsole {
235 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
236 called_isatty = true;
241 [MethodImplAttribute(MethodImplOptions.InternalCall)]
242 static extern bool Isatty (IntPtr handle);
244 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245 internal static extern int InternalKeyAvailable (int ms_timeout);
247 [MethodImplAttribute(MethodImplOptions.InternalCall)]
248 internal static extern bool TtySetup (string teardown, out byte verase, out byte vsusp, out byte intr);
250 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251 internal static extern bool SetEcho (bool wantEcho);
253 [MethodImplAttribute(MethodImplOptions.InternalCall)]
254 internal static extern bool SetBreak (bool wantBreak);
256 [MethodImplAttribute(MethodImplOptions.InternalCall)]
257 internal static extern bool GetTtySize (IntPtr handle, out int width, out int height);
259 [MethodImplAttribute(MethodImplOptions.InternalCall)]
260 internal static extern void Suspend ();