Skip to content

Commit

Permalink
BIG keyboard work (not finished though)
Browse files Browse the repository at this point in the history
  • Loading branch information
zdimension committed Aug 9, 2015
1 parent 9c90d10 commit 58646d1
Show file tree
Hide file tree
Showing 10 changed files with 598 additions and 274 deletions.
124 changes: 124 additions & 0 deletions source/Cosmos.HAL/ConsoleKeyEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
namespace Cosmos.HAL
{
public enum ConsoleKeyEx
{
NoName,

Escape,

F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,

PrintScreen,
ScrollLock,
PauseBreak,

Backquote,
D1,
D2,
D3,
D4,
D5,
D6,
D7,
D8,
D9,
D0,
Minus,
Equal,
Backslash,
Backspace,

Tab,
Q,
W,
E,
R,
T,
Y,
U,
I,
O,
P,
LBracket,
RBracket,
Enter,

CapsLock,
A,
S,
D,
F,
G,
H,
J,
K,
L,
Semicolon,
Apostrophe,

Shift,
Z,
X,
C,
V,
B,
N,
M,
Comma,
Period,
Slash,

Ctrl,
LWin,
Alt,
Spacebar,
AltGr,
RWin,
Menu,

Insert,
Home,
PageUp,
Delete,
End,
PageDown,

UpArrow,
DownArrow,
LeftArrow,
RightArrow,

NumLock,
NumDivide,
NumMultiply,
NumMinus,
Num7,
Num8,
Num9,
NumPlus,
Num4,
Num5,
Num6,
Num1,
Num2,
Num3,
Num0,
NumPeriod,
NumEnter,

Power,
Sleep,
Wake
}
}
7 changes: 6 additions & 1 deletion source/Cosmos.HAL/Cosmos.HAL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@
<Compile Include="BlockDevice\MBR.cs" />
<Compile Include="BlockDevice\Partition.cs" />
<Compile Include="Bootstrap.cs" />
<Compile Include="ConsoleKeyEx.cs" />
<Compile Include="DebugTextScreen.cs" />
<Compile Include="DefaultKeyboard.cs" />
<Compile Include="KeyEvent.cs" />
<Compile Include="KeyMapping.cs" />
<Compile Include="PS2Keyboard.cs" />
<Compile Include="Device.cs" />
<Compile Include="Drivers\PCI\Audio\ES1370\Components\DACak4531.cs" />
<Compile Include="Drivers\PCI\Audio\ES1370\ES1370.cs" />
Expand Down Expand Up @@ -122,6 +125,8 @@
<Compile Include="PIT.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RTC.cs" />
<Compile Include="ScanMapBase.cs" />
<Compile Include="ScanMaps\US_Standard.cs" />
<Compile Include="TextScreen.cs" />
<Compile Include="Drivers\USB\USBHost.cs" />
<Compile Include="Drivers\USB\USBHostOHCI.cs" />
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.HAL/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static internal void InitStaticDevices() {
//TextScreen.Clear();

Global.Dbg.Send("Keyboard");
Keyboard = new DefaultKeyboard();
Keyboard = new PS2Keyboard();

// Find hardcoded ATA controllers
Global.Dbg.Send("ATA Master");
Expand Down
64 changes: 64 additions & 0 deletions source/Cosmos.HAL/KeyEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;

namespace Cosmos.HAL
{
public class KeyEvent
{
public enum KeyEventType
{
Make,
Break
}

// todo: once Github issue #137 is fixed, replace this class with ConsoleKeyInfo struct.
// Well, this one has more features

public char KeyChar
{
get;
set;
}

public ConsoleKeyEx Key
{
get;
set;
}

public ConsoleModifiers Modifiers
{
get;
set;
}

public KeyEventType Type { get; set; }

public KeyEvent()
{
KeyChar = '\0';
Key = ConsoleKeyEx.NoName;
this.Modifiers = (ConsoleModifiers)0;
Type = KeyEventType.Make;
}

public KeyEvent(char keyChar, ConsoleKeyEx key, bool shift, bool alt, bool control, KeyEventType type)
{
this.KeyChar = keyChar;
this.Key = key;
this.Modifiers = (ConsoleModifiers)0;
if (shift)
{
this.Modifiers |= ConsoleModifiers.Shift;
}
if (alt)
{
this.Modifiers |= ConsoleModifiers.Alt;
}
if (control)
{
this.Modifiers |= ConsoleModifiers.Control;
}
this.Type = type;
}
}
}
58 changes: 58 additions & 0 deletions source/Cosmos.HAL/KeyMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Cosmos.HAL
{
public class KeyMapping
{
public uint Scancode;
public char Value;
public char Shift;
public char Ctrl;
public char Alt;
public char Num;
public char Caps;
public char ShiftCaps;
public char ShiftNum;
public ConsoleKeyEx Key;
public ConsoleKeyEx NumLockKey;

public KeyMapping(uint aScanCode, char norm, char shift, char ctrl, char alt, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey)
{
Scancode = aScanCode;
Value = norm;
Shift = shift;
Ctrl = ctrl;
Alt = alt;
Num = num;
Caps = caps;
ShiftCaps = shiftcaps;
ShiftNum = shiftnum;
Key = aKey;
NumLockKey = aKey;
}

public KeyMapping(uint aScanCode, char norm, char shift, char ctrl, char alt, char num, char caps, char shiftcaps, char shiftnum, ConsoleKeyEx aKey, ConsoleKeyEx numKey)
: this(aScanCode, norm, shift, ctrl, alt, num, caps, shiftcaps, shiftnum, aKey)
{
NumLockKey = numKey;
}

public KeyMapping(uint aScanCode, char num, ConsoleKeyEx aKey, ConsoleKeyEx numKey)
: this(aScanCode, '\0', '\0', '\0', '\0', num, '\0', '\0', '\0', aKey, numKey)
{
}

public KeyMapping(uint aScanCode, int norm, int shift, int ctrl, int alt, int num, int caps, int shiftcaps, int shiftnum, ConsoleKeyEx aKey)
: this(aScanCode, (char)norm, (char)shift, (char)ctrl, (char)alt, (char)num, (char)caps, (char)shiftcaps, (char)shiftnum, aKey)
{
}

public KeyMapping(uint aScanCode, char n, ConsoleKeyEx aKey)
: this(aScanCode, n, n, n, n, n, n, n, n, aKey)
{
}

public KeyMapping(uint aScanCode, ConsoleKeyEx aKey)
: this(aScanCode, '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', aKey)
{
}
}
}
73 changes: 12 additions & 61 deletions source/Cosmos.HAL/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.HAL.ScanMaps;

namespace Cosmos.HAL {
public class ConsoleKeyInfoEx
{
// todo: once Github issue #137 is fixed, replace this class with ConsoleKeyInfo struct.

public char KeyChar
{
get;
set;
}

public ConsoleKey Key
{
get;
set;
}

public ConsoleModifiers Modifiers
{
get;
set;
}

public ConsoleKeyInfoEx(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
{
this.KeyChar = keyChar;
this.Key = key;
this.Modifiers = (ConsoleModifiers)0;
if (shift)
{
this.Modifiers |= ConsoleModifiers.Shift;
}
if (alt)
{
this.Modifiers |= ConsoleModifiers.Alt;
}
if (control)
{
this.Modifiers |= ConsoleModifiers.Control;
}
}
}

public abstract class Keyboard : Device {
// TODO: MtW: I don't like the following line in the baseclass, but for now, lets keep it here.
protected Core.IOGroup.Keyboard IO = Core.Global.BaseIOGroups.Keyboard;
protected Keyboard()
{
if (mQueuedKeys != null)
{
Console.WriteLine("Skipping creation of key queue!");
}
mQueuedKeys = new Queue<ConsoleKeyInfoEx>(32);

Initialize();
Core.INTs.SetIrqHandler(0x01, HandleIRQ);
mQueuedKeys = new Queue<KeyEvent>(32);
SetKeyLayout(new US_Standard());
}

/// <summary>
/// Initialize the device. Happens before the interrupt is registered, ie before the class is being used.
/// </summary>
protected abstract void Initialize();

private void HandleIRQ(ref Core.INTs.IRQContext aContext)
public ScanMapBase KeyLayout { get; private set; }

public void SetKeyLayout(ScanMapBase layout)
{
byte xScanCode = IO.Port60.Byte;
bool xReleased = (xScanCode & 0x80) == 0x80;
if (xReleased)
{
xScanCode = (byte)(xScanCode ^ 0x80);
}
HandleScancode(xScanCode, xReleased);
KeyLayout = layout;
}

protected abstract void HandleScancode(byte aScancode, bool aReleased);

private static Queue<ConsoleKeyInfoEx> mQueuedKeys;
private static Queue<KeyEvent> mQueuedKeys;

protected void Enqueue(ConsoleKeyInfoEx aKey)
protected void Enqueue(KeyEvent aKey)
{
mQueuedKeys.Enqueue(aKey);
}

public bool TryReadKey(out ConsoleKeyInfoEx oKey)
public bool TryReadKey(out KeyEvent oKey)
{
if (mQueuedKeys.Count > 0)
{
oKey = mQueuedKeys.Dequeue();
return true;
}
oKey = default(ConsoleKeyInfoEx);
oKey = default(KeyEvent);
return false;
}

public ConsoleKeyInfoEx ReadKey()
public KeyEvent ReadKey()
{
while (mQueuedKeys.Count == 0)
{
Expand Down
Loading

0 comments on commit 58646d1

Please sign in to comment.