From 2408319b410f71797524895d477d2e6445c6ab7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Tue, 12 Jul 2016 22:38:37 +0100 Subject: [PATCH 1/9] Keyboard Update: now it's possible to change keyboard layout(it wasn't due to Rings); Fix typo in Cosmos.System.Plugs.RuntimeTypeImpl.cs; Implement op_Inequality(implemented as op_Equality and not sure if it's needed with the changes but needed it's needed by many system methods) --- .../System/RuntimeTypeImpl.cs | 2 +- source/Cosmos.System.Plugs/System/TypeImpl.cs | 7 ++++ source/Cosmos.System/Cosmos.System.csproj | 1 + source/Cosmos.System/Global.cs | 8 +++++ source/Cosmos.System/Kernel.cs | 33 +++++++++++++++---- source/Cosmos.System/ScanMaps/FR_Standard.cs | 2 +- source/Cosmos.System/ScanMaps/ScanMap.cs | 15 +++++++++ source/Cosmos.System/ScanMaps/US_Standard.cs | 2 +- 8 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 source/Cosmos.System/ScanMaps/ScanMap.cs diff --git a/source/Cosmos.System.Plugs/System/RuntimeTypeImpl.cs b/source/Cosmos.System.Plugs/System/RuntimeTypeImpl.cs index d16de67a2..704955f36 100644 --- a/source/Cosmos.System.Plugs/System/RuntimeTypeImpl.cs +++ b/source/Cosmos.System.Plugs/System/RuntimeTypeImpl.cs @@ -9,7 +9,7 @@ namespace Cosmos.System.Plugs.System { public static string ToString(object aThis) { - throw new NotImplementedException("RuntimeTypePlug.ToStrin()"); + throw new NotImplementedException("RuntimeTypePlug.ToString()"); } } diff --git a/source/Cosmos.System.Plugs/System/TypeImpl.cs b/source/Cosmos.System.Plugs/System/TypeImpl.cs index e205a8450..e0b1dc293 100644 --- a/source/Cosmos.System.Plugs/System/TypeImpl.cs +++ b/source/Cosmos.System.Plugs/System/TypeImpl.cs @@ -30,6 +30,13 @@ namespace Cosmos.System.Plugs.System return left == right; } + [PlugMethod(Signature = "System_Boolean__System_Type_op_Inequality_System_Type__System_Type_")] + public static bool op_Inequality(uint left, uint right) + { + // for now, type info is the type id. + return left != right; + } + //System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle) } } diff --git a/source/Cosmos.System/Cosmos.System.csproj b/source/Cosmos.System/Cosmos.System.csproj index 6fdf4ecb2..68fa084b8 100644 --- a/source/Cosmos.System/Cosmos.System.csproj +++ b/source/Cosmos.System/Cosmos.System.csproj @@ -119,6 +119,7 @@ + diff --git a/source/Cosmos.System/Global.cs b/source/Cosmos.System/Global.cs index 169386e38..5cdb114ff 100644 --- a/source/Cosmos.System/Global.cs +++ b/source/Cosmos.System/Global.cs @@ -54,5 +54,13 @@ namespace Cosmos.System ScrollLock = false; //Network.NetworkStack.Init(); } + + public static void ChangeKeyLayout(ScanMapBase scanMap) + { + if (scanMap != null && HAL.Global.Keyboard != null) + { + HAL.Global.Keyboard.SetKeyLayout(scanMap); + } + } } } diff --git a/source/Cosmos.System/Kernel.cs b/source/Cosmos.System/Kernel.cs index 48db41ec2..8b4fcc011 100644 --- a/source/Cosmos.System/Kernel.cs +++ b/source/Cosmos.System/Kernel.cs @@ -20,18 +20,37 @@ 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 virtual TextScreenBase GetTextScreen() { // null means use default return null; } - protected virtual ScanMapBase GetKeyboardScanMap() - { - return new US_Standard(); - } - - /// /// Start the system up using the properties for configuration. /// @@ -56,7 +75,7 @@ namespace Cosmos.System HAL.Bootstrap.Init(); Global.mDebugger.Send("Global Init"); - Global.Init(GetTextScreen(), new PS2Keyboard(GetKeyboardScanMap())); + Global.Init(GetTextScreen(), new PS2Keyboard(KeyboardScanMap)); // Provide the user with a clear screen if they requested it if (ClearScreen) diff --git a/source/Cosmos.System/ScanMaps/FR_Standard.cs b/source/Cosmos.System/ScanMaps/FR_Standard.cs index 7155ff9bc..3288b0ae3 100644 --- a/source/Cosmos.System/ScanMaps/FR_Standard.cs +++ b/source/Cosmos.System/ScanMaps/FR_Standard.cs @@ -6,7 +6,7 @@ using Cosmos.HAL; namespace Cosmos.System.ScanMaps { - public class FR_Standard : ScanMapBase + public class FR_Standard : ScanMap { protected override void InitKeys() { diff --git a/source/Cosmos.System/ScanMaps/ScanMap.cs b/source/Cosmos.System/ScanMaps/ScanMap.cs new file mode 100644 index 000000000..8808a9bc9 --- /dev/null +++ b/source/Cosmos.System/ScanMaps/ScanMap.cs @@ -0,0 +1,15 @@ +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/ScanMaps/US_Standard.cs b/source/Cosmos.System/ScanMaps/US_Standard.cs index 43aef8047..1389bf2b5 100644 --- a/source/Cosmos.System/ScanMaps/US_Standard.cs +++ b/source/Cosmos.System/ScanMaps/US_Standard.cs @@ -6,7 +6,7 @@ using Cosmos.HAL; namespace Cosmos.System.ScanMaps { - public class US_Standard : ScanMapBase + public class US_Standard : ScanMap { public US_Standard() { 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 2/9] 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"); } } From 47222a3a9629815b0d301aa7d05121a5862862e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Mon, 18 Jul 2016 21:26:35 +0100 Subject: [PATCH 3/9] Keyboard update: KeyboardManager static class to manage different keyboards. --- source/Cosmos.HAL/Global.cs | 27 ------ source/Cosmos.HAL/KeyboardBase.cs | 2 +- .../Cosmos.System.Plugs/System/ConsoleImpl.cs | 6 +- source/Cosmos.System/Cosmos.System.csproj | 2 +- source/Cosmos.System/Global.cs | 30 ++---- source/Cosmos.System/Kernel.cs | 11 ++- .../{Keyboard.cs => KeyboardManager.cs} | 94 +++++++++++-------- source/Cosmos.System/TestingHelpers.cs | 6 +- 8 files changed, 77 insertions(+), 101 deletions(-) rename source/Cosmos.System/Keyboard/{Keyboard.cs => KeyboardManager.cs} (63%) diff --git a/source/Cosmos.HAL/Global.cs b/source/Cosmos.HAL/Global.cs index 0917377fe..58de43503 100644 --- a/source/Cosmos.HAL/Global.cs +++ b/source/Cosmos.HAL/Global.cs @@ -10,38 +10,11 @@ namespace Cosmos.HAL { public static readonly Debugger mDebugger = new Debugger("HAL", "Global"); - //public static Keyboard Keyboard; - - //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 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 public static TextScreenBase TextScreen = new TextScreen(); public static PCI Pci; - private static bool _numLock; - private static bool _capsLock; - private static bool _scrollLock; private static void InitAta(Ata.ControllerIdEnum aControllerID, Ata.BusPositionEnum aBusPosition) diff --git a/source/Cosmos.HAL/KeyboardBase.cs b/source/Cosmos.HAL/KeyboardBase.cs index c9e1a3a22..066ec3588 100644 --- a/source/Cosmos.HAL/KeyboardBase.cs +++ b/source/Cosmos.HAL/KeyboardBase.cs @@ -28,7 +28,7 @@ namespace Cosmos.HAL public delegate void KeyPressedEventHandler(byte ScanCode, bool Released); public KeyPressedEventHandler OnKeyPressed; - public void WaitForKey() + public static void WaitForKey() { Core.Global.CPU.Halt(); } diff --git a/source/Cosmos.System.Plugs/System/ConsoleImpl.cs b/source/Cosmos.System.Plugs/System/ConsoleImpl.cs index 5c7e1e82d..a3b55ffcf 100644 --- a/source/Cosmos.System.Plugs/System/ConsoleImpl.cs +++ b/source/Cosmos.System.Plugs/System/ConsoleImpl.cs @@ -384,7 +384,7 @@ namespace Cosmos.System.Plugs.System // TODO special cases, if needed, that returns -1 KeyEvent xResult; - if (Global.Keyboard.TryReadKey(out xResult)) + if (KeyboardManager.TryReadKey(out xResult)) { return xResult.KeyChar; } @@ -398,7 +398,7 @@ namespace Cosmos.System.Plugs.System public static ConsoleKeyInfo ReadKey(Boolean intercept) { - var key = Global.Keyboard.ReadKey(); + var key = KeyboardManager.ReadKey(); if (false == intercept && key.KeyChar != '\0') { Write(key.KeyChar); @@ -419,7 +419,7 @@ namespace Cosmos.System.Plugs.System KeyEvent current; int currentCount = 0; - while ((current = Global.Keyboard.ReadKey()).Key != ConsoleKeyEx.Enter) + while ((current = KeyboardManager.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 a16ec4dc9..59f656e82 100644 --- a/source/Cosmos.System/Cosmos.System.csproj +++ b/source/Cosmos.System/Cosmos.System.csproj @@ -103,7 +103,7 @@ - + diff --git a/source/Cosmos.System/Global.cs b/source/Cosmos.System/Global.cs index f389e6df8..c45c2b23a 100644 --- a/source/Cosmos.System/Global.cs +++ b/source/Cosmos.System/Global.cs @@ -19,27 +19,25 @@ namespace Cosmos.System public static Console Console = new Console(null); - public static Keyboard Keyboard; - public static bool NumLock { - get { return Keyboard.NumLock; } - set { Keyboard.NumLock = value; } + get { return KeyboardManager.NumLock; } + set { KeyboardManager.NumLock = value; } } public static bool CapsLock { - get { return Keyboard.CapsLock; } - set { Keyboard.CapsLock = value; } + get { return KeyboardManager.CapsLock; } + set { KeyboardManager.CapsLock = value; } } public static bool ScrollLock { - get { return Keyboard.ScrollLock; } - set { Keyboard.ScrollLock = value; } + get { return KeyboardManager.ScrollLock; } + set { KeyboardManager.ScrollLock = value; } } - public static void Init(TextScreenBase textScreen, Keyboard keyboard) + public static void Init(TextScreenBase textScreen) { // We must init Console before calling Inits. // This is part of the "minimal" boot to allow output. @@ -50,15 +48,6 @@ namespace Cosmos.System } 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); @@ -70,10 +59,7 @@ namespace Cosmos.System public static void ChangeKeyLayout(ScanMapBase scanMap) { - if (scanMap != null && Keyboard != null) - { - Keyboard.SetKeyLayout(scanMap); - } + KeyboardManager.SetKeyLayout(scanMap); } } } diff --git a/source/Cosmos.System/Kernel.cs b/source/Cosmos.System/Kernel.cs index 8aeecf65f..2558dabf5 100644 --- a/source/Cosmos.System/Kernel.cs +++ b/source/Cosmos.System/Kernel.cs @@ -20,8 +20,6 @@ namespace Cosmos.System // Set to signal stopped protected bool mStopped = false; - protected ScanMapBase DefaultKeyboardScanMap = new US_Standard(); - protected virtual TextScreenBase GetTextScreen() { // null means use default @@ -30,12 +28,12 @@ namespace Cosmos.System protected ScanMapBase GetKeyboardScanMap() { - return Global.Keyboard.GetKeyLayout(); + return KeyboardManager.GetKeyLayout(); } protected void SetKeyboardScanMap(ScanMapBase ScanMap) { - Global.Keyboard.SetKeyLayout(ScanMap); + KeyboardManager.SetKeyLayout(ScanMap); } /// @@ -62,7 +60,10 @@ namespace Cosmos.System HAL.Bootstrap.Init(); Global.mDebugger.Send("Global Init"); - Global.Init(GetTextScreen(), new Keyboard(new PS2Keyboard(), DefaultKeyboardScanMap)); + Global.Init(GetTextScreen()); + + //Start with a PS2Keyboard + KeyboardManager.AddKeyboard(new PS2Keyboard()); // Provide the user with a clear screen if they requested it if (ClearScreen) diff --git a/source/Cosmos.System/Keyboard/Keyboard.cs b/source/Cosmos.System/Keyboard/KeyboardManager.cs similarity index 63% rename from source/Cosmos.System/Keyboard/Keyboard.cs rename to source/Cosmos.System/Keyboard/KeyboardManager.cs index acb69453b..3fb5e8fcd 100644 --- a/source/Cosmos.System/Keyboard/Keyboard.cs +++ b/source/Cosmos.System/Keyboard/KeyboardManager.cs @@ -10,64 +10,51 @@ using Cosmos.System.ScanMaps; namespace Cosmos.System { - public class Keyboard + public static class KeyboardManager { - public bool NumLock + + public static bool NumLock { get; set; } - public bool CapsLock + public static bool CapsLock { get; set; } - public bool ScrollLock + public static bool ScrollLock { get; set; } - public bool ControlPressed + public static bool ControlPressed { get; set; } - public bool ShiftPressed + public static bool ShiftPressed { get; set; } - public bool AltPressed + public static bool AltPressed { get; set; } - protected KeyboardBase _keyboard; - protected ScanMapBase _scanMap; + public static List Keyboards = new List(); - protected Queue mQueuedKeys; + private static ScanMapBase _scanMap = new US_Standard(); + private static Queue mQueuedKeys = new Queue(); - 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) + private static void Enqueue(KeyEvent keyEvent) { mQueuedKeys.Enqueue(keyEvent); } @@ -75,31 +62,31 @@ namespace Cosmos.System /// /// Allow faking scancodes. Used for test kernels /// - internal void HandleFakeScanCode(byte aScancode, bool aReleased) + internal static void HandleFakeScanCode(byte aScancode, bool aReleased) { HandleScanCode(aScancode, aReleased); } - protected void HandleScanCode(byte aScanCode, bool aReleased) + private static void HandleScanCode(byte aScanCode, bool aReleased) { byte key = aScanCode; if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.CapsLock) && !aReleased) { // caps lock CapsLock = !CapsLock; - _keyboard.UpdateLeds(); + UpdateLeds(); } else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.NumLock) && !aReleased) { // num lock NumLock = !NumLock; - _keyboard.UpdateLeds(); + UpdateLeds(); } else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.ScrollLock) && !aReleased) { // scroll lock ScrollLock = !ScrollLock; - _keyboard.UpdateLeds(); + UpdateLeds(); } else if (_scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.LCtrl) || _scanMap.ScanCodeMatchesKey(key, ConsoleKeyEx.RCtrl)) { @@ -132,7 +119,15 @@ namespace Cosmos.System } } - public bool GetKey(byte aScancode, out KeyEvent keyInfo) + private static void UpdateLeds() + { + foreach(KeyboardBase keyboard in Keyboards) + { + keyboard.UpdateLeds(); + } + } + + public static bool GetKey(byte aScancode, out KeyEvent keyInfo) { if (_scanMap == null) { @@ -142,7 +137,7 @@ namespace Cosmos.System return keyInfo != null; } - public bool TryReadKey(out KeyEvent oKey) + public static bool TryReadKey(out KeyEvent oKey) { if (mQueuedKeys.Count > 0) { @@ -153,23 +148,48 @@ namespace Cosmos.System return false; } - public KeyEvent ReadKey() + public static KeyEvent ReadKey() { while (mQueuedKeys.Count == 0) { - _keyboard.WaitForKey(); + KeyboardBase.WaitForKey(); } return mQueuedKeys.Dequeue(); } - public ScanMapBase GetKeyLayout() + public static ScanMapBase GetKeyLayout() { return _scanMap; } - public void SetKeyLayout(ScanMapBase ScanMap) + public static void SetKeyLayout(ScanMapBase ScanMap) { - _scanMap = ScanMap; + if (ScanMap != null) + { + _scanMap = ScanMap; + } + } + + public static void AddKeyboard(KeyboardBase Keyboard) + { + if (!KeyboardExists(Keyboard.GetType())) + { + Keyboard.OnKeyPressed += new KeyboardBase.KeyPressedEventHandler(HandleScanCode); + Keyboards.Add(Keyboard); + } + } + + public static bool KeyboardExists(Type KeyboardType) + { + foreach (KeyboardBase Keyboard in Keyboards) + { + if (Keyboard.GetType() == KeyboardType) + { + return true; + } + } + + return false; } } } diff --git a/source/Cosmos.System/TestingHelpers.cs b/source/Cosmos.System/TestingHelpers.cs index 06b12c604..f73b5f5de 100644 --- a/source/Cosmos.System/TestingHelpers.cs +++ b/source/Cosmos.System/TestingHelpers.cs @@ -8,11 +8,7 @@ namespace Cosmos.System internal static void KeyboardAddFakeScanCode(byte aScanCode, bool aReleased) { Debugger.DoSend("Before HandleFakeScanCode"); - if (Global.Keyboard == null) - { - Debugger.DoSend("No Keyboard set!"); - } - Global.Keyboard.HandleFakeScanCode(aScanCode, aReleased); + KeyboardManager.HandleFakeScanCode(aScanCode, aReleased); Debugger.DoSend("After HandleFakeScanCode"); } } From 3238ebce2462bc68d9e3764aead9f04e7edc1f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Wed, 24 Aug 2016 19:39:04 +0100 Subject: [PATCH 4/9] Small change --- source/Cosmos.HAL/KeyboardBase.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/Cosmos.HAL/KeyboardBase.cs b/source/Cosmos.HAL/KeyboardBase.cs index 066ec3588..8e93a11a0 100644 --- a/source/Cosmos.HAL/KeyboardBase.cs +++ b/source/Cosmos.HAL/KeyboardBase.cs @@ -10,8 +10,6 @@ namespace Cosmos.HAL { protected KeyboardBase() { - Debugger.DoSend("mQueuedKeys created"); - Debugger.DoSend("Keylayout set"); Initialize(); Debugger.DoSend("Initialized"); UpdateLeds(); From 74ccda1ffcc60b3bf79a144b87ff4117434f7dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Wed, 24 Aug 2016 20:04:07 +0100 Subject: [PATCH 5/9] Finished merging --- source/Cosmos.System/Cosmos.System.csproj | 10 ++++----- source/Cosmos.System/Keyboard/KeyMapping.cs | 22 +++++++++---------- .../{ => Keyboard}/ScanMaps/DE_Standard.cs | 0 3 files changed, 16 insertions(+), 16 deletions(-) rename source/Cosmos.System/{ => Keyboard}/ScanMaps/DE_Standard.cs (100%) diff --git a/source/Cosmos.System/Cosmos.System.csproj b/source/Cosmos.System/Cosmos.System.csproj index acf2d453c..35e3fda0e 100644 --- a/source/Cosmos.System/Cosmos.System.csproj +++ b/source/Cosmos.System/Cosmos.System.csproj @@ -107,6 +107,9 @@ + + + @@ -124,9 +127,6 @@ - - - @@ -154,11 +154,11 @@ - - \ No newline at end of file + diff --git a/source/Cosmos.System/Keyboard/KeyMapping.cs b/source/Cosmos.System/Keyboard/KeyMapping.cs index 77bbbdc51..937b326fa 100644 --- a/source/Cosmos.System/Keyboard/KeyMapping.cs +++ b/source/Cosmos.System/Keyboard/KeyMapping.cs @@ -16,7 +16,7 @@ namespace Cosmos.System public ConsoleKeyEx Key; public ConsoleKeyEx NumLockKey; - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, char ctrl, char shiftctrl, ConsoleKeyEx aKey, ConsoleKeyEx numKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, char ctrl, char shiftctrl, ConsoleKeyEx aKey, ConsoleKeyEx numKey) { Scancode = aScanCode; Value = norm; @@ -34,37 +34,37 @@ namespace Cosmos.System NumLockKey = numKey; } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, ConsoleKeyEx aKey, ConsoleKeyEx numKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, ConsoleKeyEx aKey, ConsoleKeyEx numKey) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, shiftaltgr, '\0', '\0', aKey, numKey) { } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, ConsoleKeyEx aKey, ConsoleKeyEx numKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, ConsoleKeyEx aKey, ConsoleKeyEx numKey) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, '\0', '\0', '\0', aKey, numKey) { } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, char ctrl, char shiftctrl, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, char ctrl, char shiftctrl, ConsoleKeyEx aKey) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, shiftaltgr, ctrl, shiftctrl, aKey, aKey) { } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, ConsoleKeyEx aKey) - : this (aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, shiftaltgr, '\0', '\0', aKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, char shiftaltgr, ConsoleKeyEx aKey) + : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, shiftaltgr, '\0', '\0', aKey) { } - public KeyMapping(uint aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, char norm, char shift, char num, char caps, char shiftcaps, char shiftnum, char altgr, ConsoleKeyEx aKey) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, altgr, '\0', '\0', '\0', aKey) { } - 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) : this(aScanCode, norm, shift, num, caps, shiftcaps, shiftnum, '\0', '\0', '\0', '\0', 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, '\0', aKey, numKey) { } @@ -74,12 +74,12 @@ namespace Cosmos.System { } - 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, char n, ConsoleKeyEx aKey) + public KeyMapping(byte aScanCode, char n, ConsoleKeyEx aKey) : this(aScanCode, n, n, n, n, n, n, aKey) { } diff --git a/source/Cosmos.System/ScanMaps/DE_Standard.cs b/source/Cosmos.System/Keyboard/ScanMaps/DE_Standard.cs similarity index 100% rename from source/Cosmos.System/ScanMaps/DE_Standard.cs rename to source/Cosmos.System/Keyboard/ScanMaps/DE_Standard.cs From 171ea2e96b39bbfd31ebbf233c797ce678d3b58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Thu, 15 Sep 2016 01:52:38 +0100 Subject: [PATCH 6/9] Force AppVeyor build. From da7e34014840f8f5fe3e528bb3723ff6a242686c Mon Sep 17 00:00:00 2001 From: jp2masa Date: Mon, 24 Oct 2016 20:49:22 +0100 Subject: [PATCH 7/9] Trying to fix KeyboardManager.cs --- source/Cosmos.System/Keyboard/KeyboardManager.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/Cosmos.System/Keyboard/KeyboardManager.cs b/source/Cosmos.System/Keyboard/KeyboardManager.cs index 3fb5e8fcd..3b03d524a 100644 --- a/source/Cosmos.System/Keyboard/KeyboardManager.cs +++ b/source/Cosmos.System/Keyboard/KeyboardManager.cs @@ -111,6 +111,7 @@ namespace Cosmos.System if (!aReleased) { KeyEvent keyInfo; + if (GetKey(key, out keyInfo)) { Enqueue(keyInfo); @@ -133,7 +134,9 @@ namespace Cosmos.System { Debugger.DoSend("No KeyLayout"); } + keyInfo = _scanMap.ConvertScanCode(aScancode, ControlPressed, ShiftPressed, AltPressed, NumLock, CapsLock, ScrollLock); + return keyInfo != null; } @@ -144,7 +147,9 @@ namespace Cosmos.System oKey = mQueuedKeys.Dequeue(); return true; } + oKey = default(KeyEvent); + return false; } @@ -154,6 +159,7 @@ namespace Cosmos.System { KeyboardBase.WaitForKey(); } + return mQueuedKeys.Dequeue(); } @@ -174,7 +180,7 @@ namespace Cosmos.System { if (!KeyboardExists(Keyboard.GetType())) { - Keyboard.OnKeyPressed += new KeyboardBase.KeyPressedEventHandler(HandleScanCode); + Keyboard.OnKeyPressed = new KeyboardBase.KeyPressedEventHandler(HandleScanCode); Keyboards.Add(Keyboard); } } From a50641b615244d1de3c4b8b5dfc06d4df0ac4d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Mon, 24 Oct 2016 21:21:24 +0100 Subject: [PATCH 8/9] Updated exception message on ObjectPointerAccessAttribute errors. Fixed op_Equality and op_Inequality. --- source/Cosmos.IL2CPU/AppAssembler.cs | 4 ++-- source/Cosmos.System.Plugs/System/TypeImpl.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/Cosmos.IL2CPU/AppAssembler.cs b/source/Cosmos.IL2CPU/AppAssembler.cs index 9c173e349..73d600d03 100644 --- a/source/Cosmos.IL2CPU/AppAssembler.cs +++ b/source/Cosmos.IL2CPU/AppAssembler.cs @@ -1292,7 +1292,7 @@ namespace Cosmos.IL2CPU { if (ILOp.TypeIsReferenceType(aFrom.MethodBase.DeclaringType) && !ILOp.TypeIsReferenceType(xParams[0].ParameterType)) { - throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute!"); + throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute! Method: " + aFrom.MethodBase.GetFullName() + " Parameter: " + xParams[0].Name); } } @@ -1332,7 +1332,7 @@ namespace Cosmos.IL2CPU { if (ILOp.TypeIsReferenceType(xFromParameters[xOriginalParamsIdx].ParameterType) && !ILOp.TypeIsReferenceType(xParams[xCurParamIdx].ParameterType)) { - throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute!"); + throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute! Method: " + aFrom.MethodBase.GetFullName() + " Parameter: " + xParams[0].Name); } // normal field access XS.Comment("Loading parameter " + (xCurParamIdx + xCurParamOffset)); diff --git a/source/Cosmos.System.Plugs/System/TypeImpl.cs b/source/Cosmos.System.Plugs/System/TypeImpl.cs index e0b1dc293..8358ff082 100644 --- a/source/Cosmos.System.Plugs/System/TypeImpl.cs +++ b/source/Cosmos.System.Plugs/System/TypeImpl.cs @@ -24,14 +24,14 @@ namespace Cosmos.System.Plugs.System } [PlugMethod(Signature = "System_Boolean__System_Type_op_Equality_System_Type__System_Type_")] - public static bool op_Equality(uint left, uint right) + public static bool op_Equality([ObjectPointerAccess]uint left, uint right) { // for now, type info is the type id. return left == right; } [PlugMethod(Signature = "System_Boolean__System_Type_op_Inequality_System_Type__System_Type_")] - public static bool op_Inequality(uint left, uint right) + public static bool op_Inequality([ObjectPointerAccess]uint left, uint right) { // for now, type info is the type id. return left != right; From 53c9ecaa061afc8a6f278d1fd826689a04b7326e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Mon, 24 Oct 2016 21:28:05 +0100 Subject: [PATCH 9/9] Bugfix --- source/Cosmos.IL2CPU/AppAssembler.cs | 2 +- source/Cosmos.System.Plugs/System/TypeImpl.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Cosmos.IL2CPU/AppAssembler.cs b/source/Cosmos.IL2CPU/AppAssembler.cs index 73d600d03..b36c15db9 100644 --- a/source/Cosmos.IL2CPU/AppAssembler.cs +++ b/source/Cosmos.IL2CPU/AppAssembler.cs @@ -1332,7 +1332,7 @@ namespace Cosmos.IL2CPU { if (ILOp.TypeIsReferenceType(xFromParameters[xOriginalParamsIdx].ParameterType) && !ILOp.TypeIsReferenceType(xParams[xCurParamIdx].ParameterType)) { - throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute! Method: " + aFrom.MethodBase.GetFullName() + " Parameter: " + xParams[0].Name); + throw new Exception("Original method argument $this is a reference type. Plug attribute first argument is not an argument type, nor was it marked with ObjectPointerAccessAttribute! Method: " + aFrom.MethodBase.GetFullName() + " Parameter: " + xParam.Name); } // normal field access XS.Comment("Loading parameter " + (xCurParamIdx + xCurParamOffset)); diff --git a/source/Cosmos.System.Plugs/System/TypeImpl.cs b/source/Cosmos.System.Plugs/System/TypeImpl.cs index 8358ff082..dfa1e55a2 100644 --- a/source/Cosmos.System.Plugs/System/TypeImpl.cs +++ b/source/Cosmos.System.Plugs/System/TypeImpl.cs @@ -24,14 +24,14 @@ namespace Cosmos.System.Plugs.System } [PlugMethod(Signature = "System_Boolean__System_Type_op_Equality_System_Type__System_Type_")] - public static bool op_Equality([ObjectPointerAccess]uint left, uint right) + public static bool op_Equality([ObjectPointerAccess]uint left, [ObjectPointerAccess]uint right) { // for now, type info is the type id. return left == right; } [PlugMethod(Signature = "System_Boolean__System_Type_op_Inequality_System_Type__System_Type_")] - public static bool op_Inequality([ObjectPointerAccess]uint left, uint right) + public static bool op_Inequality([ObjectPointerAccess]uint left, [ObjectPointerAccess]uint right) { // for now, type info is the type id. return left != right;