#include "stdio.h"
#include "keyboard.h"
#include "8042.h"
#include "asm.h"
BOOL IsLShift = FALSE;
BOOL IsRShift = FALSE;
BOOL IsCapslock = FALSE;
BOOL IsAlt = FALSE;
BOOL IsCtrl = FALSE;
BOOL IsNumLock = FALSE;
KeyboardInfo kbd_scancode[] =
{
{0x00, 0x00, 0x00}, // ESC
{'1','!', 0x00, 0x00},
{'2','@', 0x00, 0x00},
{'3','#', 0x00, 0x00},
{'4','$', 0x00, 0x00},
{'5','%', 0x00, 0x00},
{'6','^', 0x00, 0x00},
{'7','&', 0x00, 0x00},
{'8','*', 0x00, 0x00},
{'9','(', 0x00, 0x00},
{'0',')', 0x00, 0x00},
{'-','_', 0x00, 0x00},
{'=','+', 0x00, 0x00},
{0x00,0x00, 0x00, 0x00}, // BACKSPACE
{0x00,0x00, 0x00, 0x00}, // TAB
{'q','Q', 0x00, 0x00},
{'w','W', 0x00, 0x00},
{'e','E', 0x00, 0x00},
{'r','R', 0x00, 0x00},
{'t','T', 0x00, 0x00},
{'y','Y', 0x00, 0x00},
{'u','U', 0x00, 0x00},
{'i','I', 0x00, 0x00},
{'o','O', 0x00, 0x00},
{'p','P', 0x00, 0x00},
{'[','{', 0x00, 0x00},
{']','}', 0x00, 0x00},
{0x00,0x00, 0x00, 0x00}, // ENTER
{0x00,0x00, 0x00, 0x00}, // CTRL
{'a','A', 0x00, 0x00},
{'s','S', 0x00, 0x00},
{'d','D', 0x00, 0x00},
{'f','F', 0x00, 0x00},
{'g','G', 0x00, 0x00},
{'h','H', 0x00, 0x00},
{'j','J', 0x00, 0x00},
{'k','K', 0x00, 0x00},
{'l','L', 0x00, 0x00},
{';',':', 0x00, 0x00},
{'\'','"', 0x00, 0x00},
{'`','~', 0x00, 0x00},
{0x00,0x00, 0x00, 0x00}, // Left Shift
{'\\','|', 0x00, 0x00},
{'z','Z', 0x00, 0x00},
{'x','X', 0x00, 0x00},
{'c','C', 0x00, 0x00},
{'v','V', 0x00, 0x00},
{'b','B', 0x00, 0x00},
{'n','N', 0x00, 0x00},
{'m','M', 0x00, 0x00},
{',','<', 0x00, 0x00},
{'.','>', 0x00, 0x00},
{'/','?', 0x00, 0x00},
{0x00,0x00, 0x00, 0x00}, // right shift
{0x00,0x00, 0x00, 0x00}, // PrtSc
{0x00,0x00, 0x00, 0x00}, // ALT
{' ',' ', 0x00, 0x00}, // space
{0x00,0x00, 0x00, 0x00}, // caps
{0x00,0x00, 0x00, 0x00}, // F1
{0x00,0x00, 0x00, 0x00}, // F2
{0x00,0x00, 0x00, 0x00}, // F3
{0x00,0x00, 0x00, 0x00}, // F4
{0x00,0x00, 0x00, 0x00}, // F5
{0x00,0x00, 0x00, 0x00}, // F6
{0x00,0x00, 0x00, 0x00}, // F7
{0x00,0x00, 0x00, 0x00}, // F8
{0x00,0x00, 0x00, 0x00}, // F9
{0x00,0x00, 0x00, 0x00}, // F10
{0x00,0x00, 0x00, 0x00}, // number lock
{0x00,0x00, 0x00, 0x00}, // scroll
{0x00,0x00, 0x00, 0x00}, // home { numlook = 7 }
{0x00,0x00, 0x00, 0x00}, // up { numlook = 8 }
{0x00,0x00, 0x00, 0x00}, // pgup { numlook = 9 }
{'-','-', 0x00, 0x00}, // -
{0x00,0x00, 0x00, 0x00}, // left { numlook = 4 }
{0x00,0x00, 0x00, 0x00}, // center { numlook = 5 }
{0x00,0x00, 0x00, 0x00}, // right { numlook = 6 }
{'+','*', 0x00, 0x00},
{0x00,0x00, 0x00, 0x00}, // end { numlook = 1 }
{0x00,0x00, 0x00, 0x00}, // down { numlook = 2 }
{0x00,0x00, 0x00, 0x00}, // pgdn { numlook = 3 }
{0x00,0x00, 0x00, 0x00}, // ins { numlook = 0 }
{0x00,0x00, 0x00, 0x00}, // del { numlook = . }
{'/','/', 0x00, 0x00}, // /
{0x00,0x00, 0x00, 0x00}, // enter
{0x00,0x00, 0x57, 0xD7}, // F11
{0x00,0x00, 0x58, 0xD8}, // F12
{0x00,0x00, 0x52, 0xD2}, // ins
{0x00,0x00, 0x53, 0xD3}, // del
{0x00,0x00, 0x47, 0xC7}, // home
{0x00,0x00, 0x4F, 0xCF}, // end
{0x00,0x00, 0x49, 0xC9}, // pgup
{0x00,0x00, 0x51, 0xD1}, // pgdn
{0x00,0x00, 0x4B, 0xCB}, // left
{0x00,0x00, 0x4D, 0xCD}, // right
{0x00,0x00, 0x48, 0xC8}, // up
{0x00,0x00, 0x50, 0xD0}, // down
{0x00,0x00, 0x38, 0xB8}, // right alt
{0x00,0x00, 0x1D, 0x9D}, // right ctrl
{0x11,0x00, 0x00, 0x00}}; // pause // 99 index
void Init_kbd()
{
int i = 0;
for(i = 0; i < 0x54; i++ )
{
kbd_scancode[i].Press = i+1;
kbd_scancode[i].Release = 0x81 + i;
}
Write_port_byte(0x21, 0xFD);
}
int GetScanCodeInfo(BYTE code, KeyboardInfo *pkbdInfo)
{
int i;
if( code > 0xD9 ) // Unknown code
return UNKNOWN_SCANCODE;
for( i = 0; i < 0x64; i++ )
{
if( kbd_scancode[i].Press == code )
{
*pkbdInfo = kbd_scancode[i];
return PRESS_SCANCODE;
}
if(kbd_scancode[i].Release == code)
{
*pkbdInfo = kbd_scancode[i];
return RELEASE_SCANCODE;
}
}
return UNKNOWN_SCANCODE;
}
void Kbd_ScanCode()
{
BYTE scancode;
int scancode_type;
KeyboardInfo Info;
scancode = GetKeyboardScanCode();
scancode_type = GetScanCodeInfo(scancode, &Info);
if(scancode_type == UNKNOWN_SCANCODE )
{
return;
}
if( scancode_type == RELEASE_SCANCODE )
{
switch(Info.Press)
{
case KEY_LSHIFT:
IsLShift = FALSE;
case KEY_RSHIFT:
IsRShift = FALSE;
break;
case KEY_ALT:
IsAlt = FALSE;
break;
case KEY_CTRL:
IsCtrl = FALSE;
break;
}
return;
}
if( scancode_type == PRESS_SCANCODE )
{
switch(Info.Press)
{
case KEY_LSHIFT:
IsLShift = TRUE;
break;
case KEY_RSHIFT:
IsRShift = TRUE;
break;
case KEY_ALT:
IsAlt = TRUE;
break;
case KEY_CTRL:
IsCtrl = TRUE;
break;
case KEY_CAPSLOCK:
IsCapslock = !IsCapslock;
break;
case KEY_NUMBERLOCK:
IsNumLock = !IsNumLock;
break;
case KEY_ENTER:
kprint("\n");
return;
}
if( IsCtrl || IsAlt )
{
// function key process
}
if( IsNumLock )
{
switch( Info.Press )
{
case 0x47:
putc('7');
return;
case 0x48:
putc('8');
return;
case 0x49:
putc('9');
return;
case 0x4B:
putc('4');
return;
case 0x4C:
putc('5');
return;
case 0x4D:
putc('6');
return;
case 0x4F:
putc('1');
return;
case 0x50:
putc('2');
return;
case 0x51:
putc('3');
return;
case 0x52:
putc('0');
return;
case 0x53:
putc('.');
return;
}
}
if( IsCapslock )
{
if( Info.Lowercase >= 'a' && Info.Lowercase <= 'z')
{
if( IsLShift || IsRShift )
putc(Info.Lowercase);
else
putc(Info.Uppercase);
return;
}
}
if( IsLShift || IsRShift )
{
if(Info.Lowercase != 0x00 )
putc(Info.Uppercase);
else
{
// function key process
// otherwise
}
return;
}
if( Info.Lowercase != 0x00 )
putc(Info.Lowercase);
}
}