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 Initialized {
52 get { return driver.Initialized; }
55 public static ConsoleColor BackgroundColor {
56 get { return driver.BackgroundColor; }
58 if (value < ConsoleColor.Black || value > ConsoleColor.White)
59 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
61 driver.BackgroundColor = value;
65 public static int BufferHeight {
66 get { return driver.BufferHeight; }
67 set { driver.BufferHeight = value; }
70 public static int BufferWidth {
71 get { return driver.BufferWidth; }
72 set { driver.BufferWidth = value; }
75 public static bool CapsLock {
76 get { return driver.CapsLock; }
79 public static int CursorLeft {
80 get { return driver.CursorLeft; }
81 set { driver.CursorLeft = value; }
84 public static int CursorSize {
85 get { return driver.CursorSize; }
86 set { driver.CursorSize = value; }
89 public static int CursorTop {
90 get { return driver.CursorTop; }
91 set { driver.CursorTop = value; }
94 public static bool CursorVisible {
95 get { return driver.CursorVisible; }
96 set { driver.CursorVisible = value; }
99 public static bool KeyAvailable {
100 get { return driver.KeyAvailable; }
103 public static ConsoleColor ForegroundColor {
104 get { return driver.ForegroundColor; }
106 if (value < ConsoleColor.Black || value > ConsoleColor.White)
107 throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
109 driver.ForegroundColor = value;
113 public static int LargestWindowHeight {
114 get { return driver.LargestWindowHeight; }
117 public static int LargestWindowWidth {
118 get { return driver.LargestWindowWidth; }
121 public static bool NumberLock {
122 get { return driver.NumberLock; }
125 public static string Title {
126 get { return driver.Title; }
127 set { driver.Title = value; }
130 public static bool TreatControlCAsInput {
131 get { return driver.TreatControlCAsInput; }
132 set { driver.TreatControlCAsInput = value; }
135 public static int WindowHeight {
136 get { return driver.WindowHeight; }
137 set { driver.WindowHeight = value; }
140 public static int WindowLeft {
141 get { return driver.WindowLeft; }
142 set { driver.WindowLeft = value; }
145 public static int WindowTop {
146 get { return driver.WindowTop; }
147 set { driver.WindowTop = value; }
150 public static int WindowWidth {
151 get { return driver.WindowWidth; }
152 set { driver.WindowWidth = value; }
155 public static void Beep (int frequency, int duration)
157 driver.Beep (frequency, duration);
160 public static void Clear ()
165 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
166 int targetLeft, int targetTop)
168 MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
169 targetLeft, targetTop, ' ', 0, 0);
172 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
173 int targetLeft, int targetTop, char sourceChar,
174 ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
176 driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
177 targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
180 public static void Init ()
185 public static int Read ()
187 return ReadKey (false).KeyChar;
190 public static string ReadLine ()
192 return driver.ReadLine ();
195 public static ConsoleKeyInfo ReadKey (bool intercept)
197 return driver.ReadKey (intercept);
200 public static void ResetColor ()
202 driver.ResetColor ();
205 public static void SetBufferSize (int width, int height)
207 driver.SetBufferSize (width, height);
210 public static void SetCursorPosition (int left, int top)
212 driver.SetCursorPosition (left, top);
215 public static void SetWindowPosition (int left, int top)
217 driver.SetWindowPosition (left, top);
220 public static void SetWindowSize (int width, int height)
222 driver.SetWindowSize (width, height);
225 public static bool IsConsole {
230 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
231 called_isatty = true;
236 [MethodImplAttribute(MethodImplOptions.InternalCall)]
237 static extern bool Isatty (IntPtr handle);
239 [MethodImplAttribute(MethodImplOptions.InternalCall)]
240 internal static extern int InternalKeyAvailable (int ms_timeout);
242 [MethodImplAttribute(MethodImplOptions.InternalCall)]
243 internal static extern bool TtySetup (string teardown, out byte verase, out byte vsusp, out byte intr);
245 [MethodImplAttribute(MethodImplOptions.InternalCall)]
246 internal static extern bool SetEcho (bool wantEcho);
248 [MethodImplAttribute(MethodImplOptions.InternalCall)]
249 internal static extern bool SetBreak (bool wantBreak);
251 [MethodImplAttribute(MethodImplOptions.InternalCall)]
252 internal static extern bool GetTtySize (IntPtr handle, out int width, out int height);
254 [MethodImplAttribute(MethodImplOptions.InternalCall)]
255 internal static extern void Suspend ();