| Crates.io | e_grid |
| lib.rs | e_grid |
| version | 0.2.1 |
| created_at | 2025-06-22 04:25:48.48444+00 |
| updated_at | 2025-07-20 06:47:32.812901+00 |
| description | A real-time, multi-monitor window grid management and tracking system for Windows. |
| homepage | https://github.com/davehorner/e_grid/ |
| repository | https://github.com/davehorner/e_grid/ |
| max_upload_size | |
| id | 1721280 |
| size | 1,515,404 |
A comprehensive, event-driven window management system that provides real-time window tracking, grid-based positioning, and efficient IPC-based communication across multiple monitors.
7/25 - now exposes e_grid_all which will grid all windows except the initial focused window. All other windows are placed across all monitors in an "optimal" way. If you open or close a window, the monitor grids will grow or shrink to accommodate; the windows will animate to their target cells with a random ease type.
NOTICE: This repository is in an interesting state; the examples and functionality may or may not be implemented. Have a look at e_midi which includes an example of playing a midi sound using the focus and defocused events. Aside from that; Feel free to take a look at the LLM cruft, it will give you an idea of some of the directions this project could go.
Dave Horner 6/25 MIT/Apache License
┌─────────────────────┐ WinEvents ┌──────────────────────┐
│ Windows System │ ──────────────→ │ IPC Server Demo │
│ - Window Creation │ │ - Minimal callbacks │
│ - Window Movement │ │ - Main loop logic │
│ - Focus Changes │ │ - Window rescanning │
│ - Window Destroy │ │ - IPC publishing │
└─────────────────────┘ │ - Focus tracking │
└──────────┬───────────┘
│ iceoryx2 IPC
│ Multi-Service:
│ • Grid Events
│ • Window Details
│ • Focus Events ⭐
│ • Commands
│ • Responses
▼
┌─────────────────────────────────────────┐
│ Client Applications │
├─────────────────────┬───────────────────┤
│ Grid Client Demo │ Focus Demo Apps │
│ - Real-time grids │ - Focus tracking │
│ - Window details │ - Event logging │
│ - Throttled UI │ - App filtering │
│ - Non-blocking │ - Multi-client │
└─────────────────────┴───────────────────┘
Virtual Grid (All Monitors):
════════════════════════════════════════════════════════════════
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
1 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
2 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
3 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ··
4 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ██
5 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ██
6 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██ ·· ·· ·· ·· ·· ·· ·· ██ ██ ██ ██ ██
7 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ··
Monitor 0 Grid (1920x1080):
══════════════════════════════════════════════════════════
0 1 2 3 4 5 6 7 8 9 10 11
0 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
1 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
2 ·· ·· ·· ██ ██ ██ ██ ·· ·· ·· ·· ··
3 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ··
4 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██
5 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██
6 ██ ██ ██ ·· ·· ·· ·· ██ ██ ██ ██ ██
7 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ··
Active Windows:
██ Notepad [HWND: 12345678] (cells: 12)
██ File Explorer [HWND: 87654321] (cells: 20)
██ VS Code [HWND: 11223344] (cells: 16)
winapi for Windows API integrationArc<Mutex<>> for safe concurrent accessMajor Update: E-Grid now provides a single, intelligent e_grid binary that auto-detects your needs!
✨ Smart Auto-Detection:
e_grid server or e_grid client# One command does it all - auto-detects and starts appropriate mode
cargo run --bin e_grid
# Or use the built binary directly
./target/debug/e_grid
🎯 What you get:
Major Update: E-Grid now includes comprehensive window focus tracking directly integrated into the main server!
✨ Key Highlights:
# Quick Test - Focus Events with Main Server
cargo run --bin e_grid # Auto-starts server + client
cargo run --example simple_focus_demo # Terminal 2: Focus tracking client
# Now click between windows to see real-time FOCUSED/DEFOCUSED events!
📖 Jump to Focus Event Documentation
ipc_server_demo - Handles window tracking and event publishinggrid_client_demo - Real-time grid reconstruction and displayFocus Event Types:
E-Grid uses an intelligent coverage-based algorithm to determine which grid cells are occupied by windows. Instead of simple boundary checking, it calculates the actual intersection area between windows and grid cells.
// In src/lib.rs - adjustable coverage threshold
const COVERAGE_THRESHOLD: f32 = 0.3; // 30% coverage required
Threshold Options:
0.1 (10%) - Very sensitive, small overlaps count as occupation0.3 (30%) - Default, balanced approach for most use cases0.5 (50%) - Window must cover majority of cell to count0.8 (80%) - Very strict, window must nearly fill entire cellThe system consists of several key components:
lib.rs): Main window tracking and grid managementwindow_events.rs): Windows API integration for event detectionipc.rs): iceoryx2-based inter-process communicationipc_demo.rs): Command-line interface for window controlgit clone <repository>
cd e_grid
cargo build --release
cargo run --bin ipc_server_demo
This starts the main server that:
# In a separate terminal
cargo run --bin grid_client_demo
This starts the intelligent client that:
cargo run --bin ipc_demo
Original combined server/client for interactive window assignment.
E-Grid now includes comprehensive focus event tracking integrated directly into the main server. This allows applications to monitor window focus changes in real-time without needing a separate focus tracking server.
pub struct WindowFocusEvent {
pub event_type: u8, // 0 = FOCUSED, 1 = DEFOCUSED
pub hwnd: u64, // Window handle
pub process_id: u32, // Process ID
pub timestamp: u64, // Unix timestamp
pub app_name_hash: u64, // Hash of "Process_{pid}" for identification
pub window_title_hash: u64, // Hash of window title for identification
pub reserved: [u8; 2], // Future expansion
}
# Terminal 1: Start main server with focus events
cargo run --bin ipc_server_demo
# Terminal 2: Start focus demo client
cargo run --example simple_focus_demo
# Windows - Automated setup
test_focus_defocus.bat
# Or comprehensive integration test
test_focus_integration.bat
The system includes several focus tracking examples:
simple_focus_demo: Basic focus event monitoring with clear outputcomprehensive_focus_demo: Advanced focus tracking with filtering and statisticsfocus_tracking_demo: Demonstrates focus event callback patternsfocus_music_demo: Example integration with MIDI/music applicationsuse e_grid::GridClient;
let mut client = GridClient::new()?;
// Set up focus callback
client.set_focus_callback(|focus_event| {
match focus_event.event_type {
0 => println!("Window {} gained focus", focus_event.hwnd),
1 => println!("Window {} lost focus", focus_event.hwnd),
_ => {}
}
})?;
// Start monitoring
client.start_background_monitoring()?;
Key Architecture Features:
The e_grid server provides multiple IPC services for different types of communication:
| Service | Purpose | Message Type | Description |
|---|---|---|---|
| Grid Events | Window lifecycle | WindowEvent |
Window creation, destruction, movement |
| Window Details | Window information | WindowDetails |
Position, size, grid coordinates, titles |
| 🎯 Focus Events | Focus tracking | WindowFocusEvent |
Focus/defocus with process info ⭐ |
| Commands | Client requests | WindowCommand |
Window assignment, grid requests |
| Responses | Server replies | WindowResponse |
Command acknowledgments, data |
| Layout | Grid layouts | GridLayoutMessage |
Save/restore window arrangements |
| Animations | Window animations | AnimationCommand |
Smooth window transitions |
Multi-Client Support: Each service supports up to 8 concurrent subscribers with individual message buffers.
# Main server (recommended for all use cases)
cargo run --bin ipc_server_demo
# Legacy interactive server/client combo
cargo run --bin ipc_demo
# Real-time grid visualization
cargo run --bin grid_client_demo
# Enhanced grid client with better error handling
cargo run --example enhanced_grid_client
# Robust grid client with reconnection
cargo run --example robust_grid_client
# Simple focus event monitoring (great for testing)
cargo run --example simple_focus_demo
# Comprehensive focus tracking with filtering
cargo run --example comprehensive_focus_demo
# Focus event callback patterns
cargo run --example focus_tracking_demo
# Focus integration with music/MIDI applications
cargo run --example focus_music_demo
# Focus callback demonstration
cargo run --example focus_callback_example
Once the client is running, you can use these commands:
assign - Interactive window assignment
v for virtual grid (coordinates span all monitors)m for monitor-specific (coordinates relative to individual monitor)list command)list - Display all tracked windows with their HWNDsgrid - Show current grid state for all monitorsquit - Exit the client[CLIENT] > list
📤 Sent GetWindowList command
📤 [RESPONSE] Window List: 12 windows
[CLIENT] > assign
Choose assignment mode (v=virtual, m=monitor): v
Enter window HWND: 12345678
Enter target row (0-based): 2
Enter target column (0-based): 5
📤 Sent virtual assignment command: HWND 12345678 to (2, 5)
[CLIENT] > grid
📤 Sent GetGridState command
📊 Grid State: 12 windows, 8 occupied cells
simple_focus_demo)Perfect for testing and learning focus events
cargo run --example simple_focus_demo
Output Example:
🎯 Simple Focus Event Demo
==========================
🟢 FOCUSED - Window: 123456 (PID: 5678) at timestamp: 1640995200
📱 App Hash: 0x8a2f3c1b5d4e6789
🪟 Title Hash: 0x1b2c3d4e5f6a7890
─────────────────────────────
🔴 DEFOCUSED - Window: 123456 (PID: 5678) at timestamp: 1640995205
📱 App Hash: 0x8a2f3c1b5d4e6789
🪟 Title Hash: 0x1b2c3d4e5f6a7890
─────────────────────────────
comprehensive_focus_demo)Advanced focus tracking with filtering and statistics
focus_music_demo)Integration example for music/MIDI applications
Windows Focus Event
↓
EVENT_SYSTEM_FOREGROUND (WinEvent)
↓
GridIpcServer::handle_window_event()
↓
1. Send DEFOCUSED for previous window
2. Update last_focused_window tracking
3. Send FOCUSED for current window
↓
GRID_FOCUS_EVENTS_SERVICE (IPC)
↓
Client Applications (up to 8 simultaneous)
The server provides these interactive commands:
g / grid - Display all monitor grids
r / rescan - Force rescan of all windows
e / event - Publish demo IPC event
c / commands - Process demo IPC commands
h / help - Show available commands
q / quit - Exit the server
The test_event_driven_demo showcases E-Grid's event-driven architecture with real-time window management:
# Build and run the event-driven demo
cargo run --bin test_event_driven_demo
# Or use the convenient batch file
run_event_driven_demo.bat
This project demonstrates advanced Rust patterns for:
Perfect foundation for building sophisticated window management tools and desktop environments.
Problem: "When I assign a window to one cell, it shows up in multiple cells"
Solution: This has been resolved with the coverage-based detection system. The new algorithm:
Adjusting Sensitivity: If you need different behavior, modify the coverage threshold:
// In src/lib.rs - make more or less sensitive
const COVERAGE_THRESHOLD: f32 = 0.5; // Increase for stricter detection
const COVERAGE_THRESHOLD: f32 = 0.1; // Decrease for more sensitive detection
The grid now accurately reflects window positions:
NOTICE: This repository is in an interesting state; the examples and functionality may or may not be implemented. Have a look at the e_midi which includes an example of playing a midi sound using the focus and defocused events. Aside from that; Feel free to take a look at the LLM cruft, it will give you an idea of some of the directions this project could go.
Dave Horner 6/25 MIT/Apache License