# Writing an OS Kernel from Scratch – Part 30: Window Manager Mouse Support — Implementing Click, Drag, and Window Operations

“The keyboard is efficient, but the mouse is intuitive. Today, we integrate mouse support into the window manager, achieving true graphical interaction!”

In the previous article, we successfully implemented the PS/2 mouse driver and provided raw mouse events at /dev/input/mouse0. But the Window Manager (WM) is still keyboard-driven — users cannot click, drag, or close windows with the mouse.

Today, we will: ✅ Read mouse events in the user-mode window manager ✅ Implement window clicking and focus switching ✅ Support title bar dragging to move windows ✅ Add click handling for close/minimize buttons

Give your WM complete mouse interaction capability!


🖥️ 1. Architecture Integration: Mouse Event Flow

Event flow:

Key design:

Display Server is responsible for:
– Listening to /dev/input/mouse0
– Maintaining global mouse coordinates
– Routing events to Window Manager

Window Manager is responsible for:
– Determining click position
– Performing window operations (move, close, etc.)

💡 Display Server doesn’t handle window logic, only does event dispatch!


🖱️ 2. Display Server Mouse Integration

1. Start mouse listening thread
2. Maintain global mouse state


🖼️ 3. Window Manager Mouse Handling

1. WM main loop enhancement
2. Window region detection
3. Mouse event state machine


🎨 4. Enhanced Window Decorations

1. Draw interaction feedback
2. Draw mouse cursor


📡 5. Display Server Composition Flow

1. Composition order
2. Z-Order management


🧪 6. Test: Mouse Interaction Operations

User operation flow:

1. Move the mouse → screen displays crosshair cursor
2. Hover over the close button → button turns red
3. Click title bar and drag → window follows mouse movement
4. Drag the bottom-right corner → window size changes in real time
5. Click the close button → window closes, focus switches to next window

Run results:

Window movement/resize is smooth
Close button hover feedback is timely
Mouse cursor always stays on top
Multi-window stacking order is correct

✅ Mouse-driven window manager working perfectly!


⚙️ 7. Optimization Directions

1. Smooth animations: interpolation during move/resize, double buffering to avoid tearing
2. Mouse acceleration: dynamically adjust sensitivity based on movement speed, skip intermediate frames at high speed
3. Multi-monitor support: seamless mouse movement between monitors, independent window management per monitor
4. Gesture support: two-finger scrolling (needs touchpad driver), three-finger workspace switching

💡 Modern WMs (like KWin, Mutter) all provide these advanced features!


💬 Final Thoughts

Mouse support transforms a window manager from usable to delightful. It turns abstract window operations into intuitive visual feedback, greatly improving user experience.

The first draggable window you implemented today is the foundation of modern desktop environment interaction.

🌟 The best interaction makes users unaware of the technology, only enjoying the smooth operation.


📬 Hands-on challenge: Add double-click title bar to maximize window functionality, and implement window shadow effects. Share your mouse interaction video in the comments!

👇 Next article you’d like to see: Taskbar implementation, or window animations and effects?


#OS #KernelDevelopment #WindowManager #MouseSupport #GUI #WM #WritingFromScratch

Last modified: 2024年4月26日

Author

Comments

Write a Reply or Comment

Your email address will not be published.