The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
keyboard.h
Go to the documentation of this file.
1#pragma once
2#include "common/stdint.h"
3
4/**
5 * keyboard key state enumeration
6 * KEY_RELEASED: the key is not pressed
7 * KEY_PRESSED: the key is pressed
8 */
9typedef enum
10{
14
15/**
16 * special key codes for keys that cannot be represented as a plain char.
17 * produced by 0xE0-prefixed PS/2 scan code sequences.
18 */
32
33/**
34 * event type tag for keyboard_event_t.
35 * KEY_EVENT_CHAR: the event carries a printable/control character in .character.
36 * KEY_EVENT_SPECIAL: the event carries a special key code in .special.
37 */
38typedef enum
39{
42 KEY_EVENT_RAW = 2, // a key went down or up (keyboard_set_raw_events)
44
45/**
46 * a single keyboard event returned by keyboard_read_event().
47 * check .type before reading either union member.
48 */
49typedef struct
50{
52 union
53 {
54 char character; // valid when type == KEY_EVENT_CHAR
55 keyboard_special_key_t special; // valid when type == KEY_EVENT_SPECIAL
56 struct
57 {
58 uint8_t code; // set 1 scan code; bit 7 set for an 0xE0 key
59 uint8_t pressed; // 1: the key went down, 0: it went up
60 } raw; // valid when type == KEY_EVENT_RAW
61 };
63
64/**
65 * initialize the keyboard driver and register the keyboard interrupt handler.
66 * should be called after the IRQ subsystem is initialized.
67 */
68void keyboard_initialize(void);
69
70/**
71 * decode one byte of PS/2 scan code set 1 (0xE0 starts a two-byte code,
72 * bit 7 marks a release). the PS/2 interrupt calls it, and the USB
73 * keyboard driver calls it with codes made from its reports. call with
74 * interrupts off.
75 * @param scancode the byte.
76 */
78
79/**
80 * report every key press and release as a KEY_EVENT_RAW event (also the
81 * modifier keys), and no character or special events, or go back to the
82 * normal events. for games, which need key releases and the positions of
83 * the keys rather than characters. the terminal turns this on with the
84 * XKEYRAW termios flag.
85 */
86void keyboard_set_raw_events(bool on);
87
88/**
89 * check if at least one event is available in the event buffer. only a
90 * hint: another reader may take the event first, so callers that then
91 * read must use a read function that copes with an empty buffer.
92 * @return 1 if an event is available, 0 otherwise.
93 */
94int keyboard_available(void);
95
96/**
97 * read the next character from the event buffer without blocking.
98 * special key events (arrow keys, etc.) are discarded; only KEY_EVENT_CHAR
99 * events are returned here. callers that need special keys should use
100 * keyboard_read_event() instead.
101 * @return the next character, or 0 if no character event is available.
102 */
103char keyboard_getchar(void);
104
105/**
106 * read the next character from the event buffer, blocking until one arrives.
107 * special key events are discarded the same way as keyboard_getchar().
108 * @return the next character (0-255), or -1 if a signal for the calling
109 * process ended the wait.
110 */
111int keyboard_read(void);
112
113/**
114 * read the next keyboard event from the event buffer without blocking.
115 * @param event_out output structure filled with the event type and value.
116 * @return 1 if an event was read, 0 if the buffer was empty.
117 */
119
120/**
121 * read the next keyboard event from the event buffer, blocking until one arrives.
122 * the calling process sleeps until the keyboard IRQ wakes it; it does not
123 * spin. safe with several readers: each event goes to exactly one of them.
124 * @param event_out output structure filled with the event type and value.
125 * @return true with an event, false if a signal for the calling process
126 * ended the wait.
127 */
129
130/**
131 * a function that sees every key event before it is buffered (in the
132 * keyboard interrupt). it returns true to take the event out of the input:
133 * the terminal uses it for Ctrl+C and the other signal keys.
134 */
135typedef bool (*keyboard_filter_fn)(const keyboard_event_t *event);
136
137/**
138 * set the filter (NULL: none).
139 */
141
142/**
143 * drop every buffered event (input typed but not read yet).
144 */
145void keyboard_flush(void);
146
147/**
148 * return the number of events dropped due to a full buffer since boot.
149 * @return total number of dropped events.
150 */
bool keyboard_wait_event(keyboard_event_t *event_out)
read the next keyboard event from the event buffer, blocking until one arrives.
Definition keyboard.c:440
void keyboard_set_raw_events(bool on)
report every key press and release as a KEY_EVENT_RAW event (also the modifier keys),...
Definition keyboard.c:106
char keyboard_getchar(void)
read the next character from the event buffer without blocking.
Definition keyboard.c:472
int keyboard_read_event(keyboard_event_t *event_out)
read the next keyboard event from the event buffer without blocking.
Definition keyboard.c:431
uint32_t keyboard_dropped_count(void)
return the number of events dropped due to a full buffer since boot.
Definition keyboard.c:510
void keyboard_set_filter(keyboard_filter_fn filter)
set the filter (NULL: none).
Definition keyboard.c:498
void keyboard_handle_scancode(uint8_t scancode)
decode one byte of PS/2 scan code set 1 (0xE0 starts a two-byte code, bit 7 marks a release).
Definition keyboard.c:245
void keyboard_flush(void)
drop every buffered event (input typed but not read yet).
Definition keyboard.c:503
void keyboard_initialize(void)
initialize the keyboard driver and register the keyboard interrupt handler.
Definition keyboard.c:588
int keyboard_available(void)
check if at least one event is available in the event buffer.
Definition keyboard.c:426
int keyboard_read(void)
read the next character from the event buffer, blocking until one arrives.
Definition keyboard.c:485
keyboard_special_key_t
special key codes for keys that cannot be represented as a plain char.
Definition keyboard.h:20
@ KEY_ARROW_LEFT
Definition keyboard.h:23
@ KEY_ARROW_RIGHT
Definition keyboard.h:24
@ KEY_ARROW_DOWN
Definition keyboard.h:22
@ KEY_PAGE_UP
Definition keyboard.h:27
@ KEY_PAGE_DOWN
Definition keyboard.h:28
@ KEY_ARROW_UP
Definition keyboard.h:21
bool(*) keyboard_filter_fn(const keyboard_event_t *event)
a function that sees every key event before it is buffered (in the keyboard interrupt).
Definition keyboard.h:135
key_state_t
keyboard key state enumeration KEY_RELEASED: the key is not pressed KEY_PRESSED: the key is pressed
Definition keyboard.h:10
@ KEY_RELEASED
Definition keyboard.h:11
@ KEY_PRESSED
Definition keyboard.h:12
keyboard_event_type_t
event type tag for keyboard_event_t.
Definition keyboard.h:39
@ KEY_EVENT_SPECIAL
Definition keyboard.h:41
@ KEY_EVENT_RAW
Definition keyboard.h:42
@ KEY_EVENT_CHAR
Definition keyboard.h:40
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
#define KEY_DELETE
Definition input.h:152
#define KEY_END
Definition input.h:148
#define KEY_INSERT
Definition input.h:151
#define KEY_HOME
Definition input.h:143
#define bool
Definition stdbool.h:8
a single keyboard event returned by keyboard_read_event().
Definition keyboard.h:50
keyboard_event_type_t type
Definition keyboard.h:51
struct keyboard_event_t::@220350057347241133033123155260061373050020356372::@067114142330314354332232202345303076121063335252 raw