File: WindowsConsoleDriver.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (630 lines) | stat: -rwxr-xr-x 20,295 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//
// System.WindowsConsoleDriver
//
// Author:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2005 Novell, Inc. (http://www.novell.com)
//

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if MONO_FEATURE_CONSOLE
using System.Runtime.InteropServices;
using System.Text;
namespace System {
	struct ConsoleCursorInfo {
		public int Size;
		public bool Visible;
	}

#pragma warning disable 169
	struct InputRecord {
		public short EventType;
		// This is KEY_EVENT_RECORD
		public bool KeyDown;
		public short RepeatCount;
		public short VirtualKeyCode;
		public short VirtualScanCode;
		public char Character;
		public int ControlKeyState;
		int pad1;
		bool pad2;
		//
	}
#pragma warning restore 169

	struct CharInfo {
		public char Character;
		public short Attributes;
	}
	
	struct Coord {
		public short X;
		public short Y;

		public Coord (int x, int y)
		{
			X = (short) x;
			Y = (short) y;
		}
	}

	struct SmallRect {
		public short Left;
		public short Top;
		public short Right;
		public short Bottom;

		public SmallRect (int left, int top, int right, int bottom)
		{
			Left = (short) left;
			Top = (short) top;
			Right = (short) right;
			Bottom = (short) bottom;
		}
	}

	struct ConsoleScreenBufferInfo {
		public Coord Size;
		public Coord CursorPosition;
		public short Attribute;
		public SmallRect Window;
		public Coord MaxWindowSize;
	}

	enum Handles {
		STD_INPUT = -10,
		STD_OUTPUT = -11,
		STD_ERROR = -12
	}

	unsafe class WindowsConsoleDriver : IConsoleDriver {
		IntPtr inputHandle;
		IntPtr outputHandle;
		short defaultAttribute;

		public WindowsConsoleDriver ()
		{
			outputHandle = GetStdHandle (Handles.STD_OUTPUT);
			inputHandle = GetStdHandle (Handles.STD_INPUT);
			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);
			defaultAttribute = info.Attribute; // Not sure about this...
		}

		// FOREGROUND_BLUE	1
		// FOREGROUND_GREEN	2
		// FOREGROUND_RED	4
		// FOREGROUND_INTENSITY	8
		// BACKGROUND_BLUE	16
		// BACKGROUND_GREEN	32
		// BACKGROUND_RED	64
		// BACKGROUND_INTENSITY	128
		static ConsoleColor GetForeground (short attr)
		{
			attr &= 0x0F;
			return (ConsoleColor) attr;
		}

		static ConsoleColor GetBackground (short attr)
		{
			attr &= 0xF0;
			attr >>= 4;
			return (ConsoleColor) attr;
		}

		static short GetAttrForeground (int attr, ConsoleColor color)
		{
			attr &= ~15;
			return (short) (attr | (int) color);
		}

		static short GetAttrBackground (int attr, ConsoleColor color)
		{
			attr &= ~0xf0;
			int c = ((int) color) << 4;
			return (short) (attr | c);
		}

		public ConsoleColor BackgroundColor {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return GetBackground (info.Attribute);
			}
			set {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				short attr = GetAttrBackground (info.Attribute, value);
				SetConsoleTextAttribute (outputHandle, attr);
			}
		}

		public int BufferHeight {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Size.Y;
			}
			set { SetBufferSize (BufferWidth, value); }
		}

		public int BufferWidth {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Size.X;
			}
			set { SetBufferSize (value, BufferHeight); }
		}

		public bool CapsLock {
			get {
				short state = GetKeyState (20); // VK_CAPITAL
				return ((state & 1) == 1);
			}
		}

		public int CursorLeft {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.CursorPosition.X;
			}
			set { SetCursorPosition (value, CursorTop); }
		}

		public int CursorSize {
			get {
				ConsoleCursorInfo info = new ConsoleCursorInfo ();
				GetConsoleCursorInfo (outputHandle, out info);
				return info.Size;
			}
			set {
				if (value < 1 || value > 100)
					throw new ArgumentOutOfRangeException ("value");

				ConsoleCursorInfo info = new ConsoleCursorInfo ();
				GetConsoleCursorInfo (outputHandle, out info);
				info.Size = value;
				if (!SetConsoleCursorInfo (outputHandle, ref info))
					throw new Exception ("SetConsoleCursorInfo failed");
			}
		}

		public int CursorTop {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.CursorPosition.Y;
			}
			set { SetCursorPosition (CursorLeft, value); }
		}

		public bool CursorVisible {
			get {
				ConsoleCursorInfo info = new ConsoleCursorInfo ();
				GetConsoleCursorInfo (outputHandle, out info);
				return info.Visible;
			}
			set {
				ConsoleCursorInfo info = new ConsoleCursorInfo ();
				GetConsoleCursorInfo (outputHandle, out info);
				if (info.Visible == value)
					return;

				info.Visible = value;
				if (!SetConsoleCursorInfo (outputHandle, ref info))
					throw new Exception ("SetConsoleCursorInfo failed");
			}
		}

		public ConsoleColor ForegroundColor {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return GetForeground (info.Attribute);
			}
			set {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				short attr = GetAttrForeground (info.Attribute, value);
				SetConsoleTextAttribute (outputHandle, attr);
			}
		}

		public bool KeyAvailable {
			get {
				int eventsRead;
				InputRecord record = new InputRecord ();
				while (true) {
					// Use GetNumberOfConsoleInputEvents and remove the while?
					if (!PeekConsoleInput (inputHandle, out record, 1, out eventsRead))
						throw new InvalidOperationException ("Error in PeekConsoleInput " +
										Marshal.GetLastWin32Error ());

					if (eventsRead == 0)
						return false;

					//KEY_EVENT == 1
					if (record.EventType == 1 && record.KeyDown && !IsModifierKey (record.VirtualKeyCode))
						return true;

					if (!ReadConsoleInput (inputHandle, out record, 1, out eventsRead))
						throw new InvalidOperationException ("Error in ReadConsoleInput " +
										Marshal.GetLastWin32Error ());
				}
			}
		}

		public bool Initialized { // Not useful on windows, so false.
			get { return false; }
		}

		public int LargestWindowHeight {
			get {
				Coord coord = GetLargestConsoleWindowSize (outputHandle);
				if (coord.X == 0 && coord.Y == 0)
					throw new Exception ("GetLargestConsoleWindowSize" + Marshal.GetLastWin32Error ());

				return coord.Y;
			}
		}

		public int LargestWindowWidth {
			get {
				Coord coord = GetLargestConsoleWindowSize (outputHandle);
				if (coord.X == 0 && coord.Y == 0)
					throw new Exception ("GetLargestConsoleWindowSize" + Marshal.GetLastWin32Error ());

				return coord.X;
			}
		}

		public bool NumberLock {
			get {
				short state = GetKeyState (144); // VK_NUMLOCK
				return ((state & 1) == 1);
			}
		}

		public string Title {
			get {
				StringBuilder sb = new StringBuilder (1024); // hope this is enough
				if (GetConsoleTitle (sb, 1024) == 0) {
					// Try the maximum
					sb = new StringBuilder (26001);
					if (GetConsoleTitle (sb, 26000) == 0)
						throw new Exception ("Got " + Marshal.GetLastWin32Error ());
				}

				return sb.ToString ();
			}
			set {
				if (value == null)
					throw new ArgumentNullException ("value");

				if (!SetConsoleTitle (value))
					throw new Exception ("Got " + Marshal.GetLastWin32Error ());
			}
		}

		public bool TreatControlCAsInput {
			get {
				int mode;
				if (!GetConsoleMode (inputHandle, out mode))
					throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());

				// ENABLE_PROCESSED_INPUT
				return ((mode & 1) == 0);
			}

			set {
				int mode;
				if (!GetConsoleMode (inputHandle, out mode))
					throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());

				bool cAsInput = ((mode & 1) == 0);
				if (cAsInput == value)
					return;

				if (value)
					mode &= ~1;
				else
					mode |= 1;

				if (!SetConsoleMode (inputHandle, mode))
					throw new Exception ("Failed in SetConsoleMode: " + Marshal.GetLastWin32Error ());
			}
		}

		public int WindowHeight {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Window.Bottom - info.Window.Top + 1;
			}
			set { SetWindowSize (WindowWidth, value); }
		}

		public int WindowLeft {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Window.Left;
			}
			set { SetWindowPosition (value, WindowTop); }
		}

		public int WindowTop {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Window.Top;
			}
			set { SetWindowPosition (WindowLeft, value); }
		}

		public int WindowWidth {
			get {
				ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
				GetConsoleScreenBufferInfo (outputHandle, out info);
				return info.Window.Right - info.Window.Left + 1;
			}
			set { SetWindowSize (value, WindowHeight); }
		}

		public void Beep (int frequency, int duration)
		{
			_Beep (frequency, duration);
		}

		public void Clear ()
		{
			Coord coord = new Coord ();
			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);

			int size = info.Size.X * info.Size.Y;
			int written;
			FillConsoleOutputCharacter (outputHandle, ' ', size, coord, out written);

			GetConsoleScreenBufferInfo (outputHandle, out info);

			FillConsoleOutputAttribute (outputHandle, info.Attribute, size, coord, out written);
			SetConsoleCursorPosition (outputHandle, coord);
		}

		public void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
					int targetLeft, int targetTop, Char sourceChar,
					ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
		{
			if (sourceForeColor < 0)
				throw new ArgumentException ("Cannot be less than 0.", "sourceForeColor");

			if (sourceBackColor < 0)
				throw new ArgumentException ("Cannot be less than 0.", "sourceBackColor");

			if (sourceWidth == 0 || sourceHeight == 0)
				return;

			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);
			CharInfo [] buffer = new CharInfo [sourceWidth * sourceHeight];
			Coord bsize = new Coord (sourceWidth, sourceHeight);
			Coord bpos = new Coord (0, 0);
			SmallRect region = new SmallRect (sourceLeft, sourceTop, sourceLeft + sourceWidth - 1, sourceTop + sourceHeight - 1);
			fixed (void *ptr = &buffer [0]) {
				if (!ReadConsoleOutput (outputHandle, ptr, bsize, bpos, ref region))
					throw new ArgumentException (String.Empty, "Cannot read from the specified coordinates.");
			}

			int written;
			short attr  = GetAttrForeground (0, sourceForeColor);
			attr  = GetAttrBackground (attr, sourceBackColor);
			bpos = new Coord (sourceLeft, sourceTop);
			for (int i = 0; i < sourceHeight; i++, bpos.Y++) {
				FillConsoleOutputCharacter (outputHandle, sourceChar, sourceWidth, bpos, out written);
				FillConsoleOutputAttribute (outputHandle, attr, sourceWidth, bpos, out written);
			}

			bpos = new Coord (0, 0);
			region = new SmallRect (targetLeft, targetTop, targetLeft + sourceWidth - 1, targetTop + sourceHeight - 1);
			if (!WriteConsoleOutput (outputHandle, buffer, bsize, bpos, ref region))
				throw new ArgumentException (String.Empty, "Cannot write to the specified coordinates.");
		}

		public void Init ()
		{
		}

		public string ReadLine ()
		{
			StringBuilder builder = new StringBuilder ();
			bool exit = false;
			do {
				ConsoleKeyInfo key = ReadKey (false);
				char c = key.KeyChar;
				exit = (c == '\n');
				if (!exit)
					builder.Append (key.KeyChar);
			} while (!exit);
			return builder.ToString ();
		}

		public ConsoleKeyInfo ReadKey (bool intercept)
		{
			int eventsRead;
			InputRecord record = new InputRecord ();
			for (;;) {
				if (!ReadConsoleInput (inputHandle, out record, 1, out eventsRead))
					throw new InvalidOperationException ("Error in ReadConsoleInput " +
									Marshal.GetLastWin32Error ());
				if (record.KeyDown && record.EventType == 1 && !IsModifierKey (record.VirtualKeyCode))
					break;
			}

			// RIGHT_ALT_PRESSED 1
			// LEFT_ALT_PRESSED 2
			// RIGHT_CTRL_PRESSED 4
			// LEFT_CTRL_PRESSED 8
			// SHIFT_PRESSED 16
			bool alt = ((record.ControlKeyState & 3) != 0);
			bool ctrl = ((record.ControlKeyState & 12) != 0);
			bool shift = ((record.ControlKeyState & 16) != 0);
			return new ConsoleKeyInfo (record.Character, (ConsoleKey) record.VirtualKeyCode, shift, alt, ctrl);
		}

		public void ResetColor ()
		{
			SetConsoleTextAttribute (outputHandle, defaultAttribute);
		}

		public void SetBufferSize (int width, int height)
		{
			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);

			if (width - 1 > info.Window.Right)
				throw new ArgumentOutOfRangeException ("width");

			if (height - 1 > info.Window.Bottom)
				throw new ArgumentOutOfRangeException ("height");

			Coord coord = new Coord (width, height);
			if (!SetConsoleScreenBufferSize (outputHandle, coord))
				throw new ArgumentOutOfRangeException ("height/width", "Cannot be smaller than the window size.");
		}

		public void SetCursorPosition (int left, int top)
		{
			Coord coord = new Coord (left, top);
			SetConsoleCursorPosition (outputHandle, coord);
		}

		public void SetWindowPosition (int left, int top)
		{
			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);
			SmallRect rect = info.Window;
			rect.Left = (short) left;
			rect.Top = (short) top;
			if (!SetConsoleWindowInfo (outputHandle, true, ref rect))
				throw new ArgumentOutOfRangeException ("left/top", "Windows error " + Marshal.GetLastWin32Error ());
		}

		public void SetWindowSize (int width, int height)
		{
			ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
			GetConsoleScreenBufferInfo (outputHandle, out info);
			SmallRect rect = info.Window;
			rect.Right = (short) (rect.Left + width - 1);
			rect.Bottom = (short) (rect.Top + height - 1);
			if (!SetConsoleWindowInfo (outputHandle, true, ref rect))
				throw new ArgumentOutOfRangeException ("left/top", "Windows error " + Marshal.GetLastWin32Error ());
		}

		static bool IsModifierKey (short virtualKeyCode)
		{
			// 0x10 through 0x14 is shift/control/alt/pause/capslock
			// 0x2C is print screen, 0x90 is numlock, 0x91 is scroll lock
			switch (virtualKeyCode) {
			case 0x10:
			case 0x11:
			case 0x12:
			case 0x14:
			case 0x90:
			case 0x91:
				return true;
			default:
				return false;
			}
		}

		//
		// Imports
		//
		[DllImport ("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static IntPtr GetStdHandle (Handles handle);

		[DllImport ("kernel32.dll", EntryPoint="Beep", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static void _Beep (int frequency, int duration);

		[DllImport ("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool GetConsoleScreenBufferInfo (IntPtr handle, out ConsoleScreenBufferInfo info);

		[DllImport ("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool FillConsoleOutputCharacter (IntPtr handle, char c, int size, Coord coord, out int written);

		[DllImport ("kernel32.dll", EntryPoint="FillConsoleOutputAttribute", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool FillConsoleOutputAttribute (IntPtr handle, short c, int size, Coord coord, out int written);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleCursorPosition (IntPtr handle, Coord coord);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleTextAttribute", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleTextAttribute (IntPtr handle, short attribute);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleScreenBufferSize", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleScreenBufferSize (IntPtr handle, Coord newSize);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleWindowInfo", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleWindowInfo (IntPtr handle, bool absolute, ref SmallRect rect);

		[DllImport ("kernel32.dll", EntryPoint="GetConsoleTitle", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static int GetConsoleTitle (StringBuilder sb, int size);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleTitle", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleTitle (string title);

		[DllImport ("kernel32.dll", EntryPoint="GetConsoleCursorInfo", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool GetConsoleCursorInfo (IntPtr handle, out ConsoleCursorInfo info);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleCursorInfo", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleCursorInfo (IntPtr handle, ref ConsoleCursorInfo info);

		[DllImport ("user32.dll", EntryPoint="GetKeyState", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static short GetKeyState (int virtKey);

		[DllImport ("kernel32.dll", EntryPoint="GetConsoleMode", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool GetConsoleMode (IntPtr handle, out int mode);

		[DllImport ("kernel32.dll", EntryPoint="SetConsoleMode", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool SetConsoleMode (IntPtr handle, int mode);

		[DllImport ("kernel32.dll", EntryPoint="PeekConsoleInput", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool PeekConsoleInput (IntPtr handle, out InputRecord record, int length, out int eventsRead);

		[DllImport ("kernel32.dll", EntryPoint="ReadConsoleInput", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool ReadConsoleInput (IntPtr handle, out InputRecord record, int length, out int nread);

		[DllImport ("kernel32.dll", EntryPoint="GetLargestConsoleWindowSize", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static Coord GetLargestConsoleWindowSize (IntPtr handle);

		[DllImport ("kernel32.dll", EntryPoint="ReadConsoleOutput", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool ReadConsoleOutput (IntPtr handle, void *buffer, Coord bsize, Coord bpos, ref SmallRect region);

		[DllImport ("kernel32.dll", EntryPoint="WriteConsoleOutput", SetLastError=true, CharSet=CharSet.Unicode)]
		extern static bool WriteConsoleOutput (IntPtr handle, CharInfo [] buffer, Coord bsize, Coord bpos, ref SmallRect region);
	}
}
#endif