Merge pull request #3406 from alexanderkyte/mobile_static_default
[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 !MOBILE
31 using System.IO;
32 using System.Runtime.CompilerServices;
33
34 namespace System {
35         static class ConsoleDriver {
36                 internal static IConsoleDriver driver;
37                 static bool is_console;
38                 static bool called_isatty;
39
40                 static ConsoleDriver ()
41                 {
42                         // Put the actual new statements into separate methods to avoid initalizing
43                         // three classes when only one is needed.
44                         if (!IsConsole) {
45                                 driver = CreateNullConsoleDriver ();
46                         } else if (Environment.IsRunningOnWindows) {
47                                 driver = CreateWindowsConsoleDriver ();
48                         } else {
49                                 string term = Environment.GetEnvironmentVariable ("TERM");
50
51                                 // Perhaps we should let the Terminfo driver return a
52                                 // success/failure flag based on the terminal properties
53                                 if (term == "dumb"){
54                                         is_console = false;
55                                         driver = CreateNullConsoleDriver ();
56                                 } else
57                                         driver = CreateTermInfoDriver (term);
58                         }
59                 }
60
61                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
62                 static IConsoleDriver CreateNullConsoleDriver () {
63                         return new NullConsoleDriver ();
64                 }
65
66                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
67                 static IConsoleDriver CreateWindowsConsoleDriver () {
68                         return new WindowsConsoleDriver ();
69                 }
70
71                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
72                 static IConsoleDriver CreateTermInfoDriver (string term) {
73                         return new TermInfoDriver (term);
74                 }
75                 
76                 public static bool Initialized {
77                         get { return driver.Initialized; }
78                 }
79
80                 public static ConsoleColor BackgroundColor {
81                         get { return driver.BackgroundColor; }
82                         set {
83                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
84                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
85
86                                 driver.BackgroundColor = value;
87                         }
88                 }
89
90                 public static int BufferHeight {
91                         get { return driver.BufferHeight; }
92                         set { driver.BufferHeight = value; }
93                 }
94
95                 public static int BufferWidth {
96                         get { return driver.BufferWidth; }
97                         set { driver.BufferWidth = value; }
98                 }
99
100                 public static bool CapsLock {
101                         get { return driver.CapsLock; }
102                 }
103
104                 public static int CursorLeft {
105                         get { return driver.CursorLeft; }
106                         set { driver.CursorLeft = value; }
107                 }
108
109                 public static int CursorSize {
110                         get { return driver.CursorSize; }
111                         set { driver.CursorSize = value; }
112                 }
113
114                 public static int CursorTop {
115                         get { return driver.CursorTop; }
116                         set { driver.CursorTop = value; }
117                 } 
118                 
119                 public static bool CursorVisible {
120                         get { return driver.CursorVisible; }
121                         set { driver.CursorVisible = value; }
122                 }
123
124                 public static bool KeyAvailable {
125                         get { return driver.KeyAvailable; }
126                 }
127
128                 public static ConsoleColor ForegroundColor {
129                         get { return driver.ForegroundColor; }
130                         set {
131                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
132                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
133
134                                 driver.ForegroundColor = value;
135                         }
136                 }
137
138                 public static int LargestWindowHeight {
139                         get { return driver.LargestWindowHeight; }
140                 }
141
142                 public static int LargestWindowWidth {
143                         get { return driver.LargestWindowWidth; }
144                 }
145
146                 public static bool NumberLock {
147                         get { return driver.NumberLock; }
148                 }
149
150                 public static string Title {
151                         get { return driver.Title; }
152                         set { driver.Title = value; }
153                 }
154
155                 public static bool TreatControlCAsInput {
156                         get { return driver.TreatControlCAsInput; }
157                         set { driver.TreatControlCAsInput = value; }
158                 } 
159
160                 public static int WindowHeight {
161                         get { return driver.WindowHeight; }
162                         set { driver.WindowHeight = value; }
163                 }
164
165                 public static int WindowLeft {
166                         get { return driver.WindowLeft; }
167                         set { driver.WindowLeft = value; }
168                 }
169
170                 public static int WindowTop {
171                         get { return driver.WindowTop; }
172                         set { driver.WindowTop = value; }
173                 }
174
175                 public static int WindowWidth {
176                         get { return driver.WindowWidth; }
177                         set { driver.WindowWidth = value; }
178                 }
179
180                 public static bool IsErrorRedirected {
181                         get {
182                                 return !Isatty (MonoIO.ConsoleError);
183                         }
184                 }
185
186                 public static bool IsOutputRedirected {
187                         get {
188                                 return !Isatty (MonoIO.ConsoleOutput);
189                         }
190                 }
191
192                 public static bool IsInputRedirected {
193                         get {
194                                 return !Isatty (MonoIO.ConsoleInput);
195                         }
196                 }
197
198                 public static void Beep (int frequency, int duration)
199                 {
200                         driver.Beep (frequency, duration);
201                 }
202                 
203                 public static void Clear ()
204                 {
205                         driver.Clear ();
206                 }
207
208                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
209                                         int targetLeft, int targetTop)
210                 {
211                         MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
212                                         targetLeft, targetTop, ' ', 0, 0);
213                 }
214
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)
218                 {
219                         driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
220                                         targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
221                 }
222
223                 public static void Init ()
224                 {
225                         driver.Init ();
226                 }
227
228                 public static int Read ()
229                 {
230                         return ReadKey (false).KeyChar;
231                 }
232
233                 public static string ReadLine ()
234                 {
235                         return driver.ReadLine ();
236                 }
237
238                 public static ConsoleKeyInfo ReadKey (bool intercept)
239                 {
240                         return driver.ReadKey (intercept);
241                 }
242
243                 public static void ResetColor ()
244                 {
245                         driver.ResetColor ();
246                 }
247
248                 public static void SetBufferSize (int width, int height)
249                 {
250                         driver.SetBufferSize (width, height);
251                 }
252
253                 public static void SetCursorPosition (int left, int top)
254                 {
255                         driver.SetCursorPosition (left, top);
256                 }
257
258                 public static void SetWindowPosition (int left, int top)
259                 {
260                         driver.SetWindowPosition (left, top);
261                 }
262
263                 public static void SetWindowSize (int width, int height)
264                 {
265                         driver.SetWindowSize (width, height);
266                 }
267
268                 public static bool IsConsole {
269                         get {
270                                 if (called_isatty)
271                                         return is_console;
272
273                                 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
274                                 called_isatty = true;
275                                 return is_console;
276                         }
277                 }
278
279                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
280                 static extern bool Isatty (IntPtr handle);
281
282                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283                 internal static extern int InternalKeyAvailable (int ms_timeout);
284
285                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
286                 unsafe internal static extern bool TtySetup (string keypadXmit, string teardown, out byte [] control_characters, out int *address);
287
288                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
289                 internal static extern bool SetEcho (bool wantEcho);
290
291                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
292                 internal static extern bool SetBreak (bool wantBreak);
293         }
294 }
295 #endif
296