driver/input, kernel: use a Condition for input

This commit is contained in:
Edgaru089 2021-11-11 02:17:02 +08:00
parent 3aafc2a032
commit 6434fe4b63
3 changed files with 37 additions and 11 deletions

View File

@ -1,14 +1,15 @@
#include "input.h" #include "input.h"
#include "consts.h"
#include "source.h" #include "source.h"
#include "internal.h" #include "internal.h"
#include "../../util/minmax.h" #include "../../util/minmax.h"
#include <stdint.h>
int __input_CursorX, __input_CursorY; smp_Condition *input_Condition;
int __input_DesktopWidth, __input_DesktopHeight; int __input_CursorX, __input_CursorY;
uint64_t __input_KeyMask[input_Key_Count / 64 + 1], __input_MouseMask[input_MouseButton_Count / 64 + 1]; int __input_DesktopWidth, __input_DesktopHeight;
uint64_t __input_KeyMask[input_Key_Count / 64 + 1], __input_MouseMask[input_MouseButton_Count / 64 + 1];
// FIXME you're supposed to disable interrupt/lock mutex accessing these variables // FIXME you're supposed to disable interrupt/lock mutex accessing these variables
@ -30,19 +31,38 @@ void input_DesktopSize(int *x, int *y) {
*y = __input_DesktopHeight; *y = __input_DesktopHeight;
} }
void input_source_PressKey(input_Key key) { __input_KeyMask[key / 64] |= (1ull << key % 64); } static inline void notify(void *data) {
void input_source_ReleaseKey(input_Key key) { __input_KeyMask[key / 64] &= ~(1ull << key % 64); } if (input_Condition)
smp_Condition_NotifyAll(input_Condition, data);
}
void input_source_PressMouse(input_MouseButton key) { __input_MouseMask[key / 64] |= (1ull << key & 64); } void input_source_PressKey(input_Key key) {
void input_source_ReleaseMouse(input_MouseButton key) { __input_MouseMask[key / 64] &= ~(1ull << key & 64); } __input_KeyMask[key / 64] |= (1ull << key % 64);
notify(NULL);
}
void input_source_ReleaseKey(input_Key key) {
__input_KeyMask[key / 64] &= ~(1ull << key % 64);
notify(NULL);
}
void input_source_PressMouse(input_MouseButton key) {
__input_MouseMask[key / 64] |= (1ull << key & 64);
notify(NULL);
}
void input_source_ReleaseMouse(input_MouseButton key) {
__input_MouseMask[key / 64] &= ~(1ull << key & 64);
notify(NULL);
}
void input_source_MoveMouse(int x, int y) { void input_source_MoveMouse(int x, int y) {
__input_CursorX = intminmax(__input_CursorX + x, 0, __input_DesktopWidth); __input_CursorX = intminmax(__input_CursorX + x, 0, __input_DesktopWidth);
__input_CursorY = intminmax(__input_CursorY + y, 0, __input_DesktopHeight); __input_CursorY = intminmax(__input_CursorY + y, 0, __input_DesktopHeight);
notify(NULL);
} }
void input_source_PositionMouse(int x, int y) { void input_source_PositionMouse(int x, int y) {
__input_CursorX = intminmax(x, 0, __input_DesktopWidth); __input_CursorX = intminmax(x, 0, __input_DesktopWidth);
__input_CursorY = intminmax(y, 0, __input_DesktopHeight); __input_CursorY = intminmax(y, 0, __input_DesktopHeight);
notify(NULL);
} }
void input_source_SetDesktopSize(int x, int y) { void input_source_SetDesktopSize(int x, int y) {

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../../smp/condiction.h"
#include <stdbool.h> #include <stdbool.h>
#include "consts.h" #include "consts.h"
@ -8,6 +9,9 @@ extern "C" {
#endif #endif
// If input_Condition is not NULL, it is notified on new input events.
extern smp_Condition *input_Condition;
// input_KeyPressed tells if a given key is present and pressed. // input_KeyPressed tells if a given key is present and pressed.
bool input_KeyPressed(input_Key key); bool input_KeyPressed(input_Key key);

View File

@ -11,7 +11,9 @@
#include "../interrupt/syscall.h" #include "../interrupt/syscall.h"
#include "../driver/irq/pic/pic.h" #include "../driver/irq/pic/pic.h"
#include "../driver/irq/pic/ps2/ps2.h" #include "../driver/irq/pic/ps2/ps2.h"
#include "../driver/input/input.h"
#include "../smp/kthread.h" #include "../smp/kthread.h"
#include "../smp/condiction.h"
#include "../execformat/pe/reloc.h" #include "../execformat/pe/reloc.h"
void execformat_pe_ReadSystemHeader(execformat_pe_PortableExecutable *pe); void execformat_pe_ReadSystemHeader(execformat_pe_PortableExecutable *pe);
@ -88,10 +90,10 @@ SYSV_ABI void kMain() {
tid = smp_thread_Start(kThreader, &args, SMP_NICENESS_DEFAULT); tid = smp_thread_Start(kThreader, &args, SMP_NICENESS_DEFAULT);
io_Printf("New thread, id=%d\n", tid); io_Printf("New thread, id=%d\n", tid);
for (;;) { input_Condition = smp_Condition_Create();
//asm volatile("hlt");
//io_WriteConsoleASCII("kMain: Interrupt hit\n"); for (;;) {
smp_Condition_Wait(input_Condition);
graphics_SwapBuffer(); graphics_SwapBuffer();
} }
} }