From eede1e6675d408eb065767e1d7ea41454a4d714d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Mon, 18 Jul 2016 02:42:33 +0100 Subject: [PATCH] Better keyboard implementation --- source/Cosmos.HAL/Cosmos.HAL.csproj | 7 +- source/Cosmos.HAL/Global.cs | 52 +++--- source/Cosmos.HAL/Keyboard.cs | 94 ---------- source/Cosmos.HAL/KeyboardBase.cs | 36 ++++ source/Cosmos.HAL/PS2Keyboard.cs | 80 +------- .../Cosmos.System.Plugs/System/ConsoleImpl.cs | 7 +- source/Cosmos.System/Cosmos.System.csproj | 11 +- source/Cosmos.System/Global.cs | 31 +++- source/Cosmos.System/Kernel.cs | 37 ++-- .../Keyboard}/ConsoleKeyEx.cs | 6 +- .../Keyboard}/ConsoleKeyExExtensions.cs | 4 +- .../Keyboard}/KeyEvent.cs | 4 +- .../Keyboard}/KeyMapping.cs | 18 +- source/Cosmos.System/Keyboard/Keyboard.cs | 175 ++++++++++++++++++ .../Keyboard}/ScanMapBase.cs | 19 +- .../{ => Keyboard}/ScanMaps/FR_Standard.cs | 2 +- .../{ => Keyboard}/ScanMaps/US_Standard.cs | 2 +- source/Cosmos.System/ScanMaps/ScanMap.cs | 15 -- source/Cosmos.System/TestingHelpers.cs | 4 +- 19 files changed, 320 insertions(+), 284 deletions(-) delete mode 100644 source/Cosmos.HAL/Keyboard.cs create mode 100644 source/Cosmos.HAL/KeyboardBase.cs rename source/{Cosmos.HAL => Cosmos.System/Keyboard}/ConsoleKeyEx.cs (98%) rename source/{Cosmos.HAL => Cosmos.System/Keyboard}/ConsoleKeyExExtensions.cs (99%) rename source/{Cosmos.HAL => Cosmos.System/Keyboard}/KeyEvent.cs (98%) rename source/{Cosmos.HAL => Cosmos.System/Keyboard}/KeyMapping.cs (73%) create mode 100644 source/Cosmos.System/Keyboard/Keyboard.cs rename source/{Cosmos.HAL => Cosmos.System/Keyboard}/ScanMapBase.cs (86%) rename source/Cosmos.System/{ => Keyboard}/ScanMaps/FR_Standard.cs (99%) rename source/Cosmos.System/{ => Keyboard}/ScanMaps/US_Standard.cs (99%) delete mode 100644 source/Cosmos.System/ScanMaps/ScanMap.cs diff --git a/source/Cosmos.HAL/Cosmos.HAL.csproj b/source/Cosmos.HAL/Cosmos.HAL.csproj index 0446aab77..876c84cef 100644 --- a/source/Cosmos.HAL/Cosmos.HAL.csproj +++ b/source/Cosmos.HAL/Cosmos.HAL.csproj @@ -97,11 +97,7 @@ - - - - @@ -127,7 +123,7 @@ - + @@ -145,7 +141,6 @@ - diff --git a/source/Cosmos.HAL/Global.cs b/source/Cosmos.HAL/Global.cs index 139f2db94..0917377fe 100644 --- a/source/Cosmos.HAL/Global.cs +++ b/source/Cosmos.HAL/Global.cs @@ -10,29 +10,29 @@ namespace Cosmos.HAL { public static readonly Debugger mDebugger = new Debugger("HAL", "Global"); - public static Keyboard Keyboard; + //public static Keyboard Keyboard; - public static bool NumLock - { - get { return _numLock; } - set { _numLock = value; Keyboard?.UpdateLeds(); } - } + //public static bool NumLock + //{ + // get { return _numLock; } + // set { _numLock = value; Keyboard?.UpdateLeds(); } + //} - public static bool CapsLock - { - get { return _capsLock; } - set { _capsLock = value; Keyboard?.UpdateLeds(); } - } + //public static bool CapsLock + //{ + // get { return _capsLock; } + // set { _capsLock = value; Keyboard?.UpdateLeds(); } + //} - public static bool ScrollLock - { - get { return _scrollLock; } - set - { - _scrollLock = value; - Keyboard?.UpdateLeds(); - } - } + //public static bool ScrollLock + //{ + // get { return _scrollLock; } + // set + // { + // _scrollLock = value; + // Keyboard?.UpdateLeds(); + // } + //} //static public PIT PIT = new PIT(); // Must be static init, other static inits rely on it not being null @@ -117,21 +117,13 @@ namespace Cosmos.HAL //PCI.Setup(); } - static public void Init(TextScreenBase textScreen, Keyboard keyboard) + static public void Init(TextScreenBase textScreen) { if (textScreen != null) { TextScreen = textScreen; } - if (keyboard == null) - { - mDebugger.Send("No keyboard specified!"); - throw new SystemException("No keyboard specified!"); - } - else - { - Keyboard = keyboard; - } + mDebugger.Send("Before Core.Global.Init"); Core.Global.Init(); mDebugger.Send("Static Devices"); diff --git a/source/Cosmos.HAL/Keyboard.cs b/source/Cosmos.HAL/Keyboard.cs deleted file mode 100644 index b03cf8fd9..000000000 --- a/source/Cosmos.HAL/Keyboard.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Cosmos.Debug.Kernel; - -namespace Cosmos.HAL { - public abstract class Keyboard : Device { - protected Keyboard(ScanMapBase scanMap) - { - if (mQueuedKeys != null) - { - Debugger.DoSend("Skipping creation of key queue!"); - } - mQueuedKeys = new Queue(); - Debugger.DoSend("mQueuedKeys created"); - SetKeyLayout(scanMap); - Debugger.DoSend("Keylayout set"); - Initialize(); - Debugger.DoSend("Initialized"); - UpdateLeds(); - Debugger.DoSend("Leds updated"); - } - - /// - /// Initialize the device. Happens before the interrupt is registered, ie before the class is being used. - /// - protected abstract void Initialize(); - - public ScanMapBase KeyLayout { get; private set; } - - public void SetKeyLayout(ScanMapBase layout) - { - KeyLayout = layout; - } - - /// - /// Allow faking scancodes. Used for test kernels - /// - internal void HandleFakeScanCode(byte aScancode, bool aReleased) - { - HandleScancode(aScancode, aReleased); - } - - public abstract void UpdateLeds(); - - protected abstract void HandleScancode(byte aScancode, bool aReleased); - - private static Queue mQueuedKeys; - - protected void Enqueue(KeyEvent aKey) - { - mQueuedKeys.Enqueue(aKey); - } - - public bool TryReadKey(out KeyEvent oKey) - { - if (mQueuedKeys.Count > 0) - { - oKey = mQueuedKeys.Dequeue(); - return true; - } - oKey = default(KeyEvent); - return false; - } - - public KeyEvent ReadKey() - { - while (mQueuedKeys.Count == 0) - { - Core.Global.CPU.Halt(); - } - return mQueuedKeys.Dequeue(); - } - - public bool ShiftPressed - { - get; - protected set; - } - - public bool ControlPressed - { - get; - protected set; - } - - public bool AltPressed - { - get; - protected set; - } - } -} diff --git a/source/Cosmos.HAL/KeyboardBase.cs b/source/Cosmos.HAL/KeyboardBase.cs new file mode 100644 index 000000000..c9e1a3a22 --- /dev/null +++ b/source/Cosmos.HAL/KeyboardBase.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Cosmos.Debug.Kernel; + +namespace Cosmos.HAL +{ + public abstract class KeyboardBase : Device + { + protected KeyboardBase() + { + Debugger.DoSend("mQueuedKeys created"); + Debugger.DoSend("Keylayout set"); + Initialize(); + Debugger.DoSend("Initialized"); + UpdateLeds(); + Debugger.DoSend("Leds updated"); + } + + /// + /// Initialize the device. Happens before the interrupt is registered, ie before the class is being used. + /// + protected abstract void Initialize(); + + public abstract void UpdateLeds(); + + public delegate void KeyPressedEventHandler(byte ScanCode, bool Released); + public KeyPressedEventHandler OnKeyPressed; + + public void WaitForKey() + { + Core.Global.CPU.Halt(); + } + } +} diff --git a/source/Cosmos.HAL/PS2Keyboard.cs b/source/Cosmos.HAL/PS2Keyboard.cs index 6c0cd4537..e8006a8da 100644 --- a/source/Cosmos.HAL/PS2Keyboard.cs +++ b/source/Cosmos.HAL/PS2Keyboard.cs @@ -6,12 +6,13 @@ using Cosmos.Debug.Kernel; namespace Cosmos.HAL { - public class PS2Keyboard : Keyboard + public class PS2Keyboard : KeyboardBase { protected Core.IOGroup.Keyboard IO = Core.Global.BaseIOGroups.Keyboard; - public PS2Keyboard(ScanMapBase scanMap): base(scanMap) + public PS2Keyboard() : base() { + } protected override void Initialize() @@ -27,7 +28,7 @@ namespace Cosmos.HAL { xScanCode = (byte)(xScanCode ^ 0x80); } - HandleScancode(xScanCode, xReleased); + OnKeyPressed?.Invoke(xScanCode, xReleased); } public override void UpdateLeds() @@ -43,78 +44,5 @@ namespace Cosmos.HAL //{ //} } - - protected override void HandleScancode(byte aScancode, bool aReleased) - { - byte key = aScancode; - if (key == 0x3A && !aReleased) - { - // caps lock - Global.CapsLock = !Global.CapsLock; - UpdateLeds(); - } - else if (key == 0x45 && !aReleased) - { - // num lock - Global.NumLock = !Global.NumLock; - UpdateLeds(); - } - else if (key == 0x46 && !aReleased) - { - // scroll lock - Global.ScrollLock = !Global.ScrollLock; - UpdateLeds(); - } - else - switch (key) - { - case 0x1D: - { - ControlPressed = !aReleased; - break; - } - case 0x2A: - case 0x36: - { - ShiftPressed = !aReleased; - break; - } - case 0x38: - { - AltPressed = !aReleased; - break; - } - default: - { - if (ControlPressed && AltPressed && (key == 0x53)) - { - Console.WriteLine("Detected Ctrl-Alt-Delete! Rebooting System..."); - Core.Global.CPU.Reboot(); - } - - if (!aReleased) - { - KeyEvent keyInfo; - if (GetKey(key, aReleased, out keyInfo)) - { - Enqueue(keyInfo); - } - } - - - break; - } - } - } - - public bool GetKey(byte aScancode, bool released, out KeyEvent keyInfo) - { - if (KeyLayout == null) - { - Debugger.DoSend("No KeyLayout"); - } - keyInfo = KeyLayout.ConvertScanCode(aScancode, ControlPressed, ShiftPressed, AltPressed, Global.NumLock, Global.CapsLock, Global.ScrollLock); - return keyInfo != null; - } } } diff --git a/source/Cosmos.System.Plugs/System/ConsoleImpl.cs b/source/Cosmos.System.Plugs/System/ConsoleImpl.cs index bdc69cf2b..5c7e1e82d 100644 --- a/source/Cosmos.System.Plugs/System/ConsoleImpl.cs +++ b/source/Cosmos.System.Plugs/System/ConsoleImpl.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; -using Cosmos.HAL; using Encoding = System.Text.Encoding; using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; @@ -385,7 +384,7 @@ namespace Cosmos.System.Plugs.System // TODO special cases, if needed, that returns -1 KeyEvent xResult; - if (HAL.Global.Keyboard.TryReadKey(out xResult)) + if (Global.Keyboard.TryReadKey(out xResult)) { return xResult.KeyChar; } @@ -399,7 +398,7 @@ namespace Cosmos.System.Plugs.System public static ConsoleKeyInfo ReadKey(Boolean intercept) { - var key = Cosmos.HAL.Global.Keyboard.ReadKey(); + var key = Global.Keyboard.ReadKey(); if (false == intercept && key.KeyChar != '\0') { Write(key.KeyChar); @@ -420,7 +419,7 @@ namespace Cosmos.System.Plugs.System KeyEvent current; int currentCount = 0; - while ((current = HAL.Global.Keyboard.ReadKey()).Key != ConsoleKeyEx.Enter) + while ((current = Global.Keyboard.ReadKey()).Key != ConsoleKeyEx.Enter) { if (current.Key == ConsoleKeyEx.NumEnter) break; //Check for "special" keys diff --git a/source/Cosmos.System/Cosmos.System.csproj b/source/Cosmos.System/Cosmos.System.csproj index 68fa084b8..a16ec4dc9 100644 --- a/source/Cosmos.System/Cosmos.System.csproj +++ b/source/Cosmos.System/Cosmos.System.csproj @@ -101,6 +101,12 @@ + + + + + + @@ -118,9 +124,8 @@ - - - + + diff --git a/source/Cosmos.System/Global.cs b/source/Cosmos.System/Global.cs index 5cdb114ff..f389e6df8 100644 --- a/source/Cosmos.System/Global.cs +++ b/source/Cosmos.System/Global.cs @@ -19,22 +19,24 @@ namespace Cosmos.System public static Console Console = new Console(null); + public static Keyboard Keyboard; + public static bool NumLock { - get { return HAL.Global.NumLock; } - set { HAL.Global.NumLock = value; } + get { return Keyboard.NumLock; } + set { Keyboard.NumLock = value; } } public static bool CapsLock { - get { return HAL.Global.CapsLock; } - set { HAL.Global.CapsLock = value; } + get { return Keyboard.CapsLock; } + set { Keyboard.CapsLock = value; } } public static bool ScrollLock { - get { return HAL.Global.ScrollLock; } - set { HAL.Global.ScrollLock = value; } + get { return Keyboard.ScrollLock; } + set { Keyboard.ScrollLock = value; } } public static void Init(TextScreenBase textScreen, Keyboard keyboard) @@ -47,8 +49,19 @@ namespace Cosmos.System Console = new Console(textScreen); } + mDebugger.Send("Creating Keyboard"); + if(keyboard != null) + { + Keyboard = keyboard; + } + else + { + Debugger.DoSend("Keyboard is null. Using default Keyboard: PS2Keyboard with US_Standard scan map."); + Keyboard = new Keyboard(new PS2Keyboard(), new ScanMaps.US_Standard()); + } + mDebugger.Send("HW Init"); - HAL.Global.Init(textScreen, keyboard); + HAL.Global.Init(textScreen); NumLock = false; CapsLock = false; ScrollLock = false; @@ -57,9 +70,9 @@ namespace Cosmos.System public static void ChangeKeyLayout(ScanMapBase scanMap) { - if (scanMap != null && HAL.Global.Keyboard != null) + if (scanMap != null && Keyboard != null) { - HAL.Global.Keyboard.SetKeyLayout(scanMap); + Keyboard.SetKeyLayout(scanMap); } } } diff --git a/source/Cosmos.System/Kernel.cs b/source/Cosmos.System/Kernel.cs index 8b4fcc011..8aeecf65f 100644 --- a/source/Cosmos.System/Kernel.cs +++ b/source/Cosmos.System/Kernel.cs @@ -20,30 +20,7 @@ namespace Cosmos.System // Set to signal stopped protected bool mStopped = false; - protected ScanMap mKeyboardScanMap = new US_Standard(); - - public ScanMap KeyboardScanMap - { - get - { - if (mKeyboardScanMap == null) - { - mKeyboardScanMap = new US_Standard(); - } - - return mKeyboardScanMap; - } - - set - { - if (value != null) - { - mKeyboardScanMap = value; - Global.ChangeKeyLayout(mKeyboardScanMap); - } - return; - } - } + protected ScanMapBase DefaultKeyboardScanMap = new US_Standard(); protected virtual TextScreenBase GetTextScreen() { @@ -51,6 +28,16 @@ namespace Cosmos.System return null; } + protected ScanMapBase GetKeyboardScanMap() + { + return Global.Keyboard.GetKeyLayout(); + } + + protected void SetKeyboardScanMap(ScanMapBase ScanMap) + { + Global.Keyboard.SetKeyLayout(ScanMap); + } + /// /// Start the system up using the properties for configuration. /// @@ -75,7 +62,7 @@ namespace Cosmos.System HAL.Bootstrap.Init(); Global.mDebugger.Send("Global Init"); - Global.Init(GetTextScreen(), new PS2Keyboard(KeyboardScanMap)); + Global.Init(GetTextScreen(), new Keyboard(new PS2Keyboard(), DefaultKeyboardScanMap)); // Provide the user with a clear screen if they requested it if (ClearScreen) diff --git a/source/Cosmos.HAL/ConsoleKeyEx.cs b/source/Cosmos.System/Keyboard/ConsoleKeyEx.cs similarity index 98% rename from source/Cosmos.HAL/ConsoleKeyEx.cs rename to source/Cosmos.System/Keyboard/ConsoleKeyEx.cs index 3642e303a..94e07a5d4 100644 --- a/source/Cosmos.HAL/ConsoleKeyEx.cs +++ b/source/Cosmos.System/Keyboard/ConsoleKeyEx.cs @@ -1,4 +1,4 @@ -namespace Cosmos.HAL +namespace Cosmos.System { public enum ConsoleKeyEx { @@ -53,7 +53,7 @@ namespace Cosmos.HAL LBracket, RBracket, Enter, - + CapsLock, A, S, @@ -130,4 +130,4 @@ namespace Cosmos.HAL Sleep, Wake } -} \ No newline at end of file +} diff --git a/source/Cosmos.HAL/ConsoleKeyExExtensions.cs b/source/Cosmos.System/Keyboard/ConsoleKeyExExtensions.cs similarity index 99% rename from source/Cosmos.HAL/ConsoleKeyExExtensions.cs rename to source/Cosmos.System/Keyboard/ConsoleKeyExExtensions.cs index 57defddff..9fd4bd826 100644 --- a/source/Cosmos.HAL/ConsoleKeyExExtensions.cs +++ b/source/Cosmos.System/Keyboard/ConsoleKeyExExtensions.cs @@ -1,6 +1,6 @@ using System; -namespace Cosmos.HAL +namespace Cosmos.System { public static class ConsoleKeyExExtensions { @@ -176,4 +176,4 @@ namespace Cosmos.HAL } } } -} \ No newline at end of file +} diff --git a/source/Cosmos.HAL/KeyEvent.cs b/source/Cosmos.System/Keyboard/KeyEvent.cs similarity index 98% rename from source/Cosmos.HAL/KeyEvent.cs rename to source/Cosmos.System/Keyboard/KeyEvent.cs index 05aed93e7..b985ccadb 100644 --- a/source/Cosmos.HAL/KeyEvent.cs +++ b/source/Cosmos.System/Keyboard/KeyEvent.cs @@ -1,6 +1,6 @@ using System; -namespace Cosmos.HAL +namespace Cosmos.System { public class KeyEvent { @@ -61,4 +61,4 @@ namespace Cosmos.HAL this.Type = type; } } -} \ No newline at end of file +} diff --git a/source/Cosmos.HAL/KeyMapping.cs b/source/Cosmos.System/Keyboard/KeyMapping.cs similarity index 73% rename from source/Cosmos.HAL/KeyMapping.cs rename to source/Cosmos.System/Keyboard/KeyMapping.cs index ba25061bf..47f8d200d 100644 --- a/source/Cosmos.HAL/KeyMapping.cs +++ b/source/Cosmos.System/Keyboard/KeyMapping.cs @@ -1,8 +1,8 @@ -namespace Cosmos.HAL +namespace Cosmos.System { public class KeyMapping { - public uint Scancode; + public byte Scancode; public char Value; public char Shift; public char Num; @@ -12,7 +12,7 @@ namespace Cosmos.HAL public ConsoleKeyEx Key; public ConsoleKeyEx NumLockKey; - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey) { Scancode = aScanCode; Value = norm; @@ -25,30 +25,30 @@ namespace Cosmos.HAL NumLockKey = aKey; } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey, ConsoleKeyEx numKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey, ConsoleKeyEx numKey) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, aKey) { NumLockKey = numKey; } - public KeyMapping(uint aScanCode, char num, ConsoleKeyEx aKey, ConsoleKeyEx numKey) + public KeyMapping(byte aScanCode, char num, ConsoleKeyEx aKey, ConsoleKeyEx numKey) : this(aScanCode, '\0', '\0', num, '\0', '\0', '\0', aKey, numKey) { } - public KeyMapping(uint aScanCode, int norm, int shift, int num, int caps, int shiftcaps, int shiftnum, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, int norm, int shift, int num, int caps, int shiftcaps, int shiftnum, ConsoleKeyEx aKey) : this(aScanCode, (char)norm, (char)shift, (char)num, (char)caps, (char)shiftcaps, (char)shiftnum, aKey) { } - public KeyMapping(uint aScanCode, char n, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, char n, ConsoleKeyEx aKey) : this(aScanCode, n, n, n, n, n, n, aKey) { } - public KeyMapping(uint aScanCode, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, ConsoleKeyEx aKey) : this(aScanCode, '\0', '\0', '\0', '\0', '\0', '\0', aKey) { } } -} \ No newline at end of file +} diff --git a/source/Cosmos.System/Keyboard/Keyboard.cs b/source/Cosmos.System/Keyboard/Keyboard.cs new file mode 100644 index 000000000..acb69453b --- /dev/null +++ b/source/Cosmos.System/Keyboard/Keyboard.cs @@ -0,0 +1,175 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Cosmos.Debug.Kernel; +using Cosmos.HAL; +using Cosmos.System.ScanMaps; + +namespace Cosmos.System +{ + public class Keyboard + { + public bool NumLock + { + get; + set; + } + + public bool CapsLock + { + get; + set; + } + + public bool ScrollLock + { + get; + set; + } + + public bool ControlPressed + { + get; + set; + } + + public bool ShiftPressed + { + get; + set; + } + + public bool AltPressed + { + get; + set; + } + + protected KeyboardBase _keyboard; + protected ScanMapBase _scanMap; + + protected Queue mQueuedKeys; + + public Keyboard(KeyboardBase Keyboard, ScanMapBase ScanMap) + { + _keyboard = Keyboard; + _keyboard.OnKeyPressed += new KeyboardBase.KeyPressedEventHandler(HandleScanCode); + + if(ScanMap == null) + { + ScanMap = new US_Standard(); + } + _scanMap = ScanMap; + + mQueuedKeys = new Queue(); + } + + protected void Enqueue(KeyEvent keyEvent) + { + mQueuedKeys.Enqueue(keyEvent); + } + + /// + /// Allow faking scancodes. Used for test kernels + /// + internal void HandleFakeScanCode(byte aScancode, bool aReleased) + { + HandleScanCode(aScancode, aReleased); + } + + protected void HandleScanCode(byte aScanCode, bool aReleased) + { + byte key = aScanCode; + if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.CapsLock) && !aReleased) + { + // caps lock + CapsLock = !CapsLock; + _keyboard.UpdateLeds(); + } + else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.NumLock) && !aReleased) + { + // num lock + NumLock = !NumLock; + _keyboard.UpdateLeds(); + } + else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.ScrollLock) && !aReleased) + { + // scroll lock + ScrollLock = !ScrollLock; + _keyboard.UpdateLeds(); + } + else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LCtrl) || _scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RCtrl)) + { + ControlPressed = !aReleased; + } + else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LShift) || _scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RShift)) + { + ShiftPressed = !aReleased; + } + else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LAlt) || _scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RAlt)) + { + AltPressed = !aReleased; + } + else + { + if (ControlPressed && AltPressed && _scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.Delete)) + { + Global.Console.WriteLine("Detected Ctrl-Alt-Delete! Rebooting System..."); + Power.Reboot(); + } + + if (!aReleased) + { + KeyEvent keyInfo; + if (GetKey(key, out keyInfo)) + { + Enqueue(keyInfo); + } + } + } + } + + public bool GetKey(byte aScancode, out KeyEvent keyInfo) + { + if (_scanMap == null) + { + Debugger.DoSend("No KeyLayout"); + } + keyInfo = _scanMap.ConvertScanCode(aScancode, ControlPressed, ShiftPressed, AltPressed, NumLock, CapsLock, ScrollLock); + return keyInfo != null; + } + + public bool TryReadKey(out KeyEvent oKey) + { + if (mQueuedKeys.Count > 0) + { + oKey = mQueuedKeys.Dequeue(); + return true; + } + oKey = default(KeyEvent); + return false; + } + + public KeyEvent ReadKey() + { + while (mQueuedKeys.Count == 0) + { + _keyboard.WaitForKey(); + } + return mQueuedKeys.Dequeue(); + } + + public ScanMapBase GetKeyLayout() + { + return _scanMap; + } + + public void SetKeyLayout(ScanMapBase ScanMap) + { + _scanMap = ScanMap; + } + } +} diff --git a/source/Cosmos.HAL/ScanMapBase.cs b/source/Cosmos.System/Keyboard/ScanMapBase.cs similarity index 86% rename from source/Cosmos.HAL/ScanMapBase.cs rename to source/Cosmos.System/Keyboard/ScanMapBase.cs index 9b01d5008..ea0f211f0 100644 --- a/source/Cosmos.HAL/ScanMapBase.cs +++ b/source/Cosmos.System/Keyboard/ScanMapBase.cs @@ -3,9 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using Cosmos.Debug.Kernel; -namespace Cosmos.HAL +using Cosmos.Debug.Kernel; +using Cosmos.HAL; + +namespace Cosmos.System { public abstract class ScanMapBase { @@ -76,5 +78,18 @@ namespace Cosmos.HAL } return found ? keyev : null; } + + public bool ScanCodeMatchesKey(byte ScanCode, ConsoleKeyEx Key) + { + for (int i = 0; i < _keys.Count; i++) + { + if (_keys[i].Scancode == ScanCode && _keys[i].Key == Key) + { + return true; + } + } + + return false; + } } } diff --git a/source/Cosmos.System/ScanMaps/FR_Standard.cs b/source/Cosmos.System/Keyboard/ScanMaps/FR_Standard.cs similarity index 99% rename from source/Cosmos.System/ScanMaps/FR_Standard.cs rename to source/Cosmos.System/Keyboard/ScanMaps/FR_Standard.cs index 3288b0ae3..7155ff9bc 100644 --- a/source/Cosmos.System/ScanMaps/FR_Standard.cs +++ b/source/Cosmos.System/Keyboard/ScanMaps/FR_Standard.cs @@ -6,7 +6,7 @@ using Cosmos.HAL; namespace Cosmos.System.ScanMaps { - public class FR_Standard : ScanMap + public class FR_Standard : ScanMapBase { protected override void InitKeys() { diff --git a/source/Cosmos.System/ScanMaps/US_Standard.cs b/source/Cosmos.System/Keyboard/ScanMaps/US_Standard.cs similarity index 99% rename from source/Cosmos.System/ScanMaps/US_Standard.cs rename to source/Cosmos.System/Keyboard/ScanMaps/US_Standard.cs index 1389bf2b5..43aef8047 100644 --- a/source/Cosmos.System/ScanMaps/US_Standard.cs +++ b/source/Cosmos.System/Keyboard/ScanMaps/US_Standard.cs @@ -6,7 +6,7 @@ using Cosmos.HAL; namespace Cosmos.System.ScanMaps { - public class US_Standard : ScanMap + public class US_Standard : ScanMapBase { public US_Standard() { diff --git a/source/Cosmos.System/ScanMaps/ScanMap.cs b/source/Cosmos.System/ScanMaps/ScanMap.cs deleted file mode 100644 index 8808a9bc9..000000000 --- a/source/Cosmos.System/ScanMaps/ScanMap.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Cosmos.HAL; - -namespace Cosmos.System.ScanMaps -{ - public abstract class ScanMap : ScanMapBase - { - - } -} diff --git a/source/Cosmos.System/TestingHelpers.cs b/source/Cosmos.System/TestingHelpers.cs index 1692a3eb0..06b12c604 100644 --- a/source/Cosmos.System/TestingHelpers.cs +++ b/source/Cosmos.System/TestingHelpers.cs @@ -8,11 +8,11 @@ namespace Cosmos.System internal static void KeyboardAddFakeScanCode(byte aScanCode, bool aReleased) { Debugger.DoSend("Before HandleFakeScanCode"); - if (HAL.Global.Keyboard == null) + if (Global.Keyboard == null) { Debugger.DoSend("No Keyboard set!"); } - HAL.Global.Keyboard.HandleFakeScanCode(aScanCode, aReleased); + Global.Keyboard.HandleFakeScanCode(aScanCode, aReleased); Debugger.DoSend("After HandleFakeScanCode"); } }