The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
keyboard.c File Reference

PS/2 keyboard driver implementation. More...

Macros

#define KEYBOARD_BUFFER_SIZE   128
#define EXT_RIGHT_ALT   0x38
#define EXT_RIGHT_CTRL   0x1D
#define SCANCODE_EXTENDED_PREFIX   0xE0

Enumerations

enum  keyboard_scancode_t {
  SCANCODE_LEFT_SHIFT = 0x2A , SCANCODE_RIGHT_SHIFT = 0x36 , SCANCODE_CAPS_LOCK = 0x3A , SCANCODE_LEFT_CTRL = 0x1D ,
  SCANCODE_LEFT_ALT = 0x38
}

Functions

void keyboard_set_raw_events (bool on)
 report every key press and release as a KEY_EVENT_RAW event (also the modifier keys), and no character or special events, or go back to the normal events.
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).
int keyboard_available (void)
 check if at least one event is available in the event buffer.
int keyboard_read_event (keyboard_event_t *event_out)
 read the next keyboard event from the event buffer without blocking.
bool keyboard_wait_event (keyboard_event_t *event_out)
 read the next keyboard event from the event buffer, blocking until one arrives.
char keyboard_getchar (void)
 read the next character from the event buffer without blocking.
int keyboard_read (void)
 read the next character from the event buffer, blocking until one arrives.
void keyboard_set_filter (keyboard_filter_fn filter)
 set the filter (NULL: none).
void keyboard_flush (void)
 drop every buffered event (input typed but not read yet).
uint32_t keyboard_dropped_count (void)
 return the number of events dropped due to a full buffer since boot.
void keyboard_initialize (void)
 initialize the keyboard driver and register the keyboard interrupt handler.

Detailed Description

PS/2 keyboard driver implementation.

This file implements a PS/2 keyboard driver that handles keyboard interrupts, processes scan codes, and provides an interface for reading keyboard events. It supports modifier keys (Shift, Ctrl, Alt, AltGr) and special keys (arrows, home, end, etc.) and maintains a circular buffer of keyboard events.

Macro Definition Documentation

◆ KEYBOARD_BUFFER_SIZE

#define KEYBOARD_BUFFER_SIZE   128

◆ EXT_RIGHT_ALT

#define EXT_RIGHT_ALT   0x38

◆ EXT_RIGHT_CTRL

#define EXT_RIGHT_CTRL   0x1D

◆ SCANCODE_EXTENDED_PREFIX

#define SCANCODE_EXTENDED_PREFIX   0xE0

Enumeration Type Documentation

◆ keyboard_scancode_t

Enumerator
SCANCODE_LEFT_SHIFT 
SCANCODE_RIGHT_SHIFT 
SCANCODE_CAPS_LOCK 
SCANCODE_LEFT_CTRL 
SCANCODE_LEFT_ALT 

Function Documentation

◆ keyboard_set_raw_events()

void keyboard_set_raw_events ( bool on)

report every key press and release as a KEY_EVENT_RAW event (also the modifier keys), and no character or special events, or go back to the normal events.

for games, which need key releases and the positions of the keys rather than characters. the terminal turns this on with the XKEYRAW termios flag.

◆ keyboard_handle_scancode()

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).

the PS/2 interrupt calls it, and the USB keyboard driver calls it with codes made from its reports. call with interrupts off.

Parameters
scancodethe byte.

◆ keyboard_available()

int keyboard_available ( void )

check if at least one event is available in the event buffer.

only a hint: another reader may take the event first, so callers that then read must use a read function that copes with an empty buffer.

Returns
1 if an event is available, 0 otherwise.

◆ keyboard_read_event()

int keyboard_read_event ( keyboard_event_t * event_out)

read the next keyboard event from the event buffer without blocking.

Parameters
event_outoutput structure filled with the event type and value.
Returns
1 if an event was read, 0 if the buffer was empty.

◆ keyboard_wait_event()

bool keyboard_wait_event ( keyboard_event_t * event_out)

read the next keyboard event from the event buffer, blocking until one arrives.

the calling process sleeps until the keyboard IRQ wakes it; it does not spin. safe with several readers: each event goes to exactly one of them.

Parameters
event_outoutput structure filled with the event type and value.
Returns
true with an event, false if a signal for the calling process ended the wait.

◆ keyboard_getchar()

char keyboard_getchar ( void )

read the next character from the event buffer without blocking.

special key events (arrow keys, etc.) are discarded; only KEY_EVENT_CHAR events are returned here. callers that need special keys should use keyboard_read_event() instead.

Returns
the next character, or 0 if no character event is available.

◆ keyboard_read()

int keyboard_read ( void )

read the next character from the event buffer, blocking until one arrives.

special key events are discarded the same way as keyboard_getchar().

Returns
the next character (0-255), or -1 if a signal for the calling process ended the wait.

◆ keyboard_set_filter()

void keyboard_set_filter ( keyboard_filter_fn filter)

set the filter (NULL: none).

◆ keyboard_flush()

void keyboard_flush ( void )

drop every buffered event (input typed but not read yet).

◆ keyboard_dropped_count()

uint32_t keyboard_dropped_count ( void )

return the number of events dropped due to a full buffer since boot.

Returns
total number of dropped events.

◆ keyboard_initialize()

void keyboard_initialize ( void )

initialize the keyboard driver and register the keyboard interrupt handler.

should be called after the IRQ subsystem is initialized.