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

126 lines
4.0 KiB
C
Raw Normal View History

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-05-07 18:35:46 +02:00
// 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/>. //
//----------------------------------------------------------------------------//
2019-05-13 23:22:27 +02:00
#include <lib/buf.h>
#include <io/keyb.h>
2019-05-14 11:48:07 +02:00
#include <ke/idt.h>
2019-04-24 11:40:14 +02:00
2020-01-10 21:58:11 +01:00
#include "scan.c"
2019-04-24 11:40:14 +02:00
2020-01-16 17:44:03 +01:00
#define KEY(code) (table[2*(code)])
#define FLAGS(code) (table[2*(code)+1])
2020-01-10 21:58:11 +01:00
static bool capsLockActive = 0;
2020-01-16 17:44:03 +01:00
static bool altPressed = 0;
static bool shiftPressed = 0;
static bool controlPressed = 0;
2019-05-08 10:33:17 +02:00
2020-01-10 21:58:11 +01:00
void KeybPrint(uchar _code)
{
uint code = (uint)_code;
const uint *table =
2020-01-16 17:44:03 +01:00
(altPressed ? LeftAltScanCodes
: (controlPressed ? LeftControlScanCodes
: (shiftPressed ? LeftShiftScanCodes
2020-01-10 21:58:11 +01:00
: RegularScanCodes)));
2020-01-16 17:44:03 +01:00
if (capsLockActive && (FLAGS(code) & CAPSLOCK))
&& !(altPressed || controlPressed)) {
if (shiftPressed)
table = RegularScanCodes;
else
table = LeftShiftScanCodes;
}
2020-01-10 21:58:11 +01:00
2020-01-16 17:44:03 +01:00
uchar ch = KEY(code);
2020-01-10 21:58:11 +01:00
if (!ch) {
switch (code) {
2020-01-16 17:44:03 +01:00
case 0x38: altPressed = 1; break;
case 0xB8: altPressed = 0; break;
2020-01-10 21:58:11 +01:00
2020-01-16 17:44:03 +01:00
case 0x36: case 0x2A: shiftPressed = 1; break;
case 0xB6: case 0xAA: shiftPressed = 0; break;
2020-01-10 21:58:11 +01:00
2020-01-16 17:44:03 +01:00
case 0x1D: controlPressed = 1; break;
case 0x9D: controlPressed = 0; break;
2020-01-10 21:58:11 +01:00
case 0x3A: capsLockActive = !capsLockActive; break;
2019-05-08 15:09:16 +02:00
}
2020-01-10 21:58:11 +01:00
return;
}
bputc(BStdIn, ch);
2020-01-16 17:44:03 +01:00
if (code && (FLAGS(code) & INVISIBLE) == 0) {
2020-01-10 21:58:11 +01:00
bputc(BStdOut, table[2 * code]);
BStdOut->flusher(BStdOut);
2019-04-24 22:40:12 +02:00
}
}
2019-05-14 11:48:07 +02:00
static void KeybHandler(ISRFrame_t *regs)
2019-04-24 11:55:15 +02:00
{
2019-04-24 21:08:44 +02:00
char status;
2020-01-10 21:58:11 +01:00
uchar code = 0;
2019-04-24 11:55:15 +02:00
// write EOI
2019-04-24 18:14:23 +02:00
IoWriteByteOnPort(0x20, 0x20);
2019-04-24 11:55:15 +02:00
2019-04-24 21:08:44 +02:00
status = IoReadByteFromPort(0x64);
2019-04-24 11:55:15 +02:00
if (status & 0x01) {
2019-04-24 21:08:44 +02:00
code = IoReadByteFromPort(0x60);
2019-04-24 11:55:15 +02:00
}
2020-01-10 21:58:11 +01:00
KeybPrint(code);
2019-05-14 11:48:07 +02:00
KeSendEOItoPIC(0x21);
2019-04-24 11:55:15 +02:00
}
2019-04-27 00:04:27 +02:00
2019-05-07 22:32:17 +02:00
void IoCreateInputBuffer(void)
{
error_t rc;
rc = BOpenPureBuf(&BStdIn, BS_RDWR, 4 * KB);
2019-05-08 10:33:17 +02:00
if (rc) KeStartPanic("[Keyb] Couldn't create BStdIn");
2019-05-07 22:32:17 +02:00
2019-05-08 15:09:16 +02:00
//BEnableLineBuffering(BStdIn);
BStdIn->flusher = bemptybuf;
2019-05-07 22:32:17 +02:00
}
2019-04-27 00:04:27 +02:00
void IoEnableKeyb(void)
{
ulong flags = KePauseIRQs();
2019-05-07 22:32:17 +02:00
2019-05-14 11:48:07 +02:00
KeRegisterISR(KeybHandler, 0x21);
2019-04-27 00:04:27 +02:00
char readedInterruptConfig = IoReadByteFromPort(0x21);
IoWriteByteOnPort(0x21, 0xFD & readedInterruptConfig);
2019-05-07 22:32:17 +02:00
2019-04-27 00:04:27 +02:00
KeRestoreIRQs(flags);
2019-05-07 22:32:17 +02:00
2019-05-14 11:48:07 +02:00
KeEnableNMI();
2019-05-07 22:32:17 +02:00
IoCreateInputBuffer();
2019-04-27 00:04:27 +02:00
}
2019-05-08 10:33:17 +02:00