os-k/kaleid/kernel/io/keyb.c

175 lines
5.1 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Basic Keyboard Driver //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel/base.h>
#include <kernel/iomisc.h>
#include <extras/buf.h>
#include <kernel/keyboard.h>
static char EarlyScanCodes[100] = { 0 };
static char Invisible[100] = { 0 };
char *ScanCodes = 0;
void KeybPrint(char code)
{
uchar ch = ScanCodes[(int)code];
if (ch != 0) {
bputc(BStdIn, ch);
if (code && Invisible[(int)code] == 0) {
bputc(BStdOut, ScanCodes[(int)code]);
//bprintf(BStdOut, "%x ", code);
BStdOut->flusher(BStdOut);
}
}
}
void ScanCodesInit(void)
{
ScanCodes = &EarlyScanCodes[0];
ScanCodes[0x01] = KEY_ESC;
Invisible[0x01] = 1;
ScanCodes[0x02] = '1';
ScanCodes[0x03] = '2';
ScanCodes[0x04] = '3';
ScanCodes[0x05] = '4';
ScanCodes[0x06] = '5';
ScanCodes[0x07] = '6';
ScanCodes[0x08] = '7';
ScanCodes[0x09] = '8';
ScanCodes[0x0A] = '9';
ScanCodes[0x0B] = '0';
ScanCodes[0x0C] = ' ';
ScanCodes[0x0D] = '+';
ScanCodes[0x0E] = 0x8;
ScanCodes[0x0F] = '\t';
ScanCodes[0x10] = 'a';
ScanCodes[0x11] = 'z';
ScanCodes[0x12] = 'e';
ScanCodes[0x13] = 'r';
ScanCodes[0x14] = 't';
ScanCodes[0x15] = 'y';
ScanCodes[0x16] = 'u';
ScanCodes[0x17] = 'i';
ScanCodes[0x18] = 'o';
ScanCodes[0x19] = 'p';
ScanCodes[0x1A] = '^';
ScanCodes[0x1B] = '$';
ScanCodes[0x1E] = 'q';
ScanCodes[0x1F] = 's';
ScanCodes[0x20] = 'd';
ScanCodes[0x21] = 'f';
ScanCodes[0x22] = 'g';
ScanCodes[0x23] = 'h';
ScanCodes[0x24] = 'j';
ScanCodes[0x25] = 'k';
ScanCodes[0x26] = 'l';
ScanCodes[0x27] = 'm';
ScanCodes[0x28] = 'u';
ScanCodes[0x29] = '*';
ScanCodes[0x2C] = 'w';
ScanCodes[0x2D] = 'x';
ScanCodes[0x2E] = 'c';
ScanCodes[0x2F] = 'v';
ScanCodes[0x30] = 'b';
ScanCodes[0x31] = 'n';
ScanCodes[0x32] = ',';
ScanCodes[0x33] = ';';
ScanCodes[0x34] = ':';
ScanCodes[0x35] = '!';
ScanCodes[0x1C] = '\n';
ScanCodes[0x39] = ' ';
ScanCodes[0x40] = KEY_BEL;
Invisible[0x40] = 1;
// Numpad
ScanCodes[0x48] = KEY_DC1; // DC1, will serve as Arrow Up
ScanCodes[0x50] = KEY_DC2; // DC2, will serve as Arrow Down
ScanCodes[0x4B] = KEY_DC3; // DC3, will serve as Arrow Left
ScanCodes[0x4D] = KEY_DC4; // DC4, will serve as Arrow Right
Invisible[0x48] = 1;
Invisible[0x50] = 1;
Invisible[0x4B] = 1;
Invisible[0x4D] = 1;
}
void KeybHandler(ISRFrame_t *regs)
{
char status;
char code = 0;
// write EOI
IoWriteByteOnPort(0x20, 0x20);
status = IoReadByteFromPort(0x64);
if (status & 0x01) {
code = IoReadByteFromPort(0x60);
if(code < 0) code = 0;
}
KeybPrint((int)code);
IoSendEOItoPIC(0x21);
}
void IoCreateInputBuffer(void)
{
error_t rc;
rc = BOpenPureBuf(&BStdIn, BS_RDWR, 4 * KB);
if (rc) KeStartPanic("[Keyb] Couldn't create BStdIn");
//BEnableLineBuffering(BStdIn);
BStdIn->flusher = bemptybuf;
}
void IoEnableKeyb(void)
{
ulong flags = KePauseIRQs();
IdtRegisterIsr(KeybHandler, 0x21);
char readedInterruptConfig = IoReadByteFromPort(0x21);
IoWriteByteOnPort(0x21, 0xFD & readedInterruptConfig);
KeRestoreIRQs(flags);
IoEnableNMI();
ScanCodesInit();
IoCreateInputBuffer();
}
void IoChangeCodePage(char *CodePage)
{
ScanCodes = &CodePage[0];
}