New test.
[mono.git] / mcs / class / corlib / System / ConsoleDriver.cs
1 //
2 // System.ConsoleDriver
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 #if NET_2_0
31 using System.IO;
32 using System.Runtime.CompilerServices;
33
34 namespace System {
35         class ConsoleDriver {
36                 internal static IConsoleDriver driver;
37                 static bool is_console;
38                 static bool called_isatty;
39
40                 static ConsoleDriver ()
41                 {
42                         if (!IsConsole) {
43                                 driver = new NullConsoleDriver ();
44                         } else if (Environment.IsRunningOnWindows) {
45                                 driver = new WindowsConsoleDriver ();
46                         } else {
47                                 driver = new TermInfoDriver (Environment.GetEnvironmentVariable ("TERM"));
48                         }
49                 }
50
51                 public static bool Echo {
52                         get { return driver.Echo; }
53                         set { driver.Echo = value; }
54                 }
55
56                 public static bool Initialized {
57                         get { return driver.Initialized; }
58                 }
59
60                 public static ConsoleColor BackgroundColor {
61                         get { return driver.BackgroundColor; }
62                         set {
63                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
64                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
65
66                                 driver.BackgroundColor = value;
67                         }
68                 }
69
70                 public static int BufferHeight {
71                         get { return driver.BufferHeight; }
72                         set { driver.BufferHeight = value; }
73                 }
74
75                 public static int BufferWidth {
76                         get { return driver.BufferWidth; }
77                         set { driver.BufferWidth = value; }
78                 }
79
80                 public static bool CapsLock {
81                         get { return driver.CapsLock; }
82                 }
83
84                 public static int CursorLeft {
85                         get { return driver.CursorLeft; }
86                         set { driver.CursorLeft = value; }
87                 }
88
89                 public static int CursorSize {
90                         get { return driver.CursorSize; }
91                         set { driver.CursorSize = value; }
92                 }
93
94                 public static int CursorTop {
95                         get { return driver.CursorTop; }
96                         set { driver.CursorTop = value; }
97                 } 
98                 
99                 public static bool CursorVisible {
100                         get { return driver.CursorVisible; }
101                         set { driver.CursorVisible = value; }
102                 }
103
104                 public static bool KeyAvailable {
105                         get { return driver.KeyAvailable; }
106                 }
107
108                 public static ConsoleColor ForegroundColor {
109                         get { return driver.ForegroundColor; }
110                         set {
111                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
112                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
113
114                                 driver.ForegroundColor = value;
115                         }
116                 }
117
118                 public static int LargestWindowHeight {
119                         get { return driver.LargestWindowHeight; }
120                 }
121
122                 public static int LargestWindowWidth {
123                         get { return driver.LargestWindowWidth; }
124                 }
125
126                 public static bool NumberLock {
127                         get { return driver.NumberLock; }
128                 }
129
130                 public static string Title {
131                         get { return driver.Title; }
132                         set { driver.Title = value; }
133                 }
134
135                 public static bool TreatControlCAsInput {
136                         get { return driver.TreatControlCAsInput; }
137                         set { driver.TreatControlCAsInput = value; }
138                 } 
139
140                 public static int WindowHeight {
141                         get { return driver.WindowHeight; }
142                         set { driver.WindowHeight = value; }
143                 }
144
145                 public static int WindowLeft {
146                         get { return driver.WindowLeft; }
147                         set { driver.WindowLeft = value; }
148                 }
149
150                 public static int WindowTop {
151                         get { return driver.WindowTop; }
152                         set { driver.WindowTop = value; }
153                 }
154
155                 public static int WindowWidth {
156                         get { return driver.WindowWidth; }
157                         set { driver.WindowWidth = value; }
158                 }
159
160                 public static void Beep (int frequency, int duration)
161                 {
162                         driver.Beep (frequency, duration);
163                 }
164                 
165                 public static void Clear ()
166                 {
167                         driver.Clear ();
168                 }
169
170                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
171                                         int targetLeft, int targetTop)
172                 {
173                         MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
174                                         targetLeft, targetTop, ' ', 0, 0);
175                 }
176
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)
180                 {
181                         driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
182                                         targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
183                 }
184
185                 public static void Init ()
186                 {
187                         driver.Init ();
188                 }
189
190                 public static int Read ()
191                 {
192                         return ReadKey (false).KeyChar;
193                 }
194
195                 public static string ReadLine ()
196                 {
197                         return driver.ReadLine ();
198                 }
199
200                 public static ConsoleKeyInfo ReadKey (bool intercept)
201                 {
202                         return driver.ReadKey (intercept);
203                 }
204
205                 public static void ResetColor ()
206                 {
207                         driver.ResetColor ();
208                 }
209
210                 public static void SetBufferSize (int width, int height)
211                 {
212                         driver.SetBufferSize (width, height);
213                 }
214
215                 public static void SetCursorPosition (int left, int top)
216                 {
217                         driver.SetCursorPosition (left, top);
218                 }
219
220                 public static void SetWindowPosition (int left, int top)
221                 {
222                         driver.SetWindowPosition (left, top);
223                 }
224
225                 public static void SetWindowSize (int width, int height)
226                 {
227                         driver.SetWindowSize (width, height);
228                 }
229
230                 public static bool IsConsole {
231                         get {
232                                 if (called_isatty)
233                                         return is_console;
234
235                                 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
236                                 called_isatty = true;
237                                 return is_console;
238                         }
239                 }
240
241                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
242                 static extern bool Isatty (IntPtr handle);
243
244                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245                 internal static extern int InternalKeyAvailable (int ms_timeout);
246
247                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
248                 internal static extern bool TtySetup (string teardown, out byte verase, out byte vsusp, out byte intr);
249
250                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251                 internal static extern bool SetEcho (bool wantEcho);
252
253                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
254                 internal static extern bool SetBreak (bool wantBreak);
255
256                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
257                 internal static extern bool GetTtySize (IntPtr handle, out int width, out int height);
258
259                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
260                 internal static extern void Suspend ();
261         }
262 }
263 #endif
264