1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include "keyboard.h" #include "print.h" #include "io.h" #include "interrupt.h" #include "global.h" #include "stdint.h"
#define KBD_BUF_PORT 0X60
#define KBD_BUF_PORT 0X60
#define esc '\033' #define delete '\0177' #define enter '\r' #define tab '\t' #define backspace '\b'
#define char_invisible 0 #define ctrl_l_char char_invisible #define ctrl_r_char char_invisible #define shift_l_char char_invisible #define shift_r_char char_invisible #define alt_l_char char_invisible #define alt_r_char char_invisible #define caps_lock_char char_invisible
#define shift_l_make 0x2a #define shift_r_make 0x36 #define alt_l_make 0x38 #define alt_r_make 0xe038 #define alt_r_break 0xe0b8 #define ctrl_l_make 0x1d #define ctrl_r_make 0xe01d #define ctrl_r_break 0xe09d #define caps_lock_make 0x3a
bool ctrl_status = false,shift_status = false,alt_status = false,caps_lock_status = false,ext_scancode = false;
char keymap[][2] = { {0, 0}, {esc, esc}, {'1', '!'}, {'2', '@'}, {'3', '#'}, {'4', '$'}, {'5', '%'}, {'6', '^'}, {'7', '&'}, {'8', '*'}, {'9', '('}, {'0', ')'}, {'-', '_'}, {'=', '+'}, {backspace, backspace}, {tab, tab}, {'q', 'Q'}, {'w', 'W'}, {'e', 'E'}, {'r', 'R'}, {'t', 'T'}, {'y', 'Y'}, {'u', 'U'}, {'i', 'I'}, {'o', 'O'}, {'p', 'P'}, {'[', '{'}, {']', '}'}, {enter, enter}, {ctrl_l_char, ctrl_l_char}, {'a', 'A'}, {'s', 'S'}, {'d', 'D'}, {'f', 'F'}, {'g', 'G'}, {'h', 'H'}, {'j', 'J'}, {'k', 'K'}, {'l', 'L'}, {';', ':'}, {'\'', '"'}, {'`', '~'}, {shift_l_char, shift_l_char}, {'\\', '|'}, {'z', 'Z'}, {'x', 'X'}, {'c', 'C'}, {'v', 'V'}, {'b', 'B'}, {'n', 'N'}, {'m', 'M'}, {',', '<'}, {'.', '>'}, {'/', '?'}, {shift_r_char, shift_r_char}, {'*', '*'}, {alt_l_char, alt_l_char}, {' ', ' '}, {caps_lock_char, caps_lock_char} };
void keyboard_init() { put_str("keyboard init start\n"); register_handler(0x21,intr_keyboard_handler); put_str("keyboard init done\n"); }
void intr_keyboard_handler(void) { bool ctrl_down_last = ctrl_status; bool shift_down_last = shift_status; bool caps_lock_last = caps_lock_status; bool break_code; uint16_t scancode = inb(KBD_BUF_PORT); if(scancode == 0xe0) { ext_scancode = true; return; } break_code = ((scancode & 0x0080) != 0); if(break_code) { uint16_t make_code = (scancode &= 0xff7f); if(make_code == ctrl_l_make || make_code == ctrl_r_make) ctrl_status = false; else if(make_code == shift_l_make || make_code == shift_r_make) shift_status = false; else if(make_code == alt_l_make || make_code == alt_r_make) alt_status = false; return; } else if((scancode > 0x00 && scancode < 0x3b) || (scancode == alt_r_make) || (scancode == ctrl_r_make)) { bool shift = false; if((scancode < 0x0e) || (scancode == 0x29) || (scancode == 0x1a) || \ (scancode == 0x1b) || (scancode == 0x2b) || (scancode == 0x27) || \ (scancode == 0x28) || (scancode == 0x33) || (scancode == 0x34) || \ (scancode == 0x35)) { if(shift_down_last) shift = true; } else { if(shift_down_last && caps_lock_last) shift = false; else if(shift_down_last || caps_lock_last) shift = true; else shift = false; } uint8_t index = (scancode & 0x00ff); char cur_char = keymap[index][shift]; if(cur_char) { put_char(cur_char); return; } if(scancode == ctrl_l_make || scancode == ctrl_r_make) ctrl_status = true; else if(scancode == shift_l_make || scancode == shift_r_make) shift_status = true; else if(scancode == alt_l_make || scancode == alt_r_make) alt_status = true; else if(scancode == caps_lock_make) caps_lock_status = !caps_lock_status; else put_str("unknown key\n"); } return; }
|