driver/input: most of the input code

This commit is contained in:
2021-10-28 18:20:02 +08:00
parent 4da91139a7
commit 94ab9ffb41
9 changed files with 303 additions and 8 deletions

163
driver/input/consts.h Normal file
View File

@ -0,0 +1,163 @@
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
// Key codes of the IBM PC (American) keyboard
typedef enum {
input_Key_Unknown = -1, // Unhandled key
input_Key_A = 0, // The A key
input_Key_B, // The B key
input_Key_C, // The C key
input_Key_D, // The D key
input_Key_E, // The E key
input_Key_F, // The F key
input_Key_G, // The G key
input_Key_H, // The H key
input_Key_I, // The I key
input_Key_J, // The J key
input_Key_K, // The K key
input_Key_L, // The L key
input_Key_M, // The M key
input_Key_N, // The N key
input_Key_O, // The O key
input_Key_P, // The P key
input_Key_Q, // The Q key
input_Key_R, // The R key
input_Key_S, // The S key
input_Key_T, // The T key
input_Key_U, // The U key
input_Key_V, // The V key
input_Key_W, // The W key
input_Key_X, // The X key
input_Key_Y, // The Y key
input_Key_Z, // The Z key
input_Key_Num0, // The 0 key
input_Key_Num1, // The 1 key
input_Key_Num2, // The 2 key
input_Key_Num3, // The 3 key
input_Key_Num4, // The 4 key
input_Key_Num5, // The 5 key
input_Key_Num6, // The 6 key
input_Key_Num7, // The 7 key
input_Key_Num8, // The 8 key
input_Key_Num9, // The 9 key
input_Key_Escape, // The Escape key
input_Key_LControl, // The left Control key
input_Key_LShift, // The left Shift key
input_Key_LAlt, // The left Alt key
input_Key_LSuper, // The left Super (Windows) key
input_Key_RControl, // The right Control key
input_Key_RShift, // The right Shift key
input_Key_RAlt, // The right Alt key
input_Key_RSuper, // The right Super (Windows) key
input_Key_Menu, // The Menu key
input_Key_LBracket, // The [ key
input_Key_RBracket, // The ] key
input_Key_Semicolon, // The ; key
input_Key_Comma, // The , key
input_Key_Period, // The . key
input_Key_Quote, // The ' key
input_Key_Slash, // The / key
input_Key_Backslash, // The \ key
input_Key_Tilde, // The ~ key
input_Key_Equal, // The = key
input_Key_Hyphen, // The - key (hyphen)
input_Key_Space, // The Space key
input_Key_Enter, // The Enter/Return keys
input_Key_Backspace, // The Backspace key
input_Key_Tab, // The Tabulation key
input_Key_PageUp, // The Page up key
input_Key_PageDown, // The Page down key
input_Key_End, // The End key
input_Key_Home, // The Home key
input_Key_Insert, // The Insert key
input_Key_Delete, // The Delete key
input_Key_Add, // The + key
input_Key_Subtract, // The - key (minus, usually from numpad)
input_Key_Multiply, // The * key
input_Key_Divide, // The / key
input_Key_Left, // Left arrow
input_Key_Right, // Right arrow
input_Key_Up, // Up arrow
input_Key_Down, // Down arrow
input_Key_Numpad0, // The numpad 0 key
input_Key_Numpad1, // The numpad 1 key
input_Key_Numpad2, // The numpad 2 key
input_Key_Numpad3, // The numpad 3 key
input_Key_Numpad4, // The numpad 4 key
input_Key_Numpad5, // The numpad 5 key
input_Key_Numpad6, // The numpad 6 key
input_Key_Numpad7, // The numpad 7 key
input_Key_Numpad8, // The numpad 8 key
input_Key_Numpad9, // The numpad 9 key
input_Key_F1, // The F1 key
input_Key_F2, // The F2 key
input_Key_F3, // The F3 key
input_Key_F4, // The F4 key
input_Key_F5, // The F5 key
input_Key_F6, // The F6 key
input_Key_F7, // The F7 key
input_Key_F8, // The F8 key
input_Key_F9, // The F9 key
input_Key_F10, // The F10 key
input_Key_F11, // The F11 key
input_Key_F12, // The F12 key
input_Key_F13, // The F13 key
input_Key_F14, // The F14 key
input_Key_F15, // The F15 key
input_Key_Pause, // The Pause key
input_Key_Count, // Keep last - the total number of keyboard keys
} input_Key;
typedef enum {
input_MouseButton_Left, // Left mouse button
input_MouseButton_Right, // Right mouse button
input_MouseButton_Middle, // Middle (scrollwheel) button
input_MouseButton_4, // 4th mouse button
input_MouseButton_5, // 5th mouse button
input_MouseButton_Count // Keep last - the number of mouse buttons
} input_MouseButton;
typedef enum {
input_EventType_MouseMoved, // Mouse has been moved (data event.MouseMove)
input_EventType_MouseButtonPressed, // Mouse button has been pressed (data event.MouseButton)
input_EventType_MouseButtonReleased, // Mouse button has been released (data event.MouseButton)
input_EventType_KeyPressed, // Keyboard key has been pressed (data event.Key)
input_EventType_KeyReleased, // Keyboard key has been released (data event.Key)
input_EventType_Count // Number of the event types
} input_EventType;
typedef struct {
input_EventType Type;
union {
struct {
int X, Y; // New position of the mouse
} MouseMove;
struct {
input_MouseButton Button; // Button
int X, Y; // Position of the mouse
} MouseButton;
struct {
input_Key Key; // Key code
bool Control, Shift, Alt, Super; // Is the modifier keys pressed?
} Key;
};
} input_Event;
#ifdef __cplusplus
}
#endif

53
driver/input/input.c Normal file
View File

@ -0,0 +1,53 @@
#include "input.h"
#include "source.h"
#include "internal.h"
#include "../../util/minmax.h"
#include <stdint.h>
int __input_CursorX, __input_CursorY;
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
bool input_KeyPressed(input_Key key) {
return __input_KeyMask[key / 64] & (1ull << key % 64);
}
bool input_MousePressed(input_MouseButton key) {
return __input_MouseMask[key / 64] & (1ull << key & 64);
}
void input_MousePosition(int *x, int *y) {
*x = __input_CursorX;
*y = __input_CursorY;
}
void input_DesktopSize(int *x, int *y) {
*x = __input_DesktopWidth;
*y = __input_DesktopHeight;
}
void input_source_PressKey(input_Key key) { __input_KeyMask[key / 64] |= (1ull << key % 64); }
void input_source_ReleaseKey(input_Key key) { __input_KeyMask[key / 64] &= ~(1ull << key % 64); }
void input_source_PressMouse(input_MouseButton key) { __input_MouseMask[key / 64] |= (1ull << key & 64); }
void input_source_ReleaseMouse(input_MouseButton key) { __input_MouseMask[key / 64] &= ~(1ull << key & 64); }
void input_source_MoveMouse(int x, int y) {
__input_CursorX = intminmax(__input_CursorX + x, 0, __input_DesktopWidth);
__input_CursorY = intminmax(__input_CursorY + y, 0, __input_DesktopHeight);
}
void input_source_PositionMouse(int x, int y) {
__input_CursorX = intminmax(x, 0, __input_DesktopWidth);
__input_CursorY = intminmax(y, 0, __input_DesktopHeight);
}
void input_source_SetDesktopSize(int x, int y) {
__input_DesktopWidth = x;
__input_DesktopHeight = y;
__input_CursorX = intminmax(__input_CursorX, 0, __input_DesktopWidth);
__input_CursorY = intminmax(__input_CursorY, 0, __input_DesktopHeight);
}

29
driver/input/input.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <stdbool.h>
#include "consts.h"
#ifdef __cplusplus
extern "C" {
#endif
// input_KeyPressed tells if a given key is present and pressed.
bool input_KeyPressed(input_Key key);
// input_MousePressed tells if a given mouse button is pressed.
bool input_MousePressed(input_MouseButton key);
// input_MouseButton returns the mouse position.
void input_MousePosition(int *x, int *y);
// input_DesktopSize returns the size of the rectangular area to which
// the mouse cursor is bounded.
//
// Don't use this to do anything not related to mouse position, e.g., get the screen size.
void input_DesktopSize(int *x, int *y);
#ifdef __cplusplus
}
#endif

9
driver/input/internal.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "consts.h"
#include "stdint.h"
extern int __input_CursorX, __input_CursorY;
extern int __input_DesktopWidth, __input_DesktopHeight;
extern uint64_t __input_KeyMask[input_Key_Count / 64 + 1], __input_MouseMask[input_MouseButton_Count / 64 + 1]; // bitsets for keys and mouse buttons

29
driver/input/source.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
// This file is intended only for input source devices like a PS/2 driver.
#include <stdbool.h>
#include "consts.h"
#ifdef __cplusplus
extern "C" {
#endif
void input_source_PressKey(input_Key key);
void input_source_ReleaseKey(input_Key key);
void input_source_PressMouse(input_MouseButton key);
void input_source_ReleaseMouse(input_MouseButton key);
void input_source_MoveMouse(int x, int y); // Moves mouse cursor by a relative amount
void input_source_PositionMouse(int x, int y); // Moves mouse cursor to an absolute position
// input_source_SetDesktopSize sets the size of the rectangular area to which
// the mouse cursor is bounded.
void input_source_SetDesktopSize(int x, int y);
#ifdef __cplusplus
}
#endif