# Writing an OS Kernel from Scratch – Part 29: Mouse Driver — Adding Pointer Control to Your GUI

“The keyboard is efficient, but the graphical interface can’t do without a mouse. Today, we implement a PS/2 mouse driver, enabling your window manager to support clicking and dragging!”

In the previous article, we implemented a keyboard-driven window manager. But a true GUI needs mouse support:

Clicking buttons
Dragging windows
Precise positioning

And the most classic mouse interface on x86 PCs is PS/2 (reusing the i8042 keyboard controller).

Today, we will: ✅ Parse the PS/2 mouse protocol ✅ Implement the i8042 controller driver ✅ Register the mouse device in devfs ✅ Integrate mouse events into the Display Server

Give your OS complete pointer control capability!


🔌 1. PS/2 Mouse Hardware Basics

PS/2 interface features:

6-pin Mini-DIN connector (mouse is typically green)
Serial communication: Clock line (CLK) + Data line (DATA)
Managed by the i8042 keyboard controller (ports 0x60/0x64)

i8042 ports:

Port Name Role
0x60 Data Port Read/write mouse/keyboard data
0x64 Status Port Read controller status
0x64 Command Port Send controller commands

Status register bits:
bit 0 (OBF): 1 = output buffer full (data readable)
bit 1 (IBF): 1 = input buffer full (do not write commands)
bit 5 (AUX_BSY): 1 = mouse busy

💡 PS/2 mouse uses the AUX port (separate from the main keyboard port)!


📡 2. PS/2 Mouse Protocol

Initialization flow:
1. Detect mouse presence
2. Enable mouse
3. Set sample rate/resolution
4. Enable data reporting

Mouse data packet format (3 bytes):

L/R/M: Left/Right/Middle button states (1=pressed)
X/Y sign: 1=negative direction (left/up)
Overflow bits: movement exceeds -255~255 range

✅ We only handle standard 3-byte packets (ignoring scroll wheel and other extensions).


⚙️ 3. i8042 Controller Driver

1. Wait for controller ready
2. Send AUX command
3. Initialize mouse


🖱️ 4. Mouse Interrupt Handling

1. Register IRQ12 interrupt
2. Interrupt handler

🔑 Interrupts only collect data; parsing is done in the user-mode thread (avoiding complex kernel logic)!


📁 5. Mouse Device File (/dev/input/mouse0)

1. Register character device
2. read system call
3. Mouse event structure


🖥️ 6. Display Server Mouse Integration

1. Start mouse listening thread
2. Maintain absolute coordinates


🧪 7. Test: Mouse-Controlled Window

Operation flow:

1. System boots → PS/2 mouse detected
2. Move mouse → screen displays crosshair cursor
3. Click window title bar → window gains focus (title turns blue)
4. Drag title bar → window follows mouse movement
5. Click close prompt → window closes

Run results:

PS/2 mouse movement smooth → immediate cursor response
Click title bar → window focus switches correctly
Drag to move → window precisely follows mouse
Close button click → window successfully closes, focus transfers to next window

✅ PS/2 mouse driver works perfectly!


⚠️ 8. Debugging Notes

1. i8042 responds slowly: wait for IBF=0 before issuing commands, wait for OBF=1 before reading data
2. Mouse packets may be misaligned: detect via the high bit of the first byte (bit 3 = 1 marks packet start)
3. QEMU PS/2 mouse needs to be enabled with -usb -device usb-tablet if using USB mouse


💬 Final Thoughts

From keyboard to mouse, from text to graphics — each step brings your OS closer to a modern, usable operating system. The PS/2 mouse driver isn’t just a driver; it’s the bridge between your OS and user interaction.

🌟 The real operating system isn’t about code, but about interaction.


📬 Hands-on challenge: Add scroll wheel support and enhance with acceleration curves. Share your driver debugging experience in the comments!

👇 Next article you’d like to see: Mouse-driven window management, or system console (Terminal Emulator) implementation?


#OS #KernelDevelopment #MouseDriver #PS2 #i8042 #DeviceDriver #GUI #WritingFromScratch

Last modified: 2024年5月1日

Author

Comments

Write a Reply or Comment

Your email address will not be published.