Navigation Menu

Skip to content

Commit

Permalink
Userspace keyboard driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkie committed Jan 21, 2010
1 parent a5b8f00 commit ff6992c
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 158 deletions.
75 changes: 70 additions & 5 deletions app/d/testapp.d
Expand Up @@ -12,6 +12,9 @@ import user.ramfs;

import libos.console;
import libos.ramfs;
import libos.keyboard;

import user.keycodes;

void main() {

Expand All @@ -29,12 +32,74 @@ void main() {

Console.putString("\n > ");

//char[128] str;
Keyboard.initialize();


char[128] str;
uint pos = 0;

bool released;
for(;;) {
Key key = Keyboard.nextKey(released);
if (!released) {
if (key == Key.Return) {
Console.putChar('\n');

// while(read(fd, str, str.length) == str.length){
// Console.putString(str);
// }
if (pos != 0) {
// interpret str
interpret(str[0..pos]);
}

for(;;) {}
// print prompt
Console.putString(" > ");

// go back to start
pos = 0;
}
else if (key == Key.Backspace) {
if (pos > 0) {
uint x,y;
Console.getPosition(x,y);
Console.setPosition(x-1,y);
Console.putChar(' ');
Console.setPosition(x-1,y);
pos--;
}
}
else {
char translate = Keyboard.translateKey(key);
if (translate != '\0' && pos < 128) {
str[pos] = translate;
Console.putChar(translate);
pos++;
}
}
}
}
Console.putString("Done");
}

bool streq(char[] stra, char[] strb) {
if (stra.length != strb.length) {
return false;
}

foreach(size_t i, c; stra) {
if (strb[i] != c) {
return false;
}
}

return true;
}

void interpret(char[] str) {
if (streq(str, "clear")) {
Console.clear();
}
else {
Console.putString("Unknown Command: ");
Console.putString(str);
Console.putString(".\n");
}
}
Binary file modified build/iso/boot/testapp
Binary file not shown.
2 changes: 1 addition & 1 deletion kernel/arch/x86_64/architecture/keyboard.d
Expand Up @@ -250,7 +250,7 @@ private:
0x4B: Key.Left,
0x50: Key.Down,
0x4D: Key.Right,
0x35: Key.KeypadBackslash,
0x35: Key.KeypadSlash,
0x1C: Key.KeypadReturn,

/*0xff: Key.Application,
Expand Down
4 changes: 2 additions & 2 deletions kernel/core/syscall.d
Expand Up @@ -82,13 +82,13 @@ public:
}

SyscallError open(out ubyte* ret, OpenArgs* params) {
Gib gib = RamFS.open(params.path, params.flags);
Gib gib = RamFS.open(params.path, params.flags, params.index);
ret = gib.ptr;
return SyscallError.OK;
}

SyscallError create(out ubyte* ret, CreateArgs* params) {
Gib gib = RamFS.create(params.path, params.flags);
Gib gib = RamFS.create(params.path, params.flags, params.index);
ret = gib.ptr;
return SyscallError.OK;
}
Expand Down
159 changes: 24 additions & 135 deletions kernel/dev/keyboard.d
Expand Up @@ -2,6 +2,7 @@ module kernel.dev.keyboard;

// Import the architecture specific keyboard driver
import architecture.keyboard;
import architecture.vm;

import kernel.core.error;

Expand All @@ -20,156 +21,44 @@ static:

ErrorVal initialize() {
_buffer = RamFS.create("/devices/keyboard", Access.Kernel | Access.Read | Access.Write);
_writeOffset = cast(ushort*)_buffer.ptr;
*_writeOffset = 0;
_buffer.seek(2);
_readOffset = cast(ushort*)_buffer.pos;
*_readOffset = 0;
_buffer.seek(2);
_buffer.write(cast(ushort)(3 * VirtualMemory.getPageSize()));
_maxOffset = ((3 * VirtualMemory.getPageSize()) / 2) - 3;
ErrorVal ret = KeyboardImplementation.initialize(&putKey);
return ret;
}

private:

void putKey(Key nextKey, bool released) {
keyState[nextKey] = !released;

char translated = translateScancode(nextKey);
if (translated != '\0' && !released) {
// printable
Console.putCharUnsafe(translated);
}

if (released) {
nextKey = -nextKey;
}
}

static bool keyState[256] = false;
Gib _buffer;

char translateScancode(Key scanCode) {
// keyboard scancodes are ordered by their position on the keyboard

// check for shift state
bool up = false;
char trans = '\0';

if (keyState[Key.LeftShift] || keyState[Key.RightShift]) {
// up key
up = true;
if ((((*_writeOffset)+1) == *_readOffset) || ((*_writeOffset + 1) >= _maxOffset && (*_readOffset == 0))) {
// lose this key
return;
}

if (scanCode >= Key.A && scanCode <= Key.Z) {
if (up) {
trans = 'A' + (scanCode - Key.A);
}
else {
trans = 'a' + (scanCode - Key.A);
}
}
else if (scanCode >= Key.Num0 && scanCode <= Key.Num9) {
if (up) {
switch (scanCode) {
case Key.Num0:
trans = ')';
break;
case Key.Num1:
trans = '!';
break;
case Key.Num2:
trans = '@';
break;
case Key.Num3:
trans = '#';
break;
case Key.Num4:
trans = '$';
break;
case Key.Num5:
trans = '%';
break;
case Key.Num6:
trans = '^';
break;
case Key.Num7:
trans = '&';
break;
case Key.Num8:
trans = '*';
break;
default:
case Key.Num9:
trans = '(';
break;
}
}
else {
trans = '0' + (scanCode - Key.Num0);
}
}
else if (scanCode == Key.Space) {
trans = ' ';
}
else if (scanCode == Key.Tab) {
trans = '\t';
}
else if (scanCode == Key.Quote) {
if (up) trans = '~'; else trans = '`';
}
else if (scanCode == Key.LeftBracket) {
if (up) trans = '{'; else trans = '[';
}
else if (scanCode == Key.RightBracket) {
if (up) trans = '}'; else trans = ']';
}
else if (scanCode == Key.Minus) {
if (up) trans = '_'; else trans = '-';
}
else if (scanCode == Key.Equals) {
if (up) trans = '+'; else trans = '=';
// put in the buffer at the write pointer position
_buffer.write(cast(short)nextKey);
if ((*_writeOffset + 1) >= _maxOffset) {
_buffer.rewind();
_buffer.seek(6);
*_writeOffset = 0;
}
else if (scanCode == Key.Comma) {
if (up) trans = '<'; else trans = ',';
else {
*_writeOffset = (*_writeOffset) + 1;
}
else if (scanCode == Key.Period) {
if (up) trans = '_'; else trans = '.';
}
else if (scanCode == Key.Semicolon) {
if (up) trans = ':'; else trans = ';';
}
else if (scanCode == Key.Apostrophe) {
if (up) trans = '"'; else trans = '\'';
}
else if (scanCode == Key.Slash) {
if (up) trans = '|'; else trans = '\\';
}
else if (scanCode == Key.Backslash) {
if (up) trans = '?'; else trans = '/';
}
else if (scanCode == Key.Return) {
trans = '\n';
}
else if (scanCode >= Key.Keypad0 && scanCode <= Key.Keypad9) {
if (!(up)) {
trans = '0' + (scanCode - Key.Keypad0);
}
}
else if (scanCode == Key.KeypadAsterisk) {
trans = '*';
}
else if (scanCode == Key.KeypadMinus) {
trans = '-';
}
else if (scanCode == Key.KeypadBackslash) {
trans = '/';
}
else if (scanCode == Key.KeypadPlus) {
trans = '+';
}
else if (scanCode == Key.KeypadReturn) {
trans = '\n';
}
else if (scanCode == Key.KeypadPeriod) {
trans = '.';
}

return trans;
}

Gib _buffer;
ushort* _writeOffset;
ushort* _readOffset;
ushort _maxOffset;
}
8 changes: 4 additions & 4 deletions kernel/filesystem/ramfs.d
Expand Up @@ -219,7 +219,7 @@ static:
return ErrorVal.Fail;
}

Gib create(char[] name, uint flags) {
Gib create(char[] name, uint flags, uint gibIndex = 1) {
// Open directory where name should be placed
char[] path;
char[] filename;
Expand All @@ -230,20 +230,20 @@ static:

ubyte* dirptr = locate(path);
Directory dir;
dir.gib = GibAllocator.open(dirptr, Access.Kernel | Access.Read | Access.Write);
dir.gib = GibAllocator.open(dirptr, Access.Kernel | Access.Read | Access.Write, gibIndex);

newGib = GibAllocator.alloc(flags);
dir.link(newGib, filename);

return newGib;
}

Gib open(char[] name, uint flags) {
Gib open(char[] name, uint flags, uint gibIndex = 1) {
// Open directory where name should be placed
Gib gib;
ubyte* gibptr = locate(name);
if (gibptr !is null) {
gib = GibAllocator.open(gibptr, flags);
gib = GibAllocator.open(gibptr, flags, gibIndex);
}
return gib;
}
Expand Down
6 changes: 2 additions & 4 deletions kernel/mem/giballocator.d
Expand Up @@ -25,9 +25,8 @@ struct GibAllocator {
static:
public:

Gib alloc(uint flags) {
Gib alloc(uint flags, uint gibIndex = 1) {
Gib ret;
uint gibIndex = 1;
if (flags & Access.Kernel != 0) {
// kernel gib
gibIndex = nextFreeKernelGib;
Expand All @@ -40,10 +39,9 @@ public:
return ret;
}

Gib open(ubyte* gibaddr, uint flags) {
Gib open(ubyte* gibaddr, uint flags, uint gibIndex = 1) {
Gib ret;
ret._gibaddr = gibaddr;
uint gibIndex = 1;
if (flags & Access.Kernel != 0) {
// kernel gib
gibIndex = nextFreeKernelGib;
Expand Down
10 changes: 10 additions & 0 deletions libos/console.d
Expand Up @@ -60,6 +60,16 @@ static:
}
}

void getPosition(out uint x, out uint y) {
x = videoInfo.xpos;
y = videoInfo.ypos;
}

void setPosition(uint x, uint y) {
videoInfo.xpos = x;
videoInfo.ypos = y;
}

void clear() {
ubyte* ptr = cast(ubyte*)videoBuffer;

Expand Down

0 comments on commit ff6992c

Please sign in to comment.