keyboard.c (2703B)
1 // This file contains general functions that are used as an interface 2 // to other parts of the kernel and to user space programs, and are used 3 // by all keyboard drivers (e.g. PS2 and others) the same 4 5 #include "keyboard.h" 6 7 static int keyboard_callbacks_pos = 0; 8 static void (*keyboard_callbacks[MAX_CALLBACK_COUNT])(struct keyboard_event); 9 10 // TODO: As long as we cannot allocate memory dynamically, use fixed one 11 static struct keyboard_event kbd_event; 12 13 static bool shift_active = false; 14 15 static bool keycode_is_printable[256] = { 16 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 17 18 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 19 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 20 1, 1, 1, 1, 1, 1, 21 22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23 24 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 25 26 0 ,0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 27 28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 29 30 0 31 }; 32 33 static char keycode_to_char[256] = { 34 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 35 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o' ,'p', 'a', 's', 'd', 'f', 36 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 37 38 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 39 40 ',', '.', '/', '\\', '\'', ';', '-', '=', ' ', ' ', ' ', '\t', ' ', 41 42 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ']', '[', ' ', ' ', ' ', 43 44 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '*', '+', '.', 45 46 ' ' 47 }; 48 49 static char keycode_to_char_shift[256] = { 50 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 51 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O' ,'P', 'A', 'S', 'D', 'F', 52 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 53 54 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 55 56 '<', '>', '?', '|', '"', ':', '_', '+', ' ', ' ', ' ', '\t', ' ', 57 58 ' ', ' ', ' ', ' ', ' ', ' ', ' ', '}', '{', ' ', ' ', ' ', 59 60 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '*', '+', '.', 61 62 ' ' 63 }; 64 65 void keyboard_add_callback(void (*callback)(struct keyboard_event)) { 66 keyboard_callbacks[keyboard_callbacks_pos] = callback; 67 keyboard_callbacks_pos++; 68 } 69 70 void keyboard_fire_event(enum keyboard_key kbdkey, bool released) { 71 if (kbdkey == KEY_LSHIFT || kbdkey == KEY_RSHIFT) { 72 shift_active = !released; 73 } 74 75 kbd_event.key = kbdkey; 76 kbd_event.released = released; 77 78 // TODO: We should make sure that they always match? 79 if (kbdkey < 256) { 80 kbd_event.printable = keycode_is_printable[kbdkey]; 81 82 if (shift_active) { 83 kbd_event.ascii = keycode_to_char_shift[kbdkey]; 84 } else { 85 kbd_event.ascii = keycode_to_char[kbdkey]; 86 } 87 } 88 89 for (int i = 0; i < keyboard_callbacks_pos; i++) { 90 keyboard_callbacks[i](kbd_event); 91 } 92 }