💻 Client terminal display: TTY detection, frame rendering, and interactive/stdout output routing
More...
💻 Client terminal display: TTY detection, frame rendering, and interactive/stdout output routing
The display system supports dual output modes:
- Interactive TTY Mode: Direct terminal control with cursor positioning
- Redirect Mode: Plain text output for pipes and file redirection
- Snapshot Mode: Single frame capture with minimal terminal control
TTY Detection and Management
Implements robust TTY detection across platforms:
- Environment Variables: Check $TTY for explicit terminal path
- Standard Streams: Test stdin/stdout/stderr for TTY status
- Controlling Terminal: Fall back to /dev/tty (Unix) or CON (Windows)
- Validation: Verify TTY path accessibility and permissions
Terminal Control Sequences
Uses platform abstraction layer for terminal operations:
- Initialization: Set terminal to optimal display mode
- Cursor Management: Hide cursor and position for frame updates
- Screen Control: Clear screen and scrollback buffer
- Reset Operations: Restore terminal to original state
Frame Rendering Pipeline
Frame display follows a structured pipeline:
- Mode Detection: Determine output mode (TTY vs redirect)
- Cursor Positioning: Position cursor for frame update (TTY mode)
- Data Writing: Write frame data to appropriate file descriptor
- Synchronization: Ensure data reaches terminal (fsync for redirect)
- State Updates: Track frame dimensions and display state
Snapshot Mode Support
Special handling for single-frame capture:
- Timing Control: Coordinate with protocol for snapshot timing
- Output Routing: Final frame written to both TTY and stdout
- Format Control: Skip terminal control sequences in snapshot output
- Cleanup: Add newline terminator for proper file formatting
Platform Compatibility
Cross-platform terminal support:
- Unix/Linux: Uses /dev/tty and POSIX terminal I/O
- macOS: Enhanced TTY detection with $TTY environment variable
- Windows: CON device for console output with Windows Console API
- Error Handling: Graceful fallback for unsupported operations
Integration Points
- main.c: Display subsystem initialization and lifecycle management
- protocol.c: Frame data reception and rendering requests
- server.c: Terminal capability reporting and resize handling
- options.c: Display mode configuration from command line
Error Handling
Display errors handled with graceful degradation:
- TTY Access Errors: Fall back to stdout redirection mode
- Write Errors: Log errors but continue processing
- Terminal Control Errors: Skip control sequences, continue with data
- Permission Errors: Try alternative TTY paths before failing
- Author
- Zachary Fogg me@zf.nosp@m.o.gg
- Date
- September 2025
- Version
- 2.0
Definition in file display.c.