menios icon indicating copy to clipboard operation
menios copied to clipboard

Implement PS/2 mouse driver and input support

Open pbalduino opened this issue 4 months ago • 0 comments

Summary

Implement PS/2 mouse driver to provide mouse input support alongside the existing PS/2 keyboard driver.

Description

Add PS/2 mouse driver functionality that detects, initializes, and processes mouse input events, providing both raw mouse data and processed mouse events for the system.

Current State

  • PS/2 keyboard driver operational in src/kernel/driver/ps2kb/
  • PS/2 controller initialization exists
  • No mouse detection or input processing
  • Applications cannot receive mouse input

Tasks

Phase 1: PS/2 Mouse Detection & Initialization

  • [ ] Add PS/2 mouse detection during PS/2 controller initialization
  • [ ] Implement mouse device identification (0x00, 0x03, 0x04 device IDs)
  • [ ] Add mouse initialization sequence (enable data reporting, set defaults)
  • [ ] Configure mouse sampling rate and resolution
  • [ ] Handle mouse detection failures gracefully

Phase 2: Mouse Data Processing

  • [ ] Implement mouse packet parsing (3-byte standard packets)
  • [ ] Handle mouse movement data (X/Y delta processing)
  • [ ] Process mouse button states (left, right, middle buttons)
  • [ ] Add scroll wheel support for wheel mice (4-byte packets)
  • [ ] Implement mouse coordinate scaling and acceleration

Phase 3: Input Event System Integration

  • [ ] Design mouse input event structure
  • [ ] Add mouse event buffering and queuing
  • [ ] Integrate with existing input system or create input subsystem
  • [ ] Support multiple input event consumers
  • [ ] Handle input event overflow conditions

Phase 4: Device Interface

  • [ ] Create /dev/mouse0 device file (integration with #140)
  • [ ] Implement mouse device file operations
  • [ ] Add mouse configuration ioctl operations
  • [ ] Support raw mouse data access
  • [ ] Add mouse device information reporting

Implementation Details

Mouse Packet Format (Standard 3-byte)

// PS/2 mouse packet structure
typedef struct ps2_mouse_packet {
    uint8_t flags;          // Button states and overflow flags
    int16_t delta_x;        // X movement delta
    int16_t delta_y;        // Y movement delta
} __attribute__((packed)) ps2_mouse_packet_t;

// Flags byte layout:
// Bit 7: Y overflow    Bit 6: X overflow
// Bit 5: Y sign        Bit 4: X sign
// Bit 3: Always 1      Bit 2: Middle button
// Bit 1: Right button  Bit 0: Left button

Mouse Event Structure

typedef struct mouse_event {
    uint64_t timestamp;     // Event timestamp
    int16_t delta_x;        // X movement
    int16_t delta_y;        // Y movement
    uint8_t buttons;        // Button state bitmask
    int8_t scroll_wheel;    // Scroll wheel delta (if supported)
} mouse_event_t;

PS/2 Controller Integration

// PS/2 mouse commands
#define PS2_MOUSE_SET_DEFAULTS      0xF6
#define PS2_MOUSE_ENABLE_REPORTING  0xF4
#define PS2_MOUSE_DISABLE_REPORTING 0xF5
#define PS2_MOUSE_SET_SAMPLE_RATE   0xF3
#define PS2_MOUSE_GET_DEVICE_ID     0xF2
#define PS2_MOUSE_SET_RESOLUTION    0xE8

// Integration with existing PS/2 code
extern void ps2_send_mouse_command(uint8_t command);
extern uint8_t ps2_read_mouse_data(void);

Interrupt Handler

  • Share IRQ12 handling with existing PS/2 infrastructure
  • Distinguish between keyboard and mouse interrupts
  • Process mouse packets on mouse interrupts
  • Generate mouse events for input system

Success Criteria

  • [ ] PS/2 mouse detected and initialized correctly
  • [ ] Mouse movement tracked accurately
  • [ ] Mouse button states reported correctly
  • [ ] Scroll wheel support for compatible mice
  • [ ] /dev/mouse0 device provides mouse access
  • [ ] Multiple applications can receive mouse events
  • [ ] Mouse input works with existing console system

Dependencies

  • Integrates with: PS/2 controller infrastructure (existing)
  • May integrate with: #140 (device filesystem for /dev/mouse0)
  • May integrate with: #32 (input subsystem if expanded)

Priority

Medium-High - Essential for GUI applications and improved user interaction

Files to Create/Modify

  • src/kernel/driver/ps2mouse/ps2mouse.c - PS/2 mouse driver
  • include/kernel/driver/ps2mouse.h - Mouse driver interface
  • src/kernel/driver/ps2kb/ps2kb.c - Integration with PS/2 controller
  • Mouse device file implementation (future integration)

Integration Notes

  • Must coexist with existing PS/2 keyboard driver
  • Should not interfere with keyboard functionality
  • Consider shared PS/2 controller resource management
  • Plan for future input event system expansion

pbalduino avatar Sep 29 '25 13:09 pbalduino