using System.Collections.Generic;
using Cosmos.HAL;
using Cosmos.System.ScanMaps;
using MyConsole = System.Console;
namespace Cosmos.System
{
///
/// Keyboard manager class. Used to manage keyboard.
///
public static class KeyboardManager
{
///
/// Get and set NumLock.
///
public static bool NumLock { get; set; }
///
/// Get and set CapsLock.
///
public static bool CapsLock { get; set; }
///
/// Get and set ScrollLock.
///
public static bool ScrollLock { get; set; }
///
/// Get and set Ctrl pressed.
///
public static bool ControlPressed { get; set; }
///
/// Get and set Shift pressed.
///
public static bool ShiftPressed { get; set; }
///
/// Get and set Alt pressed.
///
public static bool AltPressed { get; set; }
///
/// Get if queued keys exists.
///
public static bool KeyAvailable => mQueuedKeys.Count > 0;
private static List mKeyboardList = new List();
private static ScanMapBase mScanMap = new US_Standard();
private static Queue mQueuedKeys = new Queue();
///
/// Create new instance of the class.
///
/// An I/O error occurred.
static KeyboardManager()
{
foreach (var keyboard in HAL.Global.GetKeyboardDevices())
{
AddKeyboard(keyboard);
}
}
///
/// Enqueue keyEvent.
///
/// KeyEvent to enqueue.
private static void Enqueue(KeyEvent keyEvent)
{
mQueuedKeys.Enqueue(keyEvent);
}
///
/// Allow faking scancodes. Used for test kernels
///
/// A scan code.
/// Key released.
/// An I/O error occurred.
internal static void HandleFakeScanCode(byte aScancode, bool aReleased)
{
HandleScanCode(aScancode, aReleased);
}
///
/// Handle scan code. Used to update LEDs,
///
/// A scan code.
/// Key released.
/// An I/O error occurred.
private static void HandleScanCode(byte aScanCode, bool aReleased)
{
Global.mDebugger.Send("KeyboardManager.HandleScanCode");
byte key = aScanCode;
if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.CapsLock) && !aReleased)
{
// caps lock
CapsLock = !CapsLock;
UpdateLeds();
}
else if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.NumLock) && !aReleased)
{
// num lock
NumLock = !NumLock;
UpdateLeds();
}
else if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.ScrollLock) && !aReleased)
{
// scroll lock
ScrollLock = !ScrollLock;
UpdateLeds();
}
else if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LCtrl) || mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RCtrl))
{
ControlPressed = !aReleased;
}
else if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LShift) || mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RShift))
{
ShiftPressed = !aReleased;
}
else if (mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LAlt) || mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RAlt))
{
AltPressed = !aReleased;
}
else
{
if (ControlPressed && AltPressed && mScanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.Delete))
{
//Global.Console.WriteLine("Detected Ctrl-Alt-Delete! Rebooting System...");
MyConsole.WriteLine("Detected Ctrl-Alt-Delete! Rebooting System...");
Power.Reboot();
}
if (!aReleased)
{
KeyEvent keyInfo;
if (GetKey(key, out keyInfo))
{
Enqueue(keyInfo);
}
}
}
}
///
/// Update keyboard LEDs.
///
private static void UpdateLeds()
{
foreach (KeyboardBase keyboard in mKeyboardList)
{
keyboard.UpdateLeds();
}
}
///
/// Get key pressed.
///
/// A scan code.
/// KeyEvent output.
/// bool value.
public static bool GetKey(byte aScancode, out KeyEvent keyInfo)
{
if (mScanMap == null)
{
Global.mDebugger.Send("No KeyLayout");
}
keyInfo = mScanMap.ConvertScanCode(aScancode, ControlPressed, ShiftPressed, AltPressed, NumLock, CapsLock, ScrollLock);
return keyInfo != null;
}
///
/// Try read key.
///
/// Output KeyEvent.
/// bool value.
/// Thrown when queue is empty.
public static bool TryReadKey(out KeyEvent oKey)
{
if (mQueuedKeys.Count > 0)
{
oKey = mQueuedKeys.Dequeue();
return true;
}
oKey = default(KeyEvent);
return false;
}
///
/// Read key.
///
/// KeyEvent value.
/// Thrown when queue is empty.
public static KeyEvent ReadKey()
{
while (mQueuedKeys.Count == 0)
{
KeyboardBase.WaitForKey();
}
return mQueuedKeys.Dequeue();
}
///
/// Get key layout.
///
/// ScanMapBase value.
public static ScanMapBase GetKeyLayout()
{
return mScanMap;
}
///
/// Set key layout.
///
/// A scan map
public static void SetKeyLayout(ScanMapBase aScanMap)
{
if (aScanMap != null)
{
mScanMap = aScanMap;
}
}
///
/// Add keyboard
///
/// A keyboard to add.
/// An I/O error occurred.
private static void AddKeyboard(KeyboardBase aKeyboard)
{
Global.mDebugger.Send("KeyboardManager.AddKeyboard");
aKeyboard.OnKeyPressed = HandleScanCode;
mKeyboardList.Add(aKeyboard);
}
}
}