From TTY and PTY Internals to GPU-Accelerated Rendering
Introduction
- What You Will Learn
- Who This Book Is For
- Why Terminals Still Matter
- How to Read This Book
- Notation and Conventions
PART I: FOUNDATIONS
Chapter 1: The Evolution of Terminals
- From Teletype to ASCII
- The Rise of Dumb Terminals
- VT100 and the DEC Revolution
- The First Terminal Emulators
- Why History Stuck: Compatibility as a Force
Chapter 2: The Unix Terminal Model
- Terminal as Device
- The Shell as Terminal Client
- Standard Streams and Their Meaning
- The Pseudo-Terminal Abstraction
- A Mental Model for the Rest of the Book
Chapter 3: TTY and PTY Architecture
- The UNIX TTY Subsystem
- PTY Master and Slave
- PTY Allocation: from /dev/ptmx to pts
- File Descriptor Ownership and Inheritance
- PTY Lifecycle and Cleanup
PART II: OPERATING SYSTEM MECHANICS
Chapter 4: Sessions, Process Groups and Job Control
- Sessions and Controlling Terminals
- Process Groups and Foreground/Background
- Job Control Mechanics
- The Role of the Shell in Job Control
- TIOCSTI and Input Injection
Chapter 5: Terminal Modes and termios
- The termios Structure
- Input Processing and Canonical Mode
- Non-Canonical and Raw Modes
- Output Processing and Special Characters
- Line Disciplines Beyond the Default
Chapter 6: Signals and Terminal Interaction
- Terminal-Generated Signals
- Process-Generated and Terminal-Affecting Signals
- SIGHUP and Terminal Hangup
- SIGWINCH and Resize Notifications
- Signal Masks, Sessions and Process Groups
PART III: TERMINAL PROTOCOLS
Chapter 7: The VT100 and ANSI Heritage
- The ANSI Architecture
- DEC Private Mode Extensions
- The VT220: The Real Standard
- Compatibility Layers and Confusion
- What Modern Terminals Still Must Support
Chapter 8: Control Sequences and Escape Parsing
- The Escape Character Family
- CSI Sequences: Syntax and Semantics
- SGR: Graphics Rendition in Detail
- Cursor and Window Manipulation
- Parser State Machines and Ambiguities
Chapter 9: Colors, Graphics and Screen Manipulation
- 3-Bit, 4-Bit and 8-Bit Color
- 24-Bit True Color and True Color Negotiation
- ANSI Background Graphics (SGR 48)
- Alternate Screen Buffer Mechanics
- Scroll, Erase and Insert Operations
Chapter 10: Modern Protocol Extensions
- Mouse Reporting Modes
- Keyboard Protocol and Disambiguation
- Bracketed Paste Mode
- Hyperlinks (OSC 8) and URI Schemes
- Focus, Title and Property Control
PART IV: TEXT REPRESENTATION
Chapter 11: Characters, Encodings and Unicode
- From ASCII to UTF-8
- Code Points, Code Units and Grapheme Clusters
- Combining Marks and Canonical Composition
- East Asian Width and Column Calculation
- Terminal Code Page Translations
Chapter 12: Layout, Shaping and Complex Text
- The Fixed-Cell Constraint
- Grapheme Cluster Segmentation
- Bidirectional Text in a Grid
- Ligatures and Font Features
- Zero-Width and Invisible Characters
PART V: RENDERING
Chapter 13: Terminal Cell Models and Screen Buffers
- The Terminal Cell: Data Structure Design
- Primary and Alternate Screen Buffers
- Scrollback Storage Strategies
- Dirty Region Tracking
- Selection and Clipboard State
Chapter 14: The Rendering Pipeline
- Layout and Metrics Computation
- Glyph Selection and Font Fallback
- Rasterization and Texture Management
- Frame Scheduling and VSync
- Performance Profiling and Bottlenecks
Chapter 15: GPU-Accelerated Rendering
- Why GPU Rendering for Text
- OpenGL and Vulkan in Terminals
- GPU vs CPU Rendering Trade-offs
- Texture Atlases and Batch Drawing
- Cross-Platform Rendering Abstractions
PART VI: INPUT AND EVENTS
Chapter 16: Keyboard Input and Event Translation
- Keyboard Event Sources
- Modifier Key Tracking
- Key-to-Sequence Translation
- Escape Key Handling and Ambiguity
- Cross-Platform Keyboard Abstractions
Chapter 17: Mouse, Clipboard and Advanced Input
- Mouse Event Reporting to Programs
- Click-to-Select and Drag
- Clipboard and Primary Selection
- Drag-and-Drop Text
- Touch and Other Input Modalities
PART VII: THE MODERN TERMINAL ECOSYSTEM
Chapter 18: Terminal Emulator Architectures
- xterm: The Reference Implementation
- Alacritty: GPU-First Design
- Kitty: Protocol Innovation
- Ghostty: Modern Zig Architecture
- WezTerm: Scriptable and Flexible
Chapter 19: Multiplexers, SSH and Remote Terminals
- How tmux Works: Protocol and Architecture
- Screen: History and Design
- SSH and Terminal Emulation
- TERM, terminfo and Capability Negotiation
- Nested Sessions and Capability Conflicts
PART VIII: IMPLEMENTATION - BUILDING A TERMINAL FROM SCRATCH
Chapter 20: Project Foundation and PTY Setup
- Project Structure and Dependencies
- PTY Creation and Shell Launch
- Bidirectional I/O Between PTY and Shell
- Signal Handling for the Child Process
- The First Working Terminal
Chapter 21: Terminal State and Escape Sequence Parsing
- TerminalState: Data Structures
- Screen Buffer Implementation
- Escape Sequence Parser State Machine
Chapter 22: Screen Buffer Management and Text Rendering
- Cell Structure and Buffer Layout
- Scroll, Erase and Insert Operations
- UTF-8 Input Handling
- Window Integration and Display
- Complete Working Example
Chapter 23: Input Handling, Mouse and Polish
- Keyboard Event Translation to PTY
- Mouse Event Reporting
- Window Resize and SIGWINCH
- Putting It Together: The Final Main
- Alternate Screen Buffer Support
- Testing and Edge Cases