# Direct I/O & Graphics Substrates (`DMCS-1.0`) How to talk directly to display servers, terminals, and networks from raw machine code with **zero client libraries** (`libX11`, `libwayland-client`, `libc`, `SDL`, `GTK`, `Qt` are all unnecessary). --- ## 1. Raw X11 / Xwayland Wire Protocol (`/tmp/.X11-unix/X0`) Fastest path to a native desktop GUI window on Linux Xorg or Wayland+Xwayland (Hyprland, KDE, GNOME, Sway). ### Step A: Unix Socket Connect + 12-Byte Setup Handshake - `sockaddr_un` (20 bytes): `01 00` (`AF_UNIX`) + `"/tmp/.X11-unix/X0\0"`. - Setup request (12 bytes, no auth): `6c 00 0b 00 00 00 00 00 00 00 00 00` (`'l'` LSBFirst, proto `11.0`, auth_name_len `0`, auth_data_len `0`). - Read 8-byte header into `setup_buf`: - `setup_buf[0] == 1` → Success! (`0` = Failed / needs Xauthority, `2` = Authenticate). - Remaining bytes = `u16 setup_buf[6] * 4`. Read them into `setup_buf + 8`. - Parse Screen 0 fields (exact X11 core protocol offsets): - `res_base = u32 setup_buf[12]` → allocate `win_id = res_base + 1`, `gc_id = res_base + 2`. - `vendor_len = u16 setup_buf[24]`, `vpad = (vendor_len + 3) & ~3`. - `num_formats = u8 setup_buf[29]`. - `screen = setup_buf + 40 + vpad + num_formats * 8`: - `root_win = u32 screen[0]` - `root_visual = u32 screen[32]` - `root_depth = u8 screen[38]` ### Step B: Essential Window Requests (Sent Sequentially over the Socket) 1. **`CreateWindow` (Opcode `1`, 40 bytes = 10 dwords):** - `[0..3]`: `u8 1`, `u8 root_depth`, `u16 10` - `[4..7]`: `u32 win_id`, `[8..11]`: `u32 root_win` - `[12..15]`: `s16 x`, `s16 y`; `[16..19]`: `u16 W`, `u16 H` - `[20..23]`: `u16 border=0`, `u16 class=1 (InputOutput)` - `[24..27]`: `u32 root_visual` - `[28..31]`: `u32 value_mask = 0x00000802` (`CWBackPixel | CWEventMask`) - `[32..35]`: `u32 bg_pixel` (`0x00RRGGBB`) - `[36..39]`: `u32 event_mask` (`KeyPress=0x1 | ButtonPress=0x4 | ButtonRelease=0x8 | PointerMotion=0x40 | Exposure=0x8000`) 2. **`ChangeProperty` for Window Title (`WM_NAME`, Opcode `18`):** - `[0..3]`: `u8 18`, `u8 0 (Replace)`, `u16 (6 + ceil(len/4))` - `[4..7]`: `u32 win_id`, `[8..11]`: `u32 39 (WM_NAME)`, `[12..15]`: `u32 31 (STRING)` - `[16..19]`: `u8 8 (format)`, `3B pad`; `[20..23]`: `u32 len` - `[24..]`: ASCII title padded to 4-byte multiple. 3. **`ChangeProperty` for Fixed Floating Size (`WM_NORMAL_HINTS`, Opcode `18`, 96 bytes = 24 dwords):** - Crucial on tiling compositors (Hyprland, Sway, i3)! - `[0..3]`: `u8 18`, `u8 0`, `u16 24` - `[4..7]`: `u32 win_id`, `[8..11]`: `u32 40 (WM_NORMAL_HINTS)`, `[12..15]`: `u32 41 (WM_SIZE_HINTS)` - `[16..19]`: `u8 32 (format)`, `3B pad`; `[20..23]`: `u32 18` - Payload (72 bytes = 18 dwords): `d[0] = 0x30 (PMinSize|PMaxSize)`, `d[5] = min_w`, `d[6] = min_h`, `d[7] = max_w`, `d[8] = max_h`, rest `0`. 4. **`CreateGC` (Opcode `55`, 16 bytes) + `MapWindow` (Opcode `8`, 8 bytes):** - `CreateGC`: `37 00 04 00`, `gc_id`, `win_id`, `value_mask = 0`. - `MapWindow`: `08 00 02 00`, `win_id`. ### Step C: Flicker-Free Software Framebuffer Blitting (`PutImage`, Opcode `72`) Maintain a `W × H × 4` BGRA pixel buffer in `.bss` (`0x00RRGGBB` per pixel). Because standard X11 request length is `u16` in 4-byte units (max `262,140` bytes per request without `BIG-REQUESTS`), slice tall framebuffers into horizontal strips of height `SH` where `24 + W * SH * 4 <= 262,140`: - 24-byte `PutImage` header: - `[0..3]`: `u8 72`, `u8 2 (ZPixmap)`, `u16 ((24 + W*SH*4) / 4)` - `[4..7]`: `u32 win_id`, `[8..11]`: `u32 gc_id` - `[12..15]`: `u16 W`, `u16 SH` - `[16..19]`: `s16 dst_x=0`, `s16 dst_y` - `[20..23]`: `u8 left_pad=0`, `u8 root_depth`, `u16 0` - Send 24-byte header via `write_all`, then `W * SH * 4` raw pixel bytes via `write_all`. ### Step D: X11 32-Byte Events Read exact 32-byte packets (`ev[0] & 0x7F`): - `2` (`KeyPress`): `u8 ev[1]` = keycode, `u16 ev[28]` = modifier mask (bit 0 = Shift, bit 2 = Ctrl). - `4` (`ButtonPress`) / `5` (`ButtonRelease`): `u8 ev[1]` = button (`1`=left, `3`=right, `4/5`=wheel), `s16 ev[24]` = `x`, `s16 ev[26]` = `y`. - `6` (`MotionNotify`): `s16 ev[24]` = `x`, `s16 ev[26]` = `y`. - `12` (`Expose`): redraw when `u16 ev[16] == 0` (final expose in batch). - EOF (`read_exact == 0`): window closed by WM → `exit(0)`. --- ## 2. Native Wayland Wire Protocol (`$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY`) For pure Wayland environments **without** Xwayland. Remarkably clean binary protocol over `AF_UNIX` `SOCK_STREAM`! ### Wire Framing Every message is 4-byte aligned: - `u32 object_id` - `u32 (msg_size_bytes << 16) | opcode` - Arguments (integers, length-prefixed NUL-padded strings, `new_id`, and file descriptors passed out-of-band via `sendmsg` + `SCM_RIGHTS`). ### Minimal Window Bring-Up Sequence 1. Connect to `$XDG_RUNTIME_DIR/wayland-0` (or `wayland-1`). 2. `wl_display(1).get_registry(op=1, new_id=2)` + `wl_display(1).sync(op=0, new_id=3)`. 3. Read `wl_registry(2).global` events (`op=0`): capture global `name` for `"wl_compositor"`, `"wl_shm"`, `"xdg_wm_base"`, and `"wl_seat"`. 4. Bind them with `wl_registry(2).bind(op=0)` to fixed client IDs (`4=compositor`, `5=shm`, `6=xdg_wm_base`, `7=seat`). 5. Create surface: `wl_compositor(4).create_surface(op=0, new_id=8)` → `xdg_wm_base(6).get_xdg_surface(op=2, new_id=9, surface=8)` → `xdg_surface(9).get_toplevel(op=1, new_id=10)` → `wl_surface(8).commit(op=6)`. 6. Allocate shared pixel buffer via syscalls: `fd = memfd_create("fb", 1)` → `ftruncate(fd, W*H*4)` → `fb = mmap(0, W*H*4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)`. 7. Pass `fd` to compositor via `sendmsg` (`SCM_RIGHTS`) with `wl_shm(5).create_pool(op=0, new_id=11, size=W*H*4)` → `wl_shm_pool(11).create_buffer(op=0, new_id=12, offset=0, w=W, h=H, stride=W*4, format=1 /* XRGB8888 */)`. 8. On `xdg_surface(9).configure(op=0, serial)`: reply `xdg_surface(9).ack_configure(op=4, serial)`, render pixels into `fb`, send `wl_surface(8).attach(op=1, buf=12, 0, 0)` + `damage_buffer(op=9, 0, 0, W, H)` + `commit(op=6)`. 9. Always answer `xdg_wm_base(6).ping(op=0, serial)` with `pong(op=3, serial)`. --- ## 3. Linux Framebuffer (`/dev/fb0`) & ANSI Truecolor TTY - **`/dev/fb0` (Kiosk / Console / Embedded):** `openat(AT_FDCWD, "/dev/fb0", O_RDWR)` → `ioctl(fd, 0x4600 /* FBIOGET_VSCREENINFO */, info_buf)` → `mmap` framebuffer → draw directly with the exact same `fill_rect` / `draw_glyph` primitives! - **ANSI Truecolor TTY (Zero-GUI / SSH Dashboards):** Put stdin in raw mode via `ioctl(0, 0x5401 /* TCGETS */)` → clear `ICANON|ECHO` (`~0x0A`) → `ioctl(0, 0x5402 /* TCSETS */)`, emit `\x1b[2J\x1b[H` + `\x1b[48;2;R;G;Bm` half-block (`▀`) graphics and `poll` stdin + timers! --- ## 4. Direct Network Servers & Clients (`AF_INET` / `AF_UNIX`) - **Minimal HTTP/1.1 Server (~1.5 KB total ELF64):** `socket(2, 1, 0)` → `setsockopt(fd, 1, 2, &one, 4)` (`SO_REUSEADDR`) → `bind(fd, sockaddr_in{2, htons(port), 0}, 16)` → `listen(fd, 16)` → loop: `accept` → `read` request → parse method/path → `write_all` `"HTTP/1.1 200 OK\r\n..."` → `close`. - Combine with `poll(7)` (`timeout_ms = 16`) for real-time 60 FPS animations, clocks, system monitors, or networked GUI dashboards in a single thread with zero libraries. --- ## 5. Embedded `5×7` Bitmap Font Convention Zero external font dependencies: store 7 bytes per glyph (`bit 4` = leftmost pixel, `bit 0` = rightmost pixel). `22` glyphs = `154` bytes; full printable ASCII (`95` glyphs) = only `665` bytes! Render cleanly at any integer scale `S` (`2×`, `3×`, `4×`) by invoking `fill_rect(x + col*S, y + row*S, S, S, color)`.