增加键盘监听回调函数
This commit is contained in:
parent
66e698432e
commit
e982d72613
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
static KEY_STATUS *key_status;
|
static KEY_STATUS *key_status;
|
||||||
Queue *key_char_queue;
|
Queue *key_char_queue;
|
||||||
|
struct key_listener* head_listener;
|
||||||
|
|
||||||
unsigned char keyboard_map[128] =
|
unsigned char keyboard_map[128] =
|
||||||
{
|
{
|
||||||
|
@ -87,21 +88,7 @@ unsigned char shift_keyboard_map[128] =
|
||||||
0, /* All other keys are undefined */
|
0, /* All other keys are undefined */
|
||||||
};
|
};
|
||||||
|
|
||||||
void init_keyboard(){
|
static void default_handle(uint32_t key,int release,char c){
|
||||||
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];
|
|
||||||
|
|
||||||
|
|
||||||
if(!release) {
|
if(!release) {
|
||||||
if(c == -1) {
|
if(c == -1) {
|
||||||
key_status->is_shift = 1;
|
key_status->is_shift = 1;
|
||||||
|
@ -126,6 +113,45 @@ int handle_keyboard_input(){
|
||||||
return 0;
|
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;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,8 @@
|
||||||
#define KEYBOARD_DATA_PORT 0x60
|
#define KEYBOARD_DATA_PORT 0x60
|
||||||
#define KEYBOARD_STATUS_PORT 0x64
|
#define KEYBOARD_STATUS_PORT 0x64
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int is_shift;
|
int is_shift;
|
||||||
int is_ctrl;
|
int is_ctrl;
|
||||||
|
@ -15,7 +17,13 @@ typedef struct {
|
||||||
unsigned char *shift_keyboard_map[128];
|
unsigned char *shift_keyboard_map[128];
|
||||||
}KEY_MAP;
|
}KEY_MAP;
|
||||||
|
|
||||||
|
typedef struct key_listener{
|
||||||
|
void (*func)(uint32_t key,int release,char c);
|
||||||
|
struct key_listener *next;
|
||||||
|
}kl_t;
|
||||||
|
|
||||||
void init_keyboard();
|
void init_keyboard();
|
||||||
int handle_keyboard_input();
|
int handle_keyboard_input();
|
||||||
|
void add_listener(struct key_listener* listener);
|
||||||
|
|
||||||
#endif //CRASHPOWEROS_KEYBOARD_H
|
#endif //CRASHPOWEROS_KEYBOARD_H
|
||||||
|
|
Loading…
Reference in New Issue