Skip to content

Input System

NtinosTheGamer2324 edited this page Dec 11, 2025 · 1 revision

Input System

ModuOS supports keyboard and mouse input through PS/2 and USB interfaces.

PS/2 Driver

File: src/drivers/input/ps2/ps2.c

PS/2 Ports

#define PS2_DATA_PORT    0x60
#define PS2_STATUS_PORT  0x64
#define PS2_COMMAND_PORT 0x64

PS/2 Controller

Channels:

  • Port 1: Keyboard (IRQ 1)
  • Port 2: Mouse (IRQ 12)

Keyboard

Scan codes: Scan code Set 1 (XT compatible)

Common keys:

#define KEY_ESC         0x01
#define KEY_ENTER       0x1C
#define KEY_SPACE       0x39
#define KEY_BACKSPACE   0x0E
#define KEY_LEFT        0x4B
#define KEY_RIGHT       0x4D
#define KEY_UP          0x48
#define KEY_DOWN        0x50

Reading keyboard:

uint8_t scancode = inb(PS2_DATA_PORT);

// Convert to ASCII
char ch = scancode_to_ascii(scancode);

Mouse

Packet format (3 bytes):

Byte 0: Status flags
  Bit 0: Left button
  Bit 1: Right button
  Bit 2: Middle button
  Bit 4: X sign
  Bit 5: Y sign
Byte 1: X movement (-256 to +255)
Byte 2: Y movement (-256 to +255)

Reading mouse:

uint8_t status = mouse_packet[0];
int8_t dx = mouse_packet[1];
int8_t dy = mouse_packet[2];

bool left_button = status & 0x01;
bool right_button = status & 0x02;

Input Abstraction

File: src/drivers/input/input.c

Input Events

typedef enum {
    INPUT_EVENT_KEYPRESS,
    INPUT_EVENT_KEYRELEASE,
    INPUT_EVENT_MOUSE_MOVE,
    INPUT_EVENT_MOUSE_BUTTON
} input_event_type_t;

typedef struct {
    input_event_type_t type;
    union {
        struct {
            uint8_t scancode;
            char ascii;
        } key;
        struct {
            int dx, dy;
            uint8_t buttons;
        } mouse;
    };
} input_event_t;

Input API

// Initialize input system
int input_init(void);

// Get next input event (blocking)
int input_get_event(input_event_t *event);

// Check if input available
int input_has_event(void);

USB Input (Work in Progress)

Planned support:

  • USB keyboards (HID)
  • USB mice (HID)
  • Game controllers

Next Steps

Clone this wiki locally