From e982d726134d7872d75e7736cbafbc4deb40659e Mon Sep 17 00:00:00 2001 From: xiaoyi1212 Date: Mon, 15 Apr 2024 22:53:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=AE=E7=9B=98=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- driver/keyboard.c | 56 +++++++++++++++++++++++++++++++++------------- include/keyboard.h | 8 +++++++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/driver/keyboard.c b/driver/keyboard.c index 61ea383..aacfae9 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -6,6 +6,7 @@ static KEY_STATUS *key_status; Queue *key_char_queue; +struct key_listener* head_listener; unsigned char keyboard_map[128] = { @@ -87,21 +88,7 @@ unsigned char shift_keyboard_map[128] = 0, /* All other keys are undefined */ }; -void init_keyboard(){ - key_status = (KEY_STATUS*) alloc(sizeof(KEY_STATUS)); - key_status->is_shift = 0; - - key_char_queue = create_queue(); - register_interrupt_handler(0x21,handle_keyboard_input); -} - -int handle_keyboard_input(){ - unsigned char status = read_port(KEYBOARD_STATUS_PORT); - uint32_t key = read_port(KEYBOARD_DATA_PORT); - int release = key & 0xb10000000; - char c = key_status->is_shift ? shift_keyboard_map[(unsigned char )key] : keyboard_map[(unsigned char )key]; - - +static void default_handle(uint32_t key,int release,char c){ if(!release) { if(c == -1) { key_status->is_shift = 1; @@ -126,6 +113,45 @@ int handle_keyboard_input(){ return 0; } } +} + +void init_keyboard(){ + key_status = (KEY_STATUS*) alloc(sizeof(KEY_STATUS)); + key_status->is_shift = 0; + + key_char_queue = create_queue(); + head_listener = (struct key_listener*) kmalloc(sizeof(struct key_listener)); + head_listener->func = default_handle; + + register_interrupt_handler(0x21,handle_keyboard_input); +} + +int handle_keyboard_input(){ + unsigned char status = read_port(KEYBOARD_STATUS_PORT); + uint32_t key = read_port(KEYBOARD_DATA_PORT); + int release = key & 0xb10000000; + char c = key_status->is_shift ? shift_keyboard_map[(unsigned char )key] : keyboard_map[(unsigned char )key]; + + struct key_listener* h = head_listener; + while (1){ + h->func(key,release,c); + h = h->next; + if(h == NULL) break; + } return 0; } + +void add_listener(struct key_listener* listener){ + if(listener == NULL) return; + + struct key_listener* h = head_listener,*buf = NULL; + while (1){ + buf = h; + h = h->next; + if(h == NULL){ + buf->next = listener; + break; + } + } +} \ No newline at end of file diff --git a/include/keyboard.h b/include/keyboard.h index 8cc70b1..2a3b576 100644 --- a/include/keyboard.h +++ b/include/keyboard.h @@ -4,6 +4,8 @@ #define KEYBOARD_DATA_PORT 0x60 #define KEYBOARD_STATUS_PORT 0x64 +#include + typedef struct { int is_shift; int is_ctrl; @@ -15,7 +17,13 @@ typedef struct { unsigned char *shift_keyboard_map[128]; }KEY_MAP; +typedef struct key_listener{ + void (*func)(uint32_t key,int release,char c); + struct key_listener *next; +}kl_t; + void init_keyboard(); int handle_keyboard_input(); +void add_listener(struct key_listener* listener); #endif //CRASHPOWEROS_KEYBOARD_H